Author Topic: Modifying the File Dialog behaviour C#  (Read 6168 times)

0 Members and 1 Guest are viewing this topic.

smakfactor1

  • Guest
Modifying the File Dialog behaviour C#
« on: November 25, 2014, 05:27:06 PM »
Full disclosure(and my first post!) I'm very new to programming. 

I want to create my own File Dialog Box so that it behaves how most people in the office would like it to.  It will be a separate command, but work almost identically.  One feature I want set is that when the command is run, it opens the dialog box in the location of the current drawing the user is in(we have over 1000 different project folders on the network).

The other is just setting only .dwg to viewable to weed out the noise. (this part seems to work already).

So my issue right now is I'm not even sure how to approach getting the File Dialog box to launch an actual drawing.  I can get it to pop up, but selecting and clicking OK(this is probably the method I need to use).  I've been searching for a couple days and almost everything I've come across is hard-coding a drawing path into the code.

My attempts earlier were trying to do it via SendStringToExecute, but I don't think that's the proper way to approach this. 

Regardless of the fact that I can't get that to work either.  Any help would be greatly appreciated.

Here's my random/crap code(note I'm programming this to work in Autocad and Bricscad):

Code - C#: [Select]
  1. namespace HASD_QuickOpen
  2. {
  3.     public class Commands : AcTrx.IExtensionApplication
  4.     {
  5.        
  6.         public void Initialize()
  7.         {
  8.  
  9.         }
  10.  
  11.         public void Terminate()
  12.         {
  13.  
  14.         }
  15.  
  16.         [AcTrx.CommandMethod("QO", AcTrx.CommandFlags.Session)]
  17.         public void SelectFiles()
  18.         {
  19.             AcEd.Editor ed = AcAp.Application.DocumentManager.MdiActiveDocument.Editor;
  20.  
  21.             AcAp.Document doc = AcAp.Application.DocumentManager.MdiActiveDocument;
  22.  
  23.             AcDb.HostApplicationServices hs = AcDb.HostApplicationServices.Current;
  24.  
  25.             AcWnd.OpenFileDialog ofd = new AcWnd.OpenFileDialog("Select a file using an OpenFileDialog", null, "dwg;*", "SelectFileTest", AcWnd.OpenFileDialog.OpenFileDialogFlags.AllowMultiple); //I think I should be getting the current drawing path and setting it as the default here
  26.  
  27.             System.Windows.Forms.DialogResult dr = ofd.ShowDialog();
  28.  
  29.             string filePath = dr.ToString();
  30.  
  31.             string fpStr = (filePath.ToString());
  32.  
  33.             string cmdStr = @"_open" + fpStr;
  34.  
  35.            
  36.             doc.SendStringToExecute(cmdStr, true, true, true); //I can't get it to just take cmdStr
  37.         }
  38.     }
  39. }
  40.  


edit-kdub : formatting [code=csharp]
« Last Edit: November 25, 2014, 07:04:53 PM by Kerry »

n.yuan

  • Bull Frog
  • Posts: 348
Re: Modifying the File Dialog behaviour C#
« Reply #1 on: November 25, 2014, 05:46:28 PM »
You almost there:

The returned value of ShowDialog() method, simply tells you how the dialog is closed/hidden (OKed, or Cancelled). Obviously, if it is cancelled, you do not want AutoCAD to try open a drawing. Remeber, after the dialog is closed, one still can access it (i.e. after OK is clicked, the form becomes hidden, but its property "Filename" can be retirieved).

So, you would do this:

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

    Dim fileName AsString = ofd.FileName

    Applicatioon.DocumentManager.Open(fileName, False[True])

Else
   System.Windows.Forms.MessageBox.Show("Cancelled!")
End If

smakfactor1

  • Guest
Re: Modifying the File Dialog behaviour C#
« Reply #2 on: November 25, 2014, 06:09:39 PM »
Thank you for the quick reply.  I had to do some juggling to get rid of most of my squigglies, but here's where we're at now.

