2012-05-05 20:18:47 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 NBEE Embedded Systems, Miguel Angel Ajo <miguelangel@nbee.es>
|
|
|
|
* Copyright (C) 1992-2012 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2012-07-06 19:10:55 +00:00
|
|
|
/**
|
|
|
|
* This file builds the base classes for all kind of python plugins that
|
|
|
|
* can be included into kicad.
|
|
|
|
* they provide generic code to all the classes:
|
|
|
|
*
|
|
|
|
* KiCadPlugin
|
|
|
|
* /|\
|
|
|
|
* |
|
|
|
|
* |\-FilePlugin
|
|
|
|
* |\-FootprintWizardPlugin
|
2013-03-16 03:27:48 +00:00
|
|
|
* |\-ActionPlugin
|
2012-07-06 19:10:55 +00:00
|
|
|
*
|
|
|
|
* It defines the LoadPlugins() function that loads all the plugins
|
|
|
|
* available in the system
|
|
|
|
*
|
|
|
|
*/
|
2013-03-16 03:27:48 +00:00
|
|
|
%pythoncode
|
2012-05-05 20:18:47 +00:00
|
|
|
{
|
|
|
|
|
2013-03-16 03:27:48 +00:00
|
|
|
KICAD_PLUGINS={}
|
|
|
|
|
|
|
|
def ReloadPlugin(name):
|
|
|
|
if not KICAD_PLUGINS.has_key(name):
|
|
|
|
return False
|
|
|
|
|
2013-03-17 02:03:18 +00:00
|
|
|
KICAD_PLUGINS[name]["object"].deregister()
|
2013-03-16 03:27:48 +00:00
|
|
|
mod = reload(KICAD_PLUGINS[name]["module"])
|
2013-03-17 02:03:18 +00:00
|
|
|
KICAD_PLUGINS[name]["object"]= mod.register()
|
|
|
|
|
|
|
|
|
|
|
|
def ReloadPlugins():
|
|
|
|
import os.path
|
|
|
|
for k in KICAD_PLUGINS.keys():
|
|
|
|
plugin = KICAD_PLUGINS[k]
|
|
|
|
|
|
|
|
filename = plugin["filename"]
|
|
|
|
mtime = plugin["modification_time"]
|
|
|
|
now_mtime = os.path.getmtime(filename)
|
|
|
|
|
|
|
|
if mtime!=now_mtime:
|
|
|
|
print filename, " is modified, reloading"
|
|
|
|
KICAD_PLUGINS[k]["modification_time"]=now_mtime
|
|
|
|
ReloadPlugin(k)
|
2013-03-16 03:27:48 +00:00
|
|
|
|
|
|
|
|
2013-07-19 18:27:22 +00:00
|
|
|
def LoadPlugins( plugpath ):
|
2012-07-06 19:10:55 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2012-07-22 12:15:38 +00:00
|
|
|
kicad_path = os.environ.get('KICAD_PATH')
|
|
|
|
plugin_directories=[]
|
2012-07-06 19:10:55 +00:00
|
|
|
|
2013-07-19 18:27:22 +00:00
|
|
|
if plugpath and os.path.isdir( plugpath ):
|
|
|
|
plugin_directories.append( plugpath )
|
|
|
|
|
2012-07-22 12:15:38 +00:00
|
|
|
if kicad_path and os.path.isdir(kicad_path):
|
|
|
|
plugin_directories.append(os.path.join(kicad_path, 'scripting', 'plugins'))
|
2013-03-16 03:27:48 +00:00
|
|
|
|
2014-02-15 10:01:27 +00:00
|
|
|
if sys.platform.startswith('linux'):
|
2012-07-22 12:15:38 +00:00
|
|
|
plugin_directories.append(os.environ['HOME']+'/.kicad_plugins/')
|
|
|
|
plugin_directories.append(os.environ['HOME']+'/.kicad/scripting/plugins/')
|
2012-07-06 19:10:55 +00:00
|
|
|
|
2014-02-15 10:01:27 +00:00
|
|
|
if sys.platform.startswith('darwin'):
|
|
|
|
for singlepath in sys.path:
|
|
|
|
if os.path.isdir( os.path.join( singlepath, 'scripting', 'plugins') ):
|
|
|
|
plugin_directories.append( os.path.join( singlepath, 'scripting', 'plugins') )
|
2012-07-06 19:10:55 +00:00
|
|
|
|
2012-07-22 12:15:38 +00:00
|
|
|
for plugins_dir in plugin_directories:
|
|
|
|
sys.path.append(plugins_dir)
|
2013-03-16 03:27:48 +00:00
|
|
|
|
|
|
|
if not os.path.isdir(plugins_dir):
|
2012-07-06 19:10:55 +00:00
|
|
|
continue
|
2013-03-16 03:27:48 +00:00
|
|
|
|
2012-07-22 12:15:38 +00:00
|
|
|
for module in os.listdir(plugins_dir):
|
|
|
|
if os.path.isdir(plugins_dir+module):
|
|
|
|
__import__(module, locals(), globals())
|
|
|
|
|
|
|
|
if module == '__init__.py' or module[-3:] != '.py':
|
|
|
|
continue
|
2013-03-16 03:27:48 +00:00
|
|
|
|
|
|
|
mod = __import__(module[:-3], locals(), globals())
|
|
|
|
|
2013-03-17 02:03:18 +00:00
|
|
|
module_filename = plugins_dir+"/"+module
|
|
|
|
mtime = os.path.getmtime(module_filename)
|
2013-03-16 03:27:48 +00:00
|
|
|
if hasattr(mod,'register'):
|
2013-03-17 02:03:18 +00:00
|
|
|
KICAD_PLUGINS[module]={"filename":module_filename,
|
|
|
|
"modification_time":mtime,
|
|
|
|
"object":mod.register(),
|
2013-03-16 03:27:48 +00:00
|
|
|
"module":mod}
|
|
|
|
|
2012-07-06 19:10:55 +00:00
|
|
|
|
2012-05-05 20:18:47 +00:00
|
|
|
|
|
|
|
class KiCadPlugin:
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
2013-03-16 03:27:48 +00:00
|
|
|
|
2012-05-05 20:18:47 +00:00
|
|
|
def register(self):
|
|
|
|
if isinstance(self,FilePlugin):
|
|
|
|
pass # register to file plugins in C++
|
|
|
|
if isinstance(self,FootprintWizardPlugin):
|
2012-05-09 17:37:25 +00:00
|
|
|
PYTHON_FOOTPRINT_WIZARDS.register_wizard(self)
|
2012-05-05 20:18:47 +00:00
|
|
|
return
|
2013-03-16 03:27:48 +00:00
|
|
|
|
|
|
|
if isinstance(self,ActionPlugin):
|
|
|
|
pass # register to action plugins in C++
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
def deregister(self):
|
|
|
|
if isinstance(self,FilePlugin):
|
|
|
|
pass # register to file plugins in C++
|
|
|
|
if isinstance(self,FootprintWizardPlugin):
|
|
|
|
PYTHON_FOOTPRINT_WIZARDS.deregister_wizard(self)
|
|
|
|
return
|
|
|
|
|
2012-05-05 20:18:47 +00:00
|
|
|
if isinstance(self,ActionPlugin):
|
|
|
|
pass # register to action plugins in C++
|
2013-03-16 03:27:48 +00:00
|
|
|
|
2012-05-05 20:18:47 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
|
2013-03-16 03:27:48 +00:00
|
|
|
|
|
|
|
|
2012-05-05 20:18:47 +00:00
|
|
|
class FilePlugin(KiCadPlugin):
|
|
|
|
def __init__(self):
|
|
|
|
KiCadPlugin.__init__(self)
|
2013-03-16 03:27:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2012-05-05 20:18:47 +00:00
|
|
|
class FootprintWizardPlugin(KiCadPlugin):
|
|
|
|
def __init__(self):
|
|
|
|
KiCadPlugin.__init__(self)
|
|
|
|
self.defaults()
|
2013-03-16 03:27:48 +00:00
|
|
|
|
2012-05-05 20:18:47 +00:00
|
|
|
def defaults(self):
|
|
|
|
self.module = None
|
|
|
|
self.parameters = {}
|
2012-05-16 09:35:18 +00:00
|
|
|
self.parameter_errors={}
|
2012-05-05 20:18:47 +00:00
|
|
|
self.name = "Undefined Footprint Wizard plugin"
|
|
|
|
self.description = ""
|
|
|
|
self.image = ""
|
2013-03-16 03:27:48 +00:00
|
|
|
|
2012-05-05 20:18:47 +00:00
|
|
|
def GetName(self):
|
|
|
|
return self.name
|
2013-03-16 03:27:48 +00:00
|
|
|
|
2012-05-05 20:18:47 +00:00
|
|
|
def GetImage(self):
|
|
|
|
return self.image
|
2013-03-16 03:27:48 +00:00
|
|
|
|
2012-05-05 20:18:47 +00:00
|
|
|
def GetDescription(self):
|
|
|
|
return self.description
|
2013-03-16 03:27:48 +00:00
|
|
|
|
|
|
|
|
2012-05-05 20:18:47 +00:00
|
|
|
def GetNumParameterPages(self):
|
|
|
|
return len(self.parameters)
|
2013-03-16 03:27:48 +00:00
|
|
|
|
2012-05-05 20:18:47 +00:00
|
|
|
def GetParameterPageName(self,page_n):
|
|
|
|
return self.parameters.keys()[page_n]
|
2013-03-16 03:27:48 +00:00
|
|
|
|
2012-05-05 20:18:47 +00:00
|
|
|
def GetParameterNames(self,page_n):
|
|
|
|
name = self.GetParameterPageName(page_n)
|
|
|
|
return self.parameters[name].keys()
|
2013-03-16 03:27:48 +00:00
|
|
|
|
2012-05-05 20:18:47 +00:00
|
|
|
def GetParameterValues(self,page_n):
|
|
|
|
name = self.GetParameterPageName(page_n)
|
2012-05-10 22:30:26 +00:00
|
|
|
values = self.parameters[name].values()
|
2012-05-16 09:35:18 +00:00
|
|
|
return map( lambda x: str(x) , values) # list elements as strings
|
2013-03-16 03:27:48 +00:00
|
|
|
|
2012-05-16 09:35:18 +00:00
|
|
|
def GetParameterErrors(self,page_n):
|
|
|
|
self.CheckParameters()
|
|
|
|
name = self.GetParameterPageName(page_n)
|
|
|
|
values = self.parameter_errors[name].values()
|
|
|
|
return map( lambda x: str(x) , values) # list elements as strings
|
2013-03-16 03:27:48 +00:00
|
|
|
|
2012-05-16 09:35:18 +00:00
|
|
|
def CheckParameters(self):
|
|
|
|
return ""
|
2013-03-16 03:27:48 +00:00
|
|
|
|
2012-05-16 09:35:18 +00:00
|
|
|
def TryConvertToFloat(self,value):
|
|
|
|
v = value
|
|
|
|
try:
|
|
|
|
v = float(value)
|
|
|
|
except:
|
|
|
|
pass
|
2013-03-16 03:27:48 +00:00
|
|
|
|
2012-05-16 09:35:18 +00:00
|
|
|
return v
|
2013-03-16 03:27:48 +00:00
|
|
|
|
2012-05-05 20:18:47 +00:00
|
|
|
def SetParameterValues(self,page_n,values):
|
2012-05-16 09:35:18 +00:00
|
|
|
name = self.GetParameterPageName(page_n)
|
|
|
|
keys = self.parameters[name].keys()
|
2012-05-05 20:18:47 +00:00
|
|
|
n=0
|
|
|
|
for key in keys:
|
2012-05-16 09:35:18 +00:00
|
|
|
val = self.TryConvertToFloat(values[n])
|
|
|
|
self.parameters[name][key] = val
|
|
|
|
print "[%s][%s]<="%(name,key),val
|
2012-05-05 20:18:47 +00:00
|
|
|
n+=1
|
2013-03-16 03:27:48 +00:00
|
|
|
|
|
|
|
|
2012-05-16 09:35:18 +00:00
|
|
|
def ClearErrors(self):
|
|
|
|
errs={}
|
2013-03-16 03:27:48 +00:00
|
|
|
|
2012-05-16 09:35:18 +00:00
|
|
|
for page in self.parameters.keys():
|
|
|
|
page_dict = self.parameters[page]
|
|
|
|
page_params = {}
|
|
|
|
for param in page_dict.keys():
|
|
|
|
page_params[param]=""
|
2013-03-16 03:27:48 +00:00
|
|
|
|
2012-05-16 09:35:18 +00:00
|
|
|
errs[page]=page_params
|
2013-03-16 03:27:48 +00:00
|
|
|
|
|
|
|
self.parameter_errors = errs
|
|
|
|
|
|
|
|
|
2012-05-05 20:18:47 +00:00
|
|
|
def GetModule(self):
|
|
|
|
self.BuildFootprint()
|
|
|
|
return self.module
|
2013-03-16 03:27:48 +00:00
|
|
|
|
2012-05-05 20:18:47 +00:00
|
|
|
def BuildFootprint(self):
|
|
|
|
return
|
2013-03-16 03:27:48 +00:00
|
|
|
|
2012-05-05 20:18:47 +00:00
|
|
|
def Show(self):
|
|
|
|
print "Footprint Wizard Name: ",self.GetName()
|
|
|
|
print "Footprint Wizard Description: ",self.GetDescription()
|
|
|
|
n_pages = self.GetNumParameterPages()
|
|
|
|
print " setup pages: ",n_pages
|
|
|
|
for page in range(0,n_pages):
|
|
|
|
name = self.GetParameterPageName(page)
|
|
|
|
values = self.GetParameterValues(page)
|
|
|
|
names = self.GetParameterNames(page)
|
|
|
|
print "page %d) %s"%(page,name)
|
|
|
|
for n in range (0,len(values)):
|
|
|
|
print "\t%s\t:\t%s"%(names[n],values[n])
|
|
|
|
|
|
|
|
class ActionPlugin(KiCadPlugin):
|
|
|
|
def __init__(self):
|
|
|
|
KiCadPlugin.__init__(self)
|
|
|
|
|
|
|
|
}
|