You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让AutoCAD ObjectARX VB.NET插入的块引用携带自定义属性

解决AutoCAD块插入时丢失自定义属性的问题

你的代码仅创建了块引用(BlockReference),但没有处理块预定义的自定义属性。块的自定义属性存储在对应的BlockTableRecord的AttributeDefinition对象中,需要手动遍历这些定义,生成对应的AttributeReference并附加到块引用上,才能让插入的块携带属性。

以下是修改后的完整fInsertBlock函数实现:

Public Shared Function fInsertBlock(ByVal sBlockName As String, ByVal naInsPnt3D As Point3d) As String
    'Block Name 为PVcDes的自定义块之一
    'naInsPnt3D 为块在模型空间的放置位置
    '返回块的句柄
    fInsertBlock = "Error" '设置失败时的返回值
    Dim acDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
    Dim acCurDb As Database = acDoc.Database
    Dim acEditor As Editor = acDoc.Editor
    
    '启动事务并查找目标块的块表ID
    Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
        Dim acBlkTbl As BlockTable = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) '以只读方式打开块表
        Dim acBlkRecId As ObjectId = ObjectId.Null      '初始化块表记录ID
        
        If Not acBlkTbl.Has(sBlockName) Then
            MsgBox("缺失块,请在图纸中添加" & sBlockName & "块。", vbOKOnly, "错误 U1016") '报错并退出
            Exit Function
        Else
            acBlkRecId = acBlkTbl.Item(sBlockName)       '获取目标块的块表记录ID
        End If
        
        '打开目标块的块表记录,读取属性定义
        Dim acBlkDefRec As BlockTableRecord = acTrans.GetObject(acBlkRecId, OpenMode.ForRead)
        Dim acBlkTblRec As BlockTableRecord = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
        Dim acBlkRef As New BlockReference(naInsPnt3D, acBlkRecId)
        
        acBlkTblRec.AppendEntity(acBlkRef)
        acBlkRef.SetDatabaseDefaults()
        acTrans.AddNewlyCreatedDBObject(acBlkRef, True)
        
        '遍历块定义中的属性定义,生成对应的属性引用
        For Each objId As ObjectId In acBlkDefRec
            Dim dbObj As DBObject = acTrans.GetObject(objId, OpenMode.ForRead)
            Dim attDef As AttributeDefinition = TryCast(dbObj, AttributeDefinition)
            
            '仅处理有效的属性定义,排除不可见或常量属性(可根据需求调整)
            If attDef IsNot Nothing AndAlso Not attDef.Constant AndAlso Not attDef.Invisible Then
                Dim attRef As New AttributeReference()
                attRef.SetAttributeFromBlock(attDef, acBlkRef.BlockTransform)
                attRef.TextString = attDef.TextString '使用属性定义的默认值
                attRef.Position = attDef.Position.TransformBy(acBlkRef.BlockTransform)
                
                acBlkRef.AttributeCollection.AppendAttribute(attRef)
                acTrans.AddNewlyCreatedDBObject(attRef, True)
            End If
        Next
        
        acTrans.Commit()
        fInsertBlock = acBlkRef.Handle.ToString
        acEditor.UpdateScreen()
    End Using
End Function

关键修改说明

  • 打开目标块的BlockTableRecord(acBlkDefRec),用于读取预定义的属性定义。
  • 遍历块定义中的所有对象,筛选出AttributeDefinition类型的对象。
  • 对每个有效属性定义,创建对应的AttributeReference,通过SetAttributeFromBlock继承属性定义的样式、对齐方式等设置。
  • 将属性引用添加到块引用的AttributeCollection中,并在事务中注册新创建的属性对象。
  • 可根据需求调整筛选条件(比如是否包含常量、不可见属性)。

内容的提问来源于stack exchange,提问作者JMc1999

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.11 16:04:57