Opening dbobjects inside a dbobjectcollection

Opening dbobjects inside a dbobjectcollection

Anonymous
Not applicable
1,742 Views
2 Replies
Message 1 of 3

Opening dbobjects inside a dbobjectcollection

Anonymous
Not applicable

Hello all,

 

I am just starting to learn VB.NET and I am trying to get the length of polyline segments.

 

I ask the user to select the polylines and I explode them using entity.explode. This function provides the exploded segments as a dbobjectcollection. Nevertheless, when I try to open the dbobjects inside the collection I get an "exception error".

 

This is my code, I am getting the error in the bold line:

 

Public Class ARCommands

    <CommandMethod("getlineslength")> _
    Public Sub getlineslength()
        Dim myDWG As Document = _
            Application.DocumentManager.MdiActiveDocument
        Dim myEd As Editor = myDWG.Editor
        Dim acTypValAr(0) As TypedValue
        acTypValAr.SetValue(New  _
                            TypedValue(DxfCode.Start, _
                                       "LWPOLYLINE"), 0)
        Dim acSelFtr As SelectionFilter = _
            New SelectionFilter(acTypValAr)
        Dim myPSR As PromptSelectionResult = _
            myEd.GetSelection(acSelFtr)

        Dim myobjects As DBObjectCollection = _
            New DBObjectCollection()

        If myPSR.Status = PromptStatus.OK Then
            Using myTrans As Transaction = _
                myDWG.Database.TransactionManager.StartTransaction()

                For Each so As SelectedObject In myPSR.Value
                    Dim myEnt As Entity = _
                        so.ObjectId.GetObject(OpenMode.ForRead)
                    myEnt.Explode(myobjects)
                Next

                For I As Integer = 1 To myobjects.Count - 1

                    Dim myobject As DBObject = _
                        myTrans.GetObject(myobjects.Item(I - 1). _
                                          ObjectId, OpenMode.ForRead)
                    Dim myline As Line = _
                        myTrans.GetObject(myobject.ObjectId, _
                                          OpenMode.ForRead)
                    MsgBox(myline.Length.ToString)
                Next
                myTrans.Commit()
            End Using
        End If
    End Sub
End Class

 

 

Thank you,

Daniel Archila

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

norman.yuan
Mentor
Mentor
Accepted solution

The entities in the DBObjectCollection, as the result of Entity.Explode(), are not database residing objects, thus you do not need to open it with Transaction for read/write. You just go ahead work with them, such as:

 

foreach (var ent in myCol)

{

   Line lin=ent as Line;

   if (lin!=null)

   {

      double l=lin.Length;

   }

}

 

Do not forget to dispose them if you are not intend to append them into databse.

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 3

Anonymous
Not applicable

I got it. Thank you very much Norman!

0 Likes