Author Topic: Problem with file opening  (Read 1505 times)

0 Members and 1 Guest are viewing this topic.

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Problem with file opening
« on: February 19, 2013, 06:14:56 AM »
I need override the behaviour of document opening, for security, but I get a problem...

After the document opens and becomes the active when I switch back in the previous document, I see that the "Select File" dialog is open. And it is not my own dialog, but it is the "native" AutoCAD window which I also try to redefine. This window shan't appear. How to solve this problem? Read the comments please below.

Code - C#: [Select]
  1.  
  2. ...
  3. App.DocumentCollection docMng = cad.DocumentManager;
  4. docMng.DocumentLockModeChanged += new App.DocumentLockModeChangedEventHandler(docMng_DocumentLockModeChanged);
  5. ...
  6.  
  7. static void docMng_DocumentLockModeChanged(object sender, App.DocumentLockModeChangedEventArgs e) {
  8.     if (string.Compare(e.GlobalCommandName, "OPEN", true) == 0) {
  9.         e.Veto();
  10.         ShowOpenFileDialog();
  11.     }
  12. }
  13. ...
  14.  
  15. [Rtm.CommandMethod("bush-open", Rtm.CommandFlags.Modal)]
  16. public static void ShowOpenFileDialog() {
  17.     // using CadWin = Autodesk.AutoCAD.Windows;
  18.     CadWin.OpenFileDialog dialog = new CadWin.OpenFileDialog(
  19.         String.Format("[{0}] {1}", Resource.startTitle, Resource.docOpenTitle),
  20.         "", "dwg;dwt;dws;dxf", "", CadWin.OpenFileDialog.OpenFileDialogFlags.SearchPath); // It is my dialog
  21.  
  22.     // using WinForms = System.Windows.Forms;
  23.     WinForms.DialogResult result = dialog.ShowDialog();
  24.  
  25.     if (result != Win.Forms.DialogResult.OK)
  26.         return;
  27.    
  28.     if (!dialog.Filename.ToLower().StartsWith(variables["%cadDir%"])) {
  29.         DirectoryInfo dir = new DirectoryInfo(Path.GetDirectoryName(dialog.Filename));
  30.  
  31.         if (!dir.Exists)
  32.             return;
  33.  
  34.         FileInfo[] files = dir.GetFiles("acad*.lsp", SearchOption.TopDirectoryOnly);
  35.         FileInfo[] files2 = dir.GetFiles("acad*.fas", SearchOption.TopDirectoryOnly);
  36.         FileInfo[] files3 = dir.GetFiles("acad*.mnl", SearchOption.TopDirectoryOnly);
  37.  
  38.         String fileName = String.Empty;
  39.         try {
  40.             foreach (FileInfo[] collection in new FileInfo[][] { files, files2, files3 }) {
  41.                 foreach (FileInfo item in collection) {
  42.                     fileName = item.FullName;
  43.                     item.Delete();
  44.                     cad.DocumentManager.MdiActiveDocument.Editor.WriteMessage(Resource.fileDeleted, fileName);
  45.                 }
  46.             }
  47.         }
  48.         catch (Exception ex) {
  49.             // using Win = System.Windows;
  50.             Win.MessageBox.Show(String.Format(Resource.disableFileOpen, fileName, ex.Message),
  51.                 Resource.warning, Win.MessageBoxButton.OK, Win.MessageBoxImage.Stop);
  52.             return;
  53.         }                
  54.     }            
  55.     App.Document doc = cad.DocumentManager.Open(dialog.Filename.Trim()); // Problem is here! Was opened "native" dialog. Why???
  56.     if (doc != null)                
  57.         cad.DocumentManager.MdiActiveDocument = doc;
  58. }
  59.  
« Last Edit: February 19, 2013, 06:35:12 AM by Andrey »

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: Problem with file opening
« Reply #1 on: February 19, 2013, 06:34:20 AM »
If to replace a code string
Code - C#: [Select]
  1. ShowOpenFileDialog();
on
Code - C#: [Select]
  1. cad.DocumentManager.MdiActiveDocument.SendStringToExecute("bush-open ", true, false, false);
that everything works as it is necessary.