If ever, you were searching for a code snippet to see how you can create MPolygon object using .NET API, your search ends here :)
Here is the C# .NET code snippet which demonstrates Mpolygon creation. Make sure to reference AcMPolygonMGD.dll assembly in your project.
using (Transaction trans = db.TransactionManager.StartTransaction())
{
try
{
BlockTable blockTable = trans.GetObject(db.BlockTableId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead) as BlockTable;
BlockTableRecord blockTableRecord = trans.GetObject(blockTable[BlockTableRecord.ModelSpace], Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite) as BlockTableRecord;
MPolygonLoop mPolygonLoop = new MPolygonLoop();
mPolygonLoop.Add(new BulgeVertex(new Point2d(2, 2), 0));
mPolygonLoop.Add(new BulgeVertex(new Point2d(2, 1), 0));
mPolygonLoop.Add(new BulgeVertex(new Point2d(1, 1), 0));
mPolygonLoop.Add(new BulgeVertex(new Point2d(1, 2), 0));
mPolygonLoop.Add(new BulgeVertex(new Point2d(2, 2), 0));
MPolygon mPolygon = new MPolygon();
mPolygon.AppendMPolygonLoop(mPolygonLoop, false, 0);
// set the pattern scale and space
mPolygon.PatternScale = 0.05;
mPolygon.PatternSpace = 0.05;
//set the predefined pattern.
mPolygon.SetPattern(HatchPatternType.PreDefined, "ANSI37");
blockTableRecord.AppendEntity(mPolygon);
trans.AddNewlyCreatedDBObject(mPolygon, true);
trans.Commit();
}
catch (System.Exception ex)
{
ed.WriteMessage("nException: " + ex.Message);
}
}

Leave a Reply to Norman YuanCancel reply