Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

It seems that I able to find the way to set correctly (at least most of the time) the Z orientation. I paste here my work in progress code hoping that can help someone.

I will try to ask some help  in the API section of the forum.

 

have a good day all

 

import adsk.core, adsk.cam, traceback, os

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        doc = app.activeDocument
        products = doc.products
        design = app.activeProduct
        if not design:
            ui.messageBox('No active Fusion design', 'No Design')
            return

        # Get the CAM product
        cam = adsk.cam.CAM.cast(products.itemByProductType("CAMProductType"))

        # List of all setups
        setups = cam.setups
        
        for setup in setups:
            
            #keep model in the setup
            setupBodies=setup.models

            #check and count programs already exist
            ops=setup.operations
            opsNum=ops.count

            if setupBodies.count>0:
                
                setupFirstBody=setupBodies.item(0)
                (origin, xAxis, yAxis, zAxis) = setup.workCoordinateSystem.getAsCoordinateSystem()

                for op in ops:
                    opName=op.name
                    if opName.startswith('BORR 8 LEFT'):
                        opToolOrient0=op.parameters.itemByName('overrideToolView')
                        opToolOrient0.expression="true"
                        opToolOrient1=op.parameters.itemByName('view_orientation_mode')
                        opToolOrient1.expression="'axesZX'"
                        opToolOrientZ=op.parameters.itemByName('view_orientation_axisZ')
                        opToolOrientZ.expression="true"

                        firstBody=setupFirstBody
                        facesModel=firstBody.faces
                        #transBody=firstBody.transform
                        #transMatrix=transBody.asMatrix()

                        searchEdge=True
                        rak=0
                        rakMax=facesModel.count
                        while searchEdge and rak<=rakMax:
                            
                            faceModel=[]
                            faceModel=facesModel.item(rak)
                            rak=rak+1
                            facePlane=faceModel.geometry
                            if facePlane.objectType=="adsk::core::Plane":
                                faceNormal=facePlane.normal
                                if xAxis.isParallelTo(faceNormal):
                                    newZ=faceModel
                                    cadObjectParam=[]
                                    cadObjectParam: adsk.cam.CadObjectParameterValue = opToolOrientZ.value
                                    opZ=cadObjectParam.value
                                    opZ=newZ
                    
                                    cadObjectParam.value = [opZ]
                                    angleZs=abs(xAxis.angleTo(faceNormal))
                                    if angleZs>1:
                                        op.parameters.itemByName('view_orientation_flipZ').expression='false'
                                    else:
                                        op.parameters.itemByName('view_orientation_flipZ').expression='true'
                                    
                                    searchEdge=False
                            
                        #axis=cadObjectParam.value
                        #axixAxiss.assign(xAxis)
                        #if newZ.isValid:
                    #regen operation of the setup
                    cam.generateToolpath(op)      

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