Save stp files to Vault

Save stp files to Vault

mich_rymut
Explorer Explorer
324 Views
1 Reply
Message 1 of 2

Save stp files to Vault

mich_rymut
Explorer
Explorer

Hi,

I am trying to make a rule that lets me select parts in an assembly and then generate .stp files of chosen parts, and then save them into vault.
I am using Inventor Professional 2023 and Vault Professional 2023.
I have this rule below, that lets me choose parts and then generates .stp files of those parts into a chosen folder, but i dont know how to make it to save those stp files to vault.
Is there a way to save those files directly to vault?
Or a way to automaticly  import them into vault after saving them into a folder on my desktop?
Here is the rule:

Sub Main()
	Dim ass As AssemblyDocument = ThisApplication.ActiveDocument
	Dim oSelSet As SelectSet = ass.SelectSet
	If oSelSet.Count = 0 Then
		MessageBox.Show("Proszę wybrać pliki przed uruchomieniem polecenia", "Error")
		Exit Sub
	End If
	'this runs the custom Function defined below to return a selected folder path
	oFolder = GetFolder
	'make sure a folder was selected, if not exit rule
	If String.IsNullOrEmpty(oFolder) Then Exit Sub
	Dim i As Integer = 0
	Dim result As Boolean
	Dim Occ As ComponentOccurrence
	For Each Occ In oSelSet
		Dim OccDoc As Document = Occ.Definition.Document
		'get file name (without path & without file extension)
		oName = System.IO.Path.GetFileNameWithoutExtension(OccDoc.FullFileName)
		'put new STEP file name together
		oNewName = oFolder & "\" & oName & ".stp"
		result = ExportToSTEP(OccDoc, oNewName)
		If result = True Then
			i = i + 1
		End If
	Next
	MessageBox.Show("Liczba zapisanych plików: " & i, "Wynik zapisu")
End Sub

'Here's the subroutine.
Function ExportToSTEP(doc As Document, NewFileName As String) As Boolean
	' Get the STEP translator Add-In.
	Dim oSTEPTranslator As TranslatorAddIn
	oSTEPTranslator = ThisApplication.ApplicationAddIns.ItemById("{90AF7F40-0C01-11D5-8E83-0010B541CD80}")
	If oSTEPTranslator Is Nothing Then
		MsgBox("Could not access STEP translator.")
		ExportToSTEP = False
		Exit Function
	End If

	oContext = ThisApplication.TransientObjects.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	oOptions = ThisApplication.TransientObjects.CreateNameValueMap
	oData = ThisApplication.TransientObjects.CreateDataMedium
	oData.FileName = NewFileName
	
	If oSTEPTranslator.HasSaveCopyAsOptions(doc, oContext, oOptions) Then
		' Set application protocol.
		' 2 = AP 203 - Configuration Controlled Design
		' 3 = AP 214 - Automotive Design
		oOptions.Value("ApplicationProtocolType") = 3
		'oOptions.Value("Author") = ""
		'oOptions.Value("Authorization") = ""
		'oOptions.Value("Description") = ""
		'oOptions.Value("Organization") = ""

		oSTEPTranslator.SaveCopyAs(doc, oContext, oOptions, oData)
		ExportToSTEP = True
    End If
End Function

Function GetFolder() As String
	'Imports System.Windows.Forms
	Dim oSFolder As String
	Dim oFDialog As New System.Windows.Forms.FolderBrowserDialog
	oFDialog.Description = "Wybierz folder do zapisu plików .step."
	oFDialog.RootFolder = System.Environment.SpecialFolder.MyComputer
	Dim oResult As DialogResult = oFDialog.ShowDialog()
	If oResult = DialogResult.OK Then
		oSFolder = oFDialog.SelectedPath
	Else
		Return String.Empty
	End If
	Return oSFolder
End Function



0 Likes
Accepted solutions (1)
325 Views
1 Reply
Reply (1)
Message 2 of 2

