Author Topic: how far have I come??  (Read 2583 times)

0 Members and 1 Guest are viewing this topic.

Draftek

  • Water Moccasin
  • Posts: 1501
how far have I come??
« on: August 07, 2006, 05:07:03 PM »
Not too far evidently..

Since I got such an overwhelming response to my drag and drop problem  :cry: and could not find an answer anywhere else I decided to suck it up and write an insert method. I didn't want any autocad related code in my project and I didn't want to lock this down to a particular version of autocad so I decided to attempt using late binding in c#.

Here is the result:
Code: [Select]
// method to insert a drawing file into autocad using late binding.
        // As of 8/7/06 Tested for versions 2000 to 2007
        public static void InsertDrawingLB(string sDwgName)
        {
            object acadApp = null;
            object[] parameters;
            object cmd = null;
            if (!File.Exists(sDwgName))
            {
                throw new System.ArgumentException("Drawing does not exist", "sDwgName");
               
            }
            // try to open an existing instance
            try
            {
                acadApp = (object)Marshal.GetActiveObject("AutoCAD.Application");
            }
            catch // if not successful try to start a new instance
            {
                try
                {
                    acadApp = (object)Microsoft.VisualBasic.Interaction.CreateObject(
                        "AutoCAD.Application", null);
                    parameters = new object[1];
                    parameters[0] = true;
                    cmd = acadApp.GetType().InvokeMember("Visible",
                        BindingFlags.SetProperty, null, acadApp, parameters);
                }
                catch // you don't even have autocad installed
                {
                    throw new System.Exception("Could Not find AutoCAD to open");
                   
                }
            }
            try // need to make autocad active and the top dog
            {
                cmd = acadApp.GetType().InvokeMember("Caption",
                    BindingFlags.GetProperty, null, acadApp, null);
                string caption = (string)cmd;
                Microsoft.VisualBasic.Interaction.AppActivate(caption);
                cmd = acadApp.GetType().InvokeMember("WindowState",
                    BindingFlags.GetProperty, null, acadApp, null);

                parameters = new object[1];
                parameters[0] = 3;
                cmd = acadApp.GetType().InvokeMember("WindowState",
                    BindingFlags.SetProperty, null, acadApp, parameters);
            }
            catch
            {
                throw new System.Exception("Problem forcing the AutoCAD window to Top");
            }
            object dwg = null;
            try // finally, insert the drawing using the newest technology - a script!!!
            {
                // first get the drawing
                try
                {
                    dwg = acadApp.GetType().InvokeMember("ActiveDocument",
                        BindingFlags.GetProperty, null, acadApp, null);
                }
                catch
                {
                    throw new System.Exception("You must have a drawing loaded");
                }
                string sTemp = "(Command " + @"""" + "Insert" + @"""" + " " + @"""" +
                        sDwgName + @"""" + " )" + "\r";
                parameters = new object[1];
                parameters[0] = sTemp;
                cmd = dwg.GetType().InvokeMember("SendCommand",
                    BindingFlags.InvokeMethod, null, dwg, parameters);
            }
            catch
            {
                throw new System.Exception("Could not Insert the drawing");
            }
        }

What cracks me up is, when all said and done, I had to use visual basic (.net admittedly), scripting a string using sendcommand to send an autolisp string to the command prompt.

anyway, this works if you ever need it. No reference to autocad needed (com or otherwise).

oh, and if you see a better way of doing this, please post.
Soli Deo Gloria