Code - C#: [Select]
  1.  [AcTrx.CommandMethod("QO", AcTrx.CommandFlags.Session)]
  2.         public void SelectFiles()
  3.         {
  4.             AcEd.Editor ed = AcAp.Application.DocumentManager.MdiActiveDocument.Editor;
  5.  
  6.             AcAp.Document doc = AcAp.Application.DocumentManager.MdiActiveDocument;
  7.  
  8.             AcDb.HostApplicationServices hs = AcDb.HostApplicationServices.Current;
  9.  
  10.             AcWnd.OpenFileDialog ofd = new AcWnd.OpenFileDialog("Select a file using an OpenFileDialog", null, "dwg;*", "SelectFileTest", AcWnd.OpenFileDialog.OpenFileDialogFlags.AllowMultiple);
  11.  
  12.             System.Windows.Forms.DialogResult dr = ofd.ShowDialog();
  13.  
  14.             var fileName = ofd.Filename;
  15.  
  16.             if (dr == System.Windows.Forms.DialogResult.OK)
  17.             {
  18.  
  19.                 AcAp.Application.DocumentManager.Open(fileName, false[true]);
  20.  
  21.             }
  22.  
  23.             else System.Windows.Forms.MessageBox.Show("Cancelled!");
  24.  
  25.         }
  26.     }
  27. }
  28.  

It doesn't know what Open is, and doesn't like our formatting on the true/false.

Open: Autodesk.Autocad.ApplicationServices.DocumentCollection does not contain a definition for 'Open' and no extension method etc etc

false/true: Cannot apply indexing with [] to an expression of type 'bool'


edit-kdub : formatting [code=csharp]
« Last Edit: November 25, 2014, 07:06:25 PM by Kerry »

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Re: Modifying the File Dialog behaviour C#
« Reply #3 on: November 25, 2014, 06:24:53 PM »
Do you have a reference to acmdg.dll? The Open() method is defined as an extension. Just use true or false, depending on if you want it opened Read-Only or not.

smakfactor1

  • Guest
Re: Modifying the File Dialog behaviour C#
« Reply #4 on: November 25, 2014, 06:28:56 PM »
Do you have a reference to acmdg.dll? The Open() method is defined as an extension. Just use true or false, depending on if you want it opened Read-Only or not.