mich_rymut
Explorer
Explorer
Accepted solution

Hi,
i have managed to get it to work.
I followed this guide to install vault API and all the libraries:
https://www.autodesk.com/autodesk-university/class/iLogic-and-Vault-Vault-and-iLogic-2020#handout
And i got it working with this code: 

AddReference "QuickstartiLogicLibrary.dll"

Sub
Main() Dim ex As Exception break 'the rule can be run on assembly files only If ThisDoc.Document.DocumentType <> Inventor.DocumentTypeEnum.kAssemblyDocumentObject Logger.Info("Rule iLogicVault_Generate_stp applies to assembly documents only. Exited without action.") Return End If 'enable iLogicVault commands and validate user's login state Dim iLogicVault As New QuickstartiLogicLibrary.QuickstartiLogicLib If iLogicVault.LoggedIn = False Logger.Error("Not Logged In to Vault! - Login first and repeat executing this rule.") Exit Sub End If Dim ass As AssemblyDocument = ThisApplication.ActiveDocument Dim oSelSet As SelectSet = ass.SelectSet If oSelSet.Count = 0 Then MessageBox.Show("Choose files before running the rule", "Error") Exit Sub End If Dim i As Integer = 0 Dim result As Boolean Dim Occ As ComponentOccurrence For Each Occ In oSelSet Dim OccDoc As Document = Occ.Definition.Document 'Get the file path and name with extension oName = System.IO.Path.GetFullPath(OccDoc.FullFileName) ' change extensions by removing last 3 characters and adding stp lenght_oName = oName.Length lenght_we = lenght_oName - 3 oName_we = oName.remove(lenght_we) oName_ne = oName_we & "stp" oNewName = oName_ne result = ExportFiles(OccDoc, oNewName) If result = True Then i = i + 1 End If Next MessageBox.Show("Number of saved files: " & i, "Result") End Sub Function ExportFiles(doc As Document, NewFileName As String) As Boolean ' Get the STEP translator Add-In. Dim oSTEPTranslator As TranslatorAddIn oSTEPTranslator = ThisApplication.ApplicationAddIns.ItemById("{90AF7F40-0C01-11D5-8E83-0010B541CD80}") If oSTEPTranslator Is Nothing Then MsgBox("Could not access STEP translator.") ExportFiles = False Exit Function End If oContext = ThisApplication.TransientObjects.CreateTranslationContext oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism oOptions = ThisApplication.TransientObjects.CreateNameValueMap oData = ThisApplication.TransientObjects.CreateDataMedium oData.FileName = NewFileName oNewName = NewFileName If oSTEPTranslator.HasSaveCopyAsOptions(doc, oContext, oOptions) Then ' Set application protocol. ' 2 = AP 203 - Configuration Controlled Design ' 3 = AP 214 - Automotive Design oOptions.Value("ApplicationProtocolType") = 3 'oOptions.Value("Author") = "" 'oOptions.Value("Authorization") = "" 'oOptions.Value("Description") = "" 'oOptions.Value("Organization") = "" 'check if file exists and if it does, delete it. Dim oFileInfo As New System.IO.FileInfo(oNewName) Try If oFileInfo.Exists = True Then If (oFileInfo.Attributes) Then oFileInfo.Attributes = (oFileInfo.Attributes And Not oFileInfo.Attributes.ReadOnly) System.IO.File.Delete(oNewName) End If End If Catch End Try 'finally save step file oSTEPTranslator.SaveCopyAs(doc, oContext, oOptions, oData) ExportFiles = True ' export file to vault Dim iLogicVault As New QuickstartiLogicLibrary.QuickstartiLogicLib Dim mVaultPath As String = iLogicVault.ConvertLocalPathToVaultPath(oNewName) success = iLogicVault.AddFile(oNewName, mVaultPath, True) If (success <> True) Logger.Error("Failed to add/update stp file to Vault") Else Logger.Info("Stp file successfully created and uploaded to Vault.") End If End If End Function

 

0 Likes