code review/share for automated change of parameters and stl-export

code review/share for automated change of parameters and stl-export

picadilly
Advocate Advocate
293 Views
2 Replies
Message 1 of 3

code review/share for automated change of parameters and stl-export

picadilly
Advocate
Advocate

I do an open-source-project called osVAC neo. For details see: osvacneo.de

 

It is similiar to plumbing:

you have a few basic parts, like bows and T-connectors, but you need them in lots of different sizes and combinations thereof.

So, I did the designs in f360 and to get the different sizes I change parameters.

 

To generate the stl-files I have to:

1) change the desired parameters

2) exports part(s) als stl (and step-files)

3) repeat the above for all desired combinations of parameters

 

Now, I have a first decent prototype up and running. I would like to share the code and would appreciate comments. I will also attach my f360-test-design.

 

Brief description, how to use:

There ist a section in the code, where you place your user parameters und values:

 

        ###################################################################################
        # change to your personal data here
        inputData1 = (
            ("height", (10, 20)),
            ("width", (30, 40)),
            ("length", (50,60))
        )
        inputData2 = (
            ("MaleDiameterInner", (25, 32, 36, 40, 50)),
            ("FemaleDiameterInner", (25, 32, 36, 40, 50))
        )
        inputData = inputData1
        destFolder = "D:\\tmp\\F360"
        # end of personal data
        ###################################################################################

 

 

The program will iterate and build every possible permutation of the give values (for inputData1 there are 8 permutations, for inputData2 there are 25) and generate a properly named stil-file:

 

picadilly_1-1737801076660.png

 

picadilly_2-1737801135806.png

 

 

 

 

 

 

0 Likes
294 Views
2 Replies
Replies (2)
Message 2 of 3

picadilly
Advocate
Advocate

And here comes the code:

 

#Author-
#Description-

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


###########################################################################################
def get_immediate_children(component):
    # Get the child occurrences (components) directly under the specified component
    child_occurrences = component.occurrences

    # Extract the actual component objects from the occurrences
    child_components = [occ.component for occ in child_occurrences if occ.isVisible]

    return child_components


###########################################################################################
def _generateIteratedDataSet( inputDataSets, oneDataLine, iteratedData ):
    index = len(oneDataLine)

    if( index < len(inputDataSets) ):
        inputDataSet = inputDataSets[index][1]

        for data in inputDataSet:
            oneDataLine.append( data )
            _generateIteratedDataSet( inputDataSets, oneDataLine, iteratedData )
            oneDataLine.pop(index)
    else:
        tmp = oneDataLine.copy()
        iteratedData.append(tmp)


###########################################################################################
def generateIteratedDataSet( inputDataSets ):
    names = []

    for inputDataSet in inputDataSets:
        names.append( inputDataSet[0] )

    interatedData=[]
    _generateIteratedDataSet( inputDataSets, [], interatedData)
    
    return( (names, interatedData) )


###########################################################################################
def verifyUserParameters(app, design, names):
    ui = app.userInterface
    userParameters = design.userParameters

    for name in names:
        userParameter = userParameters.itemByName(name)

        if userParameter:
            dummy = 1
        else:
            ui.messageBox('User-Parameter "{}" nicht gefunden.'.format(parameterName), 'Fehler')
            return


###########################################################################################
def setUserParameters(app, design, names, data):
    ui = app.userInterface
    userParameters = design.userParameters

    length = len(names)

    for index in range(0, length, 1):
        name = names[index]
        val = data[index]
        userParameter = userParameters.itemByName(name)
        userParameter.expression = str(val)
 
    # update parametric text plugin
    app.fireCustomEvent('thomasa88_ParametricText_Ext_Update')            
    adsk.doEvents()
    adsk.doEvents()
    dummy = 1


###########################################################################################
def generateFileName( componentName, names, data ):
    length = len(names)
    text = ""

    for index in range(0, length, 1):
        name = names[index]
        val = data[index]

        if( len(text) != 0 ):
            text = text + "-"

        text = text + name + "=" + str(val)

    text = componentName + "-" + text
    
    dummy = 1
    return( text )


###########################################################################################
def exportStl( exportMgr, component, destFolder, fnBody ):
    fn = os.path.join( destFolder, fnBody + '.stl')
    
    stlOptions = exportMgr.createSTLExportOptions(component, fn)
    stlOptions.sendToPrintUtility = False # Setze auf True, wenn du die Exportdatei direkt drucken möchtest

    # Körper als STL exportieren
    exportMgr.execute(stlOptions)


###########################################################################################
def run(context):
    ui = None
    try:
        # Zugriff auf Fusion 360 UI
        app = adsk.core.Application.get()
        ui = app.userInterface

        # Dokument und Design erhalten
        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)

        if not design:
            ui.messageBox('Kein aktives Fusion 360 Design.', 'Fehler')
            return
        
        # Get the root component
        root_component = design.rootComponent
        
        # Get the immediate child components under the root component
        immediate_children = get_immediate_children(root_component)

        ###################################################################################
        # change to your personal data here
        inputData1 = (
            ("height", (10, 20)),
            ("width", (30, 40)),
            ("length", (50,60))
        )
        inputData2 = (
            ("MaleDiameterInner", (25, 32, 36, 40, 50)),
            ("FemaleDiameterInner", (25, 32, 36, 40, 50))
        )
        inputData = inputData1
        destFolder = "D:\\tmp\\F360"
        # end of personal data
        ###################################################################################


        iteratedDataSet = generateIteratedDataSet( inputData )
        names = iteratedDataSet[0]
        iteratedData = iteratedDataSet[1]

        verifyUserParameters( app, design, names )

        for data in iteratedData:
            setUserParameters( app, design, names, data )

            exportMgr = design.exportManager 

            for component in immediate_children: 
                fnBody = generateFileName( component.name, names, data )
                exportStl( exportMgr, component, destFolder, fnBody )
                dummy = 0

        dummy=1
        return

        # dead old code below
        # User-Parameter ändern
        parameterName = 'HoseDiameterInner'  # Name des zu ändernden Parameters
        parameterObj = ui.inputBox("Bitte geben Sie den Namen des Parameters ein:", "Parameter Name", "HoseDiameterInner")
        parameterName = parameterObj[0]

        # Überprüfen, ob der Benutzer einen Namen eingegeben hat
        if not parameterName:
            ui.messageBox("Es wurde kein Parametername eingegeben.")
            return

        for parameterValue in [25, 32, 36]:
            set_parameter(app, design, parameterName, parameterValue)
            
            # Export
            exportMgr = design.exportManager        
            for component in immediate_children: 
                export_stl(exportMgr, component, parameterValue)

        ui.messageBox('Export erfolgreich abgeschlossen.')

    except:
        if ui:
            ui.messageBox('Fehler aufgetreten:\n{}'.format(traceback.format_exc()))
0 Likes
Message 3 of 3

picadilly
Advocate
Advocate

nobody interested into this? I guessed, that some users out there would have similiar requirements and would be curious, how they are doing?

0 Likes