简体   繁体   中英

How to change Color of Block reference in autocad using interop COM

I have problem to change the color of the acadblock. But i can able to change the color for line and arc etc. But when i try to change the block color it does not change. Can any body tell me how to do?

Here i have mentioned my code:

        AcadApplication acadApp;
        AcadDocument curDoc;
        AcadSelectionSet selset;
        AcadLine lin;
        AcadBlockReference blkRef;
        short[] ftype = new short[1];
        object[] fdata = new object[1];
        ftype[0] = 0;
        fdata[0] = "Line,INSERT";

        acadApp = (AcadApplication)Marshal.GetActiveObject("Autocad.Application.18");
        curDoc = acadApp.ActiveDocument;
        selset = curDoc.SelectionSets.Add("Selset2");
        selset.Select(AcSelect.acSelectionSetAll, null, null, ftype, fdata);
        foreach (AcadEntity item in selset)
        {
            if (item.ObjectName == "AcDbLine")
            {
                item.color = ACAD_COLOR.acYellow;  //here working fine

            }
            else if (item.ObjectName == "AcDbBlockReference")
            {
                item.color = ACAD_COLOR.acMagenta;  //here does not working

            }
        }
        selset.Delete();

Thanks in Advance..

You need to create and assign your color using the AcadApplication as a color object factory.

const string dwgPath = @"C:\Test.dwg";
var acadDoc = acadDocs.Open(dwgPath);

foreach (AcadEntity ent in acadDoc.ModelSpace)
{
    var block = ent as AcadBlockReference;
    if (block == null) continue;
    {
        if (!block.Name.Equals("BlockName", StringComparison.CurrentCultureIgnoreCase)) continue;

        var newColor = acadApp.GetInterfaceObject("AutoCAD.AcCmColor.18") as AcadAcCmColor;
        if (newColor != null)
        {
            newColor.ColorIndex = AcColor.acMagenta;
            block.TrueColor = newColor;
        }
    }
}

Note that the registered AcCmColor class has to match the AutoCAD interop library you have loaded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM