Paid gig - Fusion API Python Script

Paid gig - Fusion API Python Script

info83PHN
Advocate Advocate
704 Views
4 Replies
Message 1 of 5

Paid gig - Fusion API Python Script

info83PHN
Advocate
Advocate

Hi all

I really don't know how to get started, so looking for someone to make a script which I can later expand on to.

 

Script needs :

Input Dialog with 7 line text area, OK, Cancel.
On 'Cancel', close dialog, do nothing.

On 'OK', close dialog and then ...

Convert the 7 lines of text from the text input area to Uppercase.

Split the 7 lines of text in to variables ( userVarName, userVar1, userVar2, userVar3 .... , userVar6 ) and trim off and blank spaces either end of each variable.

'Save As' the opened design as 'userVarName'.

Change view to 'Home' ( to center the model )

Change view to 'Front'

Edit 'Sketch1'

Sketch1 contains 6 text items. Change the text in the text items to userVar1 ... userVar6

If the userVar = the text "SMALLER" then set the text size to 6.75mm

If the userVar = the text "LARGE" then set the text size to 9mm

 

Note - the above 6 text items in the sketch could be moved to different sketches is it is easier to process.

 

Leave the Sketch open in Edit mode and end the script.

 

 

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

cam.tech
Participant
Participant
Accepted solution

Hello

I made a script which does the job more similar to what you want. I am happy to help you out. Reach out to me via email.

savetodoc.gif

 

We provide post processors support for PowerMill, FeatureCAM and Fusion 360. Reach out to us via techcamindia@gmail.com for trail post processor.
Message 3 of 5

kandennti
Mentor
Mentor
Accepted solution

Hi @info83PHN .

 

There were multiple parts I didn't understand, but I tried to create a script with my interpretation.

 

# Fusion360API Python script

import traceback
import adsk.fusion
import adsk.core

def run(context):
    ui: adsk.core.UserInterface = None
    try:
        app: adsk.core.Application = adsk.core.Application.get()
        ui = app.userInterface
        des: adsk.fusion.Design = app.activeProduct
        root: adsk.fusion.Component = des.rootComponent

        # input values
        msg1: str = 'Please enter the following values.\n'
        msg2: str = 'userVarName,userVar1,userVar2,userVar3,userVar4,userVar5,userVar6'
        resValues, cancelled = ui.inputBox(msg1 + msg2, '', msg2)
        if cancelled:
            return

        # split
        values = resValues.split(',')
        if len(values) != 7:
            ui.messageBox('The characters you entered are incorrect.')
            return
        
        # convert & trim
        values = [s.strip().upper() for s in values]

        # saveAs - activeProject.rootFolder
        rootFolder: adsk.core.DataFolder = app.data.activeProject.rootFolder
        doc: adsk.fusion.FusionDocument = app.activeDocument
        doc.saveAs(values[0], rootFolder, '', '')

        # view
        vp: adsk.core.Viewport = app.activeViewport
        vp.fit()
        cam: adsk.core.Camera = vp.camera
        cam.viewOrientation = adsk.core.ViewOrientations.FrontViewOrientation
        vp.camera = cam
        vp.refresh()

        # get sketch1 - from root component
        skt1: adsk.fusion.Sketch = root.sketches.itemByName('sketch1')
        if not skt1:
            ui.messageBox('"sketch1" is not found.')
            return

        if skt1.sketchTexts.count < 6:
            ui.messageBox('"sketch1" has less than 6 texts.')
            return

        # Rewriting sketch text
        # The combination of the text in sketch1 and the input text is unknown.
        # For now, I'll assume the order in which they were found.
        unitMgr: adsk.core.UnitsManager = des.unitsManager
        txt: adsk.fusion.SketchText
        for txt, value in zip(skt1.sketchTexts, values[1:]):
            txt.text = value
            if value == 'SMALLER':
                txt.height = unitMgr.convert(6.75, 'mm', unitMgr.internalUnits)
            if value == 'LARGE':
                txt.height = unitMgr.convert(9., 'mm', unitMgr.internalUnits)

        # Edit mode for sketches
        sels: adsk.core.Selections = ui.activeSelections
        sels.clear()
        sels.add(skt1)
        app.executeTextCommand(u'Commands.Start SketchActivate')

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

 

I can't visualize what you are thinking, just in words.

0 Likes
Message 4 of 5

info83PHN
Advocate
Advocate

Thank You Makoto.

At first glance it looks like your script will almost do what I need. As I am a novice with python, I will need some time to work through this and try and understand the code ( last coded in VB5 about 15 years ago ).

Thank You

0 Likes
Message 5 of 5

info83PHN
Advocate
Advocate

Thank You kandennti

 

Your code certainly put me on the right track and I was able to reach my goal from there.

0 Likes