CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

将线型指定给图层 (.NET)

2023-1-1 13:22| 发布者: admin| 查看: 279| 评论: 0|来自: AutoCAD

定义图层时,线型提供了另一种传达视觉信息的方法。线型是虚线、点和空格的重复模式,可用于区分一条线与另一条线的用途。

线型名称和定义描述特定的虚线点序列、虚线和空格的相对长度以及任何包含的文本或形状的特征。

使用线型属性将线型指定给图层。此属性将线型的名称作为输入。

注意:必须先在图形中定义线型,然后才能将线型指定给图层。

设置图层的线型

以下示例创建一个名为“ABC”的新图层,并为其指定“中心”线型。

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
 
<CommandMethod("SetLayerLinetype")> _
Public Sub SetLayerLinetype()
    '' Get the current document and 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 Layer table for read
        Dim acLyrTbl As LayerTable
        acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, _
                                     OpenMode.ForRead)

        Dim sLayerName As String = "ABC"

        If acLyrTbl.Has(sLayerName) = False Then
            Using acLyrTblRec As LayerTableRecord = New LayerTableRecord()

                '' Assign the layer a name
                acLyrTblRec.Name = sLayerName

                '' Open the Layer table for read
                Dim acLinTbl As LinetypeTable
                acLinTbl = acTrans.GetObject(acCurDb.LinetypeTableId, _
                                             OpenMode.ForRead)

                If acLinTbl.Has("Center") = True Then
                    '' Set the linetype for the layer
                    acLyrTblRec.LinetypeObjectId = acLinTbl("Center")
                End If

                '' Upgrade the Layer table for write
                acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForWrite)

                '' Append the new layer to the Layer table and the transaction
                acLyrTbl.Add(acLyrTblRec)
                acTrans.AddNewlyCreatedDBObject(acLyrTblRec, True)
            End Using
        Else
            Dim acLyrTblRec As LayerTableRecord = acTrans.GetObject(acLyrTbl(sLayerName), _
                                                                    OpenMode.ForRead)

            '' Open the Layer table for read
            Dim acLinTbl As LinetypeTable
            acLinTbl = acTrans.GetObject(acCurDb.LinetypeTableId, _
                                         OpenMode.ForRead)

            If acLinTbl.Has("Center") = True Then
                '' Upgrade the Layer Table Record for write
                acTrans.GetObject(acLyrTbl(sLayerName), OpenMode.ForWrite)

                '' Set the linetype for the layer
                acLyrTblRec.LinetypeObjectId = acLinTbl("Center")
            End If
        End If

        '' Save 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;
 
[CommandMethod("SetLayerLinetype")]
public static void SetLayerLinetype()
{
    // Get the current document and database
    Document acDoc = Application.DocumentManager.MdiActiveDocument;
    Database acCurDb = acDoc.Database;

    // Start a transaction
    using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
    {
        // Open the Layer table for read
        LayerTable acLyrTbl;
        acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,
                                        OpenMode.ForRead) as LayerTable;

        string sLayerName = "ABC";

        if (acLyrTbl.Has(sLayerName) == false)
        {
            using (LayerTableRecord acLyrTblRec = new LayerTableRecord())
            {
                // Assign the layer a name
                acLyrTblRec.Name = sLayerName;

                // Open the Layer table for read
                LinetypeTable acLinTbl;
                acLinTbl = acTrans.GetObject(acCurDb.LinetypeTableId,
                                                OpenMode.ForRead) as LinetypeTable;

                if (acLinTbl.Has("Center") == true)
                {
                    // Set the linetype for the layer
                    acLyrTblRec.LinetypeObjectId = acLinTbl["Center"];
                }

                // Upgrade the Layer table for write
                acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForWrite);

                // Append the new layer to the Layer table and the transaction
                acLyrTbl.Add(acLyrTblRec);
                acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);
            }
        }
        else
        {
            LayerTableRecord acLyrTblRec = acTrans.GetObject(acLyrTbl[sLayerName],
                                            OpenMode.ForRead) as LayerTableRecord;

            // Open the Layer table for read
            LinetypeTable acLinTbl;
            acLinTbl = acTrans.GetObject(acCurDb.LinetypeTableId,
                                            OpenMode.ForRead) as LinetypeTable;

            if (acLinTbl.Has("Center") == true)
            {
                // Upgrade the Layer Table Record for write
                acTrans.GetObject(acLyrTbl[sLayerName], OpenMode.ForWrite);

                // Set the linetype for the layer
                acLyrTblRec.LinetypeObjectId = acLinTbl["Center"];
            }
        }

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

VBA/ActiveX 代码参考

Sub SetLayerLinetype()
    On Error Resume Next
    Dim layerObj As AcadLayer
 
    Set layerObj = ThisDrawing.Layers.Add("ABC")
    layerObj.Linetype = "Center"
End Sub

路过

雷人

握手

鲜花

鸡蛋

最新评论

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

GMT+8, 2024-5-19 14:58

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部