← Back to team overview

kicad-developers team mailing list archive

[PATCH] Changes to python interface enabling net->pads access.

 

Hello,

This is my fourth patch to Kicad. Each of those instances have had process
errors, hopefully I'm not repeating any of those.

some time ago, the connectivity model of pcbnew was reworked to improve
ratsnest calculations.
One of the side effects of that change was that the Pads() api on net went
away.

The new way to access this information appears to be to call
CONNECTIVITY_DATA::GetNetItems( int aNetCode,
                                                              const KICAD_T
aTypes[] ) const

where aTypes serves as a filter.

The changes of this patch are to expose this api to the python world. My
checkin comments give more details on the changes[1] and of course the
patch is attached.


A couple code review type observations on the existing code.


   1. The new connectivity stuff is in connectivity.h. Perhaps it should be
   called class_connectivity.h to match the naming of other files? better yet
   class_connectivity_data.h
   2. The initial commit comment of connectivity.h should says this:
   "New connectivity algorithm."
   It would have been helpful to have more. Presumably, there's a design
   document somewhere describing the change. Or perhaps and email thread?
   Could have saved some hunting.
   3. The API that I'm exposing in python is this one:
   const std::list<BOARD_CONNECTED_ITEM*> GetNetItems( int aNetCode,
                                          const KICAD_T aTypes[] ) const;

   And is used like this:
   const KICAD_T types[] = { PCB_PAD_T, PCB_ZONE_AREA_T, EOT };
   auto netItems = m_connectivity->GetNetItems( i, types );

   why not just use a std::vector? Adding the EOT is another thing folks
   have to remember to include.
   4. m_connectivity is a public member of connectivity_data and used all
   over the place. My head is ringing with the voices of my college TAs about
   access methods.



Miles



[1] check-in comments
Changes to python interface enabling net->pads access.

Added new file connectivity.i to expose CONNECTIVITY_DATA to
python. enables access to d_pads from nets.

Added typemap to board_connected_item.i. since board_connected_item
doesn't use virtual inheritance, when returning a std::list of these
items, the typemap populates the return list with the child
types. This enables python scripts to use the full interface of those
classes (pad, track, zone)

Added typeinfo.i to enable passing a list of KICAD_T values to methods
GetNetItems. This list acts as a query filter. typeinfo.i is included
from pcbnew.i right after kicad.i (instead of in board.i like the
others) typeinfo.h is already being included kicad.i so to ensure the
typemap(s) are properly applied I put them next to each other.

The two new files (typeinfo.i and connectivity.i) were added to
pcbnew's CMakeLists.txt as dependencies.
From 2e291a7e7e8d3b8b34a7487725b245e2b56c10e4 Mon Sep 17 00:00:00 2001
From: Miles McCoo <mail@xxxxxxxxxx>
Date: Wed, 15 Nov 2017 14:16:21 +0100
Subject: [PATCH] Changes to python interface enabling net->pads access.
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="------------2.7.4"

This is a multi-part message in MIME format.
--------------2.7.4
Content-Type: text/plain; charset=UTF-8; format=fixed
Content-Transfer-Encoding: 8bit


Added new file connectivity.i to expose CONNECTIVITY_DATA to
python. enables access to d_pads from nets.

Added typemap to board_connected_item.i. since board_connected_item
doesn't use virtual inheritance, when returning a std::list of these
items, the typemap populates the return list with the child
types. This enables python scripts to use the full interface of those
classes (pad, track, zone)

Added typeinfo.i to enable passing a list of KICAD_T values to methods
GetNetItems. This list acts as a query filter. typeinfo.i is included
from pcbnew.i right after kicad.i (instead of in board.i like the
others) typeinfo.h is already being included kicad.i so to ensure the
typemap(s) are properly applied I put them next to each other.

The two new files (typeinfo.i and connectivity.i) were added to
pcbnew's CMakeLists.txt as dependencies.
---
 pcbnew/CMakeLists.txt              |  2 ++
 pcbnew/swig/board.i                |  3 +-
 pcbnew/swig/board_connected_item.i | 67 ++++++++++++++++++++++++++++++++++++
 pcbnew/swig/connectivity.i         | 39 +++++++++++++++++++++
 pcbnew/swig/pcbnew.i               |  5 ++-
 pcbnew/swig/typeinfo.i             | 69 ++++++++++++++++++++++++++++++++++++++
 6 files changed, 183 insertions(+), 2 deletions(-)
 create mode 100644 pcbnew/swig/connectivity.i
 create mode 100644 pcbnew/swig/typeinfo.i


