Compare and Edit after running script

Compare and Edit after running script

dames123
Advocate Advocate
2,355 Views
24 Replies
Message 1 of 25

Compare and Edit after running script

dames123
Advocate
Advocate

I have a script I run that creates NCPrograms. It's crude as I'm not a coder. There's one line that creates an error when using Compare and Edit after creating the NCPrograms. It does something to the flag to set the machine to 1.

 

prg.machine = setup.machine  # Set the machine to the same as the setup
 
I get the following error message in Compare and edit.
 
dames123_0-1724186371917.png

Is this a bug in Fusion or is there another way to write that line in the script?

 

 

#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 duplicatedPrg:
                
                camObj.ncPrograms[-1].name = (setup.name + " SETUPSHEET")
                #ncParameters = duplicatedPrg.parameters
                
                # Set the post processor path only for the duplicated program
                post_processor_path = 'linked://' + 'setup-sheet-excel-2007.cps'
                ncParameters.itemByName('nc_program_post').value.value = post_processor_path
                
                
            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()))

 

 

Any help is appreciated.

 

 

0 Likes
Accepted solutions (3)
2,356 Views
24 Replies
Replies (24)
Message 2 of 25

jeff.pek
Community Manager
Community Manager

Are you able to share your model, after having run the script? It doesn't seem like the script is setting the value to 'machine', but I can't tell where that got set or which parameter(s) it's on.

 

Thanks,

  Jeff

0 Likes
Message 3 of 25

dames123
Advocate
Advocate
Hi Jeff,

https://a360.co/3Xar3fR

Thanks for the help.
0 Likes
Message 4 of 25

dames123
Advocate
Advocate
One thing to note is if I open each NCProgram dialog individually, without changing anything, then close and try compare and edit again, the issue is gone. That's what led me to believe it's a bug.
0 Likes
Message 5 of 25

Jorge_Jaramillo
Collaborator
Collaborator

Hi,

 

In your script you must condition set the machine in the NCprogram only if the machine in the setup is set to something different to None, like so:

    if setup.machine:
        prg.machine = setup.machine

 

Now, in the compare and edit option you won't be able to edit the machine.

In the test I made, just after running the script all machine name are the same, and they identified by an internal number, like in the following image:

Jorge_Jaramillo_0-1724195656944.png

 

If I try to edit any of those ID I get the error message you get.

The correct way to change the machine of a NC program is from the edit option of it, either changing the machine of disabling the "Use machine configuration" to remove the machine from the NC program.

Then, when I get back to compare and edit I get the label "<not editable>" in the "Edit all" column for the machine parameter line, like in the following image:

Jorge_Jaramillo_1-1724195876142.png

 

So, my conclusion is that you can set the machine from the script if it is different that none in the setup's machine, or with the NC program edit form to choose from a machine from the machines drop down list or to remove it if it was already set up.

 

Regards,

Jorge Jaramillo

 

Message 6 of 25

dames123
Advocate
Advocate
Hi Jorge,

Thanks for the help. Correct me if I'm misunderstanding but I'm not trying to edit the machine in compare and edit. I'm trying to resolve the error because it is preventing me from editing anything else in compare and edit such as the file path and post. I tried to get the post to populate with the script but it's not working.

Thanks.
0 Likes
Message 7 of 25

Jorge_Jaramillo
Collaborator
Collaborator
Hi,

In my tests, with and without machine on the setup configuration, I get the NC programs without any error.
What do you have setup's machine?
Can you share a screencast of both setup and NC program where the machine field is visible?
Or even better, if you could share your design to make tests over it.

Regards,
Jorge Jaramillo
0 Likes
Message 8 of 25

dames123
Advocate
Advocate
Hi Jorge,
The link to the project is a few posts up. I get the NCPrograms without error also, but I need to change the file path of all the NCPrograms and the post in the duplicated NCPrograms (after the NCPrograms are created). That's why I need to use compare and edit. The error is in compare and edit caused by how I'm setting the machine in the script. With that error, compare and edit won't let me edit anything else in the dialog without getting the error shown in the first post in this thread.

This is all to help with the terrible workflow of making setup sheets. Create NCProgram for machine, get post for machine, duplicate NCProgram for setup sheet, get post for setup sheet, etc. It's a terrible workflow but the HTML setup sheets just don't work for me. We have specific needs for documentation. In PowerMILL (our current CAM software) this is all done with one click. I'm trying to replicate this in Fusion.
0 Likes
Message 9 of 25

Jorge_Jaramillo
Collaborator
Collaborator

Hi,

 

You can leave empty the machine setting in the NC programs by:

  • not assigning anything to prg.machine
  • setting nc_program_useMachineConfig parameter to False

And then when you edit and compare the NC programs not errors are displayed:

Jorge_Jaramillo_0-1724272687435.png

 

