3

dg1 と dg2 の 2 つの図面があります。dg2 にコピーしたいブロック 'b1' を dg1 に定義しました。アーリー バインディングではうまく機能しますが、レイト バインディングでコードを試すとエラーが発生します。問題のコードは次のとおりです。

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

そして、例外が発生します:他の図面でブロックをコピーしようとすると、無効なオブジェクト配列例外が発生します。

レイトバインディングでどうすればいいですか?

Autocad2009を使用しています。

Autocad の Late Binding とオブジェクトのコピーを実装する目的

ありがとう

4

1 に答える 1

0

You can late-bind cast to an object.

http://www.dicks-clicks.com/excel/olBinding.htm

You can use entities: http://msdn.microsoft.com/en-us/library/gg309272.aspx

or the Dynamic and cast to a similar object definition or property bag, if you will.

In Microsoft Dynamics CRM 2011 and Microsoft Dynamics CRM Online, you can use the Entity class when you work with entities. In Microsoft Dynamics CRM 4.0, you used the DynamicEntity class. When initialized, the Entity class contains the logical name of an entity and a property-bag array of the entity’s attributes. This lets you use late binding so that you can work with types such as custom entities and custom attributes that were not available when your application was compiled. Because the entity base class always contains the property bag of attributes and values, the ReturnDynamicsEntities property from Microsoft Dynamics CRM 4.0 is no longer necessary. The key difference between early and late binding involves type conversion. While early binding provides compile-time checking of all types so that no implicit casts occur, late binding checks types only when the object is created or an action is performed on the type. The Entity class requires types to be explicitly specified to prevent implicit casts.

It might look something like this then

blockCollection[0] = (ACadBlock)oSourceFile.Blocks.Item("b1");

于 2013-09-19T00:46:22.750 に答える