--------------2.7.4
Content-Type: text/x-patch; name="0001-Changes-to-python-interface-enabling-net-pads-access.patch"
Content-Transfer-Encoding: 8bit
Content-Disposition: attachment; filename="0001-Changes-to-python-interface-enabling-net-pads-access.patch"

diff --git a/pcbnew/CMakeLists.txt b/pcbnew/CMakeLists.txt
index 795c7ed..aa1886f 100644
--- a/pcbnew/CMakeLists.txt
+++ b/pcbnew/CMakeLists.txt
@@ -438,6 +438,7 @@ if( KICAD_SCRIPTING )   # Generate pcbnew.py and pcbnew_wrap.cxx using swig
         DEPENDS swig/board_design_settings.i
         DEPENDS swig/board_item.i
         DEPENDS swig/board_item_container.i
+        DEPENDS swig/connectivity.i
         DEPENDS swig/dimension.i
         DEPENDS swig/drawsegment.i
         DEPENDS swig/edge_mod.i
@@ -452,6 +453,7 @@ if( KICAD_SCRIPTING )   # Generate pcbnew.py and pcbnew_wrap.cxx using swig
         DEPENDS swig/text_mod.i
         DEPENDS swig/track.i
         DEPENDS swig/units.i
+        DEPENDS swig/typeinfo.i
         DEPENDS swig/zone.i
         DEPENDS swig/zone_settings.i
 
diff --git a/pcbnew/swig/board.i b/pcbnew/swig/board.i
index 1504140..895ec0b 100644
--- a/pcbnew/swig/board.i
+++ b/pcbnew/swig/board.i
@@ -3,7 +3,7 @@
  *
  * Copyright (C) 2012 NBEE Embedded Systems, Miguel Angel Ajo <miguelangel@xxxxxxx>
  * Copyright (C) 2016 SoftPLC Corporation, Dick Hollenbeck <dick@xxxxxxxxxxx>
- * Copyright (C) 1992-2012 KiCad Developers, see AUTHORS.txt for contributors.
+ * Copyright (C) 1992-2017 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
@@ -48,6 +48,7 @@ HANDLE_EXCEPTIONS(BOARD::TracksInNetBetweenPoints)
 %include board_item_container.i
 %include board_connected_item.i
 %include board_design_settings.i
+%include connectivity.i
 %include pad.i
 %include track.i
 %include zone.i
diff --git a/pcbnew/swig/board_connected_item.i b/pcbnew/swig/board_connected_item.i
index 9f3e49b..87c15a7 100644
--- a/pcbnew/swig/board_connected_item.i
+++ b/pcbnew/swig/board_connected_item.i
@@ -1,3 +1,27 @@
+/*
+ * This program source code file is part of KiCad, a free EDA CAD application.
+ *
+ * Copyright (C) 1992-2017 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
+ */
+
+
 
 %include class_board_connected_item.h
 
@@ -5,3 +29,46 @@
 #include <class_board_connected_item.h>
 %}
 
