Author Topic: Explode Dimension with .NET  (Read 2937 times)

0 Members and 1 Guest are viewing this topic.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2165
  • class keyThumper<T>:ILazy<T>
Explode Dimension with .NET
« on: September 02, 2023, 09:00:09 PM »
for info:
response from https://forums.autodesk.com/t5/net/how-to-explode-dimstyle-with-net/m-p/12213503/thread-id/78487

Code - C#: [Select]
  1. [CommandMethod("EXPDim")]
  2.  
  3. public void ExplodeDimensions()
  4. {
  5.    Document doc = CadApp.DocumentManager.MdiActiveDocument;
  6.    Database db = doc.Database;
  7.    Editor ed = doc.Editor;
  8.  
  9.    RXClass @class = RXObject.GetClass(typeof(Dimension));
  10.    DBObjectCollection explodedObjects = new DBObjectCollection();
  11.  
  12.    PromptEntityOptions peo = new PromptEntityOptions(
  13.       "\nSelect a dimension to explode : ");
  14.    peo.SetRejectMessage($"Selected object is not an {@class.Name}");
  15.    peo.AddAllowedClass(typeof(Dimension), exactMatch: false);
  16.  
  17.    bool finished = false;
  18.  
  19.    while (!finished)
  20.    {
  21.       PromptEntityResult result = ed.GetEntity(peo);
  22.  
  23.       if (result.Status != PromptStatus.OK)
  24.       {
  25.          finished = true;
  26.          return;
  27.       }
  28.       using (var tr = db.TransactionManager.StartOpenCloseTransaction())
  29.       {
  30.          explodedObjects.Clear();
  31.  
  32.          Entity ent = (Entity)tr.GetObject(
  33.             result.ObjectId,
  34.             OpenMode.ForRead);
  35.  
  36.          ent.Explode(explodedObjects);
  37.  
  38.          // optional (EraseOriginal(ed))
  39.          //
  40.          if (explodedObjects.Count > 0 )
  41.          {
  42.             ent.UpgradeOpen();
  43.             ent.Erase();
  44.          }
  45.  
  46.          BlockTableRecord btr = (BlockTableRecord)tr.GetObject(
  47.             db.CurrentSpaceId,
  48.             OpenMode.ForWrite);
  49.  
  50.          foreach (DBObject obj in explodedObjects)
  51.          {
  52.             Entity explodedEntity = (Entity)obj;
  53.             if(explodedEntity is MText)
  54.             {
  55.                // just for giggles, to visually identify dimensions processed.
  56.                explodedEntity.ColorIndex = 211;
  57.             }
  58.  
  59.             btr.AppendEntity(explodedEntity);
  60.             tr.AddNewlyCreatedDBObject(explodedEntity, true);
  61.          }
  62.          tr.Commit();
  63.       }
  64.    }
  65. }
  66.  
  67. internal bool EraseOriginal(Editor ed)
  68. {
  69.    PromptKeywordOptions pko = new PromptKeywordOptions("\nErase original objects?")
  70.    {
  71.       AllowNone = true
  72.    };
  73.    pko.Keywords.Add("Yes");
  74.    pko.Keywords.Add("No");
  75.    pko.Keywords.Default = "Yes";
  76.  
  77.    PromptResult pkr = ed.GetKeywords(pko);
  78.    if (pkr.Status != PromptStatus.OK) return false;
  79.  
  80.    return (pkr.StringResult == "Yes");
  81. }
  82.  
  83.  

>>>>
Perhaps have a play with this.
The PromptEntityOptions will select anything derived from Dimension.
Will be fairly simple to modify if you want to select multiple objects at one time.
This routine has had minimal testing, so review the code and test.

Regards,

 
added:
I've used a single entity selection cycle for this ,
because I don't see the use-case for having a multi-select option,

Can anyone offer a use-case for exploding multiple dimensions ??
Seems to me it bashes the integrity resulting from 'conventional' associative dimensions.

« Last Edit: September 02, 2023, 10:58:48 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.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2165
  • class keyThumper<T>:ILazy<T>
Re: Explode Dimension with .NET
« Reply #1 on: September 03, 2023, 11:02:47 PM »
the forum software at AutoDesk doesn't like the variable name
Code - C#: [Select]
  1. @class

it thinks it was a forum member username perhaps . . . anyway it just forgot to display the variable name in the declaration statement in the code pane  :)  :|

refactored to rxClass  !
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.