Casting selectionset object to acadpolyline

Casting selectionset object to acadpolyline

Anonymous
Not applicable
2,189 Views
6 Replies
Message 1 of 7

Casting selectionset object to acadpolyline

Anonymous
Not applicable

Hi,

I am trying to access properties such as polyline.closed, from polylines in a selection set.

 

I use code as follows:

 

dim pl as acadpolyline

IF selectionset.item(i).objectname = "AcDbPolyline" Then

 

<<Here I need to cast the object as acadpolyline >>

 

Endif

 

I have tried with:

  1. pl = selectionset.item(i)
  2. pl = document.handletoobject(selectionset.item(i).handle)
  3. for each pl in selection if pl.handle = selectionset.item(i).handle then ......

All attempts give error as:

Unable to cast COM object of type AcadentityClass to interface type AcadPolyline

 

I have refernces AcCoreMgd, AcDbMgd, AcMgd, AutoCAD.Interop & AutoCad.Interop.common

Am I missing any libraries to cast acadentity into acadpolyline? Or what else am I missing?

 

Thanks for all help in advance

Mdshameer

 

0 Likes
Accepted solutions (1)
2,190 Views
6 Replies
Replies (6)
Message 2 of 7

SENL1362
Advisor
Advisor
in c# Selection items are a list of ObjectId's.
Use the transaction to return te Object/Entity or specific EntityType, like so:
using (Transaction tr = doc.Database.TransactionManager.StartTransaction())
{
SelectionSet currentlySelectedEntities = selectionResult.Value;
foreach (ObjectId id in currentlySelectedEntities.GetObjectIds())
{
Entity ent = tr.GetObject(id, OpenMode.ForRead) as Entity;
//or
if (id.ObjectClass.DxfName=="LWPOLYLINE")
Polyline pl=tr.GetObject(id, OpenMode.ForRead) as Polyline;
if (pl != null)
0 Likes
Message 3 of 7

Anonymous
Not applicable

SENL

I am trying this. Meanwhile what may be the difference between

document.objectidtoobject and tr.getobj()

Thanks

 

0 Likes
Message 4 of 7

Anonymous
Not applicable

SENL

I dont have classes such as selectionset, entity and polyline on my code.

These are acadselectionset, acadentity and acadpolyline on my code

I believe this is a problem with reference to use COM and .NET entities

Thanks

mdshameer

0 Likes
Message 5 of 7

SENL1362
Advisor
Advisor
sorry about that, i didn't knew you are using an out of process implementation.
A won't be able to help because i have no experience with that.
0 Likes
Message 6 of 7

norman.yuan
Mentor
Mentor
Accepted solution

The code shows you use COM object (AcadPolyline), however it is not clear what the SelectionSet in your code is, COM object AcadSelectionSet, or Autodesk.AutoCAD.EditorInput.SelectionSet?

 

You'd better show more code (or describe more details) on how the SelectionSet is created/obtained.

 

Another thing: are you sure the entities you selected are COM AcadPolyline (Polyline2d in .NET API), not AcadLWPolyline (Polyline in .NET API)? AcadPolyline was only used in earler version of AutoCAD. If people refer "polyline" in AutoCAD, it is usually "AcadLWPolyline" (COM) or "Polyline" (.NET). If the polyline is in 3D, then it is Acad3DPolyline or Polyline3d.

 

In your code, since you test ObjectName="AcadPolyline", which is AcadLWPolyline, yet you try to cast it to AcadPolyline, not AcadLWPolyline. This could be the cause of problem, assuming the selectionset is a COM object.

 

So, the possible problem with your code:

 

1. you cannot cast object direcctly betweem COM object and .NET object;

2. you need to make sure you know what the polyline is, AcadLWPolyline, or Acad3DPolyline, or the old AcadPolyline, to cast it correctly

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 7 of 7

Anonymous
Not applicable

Yuan -  this works

I was using three separate code segments for AcadPolyLine, AcadLWPolyline & Acad3DPolyline. The segment for AcadPolyline was not necessary on the first place, because as you said, both are same and AcadLWPolyline is the appropriate COM object I should have used. 

 This line solves the problem - 

"In your code, since you test ObjectName="AcadPolyline", which is AcadLWPolyline, yet you try to cast it to AcadPolyline, not AcadLWPolyline. This could be the cause of problem, assuming the selectionset is a COM object."

Thanks

 

And yes, my selectionset is AcadSelectionSet which is a COM object

0 Likes