Kean Walmsley

July 2009

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 31  

Twitter Updates

    follow me on Twitter



    « CommandComplete bonus tool | Main | Creating an AutoCAD table linked to an Excel spreadsheet using .NET »

    August 21, 2007

    Using AutoCAD's file selection dialog from .NET

    Today I started putting together some code showing how to link an Excel sheet to an AutoCAD table (watch this space - there should be something posted later this week). As I was working through it I decided enough was enough, and rather than - yet again - using the command-line to get the name of the file, I'd use the opportunity to demonstrate how to make use of AutoCAD's standard file selection dialog in your applications.

    At which point I decided to cut the original post short, as I didn't want this useful (but quite short) topic to get swamped by the broader one.

    Here's the C# code that asks the user to select an Excel spreadsheet:

    using Autodesk.AutoCAD.ApplicationServices;

    using Autodesk.AutoCAD.DatabaseServices;

    using Autodesk.AutoCAD.EditorInput;

    using Autodesk.AutoCAD.Runtime;

    using Autodesk.AutoCAD.Windows;

    namespace OpenFiles

    {

      public class Commands

      {

        [CommandMethod("SS")]

        static public void SelectSpreadsheet()

        {

          Document doc =

            Application.DocumentManager.MdiActiveDocument;

          Database db = doc.Database;

          Editor ed = doc.Editor;

          OpenFileDialog ofd =

            new OpenFileDialog(

              "Select Excel spreadsheet to link",

              null,

              "xls; xlsx",

              "ExcelFileToLink",

              OpenFileDialog.OpenFileDialogFlags.DoNotTransferRemoteFiles

            );

          System.Windows.Forms.DialogResult dr =

            ofd.ShowDialog();

          if (dr != System.Windows.Forms.DialogResult.OK)

            return;

          ed.WriteMessage(

            "\nFile selected was \"{0}\".",

            ofd.Filename

          );

        }

      }

    }

    A few notes on the arguments to the OpenFileDialog constructor:

    1. The first string is the one shown in the title bar (see below)
    2. You can pass in a default filename in the second argument, but in our case it isn't appropriate
    3. You can provide multiple file extensions upon which to filter in the third argument, separated by semi-colons
    4. The fourth argument is an internal identifier used to store data about the dialog, such as size, position and last navigated path (this data will be picked up automatically the next time the identifier is used)
    5. The fifth argument is for flags: we're choosing not to copy remote files locally if selected via a URL, in this case

    If we were opting to allow multiple file selection (passing AllowMultiple as an additional flag to the fifth argument), then we would access the files returned using ofd.GetFileNames() to access an array of strings.

    Here's the dialog we see when we run the SS command:

    Open_file_dialog

    TrackBack

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

    Listed below are links to weblogs that reference Using AutoCAD's file selection dialog from .NET:

    Comments

    Thank you, Kean.

    Roland

    Kean,
    Please don't stop just here. It will be great to see how to open the drawings and update the tables. Would it make a difference if the OpenFileDialog is called from a button on a user form? Thanks.

    It's coming, never fear... :-)

    I'm not sure what you mean by "open drawings", as I'm going to link in an Excel spreadsheet into the active drawing.

    Yes, you should be able to use an OpenFileDialog from a button on a form - I haven't looked at what happens if it's a modeless dialog, but a modal dialog should just work. A modeless dialog probably does, too, but I'd need to have a play around with it, to make sure. Unless someone else out there has already done this?

    Cheers,

    Kean

    My bad. I didn't rialize that you were opening one excel file to update the active drawing. Somehow, I though you were going to open one or more drawings and update them with information stored in an excel file. The years don't pass without damage. Thanks

    Kean, for the file types can you pass strings representing the document types?
    "*.xls Excel 97-2003 document"
    "*.xlsx Excel 2007 document"

    Excellent post, thanks!

    -Danny

    Hi Danny,

    Unfortunately not - OpenFileDialog wraps acedGetFileNavDialog(), which doesn't support filter descriptions. At least I'm 85% sure it doesn't. :-)

    Regards,

    Kean

    Hi Kean, first sorry for my english, I'm peruvian and my question is the next: Do you have an example where show me a form and it returns the corresponding data in a list?, like this for example:

    (15, "Carlos", 25.6, ("Ana", "Luis", "Pedro"), "YES")

    sorry, the list will be like this:

    (15 "Carlos" 25.6 ("Ana" "Luis" "Pedro") "YES")

    This list, I want to use it in a LISP aplication.

    Thank you Kean.

    Kean,

    I am using the Filedialog with the flags to AllowFoldersOnly. I want to specify the starting folder location. I tried entering it in the 2nd argument but that didn't work. How should I go about doing that in addition to having it allow folders only?

    David,

    This worked for me:

    OpenFileDialog ofd =
    new OpenFileDialog(
    "Select Excel spreadsheet",
    "c:\\temp\\",
    "xls; xlsx",
    "ExcelFileToLink",
    OpenFileDialog.OpenFileDialogFlags.AllowFoldersOnly |
    OpenFileDialog.OpenFileDialogFlags.DefaultIsFolder |
    OpenFileDialog.OpenFileDialogFlags.ForceDefaultFolder
    );

    Regards,

    Kean

    is there anyway to specify for it to have it filter multiple file types simultaneously? similar to the windows openfiledialog: "Points (*.csv, *.txt)|*.csv;*.txt"

    i might just end up using the windows file dialog...

    Verify your Comment

    Previewing your Comment

    This is only a preview. Your comment has not yet been posted.

    Working...
    Your comment could not be posted. Error type:
    Your comment has been posted. Post another comment

    The letters and numbers you entered did not match the image. Please try again.

    As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

    Having trouble reading this image? View an alternate.

    Working...

    Post a comment

    Feed & Share

    Search