3

I'm trying to draw a series of polyhedrons in AutoCAD that represent a wall. Each part of the wall is one individual polyhedron which will all eventually be grouped together. I'm trying to draw them as PolygonMeshes so they can be rendered as solid objects. Here is my DrawPolyhedron method

public static ObjectId DrawPolyhedronMesh(Polyhedron polyhedronToDraw, string layerToInsertOn = "0")
{
    using (Transaction acTrans = _database.TransactionManager.StartTransaction())
    {
        // Open the Block table record for read
        BlockTable acBlkTbl = acTrans.GetObject(_database.BlockTableId, OpenMode.ForRead) as BlockTable;

        // Open the Block table record Model space for write
        BlockTableRecord acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

        // Create a polygon mesh
        PolygonMesh acPolyMesh = new PolygonMesh();
        acPolyMesh.Layer = layerToInsertOn;
        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();
        foreach(Point p in polyhedronToDraw.Vertices)
        {
            acPts3dPMesh.Add(GeometryAdapter.ClearspanPointToAcadPoint(p));
        }
        foreach (Point3d acPt3d in acPts3dPMesh)
        {
            PolygonMeshVertex acPMeshVer = new PolygonMeshVertex(acPt3d);
            acPolyMesh.AppendVertex(acPMeshVer);
            acTrans.AddNewlyCreatedDBObject(acPMeshVer, true);
        }

        // Open the active viewport
        ViewportTableRecord acVportTblRec = acTrans.GetObject(_editor.ActiveViewportId, OpenMode.ForWrite) as ViewportTableRecord;

        // Rotate the view direction of the current viewport
        acVportTblRec.ViewDirection = new Vector3d(-1, -1, 1);
        _editor.UpdateTiledViewportsFromDatabase();

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

And here is the result:

enter image description here

enter image description here

It draws my wall except for some reason each individual polyhedron is casting a line to a point way out in space. Each one is casting back to the same point. I'm very confused as to why this is as I checked the vertices collection before and after drawing the mesh and there isn't any point that looks out of place.

EDIT: Turns out the point the weird lines are going to is the origin (0, 0, 0)

Nick Gilbert
  • 4,159
  • 8
  • 43
  • 90
  • `polyhedronToDraw.Vertices` contains 16 vertices? – Chris Aug 12 '15 at 16:20
  • Shouldn't the count be `acPolyMesh.MSize * acPolyMesh.NSize == 16`? Though I have no experience with autocad, and the documentation I found doesn't seem to fully match what you have there. – Chris Aug 12 '15 at 16:26

0 Answers0