Volatil3
Volatil3

Reputation: 15008

AutoCad: Getting 'Invalid object array exception' in Late Binding while copying Objects

I have two drawings, dg1 and dg2. I have defined a block 'b1' in dg1 which I want to copy in dg2. It works well in Early Binding but giving error when I try code in late binding. The code in question is:

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

And I get exception: Invalid object array exception while I try to copy a block in other drawing.

How do I do it in late Binding?

I am using Autocad 2009.

The Objective of implement Late Binding for Autocad and copying Object

Thanks

Upvotes: 3

Views: 684

Answers (1)

FlavorScape
FlavorScape

Reputation: 14309

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

Upvotes: 0

Related Questions