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: 

Chatgpt Pseudo Code

2 REPLIES 2
Reply
Message 1 of 3
sheryl.k.chambers
220 Views, 2 Replies

Chatgpt Pseudo Code

I'd like to have a script that exports the selected vertices of a sketch as a csv file.  I think I'm having terminology issues.  What is the proper name for what I've selected here in sketch mode?  Points?  Points2d?  

 

All assistance is welcome.

 

import adsk.core, adsk.fusion, adsk.cam, traceback
import csv

# Function to retrieve XYZ data of selected points
def get_selected_points_xyz():
    app = adsk.core.Application.get()
    design = app.activeProduct
    product_type = design.productType

    if product_type != 'DirectDesignProductType':
        return None

    # Get the selected entities
    selected_entities = design.activeProduct.rootComponent.activeSelections
    xyz_data = []

    for entity in selected_entities:
        if hasattr(entity, 'geometry'😞
            if entity.geometry.objectType == adsk.core.Point3D.classType():
                x = entity.geometry.x
                y = entity.geometry.y
                z = entity.geometry.z
                xyz_data.append([x, y, z])

    return xyz_data

# Function to export XYZ data to a CSV file
def export_xyz_to_csv(file_path, xyz_data😞
    with open(file_path, 'w', newline='') as file:
        writer = csv.writer(file)
        writer.writerow(['X', 'Y', 'Z'])  # Write header row

        for data in xyz_data:
            writer.writerow(data)

# Run the script
try:
    app = adsk.core.Application.get()
    ui = app.userInterface

    # Check if any entities are selected
    selected_entities = app.activeProduct.rootComponent.activeSelections
    if len(selected_entities) == 0:
        ui.messageBox('No entities are selected.')
    else:
        # Retrieve XYZ data of selected points
        selected_points_xyz = get_selected_points_xyz()
        if not selected_points_xyz:
            ui.messageBox('No points with XYZ data are selected.')
        else:
            # Prompt the user to select a destination file path
            save_dialog = ui.createFileDialog()
            save_dialog.title = 'Save XYZ Data'
            save_dialog.filter = 'CSV Files (*.csv)'
            save_dialog.isMultiSelectEnabled = False
            dialog_result = save_dialog.showSave()
            if dialog_result == adsk.core.DialogResults.DialogOK:
                file_path = save_dialog.filename

                # Export the XYZ data to the CSV file
                export_xyz_to_csv(file_path, selected_points_xyz)
                ui.messageBox('XYZ data exported successfully to {}'.format(file_path))
            else:
                ui.messageBox('Export canceled by the user.')

except:
    if ui:
        ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
2 REPLIES 2
Message 2 of 3

Hi @sheryl.k.chambers -San.

 

The points in the sketch are SketchPoint objects.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-7c577d59-3345-408e-b6b3-1d908064d026 

 

I modified it to avoid errors.
I only had to fix three places. This is excellent if the script is made in chatGPT.

import adsk.core, adsk.fusion, adsk.cam, traceback
import csv

# Function to retrieve XYZ data of selected points
def get_selected_points_xyz():
    app = adsk.core.Application.get()
    design: adsk.fusion.Design = app.activeProduct
    product_type = design.productType

    # if product_type != 'DirectDesignProductType':
    if product_type != 'DesignProductType':
        return None

    # Get the selected entities
    # selected_entities = design.activeProduct.rootComponent.activeSelections
    selected_entities = [sel.entity for sel in ui.activeSelections]
    xyz_data = []

    for entity in selected_entities:
        if hasattr(entity, 'geometry'):
            if entity.geometry.objectType == adsk.core.Point3D.classType():
                x = entity.geometry.x
                y = entity.geometry.y
                z = entity.geometry.z
                xyz_data.append([x, y, z])

    return xyz_data

# Function to export XYZ data to a CSV file
def export_xyz_to_csv(file_path, xyz_data):
    with open(file_path, 'w', newline='') as file:
        writer = csv.writer(file)
        writer.writerow(['X', 'Y', 'Z'])  # Write header row

        for data in xyz_data:
            writer.writerow(data)

# Run the script
try:
    app = adsk.core.Application.get()
    ui = app.userInterface

    # Check if any entities are selected
    # selected_entities = app.activeProduct.rootComponent.activeSelections
    selected_entities = [sel.entity for sel in ui.activeSelections]
    if len(selected_entities) == 0:
        ui.messageBox('No entities are selected.')
    else:
        # Retrieve XYZ data of selected points
        selected_points_xyz = get_selected_points_xyz()
        if not selected_points_xyz:
            ui.messageBox('No points with XYZ data are selected.')
        else:
            # Prompt the user to select a destination file path
            save_dialog = ui.createFileDialog()
            save_dialog.title = 'Save XYZ Data'
            save_dialog.filter = 'CSV Files (*.csv)'
            save_dialog.isMultiSelectEnabled = False
            dialog_result = save_dialog.showSave()
            if dialog_result == adsk.core.DialogResults.DialogOK:
                file_path = save_dialog.filename

                # Export the XYZ data to the CSV file
                export_xyz_to_csv(file_path, selected_points_xyz)
                ui.messageBox('XYZ data exported successfully to {}'.format(file_path))
            else:
                ui.messageBox('Export canceled by the user.')

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

 

If you want to export the coordinate values of all displayed sketch points in a csv file, please use this.

https://github.com/kantoku-code/Fusion360-ExportSketchPointsCoordinate 

Message 3 of 3

That did the trick, thanks so much.  I only had to multiply the units by 10 to get mm (defaults to cm?)

 I followed this tuition and found it to be sensitive to using this exact starting language of the prompt:

https://youtu.be/NV4KNvGJaYI 

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

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report