iLogic Overwrite Files with Renamed Files

iLogic Overwrite Files with Renamed Files

EScales
Advocate Advocate
920 Views
3 Replies
Message 1 of 4

iLogic Overwrite Files with Renamed Files

EScales
Advocate
Advocate

Here is my scenario.  I have an iLogic rule that automatically saves an .ipt file as a .stp file and an .stl file and also attaches them to the .ipt file.  This allows me to check into Vault, the .ipt file and both of the attached files together.

My rule is setup to save the .stp and .stl files as the iProperties "Part Number", not the actual file name.  The rule also looks at the iProperties "Revision Number" and appends it with an "_" on the end of the new file name.  If the Revision Number is "-", then it appends "0" to the filename, otherwise it uses whatever Rev letter is currently in the field...ie "A, B, C" 

This image shows the filename is "Z-Eric999.ipt and the stp/stl filenames are pulled from the Part Number.  Then, there is no revision, so it appends "_0" to the name.

Step Attached to Part.PNG

We are using catogories/lifecycles for Revision control in Vault.  So, if I change my vault lifecycle back to Work in Progress and check the files out again, to make a change, the Revision is bumped to Revision "A".  Now, I'm back in Inventor making my changes and I save my part again.  This triggers my iLogic rule to resave the .stp and .stl files again.  So far, up to this point, everything works the way I want.

