Hi,
Here's a .NET command which allows to create a MPOLYGON in Vanilla from a selection set .
NETLOAD AcadMoplygon.dll, type PL2MPG, select entities (closed lwpolylines or 2d polylines and circles), enter the hatch pattern name (or a dot for none).
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
namespace AcadMPolygon
{
public class MpgClass : IExtensionApplication
{
public void Initialize()
{
Autodesk.AutoCAD.Runtime.SystemObjects.DynamicLinker.LoadModule("AcMPolygonObj17.dbx", false, false);
}
public void Terminate()
{
}
[CommandMethod("PL2MPG")]
public void PL2MPG()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
try
{
Database db = HostApplicationServices.WorkingDatabase;
TypedValue[] filter = { new TypedValue(-4, "<or"),
new TypedValue(0, "CIRCLE"),
new TypedValue(-4, "<and"),
new TypedValue(0, "LWPOLYLINE"),
new TypedValue(-4, "&"),
new TypedValue(70, 1),
new TypedValue(-4, "and>"),
new TypedValue(-4, "<and"),
new TypedValue(0, "POLYLINE"),
new TypedValue(-4, "&"),
new TypedValue(70, 1),
new TypedValue(-4, "<not"),
new TypedValue(-4, "&"),
new TypedValue(70, 120),
new TypedValue(-4, "not>"),
new TypedValue(-4, "and>"),
new TypedValue(-4, "or>"),
};
SelectionFilter selFilter = new SelectionFilter(filter);
PromptSelectionResult result = ed.GetSelection(selFilter);
if (result.Status == PromptStatus.OK)
{
ObjectId[] selSet = result.Value.GetObjectIds();
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
MPolygon mPolygon = new MPolygon();
foreach (ObjectId objId in selSet)
{
Entity ent = (Entity)tr.GetObject(objId, OpenMode.ForWrite);
if (ent is Polyline)
mPolygon.AppendLoopFromBoundary((Polyline)ent, true, 1E-9);
else if (ent is Circle)
mPolygon.AppendLoopFromBoundary((Circle)ent, true, 1E-9);
else
mPolygon.AppendLoopFromBoundary((Polyline2d)ent, true, 1E-9);
ent.Erase();
}
PromptStringOptions pso = new PromptStringOptions("\nEnter the hatch pattern name or a dot (.) for none: ");
pso.AllowSpaces = false;
pso.DefaultValue = (string)Application.GetSystemVariable("HPNAME");
PromptResult pr = ed.GetString(pso);
if (pr.Status == PromptStatus.OK)
{
string pat = pr.StringResult;
if (pat != ".")
{
try
{
Application.SetSystemVariable("HPNAME", pat);
mPolygon.SetPattern(HatchPatternType.PreDefined, pat);
mPolygon.PatternScale = (double)Application.GetSystemVariable("HPSCALE");
}
catch { }
}
}
mPolygon.BalanceTree();
mPolygon.BalanceDisplay();
btr.AppendEntity(mPolygon);
tr.AddNewlyCreatedDBObject(mPolygon, true);
tr.Commit();
}
}
}
catch(Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage("Error: " + ex.Message);
}
catch (System.Exception ex)
{
ed.WriteMessage("Error: " + ex.Message);
}
}
}
}