Assign a selected object to a geometry type

Assign a selected object to a geometry type

Anonymous
Not applicable
551 Views
2 Replies
Message 1 of 3

Assign a selected object to a geometry type

Anonymous
Not applicable

Hello ,

I am new to programming using c# in autocad

I have selected some objects in autocad drawing using ( c# code )  as the part of code followed :

'''''''''''''''''''

foreach (SelectedObject sobj in ss)
{
Entity ent = trans.GetObject(sobj.ObjectId, OpenMode.ForWrite) as Entity;

if (ent != null)
{
//edt.WriteMessage("\n Type of object is : " + ent.GetType().FullName);

if (ent.GetType().Name == "PolyLine")
{
Polyline pl = new Polyline();
pl = ent;

}
}

}

'''''''''''''''

 

so when i write ( pl = ent;) it gives me an error that it can't be assigned and have to converted i first.

so how can i do the conversion ?

 

Thanks ,,

 

0 Likes
Accepted solutions (1)
552 Views
2 Replies
Replies (2)
Message 2 of 3

_gile
Consultant
Consultant
Accepted solution

Hi,

 

You can just downcast the Entity into a Polyline:

Polyline pl = (Polyline)ent;

But you could do it more simple and efficient:

Polyline pl = trans.GetObject(sobj.ObjectId, OpenMode.ForWrite) as Polyline;
if (pl != null)
{ ...
}

Or, even more efficient, check the ObjectId.ObjectClass before opening it

if (sobj.ObjectId.ObjectClass == RXObject.GetClass(typeof(Poilyline)))
{
    // here you're sure this is a polyline so you can directly cast
    Polyline pl = (Polyline)trans.GetObject (sobj.ObjectId, OpenMode.ForWrite)
... }

You can also use a SelectionFilter to insure the selection only contains Polylines.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 3

Anonymous
Not applicable

Thanks a lot , it worked 

0 Likes