My problems begin when my rule automatically resaves the .stp and .stl files.  Now, the rule sees that the Revision Number is different (now it's "A") and saves a new file with the part number and "_A" appended to the name.  It then attaches this new file to the .ipt file along with the previously attached files.

This image shows both the originally attached files and the newly resaved files also attached.

Multiple_Step_Files.png

So,when I check the .ipt file into the vault, it checks in all the attached files, but the newest files don't match the revision level in the vault, because they are brand new and have not be through the lifecycle yet.

This image shows the revision level mismatch in vault.

Vault_Rev_Match.png

I'd really like the iLogic rule to replace the files with the newly saved files, remaining attached and maintaining the current vault revision level.  And, in vault, there'd  only be one .stp and one .stl file attached to the .ipt file instead of accumulating multiple files.

I have separate rules for the .stp and .stl file saving, but they are triggered at the same time when saving the .ipt file.

Here is my current .stp file rule.

SyntaxEditor Code Snippet

''' <summary>
''' Will only run if the activedocument has been saved.
''' </summary> 
Sub Main()
	Dim trans As Transaction = ThisApplication.TransactionManager.StartTransaction(ThisApplication.ActiveDocument, "Export and Attach Step file")
		If Not iProperties.Value("Project", "Part Number") = "" Then
			Dim filenameToAttach As String = ExportToStep()
			If Not filenameToAttach = String.Empty Then
				Dim doc As Document = ThisApplication.ActiveDocument
				AddReferences(doc, filenameToAttach)
			End If
		Else
			MessageBox.Show("NuTec Part Number is blank...Cannot save the .stp file","Error Saving File")
			trans.Abort()
		End If
End Sub

''' <summary>
''' Returns an empty string if the stp file didn't save for some reason.
''' </summary>
''' <returns></returns>
Function ExportToStep() As String
	Dim filename As String
	' Get the STEP translator Add-In.
	Dim oSTEPTranslator As TranslatorAddIn
	oSTEPTranslator = ThisApplication.ApplicationAddIns.ItemById("{90AF7F40-0C01-11D5-8E83-0010B541CD80}")
	Dim oContext As TranslationContext
	oContext = ThisApplication.TransientObjects.CreateTranslationContext
	Dim oOptions As NameValueMap
	oOptions = ThisApplication.TransientObjects.CreateNameValueMap

	Dim Rev As String
	If iProperties.Value("Project", "Revision Number") = "-" Then
		Rev = "_0"
	ElseIf iProperties.Value("Project", "Revision Number") = "" Then
		Rev = ""
	ElseIf Not iProperties.Value("Project", "Revision Number") = "-" Or iProperties.Value("Project", "Revision Number") = "" Then
		Rev = "_" & iProperties.Value("Project", "Revision Number")
	End If

	oStepFileName = iProperties.Value("Project", "Part Number") & Rev

	If oSTEPTranslator.HasSaveCopyAsOptions(ThisApplication.ActiveDocument, oContext, oOptions) Then
	    ' Set application protocol.
	    ' 2 = AP 203 - Configuration Controlled Design
	    ' 3 = AP 214 - Automotive Design
	    oOptions.Value("ApplicationProtocolType") = 3
	    ' Other options...
	    'oOptions.Value("Author") = ""
	    'oOptions.Value("Authorization") = ""
	    'oOptions.Value("Description") = ""
	    'oOptions.Value("Organization") = ""
	    oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	    Dim oData As DataMedium
	    oData = ThisApplication.TransientObjects.CreateDataMedium
		oData.FileName = ThisDoc.Path & "\" & oStepFileName & ".stp"
		oSTEPTranslator.SaveCopyAs(ThisApplication.ActiveDocument, oContext, oOptions, oData)
		filename = oData.FileName
	End If
	If System.IO.File.Exists(filename) Then
		Return filename
	Else
		Return ""
	End If
End Function

''' <summary>
''' Attaches any file using the full c:\path\to\your\file.extension format.
''' </summary>
''' <param name="odoc"></param>
''' <param name="selectedfile"></param>
Public Sub AddReferences(ByVal odoc As Inventor.Document, ByVal selectedfile As String)
	Dim oleReference As ReferencedOLEFileDescriptor
	If selectedfile.Contains("|") Then ' we have multiple files selected.
		Dim file As String() = selectedfile.Split("|")
		For Each s As String In file
			oleReference = odoc.ReferencedOLEFileDescriptors _
				.Add(s, OLEDocumentTypeEnum.kOLEDocumentLinkObject)
			oleReference.BrowserVisible = True
			oleReference.Visible = False
			oleReference.DisplayName = Mid$(s, InStrRev(s, "\") + 1)
		Next
	Else
		oleReference = odoc.ReferencedOLEFileDescriptors _
				.Add(selectedfile,OLEDocumentTypeEnum.kOLEDocumentLinkObject)
		oleReference.BrowserVisible = True
		oleReference.Visible = False
		oleReference.DisplayName = Mid$(selectedfile, InStrRev(selectedfile, "\") + 1)
	End If
End Sub

 

0 Likes
Accepted solutions (1)
921 Views
3 Replies
Replies (3)
Message 2 of 4

chandra.shekar.g
Autodesk Support
Autodesk Support
Accepted solution

@EScales,

 

Hoping that below 3 steps would help in avoiding overwrite files.

 

  1. Before adding .stp or .stl reference to part document, delete the existing reference by below code.
Public Sub AddReferences(ByVal odoc As Inventor.Document, ByVal selectedfile As String)
	Dim oleReference As ReferencedOLEFileDescriptor
For each oleReference in odoc.ReferencedOLEFileDescriptors
Call oleReference.Delete()
Next If selectedfile.Contains("|") Then ' we have multiple files selected. Dim file As String() = selectedfile.Split("|") For Each s As String In file oleReference = odoc.ReferencedOLEFileDescriptors _ .Add(s, OLEDocumentTypeEnum.kOLEDocumentLinkObject) oleReference.BrowserVisible = True oleReference.Visible = False oleReference.DisplayName = Mid$(s, InStrRev(s, "\") + 1) Next Else oleReference = odoc.ReferencedOLEFileDescriptors _ .Add(selectedfile,OLEDocumentTypeEnum.kOLEDocumentLinkObject) oleReference.BrowserVisible = True oleReference.Visible = False oleReference.DisplayName = Mid$(selectedfile, InStrRev(selectedfile, "\") + 1) End If End Sub

     2. Delete the file by searching name in vault (https://forums.autodesk.com/t5/vault-customization/delete-api/td-p/3019450) 

     3. Revise the newly added file (https://forums.autodesk.com/t5/vault-forum/revise-using-api/td-p/2763740)

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 3 of 4

EScales
Advocate
Advocate

Step number 1 seems to work, but I have one little problem.  I have separate rules for .stp and .stl files.  If I add these additional lines to both rules. The second rule deletes the file that the first rule just created.  Is there a way of only deleting the old .stp files when running the .stp rule and only deleting the old .stl files when running the .stl rule?

Can something be added to this?

ReferencedOLEFileDescriptor (enter file type here?)

 

0 Likes
Message 4 of 4

EScales
Advocate
Advocate

It seems that if I only add this to one of my rules and trigger both rules to run before saving the .ipt file, everything works fine.  I'm happy with that. 

Thanks for all the help!  I really appreciate it.

0 Likes