CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

创建属性定义和属性引用 (.NET)

2023-1-1 10:23| 发布者: admin| 查看: 561| 评论: 0|来自: AutoCAD

若要创建属性定义,首先必须创建一个对象,然后使用该方法将其追加到对象。定义属性定义时,应指定属性文本的高度、属性模式、提示和标记字符串、插入点以及默认属性值。AttributeDefinitionBlockTableRecordAppendEntity

属性定义的模式由以下属性控制:

不断
为属性提供块插入的固定值。
无形
指定插入块时不显示或打印属性值。ATTDISP 命令将覆盖所有属性的“不可见”模式。
IsMTextAttributeDefinition
指定属性值可以包含多行文本。选择此选项后,可以指定属性的边界宽度。
锁定位置在块
锁定块参照内属性的位置。解锁后,可以使用夹点编辑相对于块的其余部分移动属性,并且可以调整多行属性的大小。
预设
插入包含预设属性的块时,将属性设置为其默认值。
核查
在插入块时提示您验证属性值是否正确。

当插入包含属性的块并使用属性进行设置时,将显示提示字符串。属性的默认值是使用属性设置的。如果属性设置为 True,则在插入具有属性定义的块时,将禁用提示输入新值的功能。PromptTextStringConstant

标记字符串标识属性的每个匹配项,并分配有属性。可以使用除空格或感叹号以外的任何字符。AutoCAD 将小写字母更改为大写字母。Tag

在块中定义属性定义后,每当使用 INSERT 命令插入块时,都可以为每个未定义为常量的属性引用指定不同的值。创建对象时,对象不包含对象中定义的任何属性,直到使用方法将它们追加到对象。BlockReferenceBlockTableRecordBlockReferenceAppendAttribute

在将属性附加到对象之前,请使用该方法将对象的属性复制到对象。该属性可用于块表记录,以查看它是否包含属性定义。BlockReferenceSetAttributeFromBlockAttributeDefinitionAttributeReferenceHasAttributeDefinitions

在模型空间或图纸空间上创建的属性定义不被视为附加到任何给定块。

定义属性定义

此示例创建一个块,然后向该块添加一个属性。

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry

<CommandMethod("AddingAttributeToABlock")> _
Public Sub AddingAttributeToABlock()
    ' Get the current database and start a transaction
    Dim acCurDb As Autodesk.AutoCAD.DatabaseServices.Database
    acCurDb = Application.DocumentManager.MdiActiveDocument.Database

    Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
        ' Open the Block table for read
        Dim acBlkTbl As BlockTable
        acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)

        If Not acBlkTbl.Has("CircleBlockWithAttributes") Then
            Using acBlkTblRec As New BlockTableRecord
                acBlkTblRec.Name = "CircleBlockWithAttributes"

                ' Set the insertion point for the block
                acBlkTblRec.Origin = New Point3d(0, 0, 0)

                ' Add a circle to the block
                Using acCirc As New Circle
                    acCirc.Center = New Point3d(0, 0, 0)
                    acCirc.Radius = 2

                    acBlkTblRec.AppendEntity(acCirc)

                    ' Add an attribute definition to the block
                    Using acAttDef As New AttributeDefinition
                        acAttDef.Position = New Point3d(0, 0, 0)
                        acAttDef.Verifiable = True
                        acAttDef.Prompt = "Door #: "
                        acAttDef.Tag = "Door#"
                        acAttDef.TextString = "DXX"
                        acAttDef.Height = 1
                        acAttDef.Justify = AttachmentPoint.MiddleCenter
                        acBlkTblRec.AppendEntity(acAttDef)

                        acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForWrite)
                        acBlkTbl.Add(acBlkTblRec)
                        acTrans.AddNewlyCreatedDBObject(acBlkTblRec, True)
                    End Using
                End Using
            End Using
        End If

        ' Save the new object to the database
        acTrans.Commit()

        ' Dispose of the transaction
    End Using
End Sub

C#

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;

