Autocad API,读取块属性铸造错误
Autocad API , reading block attributes casting error
我收到以下错误
"Unable to cast object of type
autodesk.autocad.databaseservices.polyline to type
autodesk.autocad.databaseservices.blockrefereance
在读取方块属性时绘图上有很多东西。
代码:
Dim db As Database = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database
Using tr As Transaction = db.TransactionManager.StartTransaction
Dim ed As Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor
Dim blockids As ObjectIdCollection = New ObjectIdCollection
Dim bt As BlockTable = tr.GetObject(db.BlockTableId, OpenMode.ForRead)
Dim btr As BlockTableRecord = tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForRead)
For Each objid As ObjectId In btr
If btr.IsAnonymous = False Or btr.IsLayout = False Then
Dim blkref As BlockReference = tr.GetObject(objid, OpenMode.ForRead)
Dim attcol As AttributeCollection = blkref.AttributeCollection
For Each attid As ObjectId In attcol
Dim attref As AttributeReference = tr.GetObject(attid, OpenMode.ForRead)
ed.WriteMessage(attref.Tag.ToString & vbNewLine)
ed.WriteMessage(attref.TextString.ToString & vbNewLine)
Next
End If
Next
tr.Commit()
End Using
你能帮忙吗!!!
您的代码在 btr (ModelSpace) 中循环,但假设它只处理块。
您需要先进行另一次检查,以确认您正在检查的实体类型。最好在检查它是否是匿名之前。
问题是什么?
我有一个装满各种 goods/products 的购物篮。牛奶、小苏打、苹果。香蕉。有些东西可以吃,有些东西有毒。如果我假设所有的东西都是可食用的并且我尝试吃一些东西——那可能会导致一些非常严重的问题。例如我可以吃苹果,但如果我尝试吃牙刷,那么我的 body 就会崩溃并抛出错误。一定要先确认能不能吃再放进嘴里…………啊,好吧,我们继续……Autocad也是一样。
解决方案很简单:首先检查您正在处理的 object 是否是 blockReference。如果是这样,那么继续。我没有检查下面的代码,因为我不是 VB.NET 专家,但是像这样的东西可以让你在继续之前检查你是否正在处理 blockReference:
For Each objid As ObjectId In btr
If btr.IsAnonymous = False Or btr.IsLayout = False Then
Dim ent as Entity = tr.GetObject(objid, OpenMode.ForRead) // ADD THIS LINE
If TypeOf ent Is [BlockReference] Then // ADD THIS IF STATEMENT
// ADD IN THE REST OF YOUR CODE HERE
// e.g. Dim blkref As BlockReference = tr.GetObject(objid,
// OpenMode.ForRead) etc.
End If
我收到以下错误
"Unable to cast object of type
autodesk.autocad.databaseservices.polyline to type autodesk.autocad.databaseservices.blockrefereance
在读取方块属性时绘图上有很多东西。
代码:
Dim db As Database = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database
Using tr As Transaction = db.TransactionManager.StartTransaction
Dim ed As Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor
Dim blockids As ObjectIdCollection = New ObjectIdCollection
Dim bt As BlockTable = tr.GetObject(db.BlockTableId, OpenMode.ForRead)
Dim btr As BlockTableRecord = tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForRead)
For Each objid As ObjectId In btr
If btr.IsAnonymous = False Or btr.IsLayout = False Then
Dim blkref As BlockReference = tr.GetObject(objid, OpenMode.ForRead)
Dim attcol As AttributeCollection = blkref.AttributeCollection
For Each attid As ObjectId In attcol
Dim attref As AttributeReference = tr.GetObject(attid, OpenMode.ForRead)
ed.WriteMessage(attref.Tag.ToString & vbNewLine)
ed.WriteMessage(attref.TextString.ToString & vbNewLine)
Next
End If
Next
tr.Commit()
End Using
你能帮忙吗!!!
您的代码在 btr (ModelSpace) 中循环,但假设它只处理块。
您需要先进行另一次检查,以确认您正在检查的实体类型。最好在检查它是否是匿名之前。
问题是什么?
我有一个装满各种 goods/products 的购物篮。牛奶、小苏打、苹果。香蕉。有些东西可以吃,有些东西有毒。如果我假设所有的东西都是可食用的并且我尝试吃一些东西——那可能会导致一些非常严重的问题。例如我可以吃苹果,但如果我尝试吃牙刷,那么我的 body 就会崩溃并抛出错误。一定要先确认能不能吃再放进嘴里…………啊,好吧,我们继续……Autocad也是一样。
解决方案很简单:首先检查您正在处理的 object 是否是 blockReference。如果是这样,那么继续。我没有检查下面的代码,因为我不是 VB.NET 专家,但是像这样的东西可以让你在继续之前检查你是否正在处理 blockReference:
For Each objid As ObjectId In btr
If btr.IsAnonymous = False Or btr.IsLayout = False Then
Dim ent as Entity = tr.GetObject(objid, OpenMode.ForRead) // ADD THIS LINE
If TypeOf ent Is [BlockReference] Then // ADD THIS IF STATEMENT
// ADD IN THE REST OF YOUR CODE HERE
// e.g. Dim blkref As BlockReference = tr.GetObject(objid,
// OpenMode.ForRead) etc.
End If