Save the path

With Data Standard 2016, the selection of a Vault folder got strongly improved. A series of dynamic combo-boxes lets you pick step by step the folder in where you will store you file. From several sources, we heard the wish to apply a “memory” to such comboboxes, so that the next time you save a new file, the path is already set to the previous selected path.

The solution is pretty simple. When you hit the OK button, the selected path chunks of each combobox shall be stored somewhere, i.e. in a local text file. The next time you save a new file and the Data Standard dialog shows up, the stored selections shall be applied to the comboboxes.

Although the solution is simple, and it’s doable in few lines of code, it’s important to understand some basics. For instance, a combobox provides several properties, such as SelectedValue, SelecteItem, SelectedIndex. All of these properties sets the focus on a given position of the list of values. In the case of the folder navigation with Data Standard, also called BreadCrumb, the comboboxes don’t just contain the according folder, but also the folder Vault ID. So, by using SelectedValue with a folder name, nothing happen, as the object in the combobox is not a text, but a combination of text and ID. Using SelectedItem would be better, but then we would have to set the folder name and folder ID. SelectedIndex is just a number from 0 to xx. By setting the SelectedIndex to 1, the second element of the list is selected.

So, to keep it simple, I just store the SelectedIndex of each combobox into a little text file, and reuse such index in order to set again the comboboxes on the next dialog. In the InitializeWindow function, you will find at the end of such function the command AddCombo -data $root which adds the first combo to the dialog. Right after, I’ll read the stored index from a local text file and set the combos to the according index, like this

function InitializeWindow
{
  ...
  ....
  if ($Prop["_CreateMode"].Value)
  {
    ...
    ....
    AddCombo -data $root
    $path = Get-Content c:\temp\breadcrumb.txt
    for ($index = 0; $index -lt $path.Count; $index++) 
    {
      $dsWindow.FindName("cmbBreadCrumb_"+$index).SelectedIndex=$path[$index]
    }
  }
}

The lines 10 to 14 are the one added by me. This basically sets the combo to a default value, and so when the dialog shows up, the path is set to the last saved path.

As you can see, I’m accessing the comboboxes by the name “cmbBreadCrumb_”. This is bacuse in the code for the BreadCrumb, the comboboxes are called cmbBreadCrumb_0, cmbBreadCrumb_1, cmbBreadCrumb _2, etc.

The comboboxes are added dynamically. When you select a value on the first one, the second one will be added automatically with according child folders. So, I just have to set the first index, this will cause the addition of the second combo. By setting the index of the second combo, a third will be added. And so on.

In the OnPostCloseDialog we will save the current state of the comboboxes. We go through the current comboboxes and store the SelectedIndex into the local text file, like this:

function OnPostCloseDialog
{
  $breadCrumb = $dsWindow.FindName("BreadCrumb")
  $path = @()
  foreach ($cmb in $breadCrumb.Children) 
  {
    $path += $cmb.SelectedIndex
  }
  $path | Out-File c:\temp\breadcrumb.txt
  ...
  ....
}

As you can see, I’m searching for a control called BreadCrumb. That is the place holder where all the comboboxes will be displayed. In the saving routine above, I’m going through the children of the BreadCrumb, which are the according comboboxes, and save the index in to a variable $path which I then store into the local file breadcrumb.txt.

Now, this all would already work, but there is one little problem. As mentioned before, the comboboxes are added dynamically to the window. Unfortunately those comboboxes are added but not registered, so when try accessing a “cmbBreadCrumb_1”, for instance, we get an error message, as this element is now known. In order to solve this problem, we have a improve the code for the BreadCrumb. In the function AddCombo we have to register the new added combo. And in the function OnSelectionChanged we have to unregister the combos that will be removed in case the user selects another path. Hiere is the code.

function AddCombo($data)
{
  ...
  ....
  $breadCrumb.RegisterName($cmb.Name, $cmb)
  $breadCrumb.Children.Add($cmb)
  $dsDiag.Trace("<< AddCombo")
}

 

function OnSelectionChanged($sender)
{
  ...
  ....
  while($children -gt $position )
  {
    $cmb = $breadCrumb.Children[$children]
    $breadCrumb.UnregisterName($cmb.Name)
    $breadCrumb.Children.Remove($breadCrumb.Children[$children])
    $children--
  }
  ...
  ....
}

In the function AddCombo I’ve added line 5 and in the OnSelectionChange the line 8.

Once this is done, you should now see how the path will be reset every time you save a new file.

Enjoy!

This entry was posted in Data Standard. Bookmark the permalink.

One Response to Save the path

  1. Christian kolb says:

    Hi,
    This works very well in Inventor, Thanks therefore.
    But it does not Work in Autocad?
    Do You have a tip for me?
    Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s