AutoCAD Map 3D Developer
Welcome to Autodesk’s AutoCAD Map 3D Developer Forums. Share your knowledge, ask questions, and explore popular AutoCAD Map 3D Developer topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

C# modify color of hach in mpolygon

6 REPLIES 6
Reply
Message 1 of 7
olivier.eckmann
2305 Views, 6 Replies

C# modify color of hach in mpolygon

olivier.eckmann
Advocate
Advocate

Hello,

 

I try to modifiy colr of hatch pattern's mpolygon.

I can read information in Mpolygon.Hatch.ColorIndex, but if I modify this value, color doesn't change.

If I read again after modification, I obtain the value I've modified, but color doesn't reflect my change.

All other information like angle, scale, hatchpattern can be changed succesfully, but not color.

 

Do you have any solution?

 

Thanks

 

Olivier

 

0 Likes

C# modify color of hach in mpolygon

Hello,

 

I try to modifiy colr of hatch pattern's mpolygon.

I can read information in Mpolygon.Hatch.ColorIndex, but if I modify this value, color doesn't change.

If I read again after modification, I obtain the value I've modified, but color doesn't reflect my change.

All other information like angle, scale, hatchpattern can be changed succesfully, but not color.

 

Do you have any solution?

 

Thanks

 

Olivier

 

6 REPLIES 6
Message 2 of 7

norman.yuan
Mentor
Mentor

I assume that you have set reference to AcMPolygonMgd.dll in your project. To set color of the "Pattern fill color", you do not go down the route MPolygon->Hatch->Color. Instead, simple set MPolygon.PatternColor property to desired color. Following code change a MPolygon's fill color to red:

 

public static void RunMyMPolyCommand()
        {
            Document dwg = Application.DocumentManager.MdiActiveDocument;
            Editor ed = dwg.Editor;

            PromptEntityOptions opt = new PromptEntityOptions("\nPick a MPloygon entity:");
            opt.SetRejectMessage("Invalid pick: not a MPloygon.");
            opt.AddAllowedClass(typeof(MPolygon), true);
            PromptEntityResult res = ed.GetEntity(opt);

            if (res.Status == PromptStatus.OK)
            {
                ed.WriteMessage("\nPicked entity is {0}", res.ObjectId.ObjectClass.DxfName);

                using (Transaction tran = dwg.Database.TransactionManager.StartTransaction())
                {
                    MPolygon polygon = (MPolygon)tran.GetObject(res.ObjectId, OpenMode.ForWrite);

                    Color color = Color.FromColorIndex(ColorMethod.ByColor, (short)1);
                    polygon.PatternColor = color;

                    tran.Commit();
                }

                ed.WriteMessage("\nMyCmd executed!");
            }
            else
            {
                ed.WriteMessage("\n*Cancel*");
            }
            Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
        }

 Hope this is what you are after.

Norman Yuan

Drive CAD With Code

EESignature

0 Likes

I assume that you have set reference to AcMPolygonMgd.dll in your project. To set color of the "Pattern fill color", you do not go down the route MPolygon->Hatch->Color. Instead, simple set MPolygon.PatternColor property to desired color. Following code change a MPolygon's fill color to red:

 

public static void RunMyMPolyCommand()
        {
            Document dwg = Application.DocumentManager.MdiActiveDocument;
            Editor ed = dwg.Editor;

            PromptEntityOptions opt = new PromptEntityOptions("\nPick a MPloygon entity:");
            opt.SetRejectMessage("Invalid pick: not a MPloygon.");
            opt.AddAllowedClass(typeof(MPolygon), true);
            PromptEntityResult res = ed.GetEntity(opt);

            if (res.Status == PromptStatus.OK)
            {
                ed.WriteMessage("\nPicked entity is {0}", res.ObjectId.ObjectClass.DxfName);

                using (Transaction tran = dwg.Database.TransactionManager.StartTransaction())
                {
                    MPolygon polygon = (MPolygon)tran.GetObject(res.ObjectId, OpenMode.ForWrite);

                    Color color = Color.FromColorIndex(ColorMethod.ByColor, (short)1);
                    polygon.PatternColor = color;

                    tran.Commit();
                }

                ed.WriteMessage("\nMyCmd executed!");
            }
            else
            {
                ed.WriteMessage("\n*Cancel*");
            }
            Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
        }

 Hope this is what you are after.

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 7

olivier.eckmann
Advocate
Advocate

Hello,

 

thanks a lot. until now I used polygon->hatch->patternScale or PatternSpace and all other PatternXXX properties and when I used polygon->hatch->SetHatchPattern all modification was applied (except color)

But now as you've shawn me, I can access these properties directly from mpolygon class and it works fine. And affecting PatternColor works fine too.

 

But I've a new problem to read the actual patterncolor. In your sample code, I've just added a line after obtaining the mpolygon to read its color :

 

MPolygon polygon = (MPolygon)tran.GetObject(res.ObjectId, OpenMode.ForWrite);
Color colorused = polygon.PatternColor;

...

and I've a crash "Unhandled acces violation" because PatternColor generated an exception.

So I tried to read the color of Hatch (mpoly->Hatch->Color) but its index is always 257.

 

