I made a thing. Export DXF and SAT files for sheet metal parts

I made a thing. Export DXF and SAT files for sheet metal parts

magnus.wallem65FKK
Participant Participant
619 Views
0 Replies
Message 1 of 1

I made a thing. Export DXF and SAT files for sheet metal parts

magnus.wallem65FKK
Participant
Participant

Here's a happy post for you all 🙂 

I've made a script that looks for flat patterns in the file, exports the dxf, if the part has a bend it also exports the SAT file. These are the files that our sheet metal shop use for constructing items. 

Have a look, enjoy. 

 

 

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

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

        # 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

        # Get active design
        product = app.activeProduct
        root_component = adsk.fusion.Component.cast(product.rootComponent)
        design_name = root_component.name  # Store the design name

        # Create a list to store the selected components
        selected_components = []

        # Create a list to store components missing flat patterns
        missing_flat_patterns = []

        # Select components that are sheet metal and have flat patterns
        component_names = set()  # Store component names to avoid counting duplicates
        for occ in root_component.allOccurrences:
            comp = adsk.fusion.Component.cast(occ.component)
            if comp and comp.name not in component_names:
                component_names.add(comp.name)  # Add the component name to the set
                if is_sheet_metal_component(comp):
                    if not comp.flatPattern:
                        missing_flat_patterns.append(comp.name)
                    else:
                        selected_components.append(comp)

        if missing_flat_patterns:
            message = "The following components are missing flat patterns and cannot be exported:\n\n"
            message += "\n".join(missing_flat_patterns)
            ui.messageBox(message)
            return

        # Create a single exportManager instance
        exportMgr = root_component.parentDesign.exportManager

        # Export the selected components one by one with a specified format
        total_exports = 0
        failed_exports = []  # List to store the names of failed files and components

        for comp in selected_components:
            compName = comp.name
            # Specify the full file path for the SAT export
            sat_file_path = os.path.join(export_folder, f"{compName}.sat")

            # Check if the part has bends
            has_bend = has_bend_in_component(comp)

            if has_bend:
                # Export the component with SAT format
                satOptions = exportMgr.createSATExportOptions(sat_file_path, comp)
                try:
                    exportMgr.execute(satOptions)
                    total_exports += 1
                except Exception as e:
                    failed_exports.append(f"Component: {compName}, Type: SAT")

                # Check if a flat pattern exists
                if comp.flatPattern:
                    compName = comp.name
                    # Specify the full file path for the DXF export
                    dxf_file_path = os.path.join(export_folder, f"{compName}_FlatPattern.dxf")

                    exportMgr = comp.parentDesign.exportManager
                    dxf_options = exportMgr.createDXFFlatPatternExportOptions(dxf_file_path, comp.flatPattern)

                    try:
                        exportMgr.execute(dxf_options)
                        total_exports += 1
                    except Exception as e:
                        failed_exports.append(f"Component: {compName}, Type: DXF")
            else:
                # If no bends, export only the DXF
                if comp.flatPattern:
                    compName = comp.name
                    # Specify the full file path for the DXF export
                    dxf_file_path = os.path.join(export_folder, f"{compName}.dxf")

                    exportMgr = comp.parentDesign.exportManager
                    dxf_options = exportMgr.createDXFFlatPatternExportOptions(dxf_file_path, comp.flatPattern)

                    try:
                        exportMgr.execute(dxf_options)
                        total_exports += 1
                    except Exception as e:
                        failed_exports.append(f"Component: {compName}, Type: DXF")

        summary_message = f'{total_exports} files exported'

        if failed_exports:
            summary_message += f'\n\nFailed to export the following files or components:\n'
            for failed_export in failed_exports:
                summary_message += f"{failed_export}\n"

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

def is_external_component(comp):
    # Check if the component is external
    return comp.classType() != adsk.fusion.Component.classType()

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 has_bend_in_component(comp):
    # Check if the component has a bend
    fp = comp.flatPattern
    if not fp:
        return False  # No flat pattern, no bends

    topFaceZ = fp.topFace.pointOnFace.z
    bendLinesBody = fp.bendLinesBody
    if not bendLinesBody:
        return False  # No bend lines body, no bends

    for BRepEdge in bendLinesBody.edges:
        # Check if this edge is in the same Z plane as the top face.
        # Because of issues with floating point comparisons, it uses a tolerance.
        if math.fabs(BRepEdge.pointOnEdge.z - topFaceZ) < 0.00001:
            return True
    return False

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

 

-Wall-E

620 Views
0 Replies
Replies (0)