Explode results and selection results differ on computers

Explode results and selection results differ on computers

h3nrik
Contributor Contributor
726 Views
5 Replies
Message 1 of 6

Explode results and selection results differ on computers

h3nrik
Contributor
Contributor

Hi

Am at a loss at the moment (again, as usual..)

Same DWG located on a network share

Using this method (update since first post) to explode within a selection

 

 

 

        public static bool WithinSelection(SelectionSet selection) {
            Document document = Application.DocumentManager.MdiActiveDocument;
            Editor editor = Application.DocumentManager.MdiActiveDocument.Editor;
            editor.WriteMessage("\nAtiveViewPortId: " + editor.ActiveViewportId + ", CurrentViewportObjectId: " + editor.CurrentViewportObjectId);
            var somthingExploded = false;
            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;
                    foreach (ObjectId objectId in selection.GetObjectIds()) {
                        Entity selectedEntity = transaction.GetObject(objectId, OpenMode.ForRead) as Entity;
                        if (selectedEntity == null) {
                            editor.WriteMessage("\nNull entity with " + objectId.ToString());
                            continue;
                        }
                        if (selectedEntity is BlockReference selectedBlockReference) {
                            editor.WriteMessage("\nSelected handle:" + selectedEntity.Id.Handle + ", Name: " + selectedBlockReference.Name + ", BlockName:" + selectedBlockReference.BlockName);
                            using (DBObjectCollection resultOfExplode = new DBObjectCollection()) {
                                try {
                                    selectedEntity.Explode(resultOfExplode);
                                    foreach (DBObject obj in resultOfExplode) {
                                        Entity explodedEntity = obj as Entity;
                                        blockTableRecord.AppendEntity(explodedEntity);
                                        transaction.AddNewlyCreatedDBObject(explodedEntity, true);
                                        //editor.WriteMessage("\nAdding exploded into transaction " + explodedEntity.Id.Handle + " - " + explodedEntity);
                                    }
                                    if (resultOfExplode.Count > 0) {
                                        editor.WriteMessage("\nExploded " + resultOfExplode.Count.ToString());
                                        selectedEntity.UpgradeOpen();
                                        selectedEntity.Erase(true);
                                        somthingExploded = true;
                                    } else {
                                        editor.WriteMessage("\nNothing exploded");
                                    }
                                } catch (Autodesk.AutoCAD.Runtime.Exception ex) {
                                    editor.WriteMessage("\nError 1 " + ex.Message + " " + ex.StackTrace + "\nEntity: " + selectedEntity);
                                }
                            }
                        } else {
                            editor.WriteMessage("\nSelected handle is not a block:" + selectedEntity.Id.Handle + " - " + selectedEntity);
                            continue;
                        }
                    }
                    editor.WriteMessage("\nCommit transactoin");
                    transaction.Commit();
                } catch (Autodesk.AutoCAD.Runtime.Exception ex) {
                    editor.WriteMessage("\nError 2 " + ex.Message + " " + ex.StackTrace);
                    transaction.Abort();
                }
            }
            return somthingExploded;
        }

 

Called like this two times in a row with the same coordinates

var selectionResult = editor.SelectCrossingWindow(new Point3d(x1, y1, 0), new Point3d(x2, y2, 0));
            if (selectionResult.Status != PromptStatus.OK) {
                editor.WriteMessage("\nInvalid selection - " + selectionResult.Status);
                return;
            }
            var result = Explode.WithinSelection(selectionResult.Value);

 

 

 

Computer 1 yields this

AtiveViewPortId: (2569554406032), CurrentViewportObjectId: (0)
Selected handle:9695, Name: PICT_OWNER, BlockName:*Model_Space
Exploded 2
Commit transactoin

AtiveViewPortId: (2569554406032), CurrentViewportObjectId: (0)
Selected handle:9754, Name: BackingSheet, BlockName:*Model_Space
Exploded 325
Selected handle is not a block:974E - Autodesk.AutoCAD.DatabaseServices.Polyline2d
Commit transactoin


Computer 2
AtiveViewPortId: (1948773767824), CurrentViewportObjectId: (0)
Selected handle:9695, Name: PICT_OWNER, BlockName:*Model_Space
Exploded 2
Commit transactoin
True
Invalid selection - Error



What steps can I take to try and figure out why the second explode fails on computer 2? Can I get more details as to why the selection is an Error? My best clue so far is that if I call a different CommandMethod, that will execute the same WithinSelection-method, it works but the handles on selected and exploded entities are not the same as Computer 1..
Selected handle:97CA - Autodesk.AutoCAD.DatabaseServices.BlockReference
Exploded into 325
Selected handle:97C4 - Autodesk.AutoCAD.DatabaseServices.Polyline2d
Selected is not BlockReference, skipping
Commit transactoin

