Visibility prevents export and cannot be changed

Visibility prevents export and cannot be changed

Julie_7
Advocate Advocate
624 Views
3 Replies
Message 1 of 4

Visibility prevents export and cannot be changed

Julie_7
Advocate
Advocate

At first I did not understand why my adding could 3D print the bodies that I specified, but some of the STL files were not usable. Their small size gave me a clue, and I discovered that a body must be visible in order to save as STL.

 

1. Why does the visibility of a body affect the ability to export the STL?

Even in the UI I can select a body in the browser and use it for operations, such as combine without it being visible.

 

My first attempt at a solution was to just to make the body visible "body.isVisible = True" but that did not work.

I then read every forum post related to visibility, and there are many. That is how I found out that there is also a different way to express visibility using isLightBulbOn (which is very strange because it uses a UI based term).

 

Because changing the visibility of a body doesn't not automatically propagate upward through the containment tree, it is necessary to manually start at the top and set each item to be visible. (I created another post about this from the user/UI perspective.)

 

I cannot find an easy way to just move from the body up through the hierarchy to set each container to visible (I mean isLightBulbOn).

 

2. What is the best way using the API to make the visibility changes necessary to allow the export manager to use STLExportOptions(body, filename) to actually export the body?

 

0 Likes
625 Views
3 Replies
Replies (3)
Message 2 of 4

kandennti
Mentor
Mentor

Hi @Julie_7 .

 

>1. Why does the visibility of a body affect the ability to export the STL?

Since I was not familiar with it, I created simple data and scripts to test it.

The purpose is to export targetBody in STL.

1.png


Whether it exports at the position you want is another matter, but if you run the script with the first image, it exports without problems.

 

 

# Fusion360API Python script

import traceback
import adsk.core as core
import adsk.fusion as fusion

def run(context):
    ui: core.UserInterface = None
    try:
        app: core.Application = core.Application.get()
        ui = app.userInterface
        des: fusion.Design = app.activeProduct
        root: fusion.Component = des.rootComponent

        path = r'C:\temp\test.stl'

        occ: fusion.Occurrence = root.occurrences[0]
        comp: fusion.Component = occ.component

        targetOcc: fusion.Occurrence = comp.occurrences[0]
        targetComp: fusion.Component = targetOcc.component
        targetBody: fusion.BRepBody = targetComp.bRepBodies[0]

        expMgr: fusion.ExportManager = des.exportManager
        # stlOpt: fusion.STLExportOptions = expMgr.createSTLExportOptions(root, path)
        # stlOpt: fusion.STLExportOptions = expMgr.createSTLExportOptions(occ, path)
        # stlOpt: fusion.STLExportOptions = expMgr.createSTLExportOptions(targetOcc, path)
        # stlOpt: fusion.STLExportOptions = expMgr.createSTLExportOptions(targetComp, path)
        stlOpt: fusion.STLExportOptions = expMgr.createSTLExportOptions(targetBody, path)
        expMgr.execute(stlOpt)

        print(f'done : targetBody.isVisible={targetBody.isVisible}')

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

 

 

 

Next, we have hidden the "Bodies" targetBody is not visible on the screen, but the isVisible property is True.
1.png
This was also exported successfully.

 

Also, even when these were hidden, they were exported successfully.

1.png

 

Next, targetBody was hidden, and each pattern was tried, but none of them were exported.

1.png

In other words, I believe whether it is exported or not is determined by the BRepBody.isVisible property, not whether it is visible on the screen.

0 Likes
Message 3 of 4

kandennti
Mentor
Mentor

@Julie_7 .

 

>2. What is the best way using the API to make the visibility changes~

The easiest way I know of is to copy-paste only the bodies you need into a new document and export everything.

# Fusion360API Python script

import traceback
import adsk.core as core
import adsk.fusion as fusion

def run(context):
    ui: core.UserInterface = None
    try:
        app: core.Application = core.Application.get()
        ui = app.userInterface
        des: fusion.Design = app.activeProduct
        root: fusion.Component = des.rootComponent

        path = r'C:\temp\test.stl'

        occ: fusion.Occurrence = root.occurrences[0]
        comp: fusion.Component = occ.component

        targetOcc: fusion.Occurrence = comp.occurrences[0]
        targetComp: fusion.Component = targetOcc.component
        targetBody: fusion.BRepBody = targetComp.bRepBodies[0]

        # clone Body
        tmpMgr: fusion.TemporaryBRepManager = fusion.TemporaryBRepManager.get()
        clone: fusion.BRepBody = tmpMgr.copy(targetBody)

        # new doc
        newDoc: fusion.FusionDocument = app.documents.add(
            core.DocumentTypes.FusionDesignDocumentType
        )
        newDes: fusion.Design = app.activeProduct
        newDes.designType = fusion.DesignTypes.DirectDesignType
        newRoot: fusion.Component = newDes.rootComponent

        # add temp body
        newRoot.bRepBodies.add(clone)

        # export stl
        expMgr: fusion.ExportManager = newDes.exportManager
        stlOpt: fusion.STLExportOptions = expMgr.createSTLExportOptions(newRoot, path)
        expMgr.execute(stlOpt)

        # close new doc
        newDoc.close(False)

        print('done')

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


This one may also not output in the position you want, but position is another matter.

0 Likes
Message 4 of 4

Julie_7
Advocate
Advocate
I my failed attempt to export, I was setting the isVisible property to true before creating the export options and that is when it was succeeding with the export (no errors or exceptions) but the STL file had not body in it.
I will give it another try.