Save and Replace Rule

Save and Replace Rule

layochim
Enthusiast Enthusiast
326 Views
4 Replies
Message 1 of 5

Save and Replace Rule

layochim
Enthusiast
Enthusiast

I'm trying to modify this rule to only rename certain characters in the file name, but have no success.  What i'm trying to accomplish is when i save my parent assembly which is "ST-100000 Stairs Assembly" to "250001-100000" i'd like for all the parts to retain the same amount of characters from the left.  Each part in the assembly uses "ST" as the first two characters, then it's the same amount of characters afterwards except for added characters such as "Stairs assembly" which i don't need along with other parts.  Any help?

 

If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MsgBox("This rule '" & iLogicVb.RuleName & "' only works for Assembly Documents.",vbOK, "WRONG DOCUMENT TYPE")
	Return
End If

Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument

Dim oFileDlg As Inventor.FileDialog = Nothing
ThisApplication.CreateFileDialog(oFileDlg)
oFileDlg.Filter = "Autodesk Inventor Assembly Files (*.iam)|*.iam"
oFileDlg.InitialDirectory = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath
oFileDlg.FileName = IO.Path.GetFileName(oADoc.FullFileName).TrimEnd("."c,"i"c,"a"c,"m"c)
oFileDlg.DialogTitle = "Specify New Name & Location For Copied Assembly"
oFileDlg.CancelError = True

On Error Resume Next
oFileDlg.ShowSave
If Err.Number <> 0 Then
	MsgBox("No File Saved.", vbOKOnly, "DIALOG CANCELED")
ElseIf oFileDlg.FileName <> "" Then
	oNewFileName = oFileDlg.FileName
	oADoc.SaveAs(oNewFileName, False)
End If

oADoc = Nothing

InventorVb.DocumentUpdate()

oADoc = ThisApplication.ActiveDocument

Dim oLast3Chars As String
For Each oRefDoc As Document In oADoc.AllReferencedDocuments
	ThisApplication.Documents.Open(oRefDoc.FullFileName,False)
	oLast3Chars = Left(Right(oRefDoc.FullFileName, 7), 10)
	If oRefDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
		oRefDoc.SaveAs(Left(oADoc.FullFileName, Len(oADoc.FullFileName) -4) & oLast3Chars & ".ipt", True)
	ElseIf oRefDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
		oRefDoc.SaveAs(Left(oADoc.FullFileName, Len(oADoc.FullFileName) -4) & oLast3Chars & ".iam", True)
	End If
	oRefDoc.Close
Next

Dim oOccDoc As Document
Dim oOccNewFileName As String
For Each oOcc As ComponentOccurrence In oADoc.ComponentDefinition.Occurrences
	oOccDoc = oOcc.Definition.Document
	oLast3Chars = Left(Right(oOccDoc.FullFileName, 7), 10)
	If oOccDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
		oOccNewFileName = Left(oADoc.FullFileName,Len(oADoc.FullFileName)-4) & oLast3Chars & ".ipt"
	ElseIf oOccDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
		oOccNewFileName = Left(oADoc.FullFileName,Len(oADoc.FullFileName)-4) & oLast3Chars & ".iam"
	End If
	oOcc.Replace(oOccNewFileName, True)
Next
0 Likes
Accepted solutions (1)
327 Views
4 Replies
Replies (4)
Message 2 of 5

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi @layochim, I didn't test, but I think this might work for you

If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MsgBox("This rule '" & iLogicVb.RuleName & "' only works for Assembly Documents.",vbOK, "WRONG DOCUMENT TYPE")
	Return
End If

Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument

Dim oFileDlg As Inventor.FileDialog = Nothing
ThisApplication.CreateFileDialog(oFileDlg)
oFileDlg.Filter = "Autodesk Inventor Assembly Files (*.iam)|*.iam"
oFileDlg.InitialDirectory = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath
oFileDlg.FileName = IO.Path.GetFileName(oADoc.FullFileName).TrimEnd("."c,"i"c,"a"c,"m"c)
oFileDlg.DialogTitle = "Specify New Name & Location For Copied Assembly"
oFileDlg.CancelError = True

On Error Resume Next
oFileDlg.ShowSave
If Err.Number <> 0 Then
	MsgBox("No File Saved.", vbOKOnly, "DIALOG CANCELED")
ElseIf oFileDlg.FileName <> "" Then
	oNewFileName = oFileDlg.FileName
	oADoc.SaveAs(oNewFileName, False)
