Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Rule ilogic o VBA in Inventor 2015 to subtitute and rename a mass of assembly in

33 REPLIES 33
SOLVED
Reply
Message 1 of 34
Anonymous
2750 Views, 33 Replies

Rule ilogic o VBA in Inventor 2015 to subtitute and rename a mass of assembly in


Hi, I have a problem:I have an assembly containing a mass of assembleys.
Often I have to update all those assembley changing the last part of their names, the better thing would be substitute each assembly and rename the new one substituing the last part of name with an other. The important thing is that all the assembleys have the same last part of name and I have to substitute this part with the same text for each assembleys.
Just this, not copy the parts and subassembly contained in those assembly,just riutilize them . This assembleys are normal (not ilogic, no iassembly)
Could you write to me a rule (better would be VBA but ok also ilogic if you are able to do by it) to do this automatically for whole assembleys contained in my assembly? I don't need form or similar, it could be sufficiently to write the text to substitute and the newone at code level
Actually I make it by design assistance but I lose a lot of time each time.
Thank you for you help
33 REPLIES 33
Message 2 of 34
Owner2229
in reply to: Anonymous

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?

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 3 of 34
Anonymous
in reply to: Anonymous

Hi and thank you for your time.
1- well,maybe it would be better to have a copy of the assembleys ,but no their components under ,if possible,if just rename no problem,I could male a copy of all before to another path so I could have the old revisione.
2-The assembly that contain the assembleys is just to keep all there,but I don't need to rename it. The assembleys contained in it has to be "saved as " ,to undestand you, just those assembleys, not their sub parts/assembleys. I hope to explain right
3- the lenght of whole assembleys is the same, the path is the same ,and the last part is always "ed0", and I have to substitute it with "_logo_ed1" this time,but in future this text can be differenti,but no problem,I will change the source code eventually

4-I think that text doesn't be in the rest of filename. I think you ask me this because you think to male a search ? Please to considerazione also to take a function like "right()" or similar if there is

Thank you again a lot
Message 4 of 34
Owner2229
in reply to: Anonymous

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

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 5 of 34
Anonymous
in reply to: Owner2229

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! 

Message 6 of 34
Owner2229
in reply to: Anonymous

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

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 7 of 34
Anonymous
in reply to: Anonymous

Thank you for your time!
But if I try to paste this code in Inventor VBA module it give's many errors... I don't know why.
Could you help me?
Message 8 of 34
Owner2229
in reply to: Anonymous

Can you provide both error screens please?

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 9 of 34
Anonymous
in reply to: Anonymous
Message 10 of 34
Owner2229
in reply to: Anonymous

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

 

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 11 of 34
Anonymous
in reply to: Anonymous

Fantastic!!!! A last thing: if I would chose a new path to save the new files? It is sufficiently to do it at code level,not with a form.
Thank you for your time
Message 12 of 34
Owner2229
in reply to: Anonymous

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
Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 13 of 34
Anonymous
in reply to: Anonymous

You rule!!!! Thank you!!!
Message 14 of 34
Owner2229
in reply to: Anonymous

You're welcomed 😉

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 15 of 34
Anonymous
in reply to: Owner2229

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")

 

 

 

 

Message 16 of 34
Owner2229
in reply to: Anonymous

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
Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 17 of 34
Anonymous
in reply to: Anonymous

Thank you for your time!
This time i don't need my drawings' files to be renamed / moved as well and updated to reference to the new assembly names. But it could be very useful to have also this option!
Are you so kindly to include a check with this?
Thank you a lot
Message 18 of 34
Owner2229
in reply to: Anonymous

Are the Drawings' filenames the same as the Assemblies' ?

 

E.g.:

MyAssyOne.iam

MyAssyOne.idw

 

Or do they differ somehow?

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 19 of 34
Anonymous
in reply to: Anonymous

Yes, we consider to have same filename to not complicate toomuch the things. But would you able to make it also with differenti names?! Wow
Message 20 of 34
Owner2229
in reply to: Anonymous

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

 

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report