FlatPattern Object

FlatPattern Object

PW_VOX
Advocate Advocate
441 Views
4 Replies
Message 1 of 5

FlatPattern Object

PW_VOX
Advocate
Advocate

Is it possible to modify a FlatPattern object from the API, for example create a sketch on a face, extrude cut it out and then export to DXF? 

0 Likes
442 Views
4 Replies
Replies (4)
Message 2 of 5

kandennti
Mentor
Mentor

Hi @PW_VOX .

 

I tried it.

When I run the following script with the attached data active, it modifies the image from left to right and exits.

1.png

 

# Fusion360API Python script

import traceback
import adsk.fusion
import adsk.core

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

        timeline: adsk.fusion.Timeline = des.timeline
        backupMarker = timeline.markerPosition

        # get UnfoldFeature
        unfordFaets: adsk.fusion.UnfoldFeatures = root.features.unfoldFeatures
        if unfordFaets.count < 1:
            return

        unfordFaet: adsk.fusion.UnfoldFeature = unfordFaets[0]
        unfordGroup: adsk.fusion.TimelineGroup = unfordFaet.timelineObject.parentGroup

        # find sketch
        skt: adsk.fusion.Sketch = find_Feature_Of_T(
            unfordGroup,
            adsk.fusion.Sketch.classType()
        )
        if not skt:
            return

        # find ExtrudeFeature
        extrudeFaet: adsk.fusion.ExtrudeFeature = find_Feature_Of_T(
            unfordGroup,
            adsk.fusion.ExtrudeFeature.classType()
        )
        if not extrudeFaet:
            return

        # add sketchCircle
        skt.sketchCurves.sketchCircles.addByCenterRadius(
            adsk.core.Point3D.create(1,2,0),
            1.0
        )
        prof: adsk.fusion.Profile = skt.profiles[-1]

        # update profile
        backupCollapsed = unfordGroup.isCollapsed
        if backupCollapsed:
            unfordGroup.rollTo(True) #Required
            unfordGroup.isCollapsed = False

        extrudeTimeObject: adsk.fusion.TimelineObject = extrudeFaet.timelineObject
        extrudeTimeObject.rollTo(True)

        objs: adsk.core.ObjectCollection = None
        if isinstance(extrudeFaet.profile, adsk.core.ObjectCollection):
            objs = extrudeFaet.profile
        else:
            objs = adsk.core.ObjectCollection.create()
            objs.add(extrudeFaet.profile)
        
        objs.add(prof)
        extrudeFaet.profile = objs

        if backupCollapsed:
            # unfordGroup.rollTo(True) #NG
            timeline.moveToBeginning()
            unfordGroup.isCollapsed = backupCollapsed

        # Return marker position
        timeline.markerPosition = backupMarker


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


def find_Feature_Of_T(
    timelineGroup: adsk.fusion.TimelineGroup,
    T: str) -> adsk.fusion.Feature:

    lst = [tl.entity for tl in timelineGroup if tl.entity.objectType == T]
    if len(lst) < 1:
        return None

    return lst[0]

 

 

If the Unfold group in the timeline is collapsed, it needs to be opened.

1.png

When collapsing the group again, I got an error in the rollTo method, so I moved the timeline marker to the top and collapsed it. The cause was not found.

0 Likes
Message 3 of 5

PW_VOX
Advocate
Advocate

Thanks for ypur effort, I'll study your script and come to you

0 Likes
Message 4 of 5

BrianEkins
Mentor
Mentor

I think this may be what you're looking for. It's using functionality that was new in the previous release to access or create the flat pattern and then creates a sketch and extrusion on the flat pattern model.

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

        # Check if the active component is a sheet metal part.
        if app.activeProduct.productType == 'DesignProductType':
            des: adsk.fusion.Design = app.activeProduct
            activeComp = des.activeComponent

            # Check to see if there is a flat pattern for this component.
            flat: adsk.fusion.FlatPattern = None
            if activeComp.flatPattern:
                flat = activeComp.flatPattern
            else:
                # Check to see if any of the bodies in the component are sheet metal part.
                body: adsk.fusion.BRepBody = None
                smBodies = []
                for body in activeComp.bRepBodies:
                    if body.isSheetMetal:
                        smBodies.append(body)

                if len(smBodies) == 0:
                    ui.messageBox('The active component does not contain a sheet metal body.')
                    return
                elif len(smBodies) == 1:
                    # Create a flat pattern of the body by finding a face to use
                    # for the stationary face and then creating the flat pattern.
                    stationaryFace = GetStationaryFace(smBodies[0])
                    flat = activeComp.createFlatPattern(stationaryFace)
                elif len(smBodies) > 1:
                    ui.messageBox('There is more than one sheet metal part in the active component. Manually create a flat pattern of the one to process.')
                    return
        elif app.activeProduct.productType == 'FlatPatternProductType':
            flatProd: adsk.fusion.FlatPatternProduct = app.activeProduct
            flat = flatProd.flatPattern
        else:
            ui.messageBox('A Design or Flat pattern must be active.')
            return

        flatComp = flat.parentComponent
        flatBody = flat.bodies[0]

        # Create a sketch.
        sk: adsk.fusion.Sketch = flatComp.sketches.addWithoutEdges(flat.topFace)

        # Draw a circle in the center of the flat pattern range box.
        flatBounds = flatBody.boundingBox
        centerX = (flatBounds.minPoint.x + flatBounds.maxPoint.x) / 2
        centerY = (flatBounds.minPoint.y + flatBounds.maxPoint.y) / 2
        center = sk.modelToSketchSpace(adsk.core.Point3D.create(centerX, centerY, 0))
        sk.sketchCurves.sketchCircles.addByCenterRadius(center, 1)

        # Create an extrusion.
        prof = sk.profiles[0]
        extInput = flatComp.features.extrudeFeatures.createInput(prof, adsk.fusion.FeatureOperations.CutFeatureOperation)
        thruAll = adsk.fusion.ThroughAllExtentDefinition.create()
        extInput.setOneSideExtent(thruAll, adsk.fusion.ExtentDirections.NegativeExtentDirection)
        ext = flatComp.features.extrudeFeatures.add(extInput)
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


def GetStationaryFace(body: adsk.fusion.BRepBody) -> adsk.fusion.BRepFace:
    # Find the largest planar face of the body and return it.
    face: adsk.fusion.BRepFace = None
    bigFace: adsk.fusion.BRepFace = None
    bigSize = 0
    for face in body.faces:
        if face.geometry.surfaceType == adsk.core.SurfaceTypes.PlaneSurfaceType:
            if face.area > bigSize:
                bigSize = face.area
                bigFace = face

    return bigFace
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes
Message 5 of 5

PW_VOX
Advocate
Advocate

Thanks Brian, that is exactly what I was looking for.

0 Likes