Author Topic: Connecting to ACAD 2010 w/ C#  (Read 8230 times)

0 Members and 1 Guest are viewing this topic.

ArgV

  • Guest
Connecting to ACAD 2010 w/ C#
« on: June 28, 2010, 05:41:31 AM »
Well, I asked about C++ a while ago, and many people pointed me to .NET. I took a class for C++ in school, and realized they are right. With C++ I have to do almost everything (or at a minimum, learn the objectARX library).

So, my new question: Connecting to autocad using C#.

I have to add a reference. so, I go to references>>add reference>>[COM TAB]>>Autocad 2010 type library>>[OK]

great.

then I write something like this:

Code: [Select]
using System;
using System.Runtime.InteropServices;
using AutoCAD;

namespace AcadExample
{

    public class AutoCADConnector
    {

        public AcadApplication GetAutoCAD()
        {

            return

            (AcadApplication)Marshal.GetActiveObject("AutoCAD.Application.18");

        }

    }

}

it doesn't work. I get: Error   1   The type or namespace name 'AutoCAD' could not be found (are you missing a using directive or an assembly reference?)

Any ideas? Do I need to add a path? I know it's probably something stupid, but it's alot easier to ask, then it is to scour every page of every document to find the one simple line that tells me how to do it. !! :)

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8715
  • AKA Daniel
Re: Connecting to ACAD 2010 w/ C#
« Reply #1 on: June 28, 2010, 05:52:02 AM »
would you like me to move this to the .NET area?

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Connecting to ACAD 2010 w/ C#
« Reply #2 on: June 28, 2010, 09:28:55 AM »
Skip the COM part, and go to the browse tab, and reference acmgd.dll and acdbmdg.dll

I set copy local to false, and go from there
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

ArgV

  • Guest
Re: Connecting to ACAD 2010 w/ C#
« Reply #3 on: June 29, 2010, 04:19:08 AM »
would you like me to move this to the .NET area?

Sorry, sure. if you want. I guess thats closer to the actual topic than C++.

ArgV

  • Guest
Re: Connecting to ACAD 2010 w/ C#
« Reply #4 on: July 02, 2010, 04:57:18 AM »
Skip the COM part, and go to the browse tab, and reference acmgd.dll and acdbmdg.dll

I set copy local to false, and go from there

Ok, I tried that, I did the tutorial from the autodesk developer website (a program that compiles a .DLL file, use NETLOAD to run it, and it writes an MTEXT object on the screen).

BUT, here is what I would like to start out with, to make things plain and simple. (I like simplicity) I've scoured the google machine looking for answers to a simple question, and only found articles that did not help me. I would LOVE some simple help, once I get started, I think I can use my limited knowledge of the autocad objects and properties to fumble my way through stuff. But, I go ONE concept at a time. (or sometimes combine 2 or 3) :)

Here is what I want to do:

using visual studios 2008, and C#, and autocad 2010.

1. Start a project using just a regular console application (to start out with)
2. write code to start an open file dialog box to find a .dwg file.
3. the program should open the file (without having autocad already open) and get all the block names contained within the file.
4. output the names to the screen.
5. close the autocad file.

PLEASE, I DO NOT NEED anyone to write this whole thing for me, I simply need the first part.. step by step method to connect my program to a chosen autocad file.

if you want to write the whole thing out, fine, but I don't need that. I just need to know how to basically run a program .exe file that can open an autocad file.

thank you very much!

-argV



Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Re: Connecting to ACAD 2010 w/ C#
« Reply #5 on: July 02, 2010, 10:58:02 AM »
Well hopefully one of the more knowledgeable code gurus will step in, but here's a start. This code works fine in Debug mode, but when run by double clicking the EXE file in Explorer Windows reports it has a problem and must close, yet an instance of Acad is left running...it is invisible though so must be closed with Task Manager OR run the program again so it will find the running instance then it will be set to visible.

