メッシュを作成する(.NET)

矩形メッシュ(PolygonMesh オブジェクト)は、平面の切り子面を使用してオブジェクトのサーフェスを表します。メッシュの密度、つまり切り子面の個数は、行と列で構成されるグリッドのように、M、N で示す頂点の行列で定義します。M および N は、それぞれ与えられた頂点の列と行を指定します。メッシュは、2D および 3D で作成することができますが、主に 3D で使用します。

PolygonMesh オブジェクトのインスタンスを作成してから新しいメッシュの頂点の密度と配置を指定します。このメソッドはオプションで次の 6 つの値を取ります。作成するポリゴン メッシュの種類、M 方向および N 方向の頂点数を定義する 2 つの整数、メッシュ内のすべての頂点の座標を含む点のコレクション、M 方向または N 方向でメッシュが閉じられているかどうかを定義する 2 つのブール値。

ポリゴン メッシュを作成したら、IsMClosed および NClosed プロパティを使用して、メッシュを閉じます。

ポリゴン メッシュを作成する

次の例では、4×4 のポリゴン メッシュを作成します。メッシュの 3 次元的な特質をより明確に表示できるように、アクティブ ビューポートの方向を調整します。

C# の例

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
        using (PolygonMesh acPolyMesh = new PolygonMesh())
        {
            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();
    }
}