Extruding text fails with: RuntimeError: 3 : Some input argument is invalid.

Extruding text fails with: RuntimeError: 3 : Some input argument is invalid.

ksautodesk5MA6Q
Explorer Explorer
305 Views
2 Replies
Message 1 of 3

Extruding text fails with: RuntimeError: 3 : Some input argument is invalid.

ksautodesk5MA6Q
Explorer
Explorer

I'm trying to create different bodies for text embossed in a surface in order to 3d print with multi material.

The following code breaks when using the inward type (outward = False).

The idea is that when I can print multi-material, the text should be on the same surface (not outward). Therefore I want to carve the extrusion out first to avoid any glitches on the top surface, then create a second extrusion that creates new bodies. ex3 ends up in the runtime error.

If I stop at ex2, I can pretty well create this extrusion manually in the design, going downwards and creating 2 new bodies (one for T, one for X).

I can't figure out what is wrong and how to fix it.

 

 

 

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

app: adsk.core.Application = adsk.core.Application.get()

def run(context):
    ui = None
    try:
        for document in app.documents:
            if document.name == os.path.basename(__file__):
                document.close(False)
        doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
        doc.name = os.path.basename(__file__)
        doc.activate()

        design: adsk.core.Product = app.activeProduct
        targetComponent: adsk.fusion.Component = design.rootComponent

        extrudes: adsk.fusion.ExtrudeFeatures = targetComponent.features.extrudeFeatures
        fillets: adsk.fusion.FilletFeatures = targetComponent.features.filletFeatures
        planes: adsk.fusion.ConstructionPlanes = targetComponent.constructionPlanes

        constructionPlane: adsk.fusion.ConstructionPlane = targetComponent.xYConstructionPlane
        sketches: adsk.fusion.Sketches = targetComponent.sketches
        sketch: adsk.fusion.Sketch = sketches.add(constructionPlane)
        baseOrigin: adsk.core.Point3D = sketch.modelToSketchSpace(adsk.core.Point3D.create(0, 0, 0))
        print(f"base origin: {dumpPoint3D(baseOrigin)}")

        length = LABEL_WIDTH
        depth = LABEL_DEPTH
        tolerance = 0.000001

        sketch.isComputeDeferred = True
        lines: adsk.fusion.SketchLines = sketch.sketchCurves.sketchLines

        lowerLeft = adsk.core.Point3D.create(0,0,0)
        upperRight = adsk.core.Point3D.create(5,1,0)
        rectLines = sketch.sketchCurves.sketchLines.addTwoPointRectangle(lowerLeft, upperRight)

        profiles1 = adsk.core.ObjectCollection.create()
        profiles1.add(sketch.profiles.item(0))

        extrudeHeight = 0.4
        extrudeHeightValue = adsk.core.ValueInput.createByReal(extrudeHeight)
        ex1 = extrudes.addSimple(profiles1, extrudeHeightValue, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)

        topFace = [face for face in ex1.faces if abs(face.centroid.z - extrudeHeight) <= tolerance][0]

        planeInput = planes.createInput()
        planeInput.setByOffset(topFace, adsk.core.ValueInput.createByReal(0.0))
        textPlane = planes.add(planeInput)

        textSketch: adsk.fusion.Sketch = sketches.add(textPlane)

        texts = adsk.core.ObjectCollection.create()
        textInput = textSketch.sketchTexts.createInput2(f"TX", 0.6)
        p1 = textSketch.sketchPoints.add(lowerLeft)
        p2 = textSketch.sketchPoints.add(upperRight)
        textInput.setAsMultiLine(p1, p2,
                                 adsk.core.HorizontalAlignments.CenterHorizontalAlignment,
                                 adsk.core.VerticalAlignments.MiddleVerticalAlignment, 0)
        text: adsk.fusion.SketchText = textSketch.sketchTexts.add(textInput)
        texts.add(text)

        outward = False
        if outward: # works
            textHeight = 0.4
            textHeightValue = adsk.core.ValueInput.createByReal(textHeight)
            ex2 = extrudes.addSimple(texts, textHeightValue, adsk.fusion.FeatureOperations.JoinFeatureOperation)
        else:
            textHeight = 0.2
            textHeightValue = adsk.core.ValueInput.createByReal(-textHeight)
            ex2 = extrudes.addSimple(texts, textHeightValue, adsk.fusion.FeatureOperations.CutFeatureOperation)
            ex3 = extrudes.addSimple(texts, textHeightValue, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
        else:
            print('Failed:\n{}'.format(traceback.format_exc()))

 

 

 I also tried replacing ex3 line with

 

 

            ex3Input = extrudes.createInput(texts, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
            ex3Distance = adsk.fusion.DistanceExtentDefinition.create(textHeightValue)
            ex3Input.setOneSideExtent(ex3Distance, adsk.fusion.ExtentDirections.PositiveExtentDirection)
            print(ex3Distance.isValid)
            print(ex3Input.isValid)
            ex3 = extrudes.add(ex3Input)

 

 

While this shows True for both isValid prints, the result is the same.

 

EDITED:

I also tried to switch the extrusions around i.e. first create the new bodies, then try to cut out, setting participantBodies to the bodiy created by ex1, but I'm getting the same error.

 

I also get the same error when I simply duplicate the ex3 line - what ever this is (Cut or NewBody). The second extrusion with the same profile always fails.

 

If this is an API bug, what would be a good option to work around this?

Thanks

0 Likes
306 Views
2 Replies
Replies (2)
Message 2 of 3

ksautodesk5MA6Q
Explorer
Explorer

My current workaround is to

1. let the text explode into SketchCurves and pass the sketch profiles to the first extrusion.

2. create a new sketch and copy the sketchCurves from first sketch to the new one

3. pass the profiles of the new sketch to the second extrusion.

While this works, it's a bunch of code, looks bad within the design document and I still would like to understand why I can't create a second extrusion with the same profiles using the API (while this is not a problem in the design editor itself).

0 Likes
Message 3 of 3

BrianEkins
Mentor
Mentor

This seems like a bug with the API. I'll see that it's reported. I was able to reverse the order of the extrudes of the text, and the first one worked while the second failed. I think you should be able to re-use the variables you passed in. I tried re-creating the ObjectCollection for the second extrude, but it still had the problem.

 

Here's some code that works around the problem. It extrudes the text to create the first extrusion, but then, instead of creating another extrude to create the cavity portion, it uses the Combine feature to subtract the bodies created by the extrude from the block.

 

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

app: adsk.core.Application = adsk.core.Application.get()

def run(context):
    ui = None
    try:
        for document in app.documents:
            if document.name == os.path.basename(__file__):
                document.close(False)
        doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
        doc.name = os.path.basename(__file__)
        doc.activate()

        design: adsk.core.Product = app.activeProduct
        targetComponent: adsk.fusion.Component = design.rootComponent

        extrudes: adsk.fusion.ExtrudeFeatures = targetComponent.features.extrudeFeatures
        fillets: adsk.fusion.FilletFeatures = targetComponent.features.filletFeatures
        planes: adsk.fusion.ConstructionPlanes = targetComponent.constructionPlanes

        constructionPlane: adsk.fusion.ConstructionPlane = targetComponent.xYConstructionPlane
        sketches: adsk.fusion.Sketches = targetComponent.sketches
        sketch: adsk.fusion.Sketch = sketches.add(constructionPlane)
        baseOrigin: adsk.core.Point3D = sketch.modelToSketchSpace(adsk.core.Point3D.create(0, 0, 0))
        print(f"base origin: {baseOrigin.asArray()}")

        length = 8
        depth = 1
        tolerance = 0.000001

        sketch.isComputeDeferred = True
        lines: adsk.fusion.SketchLines = sketch.sketchCurves.sketchLines

        lowerLeft = adsk.core.Point3D.create(0,0,0)
        upperRight = adsk.core.Point3D.create(5,1,0)
        rectLines = sketch.sketchCurves.sketchLines.addTwoPointRectangle(lowerLeft, upperRight)

        profiles1 = adsk.core.ObjectCollection.create()
        profiles1.add(sketch.profiles.item(0))

        extrudeHeight = 0.4
        extrudeHeightValue = adsk.core.ValueInput.createByReal(extrudeHeight)
        ex1 = extrudes.addSimple(profiles1, extrudeHeightValue, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)

        topFace = [face for face in ex1.faces if abs(face.centroid.z - extrudeHeight) <= tolerance][0]

        planeInput = planes.createInput()
        planeInput.setByOffset(topFace, adsk.core.ValueInput.createByReal(0.0))
        textPlane = planes.add(planeInput)

        textSketch: adsk.fusion.Sketch = sketches.add(textPlane)

        texts = adsk.core.ObjectCollection.create()
        textInput = textSketch.sketchTexts.createInput2(f"TX", 0.6)
        p1 = textSketch.sketchPoints.add(lowerLeft)
        p2 = textSketch.sketchPoints.add(upperRight)
        textInput.setAsMultiLine(p1, p2,
                                 adsk.core.HorizontalAlignments.CenterHorizontalAlignment,
                                 adsk.core.VerticalAlignments.MiddleVerticalAlignment, 0)
        text: adsk.fusion.SketchText = textSketch.sketchTexts.add(textInput)
        texts.add(text)

        outward = False
        if outward: # works
            textHeight = 0.4
            textHeightValue = adsk.core.ValueInput.createByReal(textHeight)
            ex2 = extrudes.addSimple(texts, textHeightValue, adsk.fusion.FeatureOperations.JoinFeatureOperation)
        else:
            textHeight = 0.2
            textHeightValue = adsk.core.ValueInput.createByReal(-textHeight)
            ex3 = extrudes.addSimple(texts, textHeightValue, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)

        combines = targetComponent.features.combineFeatures
        toolBodies = adsk.core.ObjectCollection.create()
        for body in ex3.bodies:
            toolBodies.add(body)
        combineInput = combines.createInput(ex1.bodies[0], toolBodies)
        combineInput.isKeepToolBodies = True
        combineInput.operation = adsk.fusion.FeatureOperations.CutFeatureOperation
        combine = combines.add(combineInput)
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
        else:
            print('Failed:\n{}'.format(traceback.format_exc()))

 

 

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com