def parse(self, filename, library_filename=None): """ Parse a kicad file into a design """ design = Design() segments = set() # each wire segment junctions = set() # wire junction point (connects all wires under it) self.instance_names = [] self.library = KiCADLibrary() if library_filename is None: directory, _ = split(filename) for dir_file in listdir(directory): if dir_file.endswith('.lib'): self.library.parse(directory + '/' + dir_file) for cpt in self.library.components: design.add_component(cpt.name, cpt) with open(filename) as f: libs = [] line = f.readline().strip() # parse the library references while line and line != "$EndDescr": if line.startswith('LIBS:'): libs.extend(line.split(':', 1)[1].split(',')) line = f.readline().strip() # Now parse wires and components, ignore connections, we get # connectivity from wire segments line = f.readline() while line: prefix = line.split()[0] if line.startswith('Wire Wire Line'): self.parse_wire(f, segments) elif prefix == "Connection": # Store these to apply later self.parse_connection(line, junctions) elif prefix == "Text": design.design_attributes.add_annotation( self.parse_text(f, line)) elif prefix == "$Comp": # Component Instance inst, comp = self.parse_component_instance(f) design.add_component_instance(inst) if comp is not None: design.add_component(comp.name, comp) self.ensure_component(design, inst.library_id, libs) line = f.readline() segments = self.divide(segments, junctions) design.nets = self.calc_nets(design, segments) design.scale(MULT) return design
def parse(self, filename, library_filename=None): """ Parse a kicad file into a design """ design = Design() segments = set() # each wire segment junctions = set() # wire junction point (connects all wires under it) if library_filename is None: library_filename = splitext(filename)[0] + '-cache.lib' if exists(library_filename): for cpt in parse_library(library_filename): design.add_component(cpt.name, cpt) with open(filename) as f: libs = [] line = f.readline().strip() # parse the library references while line and line != "$EndDescr": if line.startswith('LIBS:'): libs.extend(line.split(':', 1)[1].split(',')) line = f.readline().strip() # Now parse wires and components, ignore connections, we get # connectivity from wire segments line = f.readline() while line: prefix = line.split()[0] if line.startswith('Wire Wire Line'): self.parse_wire(f, segments) elif prefix == "Connection": # Store these to apply later self.parse_connection(line, junctions) elif prefix == "Text": design.design_attributes.add_annotation( self.parse_text(f, line)) elif prefix == "$Comp": # Component Instance inst = self.parse_component_instance(f) design.add_component_instance(inst) if inst.library_id not in design.components.components: cpt = lookup_part(inst.library_id, libs) if cpt is not None: design.components.add_component(cpt.name, cpt) line = f.readline() segments = self.divide(segments, junctions) design.nets = self.calc_nets(segments) self.calc_connected_components(design) return design