简体   繁体   中英

Drawing a box with meshes in AutoCAD (C#)

I've been playing around with AutoCAD API's PolygonMesh class. I want to draw a simple box using meshes. Here is a simple method I wrote to see how PolygonMesh behaves

[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 I expect the method to do is draw a simple block. Instead I get a block but there are lines going back to the origin:

它目前在做什么

How do I change this method so that it just draws the box instead?

我想要的是

Also if someone could explain the values M and N relative to meshes that would be awesome.

From the AutoCAD ObjectARX help file:

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.

So if you change your original code to M=2/N=4, you should get a better result.

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

And the M x N should give the number of vertices added via AppendVertex. On your original code, 4x4=16, but you only added 8, so all the remaining points were left as 0,0,0 (origin), causing the problem you reported.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM