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. 🙂

20 responses to “Getting the full path of the active drawing in AutoCAD using .NET”

  1. 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.

  2. 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.

  3. Thanks, Tony - that would certainly be much easier. 🙂

    Kean

  4. Hey - What happens in Vegas stays in Vegas 😛

  5. Maybe what's coded in Vegas should also stay in Vegas? 😉

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

  7. This should get you what you need:

    Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Count

    Kean

  8. I've just recently discovered that reading Filename property of Database property is not reliable way of getting full path. If autosaving is turned on then Filename is a path of the temporary saved file with extension '.sv$'. Does anybody know a more reliable way?

    Thanks.

    1. Yes I"m finding that problem too. Did you find a solution to that issue?

  9. Kean Walmsley Avatar

    How does my original code work in this scenario?

    Kean

  10. The original code works fine. Thanks Kean!

  11. I'm a bit late, but the DWGNAME and DWGPREFIX system variables store this information as well. String concatenating DWGPREFIX & DWGNAME will give the full path and filename of the drawing.

  12. Better late than never, Mark! 🙂

    It's true you could also use sysvars, although I think Database.Filename is probably cleanest, overall.

    Kean

  13. Kean,

    I receive either "eFileError" or "eWasOpenForWrite" error when running the following... Any Clue?

    Imports Autodesk.AutoCAD.ApplicationServices
    Imports Autodesk.AutoCAD.Runtime
    Imports Autodesk.AutoCAD.DatabaseServices

    Public Class Class1
    <commandmethod("getfilename")> _
    Public Sub MyMethod()

    Dim doc As Document = Application.DocumentManager.MdiActiveDocument
    Dim hs As HostApplicationServices = HostApplicationServices.Current
    Dim path As String = hs.FindFile(doc.Name, doc.Database, FindFileHint.Default)

    End Sub

    End Class

  14. Jon,

    It's not clear whether your method is a command or whether it's being called from a modeless UI (which might make a difference, but then again it might not... typically you'd need to lock the document if using the code from a modeless UI, but then we're not really doing anything here that would need the document to be locked, as far as I can tell).

    This code has worked for me, but there may be something about your specific context that's different.

    Incidentally, did you check whether Database.Filename worked for you?

    Regards,

    Kean

  15. My method is a command, not sure why this part didnt copy in originally...

    <commandmethod("getdrawingpath")> _

    I will try your code directly and see what happens.

    Database.Filename does work, but as mentioned above it changes to a temp file after auto save.(Which I cant take chances on)

    Thanks,

    Jon

  16. Ah - it's because the CommandMethod attribute is enclosed in angled brackets in VB, and TypePad interprets it as an (invalid) HTML tag.

    Let me know how you get on: if you can describe the steps to reproduce the problem consistently, I'll do what I can to help.

    Kean

  17. Hi Kean,

    Just to complete for future readers: Toni Tanzillo is not quite right. The database Filename property shows the autosave name (.sv$), if the file was not saved until last autosave, even if it is a named dwg.

    To get the document path, one should use the Document.Name, keeping in mind that if the file is unsaved, this value does not have a path prefix. Use the following code to check that:

    using System.IO;
    using System.Diagnostic;
    using Autodesk.AutoCAD.ApplicationServices;

    Document oDocument = Application.DocumentManager.MdiActiveDocument;
    if(Path.GetDirectoryName(oDocument.Name).Equals(string.Empty))
    Debug.Assert(false, "Hoops! Usaved document.");

  18. Hi folks, for completeness i'll add the following:

    string drawingPath = Application.DocumentManager.MdiActiveDocument.Database.Filename;

    And if you just want the filePath (without the drawing extension you can do);

    Path.GetDirectoryName(drawingPath)

    hope this helps.

    Ben

    1. ApplicationServices.Application.DocumentManager.MdiActiveDocument.Name could be also used.

Leave a Reply

Your email address will not be published. Required fields are marked *