TheSwamp

Code Red => .NET => Topic started by: GumbyCAD on April 16, 2014, 09:14:58 PM

Title: Autodesk.AutoCAD.Windows.OpenFileDialog - Force to Folder Location
Post by: GumbyCAD on April 16, 2014, 09:14:58 PM
Here is one that has been bugging me for some time.

I am trying to use the AutoDESK Open File Dialogbox to open files.
But when I try to force the location it never goes to the specified path.

Have I lost the plot here and done some thing wrong... or is it a bug!


Code - Visual Basic: [Select]
  1.         Dim sPath As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments
  2.         Dim sTypes As String = "dwg; dwf; *"
  3.  
  4.         Try
  5.             Dim flags As Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags = Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.ForceDefaultFolder
  6.             If System.IO.Directory.Exists(sPath) = False Then
  7.                 sPath = Global.My.Computer.FileSystem.SpecialDirectories.MyDocuments
  8.             End If
  9.             Dim ofd As New Autodesk.AutoCAD.Windows.OpenFileDialog("", sPath, sTypes, "SelectFileTest", flags)
  10.             Dim dr As System.Windows.Forms.DialogResult = ofd.ShowDialog()
  11.             If dr = System.Windows.Forms.DialogResult.OK Then
  12.                 txtExternalFile.Text = ofd.Filename
  13.             Else
  14.                 txtExternalFile.Text = Nothing
  15.             End If
  16.         Catch ex As Exception
  17.             txtExternalFile.Text = Nothing
  18.         End Try
  19.  




edit:kdub :-> formatting code=vb.net
Title: Re: Autodesk.AutoCAD.Windows.OpenFileDialog - Force to Folder Location
Post by: Kerry on April 16, 2014, 10:25:24 PM
Hi Stephan,
This worked for me :-

Code - C#: [Select]
  1. using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
  2. using OpenFileDialog = Autodesk.AutoCAD.Windows.OpenFileDialog;
  3.  
  4. // < .. >
  5.  
  6.  
  7.             string documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  8.             Environment.SetEnvironmentVariable("MYDOCUMENTS", documents);
  9.  
  10.             var ofd = new OpenFileDialog("Select a file using an OpenFileDialog", documents,
  11.                             "dwg; dwf; *",
  12.                             "File Date Test T22",
  13.                             OpenFileDialog.OpenFileDialogFlags.DefaultIsFolder |
  14.                             OpenFileDialog.OpenFileDialogFlags.ForceDefaultFolder // .AllowMultiple
  15.                           );
  16.             DialogResult sdResult = ofd.ShowDialog();
  17.  
  18.             if (sdResult != System.Windows.Forms.DialogResult.OK) return;
  19.  
  20.             string remoteFileName = ofd.Filename;
  21.  
  22.  
Title: Re: Autodesk.AutoCAD.Windows.OpenFileDialog - Force to Folder Location
Post by: Kerry on April 16, 2014, 10:30:32 PM

using just

OpenFileDialog.OpenFileDialogFlags.ForceDefaultFolder

seems to use the dll location ( assumption requires full testing )


added afterthought.
I wish you used C# Gumby ... your code would be less of a headache (for me) to look at  :evil:
You seem to be progressing with your .NET pretty well  :-)
Title: Re: Autodesk.AutoCAD.Windows.OpenFileDialog - Force to Folder Location
Post by: GumbyCAD on April 16, 2014, 10:52:14 PM
Bingo.

Code - Visual Basic: [Select]
  1.         Try
  2.  
  3.             Dim flags As Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags = Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.DoNotTransferRemoteFiles _
  4.                                                                                        Or Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.DefaultIsFolder _
  5.                                                                                        Or Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.ForceDefaultFolder
  6.  
  7.             Dim ofd As New Autodesk.AutoCAD.Windows.OpenFileDialog("TITLE", _
  8.                                                                    My.Computer.FileSystem.SpecialDirectories.MyDocuments, _
  9.                                                                    "dwg; dwt; *", _
  10.                                                                    "SelectFileTest", _
  11.                                                                    flags)
  12.  
  13.             Dim dr As System.Windows.Forms.DialogResult = ofd.ShowDialog()
  14.             If dr = System.Windows.Forms.DialogResult.OK Then
  15.                 txtExternalFile.Text = ofd.Filename
  16.             Else
  17.                 txtExternalFile.Text = ""
  18.             End If
  19.         Catch ex As Exception
  20.             SKM.Utilities.ExceptionMessageDisplay(ex, System.Reflection.MethodBase.GetCurrentMethod.Name(), System.Reflection.MethodBase.GetCurrentMethod.Module.Name)
  21.             '                System.Environment.CurrentDirectory = current
  22.            txtExternalFile.Text = ""
  23.         End Try
  24.  

Code - C#: [Select]
  1. try {
  2.         Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags flags = Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.DoNotTransferRemoteFiles | Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.DefaultIsFolder | Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.ForceDefaultFolder;
  3.  
  4.         Autodesk.AutoCAD.Windows.OpenFileDialog ofd = new Autodesk.AutoCAD.Windows.OpenFileDialog("TITLE", My.Computer.FileSystem.SpecialDirectories.MyDocuments, "dwg; dwt; *", "SelectFileTest", flags);
  5.  
  6.         System.Windows.Forms.DialogResult dr = ofd.ShowDialog();
  7.         if (dr == System.Windows.Forms.DialogResult.OK) {
  8.                 txtExternalFile.Text = ofd.Filename;
  9.         } else {
  10.                 txtExternalFile.Text = "";
  11.         }
  12. } catch (Exception ex) {
  13.         SKM.Utilities.ExceptionMessageDisplay(ex, System.Reflection.MethodBase.GetCurrentMethod.Name(), System.Reflection.MethodBase.GetCurrentMethod.Module.Name);
  14.         //                System.Environment.CurrentDirectory = current
  15.         txtExternalFile.Text = "";
  16. }
  17.  