I do have it referenced already, no go.  :(

Am I using the right using...

Note this is a template solution I use for everything..obviously there's a lot of stuff there I don't need for this.
Code: [Select]
#if ACAD
using AcRx = Autodesk.AutoCAD.Runtime;
using AcTrx = Autodesk.AutoCAD.Runtime;
using AcAp = Autodesk.AutoCAD.ApplicationServices;
using AcDb = Autodesk.AutoCAD.DatabaseServices;
using AcGe = Autodesk.AutoCAD.Geometry;
using AcEd = Autodesk.AutoCAD.EditorInput;
using AcGi = Autodesk.AutoCAD.GraphicsInterface;
using AcClr = Autodesk.AutoCAD.Colors;
using AcWnd = Autodesk.AutoCAD.Windows;
using AcApP = Autodesk.AutoCAD.ApplicationServices.Application;
#endif

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Re: Modifying the File Dialog behaviour C#
« Reply #5 on: November 25, 2014, 06:36:30 PM »
You can keep the current usings, but i think that for C# to find the extension method you must have a direct using for the ApplicationServices (I could be wrong):

using Autodesk.AutoCAD.ApplicationServices;

smakfactor1

  • Guest
Re: Modifying the File Dialog behaviour C#
« Reply #6 on: November 25, 2014, 06:40:01 PM »
You can keep the current usings, but i think that for C# to find the extension method you must have a direct using for the ApplicationServices (I could be wrong):

using Autodesk.AutoCAD.ApplicationServices;


Might be missing a Brics reference...but shouldn't be.  BRB.

Nope.  I have all 3 listed for Bricscad in already too.

TD_MgdBrep.dll
TD-Mgd.dll
BrxMgd.dll
« Last Edit: November 25, 2014, 06:45:37 PM by smakfactor1 »

smakfactor1

  • Guest
Re: Modifying the File Dialog behaviour C#
« Reply #7 on: November 25, 2014, 06:52:48 PM »
« Last Edit: November 25, 2014, 07:00:54 PM by smakfactor1 »

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Modifying the File Dialog behaviour C#
« Reply #8 on: November 25, 2014, 10:05:32 PM »
For the extension to work you must have a reference to the assembly and a using statement with namespace where  extension method is located.
You should not have to look into extension methods the reason they were created was to make them work and feel as if they were a method of that type.

If you want a quick example take all those nice LINQ methods used on collections.
For example
Code - C#: [Select]
  1.             List<int> ints = new List<int>();
  2.             var greaterThanZero = ints.Where(i => i > 0);
  3.  
Now a List<T> is in System.Collections.Generic namespace.
And the Where extension method is in System.Linq namespace
so you must have a
Code - C#: [Select]
  1. using System.Linq;
  2.  

This works
Code - C#: [Select]
  1. using System.Linq;
  2. using LinqExtenstions = System.Linq;
  3.  

This will not work
Code - C#: [Select]
  1. using LinqExtenstions = System.Linq;
  2.  

Can you see your problem now?

Your problem is caused by using an alias and need to either remove it or add the namespace in addition to alias
Code - C#: [Select]
  1. using AcAp = Autodesk.AutoCAD.ApplicationServices;
  2. using Autodesk.AutoCAD.ApplicationServices; ********* MUST HAVE TO USE EXTENSION METHODS
  3.  


You can also use Editor.GetFileNameForOpen(PromptOpenFileOptions options) with PromptOpenFileOptions.PreferCommandLine = false

Here is an example using it to open file selected in dialog box then open the file and run command that prints the name of the document that was selected in dialog(same as printing currently open document but one way to open document and run a command and pass it some data)

Code - C#: [Select]
  1.         [CommandMethod("OpenFileWithoutAlias", CommandFlags.Session)]
  2.         public void OpenFileWithoutAlias()
  3.         {
  4.             PromptOpenFileOptions pofo = new PromptOpenFileOptions("\nEnter File");
  5.             pofo.PreferCommandLine = false;
  6.             pofo.DialogName = "Select File";
  7.             pofo.Filter = "dwg files (*.dwg)|*.dwg";
  8.             pofo.DialogCaption = "Select File";
  9.  
  10.             //pofo.InitialDirectory = Intial directory to open to
  11.             //pofo.InitialFileName = Intial file to use;
  12.            
  13.  
  14.             PromptFileNameResult pfnr = Application.DocumentManager.MdiActiveDocument.Editor.GetFileNameForOpen(pofo);
  15.             if (pfnr.Status != PromptStatus.OK) return;
  16.  
  17.             Document doc = Application.DocumentManager.Open(pfnr.StringResult);
  18.            
  19.             string cmd = String.Concat("CommandAfterOpen", " ", pfnr.StringResult, "\n");
  20.             doc.SendStringToExecute(cmd, true, false, false);
  21.  
  22.         }
  23.  
  24.         [CommandMethod("CommandAfterOpen", CommandFlags.NoHistory)]
  25.         public void CommandAfterOpen()
  26.         {
  27.             PromptStringOptions pso = new PromptStringOptions("\nEnter File");
  28.             pso.AllowSpaces = true;
  29.             PromptResult pr = Ed.GetString(pso);
  30.             if (pr.Status != PromptStatus.OK) return;
  31.  
  32.             Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nDoing Work");
  33.             Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nNoHistory so user will not run it again by pressing enter");
  34.             Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nDoing with with file " + pr.StringResult);
  35.             Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nWork Done");
  36.  
  37.         }
  38.  


smakfactor1

  • Guest
Re: Modifying the File Dialog behaviour C#
« Reply #9 on: November 26, 2014, 11:48:07 AM »
I really appreciate the time and post Jeff, thank you.  I've been lurking around the Swamp for some time now, you guys are really knowledgeable and very helpful.

So this should have corrected the issue no?

Code: [Select]
#if BCAD
using Bricscad.ApplicationServices;
using AcAp = Bricscad.ApplicationServices;
using AcApP = Bricscad.ApplicationServices.Application;
#endif

#if ACAD
using Autodesk.AutoCAD.ApplicationServices;
using AcAp = Autodesk.AutoCAD.ApplicationServices;
using AcApP = Autodesk.AutoCAD.ApplicationServices.Application;
#endif

I get the same error regardless though, I'm wondering if it's not Bricscad specifically, it's not throwing me an Acad error.

Error   1   'Bricscad.ApplicationServices.DocumentCollection' does not contain a definition for 'Open' and no extension method 'Open' accepting a first argument of type 'Bricscad.ApplicationServices.DocumentCollection' could be found (are you missing a using directive or an assembly reference?)   \Google Drive\Projects\Programming\HASD_Projects\TEMPLATE\Common\Commands.cs   97   39   Bcad

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Re: Modifying the File Dialog behaviour C#
« Reply #10 on: November 26, 2014, 11:58:18 AM »
Yes, it appears it the Bricscad API at fault here. You should be able to define your own Open() extension for that platform using the DatabaseServices.Database() constructor.