Select Vault items from within CAD

itemSelection

How would it be to select an item from Vault directly within Inventor? Yes, I’m in an Inventor model or drawing and via the Data Standard dialog I can pick an existing item from within Vault. Once again, we do this with Data Standard. So, we add an additional tab to the Inventor or AutoCAD Data Standard dialog and either at save or later, the user can place a search onto the Vault items and then select one of the resulting items. The item number will be stored into the Part Number within the Inventor file.

SelectItem

Once the file is checked in, as the part number matches with an existing item, at the assign-item operation, the association will be automatically done.

The customization for this sample involves 2 files. The Inventor.XAML and a new PS1 file. The XAML file has been enhanced with an additional tab and the according search TextBox and a DataGrid for the results. Here the XAML code for the Item tab:

<TabItem Header="Items" HorizontalAlignment="Left" Height="20" VerticalAlignment="Top" Width="55">
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="auto"/>
      <RowDefinition/>
    </Grid.RowDefinitions>
    <Grid>
      <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition Width="auto"/>
      </Grid.ColumnDefinitions>
      <TextBox x:Name="SearchText" Grid.Column="0"/>
      <Button Grid.Column="1" Content="Search" Margin="5,0,0,0" Command="{Binding PsCmd[SearchItem]}" CommandParameter="{Binding Text, ElementName=SearchText}"/>
    </Grid>
    <DataGrid Grid.Row="1" x:Name="ItemsFound" AutoGenerateColumns="True" IsReadOnly="True" ColumnWidth="Auto" HorizontalGridLinesBrush="WhiteSmoke" VerticalGridLinesBrush="WhiteSmoke">
      <DataGrid.ContextMenu>
        <ContextMenu>
          <MenuItem Command="{Binding PsCmd[SelectItem]}" Header="Select item" CommandParameter="{Binding SelectedIndex, ElementName=ItemsFound}"/>
        </ContextMenu>
      </DataGrid.ContextMenu>
    </DataGrid>
  </Grid>
</TabItem>

As you can see the Button for the search is bound to a PowerShell function called SelectItem. The entered search text is passed as a CommandParameter to the Button. The DataGrid is somewhat simple as we use the AutoGenerateColumns in order to get the appropriate columns. You’ll notice that we defined a little context menu for the DataGrid. So the user will be able to fire the “Select Item” command.

Let’s have a look to the PowerShell file:

Add-Type @"
public class itemData
{
  public string Item {get;set;}
  public string Revision {get;set;}
  public string Title {get;set;}
}
"@

function SearchItem()
{
  if($mvparam -eq '') { return }
  $srchconds = New-Object autodesk.Connectivity.WebServices.SrchCond[] 1
  $srchconds[0] = New-Object autodesk.Connectivity.WebServices.SrchCond
  $srchconds[0].PropDefId = 0
  $srchconds[0].SrchOper = 1
  $srchconds[0].SrchTxt = $mvparam
  $srchconds[0].PropTyp = "AllProperties"
  $srchconds[0].SrchRule = "Must"
  $bookmark = ''
  $status = New-Object autodesk.Connectivity.WebServices.SrchStatus
  $items = $vault.ItemService.FindItemRevisionsBySearchConditions($srchconds, $null, $true, [ref]$bookmark, [ref]$status)
  $results = @()
  foreach($item in $items)
  {
    $row = New-Object itemData
    $row.Item = $item.ItemNum
    $row.Revision = $item.RevNum
    $row.Title = $item.Title
    $results += $row
  }
  $dsWindow.FindName("ItemsFound").ItemsSource = $results
}

function SelectItem
{
  $selectedItem = $dsWindow.FindName("ItemsFound").SelectedItem
  $Prop["Part Number"].Value = $selectedItem.Item
  $dsWindow.FindName("DataTab").IsSelected = $true
}

Similar to our last post about the item tab, we create a small C# class with few properties. The function SearchItems checks whether the arguments passed to the Button via the CommandParamenter are empty. In that case the function just returns and does nothing. Otherwise a generic search on the item master will be performed. How did I came across the settings for the search condition? I just started the vapiTrace and run a generic search in Vault. Then I read the function name and arguments from the vapiTrace.

The result of the search is then transformed into a list of our custom class and set as ItemsSource to the DataGrid.

As for our XAML, the DataGrid has a little context menu item with the “Select item” command. The bound function is the SelectItem, which sets the property Part Number to the selected number and then sets the first tab as active.

That’s it! Of course we can enhance this sample by showing more information for the items, or we can additionally filter the result and show just some data. We can also think or letting the user create new items right from within Inventor or AutoCAD. Then of course the XAML and code would be a bit longer. But overall, I think this sample shows a bit of XML and PowerShell technique, and with a few lines of code the result is quite impressive!

If you like to try this out, just download the ZIP package.

This entry was posted in Data Standard, PowerShell, Vault API. Bookmark the permalink.

3 Responses to Select Vault items from within CAD

  1. Once again – great stuff. Thanks, Marco!

  2. Pingback: Unique part number check | coolorange

  3. Borre Hartviksen says:

    Looks nice, but I cant get it to work with data Standards 2016. Any suggestions ?

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