Explode within pre-selected coordinates via doc.Editor.Command ?

Explode within pre-selected coordinates via doc.Editor.Command ?

h3nrik
Contributor Contributor
268 Views
1 Reply
Message 1 of 2

Explode within pre-selected coordinates via doc.Editor.Command ?

h3nrik
Contributor
Contributor

Hi


I have this piece of code that works , values[0] and [1] are like 400,200 500,0 that are read from a text-file. So the script can go in and erase one time or eight times at different locations depending on data in text-file

 

 

 

ed = Application.DocumentManager.MdiActiveDocument.Editor;
ed.Command("ERASE, "CROSS", values[0].Trim(), values[1].Trim(), "");

 

 

 


Now I want to explode also, and thought I could just do

 

 

 

ed.Command("EXPLODE", "CROSS", values[0].Trim(), values[1].Trim(), "");

 

 

But it doesn't work at all, I get this output in the command window. But if I type out the commands manually it works fine..

 

 

Select object: 4000,2000
None found.
Select object: 0,0

 

 

Googling around all example I find is about exploading blocks with certain names or based on user selection, none which will work for me.

What am I doing wrong and what are my options?

 

Edit1:
Full method that I use to test, with static coordinates

 [CommandMethod("valitExplodeTest")]
        public static void ExplodeTest() {
            var doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            try {
                doc.Editor.ZoomExtents();
                ed.Command("EXPLODE", "4000,2000","0,0", "");
            } catch (Autodesk.AutoCAD.Runtime.Exception e) {
                ed.WriteMessage(e.Message);
            }
        }

Edit2:

I tried to change the command to some variants, and tried just this

ed.Command("EXPLODE");

Which starts the EXPLODE command but I cannot select coordinates as I can if I type in EXPLODE myself, so comparing the arguments that EXPLODE is expecting:
Manual: Expects a point or Window/Last/Crossing/BOX/ALL/Fence/WPolygon/CPolygon/Group/Add/Remove/Multiple/Previous/Undo/AUto/SIngle
From DLL: Expects a point or Last/ALL/Group

Both at least have point..and both have ALL. So I tried all

ed.Command("EXPLODE", "ALL","");

The object is not able to be exploded.
The object is not able to be exploded.
The object is not able to be exploded.
The object is not able to be exploded.
The object is not able to be exploded.
The object is not able to be exploded.
The object is not able to be exploded.
The object is not able to be exploded.
The object is not able to be exploded.
The object is not able to be exploded.
The object is not able to be exploded.
The object is not able to be exploded.
The object is not able to be exploded.
The object is not able to be exploded.

 

So it cannot explode anything...
Do I need to open some transaction or BlockTable or something before running explode? Ideas?

 

0 Likes
Accepted solutions (1)
269 Views
1 Reply
Reply (1)
Message 2 of 2

h3nrik
Contributor
Contributor
Accepted solution

After a good night sleep and more searching I found this post, https://forums.autodesk.com/t5/net/explode-command-by-c/td-p/9774112
Which was quite close and after copy-paste-glue I got something that looks like it is working just fine

Document document = Application.DocumentManager.MdiActiveDocument;
            Editor editor = Application.DocumentManager.MdiActiveDocument.Editor;
            
            using (Transaction transaction = document.Database.TransactionManager.StartTransaction()) {
                try {
                    BlockTable blockTable = transaction.GetObject(document.Database.BlockTableId, OpenMode.ForRead) as BlockTable;
                    BlockTableRecord blockTableRecord = transaction.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                    PromptSelectionResult selectionResult = editor.SelectCrossingWindow(new Point3d(4000, 2000,0), new Point3d(0, 0, 0));
                    if (selectionResult.Status == PromptStatus.OK) {
                        foreach (ObjectId objectId in selectionResult.Value.GetObjectIds()) {
                            Entity entity = transaction.GetObject(objectId, OpenMode.ForRead) as Entity;
                            using (DBObjectCollection objectCollection = new DBObjectCollection()) {
                                entity.Explode(objectCollection);

                                foreach (DBObject obj in objectCollection) {
                                    Entity explodedEntity = obj as Entity;
                                    blockTableRecord.AppendEntity(explodedEntity);
                                    transaction.AddNewlyCreatedDBObject(explodedEntity, true);
                                }
                                if (objectCollection.Count > 0) {
                                    entity.UpgradeOpen();
                                    entity.Erase(true);
                                }
                            }
                        }
                    }
                    transaction.Commit();
                } catch (Autodesk.AutoCAD.Runtime.Exception ex) {
                    editor.WriteMessage("\nError " + ex.Message);
                    transaction.Abort();
                }
            }

 

0 Likes