Get File Attachments using iLogic

Get File Attachments using iLogic

chendersonCRKPU
Contributor Contributor
225 Views
6 Replies
Message 1 of 7

Get File Attachments using iLogic

chendersonCRKPU
Contributor
Contributor

We are using Vault and typically attach PDF datasheets etc to parts. I wish to be able to loop through an assembly and extract all these attachments to a single folder.

 

Is there a way to access these attached files through the Inventor API?

 

 

0 Likes
226 Views
6 Replies
Replies (6)
Message 2 of 7

daltonNYAW9
Advocate
Advocate

Yes this is possible. We have a similar workflow with purchased parts online cut sheets. I made a rule that pdf combines all cut sheet files for the given assembly. It uses the file search for matching stock numbers or matching folder location. Here's the function that gets the file and returns it's local path

AddReference "C:\_IPI_Vault\Inventor Configuration\iLogic\itextsharp.dll"
AddReference "System.IO"
AddReference "System.Private.Uri"
AddReference "Autodesk.DataManagement.Client.Framework.Vault.dll"
Imports VDF = Autodesk.DataManagement.Client.Framework
AddReference "Connectivity.Application.VaultBase.dll"
Imports VB = Connectivity.Application.VaultBase
AddReference "Autodesk.Connectivity.WebServices.dll"
Imports AWS = Autodesk.Connectivity.WebServices

 

