def update_command(self): try: self.current_stitch = self.pattern.stitches[self.command_index] self.x, self.y, self.command = self.current_stitch self.cmd, self.thread, self.needle, self.order = decode_embroidery_command( self.command ) self.cmd_str = pyembroidery.get_common_name_dictionary()[self.cmd] except IndexError: self.current_stitch = None self.x = None self.y = None self.command = None self.cmd = None self.thread = None self.needle = None self.order = None self.cmd_str = None self.format_dictionary.update( { "index": self.command_index, "command": self.command, "cmd_str": self.cmd_str, "cmd": self.cmd, "cmd_thread": self.thread, "cmd_needle": self.needle, "cmd_order": self.order, } )
def RunScript(self, CmdTree): # initialize output CmdStr = Grasshopper.DataTree[object]() # retrieve common dictionary to translate commands common_dict = pyembroidery.get_common_name_dictionary() # only do something if there is valid input to begin with if CmdTree != None and CmdTree.DataCount: # loop through all branches of the tree for i, branch in enumerate(CmdTree.Branches): branch_path = CmdTree.Path(i) # loop through all items in the current branch for j, command in enumerate(branch): # look up the command in the dict and add the result to # the output tree try: CmdStr.Add(common_dict[command], branch_path) except KeyError: rml = self.RuntimeMessageLevel.Warning errMsg = ( "Command '{}' at branch {}, index {} " + "is not a known embroidery command integer!" + " A Null item will be inserted into the " + "output tree!") errMsg = errMsg.format(command, i, j) self.AddRuntimeMessage(rml, errMsg) CmdStr.Add(None, branch_path) else: rml = self.RuntimeMessageLevel.Warning errMsg = "Input Cmd failed to collect data!" self.AddRuntimeMessage(rml, errMsg) # return outputs if you have them; here I try it for you: return CmdStr
def set_design(self, pattern): self.pattern = pattern ctrl = self.list_control names = pyembroidery.get_common_name_dictionary() bounds = [float(e) / 10.0 for e in pattern.bounds()] # convert to mm. width = bounds[2] - bounds[0] height = bounds[3] - bounds[1] index = 0 ctrl.SetItem(ctrl.InsertItem(index, "Command Count:"), 1, str(pattern.count_stitches())) index += 1 ctrl.SetItem(ctrl.InsertItem(index, "Thread Count:"), 1, str(pattern.count_color_changes())) index += 1 ctrl.SetItem(ctrl.InsertItem(index, "Needle Count:"), 1, str(pattern.count_needle_sets())) index += 1 ctrl.SetItem(ctrl.InsertItem(index, "Thread Length:"), 1, str(self.pattern_length(pattern))) index += 1 ctrl.SetItem(ctrl.InsertItem(index, "Width:"), 1, "%.1fmm" % width) index += 1 ctrl.SetItem(ctrl.InsertItem(index, "Height:"), 1, "%.1fmm" % height) index += 1 ctrl.SetItem(ctrl.InsertItem(index, "Left:"), 1, "%.1fmm" % bounds[0]) index += 1 ctrl.SetItem(ctrl.InsertItem(index, "Top:"), 1, "%.1fmm" % bounds[1]) index += 1 ctrl.SetItem(ctrl.InsertItem(index, "Right:"), 1, "%.1fmm" % bounds[2]) index += 1 ctrl.SetItem(ctrl.InsertItem(index, "Bottom:"), 1, "%.1fmm" % bounds[3]) index += 1 stitch_counts = {} for s in pattern.stitches: command = s[2] & pyembroidery.COMMAND_MASK if command in stitch_counts: stitch_counts[command] += 1 else: stitch_counts[command] = 1 if len(stitch_counts) != 0: for the_key, the_value in stitch_counts.items(): try: the_key &= pyembroidery.COMMAND_MASK name = "COMMAND_" + names[the_key] except (IndexError, KeyError): name = "COMMAND_UNKNOWN_" + str(the_key) ctrl.SetItem(ctrl.InsertItem(index, name), 1, "%d" % the_value) index += 1 ctrl.SetItem(ctrl.InsertItem(index, "Metadata:"), 1, "%d" % len(pattern.extras)) index += 1 for the_key, the_value in pattern.extras.items(): ctrl.SetItem(ctrl.InsertItem(index, "@%s" % str(the_key)), 1, str(the_value)) index += 1
def on_menu_cell_key(self, event): col = self.last_event.GetCol() row = self.last_event.GetRow() stitches = self.design.stitches stitch = stitches[row] name_dict = pyembroidery.get_common_name_dictionary() command = event.GetId() command_name = name_dict[command] stitch[2] = command self.grid.SetCellValue(row, col, command_name)
def on_menu_duplicate(self, event): stitches = self.design.stitches position = self.last_event.GetRow() stitch = stitches[position] stitches.insert(position, stitch[:]) self.grid.InsertRows(position) common_dict = pyembroidery.get_common_name_dictionary() common_name = common_dict[stitch[2]] self.grid.SetCellValue(position, 0, common_name) self.grid.SetCellValue(position, 1, str(stitch[0])) self.grid.SetCellValue(position, 2, str(stitch[1]))
def show_popup_menu_cell(self, event): self.last_event = event col = event.GetCol() if col != 0: return row = event.GetRow() stitches = self.design.stitches stitch = stitches[row] self.last_event = event menu = wx.Menu() name_dict = pyembroidery.get_common_name_dictionary() for the_key, the_value in name_dict.items(): menu_item = menu.Append(the_key, the_value, the_value) self.Bind(wx.EVT_MENU, self.on_menu_cell_key, menu_item) self.PopupMenu(menu) menu.Destroy()
def set_design(self, set_design): self.design = set_design if self.design is not None: max = len(self.design.stitches) else: max = 0 self.grid.CreateGrid(max, 3) self.grid.EnableDragColSize(0) self.grid.EnableDragRowSize(0) self.grid.EnableDragGridSize(0) self.grid.SetColLabelValue(0, "Command") self.grid.SetColLabelValue(1, "X") self.grid.SetColLabelValue(2, "Y") common_dict = pyembroidery.get_common_name_dictionary() for i, stitch in enumerate(self.design.stitches): common_name = common_dict[stitch[2]] self.grid.SetCellValue(i, 0, common_name) self.grid.SetCellValue(i, 1, str(stitch[0])) self.grid.SetCellValue(i, 2, str(stitch[1]))
def __init__(self, *args, **kwds): # begin wxGlade: StitchPanel.__init__ kwds["style"] = kwds.get("style", 0) | wx.WANTS_CHARS wx.Panel.__init__(self, *args, **kwds) self.name_dict = pyembroidery.get_common_name_dictionary() self.emb_pattern = pyembroidery.EmbPattern() self.emb_pattern.stitch_abs(0, 0) self.name = None self.max_stitch = 0 self.current_stitch = -1 self.scale = 1 self.translate_x = 0 self.translate_y = 0 self.buffer = 0.1 self.grid = None self.text_grid = None self.clicked_position = None self.drag_point = None self.selected_point = 0 self.__set_properties() self.__do_layout() # end wxGlade self.Bind(wx.EVT_PAINT, self.on_paint) self.Bind(wx.EVT_SIZE, self.on_size) self.Bind(wx.EVT_ERASE_BACKGROUND, self.on_erase) self.Bind(wx.EVT_MOTION, self.on_mouse_move) self.Bind(wx.EVT_KEY_DOWN, self.on_key_press) self.Bind(wx.EVT_LEFT_DOWN, self.on_mouse_down) self.Bind(wx.EVT_LEFT_UP, self.on_mouse_up) self.Bind(wx.EVT_LEFT_DCLICK, self.on_left_double_click) self.Bind(wx.EVT_MIDDLE_DCLICK, self.on_left_double_click) self.Bind(wx.EVT_RIGHT_DCLICK, self.on_right_double_click) self.Bind(wx.EVT_RIGHT_DOWN, self.on_right_mouse_down) self.Bind(wx.EVT_DROP_FILES, self.on_drop_file)
def set_document_statistics(self): pattern = self.pattern self.format_dictionary.update(pattern.extras) bounds = pattern.bounds() # convert to mm. width = bounds[2] - bounds[0] height = bounds[3] - bounds[1] stitch_counts = {} for s in pattern.stitches: command = s[2] & COMMAND_MASK if command in stitch_counts: stitch_counts[command] += 1 else: stitch_counts[command] = 1 names = get_common_name_dictionary() for name in names: value = names[name].lower() self.format_dictionary[value + "_count"] = stitch_counts.get(name,0) self.format_dictionary.update( { "stitch_total": pattern.count_stitches(), "thread_total": pattern.count_threads(), "extents_left": bounds[0], "extends_top": bounds[1], "extends_right": bounds[2], "extends_bottom": bounds[3], "extends_width": width, "extends_height": height, "extents_left_mm": bounds[0] / 10.0, "extends_top_mm": bounds[1] / 10.0, "extends_right_mm": bounds[2] / 10.0, "extends_bottom_mm": bounds[3] / 10.0, "extends_width_mm": width / 10.0, "extends_height_mm": height / 10.0, } )