I'm not sure if the machine setting is needed in order to set the post-processor in the NC program; unless, I believe, there should be something in the post-processor that requires the machine setting.

 

Regards,

Jorge Jaramillo

 

0 Likes
Message 10 of 25

dames123
Advocate
Advocate
Hi Jorge,

Unfortunately, the machine does need to be assigned, or the post does not populate in the NCProgram dialog.

Maybe @jeff.pek can come up with a fix.
0 Likes
Message 11 of 25

Jorge_Jaramillo
Collaborator
Collaborator
Accepted solution

Hi,

 

I was able to set the post processor to a NC program without setting the machine.

It works as follow:

  • Look for the post processor locally
  • If not found, look for it in the Fusion's post processor library and copy it locally if found
  • Then, assign it to the NC program

 

Here is the code which I tested:

 

def create_and_duplicate_ncprogram(
    app: adsk.core.Application, 
) -> None:

    try:
        camObj: adsk.cam.CAM = app.activeProduct

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

            ncIpt: adsk.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 = False
            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)
            app.log(f' {setup.name=} :: {setup.machine=} {prg.machine=}')
            
            # Duplicate the NCProgram
            duplicatedPrg = prg.duplicate()
            if duplicatedPrg:
                camObj.ncPrograms[-1].name = f"{setup.name} SETUPSHEET"
                postConf = import_post_processor()
                if postConf:
                    prg.postConfiguration = postConf
            else:
                app.log(f"Failed to duplicate NCProgram for setup: {setup.name=}")
        app.log("Dont forget to set your tool numbers")

        for prg in ncPrograms:
            app.log(f"{prg.name} : nc_program_post = {prg.parameters.itemByName('nc_program_post').value.value}")

    except:
        app.log('Failed:\n{}'.format(traceback.format_exc()))


def import_post_processor() -> adsk.cam.PostConfiguration | None:

    POST_PROCESSOR_DESCR = 'Setup Sheet Excel 2007'
    POST_PROCESSOR_FILENAME = 'NCProgramSamplePost.cps'
    POST_PROCESSOR_URL = 'user://'

    camMngr: adsk.cam.CAMManager = adsk.cam.CAMManager.get()
    camLibMngr: adsk.cam.CAMLibraryManager = camMngr.libraryManager
    postLibrary = camLibMngr.postLibrary

    importedURL: adsk.core.URL = None
    local_url = adsk.core.URL.create(POST_PROCESSOR_URL)

    # 1. Look for post processor locally
    for url in postLibrary.childAssetURLs(local_url):
        if url.leafName == POST_PROCESSOR_FILENAME:
            importedURL = url

    if not importedURL:
        # 2. Look for post processor in Fusion's PostProcessors Library
        postQuery = postLibrary.createQuery(adsk.cam.LibraryLocations.Fusion360LibraryLocation)
        postConfigs = postQuery.execute()
        for config in postConfigs:
            if config.description == POST_PROCESSOR_DESCR:
                # 3. If found it, copy it locally
                importedURL = postLibrary.importPostConfiguration(config, local_url, POST_PROCESSOR_FILENAME)

    if importedURL:
        postConf = postLibrary.postConfigurationAtURL(importedURL)
        return(postConf)
    else:
        return(None)

 

 

You need to set this two variables according to your needs:

POST_PROCESSOR_DESCR : this is the description of the post processor in Fusion's post's library
POST_PROCESSOR_FILENAME : this is the filename as it is copied locally

 

Once I run it, the comparte and edit can be saved without any issue:

Jorge_Jaramillo_0-1724340182625.png

 

 

I hope this can help.

 

Regards,

Jorge Jaramillo

 

 

Message 12 of 25

dames123
Advocate
Advocate

Hi Jorge,

 

I tried running it and it does nothing. No error message just doesn't run. The only thing I changed was the post filename and I added adsk.core and traceback at the top because it said it couldn't find these.

 

import adsk.core, traceback


def create_and_duplicate_ncprogram(
    app: adsk.core.Application, 
) -> None:

    try:
        camObj: adsk.cam.CAM = app.activeProduct

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

            ncIpt: adsk.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 = False
            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)
            app.log(f' {setup.name=} :: {setup.machine=} {prg.machine=}')
            
            # Duplicate the NCProgram
            duplicatedPrg = prg.duplicate()
            if duplicatedPrg:
                camObj.ncPrograms[-1].name = f"{setup.name} SETUPSHEET"
                postConf = import_post_processor()
                if postConf:
                    prg.postConfiguration = postConf
            else:
                app.log(f"Failed to duplicate NCProgram for setup: {setup.name=}")
        app.log("Dont forget to set your tool numbers")

        for prg in ncPrograms:
            app.log(f"{prg.name} : nc_program_post = {prg.parameters.itemByName('nc_program_post').value.value}")

    except:
        app.log('Failed:\n{}'.format(traceback.format_exc()))


