I try to select an polyline (with always 4 corners).
Then I need to select everything within the polyline.
So far so good.
But when i try to access the selectionset i get an fatal error.
I don't no how to solve it.
I hope that someone can help me.
Greetings Jacco
Public Sub CrossingPolygonSelect()
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database
Dim acDocEd As Editor = acDoc.Editor
Dim prObjectOptions As PromptEntityOptions = New PromptEntityOptions("Selecteer het aan te sluiten profiel : ")
Dim pt As Object
prObjectOptions.SetRejectMessage(vbLf + "Je moet een profiel selecteren" + vbLf)
prObjectOptions.AllowNone = False
pt = acDocEd.GetEntity(prObjectOptions)
If pt.Status <> PromptStatus.OK Then Exit Sub
Dim prof1 As ObjectId = pt.objectid
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
Dim ent0 As Polyline2d = acTrans.GetObject(prof1, OpenMode.ForRead)
Dim acPts3d As Point3dCollection = New Point3dCollection()
Dim pt0 As Point3d
pt0 = ent0.GetPointAtParameter(0)
acPts3d.Add(pt0)
pt0 = ent0.GetPointAtParameter(1)
acPts3d.Add(pt0)
pt0 = ent0.GetPointAtParameter(2)
acPts3d.Add(pt0)
pt0 = ent0.GetPointAtParameter(3)
acPts3d.Add(pt0)
Dim sres As PromptSelectionResult = acDocEd.SelectWindowPolygon(acPts3d) ', filter)
Dim acSSet As SelectionSet
acSSet = sres.Value
For Each acSSObj As SelectedObject In acSSet
MsgBox(acSSObj.ObjectId)
Next
acTrans.Commit()
End Using
End Sub
When you say "So far so good", how far the code goes until it is not good? Have you done debugging line by line/with break point? This way you can easily tell which line of code does the bad thing.
Just from your code without see the drawing, it i shard to tell what is wrong. But if I have to guess, it could be this line:
Dim ent0 As Polyline2d = acTrans.GetObject(prof1, OpenMode.ForRead)
Are you sure the picked polyline is Polyline2d, not Polyline? Polyline in .NET API is equivalent to AcadLWPolyline in VBA, which is used to replace old AutoCAD polylne (Polyline2d in .NET API) years ago. That is, Polyline2d/AcadPolyline only exists for backward compatibility since many versions of AutoCAD back (R13/13/14? I cannot remember). So, the picked polyline should be either Polyline or Polyline3d (in .NET API).
Norman Yuan
In agreement with Norman, I would suggest a fail safe in your code:
dim pline as polyline
dim ent as entitiy = trans.getobject(selectedobject.objectid,openmode.forread)
'use the if typeof line to test the object that was selected...
if typeof ent is AutoDesk.AutoCAD.DatabaseServices.Polyline then
pline = ent
end if
Try this code
I think you have to add Try..Catch to easier search for a problem
code line
Public Sub WindowPolygonSelect() Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument Dim db As Database = doc.Database Dim ed As Editor = doc.Editor Dim pso As PromptEntityOptions = New PromptEntityOptions(vbLf & "Select a contour : ") Dim res As PromptEntityResult pso.SetRejectMessage(vbLf & "You must select a polyline only" & vbLf) pso.AllowNone = False res = ed.GetEntity(pso) If res.Status <> PromptStatus.OK Then Exit Sub Dim id As ObjectId = res.ObjectId Using tr As Transaction = db.TransactionManager.StartTransaction() Dim ent As Polyline = TryCast(tr.GetObject(id, OpenMode.ForRead), Polyline) Dim pts As Point3dCollection = New Point3dCollection() Dim p As Point3d Dim i As Integer = 0 For i = 0 To ent.NumberOfVertices - 1 p = ent.GetPoint3dAt(i) pts.Add(p) Next Dim sres As PromptSelectionResult = ed.SelectWindowPolygon(pts) ', filter) Dim ss As SelectionSet ss = sres.Value For Each ssobj As SelectedObject In ss Dim obj As Entity = DirectCast(tr.GetObject(ssobj.ObjectId, OpenMode.ForRead), Entity) ed.WriteMessage(vbLf & "{0}" & vbTab & "{1}", obj.ObjectId, obj.GetRXClass().Name) Next tr.Commit() End Using End Sub