With the TransformBy method of an object and the Mirroring method of a Matrix, you can mirror objects along a specified mirroring plane specified by three points.
使用 对象的 TransformBy 方法和 Matrix 的 Mirroring 方法,可以沿由三点指定的特定镜像平面镜像对象。
For more information on mirroring objects in 3D, see “Mirror Objects” in the AutoCAD User's Guide.
有关在三维中镜像对象的详细信息,请参见《AutoCAD 用户手册》中的“镜像对象”。
This example creates a box in model space. It then mirrors the box about a plane and colors the mirrored box red.
本样例在模型空间中创建一个长方体,然后根据某个平面镜像该长方体,并将镜像得到的长方体着色为红色。
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
<CommandMethod("MirrorABox3D")> _
Public Sub MirrorABox3D()
''获得当前文档和数据库,并启动一个事务 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)
'' 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)
'' Create a copy of the original 3D solid and change the color of the copy
Dim acSol3DCopy As Solid3d = acSol3D.Clone()
acSol3DCopy.ColorIndex = 1
'' Define the mirror plane
Dim acPlane As Plane = New Plane(New Point3d(1.25, 0, 0), _
New Point3d(1.25, 2, 0), _
New Point3d(1.25, 2, 2))
'' Mirror the 3D solid across the plane
acSol3DCopy.TransformBy(Matrix3d.Mirroring(acPlane))
'' 添加新对象到块表记录和事务中 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("MirrorABox3D")]
public static void MirrorABox3D()
{
// 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);
// 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);
// Create a copy of the original 3D solid and change the color of the copy
Solid3d acSol3DCopy = acSol3D.Clone() as Solid3d;
acSol3DCopy.ColorIndex = 1;
// Define the mirror plane
Plane acPlane = new Plane(new Point3d(1.25, 0, 0),
new Point3d(1.25, 2, 0),
new Point3d(1.25, 2, 2));
// Mirror the 3D solid across the plane
acSol3DCopy.TransformBy(Matrix3d.Mirroring(acPlane));
// 添加新对象到块表记录和事务中 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();
}
}