What's new

Scrip request: Rename and save a complex assembly from a step-file automatically in an ascending number sequence

Finish

New Member
Hello everyone,

I just registered because we have a recurring task and I hope that maybe some smart coder of the english speaking Alibre community can help us automise it.


Name: "Rename and save an assembly after step import" script
What it does: The user imports an assembly from a step file and the script automatically renames and saves all contained parts and sub-assemblies in ascending number sequence
Procedure and Things to Consider:
1. User imports step file for the first time
2. User launches script in Alibre script
3. Script renames the main assembly to "1000" (the starting number can be adjusted manually in the script code, e.g. 2000, 50000,..).
4. Script renames all sub-assemblies and parts in ascending order (1001, 1002,...,1762)
(No special order of renaming is required)
5. Script saves the renamed assembly and all its parts to a specified folder (whose path can be manually adjusted in the script code).

Is there any way someone could write such a script? - Or has it already been released and I missed it in my research?

This would be a great help for our small company, as we often receive complex CATIA assemblies as step files from customers, whose individual file names are too long for Windows (in combination with our file path) and require manual renaming in the "Save As" menu.
So we waste a lot of time on a repetitive, unproductive and unpleasant task, before we can actually start a project.
Picture of the manual "Save As Procedure" attached.

Greetings from Germany.

Finish
 

Attachments

  • Manual save as procedure.jpg
    Manual save as procedure.jpg
    620.9 KB · Views: 29

NateLiquidGravity

Alibre Super User
I'd have to really look into this. It might be possible in Alibre Script or it might require an AutoHotkey script to takeover entry in the save form. Either way 100% possible.
 

bolsover

Senior Member
I'd have to really look into this. It might be possible in Alibre Script or it might require an AutoHotkey script to takeover entry in the save form. Either way 100% possible.
I think you may be right about needing something like AutoHotKey. When you initially open a step file, there are no entries in the IADSession.ContituentFilePaths property. That property is only populated once the step has been saved as an assembly. I don't see any way to rename the component parts from the assembly session and it is no use renaming the parts after a save since that would break the assembly.
The solution seems like it will need some way of renaming the constituent parts while the save dialog is open - tricky!

David
 

Finish

New Member
Autohotkey was indeed the answer!
Thanks for making me aware of its existence.

Problem: I am not a programmer.
Solution: Luckily it's 2023 and ChatGPT is the next big thing in IT according to the news and media of the last few months.

So I gave it a try and after a few failed attempts I got this:

Code:
    counter := 30001


    ; Define the hotkey that will start the renaming process

    F1::

        ; Reset the counter to the starting value

        counter := 30001

  

        ; Send the first arrow down key to select the first file in the save as menu

        Send {Down}

  

        ; Loop through the files in the save as menu

        Loop

        {

            ; Send the counter value as the new file name

            SendInput {Raw}%counter%

      

            ; Send another arrow down key to select the next file in the save as menu

            Send {Down}

      

            ; Increment the counter

            counter++

      

            ; Wait a short time to ensure that the new file name is registered

            Sleep 50

      

            ; Check if the escape key has been pressed

            if GetKeyState("Escape", "P")

            {

                ; If the escape key has been pressed, reset the counter to the starting value and exit the loop

                counter := 30001

                break

            }

        }

    return

It's not perfect, but it does the job!

Only problems I have:

1) The script doesn't recognise the last line, so instead of stopping it keeps on counting and extending the last filename forever.
But thanks to the Esc exit function, you can stop the script and just rename the last file manually.

2) The larger the assembly, the slower the script. - But 700 files now take 5-10 minutes instead of 1-2 hours of repetitive typing.

3) Although I asked ChatGPT to write a script for Autohotkey 2.0, it requires the old version 1.1.36.02 to be installed.
So I am not sure how long this script will be compatible with future Autohotkey versions...

Maybe someone with real coding skills can improve it further?
But for now it already helps me a lot.
 
Last edited:

NateLiquidGravity

Alibre Super User
I think you may be right about needing something like AutoHotKey. When you initially open a step file, there are no entries in the IADSession.ContituentFilePaths property. That property is only populated once the step has been saved as an assembly. I don't see any way to rename the component parts from the assembly session and it is no use renaming the parts after a save since that would break the assembly.
The solution seems like it will need some way of renaming the constituent parts while the save dialog is open - tricky!

David
That was my thoughts as well. No rename function, no replace function, no way to change file paths/names linked. It even gave me errors when I attempted to "save as" individual parts of an opened assembly. - I had planned to "save as" all parts then add them all to a new assembly positioned exactly as their originals - but couldn't even "save as".
 
Last edited:

NateLiquidGravity

Alibre Super User
Nicely done.
1) The script doesn't recognise the last line, so instead of stopping it keeps on counting and extending the last filename forever.
But thanks to the Esc exit function, you can stop the script and just rename the last file manually.
Suggest to ChatGPT to alter the code so that after pressing the down arrow it should copy the existing text to the clipboard and compare that to the previous counter value to see if it had already been replaced and is therefore already at the bottom.
 