Title: Re: Autodesk.AutoCAD.Windows.OpenFileDialog - Force to Folder Location
Post by: GumbyCAD on April 16, 2014, 10:58:11 PM
I can read C# its just doesn't seam logical.

VB.net seams to read nicely from left to right....

My latest enjoyment on .NET is DataSet / DataTables / DataViews and Binding this data to controls and using LINQ to filter data.
Absolutely awesome.
Title: Re: Autodesk.AutoCAD.Windows.OpenFileDialog - Force to Folder Location
Post by: fixo on April 17, 2014, 01:04:43 AM
For WPF based app better use this method (see MSDN)
 
Code: [Select]
     [CommandMethod("fod")]
      public void testOpenFileDial()
      {
          string filename = "";
          // Configure open file dialog box
          Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
          dlg.FileName = "Drawing777"; // Default file name
          dlg.DefaultExt = ".dwg"; // Default file extension
          dlg.Filter = "Drawing (.dwg)|*.dwg"; // Filter files by extension
          dlg.InitialDirectory = @"C:\Test";// Default finitial folder extension
          dlg.RestoreDirectory = true;
          dlg.Multiselect = false;//true
          dlg.ValidateNames = true;
          // Show open file dialog box
          Nullable<bool> result = dlg.ShowDialog();

          // Process open file dialog box results
          if (result == true)
          {
              // Open document
              filename = dlg.FileName;
          }
          System.Windows.MessageBox.Show(filename);
      }
Title: Re: Autodesk.AutoCAD.Windows.OpenFileDialog - Force to Folder Location
Post by: Locke on April 17, 2014, 08:14:55 AM
For WPF based app better use this method (see MSDN)
 
Code - C#: [Select]
  1.      [CommandMethod("fod")]
  2.       public void testOpenFileDial()
  3.       {
  4.           string filename = string.Empty;
  5.  
  6.           // Configure open file dialog box
  7.           var dlg = new Microsoft.Win32.OpenFileDialog
  8.           {
  9.                FileName = "Drawing777",
  10.                DefaultExt = ".dwg",
  11.                Filter = "Drawing (.dwg)|*.dwg",
  12.                InitialDirectory = @"C:\Test",
  13.                RestoreDirectory = true,
  14.                Multiselect = false,
  15.                ValidateNames = true
  16.           };        
  17.  
  18.           // Show open file dialog box
  19.           bool? result = dlg.ShowDialog();
  20.  
  21.           // Process open file dialog box results
  22.           if (result)
  23.           {
  24.               // Open document
  25.               filename = dlg.FileName;
  26.           }
  27.           System.Windows.MessageBox.Show(filename);
  28.       }

Tweaked for clarity/fun.
Title: Re: Autodesk.AutoCAD.Windows.OpenFileDialog - Force to Folder Location
Post by: MexicanCustard on April 17, 2014, 08:42:42 AM
If your going to use Microsoft Dialog boxes inside AutoCad, make sure you give the ShowDialog method the Main Window as an owner or you risk the chance of losing the dialog behind AutoCad where the user can't find it and thinks the program froze up.

Code - C#: [Select]
  1. var dialogResult = dlg.ShowDialog(new AcadMainWindow());
  2.  

Code - C#: [Select]
  1. class AcadMainWindow : System.Windows.Forms.IWin32Window
  2.     {
  3.         public IntPtr Handle
  4.         {
  5.             get { return Application.MainWindow.Handle; }
  6.         }
  7.     }
  8.  
Title: Re: Autodesk.AutoCAD.Windows.OpenFileDialog - Force to Folder Location
Post by: paulfontes on May 21, 2014, 02:50:01 PM
Has anyone noticed strange behavior whenever .OpenFileDialog() is fired? 

acad.exe hangs around for (sometimes) several minutes after I close my AutoCAD session?  This happens on both AutoCAD 2013 and 2014, seems to be worse on 2013. 

I used the code that fixo posted to confirm my theory:

For WPF based app better use this method (see MSDN)
 
Code: [Select]
     [CommandMethod("fod")]
      public void testOpenFileDial()
      {
          string filename = "";
          // Configure open file dialog box
          Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
          dlg.FileName = "Drawing777"; // Default file name
          dlg.DefaultExt = ".dwg"; // Default file extension
          dlg.Filter = "Drawing (.dwg)|*.dwg"; // Filter files by extension
          dlg.InitialDirectory = @"C:\Test";// Default finitial folder extension
          dlg.RestoreDirectory = true;
          dlg.Multiselect = false;//true
          dlg.ValidateNames = true;
          // Show open file dialog box
          Nullable<bool> result = dlg.ShowDialog();

          // Process open file dialog box results
          if (result == true)
          {
              // Open document
              filename = dlg.FileName;
          }
          System.Windows.MessageBox.Show(filename);
      }