Author Topic: [.NET C#] Xref DrawOrder eNoInternalSpace Error [Solved]  (Read 354 times)

0 Members and 1 Guest are viewing this topic.

SameOld

  • Mosquito
  • Posts: 2
[.NET C#] Xref DrawOrder eNoInternalSpace Error [Solved]
« on: February 16, 2024, 04:06:05 PM »
Hey guys

I'm trying to get all Xrefs in a specific order. The order is set through a list with all Xrefnames (order of list = draw order reversed).
It's getting the Ids of the Xrefs just fine, but on DrawOrderTable.MoveToBottom it gives the error eNoInternalSpace.
Does anyone have any idea why?

Thanks in advance

Code - C#: [Select]
  1.     [CommandMethod("XrefDrawOrder")]
  2.         public void XrefDrawOrder()
  3.         {
  4.             Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  5.             Database db = doc.Database;
  6.             Editor ed = doc.Editor;
  7.  
  8.             List<string> xrefNames = new List<string>() {
  9.             "FS-X",
  10.             "BS-SN-x",
  11.             "BS-x_BS-G-x",
  12.             "EE-x",
  13.             "IB-x_SA-x",
  14.             "BS-AV-x",
  15.             "BS-AV-x_1"
  16.             };
  17.  
  18.             try
  19.             {
  20.                 using (Transaction tr = db.TransactionManager.StartTransaction())
  21.                 {
  22.                     XrefGraph xg = db.GetHostDwgXrefGraph(true);
  23.  
  24.                     GraphNode root = xg.RootNode;
  25.  
  26.                     List<XrefGraphNode> nodes = new List<XrefGraphNode>();
  27.  
  28.                     for(int i = 0; i < root.NumOut; i++)
  29.                     {
  30.                         XrefGraphNode child = root.Out(i) as XrefGraphNode;
  31.  
  32.                         if(child.XrefStatus == XrefStatus.Resolved)
  33.                         {
  34.                             nodes.Add(child);
  35.                         }
  36.                     }
  37.  
  38.                     foreach(string xrefName in xrefNames)
  39.                     {
  40.                         //get id of xref, this works.
  41.                         ObjectId id = nodes.Where(x => x.Database.Filename.Contains(xrefName)).FirstOrDefault().BlockTableRecordId;
  42.                         ObjectIdCollection entityIds = new ObjectIdCollection
  43.                         {
  44.                             id
  45.                         };                        
  46.  
  47.                         if (id != null)
  48.                         {
  49.                             BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
  50.                             BlockTableRecord modelSpace = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
  51.                             // Get the DrawOrderTable for ModelSpace
  52.                             DrawOrderTable dot = tr.GetObject(modelSpace.DrawOrderTableId, OpenMode.ForWrite) as DrawOrderTable;
  53.  
  54.                             //error here
  55.                             dot.MoveToBottom(entityIds);
  56.                         }
  57.                     }
  58.                     tr.Commit();
  59.                 }
  60.             }
  61.             catch (Autodesk.AutoCAD.Runtime.Exception e)
  62.             {
  63.                 ed.WriteMessage("\nError during shit: " + e.Message);
  64.             }
  65.             finally { db.Dispose(); }
  66.         }
  67.     }

edit, Kerry :  [ code = csharp ] tag added
« Last Edit: February 17, 2024, 04:29:28 PM by kdub_nz »

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2140
  • class keyThumper<T>:ILazy<T>
Re: [.NET C#] Xref DrawOrder eNoInternalSpace Error
« Reply #1 on: February 16, 2024, 07:53:38 PM »
I wonder if this is still an issue ?
https://www.autodesk.com/support/technical/article/caas/sfdcarticles/sfdcarticles/draw-order-send-to-back-and-bring-to-front-do-not-work-in-autocad.html

You may need to go to AutoDesk for this question.
You'll need to provide a reproducible demo (code and drawing )

added: found this
https://forums.autodesk.com/t5/net/manage-xref-draworder/td-p/6407481
« Last Edit: February 16, 2024, 08:04:17 PM by kdub_nz »
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

SameOld

  • Mosquito
  • Posts: 2
Re: [.NET C#] Xref DrawOrder eNoInternalSpace Error
« Reply #2 on: February 17, 2024, 04:50:53 AM »
Thanks a lot for the second link! Dont know how you found that, cause I certainly didnt. =/

His solution was a bit convoluted but the takeaway was, that i had the wrong ObjectId. My working code looks like this now:


Code - C#: [Select]
  1. [CommandMethod("DrawOrder")]
  2.         public void DrawOrder()
  3.         {
  4.             Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  5.             Database db = doc.Database;
  6.             Editor ed = doc.Editor;
  7.  
  8.             //Initialise list of xrefnames in order. Would be better to have it in a file and load it in or something instead of hardcoding.
  9.             //I use movetobottom so the first in this list will be on top of the others
  10.             List<string> xrefNames = new List<string>() {
  11.             "FS-X",
  12.             "BS-SN-x",
  13.             "BS-x_BS-G-x",
  14.             "EE-x",
  15.             "IB-x_SA-x",
  16.             "BS-AV-x",
  17.             "BS-AV-x_1"
  18.             };
  19.  
  20.  
  21.             // Build a selection filter looking for 'INSERT' objects (includes blocks and xrefs) from modelspace (DXF code 67 = 0)
  22.             TypedValue[] tvs = { new TypedValue(0, "INSERT"), new TypedValue(67, 0) };
  23.             SelectionFilter sf = new SelectionFilter(tvs);
  24.  
  25.             // Get objects matching the selection filter.
  26.             PromptSelectionResult psr = ed.SelectAll(sf);
  27.  
  28.             // Initialise Dictionary -> so we can search with linq. Dict cause you cant find the name in Entity, but in the blocktablerecord
  29.             Dictionary<string, Entity> xRefDict = new Dictionary<string, Entity>();
  30.  
  31.             try
  32.             {
  33.                 if (psr.Status == PromptStatus.OK)
  34.                 {
  35.                     // Get the object Ids from the selection.
  36.                     ObjectId[] ids = psr.Value.GetObjectIds();
  37.  
  38.                     using (Transaction tr = db.TransactionManager.StartTransaction())
  39.                     {
  40.                         //get the modelSpace
  41.                         BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
  42.                         BlockTableRecord modelSpace = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
  43.                         // Get the DrawOrderTable for ModelSpace
  44.                         DrawOrderTable dot = tr.GetObject(modelSpace.DrawOrderTableId, OpenMode.ForWrite) as DrawOrderTable;
  45.  
  46.                         foreach (ObjectId id in ids)
  47.                         {
  48.                             // Get the block reference of the ID.
  49.                             BlockReference bref = tr.GetObject(id, OpenMode.ForRead, false, true) as BlockReference;
  50.                             // Get the block table record for the block reference.
  51.                             BlockTableRecord btr = bref.BlockTableRecord.GetObject(OpenMode.ForRead) as BlockTableRecord;
  52.                             // If the record is an external reference
  53.                             if (btr.IsFromExternalReference)
  54.                             {
  55.                                 // Get the entity for the record.
  56.                                 Entity ent = tr.GetObject(id, OpenMode.ForRead) as Entity;
  57.                                 //Add it with the name as key (used to search after)
  58.                                 xRefDict.Add(btr.Name, ent);
  59.                             }
  60.                         }
  61.  
  62.  
  63.                         foreach (string xrefName in xrefNames)
  64.                         {
  65.                             ObjectId id = new ObjectId();
  66.  
  67.                             try
  68.                             {
  69.                                 //get id of xref
  70.                                 id = xRefDict.Where(x => x.Key.Contains(xrefName)).FirstOrDefault().Value.ObjectId;
  71.                             }
  72.                             catch (Autodesk.AutoCAD.Runtime.Exception e)
  73.                             {
  74.                                 ed.WriteMessage("\nError during finding id in Dict: " + e.Message);
  75.                             }
  76.                             //Has to be an idcollection even tho i want it to just move a single one
  77.                             ObjectIdCollection entityIds = new ObjectIdCollection
  78.                             {
  79.                                 id
  80.                             };
  81.  
  82.                             if (id != null)
  83.                             {
  84.                                 dot.MoveToBottom(entityIds);
  85.                             }
  86.                            
  87.                         }
  88.                         tr.Commit();
  89.                     }
  90.                 }
  91.             }
  92.             catch (Autodesk.AutoCAD.Runtime.Exception e)
  93.             {
  94.                 ed.WriteMessage("\nError during dot.MoveToBottom: " + e.Message);
  95.             }
  96.             finally { db.Dispose(); }
  97.         }
  98.     }


edit, Kerry :  [ code = csharp ] tag added
« Last Edit: February 17, 2024, 04:33:09 PM by kdub_nz »

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2140
  • class keyThumper<T>:ILazy<T>
Re: [.NET C#] Xref DrawOrder eNoInternalSpace Error [Solved]
« Reply #3 on: February 17, 2024, 04:32:16 PM »
@SameOld

I found the problem interesting, and I'm pleased that you resolved it.

Regards,
Kerry
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.