Problem extruding multiple faces in a row

Problem extruding multiple faces in a row

tiktuk
Advocate Advocate
322 Views
2 Replies
Message 1 of 3

Problem extruding multiple faces in a row

tiktuk
Advocate
Advocate

Hi,

 

I'm trying to make a box smaller by extruding each of its faces inwards (cutting) by different amounts. I let the user select each face first (can hopefully automate most of this selection work later) and the do the extrudes on the faces. The problem is that after one or a few extrudes I get an error:

 

RuntimeError: 2 : InternalValidationError : input

 

The strange thing is that it varies how many extrudes that succeeds, even for the same input values and selections.

 

Any ideas?

 

Here is the code:

 

#Author-
#Description-

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


def extrude_face(
    face: adsk.fusion.BRepFace,
    distanceExpression: str,
    name: str = ''
) -> adsk.fusion.ExtrudeFeature:
    """
    Make a box smaller by extruding the sides inwards.
    """
    app = adsk.core.Application.get()
    design = app.activeProduct
    rootComp = design.rootComponent

    distance = adsk.core.ValueInput.createByString(distanceExpression)

    # Create the extrusion.
    extrudes: adsk.fusion.ExtrudeFeatures = rootComp.features.extrudeFeatures
    ext = extrudes.addSimple(
        face, distance, adsk.fusion.FeatureOperations.CutFeatureOperation
    )
    ext.name = name

    return ext


def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface

        faces = []

        faces.append(
            ui.selectEntity('Select the front face', 'PlanarFaces').entity
        )

        faces.append(
            ui.selectEntity('Select the top face', 'PlanarFaces').entity
        )

        faces.append(
            ui.selectEntity('Select the back face', 'PlanarFaces').entity
        )

        faces.append(
            ui.selectEntity('Select the bottom face', 'PlanarFaces').entity
        )

        faces.append(
            ui.selectEntity('Select the left face', 'PlanarFaces').entity
        )

        faces.append(
            ui.selectEntity('Select the right face', 'PlanarFaces').entity
        )

        extrude_face(
            faces[0], '-10 mm', name='Front'
        )
        extrude_face(faces[1], '-10 mm', name='Top')
        extrude_face(faces[2], '-20 mm', name='Back')
        extrude_face(
            faces[3], '-10 mm', name='Bottom'
        )
        extrude_face(faces[4], '-15 mm', name='Left')
        extrude_face(
            faces[5], '-5 mm', name='Right'
        )

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

 

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

kandennti
Mentor
Mentor

Hi @tiktuk -San.

 

I think if I select all and extrude later, I lose the second face after the first extrusion.
I rewrote the run function to extrude each time a face is selected.

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface

        dataLst = [
            ("Front", "-10 mm"),
            ("Top", "-10 mm"),
            ("Back", "-20 mm"),
            ("Bottom", "-10 mm"),
            ("Left", "-15 mm"),
            ("Right", "-5 mm"),
        ]

        for name, valueStr in dataLst:
            try:
                face: adsk.fusion.BRepFace = ui.selectEntity(
                    f"Select the {name.lower()} face", 
                    "PlanarFaces"
                ).entity

                extrude_face(face, valueStr, name)
            except:
                break

        ui.messageBox("Done")

    except:
        if ui:
            ui.messageBox("Failed:\n{}".format(traceback.format_exc()))
Message 3 of 3

tiktuk
Advocate
Advocate

Thanks, @kandennti, that is a good workaround 🙂