[CommandMethod("AddingAttributeToABlock")]
public void AddingAttributeToABlock()
{
    // Get the current database and start a transaction
    Database acCurDb;
    acCurDb = Application.DocumentManager.MdiActiveDocument.Database;

    using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
    {
        // Open the Block table for read
        BlockTable acBlkTbl;
        acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;

        if (!acBlkTbl.Has("CircleBlockWithAttributes"))
        {
            using (BlockTableRecord acBlkTblRec = new BlockTableRecord())
            {
                acBlkTblRec.Name = "CircleBlockWithAttributes";

                // Set the insertion point for the block
                acBlkTblRec.Origin = new Point3d(0, 0, 0);

                // Add a circle to the block
                using (Circle acCirc = new Circle())
                {
                    acCirc.Center = new Point3d(0, 0, 0);
                    acCirc.Radius = 2;

                    acBlkTblRec.AppendEntity(acCirc);

                    // Add an attribute definition to the block
                    using (AttributeDefinition acAttDef = new AttributeDefinition())
                    {
                        acAttDef.Position = new Point3d(0, 0, 0);
                        acAttDef.Verifiable = true;
                        acAttDef.Prompt = "Door #: ";
                        acAttDef.Tag = "Door#";
                        acAttDef.TextString = "DXX";
                        acAttDef.Height = 1;
                        acAttDef.Justify = AttachmentPoint.MiddleCenter;

                        acBlkTblRec.AppendEntity(acAttDef);

                        acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForWrite);
                        acBlkTbl.Add(acBlkTblRec);
                        acTrans.AddNewlyCreatedDBObject(acBlkTblRec, true);
                    }
                }
            }
        }

        // Save the new object to the database
        acTrans.Commit();

        // Dispose of the transaction
    }
}

VBA/ActiveX Code Reference

Sub AddingAttributeToABlock ()
    ' Define the block
    Dim blockObj As AcadBlock
    Dim insertionPnt(0 To 2) As Double
    insertionPnt(0) = 0
    insertionPnt(1) = 0
    insertionPnt(2) = 0
    Set blockObj = ThisDrawing.Blocks.Add(insertionPnt, "CircleBlockWithAttributes")

    ' Add a circle to the block
    Dim circleObj As AcadCircle
    Dim center(0 To 2) As Double
    Dim radius As Double
    center(0) = 0
    center(1) = 0
    center(2) = 0
    radius = 2
    Set circleObj = blockObj.AddCircle(center, radius)
 
    ' Add an attribute to the block
    Dim attributeObj As AcadAttribute
    Dim height As Double
    Dim mode As Long
    Dim prompt As String
    Dim insertionPoint(0 To 2) As Double
    Dim tag As String
    Dim value As String
    insertionPoint(0) = 0
    insertionPoint(1) = 0
    insertionPoint(2) = 0
    height = 1
    prompt = "Door #: "
    tag = "Door#"
    value = "DXX"
    
    Set attributeObj = blockObj.AddAttribute(height, mode, prompt, insertionPoint, tag, value)
    attributeObj.Alignment = acAlignmentMiddleCenter
End Sub

Insert a block with attributes

This example creates a block with attributes, and then inserts the block in the current space.

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry

