A polyface mesh represents the surface of an object defined by faces capable of having numerous vertices. Creating a polyface mesh is similar to creating a rectangular mesh. You create a polyface mesh by creating an instance of a PolyFaceMesh object. The constructor of the PolyFaceMesh object does not accept any parameters. To add a vertex to a polyface mesh, you create a PolyFaceMeshVertex and add it to the PolyFaceMesh object using the AppendVertex method.
一个多面网格表示由许多顶点定义的对象的面。创建多面网格与创建矩形网格类似。通过创建一个 PolyFaceMesh 对象的实例可以创建一个多面网格。PolyFaceMesh 对象不接受任何参数。若要添加一个顶点到一个多面网格中,可以创建一个 PolyFaceMeshVertex 对象,然后使用 AppendVertex 方法把它添加到 PolyFaceMesh 对象中。
As you create the polyface mesh, you can set specific edges to be invisible, assign them to layers, or give them colors. To make an edge invisible, you create an instance of a FaceRecord and set which edges should be invisible and then append the FaceRecord object to the PolyFaceMesh object using the AppendFaceRecord method.
创建多面网格时,可以将特定边设置为不可见、指定边所在的图层或边的颜色。要使某个边不可见,你可以创建一个 FaceRecord 的实例并设置哪个边应该是不可见,然后利用 AppendFaceRecord 方法追加这个 FaceRecord 对象到 PolyFaceMesh 对象中。
This example creates a PolyfaceMesh object and changes the viewing direction of the active viewport to display the three-dimensional nature of the mesh.
本例创建一个 PolyfaceMesh 对象然后修改活动视口的查看方向,以便显示网格的三维空间的种类。
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
<CommandMethod("CreatePolyfaceMesh")> _
Public Sub CreatePolyfaceMesh()
''获得当前文档和数据库,并启动一个事务 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 polyface mesh
Dim acPFaceMesh As PolyFaceMesh = New PolyFaceMesh()
acPFaceMesh.SetDatabaseDefaults()
'' 添加新对象到块表记录和事务中 Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acPFaceMesh)
acTrans.AddNewlyCreatedDBObject(acPFaceMesh, True)
'' 添加顶点前,多段线必须先要添加到图形中 Before adding vertexes, the polyline must be in the drawing
Dim acPts3dPFMesh As Point3dCollection = New Point3dCollection()
acPts3dPFMesh.Add(New Point3d(4, 7, 0))
acPts3dPFMesh.Add(New Point3d(5, 7, 0))
acPts3dPFMesh.Add(New Point3d(6, 7, 0))
acPts3dPFMesh.Add(New Point3d(4, 6, 0))
acPts3dPFMesh.Add(New Point3d(5, 6, 0))
acPts3dPFMesh.Add(New Point3d(6, 6, 1))
For Each acPt3d As Point3d In acPts3dPFMesh
Dim acPMeshVer As PolyFaceMeshVertex = New PolyFaceMeshVertex(acPt3d)
acPFaceMesh.AppendVertex(acPMeshVer)
acTrans.AddNewlyCreatedDBObject(acPMeshVer, True)
Next
Dim acFaceRec1 As FaceRecord = New FaceRecord(1, 2, 5, 4)
acPFaceMesh.AppendFaceRecord(acFaceRec1)
acTrans.AddNewlyCreatedDBObject(acFaceRec1, True)
Dim acFaceRec2 As FaceRecord = New FaceRecord(2, 3, 6, 5)
acPFaceMesh.AppendFaceRecord(acFaceRec2)
acTrans.AddNewlyCreatedDBObject(acFaceRec2, True)
'' 打开活动视口 Open the active viewport
Dim acVportTblRec As ViewportTableRecord
acVportTblRec = acTrans.GetObject(acDoc.Editor.ActiveViewportId, _
OpenMode.ForWrite)
''旋转当前视口的查看方向 Rotate the view direction of the current viewport
acVportTblRec.ViewDirection = New Vector3d(-1, -1, 1)
acDoc.Editor.UpdateTiledViewportsFromDatabase()
'' 保存新对象到数据库中 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("CreatePolyfaceMesh")]
public static void CreatePolyfaceMesh()
{
// 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 polyface mesh
PolyFaceMesh acPFaceMesh = new PolyFaceMesh();
acPFaceMesh.SetDatabaseDefaults();
// 添加新对象到块表记录和事务中 Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acPFaceMesh);
acTrans.AddNewlyCreatedDBObject(acPFaceMesh, true);
// 添加顶点前,多段线必须先要添加到图形中 Before adding vertexes, the polyline must be in the drawing
Point3dCollection acPts3dPFMesh = new Point3dCollection();
acPts3dPFMesh.Add(new Point3d(4, 7, 0));
acPts3dPFMesh.Add(new Point3d(5, 7, 0));
acPts3dPFMesh.Add(new Point3d(6, 7, 0));
acPts3dPFMesh.Add(new Point3d(4, 6, 0));
acPts3dPFMesh.Add(new Point3d(5, 6, 0));
acPts3dPFMesh.Add(new Point3d(6, 6, 1));
foreach (Point3d acPt3d in acPts3dPFMesh)
{
PolyFaceMeshVertex acPMeshVer = new PolyFaceMeshVertex(acPt3d);
acPFaceMesh.AppendVertex(acPMeshVer);
acTrans.AddNewlyCreatedDBObject(acPMeshVer, true);
}
FaceRecord acFaceRec1 = new FaceRecord(1, 2, 5, 4);
acPFaceMesh.AppendFaceRecord(acFaceRec1);
acTrans.AddNewlyCreatedDBObject(acFaceRec1, true);
FaceRecord acFaceRec2 = new FaceRecord(2, 3, 6, 5);
acPFaceMesh.AppendFaceRecord(acFaceRec2);
acTrans.AddNewlyCreatedDBObject(acFaceRec2, true);
// 打开活动视口 Open the active viewport
ViewportTableRecord acVportTblRec;
acVportTblRec = acTrans.GetObject(acDoc.Editor.ActiveViewportId,
OpenMode.ForWrite) as ViewportTableRecord;
// Rotate the view direction of the current viewport
acVportTblRec.ViewDirection = new Vector3d(-1, -1, 1);
acDoc.Editor.UpdateTiledViewportsFromDatabase();
// 保存新对象到数据库中 Save the new objects to the database
acTrans.Commit();
}
}