Create Polyface Meshes
 
 
 

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.

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.

Create a polyface mesh

This example creates a PolyfaceMesh object and changes the viewing direction of the active viewport to display the three-dimensional nature of the mesh.

VB.NET

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()
 
      '' 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

C#

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();
 
      // 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();
  }
}
VBA/ActiveX Code Reference