def import_post_processor() -> adsk.cam.PostConfiguration | None:

    POST_PROCESSOR_DESCR = 'Setup Sheet Excel 2007'
    POST_PROCESSOR_FILENAME = 'setup-sheet-excel-2007.cps'
    POST_PROCESSOR_URL = 'user://'

    camMngr: adsk.cam.CAMManager = adsk.cam.CAMManager.get()
    camLibMngr: adsk.cam.CAMLibraryManager = camMngr.libraryManager
    postLibrary = camLibMngr.postLibrary

    importedURL: adsk.core.URL = None
    local_url = adsk.core.URL.create(POST_PROCESSOR_URL)

    # 1. Look for post processor locally
    for url in postLibrary.childAssetURLs(local_url):
        if url.leafName == POST_PROCESSOR_FILENAME:
            importedURL = url

    if not importedURL:
        # 2. Look for post processor in Fusion's PostProcessors Library
        postQuery = postLibrary.createQuery(adsk.cam.LibraryLocations.Fusion360LibraryLocation)
        postConfigs = postQuery.execute()
        for config in postConfigs:
            if config.description == POST_PROCESSOR_DESCR:
                # 3. If found it, copy it locally
                importedURL = postLibrary.importPostConfiguration(config, local_url, POST_PROCESSOR_FILENAME)

    if importedURL:
        postConf = postLibrary.postConfigurationAtURL(importedURL)
        return(postConf)
    else:
        return(None)

 I have the post in my local folder.

 

dames123_0-1724342422011.png

 

Thanks.

 

0 Likes
Message 13 of 25

Jorge_Jaramillo
Collaborator
Collaborator
Accepted solution

Hi,

 

You have to call the function from your run() entry function like so:

 

import adsk, adsk.core, adsk.fusion, adsk.cam, traceback

def run(context):
    app = adsk.core.Application.get()
    try:
        create_and_duplicate_ncprogram(app)
    except:
        app.log('Failed:\n{}'.format(traceback.format_exc()))
    adsk.terminate()

 



Regards,
Jorge Jaramillo

Message 14 of 25

dames123
Advocate
Advocate

Got it! 

 

But... it puts the setup sheet post in the wrong NCProgram. It should go in the duplicates (named setup sheet) but it goes in the original.

 

dames123_0-1724357169288.png

 

0 Likes
Message 15 of 25

Jorge_Jaramillo
Collaborator
Collaborator

Hi,

 

Ahh, I left the way you had it in the first version of the script.

But it is a very easy change.

Just replace this line:

 

    prg.postConfiguration = postConf

 

with this one:

 

    camObj.ncPrograms[-1].postConfiguration = postConf

 

 

Regards,

Jorge Jaramillo

 

Message 16 of 25

dames123
Advocate
Advocate

Hi Jorge,

 

The script works great. Thank you for your time and help on this.

 

Unfortunately, I'm back where I started as far as the error in compare and edit. If I don't include the following lines

 

Set use machine to yes

ncParameters.itemByName('nc_program_useMachineConfig').value.value = True

 And load machine file

 

prg.machine = setup.machine  # Set the machine to the same as the setup

 

I don't get the post in the original NCProgram from the machine in the setup. If I do include these lines, I get the compare and edit error.

 

