I can`t export a model to a single file if it contains an another linked model.

I can`t export a model to a single file if it contains an another linked model.

Anonymous
Not applicable
406 Views
1 Reply
Message 1 of 2

I can`t export a model to a single file if it contains an another linked model.

Anonymous
Not applicable

I am writing a Python plugin for printing that prints highlighted parts. To do this, I need to export them in the format "STL" in one file. Export occurs normally if the project has no linked models, or linked model is in the project root. But if there is an linked model in the project, in which there is also an linked model (the second level of nesting of linked models), then the API output the errors.

 

For export, when the associated model is at the root, I had to create a temporary occurrence at the project  root and copy the selected components in this occurrence. And break the connection of the copied models. This is done because the export function does not work differently!  After export, I delete this occurrence. But this method does not work when there is another related in the associated model. As shown in the figures:

 

Picture1.png

Picture2.png

 

Example of error:

"File" ....../MakerBotPlugin.py ", line 170, in exportToDisk
    occurence.deleteMe ()
  File "C: / ...... /AppData / Local / Autodesk / webdeploy / production / ...... / Api / Python / packages \ adsk \ fusion.py", line 18955, in deleteMe
    return _fusion.Occurrence_deleteMe (self)
RuntimeError: 5: MoveFace1 / Compute Failed // //).
    Try adjusting the values ​​or changing the input geometry."

 

Part of the export code in the attachment. Does anyone know  - is there any way to export models, regardless of their linked state and nesting level?

0 Likes
407 Views
1 Reply
Reply (1)
Message 2 of 2

Anonymous
Not applicable

I can`t add the attachment file. So add the part of the export code there:

 

# TODO: Fix the code in the future after appearance of a normal API for copying or export bodies.
def exportToDisk(selection, isSelectedAll):
    tempDir = None
    try:
        app = adsk.core.Application.get()
        design = adsk.fusion.Design.cast(app.activeProduct)
        exportMgr = design.exportManager
        rootComp = design.rootComponent
        tempDir = tempfile.TemporaryDirectory()
        occurenceCopies = []

        if isSelectedAll:
            # NOTE: Add all bodies, but linked bodies will not be exported.
            selectedBodies = [body for body in rootComp.bRepBodies]

            # NOTE: Create unlinked copies of occurences, and add to the 'bodies' list.
            for occ in rootComp.allOccurrences:
                if (occ.isReferencedComponent):
                    occurenceCopy = U.addUnlinkedCopyToRoot(rootComp, occ)
                    occurenceCopies.append(occurenceCopy)
                    occ = occurenceCopy

                for body in occ.component.bRepBodies:
                    selectedBodies.append(body)
        else:
            # NOTE: Add all bodies, but linked bodies will not be exported.
            selectedBodies = [selection.selection(index).entity for index in range(selection.selectionCount)]

            # NOTE: Create unlinked copies of occurences, and add to the 'bodies' list.
            for occ in rootComp.allOccurrences:
                if (occ.isReferencedComponent):
                    occurenceCopy = U.addUnlinkedCopyToRoot(rootComp, occ)
                    occurenceCopies.append(occurenceCopy)

                    # NOTE: Append the copy of the linked body.
                    for body in occ.component.bRepBodies:
                        if any(body.revisionId == selectedBody.revisionId for selectedBody in selectedBodies):
                            selectedBodies.append(occurenceCopy.component.bRepBodies.itemByName(body.name))

        solidCopyComponent = createSolidCopy(selectedBodies)

        fileName = os.path.join(tempDir.name, U.getRootName(rootComp))
        stlExportOptions = exportMgr.createSTLExportOptions(solidCopyComponent, fileName)
        stlExportOptions.sendToPrintUtility = False
        exportMgr.execute(stlExportOptions)

        # NOTE: Remove temporary objects.
        for occurence in list(rootComp.occurrences):
            if occurence.isValid and occurence.component.name == solidCopyComponent.name:
                occurence.deleteMe()
        for occurence in list(occurenceCopies):
            occurence.deleteMe()

    except Exception:
        U.showErrorMessage('An unresolved error occured!' + traceback.format_exc())

    return tempDir

def createSolidCopy(bodies):
    rootComp = adsk.fusion.Design.cast(adsk.core.Application.get().activeProduct).rootComponent
    solidCopyComponent = rootComp.occurrences.addNewComponent(adsk.core.Matrix3D.create()).component
    solidCopyComponent.name = uid('SolidCopyComponent')
    sourceBodies = adsk.core.ObjectCollection.create()

    for body in bodies:
        sourceBodies.add(body)

    design = adsk.fusion.Design.cast(adsk.core.Application.get().activeProduct)

    # NOTE: We need to do this because the CopyPasteBody feature is not supported in Direct Design.
    isDirectMode = design.designType == adsk.fusion.DesignTypes.DirectDesignType
    if isDirectMode:
        design.designType = adsk.fusion.DesignTypes.ParametricDesignType

    solidCopyComponent.features.copyPasteBodies.add(sourceBodies)

    if isDirectMode:
        design.designType = adsk.fusion.DesignTypes.DirectDesignType

    return solidCopyComponent
0 Likes