Can't create a sweep feature from a translated+rotated face?

Can't create a sweep feature from a translated+rotated face?

JesusFreke
Advocate Advocate
305 Views
0 Replies
Message 1 of 1

Can't create a sweep feature from a translated+rotated face?

JesusFreke
Advocate
Advocate

I'm playing around with creating sweep features, and I'm having some inconsistent results. Below is a specific example where I believe the sweep should succeed, but it fails with a feature timeline warning of "Tool body creation failed.".

 

import adsk.core
import adsk.fusion
import math

app = adsk.core.Application.get()
design = app.activeProduct
root = app.activeProduct.rootComponent
brep = adsk.fusion.TemporaryBRepManager.get()


def create_component(parent_component, *bodies):
    new_occurrence = parent_component.occurrences.addNewComponent(adsk.core.Matrix3D.create())
    base_feature = new_occurrence.component.features.baseFeatures.add()
    base_feature.startEdit()
    for body in bodies:
        new_occurrence.component.bRepBodies.add(body, base_feature)
    base_feature.finishEdit()
    return new_occurrence


def rect(x, y):
    curves = [
        adsk.core.Line3D.create(
            adsk.core.Point3D.create(0, 0, 0),
            adsk.core.Point3D.create(x, 0, 0)
        ),
        adsk.core.Line3D.create(
            adsk.core.Point3D.create(x, 0, 0),
            adsk.core.Point3D.create(x, y, 0)
        ),
        adsk.core.Line3D.create(
            adsk.core.Point3D.create(x, y, 0),
            adsk.core.Point3D.create(0, y, 0)
        ),
        adsk.core.Line3D.create(
            adsk.core.Point3D.create(0, y, 0),
            adsk.core.Point3D.create(0, 0, 0)
        )
    ]
    wire, _ = brep.createWireFromCurves(curves)
    face = brep.createFaceFromPlanarWires([wire])

    return create_component(root, face)


def run(context):
    # create a new rectangular face in the xy plane with the lower left corner on the origin
    face = rect(1, 1)
    total_matrix = adsk.core.Matrix3D.create()
    temp_matrix = adsk.core.Matrix3D.create()
    temp_matrix.translation = adsk.core.Vector3D.create(-.5, -.5, 0)
    # center the face on the origin
    total_matrix.transformBy(temp_matrix)
    temp_matrix = adsk.core.Matrix3D.create()
    temp_matrix.setToRotation(math.radians(90), adsk.core.Vector3D.create(1, 0, 0), adsk.core.Point3D.create(0, 0, 0))
    # rotate about x axis by 90 degrees, so the face is vertical
    total_matrix.transformBy(temp_matrix)
    temp_matrix = adsk.core.Matrix3D.create()
    temp_matrix.translation = adsk.core.Vector3D.create(10, 0, 0)
    # move the face so its center is at (10, 0, 0)
    total_matrix.transformBy(temp_matrix)
    face.transform = total_matrix
    design.snapshots.add()

    # create a vertical helix that starts at (10, 0, 0), and is perpendicular to the above face
    helix = brep.createHelixWire(
        adsk.core.Point3D.create(0, 0, 0),
        adsk.core.Vector3D.create(0, 0, 1),
        adsk.core.Point3D.create(10, 0, 0),
        5,
        2,
        0)
    helix_body = create_component(root, helix)

    sweep_input = root.features.sweepFeatures.createInput(
        face.bRepBodies[0].faces[0],
        adsk.fusion.Path.create(helix_body.bRepBodies[0].edges[0],
                                adsk.fusion.ChainedCurveOptions.noChainedCurves),
        adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
    root.features.sweepFeatures.add(sweep_input)

But if I directly create the face in the correct position/orientation, the sweep succeeds:

 

import adsk.core
import adsk.fusion
import math

app = adsk.core.Application.get()
design = app.activeProduct
root = app.activeProduct.rootComponent
brep = adsk.fusion.TemporaryBRepManager.get()


def create_component(parent_component, *bodies):
    new_occurrence = parent_component.occurrences.addNewComponent(adsk.core.Matrix3D.create())
    base_feature = new_occurrence.component.features.baseFeatures.add()
    base_feature.startEdit()
    for body in bodies:
        new_occurrence.component.bRepBodies.add(body, base_feature)
    base_feature.finishEdit()
    return new_occurrence


def rect(x, y):
    curves = [
        adsk.core.Line3D.create(
            adsk.core.Point3D.create(10-x/2, 0, -y/2),
            adsk.core.Point3D.create(10+x/2, 0, -y/2)
        ),
        adsk.core.Line3D.create(
            adsk.core.Point3D.create(10+x/2, 0, -y/2),
            adsk.core.Point3D.create(10+x/2, 0, y/2)
        ),
        adsk.core.Line3D.create(
            adsk.core.Point3D.create(10+x/2, 0, y/2),
            adsk.core.Point3D.create(10-x/2, 0, y/2)
        ),
        adsk.core.Line3D.create(
            adsk.core.Point3D.create(10-x/2, 0, y/2),
            adsk.core.Point3D.create(10-x/2, 0, -y/2)
        )
    ]
    wire, _ = brep.createWireFromCurves(curves)
    face = brep.createFaceFromPlanarWires([wire])

    return create_component(root, face)


def run(context):
    face = rect(1, 1)

    # create a vertical helix that starts at (10, 0, 0), and is perpendicular to the above face
    helix = brep.createHelixWire(
        adsk.core.Point3D.create(0, 0, 0),
        adsk.core.Vector3D.create(0, 0, 1),
        adsk.core.Point3D.create(10, 0, 0),
        5,
        2,
        0)
    helix_body = create_component(root, helix)

    sweep_input = root.features.sweepFeatures.createInput(
        face.bRepBodies[0].faces[0],
        adsk.fusion.Path.create(helix_body.bRepBodies[0].edges[0],
                                adsk.fusion.ChainedCurveOptions.noChainedCurves),
        adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
    root.features.sweepFeatures.add(sweep_input)

As a workaround in the first case, I've also discovered that I can use the TemporaryBRepManager to create a copy of the face and add it into a new component, and then the sweep will work with the new copy. So I'm guessing it has something to do with using a face in a transformed Occurrence.

0 Likes
306 Views
0 Replies
Replies (0)