插入具有预定义属性的现有块

时间:2014-10-13 12:34:10

标签: c# attributes block autocad

我正在开发一个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);

1 个答案:

答案 0 :(得分: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