画層に線種を割り当てる(.NET)

画層を定義する場合、視覚情報を提供する方法の 1 つが線種です。線種とは、ダッシュ、ドット、スペースのパターンを並べたもので、線を区別するために使用します。

線種名と線種定義により、特定のダッシュとドットの並び方、ダッシュとスペースの相対的長さ、文字またはシェイプを含む特性を表します。

線種を画層に割り当てるには、Linetype プロパティを使用します。このプロパティは、入力として線種名を受け取ります。

注: 線種を画層に割り当てる前に、最初に図面で線種を定義する必要があります。

画層の線種を設定する

次の例は、「ABC」という名前の新しい画層を作成し、「Center」という線種を割り当てます。

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

                '' Upgrade the Layer table for write
                acLyrTbl.UpgradeOpen()

                '' Append the new layer to the Layer table and the transaction
                acLyrTbl.Add(acLyrTblRec)
                acTrans.AddNewlyCreatedDBObject(acLyrTblRec, True)

                '' 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
                    acLyrTblRec.UpgradeOpen()

                    '' Set the linetype for the layer
                    acLyrTblRec.LinetypeObjectId = acLinTbl("Center")
                End If
            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
                acLyrTblRec.UpgradeOpen()

                '' 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;

                // Upgrade the Layer table for write
                acLyrTbl.UpgradeOpen();

                // Append the new layer to the Layer table and the transaction
                acLyrTbl.Add(acLyrTblRec);
                acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);

                // 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
                    acLyrTblRec.UpgradeOpen();

                    // Set the linetype for the layer
                    acLyrTblRec.LinetypeObjectId = acLinTbl["Center"];
                }
            }
        }
        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
                acLyrTblRec.UpgradeOpen();

                // 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