automatic set Z tool axis for side drill depending of wcs orientation

automatic set Z tool axis for side drill depending of wcs orientation

daniele_spagli
Enthusiast Enthusiast
269 Views
0 Replies
Message 1 of 1

automatic set Z tool axis for side drill depending of wcs orientation

daniele_spagli
Enthusiast
Enthusiast

Hi

I think I have build something really useful for my workflow (maybe a little confusing, but I am not a programmer).

This script search for a specific machining of a side drill and set automatically the Z axis for the tool.

The problem is that still does not work 100%. I have one case scenario with components mirrored and arranged where  the script measure wrong the angle from the face normal to the wcs x axis (PI radians, 180 degree) and set in the opposite direction the Z axis

Can someone test it if can reproduce the glitch and help me to solve it? For try it naturally you need to change the name of the operation in the script or in the manifacture section of your model. It require a setup and a drill operation to work.

 

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()))

0 Likes
270 Views
0 Replies
Replies (0)