Function GetFromVault(PurchasedFolder As String, StockNumber As String, FullFileName As String)
	'convert file name to vault format
	PurchasedFolder = PurchasedFolder.Replace("C:\_IPI_Vault", "$")
	PurchasedFolder = PurchasedFolder.Replace("\", "/")
	FullFileName = FullFileName.Replace("C:\_IPI_Vault", "$")
	FullFileName = FullFileName.Replace("\", "/")

	'get vault login
	Dim mVltCon As VDF.Vault.Currency.Connections.Connection
	mVltCon = VB.ConnectionManager.Instance.Connection
	If mVltCon Is Nothing Then
		MsgBox("Not logged in to Vault")
		Exit Function
	End If

	Dim wsFolder As AWS.Folder
	wsFolder = mVltCon.WebServiceManager.DocumentService.GetFolderByPath(PurchasedFolder)

	'get search criteria
	Dim filePropDefs As Autodesk.Connectivity.WebServices.PropDef() = mVltCon.WebServiceManager.PropertyService.GetPropertyDefinitionsByEntityClassId("FILE")
	Dim StockNumberPropDef As Integer
	Dim FolderPathPropDef As Integer
	Dim FileExtensionPropDef As Integer
	For Each filePropDef In filePropDefs
		If filePropDef.DispName = "Mfg Part Number/Stock Number"
			StockNumberPropDef = filePropDef.Id
		ElseIf filePropDef.DispName = "Folder Path"
			FolderPathPropDef = filePropDef.Id
		ElseIf filePropDef.DispName = "File Extension"
			FileExtensionPropDef = filePropDef.Id
		End If
	Next

	'set search criteria
	Dim foundfiles() As AWS.File
	Dim srs As New List(Of AWS.SrchSort)
	Dim fIDs1 As New List(Of Long)
	fIDs1.Add(wsFolder.Id)
	Dim bm As String = String.Empty
	Dim ss As New AWS.SrchStatus

	Dim scs1 As New List(Of AWS.SrchCond)
	Dim scs2 As New List(Of AWS.SrchCond)
	Dim sc1 As New AWS.SrchCond
	Dim sc2 As New AWS.SrchCond
	With sc1
		.PropDefId = FileExtensionPropDef
		.SrchOper = 1
		.SrchTxt = "pdf"
		.PropTyp = AWS.PropertySearchType.SingleProperty
		.SrchRule = AWS.SearchRuleType.Must
	End With
	With sc2
		.PropDefId = StockNumberPropDef
		.SrchOper = 1
		.SrchTxt = StockNumber
		.PropTyp = AWS.PropertySearchType.SingleProperty
		.SrchRule = AWS.SearchRuleType.Must
	End With
	scs1.Add(sc1)
	scs1.Add(sc2)

	'pdf w/ matching stock number in purchased parts folder
	foundfiles = mVltCon.WebServiceManager.DocumentService.FindFilesBySearchConditions(scs1.ToArray, srs.ToArray, fIDs1.ToArray, True, True, bm, ss)
	Try
		test = foundfiles.Length
	Catch
		scs2.Add(sc1)
		Try
		wsFolder = mVltCon.WebServiceManager.DocumentService.GetFolderByPath(FullFileName.Substring(0, FullFileName.LastIndexOf("/")))
		Catch
			MessageBox.Show(FullFileName.Substring(0, FullFileName.LastIndexOf("/")))

		End Try
		Dim fIDs2 As New List(Of Long)
		fIDs2.Add(wsFolder.Id)
		'pdf w/ matching folder location
		foundfiles = mVltCon.WebServiceManager.DocumentService.FindFilesBySearchConditions(scs2.ToArray, srs.ToArray, fIDs2.ToArray, True, True, bm, ss)
		Try
			test = foundfiles.Length
		Catch
			Logger.Info("PDF NOT IN VAULT - " & StockNumber)
			Return ""
		End Try
	End Try

	'get pdf
	Dim oSettings As VDF.Vault.Settings.AcquireFilesSettings
	oSettings = New VDF.Vault.Settings.AcquireFilesSettings(mVltCon, False)
	Dim fileToDownload As VDF.Vault.Currency.Entities.FileIteration
	fileToDownload = New VDF.Vault.Currency.Entities.FileIteration(mVltCon, foundfiles(0))
	oSettings.AddFileToAcquire(fileToDownload, VDF.Vault.Settings.AcquireFilesSettings.AcquisitionOption.Download)
	Dim oAcquireFilesResults As VDF.Vault.Results.AcquireFilesResults
	oSettings.OptionsRelationshipGathering.FileRelationshipSettings.IncludeChildren = True
	oSettings.OptionsRelationshipGathering.FileRelationshipSettings.RecurseChildren = True
	oSettings.OptionsRelationshipGathering.FileRelationshipSettings.VersionGatheringOption = VDF.Vault.Currency.VersionGatheringOption.Latest
	oSettings.OptionsResolution.OverwriteOption = VDF.Vault.Settings.AcquireFilesSettings.AcquireFileResolutionOptions.OverwriteOptions.NoOverwrite
	oAcquireFilesResults = mVltCon.FileManager.AcquireFiles(oSettings)

	Dim filefolder As AWS.Folder = mVltCon.WebServiceManager.DocumentService.GetFolderById(foundfiles(0).FolderId)
	Dim FilePath As String = filefolder.FullName & "\" & foundfiles(0).Name
	FilePath = FilePath.Replace("$", "C:/_IPI_Vault")
	FilePath = FilePath.Replace("\", "/")
	Return FilePath
End Function

 

0 Likes
Message 3 of 7

daltonNYAW9
Advocate
Advocate

I think I misunderstood your question. Are you wanting to access the embedded file under the "3rd Party" tab?

If so you could access the linked file and copy it to the new folder ex.

Dim oDoc As PartDocument = ThisDoc.Document
MessageBox.Show(oDoc.ReferencedOLEFileDescriptors.Item(1).FullFileName)

IO.File.Copy(oDoc.ReferencedOLEFileDescriptors.Item(1).FullFileName, "new folder file")
0 Likes
Message 4 of 7

chendersonCRKPU
Contributor
Contributor

The file is not embedded unfortunately. It appears as an attachment via the Vault tab

chendersonCRKPU_0-1742848735978.png

 

0 Likes
Message 5 of 7

mateusz_baczewski
Advocate
Advocate

Hello @chendersonCRKPU 

 

I wrote a code that first finds files in the Vault based on those you have in Inventor, then searches for all related files and selects those with extensions other than .iam, .ipt, .idw. Then, it downloads them to a single local folder. You need to change the path in the code where the files are downloaded. Let me know if this is how it was supposed to work.

AddReference "Autodesk.DataManagement.Client.Framework.dll"
Imports VDF1 = Autodesk.DataManagement.Client.Framework
AddReference "Autodesk.DataManagement.Client.Framework.Vault.dll"
Imports VDF = Autodesk.DataManagement.Client.Framework
AddReference "Connectivity.Application.VaultBase.dll"
Imports VB = Connectivity.Application.VaultBase
AddReference "Autodesk.Connectivity.WebServices.dll"
Imports AWS = Autodesk.Connectivity.WebServices

Sub main()
	
	Dim mVltCon As VDF.Vault.Currency.Connections.Connection
	mVltCon = VB.ConnectionManager.Instance.Connection
	If mVltCon Is Nothing Then
		MsgBox("Not logged in to Vault")
		Exit Sub
	End If
	
	Dim filePropDefs As AWS.PropDef() = mVltCon.WebServiceManager.PropertyService.GetPropertyDefinitionsByEntityClassId("FILE") 
	Dim namePropDef As AWS.PropDef = filePropDefs.[Single](Function(n) n.SysName = "Name")

	Dim oDoc As AssemblyDocument = ThisDoc.Document
	Dim totalResults As New List(Of aWS.File)()
	Dim associatedFiles As New List(Of aWS.File)()
	
	For Each oDocElement As Document In oDoc.AllReferencedDocuments
	
		Dim searchByName As New AWS.SrchCond() With { _
			.PropDefId = namePropDef.Id,
	       .PropTyp = AWS.PropertySearchType.SingleProperty, _
	       .SrchOper = 3, _
	       .SrchRule = AWS.SearchRuleType.Must, _
	       .SrchTxt = oDocElement.DisplayName}
		   
		   	Dim bookmark As String = String.Empty
		   	Dim status As Aws.SrchStatus = Nothing
			While status Is Nothing OrElse totalResults.Count < status.TotalHits
	
				Dim results As Aws.File() = mVltCon.WebServiceManager.DocumentService.FindFilesBySearchConditions(New Aws.SrchCond() {searchByName }, Nothing, Nothing, False, True, bookmark, status)
				
				If results IsNot Nothing Then
		      		totalResults.AddRange(results)
		       	Else
		       		Exit While
		       	End If
			End While
		Next
		
		For Each vaultFile As AWS.File In totalResults
			
			Dim fileAssoc As Aws.FileAssocArray = mVltCon.WebServiceManager.DocumentService.GetFileAssociationsByIds(
					New Long() {vaultFile.Id}, _
					AWS.FileAssociationTypeEnum.None, _ 
					False,  _
					AWS.FileAssociationTypeEnum.Attachment,  _
					False,  _
					False,  _
					False).First()
					
			For Each fileAssoc1 As AWS.FileAssoc In fileAssoc.FileAssocs
				fileName = fileAssoc1.CldFile.Name
				If Not (fileName.Contains(".idw") Or _
		            fileName.Contains(".ipt") Or _
		            fileName.Contains(".iam")) Then
       				associatedFiles.Add(fileAssoc1.CldFile)
    			End If
			Next
		Next
		
	 	
		For Each associatedFile As AWS.File In associatedFiles
			Dim settings As VDF.Vault.Settings.AcquireFilesSettings = New VDF.Vault.Settings.AcquireFilesSettings(mVltCon)
			settings.LocalPath = New VDF1.Currency.FolderPathAbsolute("Your/Path")
			
			Dim fileIter As VDF.Vault.Currency.Entities.FileIteration = New VDF.Vault.Currency.Entities.FileIteration(mVltCon, associatedFile)
			settings.AddFileToAcquire(fileIter, VDF.Vault.Settings.AcquireFilesSettings.AcquisitionOption.Download)

			Dim results As VDF.Vault.Results.AcquireFilesResults = mVltCon.FileManager.AcquireFiles(settings)

		Next
End Sub

 

If you found it helpful, a "Like" would be much appreciated!
If this post solved your problem, please mark it as "Solution.".

0 Likes
Message 6 of 7

daltonNYAW9
Advocate
Advocate

I didnt know about attaching files. This seems like a better workflow for our company tbh...
I made this rule that gets the attached file from vault and returns its path.

AddReference "System.IO"
AddReference "System.Private.Uri"
AddReference "Autodesk.DataManagement.Client.Framework.Vault.dll"
Imports VDF = Autodesk.DataManagement.Client.Framework
AddReference "Connectivity.Application.VaultBase.dll"
Imports VB = Connectivity.Application.VaultBase
AddReference "Autodesk.Connectivity.WebServices.dll"
Imports AWS = Autodesk.Connectivity.WebServices
Sub Main
	Dim filename As String = ThisDoc.Document.FullDocumentName

	Dim localfile As String = GetFromVault(filename)
	
	MessageBox.Show(localfile)

End Sub

Function GetFromVault(FullFileName As String)
	'convert file name to vault format
	FullFileName = FullFileName.Replace("C:\_IPI_Vault", "$")
	FullFileName = FullFileName.Replace("\", "/")
	Dim vaultpath() As String = New String() {FullFileName }


	'get vault login
	Dim mVltCon As VDF.Vault.Currency.Connections.Connection
	mVltCon = VB.ConnectionManager.Instance.Connection
	If mVltCon Is Nothing Then
		MsgBox("Not logged in to Vault")
		Exit Function
	End If

	Dim vaultfiles() As AWS.File = mVltCon.WebServiceManager.DocumentService.FindLatestFilesByPaths(vaultpath)
	Dim fileitem() As AWS.Item = mVltCon.WebServiceManager.ItemService.GetItemsByFileId(vaultfiles(0).Id)
	Dim itemIds() As Long = New Long() {fileitem(0).Id }
	Dim ItemAttmt() As AWS.ItemAttmt = mVltCon.WebServiceManager.ItemService.GetAttachmentsByItemIds(itemIds)

	Dim fileid As Long = ItemAttmt(0).AttmtArray(0).FileId

	Dim attachedfile As AWS.File = mVltCon.WebServiceManager.DocumentService.GetFileById(fileid)

	'get pdf
	Dim oSettings As VDF.Vault.Settings.AcquireFilesSettings
	oSettings = New VDF.Vault.Settings.AcquireFilesSettings(mVltCon, False)
	Dim fileToDownload As VDF.Vault.Currency.Entities.FileIteration
	fileToDownload = New VDF.Vault.Currency.Entities.FileIteration(mVltCon, attachedfile)
	oSettings.AddFileToAcquire(fileToDownload, VDF.Vault.Settings.AcquireFilesSettings.AcquisitionOption.Download)
	Dim oAcquireFilesResults As VDF.Vault.Results.AcquireFilesResults
	oSettings.OptionsRelationshipGathering.FileRelationshipSettings.IncludeChildren = True
	oSettings.OptionsRelationshipGathering.FileRelationshipSettings.RecurseChildren = True
	oSettings.OptionsRelationshipGathering.FileRelationshipSettings.VersionGatheringOption = VDF.Vault.Currency.VersionGatheringOption.Latest
	oSettings.OptionsResolution.OverwriteOption = VDF.Vault.Settings.AcquireFilesSettings.AcquireFileResolutionOptions.OverwriteOptions.NoOverwrite
	oAcquireFilesResults = mVltCon.FileManager.AcquireFiles(oSettings)

	Dim filefolder As AWS.Folder = mVltCon.WebServiceManager.DocumentService.GetFolderById(attachedfile.FolderId)
	Dim FilePath As String = filefolder.FullName & "\" & attachedfile.Name
	FilePath = FilePath.Replace("$", "C:/_IPI_Vault")
	FilePath = FilePath.Replace("\", "/")
	Return FilePath
End Function
0 Likes
Message 7 of 7

mateusz_baczewski
Advocate
Advocate

@chendersonCRKPU 

Hello,

 

I wrote a code that first finds files in the Vault based on those you have in Inventor, then searches for all related files and selects those with extensions other than .iam, .ipt, .idw. Then, it downloads them to a single local folder. You need to change the path in the code where the files are downloaded. Let me know if this is how it was supposed to work.

 

AddReference "Autodesk.DataManagement.Client.Framework.dll"
Imports VDF1 = Autodesk.DataManagement.Client.Framework
AddReference "Autodesk.DataManagement.Client.Framework.Vault.dll"
Imports VDF = Autodesk.DataManagement.Client.Framework
AddReference "Connectivity.Application.VaultBase.dll"
Imports VB = Connectivity.Application.VaultBase

Sub main()
	
	Dim mVltCon As VDF.Vault.Currency.Connections.Connection
	mVltCon = VB.ConnectionManager.Instance.Connection
	If mVltCon Is Nothing Then
		MsgBox("Not logged in to Vault")
		Exit Sub
	End If
	
	Dim filePropDefs As AWS.PropDef() = mVltCon.WebServiceManager.PropertyService.GetPropertyDefinitionsByEntityClassId("FILE") 
	Dim namePropDef As AWS.PropDef = filePropDefs.[Single](Function(n) n.SysName = "Name")

	Dim oDoc As AssemblyDocument = ThisDoc.Document
	Dim totalResults As New List(Of aWS.File)()
	Dim associatedFiles As New List(Of aWS.File)()
	
	For Each oDocElement As Document In oDoc.AllReferencedDocuments
	
		Dim searchByName As New AWS.SrchCond() With { _
			.PropDefId = namePropDef.Id,
	       .PropTyp = AWS.PropertySearchType.SingleProperty, _
	       .SrchOper = 3, _
	       .SrchRule = AWS.SearchRuleType.Must, _
	       .SrchTxt = oDocElement.DisplayName}
		   
		   	Dim bookmark As String = String.Empty
		   	Dim status As Aws.SrchStatus = Nothing
			While status Is Nothing OrElse totalResults.Count < status.TotalHits
	
				Dim results As Aws.File() = mVltCon.WebServiceManager.DocumentService.FindFilesBySearchConditions(New Aws.SrchCond() {searchByName }, Nothing, Nothing, False, True, bookmark, status)
				
				If results IsNot Nothing Then
		      		totalResults.AddRange(results)
		       	Else
		       		Exit While
		       	End If
			End While
		Next
		
		For Each vaultFile As AWS.File In totalResults
			
			Dim fileAssoc As Aws.FileAssocArray = mVltCon.WebServiceManager.DocumentService.GetFileAssociationsByIds(
					New Long() {vaultFile.Id}, _
					AWS.FileAssociationTypeEnum.None, _ 
					False,  _
					AWS.FileAssociationTypeEnum.Attachment,  _
					False,  _
					False,  _
					False).First()
					
			For Each fileAssoc1 As AWS.FileAssoc In fileAssoc.FileAssocs
				fileName = fileAssoc1.CldFile.Name
				If Not (fileName.Contains(".idw") Or _
		            fileName.Contains(".ipt") Or _
		            fileName.Contains(".iam")) Then
       				associatedFiles.Add(fileAssoc1.CldFile)
    			End If
			Next
		Next
		
	 	
		For Each associatedFile As AWS.File In associatedFiles
			Dim settings As VDF.Vault.Settings.AcquireFilesSettings = New VDF.Vault.Settings.AcquireFilesSettings(mVltCon)
			settings.LocalPath = New VDF1.Currency.FolderPathAbsolute("Your/Path")
			
			Dim fileIter As VDF.Vault.Currency.Entities.FileIteration = New VDF.Vault.Currency.Entities.FileIteration(mVltCon, associatedFile)
			settings.AddFileToAcquire(fileIter, VDF.Vault.Settings.AcquireFilesSettings.AcquisitionOption.Download)

			Dim results As VDF.Vault.Results.AcquireFilesResults = mVltCon.FileManager.AcquireFiles(settings)

		Next
End Sub
If you found it helpful, a "Like" would be much appreciated!
If this post solved your problem, please mark it as "Solution.".