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

时间:2015-09-08 15:22:30

标签: c# mesh autocad autocad-plugin

我一直在使用AutoCAD API的PolygonMesh类。我想用网格画一个简单的盒子。这是我写的一个简单的方法,用于了解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();
    }
}

我希望该方法可以绘制一个简单的块。相反,我得到了一个区块,但有一些线路可以回到原点:

What it's currently doing

如何更改此方法以便只改为方框?

What I want

此外,如果有人可以解释相对于网格的值M和N,那将是非常棒的。

1 个答案:

答案 0 :(得分:2)

从AutoCAD ObjectARX帮助文件:

  

PolygonMesh.MSize

     

访问M方向的顶点数。这是数量   将用于组成PolygonMesh中的M行的顶点if   PolyMeshType是SimpleMesh。对于任何其他PolyMeshType,M   表面密度值将用作行大小。

     

PolygonMesh.NSize

     

访问N方向的顶点数。这是数量   将用于组成PolygonMesh中的N列的顶点   如果PolyMeshType是SimpleMesh。对于任何其他PolyMeshType,N   表面密度值将用作列大小。

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

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

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