<CommandMethod("InsertingBlockWithAnAttribute")> _
Public Sub InsertingBlockWithAnAttribute()
    ' Get the current database and start a transaction
    Dim acCurDb As Autodesk.AutoCAD.DatabaseServices.Database
    acCurDb = Application.DocumentManager.MdiActiveDocument.Database

    Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
        ' Open the Block table for read
        Dim acBlkTbl As BlockTable
        acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)

        Dim blkRecId As ObjectId = ObjectId.Null

        If Not acBlkTbl.Has("CircleBlockWithAttributes") Then
            Using acBlkTblRec As New BlockTableRecord
                acBlkTblRec.Name = "CircleBlockWithAttributes"

                ' Set the insertion point for the block
                acBlkTblRec.Origin = New Point3d(0, 0, 0)

                ' Add a circle to the block
                Using acCirc As New Circle
                    acCirc.Center = New Point3d(0, 0, 0)
                    acCirc.Radius = 2

                    acBlkTblRec.AppendEntity(acCirc)

                    ' Add an attribute definition to the block
                    Using acAttDef As New AttributeDefinition
                        acAttDef.Position = New Point3d(0, 0, 0)
                        acAttDef.Prompt = "Door #: "
                        acAttDef.Tag = "Door#"
                        acAttDef.TextString = "DXX"
                        acAttDef.Height = 1
                        acAttDef.Justify = AttachmentPoint.MiddleCenter
                        acBlkTblRec.AppendEntity(acAttDef)

                        acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForWrite)
                        acBlkTbl.Add(acBlkTblRec)
                        acTrans.AddNewlyCreatedDBObject(acBlkTblRec, True)
                    End Using

                    blkRecId = acBlkTblRec.Id
                End Using
            End Using
        Else
            blkRecId = acBlkTbl("CircleBlockWithAttributes")
        End If

        ' Create and insert the new block reference
        If blkRecId <> ObjectId.Null Then
            Dim acBlkTblRec As BlockTableRecord
            acBlkTblRec = acTrans.GetObject(blkRecId, OpenMode.ForRead)

            Using acBlkRef As New BlockReference(New Point3d(2, 2, 0), acBlkTblRec.Id)

                Dim acCurSpaceBlkTblRec As BlockTableRecord
                acCurSpaceBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite)

                acCurSpaceBlkTblRec.AppendEntity(acBlkRef)
                acTrans.AddNewlyCreatedDBObject(acBlkRef, True)

                ' Verify block table record has attribute definitions associated with it
                If acBlkTblRec.HasAttributeDefinitions Then
                    ' Add attributes from the block table record
                    For Each objID As ObjectId In acBlkTblRec

                        Dim dbObj As DBObject = acTrans.GetObject(objID, OpenMode.ForRead)

                        If TypeOf dbObj Is AttributeDefinition Then
                            Dim acAtt As AttributeDefinition = dbObj

                            If Not acAtt.Constant Then
                                Using acAttRef As New AttributeReference

                                    acAttRef.SetAttributeFromBlock(acAtt, acBlkRef.BlockTransform)
                                    acAttRef.Position = acAtt.Position.TransformBy(acBlkRef.BlockTransform)

                                    acAttRef.TextString = acAtt.TextString

                                    acBlkRef.AttributeCollection.AppendAttribute(acAttRef)

                                    acTrans.AddNewlyCreatedDBObject(acAttRef, True)
                                End Using
                            End If
                        End If
                    Next
                End If
            End Using
        End If

        ' Save the new object to the database
        acTrans.Commit()

        ' Dispose of the transaction
    End Using
End Sub

C#

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;

