在 AutoCAD (C#) 中绘制带网格的盒子

Drawing a box with meshes in AutoCAD (C#)

我一直在玩弄 AutoCAD API 的多边形网格 class。我想用网格画一个简单的盒子。这是我写的一个简单方法,用于查看 PolygonMesh 的行为

[CommandMethod("TESTSIMPLEMESH")]
public void TestSimpleMesh()
{
    // Get the current document and database, and start a transaction
    Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
    Database acCurDb = acDoc.Database;

    using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
    {
        BlockTable acBlkTbl = acTrans.GetObject(_database.BlockTableId, OpenMode.ForRead) as BlockTable; // Open the Block table record for read
        BlockTableRecord acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; // Open the Block table record Model space for write

        // Create a polygon mesh
        PolygonMesh acPolyMesh = new PolygonMesh();
        acPolyMesh.MSize = 4;
        acPolyMesh.NSize = 4;
        acPolyMesh.MakeNClosed(); //What is N???
        acPolyMesh.MakeMClosed(); //What is M???

        // Add the new object to the block table record and the transaction
        acBlkTblRec.AppendEntity(acPolyMesh);
        acTrans.AddNewlyCreatedDBObject(acPolyMesh, true);

        //Creating collection of points to add to the mesh
        Point3dCollection acPts3dPMesh = new Point3dCollection();
        acPts3dPMesh.Add(new Point3d(100, 100, 0));
        acPts3dPMesh.Add(new Point3d(200, 100, 0));
        acPts3dPMesh.Add(new Point3d(200, 200, 0));
        acPts3dPMesh.Add(new Point3d(100, 200, 0));
        acPts3dPMesh.Add(new Point3d(100, 100, 100));
        acPts3dPMesh.Add(new Point3d(200, 100, 100));
        acPts3dPMesh.Add(new Point3d(200, 200, 100));
        acPts3dPMesh.Add(new Point3d(100, 200, 100));

        //Converting those points to PolygonMeshVertecies and appending them to the PolygonMesh
        foreach (Point3d acPt3d in acPts3dPMesh)
        {
            PolygonMeshVertex acPMeshVer = new PolygonMeshVertex(acPt3d);
            acPolyMesh.AppendVertex(acPMeshVer);
            acTrans.AddNewlyCreatedDBObject(acPMeshVer, true);
        }

        // Save the new objects to the database
        acTrans.Commit();
    }
}

我希望该方法做的是绘制一个简单的块。相反,我得到了一个块,但有些行回到原点:

如何更改此方法,使其只绘制方框?

此外,如果有人可以解释与网格相关的 M 和 N 值,那就太棒了。

来自 AutoCAD ObjectARX 帮助文件:

PolygonMesh.MSize

Accesses the vertex count in the M direction. This is the number of vertices that will be used to make up an M row in the PolygonMesh if the PolyMeshType is SimpleMesh. For any other PolyMeshType, the M surface density value will be used as the row size.

PolygonMesh.NSize

Accesses the vertex count in the N direction. This is the number of vertices that will be used to make up an N column in the PolygonMesh if the PolyMeshType is SimpleMesh. For any other PolyMeshType, the N surface density value will be used as the column size.

因此,如果您将原始代码更改为 M=2/N=4,您应该会得到更好的结果。

PolygonMesh acPolyMesh = new PolygonMesh();
acPolyMesh.MSize = 2;
acPolyMesh.NSize = 4;

而 M x N 应该给出通过 AppendVertex 添加的顶点数。在您的原始代码中,4x4=16,但您只添加了 8,因此所有剩余的点都保留为 0,0,0(原点),导致您报告的问题。