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

本文关键字:引用 颜色 autocad 何使用 互操作 COM 更改 | 更新日期: 2023-09-27 18:33:50

我在更改 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();

提前感谢..

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

您需要使用 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 互操作库匹配。