[CommandMethod("InsertingBlockWithAnAttribute")]
public void InsertingBlockWithAnAttribute()
{
    // Get the current database and start a transaction
    Database acCurDb;
    acCurDb = Application.DocumentManager.MdiActiveDocument.Database;

    using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
    {
        // Open the Block table for read
        BlockTable acBlkTbl;
        acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;

        ObjectId blkRecId = ObjectId.Null;

        if (!acBlkTbl.Has("CircleBlockWithAttributes"))
        {
            using (BlockTableRecord acBlkTblRec = new BlockTableRecord())
            {
                acBlkTblRec.Name = "CircleBlockWithAttributes";

                // Set the insertion point for the block
                acBlkTblRec.Origin = new Point3d(0, 0, 0);

                // Add a circle to the block
                using (Circle acCirc = new Circle())
                {
                    acCirc.Center = new Point3d(0, 0, 0);
                    acCirc.Radius = 2;

                    acBlkTblRec.AppendEntity(acCirc);

                    // Add an attribute definition to the block
                    using (AttributeDefinition acAttDef = new AttributeDefinition())
                    {
                        acAttDef.Position = new Point3d(0, 0, 0);
                        acAttDef.Prompt = "Door #: ";
                        acAttDef.Tag = "Door#";
                        acAttDef.TextString = "DXX";
                        acAttDef.Height = 1;
                        acAttDef.Justify = AttachmentPoint.MiddleCenter;
                        acBlkTblRec.AppendEntity(acAttDef);

                        acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForWrite);
                        acBlkTbl.Add(acBlkTblRec);
                        acTrans.AddNewlyCreatedDBObject(acBlkTblRec, true);
                    }
                }

                blkRecId = acBlkTblRec.Id;
            }
        }
        else
        {
            blkRecId = acBlkTbl["CircleBlockWithAttributes"];
        }

        // Insert the block into the current space
        if (blkRecId != ObjectId.Null)
        {
            BlockTableRecord acBlkTblRec;
            acBlkTblRec = acTrans.GetObject(blkRecId, OpenMode.ForRead) as BlockTableRecord;

            // Create and insert the new block reference
            using (BlockReference acBlkRef = new BlockReference(new Point3d(2, 2, 0), blkRecId))
            {
                BlockTableRecord acCurSpaceBlkTblRec;
                acCurSpaceBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

                acCurSpaceBlkTblRec.AppendEntity(acBlkRef);
                acTrans.AddNewlyCreatedDBObject(acBlkRef, true);

                // Verify block table record has attribute definitions associated with it
                if (acBlkTblRec.HasAttributeDefinitions)
                {
                    // Add attributes from the block table record
                    foreach (ObjectId objID in acBlkTblRec)
                    {
                        DBObject dbObj = acTrans.GetObject(objID, OpenMode.ForRead) as DBObject;

                        if (dbObj is AttributeDefinition)
                        {
                            AttributeDefinition acAtt = dbObj as AttributeDefinition;

                            if (!acAtt.Constant)
                            {
                                using (AttributeReference acAttRef = new AttributeReference())
                                {
                                    acAttRef.SetAttributeFromBlock(acAtt, acBlkRef.BlockTransform);
                                    acAttRef.Position = acAtt.Position.TransformBy(acBlkRef.BlockTransform);

                                    acAttRef.TextString = acAtt.TextString;

                                    acBlkRef.AttributeCollection.AppendAttribute(acAttRef);

                                    acTrans.AddNewlyCreatedDBObject(acAttRef, true);
                                }
                            }
                        }
                    }
                }
            }
        }

        // Save the new object to the database
        acTrans.Commit();

        // Dispose of the transaction
    }
}

VBA/ActiveX Code Reference

Sub InsertingBlockWithAnAttribute ()
    ' Define the block
    Dim blockObj As AcadBlock
    Dim insertionPnt(0 To 2) As Double
    insertionPnt(0) = 0
    insertionPnt(1) = 0
    insertionPnt(2) = 0
    Set blockObj = ThisDrawing.Blocks.Add(insertionPnt, "CircleBlockWithAttributes")

    ' Add a circle to the block
    Dim circleObj As AcadCircle
    Dim center(0 To 2) As Double
    Dim radius As Double
    center(0) = 0
    center(1) = 0
    center(2) = 0
    radius = 2
    Set circleObj = blockObj.AddCircle(center, radius)
 
    ' Add an attribute to the block
    Dim attributeObj As AcadAttribute
    Dim height As Double
    Dim mode As Long
    Dim prompt As String
    Dim insertionPoint(0 To 2) As Double
    Dim tag As String
    Dim value As String
    insertionPoint(0) = 0
    insertionPoint(1) = 0
    insertionPoint(2) = 0
    height = 1
    prompt = "Door #: "
    tag = "Door#"
    value = "DXX"
    
    Set attributeObj = blockObj.AddAttribute(height, mode, prompt, insertionPoint, tag, value)
    attributeObj.Alignment = acAlignmentMiddleCenter
    
    ' Insert the block, creating a block reference
    ' and an attribute reference
    Dim blockRefObj As AcadBlockReference
    insertionPnt(0) = 2
    insertionPnt(1) = 2
    insertionPnt(2) = 0
    Set blockRefObj = ThisDrawing.ActiveLayout.Block.InsertBlock(insertionPnt, "CircleBlockWithAttributes", 1, 1, 1, 0)
End Sub

路过

雷人

握手

鲜花

鸡蛋

最新评论

QQ|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1 )

GMT+8, 2024-5-19 13:19

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部