End If

oADoc = Nothing

InventorVb.DocumentUpdate()

oADoc = ThisApplication.ActiveDocument

' Get the new base name without extension and path
Dim oNewBaseName As String = IO.Path.GetFileNameWithoutExtension(oADoc.FullFileName)

' Extract the new prefix (everything before the first hyphen)
Dim oNewPrefix As String = ""
Dim oHyphenPos As Integer = oNewBaseName.IndexOf("-")
If oHyphenPos > 0 Then
    oNewPrefix = oNewBaseName.Substring(0, oHyphenPos)
End If

' Calculate how many characters to keep from original name (excluding "ST")
' Based on your example: "ST-100000" -> "250001-100000"
' We keep 8 characters total, minus 2 for "ST" = 6 characters after "ST"
Dim oCharsToKeepAfterST As Integer = oNewBaseName.Length - oNewPrefix.Length

Dim oReferenceSuffix As String
For Each oRefDoc As Document In oADoc.AllReferencedDocuments
	ThisApplication.Documents.Open(oRefDoc.FullFileName,False)
	
	' Get original filename without extension
	Dim oOriginalName As String = IO.Path.GetFileNameWithoutExtension(oRefDoc.FullFileName)
	
	' Check if it starts with "ST" and extract the suffix
	If oOriginalName.StartsWith("ST") And oOriginalName.Length > 2 Then
		' Take the specified number of characters after "ST"
		Dim oAfterST As String = oOriginalName.Substring(2)
		If oAfterST.Length >= oCharsToKeepAfterST Then
			oReferenceSuffix = oAfterST.Substring(0, oCharsToKeepAfterST)
		Else
			oReferenceSuffix = oAfterST ' Use all available characters if less than needed
		End If
	Else
		' If it doesn't start with "ST", use a fallback approach
		' You might want to modify this based on your specific needs
		oReferenceSuffix = oOriginalName
	End If
	
	' Save with new naming convention
	If oRefDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
		oRefDoc.SaveAs(Left(oADoc.FullFileName, Len(oADoc.FullFileName) -4) & "\" & oNewPrefix & oReferenceSuffix & ".ipt", True)
	ElseIf oRefDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
		oRefDoc.SaveAs(Left(oADoc.FullFileName, Len(oADoc.FullFileName) -4) & "\" & oNewPrefix & oReferenceSuffix & ".iam", True)
	End If
	oRefDoc.Close
Next

Dim oOccDoc As Document
Dim oOccNewFileName As String
For Each oOcc As ComponentOccurrence In oADoc.ComponentDefinition.Occurrences
	oOccDoc = oOcc.Definition.Document
	
	' Get original filename without extension
	Dim oOriginalName As String = IO.Path.GetFileNameWithoutExtension(oOccDoc.FullFileName)
	
	' Check if it starts with "ST" and extract the suffix
	If oOriginalName.StartsWith("ST") And oOriginalName.Length > 2 Then
		' Take the specified number of characters after "ST"
		Dim oAfterST As String = oOriginalName.Substring(2)
		If oAfterST.Length >= oCharsToKeepAfterST Then
			oReferenceSuffix = oAfterST.Substring(0, oCharsToKeepAfterST)
		Else
			oReferenceSuffix = oAfterST
		End If
	Else
		oReferenceSuffix = oOriginalName
	End If
	
	' Build new filename
	If oOccDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
		oOccNewFileName = Left(oADoc.FullFileName,Len(oADoc.FullFileName)-4) & "\" & oNewPrefix & oReferenceSuffix & ".ipt"
	ElseIf oOccDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
		oOccNewFileName = Left(oADoc.FullFileName,Len(oADoc.FullFileName)-4) & "\" & oNewPrefix & oReferenceSuffix & ".iam"
	End If
	oOcc.Replace(oOccNewFileName, True)
Next

 

EESignature

0 Likes
Message 3 of 5

layochim
Enthusiast
Enthusiast

That worked like a Treat!  Thank you very much!

0 Likes
Message 4 of 5

layochim
Enthusiast
Enthusiast

@Curtis_Waguespack is there a way to do all the sub assemblies as well with this rule? i could just copy and paste the rule into those if there's not

0 Likes
Message 5 of 5

layochim
Enthusiast
Enthusiast

To better clarify, all the parts within those sub assemblies, i see they didnt save and replace

0 Likes