How to add pocket operation from simply closed profile ?

How to add pocket operation from simply closed profile ?

maurizio_manzi
Advocate Advocate
1,088 Views
11 Replies
Message 1 of 12

How to add pocket operation from simply closed profile ?

maurizio_manzi
Advocate
Advocate
Hello,
I create a new sketch and add a simply closed profile to it.
How can I now add a pocket operation, that is using this profile as his geometry ?
Best regards
Maurizio
 
sketch = sketches.add(occComp.xYConstructionPlane) # Add new sketch
lines = sketch.sketchCurves.sketchLines # Cast lines in new sketch
circles = sketch.sketchCurves.sketchCircles # Cast circles in new sketch
arcs = sketch.sketchCurves.sketchArcs # Cast arcs in new sketch
# Create sketch for rectangular pocket:
topZ = -0.4/fPmm # Top Z-value for the rectangular pocket sketch
line1 = lines.addByTwoPoints(adsk.core.Point3D.create(1.25/fPmm, -0.25/fPmm, topZ), adsk.core.Point3D.create(1.25/fPmm, 0.25/fPmm, topZ)) # line1
arc1 = arcs.addByCenterStartSweep(adsk.core.Point3D.create(0.25/fPmm, 0.25/fPmm, topZ), line1.endSketchPoint, math.radians(90)) # arc1
line2 = lines.addByTwoPoints(arc1.endSketchPoint, adsk.core.Point3D.create(-0.25/fPmm, 1.25/fPmm, topZ)) # line2
arc2 = arcs.addByCenterStartSweep(adsk.core.Point3D.create(-0.25/fPmm, 0.25/fPmm, topZ), line2.endSketchPoint, math.radians(90)) # arc2
line3 = lines.addByTwoPoints(arc2.endSketchPoint, adsk.core.Point3D.create(-1.25/fPmm, -0.25/fPmm, topZ)) # line3
arc3 = arcs.addByCenterStartSweep(adsk.core.Point3D.create(-0.25/fPmm, -0.25/fPmm, topZ), line3.endSketchPoint, math.radians(90)) # arc3
line4 = lines.addByTwoPoints(arc3.endSketchPoint, adsk.core.Point3D.create(0.25/fPmm, -1.25/fPmm, topZ)) # line4
 arc4 = arcs.addByCenterStartSweep(adsk.core.Point3D.create(0.25/fPmm, -0.25/fPmm, topZ), line4.endSketchPoint, math.radians(90)) # arc4

prof = sketch.profiles.item(0) # Get profile
0 Likes
Accepted solutions (3)
1,089 Views
11 Replies
Replies (11)
Message 2 of 12

kandennti
Mentor
Mentor

Hi @maurizio_manzi .

 

I interpreted "pocket" as this kind of operation.

1.png
Here is a sample script.

# Fusion360API Python script

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

