CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

AutoCAD 2021 开发者帮助

为对象分配线型 (.NET)

2024-5-18 18:36| 发布者: admin| 查看: 67| 评论: 0|原作者: admin|来自: AutoCAD

为对象分配线型 (.NET)

以下示例创建一个圆圈,并为其分配“中心”线型。

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
 
<CommandMethod("SetObjectLinetype")> _
Public Sub SetObjectLinetype()
    '' 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 Linetype table for read
        Dim acLineTypTbl As LinetypeTable
        acLineTypTbl = acTrans.GetObject(acCurDb.LinetypeTableId, _
                                         OpenMode.ForRead)

        Dim sLineTypName As String = "Center"

        If acLineTypTbl.Has(sLineTypName) = False Then
            acCurDb.LoadLineTypeFile(sLineTypName, "acad.lin")
        End If

        '' 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 write
        Dim acBlkTblRec As BlockTableRecord
        acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _
                                        OpenMode.ForWrite)

        '' Create a circle object
        Using acCirc As Circle = New Circle()
            acCirc.Center = New Point3d(2, 2, 0)
            acCirc.Radius = 1
            acCirc.Linetype = sLineTypName

            acBlkTblRec.AppendEntity(acCirc)
            acTrans.AddNewlyCreatedDBObject(acCirc, True)
        End Using

        '' 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;
using Autodesk.AutoCAD.Geometry;
 
[CommandMethod("SetObjectLinetype")]
public static void SetObjectLinetype()
{
    // 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 Linetype table for read
        LinetypeTable acLineTypTbl;
        acLineTypTbl = acTrans.GetObject(acCurDb.LinetypeTableId,
                                            OpenMode.ForRead) as LinetypeTable;

        string sLineTypName = "Center";

        if (acLineTypTbl.Has(sLineTypName) == false)
        {
            acCurDb.LoadLineTypeFile(sLineTypName, "acad.lin");
        }

        // 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 write
        BlockTableRecord acBlkTblRec;
        acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                                        OpenMode.ForWrite) as BlockTableRecord;

        // Create a circle object
        using (Circle acCirc = new Circle())
        {
            acCirc.Center = new Point3d(2, 2, 0);
            acCirc.Radius = 1;
            acCirc.Linetype = sLineTypName;

            acBlkTblRec.AppendEntity(acCirc);
            acTrans.AddNewlyCreatedDBObject(acCirc, true);
        }

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

VBA/ActiveX 代码参考

Sub SetObjectLinetype()
    ' Load the Center linetype
    Dim linetypeName As String
    linetypeName = "Center"
 
    On Error Resume Next
    ThisDrawing.Linetypes.Load linetypeName, "acad.lin"
 
    ' Define the center point of the circle
    Dim centerPt(0 To 2) As Double
    centerPt(0) = 0: centerPt(1) = 3: centerPt(2) = 0
 
    ' Create a new circle and assign it the ACI value of 4
    Dim circleObj As AcadCircle
    Set circleObj = ThisDrawing.ModelSpace.AddCircle(centerPt, 1)
    circleObj.Linetype = linetypeName
 
    circleObj.Update
End Sub

路过

雷人

握手

鲜花

鸡蛋

最新评论

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

GMT+8, 2024-12-15 12:52

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部