NateLiquidGravity

Alibre Super User
3) Although I asked ChatGPT to write a script for Autohotkey 2.0, it requires the old version 1.1.36.02 to be installed.
So I am not sure how long this script will be compatible with future Autohotkey versions...
AutoHotkey 2.0 was just officially released (about a month ago?), so I wouldn't worry too much.
 

Finish

New Member
AutoHotkey 2.0 was just officially released (about a month ago?), so I wouldn't worry too much.
That's probably why ChatGPT doesn't support 2.0 yet.
I guess by the time this script is outdated, ChatGPT will be able to adapt the code for Autohotkey 2.x ;)

Nicely done.

Suggest to ChatGPT to alter the code so that after pressing the down arrow it should copy the existing text to the clipboard and compare that to the previous counter value to see if it had already been replaced and is therefore already at the bottom.

Great suggestion!
I just quoted you on ChatGPT and with its first adaptation it works even better now:

Code:
counter := 10001

; Define the hotkey that will start the renaming process
F1::
    ; Reset the counter to the starting value
    counter := 10001
 
    ; Send the first arrow down key to select the first file in the save as menu
    Send {Down}
 
    ; Loop through the files in the save as menu
    Loop
    {
        ; Send a Ctrl+C to copy the current file name to the clipboard
        Send ^c
        Sleep 100
 
        ; Get the file name from the clipboard
        fileName := Clipboard
 
        ; Send the counter value as the new file name
        SendInput {Raw}%counter%
 
        ; Send another arrow down key to select the next file in the save as menu
        Send {Down}
 
        ; Increment the counter
        counter++
 
        ; Wait a short time to ensure that the new file name is registered
        Sleep 100
 
        ; Check if the escape key has been pressed
        if GetKeyState("Escape", "P")
        {
            ; If the escape key has been pressed, reset the counter to the starting value and exit the loop
            counter := 10001
            break
        }
 
        ; Check if the current file name is the same as the previous file name
        if (fileName = lastFileName)
        {
            ; If the file names are the same, we have reached the end of the list
            break
        }
 
        ; Store the current file name as the last file name
        lastFileName := fileName
    }
return

I only had to manually increase the sleep time from 50 to 100, otherwise the script would randomly stop working after a while.
And in the last line its reaction is a bit delayed, so it still adds 3 numbers instead of only one, but then it stops by itself.
 
Last edited:

NateLiquidGravity

Alibre Super User
Put the check right after getting the value to prevent it from putting in another.
Code:
counter := 10001

; Define the hotkey that will start the renaming process
F1::
    ; Reset the counter to the starting value
    counter := 10001
 
    ; Send the first arrow down key to select the first file in the save as menu
    Send {Down}
 
    ; Loop through the files in the save as menu
    Loop
    {
        ; Send a Ctrl+C to copy the current file name to the clipboard
        Send ^c
        Sleep 100
 
        ; Get the file name from the clipboard
        fileName := Clipboard
 
        ; Check if the current file name is the same as the previous file name
        if (fileName = lastFileName)
        {
            ; If the file names are the same, we have reached the end of the list
            break
        }

        ; Send the counter value as the new file name
        SendInput {Raw}%counter%
 
        ; Send another arrow down key to select the next file in the save as menu
        Send {Down}
 
        ; Increment the counter
        counter++
 
        ; Wait a short time to ensure that the new file name is registered
        Sleep 100
 
        ; Check if the escape key has been pressed
        if GetKeyState("Escape", "P")
        {
            ; If the escape key has been pressed, reset the counter to the starting value and exit the loop
            counter := 10001
            break
        }
 
        ; Store the current file name as the last file name
        lastFileName := fileName
    }
return
 

NateLiquidGravity

Alibre Super User
Ok here is one I tested and got working right on my pc.
Code:
; Define the hotkey that will start the renaming process
F1::
    ; Reset the counter to the starting value
    counter := 10001
    lastFileName := 0
 
    ; Loop through the files in the save as menu
    Loop
    {
        ; Send a Ctrl+C to copy the current file name to the clipboard
        Send ^c
        Sleep 100
 
        ; Get the file name from the clipboard
        fileName := Clipboard
 
        ; Check if the current file name is the same as the previous file name
        if (fileName = lastFileName)
        {
            ; If the file names are the same, we have reached the end of the list
            break
        }

        ; Send the counter value as the new file name
        SendInput {Raw}%counter%
 
        ; Send another arrow down key to select the next file in the save as menu
        Send {Down}
 
        ; Increment the counter
        counter++
 
        ; Wait a short time to ensure that the new file name is registered
        Sleep 100
 
        ; Check if the escape key has been pressed (held down)
        if GetKeyState("Escape", "P")
        {
            ; If the escape key has been pressed (held down),exit the loop
            break
        }
 
        ; Store the current file name as the last file name
        lastFileName := fileName
    }
return
 
Top