[InternalValidationError] Get RuntimeError when using custom joint direction axis

[InternalValidationError] Get RuntimeError when using custom joint direction axis

NuofanQiu
Enthusiast Enthusiast
1,212 Views
7 Replies
Message 1 of 8

[InternalValidationError] Get RuntimeError when using custom joint direction axis

NuofanQiu
Enthusiast
Enthusiast

I am trying to create a revolute joint using custom joint axis, but I got an error:

RuntimeError: 2 : InternalValidationError : Utils::getObjectPath(object, objPath, nullptr, context)

And I searched at this forum, here are the relative posts:

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/copy-body-and-move-by-code/m-p/10554409

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/trouble-scaling-after-copying-and-moving-c...

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/add-rectangular-pattern-feature-to-compone...

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/rotate-imported-step-file-using-api-fully-...

But I did not get a solution from the posts above.

 

Here are my script:

#Author-
#Description-

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

def generateBox(x, y, z, rootCom: adsk.fusion.Component) -> adsk.fusion.Component:
    """
    generate a box component inside rootComponent
    x, y, z unit: m

    return: component generated by extrude object
    """

    extrudes = rootCom.features.extrudeFeatures
    x = x*100.0 # convert from m to cm
    y = y*100.0
    z = z*100.0

    # Create a new sketch on the xy plane
    sketches = rootCom.sketches
    xyPlane = rootCom.xYConstructionPlane
    sketch = sketches.add(xyPlane)

    # Draw a rectangle on the xy plane
    point1 = adsk.core.Point3D.create(-x/2, y/2, 0.0)
    point2 = adsk.core.Point3D.create(x/2, y/2, 0.0)
    point3 = adsk.core.Point3D.create(x/2, -y/2, 0.0)
    point4 = adsk.core.Point3D.create(-x/2, -y/2, 0.0)

    sketch_lines = sketch.sketchCurves.sketchLines
    line1 = sketch_lines.addByTwoPoints(point1, point2)
    line2 = sketch_lines.addByTwoPoints(point2, point3)
    line3 = sketch_lines.addByTwoPoints(point3, point4)
    line4 = sketch_lines.addByTwoPoints(point4, point1)

    # Get the profile defined by the four lines
    prof = sketch.profiles.item(0)

    # Define the distance to extrude
    distance = adsk.core.ValueInput.createByReal(z)
    
    # extrude = extrudes.addSimple(prof, distance, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
    extrude = extrudes.addSimple(prof, distance, adsk.fusion.FeatureOperations.NewComponentFeatureOperation)
    
    return extrude.parentComponent


def rotateZ(degree)->adsk.core.Matrix3D:
    """
    retrun a transform matrix to rotate about world frame's z-axis with degree
    """
    rotZ = adsk.core.Matrix3D.create()
    rotZ.setCell(0, 0, math.cos(math.radians(degree)))
    rotZ.setCell(1, 0, math.sin(math.radians(degree)))
    rotZ.setCell(2, 0, 0.0)
    rotZ.setCell(3, 0, 0.0)

    rotZ.setCell(0, 1, -math.sin(math.radians(degree)))
    rotZ.setCell(1, 1, math.cos(math.radians(degree)))
    rotZ.setCell(2, 1, 0.0)
    rotZ.setCell(3, 1, 0.0)

    rotZ.setCell(0, 2, 0.0)
    rotZ.setCell(1, 2, 0.0)
    rotZ.setCell(2, 2, 1.0)
    rotZ.setCell(3, 2, 0.0)

    rotZ.setCell(0, 3, 0.0)
    rotZ.setCell(1, 3, 0.0)
    rotZ.setCell(2, 3, 0.0)
    rotZ.setCell(3, 3, 1.0)


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

        textPalette = ui.palettes.itemById("TextCommands")
        if not textPalette.isVisible:
            textPalette.isVisible = True

        # doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)
        # design.designType = adsk.fusion.DesignTypes.ParametricDesignType
        # design.designType = adsk.fusion.DesignTypes.DirectDesignType

        rootComp = design.rootComponent
        extBox = generateBox(0.1, 0.1, 1, rootComp)
        # textPalette.writeText("num of bRepBodies: {}".format(extBox.bRepBodies.count))
        extBody = extBox.bRepBodies.item(0)
        # textPalette.writeText("num of bRepFaces: {}".format(extBody.faces.count))
        extJointFace1 = extBody.faces.item(4) # item 4 is the end extrude face, item 5 is the start extrude face
        point1 = extJointFace1.centroid

        # Define a axis of rotation
        # This can be a linear BRepEdge, ConstructionAxis, or a SketchLine
        sketches1 = extBox.sketches
        sketch = sketches1.add(extJointFace1)
        sketchOrigin = sketch.origin
        sketchOriginPoint = sketch.originPoint
        theta = 30 # degree
        sketchPoint1 = adsk.core.Point3D.create(0.0, 0.0, 0.0)
        sketchPoint2 = adsk.core.Point3D.create(math.cos(math.radians(theta))*10, math.sin(math.radians(theta))*10, 0.0)
        sketchLine = sketch.sketchCurves.sketchLines.addByTwoPoints(sketchPoint1, sketchPoint2)

        # jointGeo = adsk.fusion.JointGeometry.createByCurve(sketchLine, adsk.fusion.JointKeyPointTypes.StartKeyPoint)

        # Create the joint geometry
        jointGeo = adsk.fusion.JointGeometry.createByPlanarFace(extJointFace1, None, adsk.fusion.JointKeyPointTypes.CenterKeyPoint)

        jointOrigins = extBox.jointOrigins
        jointOriginInput = jointOrigins.createInput(jointGeo)
        jointOrigin1 = jointOrigins.add(jointOriginInput)
        # jointXAxis = jointOrigin1.secondaryAxisVector

        extBox2 = generateBox(0.1, 0.1, 0.5, rootComp)
        extBody2 = extBox2.bRepBodies.item(0)
        extJointFace2 = extBody2.faces.item(4)
        jointGeo2 = adsk.fusion.JointGeometry.createByPlanarFace(extJointFace2, None, adsk.fusion.JointKeyPointTypes.CenterKeyPoint)
        jointOrigins2 = extBox2.jointOrigins
        jointOriginInput2 = jointOrigins.createInput(jointGeo2)
        jointOrigin2 = jointOrigins2.add(jointOriginInput2)

        # Create jont
        joints = rootComp.joints
        jointInput = joints.createInput(jointOrigin1, jointOrigin2)
        # jointInput.setAsRevoluteJointMotion(adsk.fusion.JointDirections.XAxisJointDirection)
        jointInput.setAsRevoluteJointMotion(adsk.fusion.JointDirections.CustomJointDirection, sketchLine)
        joint1 = joints.add(jointInput)


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

