Align 2 different body parts and combine them

Align 2 different body parts and combine them

hkhan79SWQ
Explorer Explorer
456 Views
1 Reply
Message 1 of 2

Align 2 different body parts and combine them

hkhan79SWQ
Explorer
Explorer

Greetings,

 

I am new to Fusion 360 API and trying to combine/ align 2 body parts.

 

My understanding is that I will need to create components from the body parts as shown in the image.

 

I want to ask how do I do the following:

  1. Create a component from the body
  2. How do I align these body parts or components to create a pressure vessel? 
 
 
Also, I do not want to select the surfaces to align manually. I want the code to pick the surfaces and align using API.
0 Likes
457 Views
1 Reply
Reply (1)
Message 2 of 2

kandennti
Mentor
Mentor

Hi @hkhan79SWQ .

 

1)This can be done by using the BRepBody.createComponent method.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-f5bcc6ac-fe0d-42c9-8f3c-2df2337cb533 

 

2)Running the following sample script will move the attached f3d file to the state shown in the image.

1.png

 

# Fusion360API Python script

import traceback
import adsk
import adsk.core as core
import adsk.fusion as fusion

def run(context):
    ui: core.UserInterface = None
    try:
        app: core.Application = core.Application.get()
        ui = app.userInterface
        des: fusion.Design = app.activeProduct
        root: fusion.Component = des.rootComponent

        # get body
        capOcc: fusion.Occurrence = root.occurrences.itemByName('Component1:1')
        capNativeBody: fusion.BRepBody = capOcc.component.bRepBodies[0]
        capBody: fusion.BRepBody = capNativeBody.createForAssemblyContext(capOcc)

        cylOcc: fusion.Occurrence = root.occurrences.itemByName('Cylinder:1')
        cylNativeBody: fusion.BRepBody = cylOcc.component.bRepBodies[0]
        cylBody: fusion.BRepBody = cylNativeBody.createForAssemblyContext(cylOcc)

        # find planer face
        capFace: fusion.BRepFace = get_planer_face(capBody)
        if not capFace: return

        cylFace: fusion.BRepFace = get_planer_face(cylBody)
        if not cylFace: return

        # get face matrix
        capMat: core.Matrix3D = get_matrix(capFace)

        cylMat: core.Matrix3D = get_matrix(cylFace)

        # get transform matrix
        transformMat: core.Matrix3D = get_coordinate_transformation_matrix(
            capMat,
            cylMat,
        )

        # transform
        capOcc.transform2 = transformMat
        des.snapshots.add()

        # fin
        ui.messageBox('Done')

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


def get_coordinate_transformation_matrix(
    fromMat: core.Matrix3D,
    toMat: core.Matrix3D,
) -> core.Matrix3D:

    fo, fx, fy, fz = fromMat.getAsCoordinateSystem()
    fx.scaleBy(-1)
    fz.scaleBy(-1)
    to, tx, ty, tz = toMat.getAsCoordinateSystem()

    mat: core.Matrix3D = core.Matrix3D.create()
    mat.setToAlignCoordinateSystems(
        fo, fx, fy, fz,
        to, tx, ty, tz,
    )

    return mat


def get_matrix(
    face: fusion.BRepFace,
) -> core.Matrix3D:

    def get_unique_vector(
        refVec: core.Vector3D,
    ) -> core.Vector3D:

        tmpVec: core.Vector3D = core.Vector3D.create(1,0,0)
        if refVec.isParallelTo(tmpVec):
            tmpVec = core.Vector3D.create(0,1,0)

        return refVec.crossProduct(tmpVec)

    # ****
    geo: core.Plane = face.geometry

    origin: core.Point3D = geo.origin
    zAxis: core.Vector3D = geo.normal
    xAxis: core.Vector3D = get_unique_vector(zAxis)
    yAxis: core.Vector3D = xAxis.crossProduct(zAxis)

    mat: core.Matrix3D = core.Matrix3D.create()
    mat.setWithCoordinateSystem(
        origin,
        xAxis,
        yAxis,
        zAxis,
    )

    return mat


def get_planer_face(
    body: fusion.BRepBody,
) -> fusion.BRepFace:

    planers = [f for f in body.faces if f.geometry.objectType == core.Plane.classType()]
    if len(planers) < 1:
        return None
    else:
        return planers[0]

 

A flat surface was found on the body and moved to match.
It is not clear which side of the cylinder to match, so the user should be allowed to select the side.
Also, I would have liked to see a sample f3d file attached.

0 Likes