Anyway, accessing Acad from outside of Acad still requires the COM interops, AFAIK. Create your project, add the AutoCAD 10 Type Library and the AutoCAD/ObjectDBX Common 18.0 Type Library to the references.

then this will either get a running instance or create a new one:
Code: [Select]
using System;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;

namespace Sample_CSharp_Acad_connect
{
    class Program
    {
        private static IAcadApplication oAcadApp = null;
        private static string sAcadID = "AutoCAD.Application.18";

        static void Main()
        {
            try  //get a running AutoCAD instance if avaialbale
            {
                oAcadApp = (IAcadApplication)System.Runtime.InteropServices.Marshal.GetActiveObject(sAcadID);
            }
            catch(Exception) //none found so start a new instance
            {
                System.Type AcadProg = System.Type.GetTypeFromProgID(sAcadID);
                oAcadApp = (IAcadApplication)System.Activator.CreateInstance(AcadProg);
            }
            if (oAcadApp != null)
            {
                oAcadApp.Visible = true; //could leave this false to hide Acad from the user
                //do whatever with Acad
                //oAcadApp.Quit();
            }
        }
    }
}

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Connecting to ACAD 2010 w/ C#
« Reply #6 on: July 02, 2010, 10:58:20 AM »
ok, strike what I said, since your are connecting to Autocad from the outside world.  If you were going to use the DLL from within Autocad, you would use the 2 dlls I mentioned.  I have never connected from the outside world, so we have to wait for someone to come along that has.

edit : Jeff will get you started
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

ArgV

  • Guest
Re: Connecting to ACAD 2010 w/ C#
« Reply #7 on: July 02, 2010, 01:21:08 PM »
ok, strike what I said, since your are connecting to Autocad from the outside world.  If you were going to use the DLL from within Autocad, you would use the 2 dlls I mentioned.  I have never connected from the outside world, so we have to wait for someone to come along that has.

edit : Jeff will get you started

Yeah, I realized I wasn't exactly clear on my intentions, so I tried to be more clear. I guess it worked! ;) Thank you.

Basically, I'm working on applications for people who may have autocad installed on their machines, and perhaps want to know some basic information on a drawing, but have no interest in actually learning how to do so from within autocad. After all, as a programmer, isn't the job to make other people's lives easier?hehe. :)

so, yeah, that is the idea here. Just to open a drawing, ODBX-style and dump out some information and close.

I'm gonna try Jeff's code and see if I can't blindly fall into making it work, as he mentioned it has some issues. That being, while I wait for someone who has 'been there, done that' so to speak. :)

thank you,

-argV

ArgV

  • Guest
Re: Connecting to ACAD 2010 w/ C#
« Reply #8 on: July 02, 2010, 01:22:40 PM »
Well hopefully one of the more knowledgeable code gurus will step in, but here's a start. This code works fine in Debug mode, but when run by double clicking the EXE file in Explorer Windows reports it has a problem and must close, yet an instance of Acad is left running...it is invisible though so must be closed with Task Manager OR run the program again so it will find the running instance then it will be set to visible.

Anyway, accessing Acad from outside of Acad still requires the COM interops, AFAIK. Create your project, add the AutoCAD 10 Type Library and the AutoCAD/ObjectDBX Common 18.0 Type Library to the references.

then this will either get a running instance or create a new one:
Code: [Select]
using System;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;

namespace Sample_CSharp_Acad_connect
{
    class Program
    {
        private static IAcadApplication oAcadApp = null;
        private static string sAcadID = "AutoCAD.Application.18";

        static void Main()
        {
            try  //get a running AutoCAD instance if avaialbale
            {
                oAcadApp = (IAcadApplication)System.Runtime.InteropServices.Marshal.GetActiveObject(sAcadID);
            }
            catch(Exception) //none found so start a new instance
            {
                System.Type AcadProg = System.Type.GetTypeFromProgID(sAcadID);
                oAcadApp = (IAcadApplication)System.Activator.CreateInstance(AcadProg);
            }
            if (oAcadApp != null)
            {
                oAcadApp.Visible = true; //could leave this false to hide Acad from the user
                //do whatever with Acad
                //oAcadApp.Quit();
            }
        }
    }
}

