def parse_fzp(self, fzp_file): """ Parse the Fritzing component file """ tree = ElementTree(file=fzp_file) try: prefix = tree.find('label').text except AttributeError: pass else: self.component.add_attribute('_prefix', prefix) symbol = Symbol() self.component.add_symbol(symbol) self.body = Body() symbol.add_body(self.body) self.cid2termid.update(self.parse_terminals(tree)) self.terminals.update(self.cid2termid.values()) layers = tree.find('views/schematicView/layers') if layers is None: self.image = None else: self.image = layers.get('image')
def parse_symbol(self, symbol): """ Extract a symbol. """ symb = Symbol() for body in symbol.get('bodies'): bdy = self.parse_body(body) symb.add_body(bdy) return symb
def build_symbols(self, has_convert): """ Build all Symbols and Bodies for this component. The has_convert argument should be True if there are DeMorgan convert bodies. """ for _ in range(2 if has_convert else 1): symbol = Symbol() for _ in range(self.num_units): symbol.add_body(Body()) self.component.add_symbol(symbol)
def parse_library(self, filename, circuit): """ Parse the library file and add the components to the given circuit. """ f = open(filename) for line in f: parts = line.strip().split() prefix = parts[0] if prefix == 'DEF': component = Component(parts[1]) component.add_attribute('_prefix', parts[2]) symbol = Symbol() component.add_symbol(symbol) body = Body() symbol.add_body(body) elif prefix == 'A': # Arc x, y, radius, start, end = [int(i) for i in parts[1:6]] # convert tenths of degrees to pi radians start = round(start / 1800.0, 1) end = round(end / 1800.0, 1) body.add_shape(shape.Arc(x, y, start, end, radius)) elif prefix == 'C': # Circle x, y, radius = [int(i) for i in parts[1:4]] body.add_shape(shape.Circle(x, y, radius)) elif prefix == 'P': # Polyline num_points = int(parts[1]) poly = shape.Polygon() for i in xrange(num_points): x, y = int(parts[5 + 2 * i]), int(parts[6 + 2 * i]) poly.addPoint(x, y) body.add_shape(poly) elif prefix == 'S': # Rectangle x, y, x2, y2 = [int(i) for i in parts[1:5]] rec = shape.Rectangle(x, y, x2 - x, y2 - y) body.add_shape(rec) elif prefix == 'T': # Text angle, x, y = [int(i) for i in parts[1:4]] angle = round(angle / 1800.0, 1) text = parts[8].replace('~', ' ') body.add_shape(shape.Label(x, y, text, 'left', angle)) elif prefix == 'X': # Pin num, direction = int(parts[2]), parts[6] p2x, p2y, pinlen = int(parts[3]), int(parts[4]), int(parts[5]) if direction == 'U': # up p1x = p2x p1y = p2y - pinlen elif direction == 'D': # down p1x = p2x p1y = p2y + pinlen elif direction == 'L': # left p1x = p2x - pinlen p1y = p2y elif direction == 'R': # right p1x = p2x + pinlen p1y = p2y else: raise ValueError('unexpected pin direction', direction) # TODO: label? body.add_pin(Pin(num, (p1x, p1y), (p2x, p2y))) elif prefix == 'ENDDEF': circuit.add_component(component.name, component) f.close()
def parse_library(self, filename, circuit): """ Parse the library file and add the components to the given circuit. """ f = open(filename) for line in f: parts = line.strip().split() prefix = parts[0] if prefix == "DEF": component = Component(parts[1]) component.add_attribute("_prefix", parts[2]) symbol = Symbol() component.add_symbol(symbol) body = Body() symbol.add_body(body) elif prefix == "A": # Arc x, y, radius, start, end = [int(i) for i in parts[1:6]] # convert tenths of degrees to pi radians start = round(start / 1800.0, 1) end = round(end / 1800.0, 1) body.add_shape(shape.Arc(x, y, start, end, radius)) elif prefix == "C": # Circle x, y, radius = [int(i) for i in parts[1:4]] body.add_shape(shape.Circle(x, y, radius)) elif prefix == "P": # Polyline num_points = int(parts[1]) poly = shape.Polygon() for i in xrange(num_points): x, y = int(parts[5 + 2 * i]), int(parts[6 + 2 * i]) poly.addPoint(x, y) body.add_shape(poly) elif prefix == "S": # Rectangle x, y, x2, y2 = [int(i) for i in parts[1:5]] rec = shape.Rectangle(x, y, x2 - x, y2 - y) body.add_shape(rec) elif prefix == "T": # Text angle, x, y = [int(i) for i in parts[1:4]] angle = round(angle / 1800.0, 1) text = parts[8].replace("~", " ") body.add_shape(shape.Label(x, y, text, "left", angle)) elif prefix == "X": # Pin num, direction = int(parts[2]), parts[6] p2x, p2y, pinlen = int(parts[3]), int(parts[4]), int(parts[5]) if direction == "U": # up p1x = p2x p1y = p2y - pinlen elif direction == "D": # down p1x = p2x p1y = p2y + pinlen elif direction == "L": # left p1x = p2x - pinlen p1y = p2y elif direction == "R": # right p1x = p2x + pinlen p1y = p2y else: raise ValueError("unexpected pin direction", direction) # TODO: label? body.add_pin(Pin(num, (p1x, p1y), (p2x, p2y))) elif prefix == "ENDDEF": circuit.add_component(component.name, component) f.close()