#!/usr/bin/env python3 # -*- encoding: utf-8 -*- # # This is import_mach, a program to convert a mach 3 xml file to stepconf # xml file. Up to 4 axis can be converted. # Copyright 2015 John Thornton # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # from xml.dom.minidom import parseString import sys import os import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk filter = Gtk.FileFilter() filter.add_pattern("*.xml") filter.set_name(_("Mach configuration files")) fcd = Gtk.FileChooserDialog("Open...", None, Gtk.FileChooserAction.OPEN, (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OPEN, Gtk.ResponseType.OK)) fcd.set_current_folder(os.path.expanduser('~/Desktop')) fcd.add_filter(filter) response = fcd.run() if response == Gtk.ResponseType.OK: file_name = fcd.get_filename() machinename = os.path.splitext(os.path.basename(fcd.get_filename()))[0] fcd.destroy() if response == Gtk.ResponseType.CANCEL: quit(1) print(file_name) print(machinename) file = open(file_name,'r') #convert to string: data = file.read() #close file because we dont need it anymore: file.close() #parse the xml you got from the file dom = parseString(data) #quit() ''' if (len(sys.argv) > 1): #open the xml file for reading: file = open(sys.argv[1],'r') #convert to string: data = file.read() #close file because we dont need it anymore: file.close() #parse the xml you got from the file dom = parseString(data) else: # exit program if no file name is passed sys.exit("Usage is mark.py filename configname") if (len(sys.argv) > 2): machinename = sys.argv[2] else: xmlTag = dom.getElementsByTagName('Profile')[0].toxml() machinename =xmlTag.replace('','').replace('','') ''' try: xmlTag = dom.getElementsByTagName('Units')[0].toxml() units =xmlTag.replace('','').replace('','') if units == "0": units = "1" else: units = "0" except IndexError: units = "1" # create the pins dictionary dPins = {'1':'unused-output', '2':'unused-output', '2':'unused-output', '3':'unused-output', '4':'unused-output', '5':'unused-output', '6':'unused-output', '7':'unused-output', '8':'unused-output', '9':'unused-output', '10':'unused-input', '11':'unused-input', '12':'unused-input', '13':'unused-input', '14':'unused-output', '15':'unused-input', '16':'unused-output', '17':'unused-output'} # create the pin invert dictionary dInvert = {'1':'False', '2':'False', '2':'False', '3':'False', '4':'False', '5':'False', '6':'False', '7':'False', '8':'False', '9':'False', '10':'False', '11':'False', '12':'False', '13':'False', '14':'False', '15':'False', '16':'False', '17':'False'} errors = '' # X Axis xmlTag = dom.getElementsByTagName('Motor0Active')[0].toxml() xActive=xmlTag.replace('','').replace('','') if xActive == '1': xmlTag = dom.getElementsByTagName('Motor0StepPin')[0].toxml() xStepPin = xmlTag.replace('','').replace('','') dPins[xStepPin] = 'xstep' xmlTag = dom.getElementsByTagName('Motor0DirPin')[0].toxml() xDirPin = xmlTag.replace('','').replace('','') dPins[xDirPin] = 'xdir' xmlTag = dom.getElementsByTagName('Motor0StepNeg')[0].toxml() xStepInvert = xmlTag.replace('','').replace('','') if xStepInvert == "1": dInvert[xStepPin] = 'True' xmlTag = dom.getElementsByTagName('Motor0DirNeg')[0].toxml() xDirInvert = xmlTag.replace('','').replace('','') if xDirInvert == "1": dInvert[xDirPin] = 'True' xmlTag = dom.getElementsByTagName('M0Max')[0].toxml() xmaxlim = float(str(xmlTag.replace('','').replace('',''))) xmlTag = dom.getElementsByTagName('M0Min')[0].toxml() xminlim = float(str(xmlTag.replace('','').replace('',''))) try: xmlTag = dom.getElementsByTagName('Steps0')[0].toxml() xscale = float(xmlTag.replace('','').replace('','')) xleadscrew = float(xscale / 200) except IndexError: errors += "No X Axis Scale was found!\n" xleadscrew = 0.0 try: xmlTag = dom.getElementsByTagName('Vel0')[0].toxml() xmaxvel = float(xmlTag.replace('','').replace('','')) except IndexError: errors += "No X Axis Max Velocity was found!\n" xmaxvel = 0.0 try: xmlTag = dom.getElementsByTagName('Acc0')[0].toxml() xmaxacc = float(xmlTag.replace('','').replace('','')) except IndexError: errors += "No X Axis Acceleration was found!\n" xmaxacc = 0.0 else: xmaxlim = 0.0 xminlim = 0.0 xleadscrew = 1.0 xmaxvel = 0.0 xmaxacc = 0.0 # Y Axis xmlTag = dom.getElementsByTagName('Motor1Active')[0].toxml() yActive=xmlTag.replace('','').replace('','') if yActive == '1': xmlTag = dom.getElementsByTagName('Motor1StepPin')[0].toxml() yStepPin = xmlTag.replace('','').replace('','') dPins[yStepPin] = 'ystep' xmlTag = dom.getElementsByTagName('Motor1DirPin')[0].toxml() yDirPin = xmlTag.replace('','').replace('','') dPins[yDirPin] = 'ydir' xmlTag = dom.getElementsByTagName('Motor1StepNeg')[0].toxml() yStepInvert = xmlTag.replace('','').replace('','') if yStepInvert == "1": dInvert[yStepPin] = 'True' xmlTag = dom.getElementsByTagName('Motor1DirNeg')[0].toxml() yDirInvert = xmlTag.replace('','').replace('','') if yDirInvert == "1": dInvert[yDirPin] = 'True' xmlTag = dom.getElementsByTagName('M1Max')[0].toxml() ymaxlim = float(str(xmlTag.replace('','').replace('',''))) xmlTag = dom.getElementsByTagName('M1Min')[0].toxml() yminlim = float(str(xmlTag.replace('','').replace('',''))) try: xmlTag = dom.getElementsByTagName('Steps1')[0].toxml() yscale = float(xmlTag.replace('','').replace('','')) yleadscrew = float(yscale / 200) except IndexError: errors += "No Y Axis Scale was found!\n" yleadscrew = 0.0 try: xmlTag = dom.getElementsByTagName('Vel1')[0].toxml() ymaxvel = float(xmlTag.replace('','').replace('','')) except IndexError: errors += "No Y Axis Max Velocity was found!\n" ymaxvel = 0.0 try: xmlTag = dom.getElementsByTagName('Acc1')[0].toxml() ymaxacc = float(xmlTag.replace('','').replace('','')) except IndexError: errors += "No Y Axis Acceleration was found!\n" ymaxacc = 0.0 else: ymaxlim = 0.0 yminlim = 0.0 yleadscrew = 1.0 ymaxvel = 0.0 ymaxacc = 0.0 # Z Axis xmlTag = dom.getElementsByTagName('Motor2Active')[0].toxml() zActive=xmlTag.replace('','').replace('','') if zActive == '1': xmlTag = dom.getElementsByTagName('Motor2StepPin')[0].toxml() zStepPin = xmlTag.replace('','').replace('','') dPins[zStepPin] = 'zstep' xmlTag = dom.getElementsByTagName('Motor2DirPin')[0].toxml() zDirPin = xmlTag.replace('','').replace('','') dPins[zDirPin] = 'zdir' xmlTag = dom.getElementsByTagName('Motor2StepNeg')[0].toxml() zStepInvert = xmlTag.replace('','').replace('','') if zStepInvert == "1": dInvert[zStepPin] = 'True' xmlTag = dom.getElementsByTagName('Motor2DirNeg')[0].toxml() zDirInvert = xmlTag.replace('','').replace('','') if zDirInvert == "1": dInvert[zStepPin] = 'True' xmlTag = dom.getElementsByTagName('M2Max')[0].toxml() zmaxlim = float(str(xmlTag.replace('','').replace('',''))) xmlTag = dom.getElementsByTagName('M2Min')[0].toxml() zminlim = float(str(xmlTag.replace('','').replace('',''))) try: xmlTag = dom.getElementsByTagName('Steps2')[0].toxml() zscale = float(xmlTag.replace('','').replace('','')) zleadscrew = float(zscale / 200) except IndexError: errors += "No Z Axis Scale was found!\n" zleadscrew = 0.0 try: xmlTag = dom.getElementsByTagName('Vel2')[0].toxml() zmaxvel = float(xmlTag.replace('','').replace('','')) except IndexError: errors += "No Z Axis Max Velocity was found!\n" zmaxvel = 0.0 try: xmlTag = dom.getElementsByTagName('Acc2')[0].toxml() zmaxacc = float(xmlTag.replace('','').replace('','')) except IndexError: errors += "No Z Axis Acceleration was found!\n" zmaxacc = 0.0 else: zmaxlim = 0.0 zmaxlim = 0.0 zleadscrew = 1.0 zmaxvel = 0.0 zmaxacc = 0.0 # A Axis xmlTag = dom.getElementsByTagName('Motor3Active')[0].toxml() aActive=xmlTag.replace('','').replace('','') if aActive == '1': xmlTag = dom.getElementsByTagName('Motor3StepPin')[0].toxml() aStepPin = xmlTag.replace('','').replace('','') dPins[aStepPin] = 'astep' xmlTag = dom.getElementsByTagName('Motor3DirPin')[0].toxml() aDirPin = xmlTag.replace('','').replace('','') dPins[aDirPin] = 'adir' xmlTag = dom.getElementsByTagName('Motor3StepNeg')[0].toxml() aStepInvert = xmlTag.replace('','').replace('','') if aStepInvert == "1": dInvert[aStepPin] = 'True' xmlTag = dom.getElementsByTagName('Motor3DirNeg')[0].toxml() aDirInvert = xmlTag.replace('','').replace('','') if aDirInvert == "1": dInvert[aDirPin] = 'True' xmlTag = dom.getElementsByTagName('M3Max')[0].toxml() amaxlim = float(str(xmlTag.replace('','').replace('','')).rstrip('.')) xmlTag = dom.getElementsByTagName('M3Min')[0].toxml() aminlim = float(str(xmlTag.replace('','').replace('','')).rstrip('.')) try: xmlTag = dom.getElementsByTagName('Steps3')[0].toxml() ascale = float(xmlTag.replace('','').replace('','')) aleadscrew = float(ascale / 200) except IndexError: errors += "No A Axis Scale was found!\n" aleadscrew = 0.0 try: xmlTag = dom.getElementsByTagName('Vel3')[0].toxml() amaxvel = str(xmlTag.replace('','').replace('','')) amaxvel = amaxvel.rsplit(".",1) amaxvel = float(amaxvel[0]) except IndexError: errors += "No A Axis Max Velocity was found!\n" amaxvel = 0.0 try: xmlTag = dom.getElementsByTagName('Acc3')[0].toxml() amaxacc = str(xmlTag.replace('','').replace('','')) amaxacc = amaxacc.rsplit(".",1) amaxacc = float(amaxacc[0]) except IndexError: errors += "No A Axis Acceleration was found!\n" amaxacc = 0.0 else: amaxlim = 0.0 aminlim = 0.0 aleadscrew = 1.0 amaxvel = 0.0 amaxacc = 0.0 # open the stepconf file for writing filename = "/tmp/temp.stepconf" sc = open(filename, "wb") sc.write('\n'.encode()) sc.write("\n".encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(' \n'.encode()) sc.write(f' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) if aActive == '1': axes = 1 # rotary elif yActive == '0': axes = 2 # lathe else: axes = 0 # XYZ sc.write(f' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(f' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(f' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(f' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(f' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(f' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(f' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(' \n'.encode()) sc.write(''.encode()) # close the file sc.close() rm = open('README', 'wb') if len(errors) > 0: rm.write('The following errors were found during processing.\n'.encode()) rm.write(f'{errors}\n'.encode()) print(errors) rm.write('This file can be deleted after running the Stepconf Wizard once.\n'.encode()) rm.write('Copy the generated .stepconf file to the linuxcnc/configs directory.\n'.encode()) rm.write('Run the Stepconf Wizard from the CNC menu.\n'.encode()) rm.write('Select Modify a configuration already created with this program.\n'.encode()) rm.write('Pick the converted file from the list.\n'.encode()) rm.write('If the file did not show up then you have not copied it to the correct directory.\n'.encode()) rm.write('Run the latency test on page three with Test Base Period Jitter.\n'.encode()) rm.write('Allow the test to run for at least a hour then enter the largest Max Jitter.\n'.encode()) rm.write('Go through each screen and check for correctness then save when done.'.encode()) rm.close() print('Mach import/conversion done')