Results 1 to 4 of 4

Thread: eAlreadyInDb error in C# Autocad

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Login to Give a bone
    0

    Default eAlreadyInDb error in C# Autocad

    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
    
        }
    }
    Last edited by rkmcswain; 2019-03-25 at 12:59 PM. Reason: added [CODE] tags

Similar Threads

  1. Replies: 1
    Last Post: 2017-02-21, 03:04 PM
  2. Replies: 1
    Last Post: 2017-02-20, 06:16 PM
  3. Replies: 1
    Last Post: 2012-11-12, 11:12 PM
  4. Replies: 1
    Last Post: 2012-09-20, 09:14 AM
  5. Replies: 7
    Last Post: 2006-11-03, 07:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •