CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

编辑三维实体 (.NET)

2023-1-1 10:58| 发布者: admin| 查看: 662| 评论: 0|来自: AutoCAD

创建实体后,可以通过组合或减去实体来创建更复杂的形状。您可以连接实体、相互减去实体或查找实体的公共体积(重叠部分)。使用该方法执行这些组合。该方法允许您确定两个实体是否重叠。BooleanOperationCheckInterference

通过获得固体的 2D 横截面或将固体切成两块来进一步修改实体。使用该方法查找固体的横截面,并使用该方法将固体切成两块。GetSectionSlice

查找两个实体之间的干涉

本示例创建一个盒子和圆柱体。然后,它找到两个实体之间的干涉,并从该干涉创建一个新的固体。为了便于查看,盒子的颜色为白色,圆柱体为青色,干涉固体为红色。

VB.NET

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

    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 a 3D solid box
        Using acSol3DBox As Solid3d = New Solid3d()
            acSol3DBox.CreateBox(5, 7, 10)
            acSol3DBox.ColorIndex = 7

            '' Position the center of the 3D solid at (5,5,0) 
            acSol3DBox.TransformBy(Matrix3d.Displacement(New Point3d(5, 5, 0) - _
                                                         Point3d.Origin))

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

            '' Create a 3D solid cylinder
            '' 3D solids are created at (0,0,0) so there is no need to move it
            Using acSol3DCyl As Solid3d = New Solid3d()
                acSol3DCyl.CreateFrustum(20, 5, 5, 5)
                acSol3DCyl.ColorIndex = 4

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

                '' Create a 3D solid from the interference of the box and cylinder
                Dim acSol3DCopy As Solid3d = acSol3DCyl.Clone()

                '' Check to see if the 3D solids overlap
                If acSol3DCopy.CheckInterference(acSol3DBox) = True Then
                    acSol3DCopy.BooleanOperation(BooleanOperationType.BoolIntersect, _
                                                 acSol3DBox.Clone())

                    acSol3DCopy.ColorIndex = 1
                End If

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

        '' Save the new objects 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("FindInterferenceBetweenSolids")]