def run(context):
    ui: core.UserInterface = None
    try:
        app: core.Application = core.Application.get()
        ui = app.userInterface
        app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)

        des: fusion.Design = app.activeProduct
        root: fusion.Component = des.rootComponent

        des.designType = fusion.DesignTypes.DirectDesignType
        create_box(root)
        des.designType = fusion.DesignTypes.ParametricDesignType

        occ: fusion.Occurrence = root.occurrences.addNewComponent(
            core.Matrix3D.create()
        )
        occComp: fusion.Component = occ.component
        sketches: fusion.Sketches = occComp.sketches
        fPmm = 1

        # **********

        sketch = sketches.add(occComp.xYConstructionPlane) # Add new sketch
        lines = sketch.sketchCurves.sketchLines # Cast lines in new sketch
        circles = sketch.sketchCurves.sketchCircles # Cast circles in new sketch
        arcs = sketch.sketchCurves.sketchArcs # Cast arcs in new sketch
        # Create sketch for rectangular pocket:
        topZ = -0.4/fPmm # Top Z-value for the rectangular pocket sketch
        line1 = lines.addByTwoPoints(adsk.core.Point3D.create(1.25/fPmm, -0.25/fPmm, topZ), adsk.core.Point3D.create(1.25/fPmm, 0.25/fPmm, topZ)) # line1
        arc1 = arcs.addByCenterStartSweep(adsk.core.Point3D.create(0.25/fPmm, 0.25/fPmm, topZ), line1.endSketchPoint, math.radians(90)) # arc1
        line2 = lines.addByTwoPoints(arc1.endSketchPoint, adsk.core.Point3D.create(-0.25/fPmm, 1.25/fPmm, topZ)) # line2
        arc2 = arcs.addByCenterStartSweep(adsk.core.Point3D.create(-0.25/fPmm, 0.25/fPmm, topZ), line2.endSketchPoint, math.radians(90)) # arc2
        line3 = lines.addByTwoPoints(arc2.endSketchPoint, adsk.core.Point3D.create(-1.25/fPmm, -0.25/fPmm, topZ)) # line3
        arc3 = arcs.addByCenterStartSweep(adsk.core.Point3D.create(-0.25/fPmm, -0.25/fPmm, topZ), line3.endSketchPoint, math.radians(90)) # arc3
        line4 = lines.addByTwoPoints(arc3.endSketchPoint, adsk.core.Point3D.create(0.25/fPmm, -1.25/fPmm, topZ)) # line4
        arc4 = arcs.addByCenterStartSweep(adsk.core.Point3D.create(0.25/fPmm, -0.25/fPmm, topZ), line4.endSketchPoint, math.radians(90)) # arc4

        prof = sketch.profiles.item(0) # Get profile

        # **********
        # create pocket
        extrudeFeats: fusion.ExtrudeFeatures = occComp.features.extrudeFeatures
        extrudeFeats.addSimple(
            prof,
            core.ValueInput.createByReal(-1),
            fusion.FeatureOperations.CutFeatureOperation,
        )

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

def create_box(
    comp: fusion.Component
) -> fusion.BRepBody:

    bBox: core.OrientedBoundingBox3D = core.OrientedBoundingBox3D.create(
        core.Point3D.create(0,0,-2.9),
        core.Vector3D.create(0,1,0),
        core.Vector3D.create(1,0,0),
        5,
        5,
        5,
    )

    tmpMgr: fusion.TemporaryBRepManager = fusion.TemporaryBRepManager.get()
    box: fusion.BRepBody = tmpMgr.createBox(bBox)

    return comp.bRepBodies.add(box)
0 Likes
Message 3 of 12

maurizio_manzi
Advocate
Advocate

Hi,

thnak you for your answer.

Sorry, I mean a CAM (Manufacturate) operation from type "pocket2D":

setup.operations.createInput('pocket2d')
 
Best regards
Maurizio
 
0 Likes
Message 4 of 12

echatzief
Advocate
Advocate

Hi @maurizio_manzi 

The best way, in my opinion, to create pocket operations to your setups is assigning an object collection (adsk.core.objectCollection) to your CadContours2dParameterValue.

I personally, am adding the face (adsk.fusion.bRepFace) to the above mentioned obj collection first.

I am attaching you a code preview, to get familiar.

# selClosedColl --> object collection
# targetBRepFace --> back face of the pocket you want to do the operation on
selClosedColl.add(targetBRepFace)

# ASSIGNING FACES TO POCKET OPERATIONS
selPokcets_ = pocketOps.parameters.itemByName("pockets") 
# selPockets -->CamParameter value
valSelPock_ : adsk.cam.CadContours2dParameterValue = selPokcets_.value 
# valSelPock --> CadContours2DParameter Value
curSelections_ = valSelPock_.getCurveSelections() 
# curSelections_ --> CurveSelections
chSel_ = curSelections_.createNewFaceContourSelection()
chSel_._set_inputGeometry(selClosedColl)
valSelPock_.applyCurveSelections(curSelections_) 
selClosedColl.clear()

