<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Autodesk.AutoCAD.Runtime.Exception:“eAlreadyInDb” happened at code “blockTableRecord.AppendEntity(entity);” in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/autodesk-autocad-runtime-exception-ealreadyindb-happened-at-code/m-p/12691923#M4664</link>
    <description>Nice job, I have succeeded, many thanks!</description>
    <pubDate>Sun, 07 Apr 2024 02:06:23 GMT</pubDate>
    <dc:creator>1755384511</dc:creator>
    <dc:date>2024-04-07T02:06:23Z</dc:date>
    <item>
      <title>Autodesk.AutoCAD.Runtime.Exception:“eAlreadyInDb” happened at code “blockTableRecord.AppendEntity(entity);”</title>
      <link>https://forums.autodesk.com/t5/net-forum/autodesk-autocad-runtime-exception-ealreadyindb-happened-at-code/m-p/12683134#M4662</link>
      <description>&lt;P&gt;I&amp;nbsp; want to create&amp;nbsp; a block from some selected entities,&lt;/P&gt;&lt;P&gt;here is the code :&lt;/P&gt;&lt;P&gt;MAIN FUNCTION:&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;public class _ExtensionTest_{ 

 [CommandMethod(nameof(BlockCreateTEST023))]
  public void BlockCreateTEST023()
  {
      //AddEntityByDB aedb = new AddEntityByDB();
      Database database = HostApplicationServices.WorkingDatabase;
      Document document = Application.DocumentManager.MdiActiveDocument;
      Editor editor = document.Editor;
      String blockName = "BLOCK0001"; 

      var ccIds = document.SelectEntity().GetObjectIds() ;
      editor.WriteMessage($"{string.Join("+\n", ccIds)}  \n"); 
      var resID = ccIds.ToBlockTable(blockName);
      editor.WriteMessage($"resID:{resID}  ");
  }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;HELPER CODE:&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;public  static class BlockExtension
{ 
  public static SelectionSet SelectEntity(this Document document, SelectionFilter selectionFilter = null)
  {
      Editor editor = document.Editor;
      //----------- 
      PromptSelectionResult promptSelRes = editor.SelectImplied(); 
      ObjectId[] emptyObjectId = new ObjectId[0];
      editor.SetImpliedSelection(emptyObjectId); 
      if (promptSelRes.Status != PromptStatus.OK)
      {
          if (selectionFilter == null)
          {
              promptSelRes = editor.GetSelection();
          }
          else
          {
              promptSelRes = editor.GetSelection(selectionFilter);
          }
      }

      SelectionSet selectionSet = null;

      if (promptSelRes.Status == PromptStatus.OK)
      {
          selectionSet = promptSelRes.Value;

          editor.WriteMessage($"selected num :{selectionSet.Count.ToString()}");
      }
      else
      {
          editor.WriteMessage($"selected num :0");
      }
      return selectionSet;
  }
         
}

public  static class ObjectExtension
{

 public static ObjectId ToBlockTable(this IEnumerable&amp;lt;ObjectId&amp;gt; objectIds, string blockName)
 {
     var database =  HostApplicationServices.WorkingDatabase;
     ObjectId btrId = ObjectId.Null;
     using (Transaction trans = database.TransactionManager.StartTransaction())
     {
         BlockTable bt = (BlockTable)database.BlockTableId.GetObject(OpenMode.ForWrite);
         if (bt.Has(blockName)) { btrId = bt[blockName]; }
         if (!bt.Has(blockName))
         {
             using (BlockTableRecord btr = new BlockTableRecord() { Name = blockName })
             { 
                 foreach (var objectId in objectIds)
                 {
                     Entity ent = trans.GetObject(objectId, OpenMode.ForRead, true) as Entity; 
                     if (ent == null) continue;
                     btr.AppendEntity(ent);
                 }
                 btrId = bt.Add(btr);
                 trans.AddNewlyCreatedDBObject(btr, true);
             }
             trans.Commit();
         }
     }
     return btrId;
 }


}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I selected three circles created by handle , then I got an ERROR IT SAYS ：&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;Autodesk.AutoCAD.Runtime.Exception: eAlreadyInDb&lt;BR /&gt;在 Autodesk.AutoCAD.DatabaseServices.BlockTableRecord.AppendEntity(Entity entity)&lt;BR /&gt;在 SEPD.ACAD.Extension.BlockExtension.ToBlockTable(IEnumerable`1 objectIds, String blockName) 位置 D:\WORKSPACE\CODE_CAD\SEPD.ACAD.Extension\SEPD.ACAD.Extension\BlockExtension.cs:行号 226&lt;BR /&gt;在 SEPD.ACAD.Extension._ExtensionTest_.BlockCreate023() 位置 D:\WORKSPACE\CODE_CAD\SEPD.ACAD.Extension\SEPD.ACAD.Extension\_ExtensionTest_.cs:行号 645&lt;BR /&gt;在 Autodesk.AutoCAD.Runtime.CommandClass.InvokeWorker(MethodInfo mi, Object commandObject, Boolean bLispFunction)&lt;BR /&gt;在 Autodesk.AutoCAD.Runtime.CommandClass.InvokeWorkerWithExceptionFilter(MethodInfo mi, Object commandObject, Boolean bLispFunction)&lt;BR /&gt;在 Autodesk.AutoCAD.Runtime.PerDocumentCommandClass.Invoke(MethodInfo mi, Boolean bLispFunction)&lt;BR /&gt;在 Autodesk.AutoCAD.Runtime.CommandClass.CommandThunk.Invoke()&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;seems some problem heppend at code "btr.AppendEntity(ent)"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;TIP:&lt;/P&gt;&lt;P&gt;If I quit use&amp;nbsp;the SelectEntity Function , just create circle by code , then , everything is fine;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Apr 2024 06:30:38 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/autodesk-autocad-runtime-exception-ealreadyindb-happened-at-code/m-p/12683134#M4662</guid>
      <dc:creator>1755384511</dc:creator>
      <dc:date>2024-04-03T06:30:38Z</dc:date>
    </item>
    <item>
      <title>Re: Autodesk.AutoCAD.Runtime.Exception:“eAlreadyInDb” happened at code “blockTableRecord.AppendEntity(entity);”</title>
      <link>https://forums.autodesk.com/t5/net-forum/autodesk-autocad-runtime-exception-ealreadyindb-happened-at-code/m-p/12683786#M4663</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;The entities you're trying to add to the block definition (BlockTableRecord) are already owned by the BlockTableRecord of the space in which you selected them.&lt;/P&gt;
&lt;P&gt;You have to create a copy (clone) of each selected entity and append these clones to the block definition.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;public static ObjectId ToBlockTableRecord(this IEnumerable&amp;lt;ObjectId&amp;gt; objectIds, string blockName)
{
    var database = HostApplicationServices.WorkingDatabase;
    using (Transaction trans = database.TransactionManager.StartTransaction())
    {
        BlockTable bt = (BlockTable)trans.GetObject(database.BlockTableId, OpenMode.ForRead);

        // if the block definition already exists in the block table, return its ObjectId
        if (bt.Has(blockName))
            return bt[blockName];

        // Create a new block definition
        trans.GetObject(database.BlockTableId, OpenMode.ForWrite);
        var btr = new BlockTableRecord() { Name = blockName };
        var btrId = bt.Add(btr);
        trans.AddNewlyCreatedDBObject(btr, true);

        // Deep clone each entity in the block definition
        var mapping = new IdMapping();
        database.DeepCloneObjects(new ObjectIdCollection(objectIds.ToArray()), btrId, mapping, false);
        trans.Commit();
        return btrId;
    }
}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Apr 2024 11:41:37 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/autodesk-autocad-runtime-exception-ealreadyindb-happened-at-code/m-p/12683786#M4663</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2024-04-03T11:41:37Z</dc:date>
    </item>
    <item>
      <title>Re: Autodesk.AutoCAD.Runtime.Exception:“eAlreadyInDb” happened at code “blockTableRecord.AppendEntity(entity);”</title>
      <link>https://forums.autodesk.com/t5/net-forum/autodesk-autocad-runtime-exception-ealreadyindb-happened-at-code/m-p/12691923#M4664</link>
      <description>Nice job, I have succeeded, many thanks!</description>
      <pubDate>Sun, 07 Apr 2024 02:06:23 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/autodesk-autocad-runtime-exception-ealreadyindb-happened-at-code/m-p/12691923#M4664</guid>
      <dc:creator>1755384511</dc:creator>
      <dc:date>2024-04-07T02:06:23Z</dc:date>
    </item>
  </channel>
</rss>

