3

I'm trying to programmatically draw a solid box/rectangle in AutoCAD using C#. I'm close but the top and bottom of the mesh is not solid. Here is my method to draw a mesh

[CommandMethod("TESTSIMPLEMESH")]
public void TestSimpleMesh()
{
    // Get the current document and database, and start a transaction
    Database _database = HostApplicationServices.WorkingDatabase;
    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();

        /*
         * M indicates No of rows and N indicates No of columns, visualize it as Grid
         * So to have cube, we need two rows of vertices and 4 colomns of vertices
         * Now we need to close last column of vertices with first column of vertices that makes a simple cube or else planar surface with facets.
         */
        acPolyMesh.MSize = 2;
        acPolyMesh.NSize = 4;
        acPolyMesh.MakeNClosed(); //This function sets the PolygonMesh to be closed in the N direction. This means that the mesh will be treated as continuous from the last row on to the first row.

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

And here is the result

enter image description here

Which looks good until you get a bird's eye view of it...

enter image description here

So the sides are solid but the top and bottom aren't. How can I change the above method so that all 6 sides are solid?

Nick Gilbert
  • 4,159
  • 8
  • 43
  • 90
  • Have a look at this: http://through-the-interface.typepad.com/through_the_interface/2012/09/generating-a-transient-box-inside-autocad-using-net.html – Russ Clarke Sep 15 '15 at 17:10

1 Answers1

3

If you want a solid, use Solid3d.CreateBox instead of a PolygonMesh.

If you want a mesh, you should use the SubDMesh class instead of PolygonMesh which creates an old mesh the hard way.

[CommandMethod("CREATESUBDMESH")]
public void CreateSubDMesh()
{
  Document doc = Application.DocumentManager.MdiActiveDocument;
  Database db = doc.Database;
  Editor ed = doc.Editor;
  using (Transaction tr = db.TransactionManager.StartTransaction())
  {
    var mesh = new SubDMesh();
    mesh.Setbox(100, 100, 100, 1, 1, 1, 0);
    var currentSpace = (BlockTableRecord) tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
    currentSpace.AppendEntity(mesh);
    tr.AddNewlyCreatedDBObject(mesh, true);
    tr.Commit();
  }
}
Maxence
  • 12,868
  • 5
  • 57
  • 69
  • That actually could work perfectly. Problem is, as you currently have it, it'll always draw at the origin (0, 0, 0). Can I specify where to draw it? – Nick Gilbert Sep 15 '15 at 18:06
  • You can move it after creation: `mesh.TransformBy(Matrix3d.Displacement(new Vector3d(100, 200, 300)));` – Maxence Sep 15 '15 at 18:09
  • Gotcha, I coded it up and it almost works. My transformations aren't quite working right. Which point of the box you drew lines up with the origin? – Nick Gilbert Sep 15 '15 at 18:59
  • The box is centered on the origin. – Maxence Sep 15 '15 at 19:11