Python existing sketches to be combined, and saveAsDFX

Python existing sketches to be combined, and saveAsDFX

marylou.forsyth
Contributor Contributor
504 Views
2 Replies
Message 1 of 3

Python existing sketches to be combined, and saveAsDFX

marylou.forsyth
Contributor
Contributor

Hello to all you wonderful python coders!  I am needing assistance in merging (or projecting) existing sketches for a model, and then saving it to a DXF file in python NOT the UI.  At present, I am able to make a DFX file for each sketch, but am not able to find any python code that allows me to append/merge them into one sketch that can then be saved as a DXF.  

 

For each model, a variable amount of sketches are automatically created.  For one example when I expand the UI "Sketches" I see three subcomponents (sketches) named ID-OD, Notches and Center-Hole.  If I use the following code I can make a dxf file for each... ID-OD, Notches and Center-Hole:

 

des: adsk.fusion.Design = app.activeProduct
root: adsk.fusion.Component = des.rootComponent
skt = root.sketches.itemByName('ID-OD')
skt2 = root.sketches.itemByName('Notches')
skt3 = root.sketches.itemByName('Center-Hole')
skt.saveAsDXF(fullPath + '.dxf') and so on.
 
First question is :  Each model will have different sketch names (ie. ID-OD) how to I query the model/sketches to get a list of the sketch names so I can use it in a variable like... skt = root.sketches.itemByName(first_sketch_name)
 
Second question is : How do I combine/merge/append skt, skt2 and skt3 together ?  There have been other posts that say make a new sketch and project the sketches into it, but they are all talking UI.
 
Thank you in advance for your time and knowledge!
0 Likes
Accepted solutions (2)
505 Views
2 Replies
Replies (2)
Message 2 of 3

Rushikesh.kadam
Autodesk
Autodesk
Accepted solution

@marylou.forsyth to get the list of sketch names you can loop through the sketches object. Below is a sample code. I have assumed that all the sketches are under the root component.

 

import adsk.core, adsk.fusion, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        design: adsk.fusion.Design = app.activeProduct
        rootComp = design.rootComponent
        sketches = rootComp.sketches
        sketch_names = []
        for skecth in sketches:
            sketch_names.append(skecth.name)
        app.log(str(sketch_names))

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

 

 

For the second question, you can use the API's sketch.project method to protect all the sketches into a single sketch. 

 

I would also suggest using the Fusion 360 API and Scripts forum for Fusion API posts to reach wider Fusion API users. 

------------------------------------------------------------------------------------------------------------------------------

If my reply was helpful, please click the "Accept as Solution" button. It helps others get the answer quickly! A "Like" is always welcomed.




Rushikesh Kadam
Senior QA Engineer
Quality Assurance
Autodesk, Inc.


Message 3 of 3

marylou.forsyth
Contributor
Contributor
Accepted solution

Rushikesh Kadam, Thank you SO very much for not only your prompt and correct answer !!  You got me pointed in the right direction, bless you!  Here is the code now working, just in case anyone is looking as hard as I did for a good solution....(model already loaded and is the active component)

des: adsk.fusion.Design = app.activeProduct
root: adsk.fusion.Component = des.rootComponent
master: adsk.fusion.Sketch = root.sketches.add(root.xYConstructionPlane)

rootComp = design.rootComponent

targetBody: adsk.fusion.BRepBody = root.bRepBodies[0]
master.project(targetBody)

if (master.project(targetBody)):
folderdlg = ui.createFolderDialog()
folderdlg.title = 'Please select a FOLDER to save the sketch dxf file to:'
res = folderdlg.showDialog()
if res == adsk.core.DialogResults.DialogOK:
folder = folderdlg.folder

# write the merged file
fullPath = os.path.join(folder, order)
master.saveAsDXF(fullPath + '.dxf')
else:
ui.messageBox('No folder is selected')
else:
ui.messageBox('No Sketch located for this model')

 

Happy Coding to all!

I accept the solution, thank you!