def onTreeFootprintsSelectionChanged(self, event): configuration = Configuration() item = self.tree_footprints.GetSelection() if item.IsOk() == False: return obj = self.tree_footprints_manager.ItemToObject(item) if isinstance(obj, DataModelFootprint): if os.path.exists( os.path.join(configuration.kicad_footprints_path, obj.footprint.source_path)): mod = kicad_mod_file.KicadModFile() mod.LoadFile( os.path.join(configuration.kicad_footprints_path, obj.footprint.source_path)) image_file = tempfile.NamedTemporaryFile() mod.Render(image_file.name, self.panel_image_footprint.GetRect().width, self.panel_image_footprint.GetRect().height) img = wx.Image(image_file.name, wx.BITMAP_TYPE_ANY) image_file.close() else: img = wx.Image() img.Create(1, 1) else: img = wx.Image() img.Create(1, 1) img = img.ConvertToBitmap() self.image_footprint.SetBitmap(img)
def ShowFootprint(self, footprint): configuration = Configuration() # enable everything else if footprint: if footprint.metadata: metadata = json.loads(footprint.metadata) else: metadata = json.loads('{}') self.edit_footprint_name.Value = '' self.footprint_path = '' if NoneValue(footprint.source_path, '')!='': name = os.path.basename(footprint.source_path) if name.endswith('.kicad_mod')==True: # path is a footprint self.edit_footprint_name.Value = name.replace(".kicad_mod", "") self.footprint_path = os.path.dirname(NoneValue(footprint.source_path, '')) elif name.endswith('.pretty')==True: # path is a lib self.footprint_path = name self.edit_footprint_description.Value = MetadataValue(metadata, 'description', '') self.edit_footprint_comment.Value = MetadataValue(metadata, 'comment', '') self.button_open_url_snapeda.Label = MetadataValue(metadata, 'snapeda', '<None>') print("----", os.path.join(configuration.kicad_footprints_path, footprint.source_path)) if self.edit_footprint_name.Value!='' and os.path.exists(os.path.join(configuration.kicad_footprints_path, footprint.source_path)): mod = kicad_mod_file.KicadModFile() mod.LoadFile(os.path.join(configuration.kicad_footprints_path, footprint.source_path)) image_file = tempfile.NamedTemporaryFile() mod.Render(image_file.name, self.panel_image_footprint.GetRect().width, self.panel_image_footprint.GetRect().height) img = wx.Image(image_file.name, wx.BITMAP_TYPE_ANY) image_file.close() else: img = wx.Image() img.Create(1, 1) img = img.ConvertToBitmap() self.bitmap_edit_footprint.SetBitmap(img) else: self.edit_footprint_name.Value = '' self.edit_footprint_description.Value = '' self.edit_footprint_comment.Value = '' self.button_open_url_snapeda.Label = "<None>" img = wx.Image() img.Create(1, 1) img = img.ConvertToBitmap() self.bitmap_edit_footprint.SetBitmap(img)
def showFootprint(self): if self.part and self.part.footprint and os.path.exists( os.path.join(configuration.kicad_footprints_path, self.part.footprint.source_path)): mod = kicad_mod_file.KicadModFile() mod.LoadFile( os.path.join(configuration.kicad_footprints_path, self.part.footprint.source_path)) image_file = tempfile.NamedTemporaryFile() mod.Render(image_file.name, self.panel_image_footprint.GetRect().width, self.panel_image_footprint.GetRect().height) img = wx.Image(image_file.name, wx.BITMAP_TYPE_ANY) image_file.close() else: img = wx.Image() img.Create(1, 1) img = img.ConvertToBitmap() self.image_footprint.SetBitmap(img)
def onSelectSnapedaFrameOk(self, event): snapeda = event.data if not snapeda: return print snapeda.json self.edit_footprint_name.Value = snapeda.part_number() self.edit_footprint_description.Value = snapeda.short_description() self.snapeda_uid = snapeda.uniqueid() try: download = DownloadQuery() download.get(part_number=snapeda.part_number(), manufacturer=snapeda.manufacturer(), uniqueid=snapeda.uniqueid(), has_symbol=snapeda.has_symbol(), has_footprint=snapeda.has_footprint()) if download.error(): wx.MessageBox(download.error(), 'Error downloading footprint', wx.OK | wx.ICON_ERROR) except: print_stack() DialogSnapedaError(self).ShowModal() return self.button_open_url_snapeda.Label = "https://www.snapeda.com" + snapeda._links( ).self().href() # download footprint if download.url() and download.url() != '': try: print "Download from:", download.url() filename = os.path.join(tempfile.gettempdir(), os.path.basename(download.url())) content = scraper.get(download.url()).content with open(filename, 'wb') as outfile: outfile.write(content) outfile.close() except: print_stack() wx.MessageBox(download.url(), 'Error loading footprint', wx.OK | wx.ICON_ERROR) return # unzip file try: zip_ref = zipfile.ZipFile(filename, 'r') zip_ref.extractall(filename + ".tmp") zip_ref.close() except Exception as e: print_stack() wx.MessageBox(format(e), 'Error unziping footprint', wx.OK | wx.ICON_ERROR) for file in glob.glob(filename + ".tmp/*"): kicad_file = '' if file.endswith(".mod"): dst_libpath, list_of_parts = lib_convert.convert_mod_to_pretty( file, file.replace('.mod', '.pretty')) if len(list_of_parts) > 0: kicad_file = os.path.join( dst_libpath, list_of_parts[0] + ".kicad_mod") elif file.endswith(".kicad_mod"): kicad_file = file self.footprint.content = '' if kicad_file != '': with open(kicad_file, 'r') as content_file: self.footprint.content = content_file.read() print "****", self.footprint.content mod = kicad_mod_file.KicadModFile() mod.LoadFile(kicad_file) image_file = tempfile.NamedTemporaryFile() mod.Render(image_file.name, self.panel_image_footprint.GetRect().width, self.panel_image_footprint.GetRect().height) img = wx.Image(image_file.name, wx.BITMAP_TYPE_ANY) image_file.close() img = img.ConvertToBitmap() self.bitmap_edit_footprint.SetBitmap(img) self.footprint.md5 = hashlib.md5( self.footprint.content).hexdigest()