+%typemap(out) std::list<BOARD_CONNECTED_ITEM*> {
+    std::list<BOARD_CONNECTED_ITEM*> list = $1;
+    std::list<BOARD_CONNECTED_ITEM*>::const_iterator iter;
+
+    PyObject * retval = $result = PyList_New(0);
+  
+    for( iter=list.begin(); iter!=list.end(); iter++ ) {
+        BOARD_CONNECTED_ITEM* aItem = *iter;
+        PyObject* obj = 0x0;
+    
+        switch( aItem->Type() ) {
+        case PCB_PAD_T:  
+            obj = SWIG_NewPointerObj( SWIG_as_voidptr(aItem),
+                                      SWIGTYPE_p_D_PAD,
+                                      0 | 0 );
+            break;
+    
+        case PCB_TRACE_T:
+        case PCB_VIA_T:
+            obj = SWIG_NewPointerObj( SWIG_as_voidptr(aItem),
+                                      SWIGTYPE_p_TRACK,
+                                      0 | 0 );
+            break;
+
+        case PCB_ZONE_AREA_T:
+            obj = SWIG_NewPointerObj( SWIG_as_voidptr(aItem),
+                                      SWIGTYPE_p_ZONE_CONTAINER,
+                                      0 | 0 );
+            break;
+    
+        default:
+            obj = SWIG_NewPointerObj( SWIG_as_voidptr(aItem),
+                                      SWIGTYPE_p_BOARD_CONNECTED_ITEM,
+                                      0 | 0 );
+            break;
+        }
+
+        assert( obj );
+        PyList_Append (retval, obj );
+        Py_DECREF( obj );
+    }
+ }
+
diff --git a/pcbnew/swig/connectivity.i b/pcbnew/swig/connectivity.i
new file mode 100644
index 0000000..9073e90
--- /dev/null
+++ b/pcbnew/swig/connectivity.i
@@ -0,0 +1,39 @@
+/*
+ * This program source code file is part of KiCad, a free EDA CAD application.
+ *
+ * Copyright (C) 1992-2017 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
+ */
+
+
+// when you ask board for its connectivity, you get a shared_ptr
+%include <std_shared_ptr.i>
+
+// this shared_ptr line has to be before include connectivity.h.
+%shared_ptr(CONNECTIVITY_DATA)
+
+%include connectivity.h
+
+ 
+
+
+%{
+#include <connectivity.h>
+%}
+
diff --git a/pcbnew/swig/pcbnew.i b/pcbnew/swig/pcbnew.i
index ceae48b..d1f8690 100644
--- a/pcbnew/swig/pcbnew.i
+++ b/pcbnew/swig/pcbnew.i
@@ -2,7 +2,7 @@
  * This program source code file is part of KiCad, a free EDA CAD application.
  *
  * Copyright (C) 2012 NBEE Embedded Systems, Miguel Angel Ajo <miguelangel@xxxxxxx>
- * Copyright (C) 1992-2012 KiCad Developers, see AUTHORS.txt for contributors.
+ * Copyright (C) 1992-2017 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
@@ -38,6 +38,9 @@
 
 %include kicad.i
 
+// mostly for KICAD_T
+%include typeinfo.i
+
 %include <convert_to_biu.h>
 
 %{
diff --git a/pcbnew/swig/typeinfo.i b/pcbnew/swig/typeinfo.i
new file mode 100644
index 0000000..f04d5f7
--- /dev/null
+++ b/pcbnew/swig/typeinfo.i
@@ -0,0 +1,69 @@
+/*
+ * This program source code file is part of KiCad, a free EDA CAD application.
+ *
+ * Copyright (C) 1992-2017 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
+ */
+
+
+
+%include core/typeinfo.h
+
+%{
+#include <core/typeinfo.h>
+%}
+
+// methods like CONNECTIVITY_DATA::GetNetItems take an array of KICAD_T values,
+// terminated by EOT. for example, class_board.cpp calls the method with this argument:
+//    const KICAD_T types[] = { PCB_PAD_T, PCB_ZONE_AREA_T, EOT };
+// this typemap allows any of the following:
+//    conn = board.GetConnectivity()
+//    conn.GetNetItems(net.GetNet(), (pcbnew.PCB_PAD_T, pcbnew.PCB_TRACE_T))
+//    conn.GetNetItems(net.GetNet(), [pcbnew.PCB_PAD_T, pcbnew.PCB_TRACE_T])
+//    conn.GetNetItems(net.GetNet(), pcbnew.PCB_PAD_T)
+
+
+%typemap(in) KICAD_T [] ( KICAD_T retval[5] ){
+    retval[0] = EOT;
+    $1 = retval;
+  
+    int type;
+    int ecode = SWIG_AsVal_int( $input, &type );
+
+    if ( SWIG_IsOK( ecode ) ) {
+        retval[0] = static_cast<KICAD_T>( type );
+        retval[1] = EOT;
+    } else if ( PySequence_Check( $input ) ) {
+        // compare less than because we still need space for the EOT terminator
+        assert( PySequence_Size( $input ) <=
+                static_cast<int>( sizeof( retval )/sizeof( KICAD_T ) ) );
+        for(int i=0; i<PySequence_Size( $input ); i++) {
+            int ecode = SWIG_AsVal_int( PySequence_GetItem( $input, i ), &type );
+            if ( !SWIG_IsOK( ecode ) ) {
+                SWIG_exception_fail( SWIG_ArgError( ecode ),
+                                     "expecting KICAD_T enum values" );        
+            }
+            retval[i] = static_cast<KICAD_T>( type );
+            retval[i+1] = EOT;
+        }
+    } else {
+        SWIG_exception_fail( SWIG_ArgError( ecode ),
+                             "expecting KICAD_T enum value" );
+    }
+ }

--------------2.7.4--



Follow ups