save / post to windows file path script

save / post to windows file path script

Anonymous
Not applicable
355 Views
1 Reply
Message 1 of 2

save / post to windows file path script

Anonymous
Not applicable

Hard to come up with a good title for this one.  

My background in programming is limited. I know enough to read through programs and somewhat understand what they are doing. I program PLC's and Robots using script languages regularly, as well as other automation type stuff. 

 

At work we run into an issue where we are not that organized. The companies server is a free for all, and with multiple people programming for the machine shop I wrote a SOP for our companies filing methods so the machinists can find the files and setup sheets they need to run jobs.  

 

What I would like to do, is write a python script that would post the setup, generate the setup sheet, and follow my SOP for file organization.


Something like this. 

When the part is programmed and finished, run the script on setup 1 and have it look at our server (Z:Customer Files)

Say the part number is AB1234.9876   Our customer number is AB1234 and the part number is 9876 in that case. 

I would like the script to

look for Z:/Customer Files/AB1234/CNC Programs/AB1234.9876

  If found true

  look for Z:/Customer Files/AB1234/CNC Programs/AB1234.9876/Op1

    If found true

    Post NC file and Setup sheet in folder

      Else

      Create Op1 Folder and Post NC file and Setup sheet in folder

  Else

  Create AB1234.9876 folder

  Create Op1 Folder and Post NC file and Setup sheet in folder

   

 

I would like my file names in fusion to drive creating the correct folders and sub folders on the server so that the employees are not left creating them on their own.  The problem we run into is everyone has their own twist, or try to cut a corner to make their job easier, and at the end of the day, the machinist cannot find the file they are looking for, or end up with multiple versions saved in different locations.  We have 3 separate programmers all saving programs like its the wild west and once every month or two I have to take a day or two to completely inventory and fix our server file structure.  

     

Like I said, I have an understanding of how the code works, but I do not have enough experience to tie it all together.  If anyone could even give me a small chunk of code to point me in the right direction so I could build off of it that would be greatly appreciated!

 

Ultimately, a custom button to run the script would be the ultimate goal. 

0 Likes
356 Views
1 Reply
Reply (1)
Message 2 of 2

boopathi.sivakumar
Autodesk
Autodesk

Hi @Anonymous 

Here is the sample script does exactly what you want 

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

#Folder Location
folder = r"D:/Customer Files/"
def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface
        doc = app.activeDocument   
        products = doc.products     
        product = products.itemByProductType('CAMProductType')
        cam = adsk.cam.CAM.cast(product)

        # specify the program name
        programName = doc.name
        #add Folder
        folders = str(programName).split('.')  #Split Program by .
        addFolder = folders[0]+'/CNC Programs/'+folders[1]+'/Op1/'

        opFolder = os.path.join(folder, addFolder)

        #create Folder if not found
        if not os.path.exists(opFolder):
            os.makedirs(opFolder)

        #Alternate code if you are using custom post
        # postProcessorFile = r'D:/Fusion Post/'
        # postConfig = os.path.join(postProcessorFile, 'haas.cps') 
    
        postConfig = os.path.join(cam.genericPostFolder, 'grbl.cps') 
        units = adsk.cam.PostOutputUnitOptions.DocumentUnitsOutput

        # create the postInput object
        postInput = adsk.cam.PostProcessInput.create(programName, postConfig, opFolder, units)
        postInput.isOpenInEditor = False
       
        setups = cam.setups
        #Post processor first setup
        setup = setups.item(0)    
        cam.postProcess(setup, postInput)
        
        #generate Setupsheet
        sheetFormat = adsk.cam.SetupSheetFormats.HTMLFormat
        cam.generateSetupSheet(setup, sheetFormat, opFolder, False)
        ui.messageBox('Done')

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

This just gives an idea how scripts works inside fusion, you can refer the cam api samples 

which gives you more insights on how to control the post properties etc..


Boopathi Sivakumar
Senior Technology Consultant

0 Likes