def _parse_pad(self, pad): layers = sexp.find(pad, "layers")[1] if "F.Cu" not in layers and "*.Cu" not in layers: return pad_type = pad[2] if pad_type not in ("smd", "thru_hole"): return at = [float(x) for x in sexp.find(pad, "at")[1:]] size = [float(x) for x in sexp.find(pad, "size")[1:]] drill = sexp.find(pad, "drill") if drill: offset = sexp.find(drill, "offset") if offset: at[0] += float(offset[1]) at[1] += float(offset[2]) topleft = at[0] - size[0]/2, at[1] - size[1]/2 shape = pad[3] if shape in ("rect", "oval"): self.rect_pads.append((topleft, size)) self._update_bounds(at, dx=size[0]/2, dy=size[1]/2) elif shape == "circle": self.circ_pads.append((at, size[0]/2)) self._update_bounds(at, dx=size[0]/2, dy=size[0]/2) else: self._update_bounds(at)
def _parse(self, board): general = sexp.find(board, "general") self.bounds = [float(x) for x in sexp.find(general, "area")[1:]] self.width = self.bounds[2] - self.bounds[0] self.height = self.bounds[3] - self.bounds[1] for module in sexp.find_all(board, "module"): self.modules.append(Module(module)) self._parse_edges(board)
def _parse_graphic(self, graphic): layer = sexp.find(graphic, "layer")[1] end = [float(x) for x in sexp.find(graphic, "end")[1:]] self._update_bounds(end) if graphic[0] == "fp_line": start = [float(x) for x in sexp.find(graphic, "start")[1:]] self._update_bounds(start) self.graphic_layers[layer]["lines"].append((start, end)) elif graphic[0] == "fp_circle": center = [float(x) for x in sexp.find(graphic, "center")[1:]] self._update_bounds(center) r = math.sqrt((center[0] - end[0])**2 + (center[1] - end[1])**2) self.graphic_layers[layer]["circs"].append((center, r))
def _parse_edges(self, board): for graphic in sexp.find_all(board, "gr_line", "gr_arc", "gr_circle"): layer = sexp.find(graphic, "layer")[1] if layer != "Edge.Cuts": continue if graphic[0] == "gr_line": start = [float(x) for x in sexp.find(graphic, "start")[1:]] end = [float(x) for x in sexp.find(graphic, "end")[1:]] self.edge_lines.append((start, end)) elif graphic[0] == "gr_arc": center = [float(x) for x in sexp.find(graphic, "start")[1:]] start = [float(x) for x in sexp.find(graphic, "end")[1:]] r = math.sqrt((center[0] - start[0])**2 + (center[1] - start[1])**2) angle = float(sexp.find(graphic, "angle")[1]) * math.pi/180.0 dx = start[0] - center[0] dy = start[1] - center[1] start_angle = math.atan2(dy, dx) end_angle = start_angle + angle self.edge_arcs.append((center[0], center[1], r, start_angle, end_angle)) elif graphic[0] == "gr_circle": center = [float(x) for x in sexp.find(graphic, "center")[1:]] end = [float(x) for x in sexp.find(graphic, "end")[1:]] r = math.sqrt((center[0] - end[0])**2 + (center[1] - end[1])**2) self.edge_arcs.append((center[0], center[1], r, 0, 2*math.pi))
def _parse(self, mod): self.at = [float(x) for x in sexp.find(mod, "at")[1:]] self.bounds = [0, 0, 0, 0] self.layer = sexp.find(mod, "layer")[1] for text in sexp.find_all(mod, "fp_text"): if text[1] == "reference": self.ref = text[2] elif text[1] == "value": self.val = text[2] for graphic in sexp.find_all(mod, "fp_line", "fp_circle"): self._parse_graphic(graphic) for pad in sexp.find_all(mod, "pad"): self._parse_pad(pad)
def _parse_graphic(self, graphic): layer = sexp.find(graphic, "layer")[1] if graphic[0] == "fp_line": start = [float(x) for x in sexp.find(graphic, "start")[1:]] self._update_bounds(start) elif graphic[0] == "fp_circle": center = [float(x) for x in sexp.find(graphic, "center")[1:]] self._update_bounds(center) end = [float(x) for x in sexp.find(graphic, "end")[1:]] self._update_bounds(end) if layer == "F.Fab": if graphic[0] == "fp_line": self.fab_lines.append((start, end)) elif graphic[0] == "fp_circle": self.fab_circs.append((center, end)) elif layer == "F.SilkS": if graphic[0] == "fp_line": self.silk_lines.append((start, end)) elif graphic[0] == "fp_circle": self.silk_circs.append((center, end))
def test_auto_filtered_python(): g = Grammar(file('python3.g'), auto_filter_tokens=True, expand_all_repeaters=True) r = g.parse(file('../plyplus.py').read()) #pprint() from sexp import find print [x.tail[0] for x in find(r, 'decorator')]