All you have to do then is change your tool orientation axis to one matching your case.

Thank you, hopping this should help.

 

0 Likes
Message 5 of 12

maurizio_manzi
Advocate
Advocate

Hello,
It's not possible in this AddIn to select any think about the user.
The AddIn creates many profiles with various tool orientations.
Any one of this profiles must by added as pocket2D-Operation.

I think, I must first create a face from the profile, than the pocket2D-Operation from the face.

But I have no idea how to proceed..

Best regards

Maurizio

 

0 Likes
Message 6 of 12

kandennti
Mentor
Mentor

@maurizio_manzi .

 

The documentation states that cam.ChainSelection only allows BRepEdges or sketch geometry, so no profile could be selected.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-FCF7FFDA-7107-4E80-AE89-73C8F5413E5F 

 

・・・
        pocketOpe: cam.Operation = setup.operations[0]

        pocketPrm: cam.CAMParameter = pocketOpe.parameters.itemByName("pockets")
        cadObjPrm: cam.CadContours2dParameterValue = pocketPrm.value
        crvSels: cam.CurveSelections = cadObjPrm.getCurveSelections()
        crvSels.clear()

        # https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-FCF7FFDA-7107-4E80-AE89-73C8F5413E5F
        crvSel: cam.ChainSelection = crvSels.createNewChainSelection()
        # crvSel.inputGeometry = [prof] #NG
        crvSel.inputGeometry = [arc4]

        cadObjPrm.applyCurveSelections(crvSels)

        camObj.generateToolpath(pocketOpe)
・・・
0 Likes
Message 7 of 12

kate.qi
Alumni
Alumni
Accepted solution
# Create 2D pocket operation.
operationInput = setup.operations.createInput('pocket2d')
operationInput.tool = tool
op_2Dpocket = setup.operations.add(operationInput)
cadcontours = op_2Dpocket.parameters.itemByName('pockets').value
selection = cadcontours.getCurveSelections()
# If you want select sketch profile as cutting area:
# getting sketch
sketch = cam.designRootOccurrence.component.sketches.item(0)
# getting sketch profile.
Geometry = [sketch.sketchCurves.item(0)]
# Use chain selection
chain = selection.createNewChainSelection()
chain.inputGeometry = Geometry
# The default is closed chain,so just apply the chain.
cadcontours.applyCurveSelections(selection)
cam.generateToolpath(op_2Dpocket)
 
Hope this can help you.
 
Mengjiao Qi
 
 
Message 8 of 12

maurizio_manzi
Advocate
Advocate

Thank you very much. It's very helpfull.

 

0 Likes
Message 9 of 12

maurizio_manzi
Advocate
Advocate

Hello,
The pocket2D-Operation is now created correctly  😀

I'm just trying now to set the correctly tool orientation (code below),

but I get this error message:

"... RuntimeError: 3 : CAD object parameter requires an existing operation for modification.".

Best regards

Maurizio

 

### Code for tool orientation:

cam.generateToolpath(op_2Dpocket) # Generate toolpath
# Enable Tool orientation:
operationInput.parameters.itemByName('overrideToolView').expression = 'true'
operationInput.parameters.itemByName('view_orientation_mode').expression = "'axesZX'"
# seleting my edge for Z Axis:
toolAxis: adsk.cam.CadObjectParameterValue
operationInput.parameters.itemByName("view_orientation_axisZ").value
toolAxis.value = [sketch]
0 Likes
Message 10 of 12

kate.qi
Alumni
Alumni
Accepted solution
"... RuntimeError: 3 : CAD object parameter requires an existing operation for modification.".
--- Please set cad parameters in the operation object rather than operation input object.
Now we have plan to implement the cad parameters setting for operation input object, but still need some time to expose this function to users.
Message 11 of 12

maurizio_manzi
Advocate
Advocate

Thank you very much. It works fine

 

0 Likes
Message 12 of 12

