Tuesday, 21 January 2014

Cust 24: File Dialogs: CSharp Part 1

In this tutorial you learn using CSharp:

  • How to setup and display the Solid Edge file open dialog.
  • How to setup and display a Windows standard file open dialog  using the .Net API.
  • How to adjust various settings for the file dialogs in both types viz:
    • Title
    • Filter
    • Multiple filters
    • Composite filters
    • Default filter
    • Initial directory
    • Selecting single file
    • Selecting multiple files.
    • Opening single and multiple files in Solid Edge.
  • Display the file save dialogs using both Solid Edge API and .Net
  • Relative advantages of one type over the other.

Note:

This tutorial should be read progressively beginning with the first post in the programming series.

Full index of programming tutorials on this blog is here.

Start Visual Studio and add a CSharp Windows Forms Application project.

Add three Buttons and a listbox to the form as below:

image_thumb[1]

Declare a variable for Solid Edge:

   1:  SolidEdgeFramework.Application oApp = null;
   2:  SolidEdgeFramework.SolidEdgeDocument oDoc;






In the Form’s Load event, connect to a running instance of Inventor using:


oApp = (SolidEdgeFramework.Application)Marshal.GetActiveObject("SolidEdge.Application");


To create a file open dialog, add the following code for the first button Windows File Dialog:


OpenFileDialog dlg = new OpenFileDialog();


Before displaying the file dialog, set properties for the dialog box as below:


dlg.Title = "Select a Solid Edge File";


This sets the title of the dialog as shown below:


image_thumb[4]


Also set the default file name to appear in the File name field:


dlg.FileName = "SomeFile.par";


The dialog can start where the compiled executable for this VB application:


dlg.InitialDirectory = System.Windows.Forms.Application.StartupPath;


This is seen in image above.


Or simply specify a hardcoded path as below:


dlg.InitialDirectory = "C:\\SolidEdgeFiles\\";


Note: The double \\ are necessary as path separator and a single \ is not allowed.


To let the user see only Solid Edge files, specify a file filter a below:


dlg.Filter = "Solid Edge Part Files (*.par)|*.par”;


Where the string is made of two parts Solid Edge Part Files (*.par)  and *.par which are separated by a pipe | character.


image_thumb[7]


The part of the string to the left of the pipe is what appears in the list for the file types.


The part of the string on the right of the pipe is the actual filter.


Using this filter displays only Solid Edge part files from a folder.


Note that mentioning the *.par in the left portion of the filter is not required nor the case is important. The actual filter is on the right side of the pipe (|) character.


You may as well write the filter as "Text files|*.par" which would display Text files in the dialog but the actual files that are displayed are par files due to the *.par extension specified on the right side of the pipe.


Similarly, you may write the filter as “Solid Edge Part Files|*.txt” which will display Solid Edge Part Files in the file dialog but text files will get filtered and will be displayed.


To specify multiple types, specify the filter as below:


dlg.Filter = "Part Files (*.par)|*.par|Assembly Files (*.asm)|*.asm|Drawing Files (*.dft)|*.dft";


Using this filter, all three types of files are displayed in a selected folder and the file type selection itself looks as below:


image_thumb[19]


This string has three file filters for Solid Edge Part, Assembly and Draft files. Each one is a pair separated by pipes and each pair is again separated by a pipe.


However, the *.par filter written first remains the default filter and the user has to pull down this list to select another filter.


To set the second filter *.asm as the default, set the filter index to:


dlg.FilterIndex = 2;


This will set *.asm as the default filter when the file dialog is displayed.


To display all three file types as a single filter, specify the filter as below:


dlg.Filter = "All Solid Edge Files|*.par;*.asm;*.dft|Part Files (*.par)|*.par|Assembly Files (*.asm)|*.asm|Draft Files (*.dft)|*.dft";


image_thumb[20]


Here, three file types are specified in a single filter separated by a semi-colon.


Now its time to actually display the dialog on screen using:


DialogResult dResult = dlg.ShowDialog();


the ShowDialog method displays the file dialog upon clicking the button Windows File Dialog and the user starts interacting and browsing files.


If the user picks a file and click OK the ShowDialog function returns OK, else if the user cancels the dialog by clicking Cancel or by clicking the red cross button in the title bar top-right corner, the dialog returns Cancel which is stored in the the dResult variable of type DialogResult.


Check the value returned in dResult as below and store the file name in a string and subsequently open it in Solid Edge:


if (dResult != DialogResult.Cancel)
  string sFileName = dlg.FileName;
  //oDoc = oApp.Documents.Open(sFileName)


To allow selection of multiple files, set the property of the dialog before displaying it as below:


dlg.Multiselect = true;


This will allow user to pick multiple files using Shift and Ctrl buttons and also by dragging around multiple files in the dialog as below:


image_thumb[11]


and also all files picked will be listed in the file name field :


image_thumb[13]


Next, display all selected files in the a listbox:


First, show the dialog from the button’s Click event:


DialogResult dResult = dlg.ShowDialog();


If the user has not cancelled the dialog, declare a string array and store all the selected files using the Filenames property of the file dialog:


   1:   if (dResult != System.Windows.Forms.DialogResult.Cancel)
   2:  {
   3:    string[] sFileNames = dlg.FileNames;
   4:    for (int i = 0; i < sFileNames.Length; i++)
   5:    {
   6:      listBox1.Items.Add(sFileNames[i]);
   7:      oDoc = oApp.Documents.Open(sFileNames[i]);
   8:    }
   9:  }



Finally in a For loop, add each filename to the list box or simply open each file in Solid Edge as shown above or both.


image_thumb[15]


 


Continued to Part 2…


 


 clip_image002_thumb1_thumb_thumbDrop a comment below if you need the Visual Studio project files and if you liked the depth of this discussion, similar in-depth techniques are discussed in cMayoCAD where you create your own, brand new, fully functional CAD system with scripting capabilities using a geometric modeling kernel.

Download the detailed course contents for cMayoCAD here.
cMayoCAD2