November 2014

Sun Mon Tue Wed Thu Fri Sat
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30            










« Microbenchmarking C# code | Main | Creating an AutoCAD linetype programmatically using .NET »

December 21, 2007

Launching AutoCAD with a specific profile using .NET

This is a follow-up to this previous post, where we used COM to launch the AutoCAD process. Tony Tanzillo rightly pointed out the code in that post could be simplified slightly, so check out his comment for the specifics.

Today we're going to look at launching AutoCAD more manually, allowing us to control the working folder and specify command-line parameters. This is to address the specific question of choosing an alternative startup profile for the application, but AutoCAD's command-line parameters allow you to do a great deal more than that.

We're going to use the System.Diagnostics namespace to specify our options and launch our process, and then use COM to connect to it (so we can use the COM Automation API, as before, to run commands, etc.).

Here are the namespace declarations:

using System;

using System.Diagnostics;

using System.Runtime.InteropServices;

using Autodesk.AutoCAD.Interop;

And here's the C# code to implement your function or button message-handler:

  const string progID = "AutoCAD.Application.17.1";


  // The @ means it's a literal string - no need

  // for double backslashes


  const string exePath =

    @"c:\Program Files\Autodesk\AutoCAD 2008\acad.exe";


  AcadApplication acApp = null;


  // Let's first check we don't have AutoCAD already running


  try

  {

    acApp =

      (AcadApplication)Marshal.GetActiveObject(progID);

  }

  catch {}


  if (acApp != null)

  {

    MessageBox.Show(

      "An instance of AutoCAD is already running."

    );

  }

  else

  {

    try

    {

      // Use classes from the System.Diagnostics namespace

      // to launch our AutoCAD process with command-line

      // options


      ProcessStartInfo psi =

        new ProcessStartInfo(exePath, "/p myprofile");

      psi.WorkingDirectory = @"c:\temp";

      Process pr = Process.Start(psi);


      // Wait for AutoCAD to be ready for input

      // This doesn't wait until AutoCAD is ready

      // to receive COM requests, it seems


      pr.WaitForInputIdle();


      // Connect to our process using COM


      // We're going to loop infinitely until we get the

      // AutoCAD object.         

      // A little risky, unless we implement a timeout

      // mechanism or let the user cancel


      while (acApp == null)

      {

        try

        {

          acApp =

            (AcadApplication)Marshal.GetActiveObject(progID);

        }

        catch

        {

          // Let's let the application check its message

          // loop, in case the user has exited or cancelled


          Application.DoEvents();

        }

      }

    }

    catch (Exception ex)

    {

      MessageBox.Show(

        "Cannot create or attach to AutoCAD object: "

        + ex.Message

      );

    }

  }


  if (acApp != null)

  {

    acApp.Visible = true;

    acApp.ActiveDocument.SendCommand("_MYCOMMAND ");

  }

Well, that's it for 2007. A Happy Holidays to you all, and a Merry Christmas & Happy New Year to those of you who celebrate these events.

See you in 2008!

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d83452464869e200e54fbfa1cd8834

Listed below are links to weblogs that reference Launching AutoCAD with a specific profile using .NET:

blog comments powered by Disqus

Feed/Share

10 Random Posts