A rectangular mesh (PolygonMesh object) represents the surface of an object using planar facets. The density or number of facets for a mesh is defined in terms of a matrix of M and N vertices, similar to a grid consisting of columns and rows. M and N specify the column and row position, respectively, of any given vertex. You can create meshes in both 2D and 3D, but they are used primarily for 3D.
矩形网格(PolygonMesh 对象)使用平面镶嵌面来表示对象的曲面。网格密度或镶嵌面的数目由包含 M 乘 N 个顶点的矩阵定义,类似于由行和列组成的栅格。M 和 N 分别指定给定顶点的列和行的位置。在二维和三维空间中都可以创建网格,但它主要用于三维空间。
Create an instance of a PolygonMesh object and then specify the density and placement of the vertices for the new mesh. This method optionally takes six values: the type of Polygon Mesh to create, two integers that define the number of vertices in the M and N directions, a collection of points containing the coordinates for all the vertices in the mesh, and two booleans that define if the mesh is closed in the M or N directions.
创建 PolygonMesh 对象的实例,然后指定新的网络的顶点的密度和位置。这个方法带有六个可选的值:要创建的多面网格的类型,定义在 M 和 N 方向上的顶点的数量的两个整数,网格中包含的所有顶点坐标点的一个集合,以及两个定义网格是否关闭 M 或 N 方向的布尔值。
Once the PolygonMesh is created, use the IsMClosed and IsNClosed properties to close the mesh.
只要 PolygonMesh 被创建,使用 IsMClosed 和 IsNClosed 属性来关闭网格。
For more information on creating meshes, see “Create Surfaces” in the AutoCAD User's Guide.
更多关于创建网格的详细信息,请参见《AutoCAD 用户手册》的“创建曲面”部分。
This example creates a 4×4 polygon mesh. The direction of the active viewport is then adjusted so that the three-dimensional nature of the mesh is more easily viewed.
本例创建一个 4×4 多边形网格。然后调整活动视口的方向,以便于查看网格的三维情况。
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
<CommandMethod("Create3DMesh")> _
Public Sub Create3DMesh()
''获得当前文档和数据库,并启动一个事务 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 polygon mesh
Dim acPolyMesh As PolygonMesh = New PolygonMesh()
acPolyMesh.SetDatabaseDefaults()
acPolyMesh.MSize = 4
acPolyMesh.NSize = 4
'' 添加新对象到块表记录和事务中 Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acPolyMesh)
acTrans.AddNewlyCreatedDBObject(acPolyMesh, True)
'' 添加顶点前,多段线必须先要添加到图形中 Before adding vertexes, the polyline must be in the drawing
Dim acPts3dPMesh As Point3dCollection = New Point3dCollection()
acPts3dPMesh.Add(New Point3d(0, 0, 0))
acPts3dPMesh.Add(New Point3d(2, 0, 1))
acPts3dPMesh.Add(New Point3d(4, 0, 0))
acPts3dPMesh.Add(New Point3d(6, 0, 1))
acPts3dPMesh.Add(New Point3d(0, 2, 0))
acPts3dPMesh.Add(New Point3d(2, 2, 1))
acPts3dPMesh.Add(New Point3d(4, 2, 0))
acPts3dPMesh.Add(New Point3d(6, 2, 1))
acPts3dPMesh.Add(New Point3d(0, 4, 0))
acPts3dPMesh.Add(New Point3d(2, 4, 1))
acPts3dPMesh.Add(New Point3d(4, 4, 0))
acPts3dPMesh.Add(New Point3d(6, 4, 0))
acPts3dPMesh.Add(New Point3d(0, 6, 0))
acPts3dPMesh.Add(New Point3d(2, 6, 1))
acPts3dPMesh.Add(New Point3d(4, 6, 0))
acPts3dPMesh.Add(New Point3d(6, 6, 0))
For Each acPt3d As Point3d In acPts3dPMesh
Dim acPMeshVer As PolygonMeshVertex = New PolygonMeshVertex(acPt3d)
acPolyMesh.AppendVertex(acPMeshVer)
acTrans.AddNewlyCreatedDBObject(acPMeshVer, True)
Next
'' 打开活动视口 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("Create3DMesh")]
public static void Create3DMesh()
{
// 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 polygon mesh
PolygonMesh acPolyMesh = new PolygonMesh();
acPolyMesh.SetDatabaseDefaults();
acPolyMesh.MSize = 4;
acPolyMesh.NSize = 4;
// 添加新对象到块表记录和事务中 Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acPolyMesh);
acTrans.AddNewlyCreatedDBObject(acPolyMesh, true);
// 添加顶点前,多段线必须先要添加到图形中 Before adding vertexes, the polyline must be in the drawing
Point3dCollection acPts3dPMesh = new Point3dCollection();
acPts3dPMesh.Add(new Point3d(0, 0, 0));
acPts3dPMesh.Add(new Point3d(2, 0, 1));
acPts3dPMesh.Add(new Point3d(4, 0, 0));
acPts3dPMesh.Add(new Point3d(6, 0, 1));
acPts3dPMesh.Add(new Point3d(0, 2, 0));
acPts3dPMesh.Add(new Point3d(2, 2, 1));
acPts3dPMesh.Add(new Point3d(4, 2, 0));
acPts3dPMesh.Add(new Point3d(6, 2, 1));
acPts3dPMesh.Add(new Point3d(0, 4, 0));
acPts3dPMesh.Add(new Point3d(2, 4, 1));
acPts3dPMesh.Add(new Point3d(4, 4, 0));
acPts3dPMesh.Add(new Point3d(6, 4, 0));
acPts3dPMesh.Add(new Point3d(0, 6, 0));
acPts3dPMesh.Add(new Point3d(2, 6, 1));
acPts3dPMesh.Add(new Point3d(4, 6, 0));
acPts3dPMesh.Add(new Point3d(6, 6, 0));
foreach (Point3d acPt3d in acPts3dPMesh)
{
PolygonMeshVertex acPMeshVer = new PolygonMeshVertex(acPt3d);
acPolyMesh.AppendVertex(acPMeshVer);
acTrans.AddNewlyCreatedDBObject(acPMeshVer, 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();
}
}