AutodeskAILab / Fusion360GalleryDataset

Data, tools, and documentation of the Fusion 360 Gallery Dataset
Other
408 stars 49 forks source link

Question about import of STEP file into fusion360. #102

Closed DavidXu-JJ closed 6 months ago

DavidXu-JJ commented 6 months ago

Hi everyone. Here I have some trouble import the STEP file into fusion360 with adsk python API.

Thanks to @karldd , I try to follow https://github.com/AutodeskAILab/Fusion360GalleryDataset/blob/1084b881f3bb710267801d812d6e9286b8667059/tools/fusion360gym/server/command_export.py#L111 as is mentioned in https://github.com/AutodeskAILab/Fusion360GalleryDataset/issues/101

To import the STEP file into the fusion360 following the official adsk import manager example here: https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-3f24e9e8-422d-11e5-937b-f8b156d7cd97

I write a following program to do the import:

from pathlib import Path
import sys
import os
import json
import adsk.core, adsk.fusion

# Add the client folder to sys.path
CLIENT_DIR = os.path.join(os.path.dirname(__file__), "..", "tools/fusion360gym/client")
if CLIENT_DIR not in sys.path:
    sys.path.append(CLIENT_DIR)

CLIENT_DIR = os.path.join(os.path.dirname(__file__), "..", "tools/fusion360gym/server")
if CLIENT_DIR not in sys.path:
    sys.path.append(CLIENT_DIR)

from fusion360gym_client import Fusion360GymClient
from command_runner import CommandRunner

# Before running ensure the Fusion360GymServer is running
# and configured with the same host name and port number
HOST_NAME = "127.0.0.1"
PORT_NUMBER = 8080

def main():
    # SETUP
    # Create the client class to interact with the server
    client = Fusion360GymClient(f"http://{HOST_NAME}:{PORT_NUMBER}")
    # Clear to force close all documents in Fusion
    # Do this before a new reconstruction
    r = client.clear()
    # Example of how we read the response data
    response_data = r.json()
    print(f"[{r.status_code}] Response: {response_data['message']}")

    file_path = "C:/Users/DavidXu/Downloads/20203_7e31e92a_0000_0005.step"
    # First clear to start fresh
    r = client.clear()

    app = adsk.core.Application.get()
    product = app.activeProduct
    design = adsk.fusion.Design.cast(product)
    rootComp = design.rootComponent
    importManager = app.importManager
    stpOptions = importManager.createSTEPImportOptions(file_path)
    stpOptions.isViewFit = False
    print(importManager.importToTarget(stpOptions, rootComp))

if __name__ == "__main__":
    main()

I simply get this output:

[200] Response: Success processing clear command
False

For the function importManager.importToTarget documented at https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-7472BAC7-E570-43CE-8578-268735B6FE83, return false means the failure.

As is in the documentation: Returns true if the import was successful.

My import fails, and I also output the design.allComponents.count as 0. All seem to mean that it doesn't import successfully.

I test this code on Windows10.

What's wrong with my import code?

Thanks in advance.

karldd commented 6 months ago

I think its better to just write a regular Fusion 360 API script without importing anything from this repository. Rather I would work from the examples to import the step file then copy across the screenshot code. So I would remove all of the client code to make things simpler.

DavidXu-JJ commented 6 months ago

Thanks for your kind advice!