public static void FindInterferenceBetweenSolids()
{
    // Get the current document and database, and start a transaction
    Document acDoc = Application.DocumentManager.MdiActiveDocument;
    Database acCurDb = acDoc.Database;

    using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
    {
        // Open the Block table record 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 3D solid box
        using (Solid3d acSol3DBox = new Solid3d())
        {
            acSol3DBox.CreateBox(5, 7, 10);
            acSol3DBox.ColorIndex = 7;

            // Position the center of the 3D solid at (5,5,0) 
            acSol3DBox.TransformBy(Matrix3d.Displacement(new Point3d(5, 5, 0) -
                                                            Point3d.Origin));

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

            // Create a 3D solid cylinder
            // 3D solids are created at (0,0,0) so there is no need to move it
            using (Solid3d acSol3DCyl = new Solid3d())
            {
                acSol3DCyl.CreateFrustum(20, 5, 5, 5);
                acSol3DCyl.ColorIndex = 4;

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

                // Create a 3D solid from the interference of the box and cylinder
                Solid3d acSol3DCopy = acSol3DCyl.Clone() as Solid3d;

                // Check to see if the 3D solids overlap
                if (acSol3DCopy.CheckInterference(acSol3DBox) == true)
                {
                    acSol3DCopy.BooleanOperation(BooleanOperationType.BoolIntersect,
                                                    acSol3DBox.Clone() as Solid3d);

                    acSol3DCopy.ColorIndex = 1;
                }

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

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

VBA/ActiveX Code Reference

Sub FindInterferenceBetweenSolids()
    ' Define the box
    Dim boxObj As Acad3DSolid
    Dim length As Double
    Dim width As Double
    Dim height As Double
    Dim center(0 To 2) As Double
    center(0) = 5: center(1) = 5: center(2) = 0
    length = 5
    width = 7
    height = 10
 
    ' Create the box object in model space
    ' and color it white
    Set boxObj = ThisDrawing.ModelSpace. _
                     AddBox(center, length, width, height)
    boxObj.Color = acWhite
 
    ' Define the cylinder
    Dim cylinderObj As Acad3DSolid
    Dim cylinderRadius As Double
    Dim cylinderHeight As Double
    center(0) = 0: center(1) = 0: center(2) = 0
    cylinderRadius = 5
    cylinderHeight = 20
 
    ' Create the Cylinder and
    ' color it cyan
    Set cylinderObj = ThisDrawing.ModelSpace. _
                          AddCylinder(center, cylinderRadius, cylinderHeight)
    cylinderObj.Color = acCyan
 
    ' Find the interference between the two solids
    ' and create a new solid from it. Color the
    ' new solid red.
    Dim solidObj As Acad3DSolid
    Set solidObj = boxObj.CheckInterference(cylinderObj, True)
    solidObj.Color = acRed
 
    ZoomAll
End Sub

Slice a solid into two solids

This example creates a box in model space. It then slices the box based on a plane defined by three points. The slice is returned as a 3DSolid.

VB.NET

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

    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 a 3D solid box
        Using acSol3D As Solid3d = New Solid3d()
            acSol3D.CreateBox(5, 7, 10)
            acSol3D.ColorIndex = 7

            '' Position the center of the 3D solid at (5,5,0) 
            acSol3D.TransformBy(Matrix3d.Displacement(New Point3d(5, 5, 0) - _
                                                      Point3d.Origin))

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

            '' Define the mirror plane
            Dim acPlane As Plane = New Plane(New Point3d(1.5, 7.5, 0), _
                                   New Point3d(1.5, 7.5, 10), _
                                   New Point3d(8.5, 2.5, 10))

            Dim acSol3DSlice As Solid3d = acSol3D.Slice(acPlane, True)
            acSol3DSlice.ColorIndex = 1

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

        '' Save the new objects 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("SliceABox")]
public static void SliceABox()
{
    // Get the current document and database, and start a transaction
    Document acDoc = Application.DocumentManager.MdiActiveDocument;
    Database acCurDb = acDoc.Database;

    using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
    {
        // Open the Block table record 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 3D solid box
        using (Solid3d acSol3D = new Solid3d())
        {
            acSol3D.CreateBox(5, 7, 10);
            acSol3D.ColorIndex = 7;

            // Position the center of the 3D solid at (5,5,0) 
            acSol3D.TransformBy(Matrix3d.Displacement(new Point3d(5, 5, 0) -
                                                        Point3d.Origin));

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

            // Define the mirror plane
            Plane acPlane = new Plane(new Point3d(1.5, 7.5, 0),
                                        new Point3d(1.5, 7.5, 10),
                                        new Point3d(8.5, 2.5, 10));

            Solid3d acSol3DSlice = acSol3D.Slice(acPlane, true);
            acSol3DSlice.ColorIndex = 1;

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

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

VBA/ActiveX 代码参考

Sub SliceABox()
    ' Create the box object
    Dim boxObj As Acad3DSolid
    Dim length As Double
    Dim width As Double
    Dim height As Double
    Dim center(0 To 2) As Double
    center(0) = 5#: center(1) = 5#: center(2) = 0
    length = 5#: width = 7: height = 10#
 
    ' Create the box (3DSolid) object in model space
    Set boxObj = ThisDrawing.ModelSpace. _
                     AddBox(center, length, width, height)
    boxObj.Color = acWhite
 
    ' Define the section plane with three points
    Dim slicePt1(0 To 2) As Double
    Dim slicePt2(0 To 2) As Double
    Dim slicePt3(0 To 2) As Double
 
    slicePt1(0) = 1.5: slicePt1(1) = 7.5: slicePt1(2) = 0
    slicePt2(0) = 1.5: slicePt2(1) = 7.5: slicePt2(2) = 10
    slicePt3(0) = 8.5: slicePt3(1) = 2.5: slicePt3(2) = 10
 
    ' slice the box and color the new solid red
    Dim sliceObj As Acad3DSolid
    Set sliceObj = boxObj.SliceSolid _
                          (slicePt1, slicePt2, slicePt3, True)
    sliceObj.Color = acRed
 
    ZoomAll
End Sub

路过

雷人

握手

鲜花

鸡蛋

最新评论

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

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

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部