Sweet. that will def. get me started! I'll tinker with it and see what it does. Hopefully someone will come by that knows what may be wrong.

thanks again!

-argV

ArgV

  • Guest
A result! :)
« Reply #9 on: July 02, 2010, 02:44:24 PM »
Well hopefully one of the more knowledgeable code gurus will step in, but here's a start. This code works fine in Debug mode, but when run by double clicking the EXE file in Explorer Windows reports it has a problem and must close, yet an instance of Acad is left running...it is invisible though so must be closed with Task Manager OR run the program again so it will find the running instance then it will be set to visible.

Anyway, accessing Acad from outside of Acad still requires the COM interops, AFAIK. Create your project, add the AutoCAD 10 Type Library and the AutoCAD/ObjectDBX Common 18.0 Type Library to the references.

then this will either get a running instance or create a new one:
Code: [Select]
using System;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;

namespace Sample_CSharp_Acad_connect
{
    class Program
    {
        private static IAcadApplication oAcadApp = null;
        private static string sAcadID = "AutoCAD.Application.18";

        static void Main()
        {
            try  //get a running AutoCAD instance if avaialbale
            {
                oAcadApp = (IAcadApplication)System.Runtime.InteropServices.Marshal.GetActiveObject(sAcadID);
            }
            catch(Exception) //none found so start a new instance
            {
                System.Type AcadProg = System.Type.GetTypeFromProgID(sAcadID);
                oAcadApp = (IAcadApplication)System.Activator.CreateInstance(AcadProg);
            }
            if (oAcadApp != null)
            {
                oAcadApp.Visible = true; //could leave this false to hide Acad from the user
                //do whatever with Acad
                //oAcadApp.Quit();
            }
        }
    }
}

Sweet. that will def. get me started! I'll tinker with it and see what it does. Hopefully someone will come by that knows what may be wrong.

thanks again!

-argV

Thank you Jeff, I got exactly what I was looking for. Well, theres alot more I want to do with this, but this is the start I was hoping for.

Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;
using System.Windows.Forms;
using Autodesk.AutoCAD;


namespace test
{
    class Program
    {
        private static IAcadApplication oAcadApp = null;
        private static string sAcadID = "AutoCAD.Application.18";
        [STAThread]

        static void Main()
        {
           
            string fileName = "";

          OpenFileDialog fd = new OpenFileDialog();
            fd.Title = "C# File dialog";
            fd.InitialDirectory = @"c:\";
            fd.Filter = "dwgFiles|*.dwg";
            fd.FilterIndex = 2;
            fd.RestoreDirectory = true;
            if(fd.ShowDialog() == DialogResult.OK)
            {
                fileName = fd.FileName;
            }

            try  //get a running AutoCAD instance if avaialbale
            {
                oAcadApp = (IAcadApplication)System.Runtime.InteropServices.Marshal.GetActiveObject(sAcadID);
            }
            catch (Exception) //none found so start a new instance
            {
                System.Type AcadProg = System.Type.GetTypeFromProgID(sAcadID);
                oAcadApp = (IAcadApplication)System.Activator.CreateInstance(AcadProg);
            }
            if (oAcadApp != null)
            {
               
                oAcadApp.Visible = false; //could leave this true to enable user to see

                //Open the DWG file with the filename provided by the file browser.
                oAcadApp.Documents.Open(fileName, true, null);
             
                //do whatever with Acad
                foreach (AcadBlock block in oAcadApp.ActiveDocument.Blocks)
                {
                    Console.WriteLine(block.Name.ToString());
                }

                //close document, do not save changes
                oAcadApp.ActiveDocument.Close(false, fileName);
               
            }

            Console.Read();
        }
    }

     
   
}

BTW, closing the file at the activeDocument level seemed to keep it from hanging. Still lots of testing to do, and still looking for any helpful hints.

thanks.

-argV