.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

VB.NET Selection Set Convert VBA to .NET

1 REPLY 1
Reply
Message 1 of 2
cyconnely
1214 Views, 1 Reply

VB.NET Selection Set Convert VBA to .NET

So currently I'm trying to select objects that I am adding to the drawing. I want to add these to a selection set and then access that selection set through Interop. There is very little on the internet that I could find on doing something like this, but the command that I though might work, gives me an error,

acSSet2 = SelectionSet.FromObjectIds(ids)

 Here is a very summarized version of my code:

                    'Add the mirrored items to a new selection set
                    Dim acSSet2 As SelectionSet
                    Dim ids(15) As ObjectId
                    Dim int As Integer = 0
                    '' Step through the objects in the selection set
                    For Each acSSObj As SelectedObject In acSSet
                        
                        ' Add the new object to the block table record and the transaction
                        acBlkTblRec.AppendEntity(acEnt2)
                        acTrans.AddNewlyCreatedDBObject(acEnt2, True)
                        ids(int) = (acEnt2.ObjectId)
                        int = int + 1

                    Next
                    acSSet2 = SelectionSet.FromObjectIds(ids)


                        acadDoc = AcadApp.ActiveDocument

                        ssetObj = AcadApp.ActiveDocument.ActiveSelectionSet

                    acadDoc.Export(System.IO.Path.GetFullPath(acCurDb.Filename), "wmf", CType(acSSet2, AcadSelectionSet))

 I was hoping that when I added the items to acSSet2, AcadApp.ActiveDocument.ActiveSelectionSet would catch it and use it. But that didn't work so I added,

CType(acSSet2, AcadSelectionSet)

 but then it gave me this error

************** Exception Text **************
System.InvalidCastException: Unable to cast object of type 'Autodesk.AutoCAD.EditorInput.SelectionSetDelayMarshalled' to type 'Autodesk.AutoCAD.Interop.AcadSelectionSet'.
   at TestSelectingFiltering.TestSelecting.MyCommands.FilterBlueCircleOnLayer0()
   at Autodesk.AutoCAD.Runtime.CommandClass.InvokeWorker(MethodInfo mi, Object commandObject, Boolean bLispFunction)
   at Autodesk.AutoCAD.Runtime.CommandClass.InvokeWorkerWithExceptionFilter(MethodInfo mi, Object commandObject, Boolean bLispFunction)
   at Autodesk.AutoCAD.Runtime.PerDocumentCommandClass.Invoke(MethodInfo mi, Boolean bLispFunction)
   at Autodesk.AutoCAD.Runtime.CommandClass.CommandThunk.Invoke()

 Any help would be great, this has been driving me crazy!

Thanks!

1 REPLY 1
Message 2 of 2
norman.yuan
in reply to: cyconnely

I think you need to keep iin mind that even you are developing with AutoCAD .NET API, you can mix the code that uses AutoCAD COM API,  such as the AcadDocument.Import() in your code, whihc is onlt available with COM API, not .NET API. However, you must be aware that the classes/objects from the 2 APIs are not the same things in spite of their name's similarity. That is, in your case, AcadSelectionSet, which is required argument by the COM Import() method, is not the same as SelectionSet in .NET API. They are from different APIs and you cannot assume one is the other and use them in different API freely.

 

In your code, you try to cast .NET API's SelectionSet to COM API's AcadSelectionSet, thus the exactly the exception your code asks: InvalidCats.

 

You need to build an AcadSelectionSet with code from the SelectionSet you obtained from .NET API. You could:

 

1. Simply use COM API to do the selection. That is, create an AcadSelectionSet, and use AcadSelectionSet.Select()/SelectionOnScreen() to select the target entities; Or

 

2. You can use .NET API to obtain an .NET SelectionSet (as you code currently does) and then make an array of Entity (.NET API Entity) then array of AcadEntity (e.g. Dim acadEnt As AcadEntity=CType(.NET_Entity.AcadObject, AcadEntity)). Then, you create an empty AcadSelectionSet and call AcadSelectionSet.AddItems() to add the AcadEntity array into the AcadSelectionSet.

Norman Yuan

Drive CAD With Code

EESignature

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost