ObjectID from Dictionary Unable to cast object of type to type Polyline

ObjectID from Dictionary Unable to cast object of type to type Polyline

Sgear
Advocate Advocate
3,106 Views
7 Replies
Message 1 of 8

ObjectID from Dictionary Unable to cast object of type to type Polyline

Sgear
Advocate
Advocate

 

Hi

 

I am try to use ObjectID from Dictionary and send it to test to get Polyline Length

 

I get this error

 

System.InvalidCastException: Unable to cast object of type 'Autodesk.AutoCAD.DatabaseServices.DataTable' to type 'Autodesk.AutoCAD.DatabaseServices.Polyline'.

 

 

 

 

     Public Sub getDictionary()
            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim db As Database = doc.Database

            Using trans As Transaction = db.TransactionManager.StartTransaction()
                Dim NOD As DBDictionary = trans.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead, False)
                Dim NetworkDict As DBDictionary
                Try
                    NetworkDict = trans.GetObject(NOD.GetAt("ACAD_OC"), OpenMode.ForRead)
                Catch ex As Autodesk.AutoCAD.Runtime.Exception
                    MsgBox(ex.Message)
                    Exit Sub
                End Try

                Dim DictEnum As DbDictionaryEnumerator = NetworkDict.GetEnumerator()

                While DictEnum.MoveNext
                    Dim PageDict As DBDictionary = DirectCast(trans.GetObject(DictEnum.Value, OpenMode.ForRead), DBDictionary)
                    For Each eDict As DictionaryEntry In PageDict
                        Test66(eDict.Value)
                    Next
                End While

            End Using

        End Sub
		

       Public Sub Test(ByVal objId As ObjectId)

                 Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
            Dim db As Database = doc.Database
            Dim ed As Editor = doc.Editor
            Dim Length As Double

            Using doc.LockDocument()

                Using tr As Transaction = db.TransactionManager.StartTransaction()
                    Dim obj As DBObject = tr.GetObject(objId, OpenMode.ForRead)

                    If TypeOf (obj) Is BlockReference Then

                    Else
                        Dim l As Polyline = DirectCast(obj, Polyline)
                        Length = Length + l.Length.ToString
                    End If
                    ed.WriteMessage(vbLf & Length)

                    tr.Commit()
                End Using
            End Using

        End Sub
		
0 Likes
Accepted solutions (1)
3,107 Views
7 Replies
Replies (7)
Message 2 of 8

_gile
Consultant
Consultant
Accepted solution

So, isn't the exception message explicit ?

You cast a DBObject wich type is DataTable into a Polyline.

 

Dim l As Polyline = DirectCast(obj, Polyline)

Rather than checking if the DBObject is not a BlockReference you should check if it is a Polyine

 

Not tested (VB is far to be my favorite language)

 

       Public Sub Test(ByVal objId As ObjectId)

            Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
            Dim db As Database = doc.Database
            Dim ed As Editor = doc.Editor
            Dim Length As Double

            Using doc.LockDocument()

                Using tr As Transaction = db.TransactionManager.StartTransaction()
                    If objId.ObjectClass.DxfName = "LWPOLYLINE"
                        Dim l As Polyline = DirectCast(tr.GetObject(objId, OpenMode.ForRead), Polyline)
                        Length = Length + l.Length.ToString
                    End If
                    ed.WriteMessage(vbLf & Length)

                    tr.Commit()
                End Using
            End Using

        End Sub

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 8

Sgear
Advocate
Advocate

Thanks _gile I can cast objectID between with your solution thanks for this !

0 Likes
Message 4 of 8

Sgear
Advocate
Advocate

Forgot the Lengt of the Polyline is 0 do I need to find the object in the Drawing with the ObjectID and then check the Length

0 Likes
Message 5 of 8

Sgear
Advocate
Advocate

The ObjectID from ACAD_OC and the ObjectID of the Polyline in the drawing are not the same.

when I close the drawing and open again I get new ID I did not know that I thought the ID was same after you create it

 

ACAD_OC  = (140695840146016)
Polyline  = (140695840145968)

0 Likes
Message 6 of 8

_gile
Consultant
Consultant

I do not konw anything about ACAD_OC dictionary (maybe a MAP specific dictionary?).

 

ObjectIds are not persistant and are re-created at document re-opening (see this thread).

 

If the dictionary entry is stored as HardPointerId, SoftOwnershipId, etc. the re-created ObjectIds of the polyline and the dictionary entry should be the same.

 

Sorry, I cannot help you further with AutoCAD MAP specific features.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 7 of 8

_gile
Consultant
Consultant

Maybe you can attach a little drawing (dwg) with a polyline and a dictionary (but I'm not sure I can inspect it with AutoCAD Vanilla).



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 8 of 8

Sgear
Advocate
Advocate

ACAD_OC dictionary keep information about Definition File and Classify Object in the Drawing
the Class Name are always the same so I need to find in the forum if I can find object by Class Name not ObjectID

0 Likes