I don't know if it can be linked, I don't use MAP3D, but Civil3D 32bit (french version) on Windows7 Pro 32bit (french too).

 

Do you succeed to read the actual PatternColor without crash?

 

Thanks 

 

Olivier

 

 

0 Likes

Hello,

 

thanks a lot. until now I used polygon->hatch->patternScale or PatternSpace and all other PatternXXX properties and when I used polygon->hatch->SetHatchPattern all modification was applied (except color)

But now as you've shawn me, I can access these properties directly from mpolygon class and it works fine. And affecting PatternColor works fine too.

 

But I've a new problem to read the actual patterncolor. In your sample code, I've just added a line after obtaining the mpolygon to read its color :

 

MPolygon polygon = (MPolygon)tran.GetObject(res.ObjectId, OpenMode.ForWrite);
Color colorused = polygon.PatternColor;

...

and I've a crash "Unhandled acces violation" because PatternColor generated an exception.

So I tried to read the color of Hatch (mpoly->Hatch->Color) but its index is always 257.

 

I don't know if it can be linked, I don't use MAP3D, but Civil3D 32bit (french version) on Windows7 Pro 32bit (french too).

 

Do you succeed to read the actual PatternColor without crash?

 

Thanks 

 

Olivier

 

 

Message 4 of 7
Anonymous
in reply to: olivier.eckmann

Anonymous
Not applicable

Hi Oliver,

 

We have received reports on 'MPolygon's PatternColor throws an exception' and I could reproduce it in Map 3D / Civil 3D (Civil 3D uses Map 3D module in it). Our QA & development team is current working on a possible fix for this issue, however, I can't comment on when exactly this fix would be available.

 

Thanks,

0 Likes

Hi Oliver,

 

We have received reports on 'MPolygon's PatternColor throws an exception' and I could reproduce it in Map 3D / Civil 3D (Civil 3D uses Map 3D module in it). Our QA & development team is current working on a possible fix for this issue, however, I can't comment on when exactly this fix would be available.

 

Thanks,

Message 5 of 7
olivier.eckmann
in reply to: Anonymous

olivier.eckmann
Advocate
Advocate

Hi Partha,

 

thanks for your information.

 

Olivier

0 Likes

Hi Partha,

 

thanks for your information.

 

Olivier

Message 6 of 7
Markus_Strauss
in reply to: Anonymous

Markus_Strauss
Participant
Participant

Today i needed this information too - It seems that it is still not fixed in Acad Map 2016

0 Likes

Today i needed this information too - It seems that it is still not fixed in Acad Map 2016

Message 7 of 7

veli_vaisanen
Enthusiast
Enthusiast

Hi,

10 years is quite long time scale to fix .NET wrapper problem. One solution is to use COM API.

Br. Veli

MPolygon mp = (MPolygon)tr.GetObject(o, OpenMode.ForWrite);
if (mp != null)
{
    //-------------------------------------------------------------------------------------
    // Try to mofify entity from COM object. Need to reference AutoCAD MPolygon COM library
    // C:\Program Files\Common Files\Autodesk Shared\AcMPolygon24enu.tlb
    //-------------------------------------------------------------------------------------
    AcMPolygon mpComObj = (AcMPolygon)mp.AcadObject;
    if (mpComObj != null)
    {
        //AXDBLib.AcadAcCmColor mpTrueColor = mpComObj.TrueColor;
        //mpComObj.PatternFillTrueColor = mpTrueColor;
        mpComObj.PatternFillTrueColor = tmpHatchCOMColor;
    }

    //------------------------------------------------------------------------------------------
    // This doesn´t work yet. AutoCAD throw Violation exception if you try to read PatternColor
    //------------------------------------------------------------------------------------------
    //Autodesk.AutoCAD.Colors.Color mpPatternColor = mp.PatternColor;
    //Autodesk.AutoCAD.Colors.ColorMethod mpPatternColorMethod = mp.PatternColor.ColorMethod;
}

 

Hi,

10 years is quite long time scale to fix .NET wrapper problem. One solution is to use COM API.

Br. Veli

MPolygon mp = (MPolygon)tr.GetObject(o, OpenMode.ForWrite);
if (mp != null)
{
    //-------------------------------------------------------------------------------------
    // Try to mofify entity from COM object. Need to reference AutoCAD MPolygon COM library
    // C:\Program Files\Common Files\Autodesk Shared\AcMPolygon24enu.tlb
    //-------------------------------------------------------------------------------------
    AcMPolygon mpComObj = (AcMPolygon)mp.AcadObject;
    if (mpComObj != null)
    {
        //AXDBLib.AcadAcCmColor mpTrueColor = mpComObj.TrueColor;
        //mpComObj.PatternFillTrueColor = mpTrueColor;
        mpComObj.PatternFillTrueColor = tmpHatchCOMColor;
    }

    //------------------------------------------------------------------------------------------
    // This doesn´t work yet. AutoCAD throw Violation exception if you try to read PatternColor
    //------------------------------------------------------------------------------------------
    //Autodesk.AutoCAD.Colors.Color mpPatternColor = mp.PatternColor;
    //Autodesk.AutoCAD.Colors.ColorMethod mpPatternColorMethod = mp.PatternColor.ColorMethod;
}

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report