def effect(self): '''Main entry point: check to see which mode/tab is selected, and act accordingly.''' self.versionString = "AxiDraw Naming - Version 2.1.0 dated 2019-05-15" # Input sanitization: self.options.mode = self.options.mode.strip("\"") self.options.nickname = self.options.nickname.strip("\"") if (self.options.mode == "about"): return ad = axidraw.AxiDraw() ad.getoptions([]) ad.called_externally = True # Pass the document off for plotting ad.document = self.document ad.options.mode = "manual" if (self.options.mode == "list_names"): ad.options.manual_cmd = "list_names" if (self.options.mode == "write_name"): ad.options.manual_cmd = "write_name" + self.options.nickname ad.effect()
def effect(self): '''Main entry point: check to see which mode/tab is selected, and act accordingly.''' self.versionString = "AxiDraw Merge - Version 2.0.0 dated 2018-07-10" self.spewDebugdata = False self.start_time = time.time() self.pageDelays = 0.0 self.rows_plotted = 0 self.serial_port = None self.svg_data_written = False self.csv_data_read = False self.csv_data_written = False self.csv_file_path = None self.csv_row_count = None self.delay_between_rows = False # Not currently delaying between copies self.b_stopped = False # Not currently stopped by button press #Values to be read from file: self.svg_rand_seed_Old = float(1.0) self.svg_row_old = float(0.0) # Last row plotted. self.svg_rand_seed = float(1.0) self.svgRow = int(1) self.row_to_plot = 1 skipSerial = False if self.options.preview: skipSerial = True # Input sanitization: self.options.mode = self.options.mode.strip("\"") self.options.single_type = self.options.single_type.strip("\"") self.options.data_action = self.options.data_action.strip("\"") self.options.fontface = self.options.fontface.strip("\"") self.options.setup_type = self.options.setup_type.strip("\"") self.options.resume_type = self.options.resume_type.strip("\"") if (self.options.page_delay < 0): self.options.page_delay = 0 if (self.options.mode == "model"): return if (self.options.mode == "options"): return if (self.options.mode == "timing"): return if (self.options.mode == "csv"): skipSerial = True if (self.options.mode == "text"): return if (self.options.mode == "version"): # inkex.errormsg( gettext.gettext(self.versionString)) # Accessible from CLI only return import axidraw # https://github.com/evil-mad/axidraw import hershey_advanced ad = axidraw.AxiDraw() hta = hershey_advanced.HersheyAdv() ad.getoptions([]) self.svg = self.document.getroot() ad.ReadWCBdata(self.svg) self.svg_row_old = ad.svg_row_old # Access params from ReadWCBdata ad.called_externally = True if skipSerial == False: self.serial_port = ebb_serial.openPort() if self.serial_port is None: inkex.errormsg( gettext.gettext("Failed to connect to AxiDraw. :(")) return if self.options.mode == "autoPlot": # Note: In preview mode, we only preview-plot the _last_ row to be plotted. pen_down_travel_inches = 0.0 # Local variable pen_up_travel_inches = 0.0 # Local variable pt_estimate = 0.0 # Local variable continue_plotting = True self.row_to_plot = int(self.options.first_row) if (self.options.last_row == 0 ): # "Continue until last row of data" self.options.last_row = 10000 # A large number; only limit by size of data. self.ReadCSV() if (self.csv_row_count is not None): if (self.row_to_plot > self.csv_row_count): inkex.errormsg( gettext.gettext( "No merge data found in specified range of rows.")) continue_plotting = False if (self.options.last_row < self.options.first_row): continue_plotting = False inkex.errormsg('Nothing to plot; No data rows selected.') if (continue_plotting): ad.backup_original = copy.deepcopy(self.original_document) while (continue_plotting): self.svg_rand_seed = round( time.time() * 100) / 100 # New random seed for new plot self.mergeAndPlot(hta, ad) if self.spewDebugdata: inkex.errormsg('Merging row number ' + str(int(self.row_to_plot)) + '.') pen_down_travel_inches = pen_down_travel_inches + ad.pen_down_travel_inches # Local copy pen_up_travel_inches = pen_up_travel_inches + ad.pen_up_travel_inches # Local copy pt_estimate = pt_estimate + ad.pt_estimate # Local copy if ( ad.b_stopped ): # A pause was triggered while plotting the previous row. inkex.errormsg( 'Paused while plotting row number ' + str(int(self.row_to_plot)) + '.') continue_plotting = False else: # Finished plotting the row without being paused self.row_to_plot = self.row_to_plot + 1 if (self.row_to_plot > self.options.last_row): continue_plotting = False # We have already finished the last row. else: # We will be plotting at least one more row. Delay first. self.next_csv_row() self.delay_between_rows = True # Indicate that we are currently delaying between copies timeCounter = 10 * self.options.page_delay # 100 ms units if self.spewDebugdata: inkex.errormsg( 'Delaying ' + str(int(self.options.page_delay)) + ' seconds.') while (timeCounter > 0): timeCounter = timeCounter - 1 if (self.b_stopped == False): if self.options.preview: pt_estimate += 100 self.pageDelays += 0.1 else: time.sleep( 0.100 ) # Use short intervals to improve responsiveness self.PauseCheck( ) #Query if button pressed self.delay_between_rows = False # Not currently delaying between copies if (self.b_stopped == True ): # if button pressed self.row_to_plot = self.row_to_plot - 1 # Backtrack; we didn't actually get to that row. inkex.errormsg( 'Sequence halted after row number ' + str(int(self.row_to_plot)) + '.') continue_plotting = False # Cancel plotting sequence ad.pen_down_travel_inches = pen_down_travel_inches # Copy local values back to ad.(values) ad.pen_up_travel_inches = pen_up_travel_inches # for printing time report. ad.pt_estimate = pt_estimate self.printTimeReport(ad) elif self.options.mode == "singlePlot": doPlot = True if (self.options.single_type == "singleFix" ): # Plot a specified row self.row_to_plot = int(self.options.single_row) elif (self.options.single_type == "singleAdv" ): # Automatically advance self.row_to_plot = int(self.svg_row_old + 1) else: # ( self.options.single_type == "queryRow" ) # No plotting; Query and report last row plotted inkex.errormsg('Last row merged: Row number ' + str(int(self.svg_row_old))) inkex.errormsg('Next row to merge: Row number ' + str(int(self.svg_row_old + 1))) doPlot = False if doPlot: self.svg_rand_seed = round( time.time() * 100) / 100 # New random seed for new plot self.options.last_row = self.row_to_plot # Last row is equal to first row, in this case. self.ReadCSV() if (self.csv_row_count is not None): if (self.row_to_plot > self.csv_row_count): inkex.errormsg( gettext.gettext( "No merge data found in row number ") + str(self.row_to_plot) + '.') else: ad.backup_original = copy.deepcopy( self.original_document) self.mergeAndPlot(hta, ad) self.printTimeReport(ad) elif self.options.mode == "resume": ad.options.mode = "resume" self.svg_rand_seed = ad.svg_rand_seed_old # Preserve random seed self.row_to_plot = self.svg_row_old # Preserve SVG Row ad.options.resume_type = self.options.resume_type if (self.options.resume_type == "home"): self.options.fontface = "none" # Disable Hershey Text substitution self.mergeAndPlot(hta, ad) elif (ad.svg_application_old != "Axidraw Merge"): inkex.errormsg( gettext.gettext( "No AxiDraw Merge resume data found in file.")) elif (ad.svg_layer_old == 12345 ): # There appears to be a paused "all layers" plot self.options.last_row = self.row_to_plot self.ReadCSV() if (self.csv_row_count is not None): ad.backup_original = copy.deepcopy(self.original_document) self.mergeAndPlot(hta, ad) self.printTimeReport(ad) else: inkex.errormsg( gettext.gettext( "No in-progress plot data found saved in file.")) elif self.options.mode == "setup": if self.options.preview: inkex.errormsg( gettext.gettext( 'Command unavailable while in preview mode.')) else: ad.options.mode = self.options.mode ad.options.setup_type = self.options.setup_type ad.options.pen_pos_up = self.options.pen_pos_up ad.options.pen_pos_down = self.options.pen_pos_down ad.document = self.document ad.options.port = self.serial_port ad.effect() elif self.options.mode == "csv": # Open file dialog if self.options.data_action == "choose": # Select and upload a CSV file try: import pygtk pygtk.require('2.0') import gtk # Use gtk to create file selection dialog box. # self.useGTK = True except: inkex.errormsg( "Unable to load GTK, a required component. Please contact technical support for assistance." ) return filename = None dialog = gtk.FileChooserDialog( title="Please choose a CSV file", action=gtk.FILE_CHOOSER_ACTION_OPEN, buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK)) dialog.set_default_response(gtk.RESPONSE_OK) filter = gtk.FileFilter() filter.set_name("Text/CSV") filter.add_pattern("*.CSV") filter.add_pattern("*.csv") filter.add_pattern("*.txt") filter.add_pattern("*.TXT") filter.add_mime_type("text/csv") filter.add_mime_type("text/plain") filter.add_mime_type("application/csv") filter.add_mime_type("application/x-csv") filter.add_mime_type("text/x-csv") filter.add_mime_type("text/csv") filter.add_mime_type("text/comma-separated-values") filter.add_mime_type("text/x-comma-separated-values") filter.add_mime_type("text/tab-separated-values") dialog.add_filter(filter) filter = gtk.FileFilter() filter.set_name("All files") filter.add_pattern("*") dialog.add_filter(filter) response = dialog.run() if response == gtk.RESPONSE_OK: filename = dialog.get_filename() #inkex.errormsg( "File selected: " + filename) # Print full path # inkex.errormsg( "Selected file: " + str(os.path.basename(filename))) # Print file name elif response == gtk.RESPONSE_CANCEL: inkex.errormsg(gettext.gettext('No CSV file selected.')) filter.destroy() dialog.destroy() if filename is not None: CSVfile = open(filename, 'rU') try: dialect_read = csv.Sniffer().sniff(CSVfile.readline()) except: dialect_read = None inkex.errormsg( "Unable to determine format of selected file, " \ + str(os.path.basename(filename)) ) # Print file name if dialect_read is None: CSVfile.close() else: CSVfile.seek(0) # Rewind file to beginning reader = csv.reader(CSVfile, dialect=dialect_read) CSVrowCount = sum( 1 for row in reader) - 1 # Subtract 1 for header row CSVfile.seek(0) if (CSVrowCount > 0): CSVfile = open(filename, 'rU') reader = csv.DictReader(CSVfile, dialect=dialect_read) inkex.errormsg( "Found " + str(CSVrowCount) + " Rows of merge data in file " \ + str(os.path.basename(filename))) # Print file name key_names = "Column names: " for item in reader.fieldnames: key_names = key_names + "{{" + item + "}}, " key_names = key_names[: -2] # drop last two characters from string (", ") inkex.errormsg(key_names) # Print key list self.csv_file_path = str( filename) # Path & Name of the file self.storeCSVpath( self.svg ) # Store path & name file in our SVG file. else: inkex.errormsg( "Unable to interpret selected file" + str(os.path.basename(filename)) + ".") CSVfile.close() elif self.options.data_action == "view": self.csv_data_read = False CSVfile = None csvNode = None for node in self.svg: if node.tag == 'svg': for subNode in self.svg: if subNode.tag == inkex.addNS( 'MergeData', 'svg') or subNode.tag == 'MergeData': csvNode = subNode elif node.tag == inkex.addNS( 'MergeData', 'svg') or node.tag == 'MergeData': csvNode = node if csvNode is not None: try: CSVfile = str(csvNode.text) self.csv_data_read = True except: self.svg.remove( csvNode ) # An error before this point leaves csvDataRead as False. if CSVfile is None: inkex.errormsg( "No CSV data found in file. Please select and load a CSV file." ) return else: inkex.errormsg("The selected CSV data file is:") inkex.errormsg(CSVfile) # elif self.options.data_action == "erase": # self.eraseCSVpath(self.svg) # erase stored CSV file from our SVG file. if self.serial_port is not None: ebb_motion.doTimedPause( self.serial_port, 10) #Pause a moment for underway commands to finish... ebb_serial.closePort(self.serial_port)
def plot_to_axidraw(self, port, primary): # if primary: # pass # else: # inkex.errormsg('Skipping secondary. ' ) # return # Skip secondary units, without opening class or serial connection ad = axidraw.AxiDraw() ad.getoptions([]) if self.verbose: if primary: prim = " (primary)." else: prim = " (secondary)." inkex.errormsg('plot_to_axidraw started, at port ' + str(port) + prim) # Many plotting parameters to pass through: ad.options.mode = self.options.mode ad.options.speed_pendown = self.options.speed_pendown ad.options.speed_penup = self.options.speed_penup ad.options.accel = self.options.accel ad.options.pen_pos_up = self.options.pen_pos_up ad.options.pen_pos_down = self.options.pen_pos_down ad.options.pen_rate_raise = self.options.pen_rate_raise ad.options.pen_rate_lower = self.options.pen_rate_lower ad.options.pen_delay_up = self.options.pen_delay_up ad.options.pen_delay_down = self.options.pen_delay_down ad.options.no_rotate = self.options.no_rotate ad.options.const_speed = self.options.const_speed ad.options.report_time = self.options.report_time ad.options.manual_cmd = self.options.manual_cmd ad.options.walk_dist = self.options.walk_dist ad.options.layer = self.options.layer ad.options.copies = self.options.copies ad.options.page_delay = self.options.page_delay ad.options.preview = self.options.preview ad.options.rendering = self.options.rendering ad.options.model = self.options.model ad.options.port = port ad.options.setup_type = self.options.setup_type ad.options.resume_type = self.options.resume_type ad.options.auto_rotate = self.options.auto_rotate ad.options.resolution = self.options.resolution ad.options.reordering = self.options.reordering # Special case for this wrapper function: # If the port is None, change the port config option # to be "use first available AxiDraw": if port is None: ad.options.port_config = 1 # Use first available AxiDraw else: ad.options.port_config = 2 # Use AxiDraw specified by port ad.document = self.document ad.original_document = self.document if not primary: ad.Secondary = True # Supress general message reporting ad.called_externally = True # Supress time reporting. # Plot the document using axidraw.py ad.effect() if primary: # Collect output from axidraw.py self.document = ad.document self.outdoc = ad.get_output() else: if ad.error_out: try: the_name = ad.nameString except: the_name = port if port is not None: inkex.errormsg('Error on AxiDraw at port "' + port + '":' + ad.error_out) else: inkex.errormsg('Error on secondary AxiDraw: ' + ad.error_out) inkex.errormsg(" ")