class PartPreviewDataFrame(PanelPartPreviewData): def __init__(self, parent): super(PartPreviewDataFrame, self).__init__(parent) self.part = None self.lib_cache = KicadFileManagerLib() def SetPart(self, part): self.part = part self.showData() def showData(self): self.showSymbol() self.showFootprint() def showSymbol(self): if self.part and self.part.symbol and self.lib_cache.Exists( self.part.symbol.source_path): self.lib_cache.LoadContent(self.part.symbol) lib = kicad_lib_file.KicadLibFile() lib.Load(self.part.symbol.content) image_file = tempfile.NamedTemporaryFile() lib.Render(image_file.name, self.panel_image_symbol.GetRect().width, self.panel_image_symbol.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_symbol.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)
class EditSymbolFrame(PanelEditSymbol): def __init__(self, parent): super(EditSymbolFrame, self).__init__(parent) self.snapeda_uid = '' self.symbol_path = '' self.lib_cache = KicadFileManagerLib() def SetSymbol(self, symbol): self.symbol = symbol self.ShowSymbol(symbol) def ShowSymbol(self, symbol): configuration = Configuration() # enable everything else if symbol: if symbol.metadata: metadata = json.loads(symbol.metadata) else: metadata = json.loads('{}') self.edit_symbol_name.Value = '' self.symbol_path = '' if NoneValue(symbol.source_path, '')!='': name = os.path.basename(NoneValue(symbol.source_path, '')) if name.endswith('.lib')==False: # path is a symbol self.edit_symbol_name.Value = name.replace(".mod", "") self.symbol_path = os.path.dirname(symbol.source_path) self.edit_symbol_description.Value = MetadataValue(metadata, 'description', '') self.edit_symbol_comment.Value = MetadataValue(metadata, 'comment', '') self.button_open_url_snapeda.Label = MetadataValue(metadata, 'snapeda', '<None>') if self.edit_symbol_name.Value!='' and self.lib_cache.Exists(symbol.source_path): self.lib_cache.LoadContent(symbol) lib = kicad_lib_file.KicadLibFile() lib.Load(symbol.content) image_file = tempfile.NamedTemporaryFile() lib.Render(image_file.name, self.panel_image_symbol.GetRect().width, self.panel_image_symbol.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_symbol.SetBitmap(img) else: self.edit_symbol_name.Value = '' self.edit_symbol_description.Value = '' self.edit_symbol_comment.Value = '' self.button_open_url_snapeda.Label = "<None>" img = wx.Image() img.Create(1, 1) img = img.ConvertToBitmap() self.bitmap_edit_symbol.SetBitmap(img) def enable(self, enabled=True): self.edit_symbol_name.Enabled = enabled self.edit_symbol_description.Enabled = enabled self.edit_symbol_comment.Enabled = enabled self.button_remove_url_snapeda.Enabled = enabled self.button_symbol_editApply.Enabled = enabled self.button_symbol_editCancel.Enabled = enabled self.button_snapeda.Enabled = enabled def onButtonSnapedaClick( self, event ): # create a snapeda frame # dropdown frame dropdown = DropdownDialog(self.button_snapeda, SelectSnapedaFrame, initial_search=self.edit_symbol_name.Value, preview='symbol') dropdown.panel.Bind( EVT_SELECT_SNAPEDA_OK_EVENT, self.onSelectSnapedaFrameOk ) dropdown.Dropdown() def onSelectSnapedaFrameOk(self, event): snapeda = event.data if not snapeda: return print snapeda.json self.edit_symbol_name.Value = snapeda.part_number() self.edit_symbol_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='True', has_footprint='False') if download.error(): wx.MessageBox(download.error(), 'Error downloading symbol', 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 symbol if download.url() and download.url()!='': try: filename = os.path.join(tempfile.gettempdir(), os.path.basename(download.url())) print "Download from:", 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 symbol', 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 symbol', wx.OK | wx.ICON_ERROR) self.symbol.content = '' for file in glob.glob(filename+".tmp/*"): if file.endswith(".lib"): print "---------", file lib = KicadLibCache(filename+".tmp") symbols = lib.read_lib_file(os.path.basename(file)) if len(symbols)>0: for symbol in symbols: self.symbol.content = symbols[symbol].content break print "****", self.symbol.content mod = kicad_lib_file.KicadLibFile() mod.LoadFile(file) image_file = tempfile.NamedTemporaryFile() mod.Render(image_file.name, self.panel_image_symbol.GetRect().width, self.panel_image_symbol.GetRect().height) img = wx.Image(image_file.name, wx.BITMAP_TYPE_ANY) image_file.close() img = img.ConvertToBitmap() self.bitmap_edit_symbol.SetBitmap(img) self.symbol.md5 = hashlib.md5(self.symbol.content).hexdigest() # download 3D symbol #TODO def onButtonOpenUrlSnapedaClick( self, event ): if self.button_open_url_snapeda.Label!="<None>": webbrowser.open(self.button_open_url_snapeda.Label) def onButtonRemoveUrlSnapedaClick( self, event ): self.button_open_url_snapeda.Label = "<None>" self.button_open_url_snapeda = '' def onButtonSymbolEditApply( self, event ): symbol = self.symbol if symbol.metadata: metadata = json.loads(symbol.metadata) else: metadata = json.loads('{}') metadata['description'] = self.edit_symbol_description.Value metadata['comment'] = self.edit_symbol_comment.Value if self.button_open_url_snapeda.Label!="<None>": metadata['snapeda'] = self.button_open_url_snapeda.Label metadata['snapeda_uid'] = self.snapeda_uid metadata['updated'] = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ") else: metadata.pop('snapeda', '') metadata.pop('snapeda_uid', '') metadata.pop('updated', '') symbol.metadata = json.dumps(metadata) if not symbol.content: symbol.content = '' symbol.md5 = hashlib.md5(symbol.content).hexdigest() # send result event event = EditSymbolApplyEvent( data=symbol, # source_path is not changed in the symbol as we only have the filename here, not the full path # the full path should be reconstructed by caller symbol_name=self.edit_symbol_name.Value+".mod" ) wx.PostEvent(self, event) def onButtonSymbolEditCancel( self, event ): event = EditSymbolCancelEvent() wx.PostEvent(self, event)
class SelectSymbolFrame(PanelSelectSymbol): def __init__(self, parent, initial=None): """ Create a popup window from frame :param parent: owner :param initial: item to select by default """ super(SelectSymbolFrame, self).__init__(parent) self.file_manager_lib = KicadFileManagerLib() self.manager_lib = sync.version_manager.VersionManager( self.file_manager_lib) self.manager_lib.on_change_hook = self.onFileLibChanged self.lib_cache = KicadFileManagerLib() # create symbols list self.tree_symbols_manager = TreeManagerSymbols(self.tree_symbols) self.tree_symbols_manager.AddTextColumn("Name") self.search_filter = None self.search_symbol.Value = '' self.load() if initial: self.tree_symbols.Select( self.tree_symbols_manager.ObjectToItem( self.tree_symbols_manager.FindSymbol(initial.source_path))) # set result functions self.cancel = None self.result = None def load(self): try: self.loadSymbols() pass except Exception as e: print_stack() wx.MessageBox(format(e), 'Error', wx.OK | wx.ICON_ERROR) def loadSymbols(self): # clear all self.tree_symbols_manager.ClearItems() self.manager_lib.LoadState() # add folders with no library inside # only versioned files are available from list for file in self.file_manager_lib.files: path = os.path.dirname(os.path.normpath(file)) file_version = self.manager_lib.GetFile(file) if file_version and file_version.id: filtered = False if self.search_filter and file_version.source_path.find( self.search_filter) != -1: filtered = False elif self.search_filter: filtered = True if filtered == False: if path != '': self.tree_symbols_manager.AppendPath(path) self.tree_symbols_manager.AppendSymbol(file_version) def onFileLibChanged(self, event): # do a synchronize when a file change on disk self.load() def SetResult(self, result, cancel=None): self.result = result self.cancel = cancel # Virtual event handlers, overide them in your derived class def onTreeSymbolsSelectionChanged(self, event): item = self.tree_symbols.GetSelection() if item.IsOk() == False: return obj = self.tree_symbols_manager.ItemToObject(item) if isinstance(obj, DataModelSymbol): print "----", obj.symbol.source_path if self.lib_cache.Exists(obj.symbol.source_path): self.lib_cache.LoadContent(obj.symbol) lib = kicad_lib_file.KicadLibFile() lib.Load(obj.symbol.content) image_file = tempfile.NamedTemporaryFile() lib.Render(image_file.name, self.panel_image_symbol.GetRect().width, self.panel_image_symbol.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_symbol.SetBitmap(img) def onButtonCancelClick(self, event): if self.cancel: self.cancel() def onButtonOkClick(self, event): symbol = self.tree_symbols_manager.ItemToObject( self.tree_symbols.GetSelection()) if isinstance(symbol, DataModelSymbol) and self.result: self.result(symbol.symbol) def onSearchSymbolCancel(self, event): self.search_filter = None self.load() def onSearchSymbolButton(self, event): self.search_filter = self.search_symbol.Value self.load() def onSearchSymbolEnter(self, event): self.search_filter = self.search_symbol.Value self.load()