如何使用互操作COM更改autocad中块参照的颜色

时间:2013-10-04 11:40:33

标签: c# autocad

我有问题要更改acadblock的颜色。但我可以改变线条和弧线等的颜色。但是当我尝试更改块颜色时它不会改变。任何人都可以告诉我该怎么做吗?

我在这里提到了我的代码:

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

先谢谢..

1 个答案:

答案 0 :(得分:1)

您需要使用AcadApplication作为颜色对象工厂创建和指定颜色。

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

请注意,已注册的AcCmColor类必须与已加载的AutoCAD互操作库匹配。