CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

创建、修改和复制标注样式 (.NET)

2023-1-1 11:40| 发布者: admin| 查看: 624| 评论: 0|来自: AutoCAD

通过创建一个对象的实例,然后将其添加到 with the方法,可以创建新的标注样式。在将标注样式添加到表中之前,请使用属性设置新样式的名称。DimStyleTableRecordDimStyleTableAddName

您还可以复制现有样式或具有替代的样式。使用该方法将标注样式从源对象复制到标注样式。源对象可以是另一个对象、a、或对象,甚至是对象。如果从另一个标注样式复制样式设置,则会精确复制当前样式。如果从 a、、or 对象复制样式设置,则当前设置(包括任何对象替代)将复制到样式中。如果复制对象的当前样式,则标注样式加上任何绘图替代都将复制到新样式中。CopyFromDimStyleTableRecordDimensionToleranceLeaderDatabaseDimensionToleranceLeaderDatabase

复制标注样式和替代

本示例创建三个新的标注样式,并将当前标注样式、给定标注样式和给定尺寸的当前设置分别复制到每个新标注样式。通过在运行此示例之前遵循相应的设置,您会发现已创建不同的标注样式。Database

  1. 创建新图形并使其成为活动图形。
  2. 在新图形中创建线性尺寸。此尺寸应该是绘图中的唯一对象。
  3. 将尺寸线的颜色更改为黄色。
  4. 将 DIMCLRD 系统变量更改为 5(蓝色)。
  5. 运行以下示例:

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
 
<CommandMethod("CopyDimStyles")> _
Public Sub CopyDimStyles()
    '' Get the current database
    Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
    Dim acCurDb As Database = acDoc.Database

    '' Start a transaction
    Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()

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

        '' Open the Block table record Model space for read
        Dim acBlkTblRec As BlockTableRecord
        acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _
                                        OpenMode.ForRead)

        Dim acObj As Object = Nothing
        For Each acObjId As ObjectId In acBlkTblRec
            '' Get the first object in Model space
            acObj = acTrans.GetObject(acObjId, _
                                      OpenMode.ForRead)

            Exit For
        Next

        '' Open the DimStyle table for read
        Dim acDimStyleTbl As DimStyleTable
        acDimStyleTbl = acTrans.GetObject(acCurDb.DimStyleTableId, _
                                          OpenMode.ForRead)

        Dim strDimStyleNames(2) As String
        strDimStyleNames(0) = "Style 1 copied from a dim"
        strDimStyleNames(1) = "Style 2 copied from Style 1"
        strDimStyleNames(2) = "Style 3 copied from the running drawing values"

        Dim nCnt As Integer = 0

        '' Keep a reference of the first dimension style for later
        Dim acDimStyleTblRec1 As DimStyleTableRecord = Nothing

        '' Iterate the array of dimension style names
        For Each strDimStyleName As String In strDimStyleNames
            Dim acDimStyleTblRec As DimStyleTableRecord
            Dim acDimStyleTblRecCopy As DimStyleTableRecord = Nothing

            '' Check to see if the dimension style exists or not
            If acDimStyleTbl.Has(strDimStyleName) = False Then
                If acDimStyleTbl.IsWriteEnabled = False Then acTrans.GetObject(acCurDb.DimStyleTableId, OpenMode.ForWrite)

                acDimStyleTblRec = New DimStyleTableRecord()
                acDimStyleTblRec.Name = strDimStyleName

                acDimStyleTbl.Add(acDimStyleTblRec)
                acTrans.AddNewlyCreatedDBObject(acDimStyleTblRec, True)
            Else
                acDimStyleTblRec = acTrans.GetObject(acDimStyleTbl(strDimStyleName), _
                                                     OpenMode.ForWrite)
            End If

            '' Determine how the new dimension style is populated
            Select Case nCnt
                '' Assign the values of the dimension object to the new dimension style
                Case 0
                    Try
                        '' Cast the object to a Dimension
                        Dim acDim As RotatedDimension = acObj

                        '' Copy the dimension style data from the dimension and
                        '' set the name of the dimension style as the copied settings
                        '' are unnamed.
                        acDimStyleTblRecCopy = acDim.GetDimstyleData()
                        acDimStyleTblRec1 = acDimStyleTblRec
                    Catch
                        '' Object was not a dimension
                    End Try

                    '' Assign the values of the dimension style to the new dimension style
                Case 1
                    acDimStyleTblRecCopy = acDimStyleTblRec1

                    '' Assign the values of the current drawing to the dimension style
                Case 2
                    acDimStyleTblRecCopy = acCurDb.GetDimstyleData()
            End Select

            '' Copy the dimension settings and set the name of the dimension style
            acDimStyleTblRec.CopyFrom(acDimStyleTblRecCopy)
            acDimStyleTblRec.Name = strDimStyleName

            '' Dispose of the copied dimension style
            acDimStyleTblRecCopy.Dispose()

            nCnt = nCnt + 1
        Next

        '' Commit the changes and dispose of the transaction
        acTrans.Commit()
    End Using
End Sub

C#

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
 
[CommandMethod("CopyDimStyles")]
public static void CopyDimStyles()
{
    // Get the current database
    Document acDoc = Application.DocumentManager.MdiActiveDocument;
    Database acCurDb = acDoc.Database;

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

        // Open the Block table record Model space for read
        BlockTableRecord acBlkTblRec;
        acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                                        OpenMode.ForRead) as BlockTableRecord;

        object acObj = null;
        foreach (ObjectId acObjId in acBlkTblRec)
        {
            // Get the first object in Model space
            acObj = acTrans.GetObject(acObjId,
                                        OpenMode.ForRead);

            break;
        }

        // Open the DimStyle table for read
        DimStyleTable acDimStyleTbl;
        acDimStyleTbl = acTrans.GetObject(acCurDb.DimStyleTableId,
                                            OpenMode.ForRead) as DimStyleTable;

        string[] strDimStyleNames = new string[3];
        strDimStyleNames[0] = "Style 1 copied from a dim";
        strDimStyleNames[1] = "Style 2 copied from Style 1";
        strDimStyleNames[2] = "Style 3 copied from the running drawing values";

        int nCnt = 0;

        // Keep a reference of the first dimension style for later
        DimStyleTableRecord acDimStyleTblRec1 = null;

        // Iterate the array of dimension style names
        foreach (string strDimStyleName in strDimStyleNames)
        {
            DimStyleTableRecord acDimStyleTblRec;
            DimStyleTableRecord acDimStyleTblRecCopy = null;

            // Check to see if the dimension style exists or not
            if (acDimStyleTbl.Has(strDimStyleName) == false)
            {
                if (acDimStyleTbl.IsWriteEnabled == false) acTrans.GetObject(acCurDb.DimStyleTableId, OpenMode.ForWrite);

                acDimStyleTblRec = new DimStyleTableRecord();
                acDimStyleTblRec.Name = strDimStyleName;

                acDimStyleTbl.Add(acDimStyleTblRec);
                acTrans.AddNewlyCreatedDBObject(acDimStyleTblRec, true);
            }
            else
            {
                acDimStyleTblRec = acTrans.GetObject(acDimStyleTbl[strDimStyleName],
                                                        OpenMode.ForWrite) as DimStyleTableRecord;
            }

            // Determine how the new dimension style is populated
            switch ((int)nCnt)
            {
                // Assign the values of the dimension object to the new dimension style
                case 0:
                    try
                    {
                        // Cast the object to a Dimension
                        Dimension acDim = acObj as Dimension;

                        // Copy the dimension style data from the dimension and
                        // set the name of the dimension style as the copied settings
                        // are unnamed.
                        acDimStyleTblRecCopy = acDim.GetDimstyleData();
                        acDimStyleTblRec1 = acDimStyleTblRec;
                    }
                    catch
                    {
                        // Object was not a dimension
                    }

                    break;

                // Assign the values of the dimension style to the new dimension style
                case 1:
                    acDimStyleTblRecCopy = acDimStyleTblRec1;
                    break;
                // Assign the values of the current drawing to the dimension style
                case 2:
                    acDimStyleTblRecCopy = acCurDb.GetDimstyleData();
                    break;
            }

            // Copy the dimension settings and set the name of the dimension style
            acDimStyleTblRec.CopyFrom(acDimStyleTblRecCopy);
            acDimStyleTblRec.Name = strDimStyleName;

            // Dispose of the copied dimension style
            acDimStyleTblRecCopy.Dispose();

            nCnt = nCnt + 1;
        }

        // Commit the changes and dispose of the transaction
        acTrans.Commit();
    }
}

VBA/ActiveX 代码参考

Sub CopyDimStyles()
    Dim newStyle1 As AcadDimStyle
    Dim newStyle2 As AcadDimStyle
    Dim newStyle3 As AcadDimStyle
 
    Set newStyle1 = ThisDrawing.DimStyles. _
                        Add("Style 1 copied from a dim")
    Call newStyle1.CopyFrom(ThisDrawing.ModelSpace(0))
 
    Set newStyle2 = ThisDrawing.DimStyles. _
                        Add("Style 2 copied from Style 1")
    Call newStyle2.CopyFrom(ThisDrawing.DimStyles. _
                                Item("Style 1 copied from a dim"))
 
    Set newStyle2 = ThisDrawing.DimStyles. _
                        Add("Style 3 copied from the running drawing values")
    Call newStyle2.CopyFrom(ThisDrawing)
End Sub

使用 DIMSTYLE 命令打开标注样式管理器。现在应列出三个维度样式。样式 1 应具有黄色尺寸线。样式 2 应与样式 1 相同。样式 3 应具有蓝色尺寸线。


路过

雷人

握手

鲜花

鸡蛋

最新评论

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

GMT+8, 2024-5-19 15:03

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部