我有两张图纸,dg1 和 dg2。我在 dg1 中定义了一个块“b1”,我想在 dg2 中复制它。它在早期绑定中运行良好,但是当我在后期绑定中尝试代码时会出错。有问题的代码是:
private void Form1_Load(object sender, EventArgs e)
{
AcadApplication m_oAcadApp = null;
AcadCircle circle = null;
AcadAcCmColor color = null;
AcadDocument oSourceFile = null;
AcadDocument m_oActiveDoc = null;
try
{
object obj = Marshal.GetActiveObject("AutoCAD.Application.17");
if (obj != null)
{
m_oAcadApp = obj as AcadApplication;
m_oActiveDoc = m_oAcadApp.ActiveDocument;
foreach (AcadDocument oTempDoc in m_oAcadApp.Documents)
{
if((@"c:\d1.dwg").ToUpper() == oTempDoc.FullName.ToUpper())
{
Console.WriteLine(oTempDoc.FullName);
oSourceFile = oTempDoc;
break;
}
}
try
{
object[] objCollection = new object[1];
objCollection[0] = null;
objCollection[0] = oSourceFile.Blocks.Item("b1");
oSourceFile.CopyObjects(objCollection, m_oActiveDoc.Blocks);
}
catch (Exception ex)
{
MessageBox.Show("Opening Blocks " + ex.Message);
}
/*double[] cen = new double[] { 0, 0, 0 };
circle = m_oAcadApp.ActiveDocument.Database.ModelSpace.AddCircle(cen, 2);
color = m_oAcadApp.GetInterfaceObject("Autocad.AcCmColor.17") as AcadAcCmColor;
color.SetRGB(150, 150, 250);
circle.TrueColor = color;
m_oAcadApp.ZoomExtents();
* */
}
}
catch (Exception ex)
{
MessageBox.Show("AutoCAD not opened");
}
finally
{
// if (color != null) Marshal.FinalReleaseComObject(color);
//if (circle != null) Marshal.FinalReleaseComObject(circle);
if (m_oAcadApp != null) Marshal.FinalReleaseComObject(m_oAcadApp);
}
}
我得到异常:当我尝试在其他图形中复制一个块时出现无效的对象数组异常。
我如何在后期绑定中做到这一点?
我正在使用 Autocad 2009。
Autocad实现后期绑定和复制对象的目的
谢谢