Activating a referenced component product

Activating a referenced component product

SaeedHamza
Advisor Advisor
337 Views
1 Reply
Message 1 of 2

Activating a referenced component product

SaeedHamza
Advisor
Advisor

Hello everyone,

 

I'm trying to activate the product that contains a referenced component. This is what I want to do :

I want to select a referenced component (linked) from a design that it was imported into, and open the original design of that component.

What I want to achieve here is simply make the original design (product) of that referenced component the current active product in the app, so that I can edit with a single script both files, the original design of the referenced component and the one it was imported into.

 

I made a lot of research regarding this, but I just can't seem to change the active product to be the referenced component original file, because I want to script to go back and forth between both designs while running.

 

I have no issue if I have to at least manually open the original design of the referenced component, but I need to change the active product as mentioned above back and forth between both products.

 

Any help would be much appreciated!

 

Thanks.

Saeed Hamza
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature

0 Likes
Accepted solutions (1)
338 Views
1 Reply
Reply (1)
Message 2 of 2

kandennti
Mentor
Mentor
Accepted solution

Hi @SaeedHamza .

 

The attached f3z file looks like this.

1.png

The following script will open the external component and create a new occurrence in the root component of both documents.

# Fusion360API Python script

import traceback
import adsk.core as core
import adsk.fusion as fusion

def run(context):
    ui = core.UserInterface.cast(None)
    try:
        app: core.Application = core.Application.get()
        ui = app.userInterface

        # active document
        parentDoc: fusion.FusionDocument = app.activeDocument

        # Get document for the first external component
        childDoc: fusion.FusionDocument = get_external_component_document()
        if not childDoc:
            return

        # This method is slow
        # app.documents.open(childDoc.dataFile)

        # Since it is already loaded, simply activate it and it will appear
        childDoc.activate()

        # Create a new occurrence in the root component of each document
        create_internal_component(parentDoc)
        create_internal_component(childDoc)

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


def create_internal_component(
doc: fusion.FusionDocument) -> None:

    doc.activate()
    doc.design.rootComponent.occurrences.addNewComponent(
        core.Matrix3D.create()
    )


def get_external_component_document(
) -> fusion.FusionDocument:

    app: core.Application = core.Application.get()
    des: fusion.Design = app.activeProduct
    root: fusion.Component = des.rootComponent

    occ: fusion.Occurrence = None
    for occ in root.occurrences:
        if occ.isReferencedComponent:
            return occ.documentReference.referencedDocument

    return None

 

1.png