def walk_sheets(base_dir, sheets, sch_dict): for sheet in sheets: sheet_name = sheet.fields[0]['value'].strip('"') sheet_sch = sheet.fields[1]['value'].strip('"') schematic = sch.Schematic(os.path.join(base_dir, sheet_sch)) sch_dict[sheet_name] = (sch.Schematic(os.path.join( base_dir, sheet_sch))) walk_sheets(base_dir, schematic.sheets, sch_dict)
def get_component_positions(self, fn): import sch positions = {} s = sch.Schematic(fn) for c in s.components: ref = c.labels['ref'] positions[ref] = c.position return positions
def load(self, path): if len(path) == 0: return # remove old schematic information self._reset() base_dir = os.path.split(path)[0] top_sch = os.path.split(path)[-1] top_name = os.path.splitext(top_sch)[0] compmap = {} self.schematics[top_name] = (sch.Schematic( os.path.join(base_dir, top_sch))) # Recursively walks sheets to locate nested subschematics # TODO: re-work this to return values instead of passing them byref kch.walk_sheets(base_dir, self.schematics[top_name].sheets, self.schematics) #cmp_labels = [] for name, schematic in self.schematics.items(): for _cbase in schematic.components: c = kch.ComponentWrapper(_cbase) #if _cbase.labels['name'] not in cmp_labels: #cmp_labels.append(_cbase.labels['name']) # Skip virtual components (power, gnd, etc) if c.is_virtual: continue # Skip anything that is missing either a value or a footprint if not c.has_valid_key_fields: continue c.add_bom_fields() ''' try: a = c.description except: c.description = '' ''' if c.footprint not in self.component_type_map: self.component_type_map[c.footprint] = {} if c.value not in self.component_type_map[c.footprint]: self.component_type_map[c.footprint][ c.value] = kch.ComponentTypeContainer() self.component_type_map[c.footprint][c.value].add(c) self.ctv.attach_data(self.component_type_map) self._current_type = None self.ctv.localDB_lookup_button.disabled = True self.ctv.save_button.disabled = True
for c in lib.components: clean = c.name.replace("~", "") if clean not in found: found[clean] = c merged.components.append(c) merged.save() fps = {} paths = [ "/home/jlewallen/fieldkit/core/hardware/fk-core.sch" ] for path in paths: logger.info(path) sch = sch.Schematic(path) for comp in sch.components: name = comp.labels['name'] fp = comp.fields[2]['ref'] if fp != '""': fps[fp] = True if name not in found: logger.info("Missing: " + name) destination_library = "/home/jlewallen/conservify/kicad/conservify.pretty" search = [ "/home/jlewallen/oss/RocketScreamKicadLibrary", "/home/jlewallen/conservify/kicad", "/usr/share/kicad/modules" ] for key, value in fps.iteritems():
"fk-naturalist-rescue:Conn_01x07": "conservify:CONN_01x07", "RocketScreamKicadLibrary:CAPACITOR-CERAMIC": "conservify:CAPACITOR_CERAMIC", "RocketScreamKicadLibrary:MICRO-USB": "conservify:MICRO_USB", "RocketScreamKicadLibrary:S2B-PH-SM4-TB": "conservify:S2B_PH_SM4_TB", "RocketScreamKicadLibrary:TACT-SWITCH_2Pins": "conservify:SWITCH_CONN_02", "RocketScreamKicadLibrary:MOSFET-N": "conservify:MOSFET_N", "RocketScreamKicadLibrary:CONN-HDR-2x5": "conservify:CONN_02x05_SWD", "RocketScreamKicadLibrary:LED-SINGLE": "conservify:LED_SINGLE", "RocketScreamKicadLibrary:RF-SMA-EDGE": "conservify:RF_SMA_EDGE", } paths = [ "/home/jlewallen/fieldkit/naturalist/hardware/main-board/fk-naturalist.sch", ] for path in paths: logger.info(path) fixing = sch.Schematic(path) for comp in fixing.components: ref = comp.fields[0]['ref'] name = comp.labels['name'] fp = comp.fields[2]['ref'] if not 'conservify:' in name: if name in map: logger.info("Fixed %s -> %s" % (name, map[name])) comp.labels['name'] = map[name] else: logger.info("Missing! %s" % (name)) fixing.save()
def _on_btnFileSelectPush(self, event): # wxGlade: KiCADScrape.<event_handler> open_dialog = wx.FileDialog(self, "KiCAD Schematic", "", "", "KiCAD Schmatics (*.sch)|*.sch", wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) if open_dialog.ShowModal() == wx.ID_CANCEL: return else: self.txtFileSelect.ChangeValue(open_dialog.GetPath()) if self.schem: self.schem = None self.schem = sch.Schematic(self.txtFileSelect.GetValue()) self.lstComponent.DeleteAllItems() self.component_dict.clear() for component in self.schem.components: comp_name = clean_string(str(component.fields[0]['ref'])) if not comp_name.startswith( '#' ): comp_timestamp = clean_string(str(component.unit['time_stamp'])) comp_val = clean_string(str(component.fields[1]['ref'])) comp_footprint = clean_string(str(component.fields[2]['ref'])) comp_datasheet = clean_string(str(component.fields[3]['ref'])) comp_supplier = "\"~\"" comp_supplier_partid = "\"~\"" comp_manufacturer = "\"~\"" comp_manufacturer_partid = "\"~\"" comp_itemdescription = "\"~\"" comp_unitprice = "\"~\"" comp_notes = "\"~\"" comp_part_link = "\"~\"" for nameItem in component.fields: if clean_string(nameItem['name']) == 'mfg_name': comp_manufacturer = nameItem['ref'] comp_manufacturer = unquote_string(comp_manufacturer) for nameItem in component.fields: if clean_string(nameItem['name']) == 'mfg_partid': comp_manufacturer_partid = nameItem['ref'] comp_manufacturer_partid = unquote_string(comp_manufacturer_partid) for nameItem in component.fields: if clean_string(nameItem['name']) == 'supplier_name': comp_supplier = nameItem['ref'] comp_supplier = unquote_string(comp_supplier) for nameItem in component.fields: if clean_string(nameItem['name']) == 'supplier_partid': comp_supplier_partid = nameItem['ref'] comp_supplier_partid = unquote_string(comp_supplier_partid) for nameItem in component.fields: if clean_string(nameItem['name']) == 'description': comp_itemdescription= nameItem['ref'] comp_itemdescription = unquote_string(comp_itemdescription) for nameItem in component.fields: if clean_string(nameItem['name']) == 'part_link': comp_part_link = nameItem['ref'] comp_part_link = unquote_string(comp_part_link) for nameItem in component.fields: if clean_string(nameItem['name']) == 'unit_price': comp_unitprice = nameItem['ref'] comp_unitprice = unquote_string(comp_unitprice) for nameItem in component.fields: if clean_string(nameItem['name']) == 'notes': comp_notes = nameItem['ref'] comp_notes = unquote_string(comp_notes) #Datasheet is a bit weird... KiCAD has a value for this but I'm unsure what it's there for. We'll just use our own for now. for nameItem in component.fields: if clean_string(nameItem['name']) == 'bom_datasheet': comp_datasheet = nameItem['ref'] comp_datasheet = unquote_string(comp_datasheet) self.component_dict[comp_timestamp] = ListComponent(comp_timestamp,comp_name,comp_val,comp_footprint,comp_datasheet,comp_supplier,comp_supplier_partid,comp_manufacturer,comp_manufacturer_partid,comp_itemdescription,comp_unitprice,comp_notes,comp_part_link) self._populate_list() self.lstComponent.SortByColumn(1)
def backAnnotate(self, fileName): self.creoSchXmlName = fileName + "_creoin.xml" self.kiCadOutputName = fileName + "_creo.sch" self.kiCadOriginalFile = fileName + ".sch" self.writeInfoStr("Creo Back Annotation - Lengths and Harness Names\n") self.writeInfoStr("------------------------------------------------\n") if (os.path.isfile(self.creoSchXmlName)): self.writeInfoStr("Creo Schematic Inputfile OK: " + self.creoSchXmlName + "\n") else: self.writeErrorStr("Creo Schematic Inputfile NOT FOUND: " + self.creoSchXmlName + "\n") self.writeInfoStr( "NOTE:You need to export Creo Schematic xml file with name: " + self.creoSchXmlName + "\n") self.writeInfoStr( "(Cabling -> Logical Data -> Export -> Creo Schematic)\n") return False if (os.path.isfile(self.kiCadOriginalFile)): self.writeInfoStr("Kicad Schematic Inputfile OK: " + self.kiCadOriginalFile + "\n") else: self.writeErrorStr("Kicad Schematic Inputfile NOT FOUND: " + self.kiCadOriginalFile + "\n") return False creoXml = minidom.parse(self.creoSchXmlName) connections = creoXml.getElementsByTagName("CONNECTION") self.refDesVals = [] self.harnessNum = [] self.wireLength = [] for connection in connections: wireName = connection.getAttribute("name") type = connection.getAttribute("type") varName = "" varValue = "" if wireName[:1] == "W" or type == "ASSEMBLY": self.refDesVals.append(wireName) parameters = connection.getElementsByTagName('PARAMETER') for param in parameters: varName = param.getAttribute('name') varValue = param.getAttribute('value') if (varName == "LENGTH"): self.wireLength.append(varValue) if (varName == "HARNESS_NAME"): self.harnessNum.append(varValue) # Put lenghts to Kicad Schematic # Do not overwrite the original file self.writeInfoStr("\nProcessing wires and cables:\n") self.writeInfoStr("----------------------------\n") self.writeInfoStr(str(self.refDesVals) + "\n\n") myvariable = self.backUpFile(self.kiCadOriginalFile) kiCadSch = sch.Schematic(self.kiCadOriginalFile + ".bak") self.writeKicadSheet(kiCadSch) kiCadSch.save(self.kiCadOriginalFile) # Process subsheets self.prosessedSheets = [] for sheet in kiCadSch.sheets: for field in sheet.fields: if (field['id'] == "F1"): subSheetFilename = field['value'].replace('"', "") if (os.path.exists(subSheetFilename)): if (subSheetFilename in self.prosessedSheets): self.writeWarningStr( "Child .sch already processed: " + subSheetFilename + "!\n") self.writeWarningStr( "NOTE: Reusing schematic shows the same wire\n" ) self.writeWarningStr( "lengths and partnames in all instances of the file.\n" ) self.writeWarningStr( "Copy the .sch to a new name if you need\n") self.writeWarningStr( "to have unique names and wire lengths!\n") else: self.writeInfoStr("\nProcessing child sheet " + subSheetFilename + "\n") myvariable = self.backUpFile(subSheetFilename) kicadChildSheet = sch.Schematic(subSheetFilename + ".bak") self.writeKicadSheet(kicadChildSheet) kicadChildSheet.save(subSheetFilename) self.prosessedSheets.append(subSheetFilename) else: self.writeErrorStr("File does not exist!: " + subSheetFilename + "\n") return True