Solved! Go to Solution.
Solved by Owner2229. Go to Solution.
Hi,
1) so you want the rule to "update" the file names of the current files. Not copy them, just rename the existing parts and update the refferences in assys.
Right?
2) Should the file names update for both parts and assys or just for parts?
3) Now to the file name. How does the "last part of it" (LP) look like? Something like this?
C:\SomePath\MyAwesomePart Nr.105 MySupperLastPartOfName.ipt
4) Can the same text as in "LP" be in somewhere else in the rest of the file name, or is it unique?
Hi, so is this schema accurate?
MainAssy - do nothing
SubAssy1 - copy and rename
Part1 - rename
Part2 - rename
SubAssy2 - do nothing
Part3 - rename
Part4 - rename
SubAssy3 - copy and rename
Part5 - rename
Part6 - rename
Hi,
No, I'm sorry,maybe I explained not so much good.
The right schema is this below:
MainAssy - do nothing
SubAssy1 - copy and rename
Part1 - do nothing
Part2 - do nothing
SubAssy2 - do nothing
Part3 - do nothing
Part4 - do nothing
SubAssy3 - copy and rename
Part5 - do nothing
Part6 - copy and rename
SubAssy4 -copy and rename
To explain just the first level below the mainAssy!
Alright then, here is your code:
Dim TextToFind As String = "ed0" Dim TextToReplace As String = "_logo_ed1" Dim oDoc As Document = ThisApplication.ActiveDocument Dim oOcc As ComponentOccurrence For Each oOcc In oDoc.ComponentDefinition.Occurrences Dim aDoc As Document = oOcc.Definition.Document If aDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then Dim oName As String = aDoc.FullFileName Dim xDoc As Document = ThisApplication.Documents.Open(oName, True) Dim FNP As Integer = InStrRev(oName, "\", -1) Dim oPath As String = Left(oName, FNP) Dim oNewName As String = Mid(oName, FNP + 1) oNewName = oNewName.Replace(TextToFind, TextToReplace) xDoc.SaveAs(oPath & oNewName, False) xDoc.Close(True) End If Next
Can you provide both error screens please?
Hi, the code I send you was iLogic, not VBA. And you have placed the code outside of Sub-Routine.
Sub Test() ' ... Code goes here End Sub
Here's the VBA version of the code as whole Sub-Routine:
Sub Test() Dim TextToFind As String
TextToFind = "ed0" Dim TextToReplace As String
TextToReplace = "_logo_ed1" Dim oDoc As Document
Set oDoc = ThisApplication.ActiveDocument Dim oOcc As ComponentOccurrence For Each oOcc In oDoc.ComponentDefinition.Occurrences Dim aDoc As Document
Set aDoc = oOcc.Definition.Document If aDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then Dim oName As String
oName = aDoc.FullFileName Dim xDoc As Document
Set xDoc = ThisApplication.Documents.Open(oName, True) Dim FNP As Integer
FNP = InStrRev(oName, "\", -1) Dim oPath As String
oPath = Left(oName, FNP) Dim oNewName As String
oNewName = Mid(oName, FNP + 1) oNewName = Replace(oNewName, TextToFind, TextToReplace) xDoc.SaveAs (oPath & oNewName), False xDoc.Close (True) End If Next End Sub
Here you go:
Sub Test() Dim TextToFind As String TextToFind = "ed0" Dim TextToReplace As String TextToReplace = "_logo_ed1"
Dim NewFolder As String
' Leave this string empty to copy to current folder
NewFolder = "C:\MyFiles\"
Dim oDoc As Document Set oDoc = ThisApplication.ActiveDocument Dim oOcc As ComponentOccurrence For Each oOcc In oDoc.ComponentDefinition.Occurrences Dim aDoc As Document Set aDoc = oOcc.Definition.Document If aDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then Dim oName As String oName = aDoc.FullFileName Dim xDoc As Document Set xDoc = ThisApplication.Documents.Open(oName, True) Dim FNP As Integer FNP = InStrRev(oName, "\", -1) Dim oPath As String If NewFolder = "" Then
oPath = Left(oName, FNP)
Else
oPath = NewFolder
End If Dim oNewName As String oNewName = Mid(oName, FNP + 1) oNewName = Replace(oNewName, TextToFind, TextToReplace) xDoc.SaveAs (oPath & oNewName), False xDoc.Close (True) End If Next End Sub
You're welcomed 😉
This problem is a lot more complex than the simple answer implys.
1) The op specifies parts to be copied and renamed under the master assembly.
2) Nothing is said of drawing files. Do they need to be processed as well?
3) The replace function can get you into trouble if it is not 100% specific. if "ed0" exists anywhere in the full path filename unwanted renaming could happen.
A better solution for #3 is to include the file extention witht he replace() funciton.
newFilename = replace(oldfilename,"ed0.ipt","_logo_ed1.ipt")
newFilename = replace(oldfilename,"ed0.iam","_logo_ed1.iam")
newFilename = replace(oldfilename,"ed0.idw","_logo_ed1.idw")
Hi, these are actualy valid concerns. Roberto, do you need your drawings' files to be renamed / moved as well and updated to reference to the new assembly names?
Here is the code updated, with 3) fixed.
Sub Test() Dim TextToFind As String TextToFind = "ed0" Dim TextToReplace As String TextToReplace = "_logo_ed1" Dim NewFolder As String ' Leave this string empty to copy to current folder NewFolder = "C:\MyFiles\" Dim oDoc As Document Set oDoc = ThisApplication.ActiveDocument Dim oOcc As ComponentOccurrence For Each oOcc In oDoc.ComponentDefinition.Occurrences Dim aDoc As Document Set aDoc = oOcc.Definition.Document If aDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then Dim oName As String oName = aDoc.FullFileName Dim xDoc As Document Set xDoc = ThisApplication.Documents.Open(oName, True) Dim FNP As Integer FNP = InStrRev(oName, "\", -1) Dim oPath As String If NewFolder = "" Then oPath = Left(oName, FNP) Else oPath = NewFolder End If Dim oNewName As String oNewName = Mid(oName, FNP + 1)
Dim FRP As Integer = oNewName.LastIndexOf(TextToFind)
If Not FRP = -1 Then
oNewName = oNewName.Remove(FRP, Len(TextToFind)).Insert(FRP, TextToReplace)
End If xDoc.SaveAs (oPath & oNewName), False xDoc.Close (True) End If Next End Sub
Are the Drawings' filenames the same as the Assemblies' ?
E.g.:
MyAssyOne.iam
MyAssyOne.idw
Or do they differ somehow?
It wouldn't be a problem if the different names are made with an rule in theyr naming. I believe you know what I'm pointing at.
E.g.
MyAssyOne.iam
MyAssyOneX1A.idw
MyAssyTwo.iam
MyAssyTwoX1A.idw
The problem might come with completely different names. It would have to be handled by exceptions, so each drawing will be one line of code (imagine it for hundreds of drawing).
E.g.
MyAssyOne.iam
MyDrawingXZA.idw
MyAssyTwo.iam
MyXFADrawing.idw
Can't find what you're looking for? Ask the community or share your knowledge.