Author Topic: C# Code to Open AutoCAD (Existing Instance Issue)  (Read 3139 times)

0 Members and 1 Guest are viewing this topic.

Bill Tillman

  • Guest
C# Code to Open AutoCAD (Existing Instance Issue)
« on: November 05, 2014, 11:35:26 AM »
I've been using this VB.NET code to check for existing instance of AutoCAD and use it or open a new one if none exists:
Code: [Select]
Dim vAcadApp As AcadApplication
        Dim vAcadDoc As AcadDocument
Try
            If Process.GetProcessesByName("acad").Length > 0 Then
                vAcadApp = GetObject(, "AutoCAD.Application.19")
            Else
                vAcadApp = New AcadApplication
            End If
            vAcadApp.Visible = True
            vAcadApp.WindowState = AcWindowState.acMax
            vAcadDoc = vAcadApp.Documents.Open(DwgPath & DwgName, True)
I need to convert this over to C# and so far I've only got this part working:


Code: [Select]
using System;
using AutoCAD;

    class LaunchAutoCAD1
    {
        public static void StartACAD(string vFilename)
        {
            AcadApplication vAcadApp;
            AcadDocument vAcadDoc;
            try
            {
                vAcadApp = new AcadApplication();
                vAcadApp.Visible = true;
                vAcadApp.WindowState = AcWindowState.acMax;
                vAcadDoc = vAcadApp.Documents.Open(vFilename,true);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
            }
        }
    }

This works but unfortunately I seem to be having touble with the GetObject method to check if AutoCAD is already running. I've searched and searched and keep finding only VB.NET code or the same question by someone else which no one ever answered. Any advice would be appreciated.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: C# Code to Open AutoCAD (Existing Instance Issue)
« Reply #1 on: November 05, 2014, 12:40:31 PM »
Make the C# purist cringe and add a reference Microsoft.VisualBasic assembly then

Code - C#: [Select]
  1.             Microsoft.VisualBasic.Interaction.GetObject(Class: "AutoCAD.Application.19");
  2.  


There are some other useful ones in VB namespace
Code - C#: [Select]
  1.             Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory("C:\\Old", "C:\\New");
  2.  
           

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: C# Code to Open AutoCAD (Existing Instance Issue)
« Reply #2 on: November 05, 2014, 12:45:34 PM »
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

Bill Tillman

  • Guest
Re: C# Code to Open AutoCAD (Existing Instance Issue)
« Reply #3 on: November 05, 2014, 02:35:21 PM »
Jeff H .... I know what you mean. I ran this through a VB to C# converter on line and that's what it came up with. Unfortunately this is a true VB.NET to C# conversion and there are some politics involved here (office politics) so taking any approach that leaves even the slightest bit of visual Basic in the new code will just defeat the purpose of converting in the first place. The VB.NET code has run rock solid for two years now but we have to move this over to C#.....because!

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: C# Code to Open AutoCAD (Existing Instance Issue)
« Reply #4 on: November 05, 2014, 04:48:25 PM »
System.Diagnostics.Process.GetProcessesByName might be worth a look too -

http://msdn.microsoft.com/en-us/library/z3w4xdc9(v=vs.110).aspx

Although I don't see what all the fuss is about :)
Changing a solid code set from VB.net to C# just to change syntax is just inviting a chance for new bugs, I know you have little say in this but compiling the old code into its own dll and using it as a lib for use with new code would save a lot of work and headache IMO.
It's giving someone a job I guess :)
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien