Once you have created a solid, you can create more complex shapes by combining or subtracting solids. You can join solids, subtract solids from each other, or find the common volume (overlapping portion) of solids. Use the BooleanOperation method to perform these combinations. The CheckInterference method allows you to determine if two solids overlap.
创建实体之后,可以通过组合或切除实体来创建更复杂的形状。可以合并这些实体、获得它们的差集或找出实体的公用(重叠部分)部分。可以使用 BooleanOperation 方法完成这些组合。而 CheckInterference 方法可以让你确定两个实体之间是否交迭。
Solids are further modified by obtaining the 2D cross section of a solid or slicing a solid into two pieces. Use the GetSection method to find cross sections of solids, and the Slice method for slicing a solid into two pieces.
通过获取实体的二维横截面或将实体剖切为两个部分,可以进一步修改实体。可以使用 GetSection 方法找出实体的横截面,使用 Slice 方法将实体剖切为两部分。
This example creates a box and cylinder. It then finds the interference between the two solids and creates a new solid from that interference. For ease of viewing, the box is colored white, the cylinder is colored cyan, and the interference solid is colored red.
本样例创建一个长方体和一个圆柱体,然后找出这两个实体之间的干涉,并由干涉创建一个新的实体。为便于查看,将长方体着色为白色,圆柱体着色为青色,得到的干涉实体为红色。
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
Dim acSol3DBox As Solid3d = New Solid3d()
acSol3DBox.SetDatabaseDefaults()
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
Dim acSol3DCyl As Solid3d = New Solid3d()
acSol3DCyl.SetDatabaseDefaults()
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)
'' 保存新对象到数据库中 Save the new objects to the database
acTrans.Commit()
End Using
End Sub
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
Solid3d acSol3DBox = new Solid3d();
acSol3DBox.SetDatabaseDefaults();
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
Solid3d acSol3DCyl = new Solid3d();
acSol3DCyl.SetDatabaseDefaults();
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();
}
}
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.
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
Dim acSol3D As Solid3d = New Solid3d()
acSol3D.SetDatabaseDefaults()
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)
'' 保存新对象到数据库中 Save the new objects to the database
acTrans.Commit()
End Using
End Sub
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
Solid3d acSol3D = new Solid3d();
acSol3D.SetDatabaseDefaults();
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();
}
}