Rename NCProgram item

Rename NCProgram item

dames123
Advocate Advocate
549 Views
4 Replies
Message 1 of 5

Rename NCProgram item

dames123
Advocate
Advocate

Hello all,

 

In the following code, I can create and duplicate NCPrograms but I'm having trouble renaming the duplicate. It looks like the if statement doesn't do anything. No errors, just skips it. Any help would be appreciated.

 

#n360API Python script
import adsk.core, adsk.fusion, adsk.cam, traceback
import traceback
import adsk.core as core
import adsk.fusion as fusion
import adsk.cam as cam

def run(context):
    ui: core.UserInterface = None
    try:
        app: core.Application = core.Application.get()
        ui = app.userInterface

        camObj: cam.cam = app.activeProduct

        # https://url.us.m.mimecastprotect.com/s/R9VIC2kr1Nf6PQk3I9ku4o?domain=help.autodesk.com
        ncPrograms: cam.NCPrograms = camObj.ncPrograms
        for setup in camObj.setups:

            machineName = ""
            if setup.machine:
                # Access the name of the machine
                machineName = setup.machine.getName() if hasattr(setup.machine, 'getName') else "Unnamed Machine"

            # Log or display the machine name
            ui.messageBox(f"Setup: {setup.name}\nMachine: {machineName}")
            
            ncIpt: cam.NCProgramInput = ncPrograms.createInput()
            ncIpt.displayName = setup.name
            ncParameters = ncIpt.parameters
            ncParameters.itemByName('nc_program_filename').value.value = setup.name
            ncParameters.itemByName('nc_program_openInEditor').value.value = False
            ncParameters.itemByName('nc_program_useMachineConfig').value.value = True
            ncParameters.itemByName('nc_program_filename').value.value = setup.parameters.itemByName('job_programName').value.value # Set the filename to the same as the setup
            ncParameters.itemByName('nc_program_comment').value.value = setup.parameters.itemByName('job_programComment').value.value

            ncIpt.operations = [setup]

            prg = ncPrograms.add(ncIpt)
            prg.machine = setup.machine  # Set the machine to the same as the setup
            
            

            # Duplicate the NCProgram
            duplicatedPrg = prg.duplicate()
            if isinstance(duplicatedPrg, cam.NCProgram):
                new_name = "SETUPSHEET"
                duplicatedPrg.name = new_name
            else:
             ui.messageBox(f"Failed to duplicate NCProgram for setup: {setup.name}")
            

        ui.messageBox("Dont forget to set your tool numbers")

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

0 Likes
Accepted solutions (1)
550 Views
4 Replies
Replies (4)
Message 2 of 5

Jorge_Jaramillo
Collaborator
Collaborator
Accepted solution

Hi,

 

You are expecting a NCprogram from prg.duplicate() on line #45, but it returns a boolean instead:

Jorge_Jaramillo_0-1723771875670.png

 

That is why the returned value is never an instance of NCprogram as you want to check on line #46 (that if will always evaluate to false).

You might have to look for it in the Programs collection and then rename it.

 

Replace that if-block with the following:

            if duplicatedPrg:
                camObj.ncPrograms[-1].name = "SETUPSHEET"
            else:
                app.log(f"Failed to duplicate NCProgram for setup: {setup.name=}")

 

Regards,

Jorge Jaramillo

 

Message 3 of 5

dames123
Advocate
Advocate
Perfect! thanks for the explanation and the help.
0 Likes
Message 4 of 5

nubrandao
Collaborator
Collaborator

can you show the final script please?

0 Likes
Message 5 of 5

dames123
Advocate
Advocate
0 Likes