How to get from selected object to the object type?

How to get from selected object to the object type?

Anonymous
Not applicable
2,767 Views
8 Replies
Message 1 of 9

How to get from selected object to the object type?

Anonymous
Not applicable

HI,

 

I have a selection set containing polylines from which each entity needs to be passed to the method that accepts a AcadPolyline object.

My question is how to get from the selection set entity to the i.e.. AcadPolyline?

 

Code:

        Dim acSSPrompt As PromptSelectionResult = acDoc.Editor.GetSelection()
        Dim acSSet As SelectionSet = acSSPrompt.Value
        '' Step through the objects in the selection set
        For Each acSSObj As SelectedObject In acSSet
              'here I need to convert acSSObj to AcadPolyline
        Next

 

Thanx,

 

0 Likes
Accepted solutions (1)
2,768 Views
8 Replies
Replies (8)
Message 2 of 9

Anonymous
Not applicable

Hi there,

 

you have to cast the object to the specific type.

Here's an example from the autodesk developersguide:

 

'' Calculate the regions based on each closed loop
      Dim myRegionColl As DBObjectCollection = New DBObjectCollection()
      myRegionColl = Region.CreateFromCurves(acDBObjColl)
      Dim acRegion1 As Region = myRegionColl(0)
      Dim acRegion2 As Region = myRegionColl(1)

 

Here we create a region from on objectid from a collection

 

This is the way you sould go i think.

 

Dim SelectedObject As Polyline = DirectCast(trans.GetObject(ent.ObjectId, OpenMode.ForRead), Polyline)

 

Hope this helps

 

Kind regards,

 

Irvin

 

0 Likes
Message 3 of 9

_gile
Consultant
Consultant

Hi,

 

Look at Create and Edit AutoCAD Entities > Work with Selection Sets > Select Objects in the Drawing Area in the:

http://docs.autodesk.com/ACD/2010/ENU/AutoCAD%20.NET%20Developer%27s%20Guide/index.html

 

In this example, rather than get an Entity from the ObjectId use TryCast to get a Polyline (or Nothing) and then, test if it's Nothing

 

Dim acPl As Polyline = TryCast(acTrans.GetObject(acSSObj.ObjectId, OpenMode.ForWrite), Polyline)

If Not Pl is Nothing Then ...

 

But, IMO the better way is to use a Selection set filter to insure the selection set only contains polylines

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 4 of 9

Anonymous
Not applicable

I tried TryCast and it keeps returning Nothing, although my selected object was 3DPolyline.

What am I missing?

 

        Dim o3Dpoly As AcadComCommon.Acad3DPolyline = _
        TryCast(acTrans.GetObject(acSSObj.ObjectId, OpenMode.ForRead), AcadComCommon.Acad3DPolyline)

 

Thanx,

0 Likes
Message 5 of 9

_gile
Consultant
Consultant

Hi,

 

It looks like you're confusing between COM and .NET...



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 6 of 9

norman.yuan
Mentor
Mentor
Accepted solution

If the selected object is an 3d polyline, the corresponding class in .NET API is Polyline3d, while Acad3dPolyline is an COM API class. You cannot directly cast an .NET API object into an COM API object. That is why TryCast returns Nothing.

 

Just curious, why do you need an COM object Acad3dPolyline in the managed API project? Can't Polyline3d meet your need for some special reasons?

 

If for particular reason you have to get an COM API object, most entiiy classes in .NET API do have a shortcut property to get an equivalent COM object: AcadObject (As Object), which is an pointer to the corresponding COM object AcadXxxxx.

 

So, you do this:

 

''First get a Polyline3d object. Use TryCast, in case the ObjectId does not point to a Polyline3d object

Dim poly3d As Polyline3d=TryCast(acTrans.GetObject(acSSObj.ObjectId, OpenMode.ForRead), Polyline3d)

 

''Get Acad3dPolyline, of course you have set reference to the COM library

If poly3d IsNot Nothing then

  Dim acadPoly As Acad3dPolyline=CType(poly3d.AcadObject, Acad3dPolyline)

  ''Do what you want to the Acad3dPolyline

 

End If

 

 

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 7 of 9

Anonymous
Not applicable

Thank you, Norman - that works great!

I have one more problem, which will be a piece of cake for you, I'm sure.

Actually, I select many objects, among them points and polylines. After obtaining selection set I need to loop through the selection set  and determine the object type for each object. I was able to check if the selected object was 3DPolyline, however, I haven't figured out yet how to do it for points? It seems like there is no Point object in Autodesk.AutoCAD.DatabaseServices namespace as opposed to Polyline3d?

Thank you,

 

    Function ReturnObjectType(ByVal acSSObj As SelectedObject) As String
        Dim acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()

        'Dim oPoint = Nothing ' here I need to check whether the selected object is Point
        Dim o3Dpoly As AcadNetDbServices.Polyline3d = _
        CType(acTrans.GetObject(acSSObj.ObjectId, OpenMode.ForRead), AcadNetDbServices.Polyline3d)
        If Not oPoint Is Nothing Then
            ReturnObjectType= "Point"
        ElseIf Not o3Dpoly Is Nothing Then
            If Not o3Dpoly.Closed Then
                ReturnObjectType= "Line"
            Else
                ReturnObjectType= "Polygon"
            End If
        Else
            ReturnObjectType= "Something else"
        End If
        acTrans.Dispose()
    End Function

0 Likes
Message 8 of 9

Anonymous
Not applicable

Hi there,

 

Maybe this will help?

 

If Not Object.ReferenceEquals(ent.[GetType](), GetType(Polyline)) Then
    Exit
End If
' If this entity is a polyline check is polyline is closed 

Dim polyLine As Polyline = DirectCast(trans.GetObject(ent.ObjectId, OpenMode.ForRead), Polyline)

 

 You only need to change Polyline to DBPoint!!!


 

0 Likes
Message 9 of 9

giskumar
Enthusiast
Enthusiast

Hi,

 

here is a sample.

 

DBpoint pnt = ent as DBpoint;

if (pnt == null)

{

Messagebox.show("Its not a DBpoint");

}

else

{

Messagebox("It is a DBpoint");

}

 

0 Likes