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

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

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

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

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

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

VB.NET

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
        Using acPolyMesh As PolygonMesh = 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
            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
        End Using

        '' 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("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();
    }
}

VBA/ActiveX コード リファレンス

Sub Create3DMesh()
    Dim meshObj As AcadPolygonMesh
    Dim mSize, nSize, Count As Integer
 
    ' create the matrix of points
    Dim points(0 To 47) As Double
    points(0) = 0: points(1) = 0: points(2) = 0
    points(3) = 2: points(4) = 0: points(5) = 1
    points(6) = 4: points(7) = 0: points(8) = 0
    points(9) = 6: points(10) = 0: points(11) = 1
    points(12) = 0: points(13) = 2: points(14) = 0
    points(15) = 2: points(16) = 2: points(17) = 1
    points(18) = 4: points(19) = 2: points(20) = 0
    points(21) = 6: points(22) = 2: points(23) = 1
    points(24) = 0: points(25) = 4: points(26) = 0
    points(27) = 2: points(28) = 4: points(29) = 1
    points(30) = 4: points(31) = 4: points(32) = 0
    points(33) = 6: points(34) = 4: points(35) = 0
    points(36) = 0: points(37) = 6: points(38) = 0
    points(39) = 2: points(40) = 6: points(41) = 1
    points(42) = 4: points(43) = 6: points(44) = 0
    points(45) = 6: points(46) = 6: points(47) = 0
 
    mSize = 4: nSize = 4
 
    ' creates a 3Dmesh in model space
    Set meshObj = ThisDrawing.ModelSpace. _
                      Add3DMesh(mSize, nSize, points)
 
    ' Change the viewing direction of the viewport
    ' to better see the cylinder
    Dim NewDirection(0 To 2) As Double
    NewDirection(0) = -1
    NewDirection(1) = -1
    NewDirection(2) = 1
    ThisDrawing.ActiveViewport.direction = NewDirection
    ThisDrawing.ActiveViewport = ThisDrawing.ActiveViewport
 
    ZoomAll
End Sub