Calling Python Code

More
05 Apr 2022 20:37 #239382 by davidimurray
Hi All
I'm a total beginner with QtPyVCP and python so please be gentle. I've set out to create my own screenset complete with some conversational gcode generation wizards. I've written some code in Python which generates a txt file with my gcode in it. I've then moved the code over into mainwindow.py and got that working with the screen controls.

As I will have a few conversational tabs, it would seem sensible to have a python file for each routine and call them as required.

I've got the call to the python file working, and when I first click the actionbutton that calls pressme, I get the gcode created. But if i change the value in the doublespin boxes (dsb) and click the action button, the values don't change. By changing the SDia and FDia to fixed values I've proved the value are being passed on the first press of the button, but not subsequent presses.

Any help would be much appreciated so the Gcode is created with the last values entered into the doublespin boxes.
Thanks
Contents of mainwindow.py below
from qtpyvcp.widgets.form_widgets.main_window import VCPMainWindow

# Setup logging
from qtpyvcp.utilities import logger
LOG = logger.getLogger('qtpyvcp.' + __name__)

#Import libraries
import math
from qtpyvcp.actions.program_actions import load as loadProgram
from ODTurn import pressme

class MyMainWindow(VCPMainWindow):
    """Main window class for the VCP."""
    def __init__(self, *args, **kwargs):
        super(MyMainWindow, self).__init__(*args, **kwargs)
        
        SDia=self.dsb.value()
        FDia=self.dsb2.value()
        self.actionbutton.clicked.connect(lambda: pressme(SDia,FDia))

Contents of ODTurn.py below
from qtpyvcp.widgets.form_widgets.main_window import VCPMainWindow
import math
from qtpyvcp.actions.program_actions import load as loadProgram

def pressme(StockDia,FinDia):
        
        #Define Parameters
        CSS=0
        #StockDia=self.dsb.value()
        StockEnd=0
        #FinDia=self.dsb2.value()
        FinEnd=-10
        RDoc=0.5
        FDoc=0.1
        FRRate=100
        FFRate=20
        Clearance=0.5
        Sspeed=1000
        RTool=(str(1).zfill(2))
        FTool=(str(3).zfill(2))
        Coolant=1

        
        
        #Create File - overwrite existing
        f = open("Turning", "w")
        #Re-open file in append mode
        f = open("Turning", "a")

        #Define Roughing Diameter
        RDia = FinDia+(FDoc*2)
        #Define Number of Roughing cuts
        RCuts = math.floor(((StockDia-RDia)/2)/RDoc)

        #Gcode Output
        #Start Spindle
        f.write ("(Start Spindle & Prep)\n")
        f.write("%s%s\n"%("M3 S",Sspeed))
        f.write("%s%s\n"%("M6 T",RTool))
        if Coolant==1:
          f.write ("M8\n")
        f.write ("\n")

        #Move to corner + Clearance
        f.write ("(Move to Start Position)\n")
        f.write ("%s%s%s%s\n"%("G0 X",StockDia+Clearance," Z",StockEnd+Clearance))

        #Move to Start Position
        f.write("%s%s%s%s\n"%("G01 X",StockDia, " F",FRRate))
        f.write("%s%s%s%s\n\n"%("G01 Z",StockEnd, " F",FRRate))

         # Roughing Cut Loops
        f.write ("(Roughing Cuts)\n")
        for X in range(RCuts+1):
          #print (X+1)
          #Cutting Pass
          f.write("%s%s%s%s%s%s\n"%("G01 X",StockDia-2*(RDoc*X)," Z",FinEnd+FDoc," F",FRRate))
          #Move to clearance
          f.write("%s%s%s%s\n"%("G0 X",StockDia-2*(RDoc*X)+Clearance," Z",FinEnd+Clearance))
          f.write("%s%s\n"%("G0 Z",StockEnd+Clearance))
          f.write("%s%s\n"%("G01 x",StockDia-2*(RDoc*(X+1))))

        #Final Roughing Cut
        f.write("%s%s%s%s%s%s\n"%("G01 X",FinDia+2*(FDoc)," Z",FinEnd+FDoc," F",FRRate))
        #Move to clearance
        f.write("%s%s%s%s\n"%("G0 X",FinDia+2*(FDoc)+Clearance," Z",FinEnd+Clearance))
        f.write("%s%s\n\n"%("G0 Z",StockEnd+Clearance))

        # Finish Cut
        f.write ("(Finishing Pass)\n")
        f.write("%s%s%s%s%s%s\n"%("G01 X",FinDia," Z",StockEnd+Clearance," F",FRRate))
        f.write("%s%s%s%s\n"%("G01 Z",FinEnd, " F",FFRate))
        f.write("%s%s\n"%("G01 X",StockDia+Clearance))

        #Move to Stock corner + Clearance
        f.write("%s%s%s%s\n\n"%("G0 X",StockDia+Clearance," Z",StockEnd+Clearance))

        #Turn Off Spindle
        f.write ("(Stop Spindle & finish)\n")
        if Coolant==1:
          f.write ("M9\n")
        f.write("M2\n")
        f.write("M5\n")

        #Rewind Program
        f.write("M30\n")

        #Close File
        f.close()
        
        loadProgram ("/home/dave/linuxcnc/configs/test1/Turning")

Please Log in or Create an account to join the conversation.

More
06 Apr 2022 18:57 #239462 by davidimurray
Replied by davidimurray on topic Calling Python Code
Fixed the issue by calling the doublespinboxes directly in the function call
self.actionbutton.clicked.connect(lambda: pressme(self.dsb.value(),self.dsb2.value()))

Please Log in or Create an account to join the conversation.

Moderators: KCJLcvette
Time to create page: 0.119 seconds
Powered by Kunena Forum