Reference redirection with Vault

Since Vault 2012 Autodesk introduced the ability to redirect references within Inventor files. You may have noticed that for instance renaming files is extremely fast compared to Vault 2011. So, if you rename a component, the parent objects, like assemblies and drawings, will only be notified about the change of a child. Just when you open the assembly, the reference change will be applied. In order to accomplish this, some new properties has been added to the association object. Especially the RefId is responsible for this behavior. The RefId points to the internal reference index of an Inventor file.

In other words, each Inventor file knows about his children, as he has a list of references. These references have an identifier (index). As an example: an assembly points to a file foo.ipt and in the list of references, the index equals to the number 5. The file foo.ipt has been renamed to bar.ipt. Then the association in Vault points to the new named file bar.ipt and on the association the RefId equals 5 too. This way, when the assembly will be checked out, the file bar.ipt will be copied into the local workspace, and the assembly will be notified that for the reference number 5 the new location and name for the referenced file will be bar.ipt. The real work of replacing the reference will be handed over to Inventor.

Now as long you just work with Inventor, you don’t really have to care for it. But if you like to import data in Vault and you don’t like to manage the reference change your self, then you can leverage this technique. Actually, this technique is also quite handy if you try to import files via BCP. There you will find also a similar attribute, and if set properly, the referenced will be managed by Vault and Inventor. The following PowerShell script shows you how to import Inventor files from a folder into Vault, while all files will be renamed, in order to demonstrate the effect. This is how it will look like in Inventor, before and after.

[System.Reflection.Assembly]::LoadFrom("C:\Windows\assembly\GAC_MSIL\Autodesk.Inventor.Interop\16.0.0.0__d84147f8b4276564\Autodesk.Inventor.Interop.dll")
[System.Reflection.Assembly]::LoadFrom("C:\Program Files (x86)\Autodesk\Autodesk Vault 2013 SDK\bin\Autodesk.Connectivity.WebServices.dll")

$cred = New-Object Autodesk.Connectivity.WebServicesTools.UserPasswordCredentials("localhost","Vault","Administrator","")
$vault = New-Object Autodesk.Connectivity.WebServicesTools.WebServiceManager($cred)

function AddFile()
{
	$fileName = [System.IO.Path]::GetFileName($args[0])
	if($FileIds.Contains($fileName))
	{
		return $FileIds[$fileName] #we know already the file ID
	}
	$invDoc = $invApp.Open($args[0])

	$databaseRevisionID = ""
	$lastSavedLocation = ""
	$indices = $null
	$oldPaths = $null
	$currentPaths = $null
	$invDoc._GetReferenceInfo([ref] $databaseRevisionID, [ref] $lastSavedLocation, [ref] $indices, [ref] $oldPaths, [ref] $currentPaths, $true);
	$invDoc.Close()

	$assocs = New-Object Autodesk.Connectivity.WebServices.FileAssocParam[] $indices.Count
	$index = 0
	foreach($ref in $oldPaths)
	{
		$childId = AddFile $ref
		$assocs[$index] = New-Object Autodesk.Connectivity.WebServices.FileAssocParam
		$assocs[$index].CldFileId = $childId
		$assocs[$index].ExpectedVaultPath = $ref
		$assocs[$index].RefId = $indices[$index]
		$assocs[$index].Source = "INVENTOR"
		$assocs[$index].Typ = [Autodesk.Connectivity.WebServices.AssociationType]::Dependency
		$index ++
	}
	$buffer = New-Object Autodesk.Connectivity.WebServices.ByteArray
	$buffer.Bytes = [System.IO.File]::ReadAllBytes($args[0])
	$ext = [System.IO.Path]::GetExtension($fileName)
	$child = $vault.DocumentService.AddFile($global:folder.Id, [DateTime]::Now.Ticks.ToString() +$ext ,"", [DateTime]::Now, $assocs, $null, [Autodesk.Connectivity.WebServices.FileClassification]::None, $false, $buffer)
	$FileIds.Add($fileName, $child.Id) #remember the added file. it might come again as child
	$child.Id #return the child ID
}

$invApp = New-Object Inventor.ApprenticeServerComponentClass

$global:folder = $vault.DocumentService.GetFolderByPath("$/Designs/test")
$global:FileIds = @{}

$files = Get-ChildItem "C:\Users\Public\Documents\Autodesk\Vault Professional 2013\Samples\Inventor 2013\Padlock\Designs\Padlock" -Recurse
$files | Where-Object { -not $_.PSIsContainer } | ForEach-Object { AddFile $_.FullName }

$invApp.Close()

The script start as usual by loading the Vault WebServices and logging into Vault. If you are not familiar with this, then read this post first: Querying the Vault database. Additionally we load the Inventor apprentice server. We will use this to read out the reference information and catch the index ids. As we import the files recursively, we created a function called AddFile which will upload the file in Vault with his references first. It start by checking wether the file has been already imported. Then it opens the file with the Inventor Apprentice and uses the _GetReferenceInfo function in order to get all the information about the references. The function will provide back the list of index. These numbers are the trick. For all the references an association array will be created and the RefId set to the index from the ReferenceInfo. The rest is about uploading the file. At the end, the script just reads out the content of a folder, filters our the folders from the list and for each file it executes the AddFile function. In order to make it not so simple, the added files are renamed automatically with the current date and time.

This script does not handle any errors. So the Inventor files has to be in perfect conditions, no broken references. It should give you an overview how the technique works, and of course, you have to beef it up with more control. But we hope that you enjoy this stuff, as it is quite powerful and solves a lot of problems while dealing with Inventor files.

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

One Response to Reference redirection with Vault

  1. Pingback: Updating References with Vault 2014 API | coolorange

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