Kean Walmsley


  • About the Author
    Kean on Google+

July 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 31    








« Free webcast: AutoCAD .NET - Developing a User Interface | Main | Gluing a point to an AutoCAD curve using overrules from .NET – Part 1 »

August 21, 2009

Allowing a user to select from multiple file formats inside AutoCAD using .NET

This is a topic that I’ve covered to some degree in a couple of previous posts:

Using AutoCAD's file selection dialog from .NET

Replacing AutoCAD's OPEN command using .NET

Neither focused on the question of allowing the user to select from a number of different file format filters (try saying “different file format filters” five times, quickly :-), so I thought I’d compare and contrast the two approaches in this post.

There are two primary mechanisms provided by AutoCAD’s .NET interface for file selection:

  1. Methods from the Editor class:
    • GetFileNameForOpen()
    • GetFileNameForSave()
  2. Classes in the Autodesk.AutoCAD.Windows namespace:
    • OpenFileDialog
    • SaveFileDialog

Within the two mechanisms the open and save choices are broadly similar, the main differences being around prompting the user to overwrite (in the case of save) and the need for a file to exist (in the case of open). In this case we’ll focus on open: providing the same treatment for save is left as an exercise for the reader.

Here’s the C# code that compares the two approaches. You may need to add additional project references to AcWindows.dll and System.Windows.Forms:

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.Windows;

 

namespace FileSelectionOptions

{

  public class Commands

  {

    [CommandMethod("SF")]

    public void SelectFiles()

    {

      Editor ed =

        Application.DocumentManager.MdiActiveDocument.Editor;

 

      // First let's use the editor method, GetFileNameForOpen()

 

      PromptOpenFileOptions opts =

        new PromptOpenFileOptions(

          "Select a file using Editor.GetFileNameForOpen()"

        );

      opts.Filter =

        "Drawing (*.dwg)|*.dwg|Design Web Format (*.dwf)|*.dwf|" +

        "All files (*.*)|*.*";

      PromptFileNameResult pr = ed.GetFileNameForOpen(opts);

 

      if (pr.Status == PromptStatus.OK)

      {

        ed.WriteMessage(

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

          pr.StringResult

        );

      }

 

      // Now let's create and use an OpenFileDialog object

 

      OpenFileDialog ofd =

        new OpenFileDialog(

          "Select a file using an OpenFileDialog",

          null,

          "dwg; dwf; *",

          "SelectFileTest",

          OpenFileDialog.OpenFileDialogFlags.DoNotTransferRemoteFiles

        );

      System.Windows.Forms.DialogResult dr = ofd.ShowDialog();

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

      {

        ed.WriteMessage(

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

          ofd.Filename

        );

      }

    }

  }

}

Now let’s see what happens when we run the SF command.

Here’s the first dialog displayed using Editor.GetFileNameForOpen():

Selecting a file using GetFileNameForOpen

And here’s the equivalent dialog using OpenFileDialog:

Selecting a file using an OpenFileDialog

Overall I prefer the way the control you have over the filter list using the GetFileNameForXxx() methods: OpenFileDialog has you provide the extensions and then attempts to determine the appropriate description (which works fine for DWGs but less so for other extensions, as far as I can tell).

TrackBack

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

Listed below are links to weblogs that reference Allowing a user to select from multiple file formats inside AutoCAD using .NET:

blog comments powered by Disqus

10 Random Posts