Untitled

 avatar
unknown
plain_text
20 days ago
1.6 kB
0
Indexable
[CommandMethod("FindLargestRectangle")]
public void FindLargestRectangleCommand()
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Editor ed = doc.Editor;

    PromptEntityOptions peo = new PromptEntityOptions("\nSelect a polyline: ");
    peo.SetRejectMessage("\nInvalid selection. Please select a polyline.");
    peo.AddAllowedClass(typeof(Polyline), true);

    PromptEntityResult per = ed.GetEntity(peo);

    if (per.Status != PromptStatus.OK)
    {
        ed.WriteMessage("\nNo polyline selected.");
        return;
    }

    using (Transaction tr = doc.TransactionManager.StartTransaction())
    {
        Polyline polyline = tr.GetObject(per.ObjectId, OpenMode.ForRead) as Polyline;

        if (polyline != null)
        {
            Polyline largestRect = LargestInscribedRectangle.GetLargestRectangleInsidePolyline(polyline);

            if (largestRect != null)
            {
                // Add the resulting rectangle to the drawing
                BlockTable bt = tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                btr.AppendEntity(largestRect);
                tr.AddNewlyCreatedDBObject(largestRect, true);
                tr.Commit();
            }
            else
            {
                ed.WriteMessage("\nCould not find a valid rectangle.");
            }
        }
        else
        {
            ed.WriteMessage("\nSelected object is not a polyline.");
        }
    }
}
Leave a Comment