【问题标题】:Inserting existing block with predefined attributes插入具有预定义属性的现有块
【发布时间】:2014-10-14 16:27:11
【问题描述】:

我正在开发一个 c# 程序,该程序旨在将已经在 AutoCAD 绘图上的块插入到用户单击的任何位置,并自动将字符串值添加到第一个自定义属性。我已经通过使用新的单击点位置和现有块名称创建新块引用来完成其中的一部分。

仅由于某种原因,现有块上的两个自定义属性不会出现在新块上,它只显示块的行而没有文本。请参阅下面程序的一小部分。有谁知道为什么没有将现有的块属性添加到新块中,如果是这样,我将如何去做?我浏览了很多关于这个问题的论坛,它们都展示了如何创建全新的属性,而不是从现有块中获取预定义的属性并将它们添加到新的属性中。

Scale3d blkScale = new Scale3d(drgScalemm, drgScalemm, drgScalemm);
ObjectId bdId = bt[blkName];
Point3d pt = ptInsert.Value;

BlockReference insblkref = new BlockReference(pt, bdId);
insblkref.ScaleFactors = blkScale;
insblkref.Rotation = 0;

btr.AppendEntity(insblkref);
tr.AddNewlyCreatedDBObject(insblkref, true);

【问题讨论】:

    标签: c# attributes block autocad


    【解决方案1】:

    你的假设是错误的。从“代码”插入块时,属性不会自动从块定义中插入。所以你需要“手动”添加属性。

    VB 中的一段代码:

     Sub AttributenToevoegen(ByVal BlokRefId As ObjectId)
            Dim doc = Application.DocumentManager.MdiActiveDocument
            Dim dwg = doc.Database
            Using doc.LockDocument
                Using transactie = doc.TransactionManager.StartTransaction()
                    Try
    
                        Dim Ref As BlockReference
                        Ref = transactie.GetObject(BlokRefId, OpenMode.ForWrite)
                        Dim a = Ref.Name
    
                        Dim BlokDefinities As BlockTable
                        BlokDefinities = transactie.GetObject(dwg.BlockTableId, OpenMode.ForRead)
                        Dim Blokdefid = BlokDefinities(Ref.Name)
                        Dim BlokDefinitie As BlockTableRecord
                        BlokDefinitie = transactie.GetObject(Blokdefid, OpenMode.ForRead)
    
                        Dim AttRefIdColl = Ref.AttributeCollection
    
                        For Each elementId In BlokDefinitie
                            Dim Element As Entity
                            Element = transactie.GetObject(elementId, OpenMode.ForRead)
    
                            If TypeOf Element Is AttributeDefinition Then
                                Dim attribuutdefinitie = CType(Element, AttributeDefinition)
                                Dim attribuutreferentie As New AttributeReference
                                attribuutreferentie.SetAttributeFromBlock(attribuutdefinitie, Ref.BlockTransform)
                                AttRefIdColl.AppendAttribute(attribuutreferentie)
                                transactie.AddNewlyCreatedDBObject(attribuutreferentie, True)
                            End If
                        Next
    
                        transactie.Commit()
                    Catch ex As Exception
                        MsgBox("Er ging iets fout: " & vbCrLf & ex.Message)
                    End Try
                End Using
            End Using
    

    【讨论】:

      猜你喜欢
      • 2019-09-19
      • 2014-05-05
      • 2021-09-24
      • 1970-01-01
      • 1970-01-01
      • 2021-06-22
      • 1970-01-01
      • 1970-01-01
      • 2014-11-14
      相关资源
      最近更新 更多