How do you pass selections to cam operation geometery selection?

How do you pass selections to cam operation geometery selection?

andy_hughesSG9EM
Observer Observer
193 Views
5 Replies
Message 1 of 6

How do you pass selections to cam operation geometery selection?

andy_hughesSG9EM
Observer
Observer

I've attempted to follow this guide but I've yet to get it to work. I am wanting to automatically find and select the top face of my stock body and then pass it to my Face operation. 

Holding shift over the selection button reveals the parameter name to be "sel_face_contours". However I keep getting this error:

---------------------------
TestScript
---------------------------
Failed:
Traceback (most recent call last):
File "TestScript.py", line 48, in run
contourParam: adsk.cam.CadContours2dParameterValue = op.parameters.itemByName('sel_face_contours').value
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'value'

---------------------------
OK
---------------------------

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

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface
        doc = app.activeDocument
        product = doc.products.itemByProductType('DesignProductType')
        design = adsk.fusion.Design.cast(product)
        rootComp = design.rootComponent

        # Find the BRepBody named "Stock"
        targetBody = None
        
        # Check for the BRepBody in the root component
        for body in rootComp.bRepBodies:
            if body.name == 'Stock':
                targetBody = body
                break

        # Find the planar face with the minimum Y coordinate
        min_y_face = None
        min_y = float('inf')

        for face in targetBody.faces:
            # Check for planar faces using the corrected enum: PlaneSurfaceType
            if face.geometry.surfaceType == adsk.core.SurfaceTypes.PlaneSurfaceType:
                bbox = face.boundingBox
                
                # Check the minimum Y-coordinate of the face's bounding box
                face_min_y = bbox.minPoint.y
                
                if face_min_y < min_y:
                    min_y = face_min_y
                    min_y_face = face

        # Get the CAM Product and the specific Setup/Operation
        cam_product = doc.products.itemByProductType('CAMProductType')
        cam: adsk.cam.CAM = adsk.cam.CAM.cast(cam_product)
        setup: adsk.cam.Setup = cam.setups.itemByName('Charles') 
        op: adsk.cam.Operation = setup.operations.itemByName('Facinghead')
    
        # Get the CadContours2dParameterValue object
        contourParam: adsk.cam.CadContours2dParameterValue = op.parameters.itemByName('sel_face_contours').value

        # Get the CurveSelections object and clear any previous selections
        curveSelections = contourParam.getCurveSelections()
        curveSelections.clear() 

        # Create a new face contour selection
        faceContourSel: adsk.cam.FaceContourSelection = curveSelections.createNewFaceContourSelection()

        # Set selection properties (common for stock contour)
        faceContourSel.isSelectingSamePlaneFaces = False
        faceContourSel.inputGeometry = [min_y_face]
        contourParam.applyCurveSelections(curveSelections)

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

 

0 Likes
194 Views
5 Replies
Replies (5)
Message 2 of 6

kandennti
Mentor
Mentor

Hi @andy_hughesSG9EM -San.

I may be mistaken, but I believe the parameter name is “stockContours”.
pic.png

0 Likes
Message 3 of 6

andy_hughesSG9EM
Observer
Observer

You are correct. I was finally able to get something to work.

try:
        for body in rootComp.bRepBodies:
            if body.name == 'Stock':
                stock_body = body

        # 1. Collect all "flat" faces (planes) into a list
        flat_faces = []

        for face in stock_body.faces:
            bb = face.boundingBox
            
            # Check for flat face (perpendicular to Y)
            if bb.maxPoint.y == bb.minPoint.y:
                flat_faces.append(face)

        # 2. Identify the specific face with the lowest minPoint.y
        bottom_face = None # Initialize the variable

        if flat_faces:
            # Use the min() function to find the face object with the smallest minPoint.y
            bottom_face = min(flat_faces, key=lambda f: f.boundingBox.minPoint.y)

        setup = cam.setups.itemByName('Charles')
        op = setup.operations.itemByName('Facinghead')

        # --- Get the stockContours parameter ---
        contourParam: adsk.cam.CadContours2dParameterValue = op.parameters.itemByName('stockContours').value
        curveSelections = contourParam.getCurveSelections()
        fc: adsk.cam.FaceContourSelection = curveSelections.createNewFaceContourSelection()
        fc.isSelectingSamePlaneFaces = True
        fc.loopType = adsk.cam.LoopTypes.AllLoops
        fc.sideType = adsk.cam.SideTypes.StartOutsideSideType
        fc.inputGeometry = [bottom_face]  
        contourParam.applyCurveSelections(curveSelections)
        cam.generateToolpath(op)
     
    except:
        ui.messageBox(f"selectGeometery(): failed:\n{traceback.format_exc()}")
Message 4 of 6

andy_hughesSG9EM
Observer
Observer

How do I access these parameters?

andy_hughesSG9EM_0-1761251422105.png

 

0 Likes
Message 5 of 6

Jorge_Jaramillo
Collaborator
Collaborator

Hi,

 

Every selection group is modeled as an "adsk.cam.ChainSelection" object.

You can find its properties here: https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-FCF7FFDA-7107-4E80-AE89-73C8F5413E5F

 

Regards,

Jorge Jaramillo

 

Message 6 of 6

andy_hughesSG9EM
Observer
Observer

Awesome! Thank you Jorge!

0 Likes