In the script, I first generate two box and set up joint origins, then I draw a sketch line as the custom joint axis. After that, I joint them together, but got an error.

 

So, how to set up a custom joint axis for a revolute joint?

Thanks a lot.

 

0 Likes
Accepted solutions (1)
1,213 Views
7 Replies
Replies (7)
Message 2 of 8

kandennti
Mentor
Mentor

Hi @NuofanQiu -San.

 

I do not understand what you want to do.
Could you please attach an f3d file with GUI manipulation of what state you want to achieve?

Message 3 of 8

kandennti
Mentor
Mentor
Accepted solution

@NuofanQiu -San.

 

I could not use sketchLine on the rotational axis of the joint even though I manipulated it in the GUI.
Therefore, we tried using ConstructionAxis.

・・・
        # Create jont
        joints = rootComp.joints
        jointInput = joints.createInput(jointOrigin1, jointOrigin2)
        # jointInput.setAsRevoluteJointMotion(adsk.fusion.JointDirections.XAxisJointDirection)
        # jointInput.setAsRevoluteJointMotion(adsk.fusion.JointDirections.CustomJointDirection, sketchLine)

        axisLine: adsk.fusion.ConstructionAxis = get_revolute_axis(rootComp, sketchPoint1, sketchPoint2)
        jointInput.setAsRevoluteJointMotion(adsk.fusion.JointDirections.CustomJointDirection, axisLine)

        joint1 = joints.add(jointInput)

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


def get_revolute_axis(
    comp: adsk.fusion.Component,
    sPoint: adsk.core.Point3D,
    ePoint: adsk.core.Point3D,
) -> adsk.fusion.ConstructionAxis:

    app: adsk.core.Application = adsk.core.Application.get()
    des: adsk.fusion.Design = app.activeProduct

    vec: adsk.core.Vector3D = sPoint.vectorTo(ePoint)
    vec.normalize()

    infinite: adsk.core.InfiniteLine3D = adsk.core.InfiniteLine3D.create(
        sPoint,
        vec,
    )

    baseFeat: adsk.fusion.BaseFeature = None
    if des.designType == adsk.fusion.DesignTypes.ParametricDesignType:
        baseFeat = comp.features.baseFeatures.add()

    axes: adsk.fusion.ConstructionAxes = comp.constructionAxes
    axisIpt: adsk.fusion.ConstructionAxisInput = axes.createInput()
    axis: adsk.fusion.ConstructionAxis = None
    if baseFeat:
        try:
            baseFeat.startEdit()
            axisIpt.setByLine(infinite)
            axis = axes.add(axisIpt)
        except:
            pass
        finally:
            baseFeat.finishEdit()
    else:
        axisIpt.setByLine(infinite)
        axis = axes.add(axisIpt)

    return axis
Message 4 of 8

NuofanQiu
Enthusiast
Enthusiast

Thank you for your reply.

Actually what I want to do have post in this post: https://forums.autodesk.com/t5/fusion-360-api-and-scripts/how-to-create-a-custom-joint-direction-for...

 

I found it is not easy to create a custom joint axis for a revolute joint even use GUI(maybe I am not familiar with Fusion360).

I found the API description here:

NuofanQiu_0-1688393954865.png

A linear edge, planar face and cylindrical face seems not suitable for the Bennett mechanism, so I tried to use the sketch line as the custom rotation axis.

 

I will try your method right now! And give you a response as soon as possible.

Thanks again! You are so nice!

Message 5 of 8

NuofanQiu
Enthusiast
Enthusiast
Hi @kandennti -San (I just learned 'San' is a respectful title in Japanese)
You code is elegant and easy to understand. I think it solved my problem(although I haven't finished it yet).

I think you are very experienced in Fusion360 API. Do you have any suggestion or tutorial recommendation for learning Fusion360 API? I have read the introduction in Fusion360 API web-site and struggling with Fusion360 API.
I found that you have write several post about Fusion360API on your blog. I will read them. But I do not understand Japanese(I will try some translator), do you have any good English resources that can help me for learning Fusion360 API?
Thanks a lot!
0 Likes
Message 6 of 8

kandennti
Mentor
Mentor

@NuofanQiu -San.

 

I have not seen any solid tutorials.
The content I do know about is online documentation and @prainsberry -San here.

https://github.com/tapnair/Fusion360APIClass 

 

Also, this forum is a big help.

Message 7 of 8

NuofanQiu
Enthusiast
Enthusiast
@kandennti -San
Again, thank you so much!
Message 8 of 8

huzq1
Observer
Observer

Hi, I try this way, but still have runtime error when I  add ConstructionAxis to a sub component(to root component is  ok). do you know why? Thank you!

0 Likes