But...I figured out what the error is. Compare and edit wants the ID number of the machine file as a string (I'm guessing) as it wants to see '1' as opposed to 1. Typing in '1' in the compare and edit dialog clears the error. Is there a way to add the '' in the line prg.machine = setup.machine? If not I'm still 95% there with what I'm trying to accomplish and I thank you again for your help.

 

Thanks

David

0 Likes
Message 17 of 25

Jorge_Jaramillo
Collaborator
Collaborator
Accepted solution

Hi,

 

If you need to set the post-processor to the original NCprogram, here is an update of the function:

def import_post_processor(post_processor_descr: str, post_processor_filename: str) -> adsk.cam.PostConfiguration | None:

    POST_PROCESSOR_URL = 'user://'

    camMngr: adsk.cam.CAMManager = adsk.cam.CAMManager.get()
    camLibMngr: adsk.cam.CAMLibraryManager = camMngr.libraryManager
    postLibrary = camLibMngr.postLibrary

    importedURL: adsk.core.URL = None
    local_url = adsk.core.URL.create(POST_PROCESSOR_URL)

    # 1. Look for post processor locally
    for url in postLibrary.childAssetURLs(local_url):
        if url.leafName == post_processor_filename:
            importedURL = url

    if not importedURL:
        # 2. Look for post processor in Fusion's PostProcessors Library
        postQuery = postLibrary.createQuery(adsk.cam.LibraryLocations.Fusion360LibraryLocation)
        postConfigs = postQuery.execute()
        for config in postConfigs:
            if config.description == post_processor_descr:
                # 3. If found it, copy it locally
                importedURL = postLibrary.importPostConfiguration(config, local_url, post_processor_filename)

    if importedURL:
        postConf = postLibrary.postConfigurationAtURL(importedURL)
        return(postConf)
    else:
        return(None)

In this version of the import_post_processor() function, the first argument is the post processor description as it is in the post library; and the second argument is the name of the file as it is copied locally.

 

Then you need to update how it is called in you main routine:

(...)

            prg = ncPrograms.add(ncIpt)
            # app.log(f' {setup.name=} :: {setup.machine=} {prg.machine=}')
            postConf = import_post_processor('Dumper', 'dump.cps') # replace description and filename with
            if postConf:
                prg.postConfiguration = postConf
            
            # Duplicate the NCProgram
            duplicatedPrg = prg.duplicate()
            if duplicatedPrg:
                camObj.ncPrograms[-1].name = f"{setup.name} SETUPSHEET"
                postConf = import_post_processor('Setup Sheet Excel 2007', 'setup-sheet-excel-2007.cps')
                if postConf:
                    camObj.ncPrograms[-1].postConfiguration = postConf
            else:
                app.log(f"Failed to duplicate NCProgram for setup: {setup.name=}")

(...)

In this case (line #5) I'm setting 'dump.cps' to the original NC program and 'setup-sheet-excel-2007.cps' (line #13) to the copied NC program.  You can set the ones you want on each.

 

Regards,

Jorge Jaramillo

 

Message 18 of 25

dames123
Advocate
Advocate
Thanks Jorge!!
0 Likes
Message 19 of 25

nubrandao
Collaborator
Collaborator

hi, can any one share the final version? im trying to make it work, but you guys posted many scripts versions and now the final doesnt work, please

0 Likes
Message 20 of 25

Jorge_Jaramillo
Collaborator
Collaborator

Hi,

 

This is the full source code of the script:

import adsk, adsk.core, adsk.fusion, adsk.cam, traceback

app = adsk.core.Application.get()

def create_and_duplicate_ncprogram(
    app: adsk.core.Application, 
) -> None:

    try:
        camObj: adsk.cam.CAM = app.activeProduct

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

            ncIpt: adsk.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 = False
            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)
            # app.log(f' {setup.name=} :: {setup.machine=} {prg.machine=}')
            postConf = import_post_processor('Dumper', 'dump.cps') # replace description and filename with the ones you need
            if postConf:
                prg.postConfiguration = postConf
            
            # Duplicate the NCProgram
            duplicatedPrg = prg.duplicate()
            if duplicatedPrg:
                camObj.ncPrograms[-1].name = f"{setup.name} SETUPSHEET"
                postConf = import_post_processor('Setup Sheet Excel 2007', 'setup-sheet-excel-2007.cps')
                if postConf:
                    camObj.ncPrograms[-1].postConfiguration = postConf
            else:
                app.log(f"Failed to duplicate NCProgram for setup: {setup.name=}")
        app.log("Dont forget to set your tool numbers")

        for prg in ncPrograms:
            app.log(f"{prg.name} : nc_program_post = {prg.parameters.itemByName('nc_program_post').value.value}")

    except:
        app.log('Failed:\n{}'.format(traceback.format_exc()))

def import_post_processor(post_processor_descr: str, post_processor_filename: str) -> adsk.cam.PostConfiguration | None:

    POST_PROCESSOR_URL = 'user://'

    camMngr: adsk.cam.CAMManager = adsk.cam.CAMManager.get()
    camLibMngr: adsk.cam.CAMLibraryManager = camMngr.libraryManager
    postLibrary = camLibMngr.postLibrary

    importedURL: adsk.core.URL = None
    local_url = adsk.core.URL.create(POST_PROCESSOR_URL)

    # 1. Look for post processor locally
    for url in postLibrary.childAssetURLs(local_url):
        if url.leafName == post_processor_filename:
            importedURL = url

    if not importedURL:
        # 2. Look for post processor in Fusion's PostProcessors Library
        postQuery = postLibrary.createQuery(adsk.cam.LibraryLocations.Fusion360LibraryLocation)
        postConfigs = postQuery.execute()
        for config in postConfigs:
            if config.description == post_processor_descr:
                # 3. If found it, copy it locally
                importedURL = postLibrary.importPostConfiguration(config, local_url, post_processor_filename)

    if importedURL:
        postConf = postLibrary.postConfigurationAtURL(importedURL)
        return(postConf)
    else:
        return(None)

def run(context) -> None:
    try:
        create_and_duplicate_ncprogram(app)
    except:
        app.log('Failed:\n{}'.format(traceback.format_exc()))
    adsk.terminate()

 

Regards,

Jorge Jaramillo

 

0 Likes