What options exist for storing and retrieving sketches?

What options exist for storing and retrieving sketches?

markusbarnes
Advocate Advocate
672 Views
4 Replies
Message 1 of 5

What options exist for storing and retrieving sketches?

markusbarnes
Advocate
Advocate

I am writing a plug-in which creates models from several predefined sketches. Although the sketches don't change, they're painstakingly constructed via code using API's sketch commands. This approach makes it hard to add, update, replace or update a sketch in the future which requires modifying code. Ideally, it would be nice if a sketch could be read in from a resource and instantiated at runtime. While I could create my own file format containing the drawing commands, it may lack some of the functionality such as parameters. Moreover, doing so may be reinventing the wheel. What options exist for storing and retrieving sketches?

 

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

BrianEkins
Mentor
Mentor

There isn't anything built-in to Fusion for saving a sketch except as part of a full design. However, I did a quick test, which may be a good option. You can create a design that contains all of your sketches and then copy them from that design into the new design as needed.

 

Here's my test code. To use it create a design that contains two sketches in the root component. You can draw whatever you want in the sketches, including dimensions and text. Have that design active, and then run the script. It will create a new design and copy the sketches into it on different construction planes. In practice, you would open the design that contains the sketches when you need it and then copy those sketches into another design that you're working on.

 

def run(context):
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface

        sourceDoc = app.activeDocument
        sourceDes: adsk.fusion.Design = sourceDoc.products.itemByProductType('DesignProductType')
        sourceSk1 = sourceDes.rootComponent.sketches[0]
        sourceSk2 = sourceDes.rootComponent.sketches[1]

        targetDoc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType, True)
        targetDes: adsk.fusion.Design = targetDoc.products.itemByProductType('DesignProductType')

        targetRoot = targetDes.rootComponent
        targetSk = targetRoot.sketches.add(targetRoot.xYConstructionPlane)
        CopySketch(sourceSk1, targetSk)

        targetSk = targetRoot.sketches.add(targetRoot.xZConstructionPlane)
        CopySketch(sourceSk2, targetSk)
    except:
        ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


def CopySketch(sourceSk: adsk.fusion.Sketch, targetSk: adsk.fusion.Sketch):
    try:
        col = adsk.core.ObjectCollection.create()
        for curve in sourceSk.sketchCurves:
            col.add(curve)

        for text in sourceSk.sketchTexts:
            col.add(text)

        pnt: adsk.fusion.SketchPoint = None
        for pnt in sourceSk.sketchPoints:
            if pnt.connectedEntities is None:
                col.add(pnt)

        trans = adsk.core.Matrix3D.create()
        sourceSk.copy(col, trans, targetSk)
    except:
        ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 5

markusbarnes
Advocate
Advocate

At first, it looked like nothing was happening. Where are the duplicated sketches?  Just when I was about to shut down for the day, I noticed a bunch of open tabs on the main window. Duh!  It copied the sketches to a new document. It happen so quickly, the drawing just flashed. Very cool! Thanks for the code. This is definitely an ideal solution. This solution raises a couple of new questions.

 

First, how can the document be redistributed for deployment? Can it be turned into a file and added to a resource folder with all the plugins' assets? Fusion 360 files normally live online.  This raises an interesting possibility.  There are several plugins that download assets from some online repository into the user's document. What API is used to retrieve those assets?  What servers are they stored in?

 

Second, I noticed the sketches were copied as entire entities. Is it possible to copy only a subset of a larger sketch?  It's cumbersome to manage hundreds of small sketches on separate planes. I was thinking along the lines of having them tiled out into one indexable matrix on a single sketch or several sketches based on their category.

0 Likes
Message 4 of 5

info83PHN
Advocate
Advocate

Not 100% certain this is what you're needing, but for some of my scripts I have created a master design and exported that locally as a Fusion f3d file in to my script folder.

 

Then, when I need that master design, which is the start of a model that I adapt often ( imagine something like a name tag backing plate and ring ), I simple run the script that Imports the f3d file as a new design.

After import, I then add a load of detail that's specific to the model, either with data from the Clipboard, or read from a text file.

 

 

    f3dFilename = "E:\\ABC\\Name Tag Master v1.f3d"
    importManager = app.importManager
    importOptions = importManager.createFusionArchiveImportOptions(f3dFilename)
    importManager.importToNewDocument(importOptions)

 

0 Likes
Message 5 of 5

BrianEkins
Mentor
Mentor
Accepted solution

The file that contains the sketch can either be saved locally on your computer, or you can save it on the cloud. With a local file, you can use the ImportManager and the importToNewDocument method to "open" the local file to access the sketch. If you want to save it on the cloud, which will give everyone access to the file, you can use the functionality associated with the Data object. The Data object is the API equivalent of the Data Panel, where you can traverse your projects, folders, and files. In this case, you might first get the ID of the file and then save that in your program to use the findByFileId method to get the file directly. You'll need to open it, and then you can access the sketch.

 

Regarding accessing a subset of a sketch, the method I illustrated in the first sample is choosing what to copy, but it's not very choosy by copying everything. You could be more selective in what's added to the collection, so not everything is copied. However, how would you do that? You would have to have some way to specify the set of items you want to copy. The easiest way to break things up is to have them in separate sketches.

 

 

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes