CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

创建区域 (.NET)

2023-1-1 14:50| 发布者: admin| 查看: 404| 评论: 0|来自: AutoCAD

通过创建对象的实例然后将其附加到对象,将区域添加到对象。在将其添加到对象之前,需要根据形成闭环的对象计算区域。该函数从对象输入数组形成的每个闭环中创建一个区域。该方法返回并要求对象。BlockTableRecordRegionBlockTableRecordBlockTableRecordCreateFromCurvesCreateFromCurvesDBObjectCollection

AutoCAD 将闭合的二维和平面三维多段线转换为单独的区域,然后转换形成闭合平面环的多段线、直线和曲线。如果两条以上的曲线共享一个端点,则生成的区域可能是任意的。因此,实际上可以使用该方法创建多个区域。您需要将创建的每个区域附加到对象。CreateFromCurvesBlockTableRecord

创建简单区域

下面的示例从单个圆创建一个区域。

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
 
<CommandMethod("AddRegion")> _
Public Sub AddRegion()
    '' 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 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 an in memory circle
        Using acCirc As Circle = New Circle()
            acCirc.Center = New Point3d(2, 2, 0)
            acCirc.Radius = 5

            '' Adds the circle to an object array
            Dim acDBObjColl As DBObjectCollection = New DBObjectCollection()
            acDBObjColl.Add(acCirc)

            '' Calculate the regions based on each closed loop
            Dim myRegionColl As DBObjectCollection = New DBObjectCollection()
            myRegionColl = Region.CreateFromCurves(acDBObjColl)
            Dim acRegion As Region = myRegionColl(0)

            '' Add the new object to the block table record and the transaction
            acBlkTblRec.AppendEntity(acRegion)
            acTrans.AddNewlyCreatedDBObject(acRegion, True)

            '' Dispose of the in memory object not appended to the database
        End Using

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

C#

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
 
[CommandMethod("AddRegion")]
public static void AddRegion()
{
    // 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 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 an in memory circle
        using (Circle acCirc = new Circle())
        {
            acCirc.Center = new Point3d(2, 2, 0);
            acCirc.Radius = 5;

            // Adds the circle to an object array
            DBObjectCollection acDBObjColl = new DBObjectCollection();
            acDBObjColl.Add(acCirc);

            // Calculate the regions based on each closed loop
            DBObjectCollection myRegionColl = new DBObjectCollection();
            myRegionColl = Region.CreateFromCurves(acDBObjColl);
            Region acRegion = myRegionColl[0] as Region;

            // Add the new object to the block table record and the transaction
            acBlkTblRec.AppendEntity(acRegion);
            acTrans.AddNewlyCreatedDBObject(acRegion, true);

            // Dispose of the in memory circle not appended to the database
        }

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

VBA/ActiveX 代码参考

Sub AddRegion()
    ' Define an array to hold the
    ' boundaries of the region.
    Dim curves(0 To 0) As AcadCircle
 
    ' Create a circle to become a
    ' boundary for the region.
    Dim center(0 To 2) As Double
    Dim radius As Double
    center(0) = 2
    center(1) = 2
    center(2) = 0
    radius = 5#
    Set curves(0) = ThisDrawing.ModelSpace.AddCircle _
                                     (center, radius)
 
    ' Create the region
    Dim regionObj As Variant
    regionObj = ThisDrawing.ModelSpace.AddRegion(curves)
 
    ZoomAll
End Sub

路过

雷人

握手

鲜花

鸡蛋

最新评论

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

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

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部