From ea90b7378152d500cd91c67c2000ccfbe5df61c9 Mon Sep 17 00:00:00 2001 From: Miguel Angel Ajo Date: Fri, 6 Apr 2012 22:38:32 +0200 Subject: [PATCH] Add method for MODULE in python that takes care of memory handling --- pcbnew/scripting/examples/createPcb.py | 6 +-- pcbnew/scripting/module.i | 60 ++++++++++++++++++++++++++ pcbnew/scripting/pcbnew.i | 1 + 3 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 pcbnew/scripting/module.i diff --git a/pcbnew/scripting/examples/createPcb.py b/pcbnew/scripting/examples/createPcb.py index f92ef21107..2af4a58bbd 100755 --- a/pcbnew/scripting/examples/createPcb.py +++ b/pcbnew/scripting/examples/createPcb.py @@ -1,21 +1,17 @@ #!/usr/bin/env python from pcbnew import * -import pcbnew pcb = BOARD() module = MODULE(pcb) module.SetReference("M1") pad = D_PAD(module) -module.m_Pads.PushBack(pad) -pad.thisown=0 +module.Add(pad) pcb.Add(module) - pcb.Save("/tmp/my2.brd") - print map( lambda x: x.GetReference() , list(pcb.GetModules())) print "Saved?" diff --git a/pcbnew/scripting/module.i b/pcbnew/scripting/module.i new file mode 100644 index 0000000000..942ef7a9aa --- /dev/null +++ b/pcbnew/scripting/module.i @@ -0,0 +1,60 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2012 NBEE Embedded Systems, Miguel Angel Ajo + * 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 + */ + +/** + * @file module.i + * @brief Specific BOARD extensions and templates + */ + + +%extend MODULE +{ + %pythoncode + { + + def GetPads(self): return self.m_Pads + def GetDrawings(self): return self.m_Drawings + + #def SaveToLibrary(self,filename): + # return SaveModuleToLibrary(filename,self) + + # + # add function, clears the thisown to avoid python from deleting + # the object in the garbage collector + # + + def Add(self,item): + + itemC = item.Cast() + + if type(itemC) is D_PAD: + item.thisown=0 + self.m_Pads.PushBack(itemC) + elif type(itemC) in [ TEXTE_PCB, DIMENSION, TEXTE_MODULE, DRAWSEGMENT]: + item.thisown = 0 + self.m_Drawings.PushBack(item) + } + +} + diff --git a/pcbnew/scripting/pcbnew.i b/pcbnew/scripting/pcbnew.i index f45011650e..0f3659bd30 100644 --- a/pcbnew/scripting/pcbnew.i +++ b/pcbnew/scripting/pcbnew.i @@ -104,4 +104,5 @@ %include "board.i" +%include "module.i" %include "units.i"