Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Checking if a component is external

1 REPLY 1
SOLVED
Reply
Message 1 of 2
magnus.wallem65FKK
118 Views, 1 Reply

Checking if a component is external

Hi folks, 

 

I'm making a script to export the files I need and it's working great for all components that are internaly in the project, but I'd like it to also do it for external components. 

 

How would that check look like? I've tried using this 

# Get the value of the property.
propertyValue = occurrence_var.isReferencedComponent

 Get the value of the property.
propertyValue = occurrence_var.isReferencedComponent

this, but cannot seem to get it working. 

Here is the rest of my code aswell 

 

import adsk.core, adsk.fusion, traceback, os

# Global variable to store the ui object
ui = None

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

        # Get active design
        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)

        # List to store external components
        external_components = []

        # Iterate through all components and check their isReferencedComponent values
        for component in design.allComponents:
            is_referenced = is_external_component(component)
            if is_referenced:
                external_components.append(component)

        if not external_components:
            ui.messageBox("No external components found in the active design.")
            return

        # Display a message with the list of external components
        components_message = "External Components:\n"
        for comp in external_components:
            components_message += f"{comp.name}\n"
        ui.messageBox(components_message, "External Components")

        # Prompt the user to select the export folder
        folderDialog = ui.createFolderDialog()
        folderDialog.title = "Select Export Folder"
        folderDialog.initialDirectory = os.path.expanduser("~\\Desktop")  # Default to the desktop

        # Show the folder dialog and get the selected folder
        dialogResult = folderDialog.showDialog()
        if dialogResult == adsk.core.DialogResults.DialogOK:
            export_folder = folderDialog.folder
        else:
            # User canceled the folder selection
            return

        # Create a single exportManager instance
        exportMgr = design.exportManager

        # Export the selected external components one by one with a specified format
        for comp in external_components:
            compName = comp.name
            # Specify the full file path for the export
            file_path = os.path.join(export_folder, compName)

            open_and_activate_component(comp)

            # Export the component with SAT format
            satOptions = exportMgr.createSATExportOptions(file_path, comp)
            exportMgr.execute(satOptions)

            if is_sheet_metal_component(comp):
                # Check if a flat pattern exists
                if comp.flatPattern:
                    export_dxf(export_folder, comp)

        if ui:
            ui.messageBox('Export completed. Files saved to the selected folder.')
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

def is_external_component(comp):
    # Check if the component is external by checking its occurrences
    external_found = False
    for occurrence in comp.allOccurrences:
        try:
            propertyValue = occurrence.properties.itemByName("isReferencedComponent").value
            if propertyValue:
                return True
        except Exception as e:
            pass  # Ignore any errors and continue checking occurrences
    return False



def is_sheet_metal_component(comp):
    # Check if the component is a sheet metal component
    is_sheet_metal = False
    for body in comp.bRepBodies:
        if body.isSheetMetal:
            is_sheet_metal = True
            break
    return is_sheet_metal

def open_and_activate_component(comp):
    # Open and activate the component
    design = comp.parentDesign
    design.activateRootComponent()
    design.activateObject(comp)

def export_dxf(export_folder, comp):
    # Specify the DXF file path
    compName = comp.name
    dxf_file_path = os.path.join(export_folder, f"{compName}_FlatPattern.dxf")

    # Create DXF export options for the flat pattern
    exportMgr = comp.parentDesign.exportManager
    dxf_options = exportMgr.createDXFFlatPatternExportOptions(dxf_file_path, comp.flatPattern)
    # Execute the export
    exportMgr.execute(dxf_options)

if __name__ == '__main__':
    run(None)

 

Labels (1)
1 REPLY 1
Message 2 of 2

Hi @magnus.wallem65FKK -San.

 

I changed the is_external_component function.

 

def is_external_component(comp: adsk.fusion.Component):
    # Check if the component is external by checking its occurrences
    # external_found = False
    # for occurrence in comp.allOccurrences:
    #     try:
    #         propertyValue = occurrence.properties.itemByName("isReferencedComponent").value
    #         if propertyValue:
    #             return True
    #     except Exception as e:
    #         pass  # Ignore any errors and continue checking occurrences
    # return False

    app: adsk.core.Application = adsk.core.Application.get()
    design: adsk.fusion.Design = adsk.fusion.Design.cast(
      app.activeProduct
    )
    root: adsk.fusion.Component = design.rootComponent
    occs: adsk.fusion.OccurrenceList = root.occurrencesByComponent(comp)
    occ: adsk.fusion.Occurrence = None
    return any(
      [occ.isReferencedComponent for occ in occs]
    )

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report