Clear current design

Clear current design

copypastestd
Advocate Advocate
867 Views
4 Replies
Message 1 of 5

Clear current design

copypastestd
Advocate
Advocate

Hello,

 

during programming, you have to run the script multiple times for debugging.

 

In order to run the script in a clean design, I use:

 

doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)

But this leads to the fact that during debugging creates a lot of new designs.

 

How to clear current design (delete all existing components etc)? This would help to solve this problem.

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

ekinsb
Alumni
Alumni
Accepted solution

Personally, I prefer to keep this code out of my program and I just manually close the previous design, create a new design and start debugging.

 

However, you could add code that will close all open documents.  The function below will do this and because Fusion requires that there is always at least one open document it will automatically create a new one. It closes the documents without saving any changes so you have to be careful using it because you will lose any modeling work.

 

def closeAll():
    ui = None 
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface

        # Build a list of the open documents.
        docs = []
        for doc in app.documents:
            docs.append(doc)
        
        # Close all open documents, without saving them.
        for doc in docs:
            doc.close(False)
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

 


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 3 of 5

copypastestd
Advocate
Advocate

Brain, tnx for your help.

0 Likes
Message 4 of 5

CaptainXamtastic
Explorer
Explorer

@ekinsb That's a fabulous script, I added it as a class to my libraries so that I can call it whenever I want whilst debugging!

 

 

        class CloseAllDocuments:
            
            def __init__(self, save):
                self.ui = None 
                try:
                    self.save = save
                    self.app = adsk.core.Application.get()
                    self.ui = app.userInterface
                    if self.save:
                        self.ui.messageBox('Saving and Closing all existing documents ...')
                    else:
                        self.ui.messageBox('Closing all existing documents without saving ...')
                    self.docs = []
                    for doc in self.app.documents:
                        self.docs.append(doc)
                    # Close all open documents, without saving them.
                    for doc in self.docs:
                        doc.close(self.save)
                except:
                    if ui:
                        self.ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))   

So when debugging I just start with:

 

CloseAllDocuments(False)

and then follow up with what I'm working on!

 

Thanks!

 

 

0 Likes
Message 5 of 5

JesusFreke
Advocate
Advocate

I have this little helper function I use at the beginning of a script. It closes the document with the given name, creates and actives a new document with the same name, and then sets the viewport in the new document to match that of the document that was just closed.

 

I find the viewport thing in particular to be useful when doing iterative design via the API. Otherwise, I find that I'm always having to move the viewport back to whatever I was just looking at, after making a tweak in the script and re-running it.

 

def setup_document(document_name="ScriptDocument"):
    app = adsk.core.Application.get()

    preview_doc = None  # type: adsk.fusion.FusionDocument
    saved_camera = None
    for document in app.documents:
        if document.name == document_name:
            preview_doc = document
            break
    if preview_doc is not None:
        preview_doc.activate()
        saved_camera = app.activeViewport.camera
        preview_doc.close(False)

    preview_doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
    preview_doc.name = document_name
    preview_doc.activate()
    if saved_camera is not None:
        is_smooth_transition_bak = saved_camera.isSmoothTransition
        saved_camera.isSmoothTransition = False
        app.activeViewport.camera = saved_camera
        saved_camera.isSmoothTransition = is_smooth_transition_bak
        app.activeViewport.camera = saved_camera
0 Likes