Creating MPolygon object using .NET API

By Partha Sarkar

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

Comments

2 responses to “Creating MPolygon object using .NET API”

  1. Norman Yuan Avatar
    Norman Yuan

    Why accessing MPolygon.PatternColor property (readin g the property value) causes fatal error. The proeprty is { set; get}. Setting the color is OK. I tried it with AcadMap2009 (32bit) and AcadMap2012(64-bit), behaved the same. Is this a bug? If it is, is Autodesk going to fixed eventually?

  2. Hi Norman,
    I have seen this issue and same is reported to our QA & Engineering team. I am expecting a fix to come in future release, but at this point of time, I can’t comment on when exactly this fix would be available.
    Thanks,
    Partha Sarkar,
    ADN

Leave a Reply to Norman YuanCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading