<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Checking if a component is external in Fusion API and Scripts Forum</title>
    <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/checking-if-a-component-is-external/m-p/12321950#M2632</link>
    <description>&lt;P&gt;Hi folks,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How would that check look like? I've tried using this&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;# Get the value of the property.
propertyValue = occurrence_var.isReferencedComponent&lt;/LI-CODE&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;Get the value of the property.&lt;/SPAN&gt;&lt;BR /&gt;propertyValue = occurrence_var.&lt;STRONG&gt;isReferencedComponent&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;this, but cannot seem to get it working.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Here is the rest of my code aswell&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;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)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 21 Oct 2023 22:03:10 GMT</pubDate>
    <dc:creator>magnus.wallem65FKK</dc:creator>
    <dc:date>2023-10-21T22:03:10Z</dc:date>
    <item>
      <title>Checking if a component is external</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/checking-if-a-component-is-external/m-p/12321950#M2632</link>
      <description>&lt;P&gt;Hi folks,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How would that check look like? I've tried using this&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;# Get the value of the property.
propertyValue = occurrence_var.isReferencedComponent&lt;/LI-CODE&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;Get the value of the property.&lt;/SPAN&gt;&lt;BR /&gt;propertyValue = occurrence_var.&lt;STRONG&gt;isReferencedComponent&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;this, but cannot seem to get it working.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Here is the rest of my code aswell&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;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)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 21 Oct 2023 22:03:10 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/checking-if-a-component-is-external/m-p/12321950#M2632</guid>
      <dc:creator>magnus.wallem65FKK</dc:creator>
      <dc:date>2023-10-21T22:03:10Z</dc:date>
    </item>
    <item>
      <title>Re: Checking if a component is external</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/checking-if-a-component-is-external/m-p/12322709#M2633</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/11614850"&gt;@magnus.wallem65FKK&lt;/a&gt;&amp;nbsp;-San.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I changed the is_external_component function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;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]
    )&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 22 Oct 2023 14:34:30 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/checking-if-a-component-is-external/m-p/12322709#M2633</guid>
      <dc:creator>kandennti</dc:creator>
      <dc:date>2023-10-22T14:34:30Z</dc:date>
    </item>
  </channel>
</rss>

