AutodeskAILab / Fusion360GalleryDataset

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

Tools to parse B-REP #61

Closed Enigma-li closed 3 years ago

Enigma-li commented 3 years ago

Hi,

Are there some tools or APIs available to parse the B-REP file to get the boundary curves after every extrusion step?

Thanks

karldd commented 3 years ago

Take a look here: https://github.com/AutodeskAILab/Fusion360GalleryDataset/blob/master/tools/reconverter/reconverter.py#L81

The inc_export_extrude() function calls back at each extrusion step. So you should have access to the BRepBody data structure.

So something like this (untested) code:

for body in self.design.rootComponent.bRepBodies:
     for edge in body.edges:
          # do something with edge

See the BRepEdge data structure for how to work with edges.

Enigma-li commented 3 years ago

Thanks very much, it is helpful :)

Could we directly load the B-REP (.setp or .smt) file and parse it using the similar sample code above? In this way, we can reduce the burden not to start from the json, then execute, then extract edges, so that we can directly parse the B-REP file.

karldd commented 3 years ago

Yes you could load the B-Rep's in the dataset in directly. There are a couple of ways to do that. Probably the fastest, if you only care about the data in the B-Rep is:

temp_brep_mgr = adsk.fusion.TemporaryBRepManager.get()
breps = temp_brep_mgr.createFromFile(smt_file)
for body in breps:
     for edge in body.edges:
          # do something with edge

This approach doesn't load the B-Rep into the parametric CAD environment. The other approach would be with the ImportManager.

Enigma-li commented 3 years ago

Thanks very much, I will try it

Enigma-li commented 3 years ago

Hi Karl,

I tested the code to extract edges from BRep file, just curious to know that could we directly run the pure python code by importing the adsk module?

Below are my sample codes to implement what I said, the fusion360 script works well, but my attempted version did not work:

Fusion360 script:

import adsk.core
import adsk.fusion
import traceback
import json
import os
import sys
import time
from os import listdir
from pathlib import Path

def run(context):
    current_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
    data_dir = os.path.join(current_dir, "testdata")

    smt_files = [os.path.join(data_dir, f) for f in listdir(data_dir) if f.endswith('.smt')]

    for j in range(len(smt_files)):
        smt_file = smt_files[j]
        temp_brep_mgr = adsk.fusion.TemporaryBRepManager.get()
        breps = temp_brep_mgr.createFromFile(smt_file)

        print('#bodies: %d' % breps.count)
        for i in range(breps.count):
            body = breps[i]
            print('Edges: %d' % body.edges.count)

Pure python version:

import sys
# impor adsk
adsk_path = r'C:\Users\chjil\AppData\Roaming\Autodesk\Autodesk Fusion 360\API\Python\defs'
sys.path.append(adsk_path)

import adsk.core
import adsk.fusion
import traceback
import json
import os
import time
from os import listdir
from pathlib import Path

def run():
    current_dir = os.path.abspath(os.path.dirname(__file__))
    data_dir = os.path.join(current_dir, "testdata")

    smt_files = [os.path.join(data_dir, f) for f in listdir(data_dir) if f.endswith('.smt')]

    for j in range(len(smt_files)):
        smt_file = smt_files[j]
        temp_brep_mgr = adsk.fusion.TemporaryBRepManager.get()
        breps = temp_brep_mgr.createFromFile(smt_file._str)

        print('#bodies: %d' % breps.count)
        for i in range(breps.count):
            body = breps[i]
            print('Edges: %d' % body.edges.count)

if __name__ == "__main__":
    print('Extract edges...')
    run()

The error from this version is that:

Traceback (most recent call last):
  File "extractEdges.py", line 36, in <module>
    run()
  File "extractEdges.py", line 26, in run
    temp_brep_mgr = adsk.fusion.TemporaryBRepManager.get()
TypeError: get() missing 1 required positional argument: 'self'

Could you please help advise?

karldd commented 3 years ago

Unfortunately I believe you have to be running the script from within Fusion 360.

The easiest way to do this is to run in debug mode from VS Code. This allows you to invoke the script from VS Code without pressing buttons in Fusion 360. https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-9701BBA7-EC0E-4016-A9C8-964AA4838954

Another option is: https://github.com/JesusFreke/fusionIdea

Enigma-li commented 3 years ago

Noted and thanks very much :)

karldd commented 3 years ago

Closing this. Feel free to reopen if you have further questions.