« Using AutoCAD 2009's new transient graphics API to show point clouds from F# | Main | A simple taxonomy of programming languages »

Getting the full path of the active drawing in AutoCAD using .NET

I had a question come in by email about how to find out the full path of a drawing open inside AutoCAD. Here's some C# code that does just this:

using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;


namespace PathTest

{

  public class Commands

  {

    [CommandMethod("PTH")]

    public void DrawingPath()

    {

      Document doc =

        Application.DocumentManager.MdiActiveDocument;

      HostApplicationServices hs =

        HostApplicationServices.Current;

      string path =

        hs.FindFile(

          doc.Name,

          doc.Database,

          FindFileHint.Default

        );

      doc.Editor.WriteMessage(

        "\nFile was found in: " + path

      );

    }

  }

}

Here's what happens when you run the PTH command, with a file opened from an obscure location, just to be sure:

Command: pth

File was found in: C:\Temp\test.dwg

Update:

Tony Tanzillo wrote in a comment:

I think you could also read the Filename property of the Database to get the full path to the drawing, keeping in mind that if the drawing is a new, unsaved document, the Filename property returns the .DWT file used to create the new drawing.

Thanks, Tony - shame on me for missing the obvious. In my defence, I wrote the post in between meetings in Las Vegas, if that's any excuse. :-)

March 13, 2008 in AutoCAD, AutoCAD .NET | Permalink

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/t/trackback/876965/27071628

Listed below are links to weblogs that reference Getting the full path of the active drawing in AutoCAD using .NET:

Comments

Hello Sir,

I am trying to link AutoCAD LT 2008 with Excel spreadsheet via VB.Net .
using data link manager option provided in the Tools.

Can u sggest me how can I link an excel sheet with autocad so that
wat changes we made in excel sheet it will reflect in AutoCAD automatically.


Programming Language: VB.net
OS: XP
Machine: 32bit

Please revert me back, I wil be thankful to you.

Posted by: Prabhav Mishra | Mar 14, 2008 9:28:15 AM

Hi Kean.

I think you could also read the Filename property of the Database to get the full path to the drawing, keeping in mind that if the drawing is a new, unsaved document, the Filename property returns the .DWT file used to create the new drawing.

Posted by: Tony Tanzillo | Mar 14, 2008 8:05:48 PM

Thanks, Tony - that would certainly be much easier. :-)

Kean

Posted by: Kean | Mar 14, 2008 8:15:51 PM

Hey - What happens in Vegas stays in Vegas :P

Posted by: Tony Tanzillo | Mar 15, 2008 3:49:52 AM

Maybe what's coded in Vegas should also stay in Vegas? ;-)

Posted by: Kean | Mar 15, 2008 6:02:59 AM

Kean,
Is it possible to determine the number of drawings open?

Posted by: Rob | Mar 18, 2008 9:15:47 AM

This should get you what you need:

Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Count

Kean

Posted by: Kean | Mar 18, 2008 4:13:57 PM

Post a comment