kate.qi
Alumni
Alumni
Accepted solution

For the setting in tool orientation, I don't think it's a good idea to set toolaxis by sketch itself.
If you tried manually, it's forbidden to select sketch from browser tree.
I would suggest to choose toolaxis by the face, edges. Of course, you could select it by sketch curves.
see the script:

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

def createBox(component😞
    size = 4
    # Create sketch
    sketches = component.sketches
    sketch = sketches.add(component.xZConstructionPlane)
    lines = sketch.sketchCurves.sketchLines
    recLines = lines.addTwoPointRectangle(adsk.core.Point3D.create(0, 0, 0), adsk.core.Point3D.create(size, size, 0))

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

    # Create an extrusion inpu
    extrudes = component.features.extrudeFeatures
    extInput = extrudes.createInput(prof, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
    distance = adsk.core.ValueInput.createByReal(size)
    extInput.setDistanceExtent(False, distance)

    # Create the extrusion.
    ext = extrudes.add(extInput)

    return ext.bodies.item(0)

def createSetup(cam, name, opType, models, machine = None😞
    setups = cam.setups
    input = setups.createInput(opType)
    input.models = models
    input.name = name
    setups.add(input)
    return setups[-1]

def run(context😞
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
        design = app.activeProduct
        # 1. Create a box.
        box = createBox(design.rootComponent)
        camWS = ui.workspaces.itemById('CAMEnvironment')
        camWS.activate()
        cam = adsk.cam.CAM.cast(doc.products.itemByProductType("CAMProductType"))
        # 2. Create a setup
        opType = adsk.cam.OperationTypes.MillingOperation
        setup = createSetup(cam, "Test", opType, [box])
        # Create 2D pocket operation.
        operationInput = setup.operations.createInput('pocket2d')
        camManager = adsk.cam.CAMManager.get()
        libraryManager = camManager.libraryManager
        toolLibraries = libraryManager.toolLibraries
        tools = toolLibraries.toolLibraryAtURL(adsk.core.URL.create('systemlibraryroot://Samples/Milling Tools (Inch).json'))
        tool = tools.item(1)
        operationInput.tool = tool
        # Add Operation
        op_2Dpocket = setup.operations.add(operationInput)
        cadcontours = op_2Dpocket.parameters.itemByName('pockets').value
        selection = cadcontours.getCurveSelections()
        # getting sketch
        sketch = cam.designRootOccurrence.component.sketches.item(0)
        # getting sketch profile.
        Geometry = [sketch.sketchCurves.item(0)]
        # Use chain selection
        chain = selection.createNewChainSelection()
        chain.inputGeometry = Geometry
        # The default is closed chain,so just apply the chain.
        cadcontours.applyCurveSelections(selection)


        # define orientation by the face
        op_2Dpocket.parameters.itemByName('overrideToolView').expression = 'true'
        op_2Dpocket.parameters.itemByName('view_orientation_mode').expression = "'axesZX'"
        toolAxis: adsk.cam.CadObjectParameterValue = op_2Dpocket.parameters.itemByName("view_orientation_axisZ").value
        toolAxis.value = [box.faces.item(5)]
        cam.generateToolpath(op_2Dpocket)


        # # define orientation by the sketch. suggest to use sketch curves.
        # op_2Dpocket.parameters.itemByName('overrideToolView').expression = 'true'
        # op_2Dpocket.parameters.itemByName('view_orientation_mode').expression = "'axesXY'"
        # ValueX = op_2Dpocket.parameters.itemByName("view_orientation_axisX").value
        # ValueX.value = [sketch.sketchCurves.item(0)]
        # op_2Dpocket.parameters.itemByName('view_orientation_flipX').expression = "true"
        # ValueY = op_2Dpocket.parameters.itemByName("view_orientation_axisY").value
        # ValueY.value = [sketch.sketchCurves.item(1)]
        # cam.generateToolpath(op_2Dpocket)

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