0 Likes
Accepted solutions (1)
727 Views
5 Replies
Replies (5)
Message 2 of 6

norman.yuan
Mentor
Mentor

You did not show how the PromptSelectionResult, which is passed into the WithinSelection() method, is obtained, but it seems implying the PromptSelectionResult is returned by calling Editor.Select[Crossing]Window() method.

 

The issue you came across ("invalid selection") clearly indicates there is nothing selected. It is not a sound coding practice by passing PromptSelectResult as parameter to the method. You should check the PromptResult status first, and ONLY CALL the exploding method if the PromptResult.Status is OK and passing the PromptSelectionResult.Value to the method, meaning there is indeed at least one entity is selected for exploding.

 

It is very possible that the selecting window is not within a current view, thus the selecting was not done correctly. Every AutoCAD programmer should know that when doing selecting with window/polygon/fence by calling API (be it .NET API, COM API, or LISP) the selecting window/polygon/fence... MUST BE within current view, or the selecting would failed, or not selecting correctly.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 6

h3nrik
Contributor
Contributor

"but it seems implying the PromptSelectionResult is returned by calling Editor.Select[Crossing]Window() method."
Yes

Ok - thanks for the correction, I will update my code to follow best practice. Hopefully it will shed some light on why different computers get different selections..*** Update is done and reflected in first post. Still same issue.

Follow up, how do I check which view I am currently trying to do the selection in? editor.ActiveViewportId?

 

Noticed now that one computer is running AutoCad 2019 and the other is running AutoCad Mechanical 2019 - can that be the source of the issue?

0 Likes
Message 4 of 6

h3nrik
Contributor
Contributor

So I made a new test method to call the explode-method, just hard coding the coordinates

[CommandMethod("valitExplodeTest2")]
        public static void ExplodeTest2() {
            Editor editor = Application.DocumentManager.MdiActiveDocument.Editor;
            editor.WriteMessage("\nalitExplodeTest2 AtiveViewPortId: " + editor.ActiveViewportId + ", CurrentViewportObjectId: " + editor.CurrentViewportObjectId);
            var selectionResult = editor.SelectCrossingWindow(new Point3d(4000, 2000, 0), new Point3d(0, 0, 0));
            if (selectionResult.Status != PromptStatus.OK) {
                editor.WriteMessage("\nInvalid selection - " + selectionResult.Status);
                return;
            }
            var result = Explode.WithinSelection(selectionResult.Value);
            editor.WriteMessage("\n" + result);
        }

Automatic run, calling valitExplodeTest2 two times in a row yield this
alitExplodeTest2 AtiveViewPortId: (1462350414480), CurrentViewportObjectId: (0)
WithinSelection AtiveViewPortId: (1462350414480), CurrentViewportObjectId: (0)
Selected handle:9695, Name: PICT_OWNER, BlockName:*Model_Space
Exploded 2
Commit transactoin
alitExplodeTest2 AtiveViewPortId: (1462350414480), CurrentViewportObjectId: (0)
Invalid selection - Error

 

If I then call the function myself one more time it magically works..why? 😄
alitExplodeTest2 AtiveViewPortId: (1462350414480), CurrentViewportObjectId: (0)
WithinSelection AtiveViewPortId: (1462350414480), CurrentViewportObjectId: (0)
Selected handle:97CA, Name: BackingSheet, BlockName:*Model_Space
Exploded 325
Selected handle is not a block:97C4 - Autodesk.AutoCAD.DatabaseServices.Polyline2d
Commit transactoin

0 Likes
Message 5 of 6

h3nrik
Contributor
Contributor

Included example DWG and complete C# source code (.txt files are .cs files).

I start the process like this:
"C:\Program Files\Autodesk\AutoCAD 2019\acad.exe" /b C:\temp\acadissue\start.scr

start.scr is:

secureload 0
netload "V:\AVEVA\MACRO_LEAP-E3D\VALIT_ISO-DFLTS_2\VALISO\DWG_Extension\dev_dll\ValIT_AcadAppendBlocks.dll"
sampleReadListOfDwg "C:\temp\acadissue\_listOfDwgs.txt"

_listOfDwgs.txt is just lines with full file path to files that should be opened and processed, in this time and moment, only
C:\temp\acadissue\f-n-block.dwg

 

0 Likes
Message 6 of 6

h3nrik
Contributor
Contributor
Accepted solution

Starting AutoCad Mechanical with vanilla profile solved my issues
"C:\Program Files\Autodesk\AutoCAD 2023\acad.exe" /p "<<VANILLA>>" /b ...

0 Likes