I want to modify my attdef value but my code returns an error on sentence : "acBlktblRec.AppendEntity(acText);". error is : "eAlreadyInDb"
This is my code:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices;
using System.Reflection;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
namespace LinkedIn
{
public class Initialization : IExtensionApplication
{
#region Commands
[CommandMethod("LISelectSet")]
public void cmdEdSelectSet()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
Database db = HostApplicationServices.WorkingDatabase;
// get the value enterd from user
PromptStringOptions pstringopt = new PromptStringOptions("\nEnter the string:");
//pstringopt.Message = "\nEnter the string";
pstringopt.AllowSpaces = true;
string ResStringInput;
PromptResult prstringress = ed.GetString(pstringopt);
if (prstringress.Status != PromptStatus.OK) return;
ResStringInput = prstringress.StringResult;
PromptSelectionOptions prSelOpts = new PromptSelectionOptions();
prSelOpts.AllowDuplicates = false;
prSelOpts.MessageForAdding = "\nSelect dimention to measure";
prSelOpts.MessageForRemoval = "\nselect dimention to remove from selection";
prSelOpts.RejectObjectsFromNonCurrentSpace = false;
prSelOpts.RejectObjectsOnLockedLayers = true;
//prSelOpts.RejectPaperspaceViewport = true;
List<string> list = new List<string>();
TypedValue[] tValues = new TypedValue[1];
tValues[0] = new TypedValue((int)DxfCode.Start, "Dimension");
SelectionFilter selfil = new SelectionFilter(tValues);
PromptSelectionResult prSelRes = ed.GetSelection(prSelOpts, selfil);
if (prSelRes.Status != PromptStatus.OK) return;
Transaction trans = db.TransactionManager.StartTransaction();
SelectionSet selSet = prSelRes.Value;
Dimension dimentionobj;
double lenTotal = 0.0;
string lenelement;
//List<string> list = new List<string>();
foreach (SelectedObject selObj in selSet)
{
dimentionobj = trans.GetObject(selObj.ObjectId, OpenMode.ForRead) as Dimension;
lenTotal += dimentionobj.Measurement;
lenelement = ResStringInput + " x " + dimentionobj.Measurement.ToString();
list.Add(lenelement);
}
DBText acText = new DBText();
acText.SetDatabaseDefaults();
string[] terms = list.ToArray();
string[] animals2 = new string[] { "deer", "moose", "boars" };
int n = 0;
foreach (String item in animals2)
{
Database acCurDb = Application.DocumentManager.MdiActiveDocument.Database;
BlockTable acBlkTbl;
acBlkTbl = trans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord acBlktblRec;
acBlktblRec = trans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
ed.WriteMessage(item);
acText.Position = new Point3d(23, 11 + n * 4, 0);
acText.Height = 3.5;
acText.TextString = item;
n = n + 1;
acBlktblRec.AppendEntity(acText);
trans.AddNewlyCreatedDBObject(acText, true);
}
trans.Commit();
}
#endregion
#region Initialization
void IExtensionApplication.Initialize()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
AssemblyName appName = Assembly.GetExecutingAssembly().GetName();
object[] attrs = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
ed.WriteMessage(((AssemblyTitleAttribute)attrs[0]).Title + " " +
appName.Version.Major + "." + appName.Version.MajorRevision + "." +
appName.Version.Minor + " is loaded...");
}
void IExtensionApplication.Terminate()
{
}
#endregion
}
}