我收到以下错误
“无法投射类型的对象 要输入的autodesk.autocad.databaseservices.polyline 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
你能帮忙!!!
答案 0 :(得分:1)
您的代码循环遍历btr(ModelSpace),但它假设它只处理块。
您需要再次检查以确认您正在检查的实体类型。最好先检查一下它是否是匿名的。
答案 1 :(得分:0)
有什么问题?
我有一个装满各种商品/产品的购物篮。牛奶,小苏打,苹果。香蕉。有些东西是可食用的,有些是非常有毒的。如果我认为所有东西都是可以食用的,我会尝试吃一些东西 - 这可能会导致一些非常严重的问题。例如我可以吃苹果,但如果我尝试吃牙刷,那么我的身体会崩溃并引发错误。我必须首先检查它是否可以食用然后再把它放进嘴里.........啊,好吧让我们继续......这与Autocad一样。
解决方案很简单:首先检查您正在处理的对象是否为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