Results 1 to 4 of 4

Thread: eAlreadyInDb error in C# Autocad

  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

  2. #2
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,520
    Login to Give a bone
    0

    Default Re: eAlreadyInDb error in C# Autocad

    Welcome to AUGI.

    I think you need to read up on the AutoCAD .NET API.

    Consider using a Try { } Catch { } block and wrapping your DBText creation, Transaction, etc with Using { } respectively.

    Also, you already add the Database at the top of the method, so no need to repeatedly access same within your Foreach + animals2 loop (same for BlockTable, Transaction, etc).

    HTH
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3620, Core i7-7700K 4.2GHz, 64GB RAM, Samsung 970 Pro M.2, 8GB NVIDIA Quadro P4000

  3. #3
    Login to Give a bone
    0

    Default Re: eAlreadyInDb error in C# Autocad

    Quote Originally Posted by BlackBox View Post
    Welcome to AUGI.

    I think you need to read up on the AutoCAD .NET API.

    Consider using a Try { } Catch { } block and wrapping your DBText creation, Transaction, etc with Using { } respectively.

    Also, you already add the Database at the top of the method, so no need to repeatedly access same within your Foreach + animals2 loop (same for BlockTable, Transaction, etc).

    HTH
    Pls explain for more clarify, you can give me example ??

  4. #4
    Member
    Join Date
    2009-04
    Posts
    8
    Login to Give a bone
    0

    Default Re: eAlreadyInDb error in C# Autocad

    Quote Originally Posted by norman.yuan View Post
    I want to modify my attdef value but my code returns an error on sentence : "acBlktblRec.AppendEntity(acText);". error is : "eAlreadyInDb"

    ...
    namespace LinkedIn
    {
    public class Initialization : IExtensionApplication
    {
    #region Commands

    [CommandMethod("LISelectSet")]
    public void cmdEdSelectSet()
    {
    ....

    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

    }
    }

    [/code]
    I only wander into here occasionally.

    To me, it is quite obvious why the code results in"eAlreadyInDb" error, because your code create a new DBText object outside the "foreach..." loop, and then inside the "foreach ...", you change the DBText's TextString and try to add the same DBObject to ModelSpace in each loop, thus the error. If your actual intention is to create 3 DBObjects with different TextString value, you need to create DBObject inside the "foreach..." loop. Also, you should move the code that create ModelSpace BlockTableRecord out of "foreach ...", and get used to use "using..." to start a transaction. Something like:

    Code:
    ....
    using (var tran=db.TRansactionManager.StartTransaction())
    {
      string[] animals2 = new string[] { "deer", "moose", "boars" };
    
      BlockTable acBlkTbl = trans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
      BlockTableRecord acBlktblRec = trans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
    
      foreach (var item in animals2)
      {
        var acText=new DbText();
        acTest.SetDatabaseDefault();
        acText.Height=...
        acText.Position=...
        acText.TextString=item
    
        acBlktblRec.AppendEntity(acText)
        tran.AddNewlyCreatedObject(acText, true);
      }
    
      tran.Commit();
    }
    ....
    HTH

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
  •