Template for vector<VECTOR2I> in swig

This commit is contained in:
qu1ck 2022-01-02 12:23:17 -08:00 committed by Mark Roszko
parent 60c0891c5f
commit 356cddd474
5 changed files with 64 additions and 24 deletions

View File

@ -154,6 +154,7 @@ typedef long time_t;
// Contains VECTOR2I
%include math.i
%template(VECTOR_VECTOR2I) std::vector<VECTOR2I>;
// ignore warning from nested classes
#pragma SWIG nowarn=325
@ -174,6 +175,7 @@ typedef long time_t;
#include <utf8.h>
%include <utf8.h>
%extend UTF8
{
const char* Cast_to_CChar() { return (self->c_str()); }

View File

@ -34,3 +34,35 @@
#include <math/vector2d.h>
%include <math/vector2d.h>
%template(VECTOR2I) VECTOR2<int>;
%extend VECTOR2<int>
{
void Set(long x, long y) { self->x = x; self->y = y; }
PyObject* Get()
{
PyObject* tup = PyTuple_New(2);
PyTuple_SET_ITEM(tup, 0, PyInt_FromLong(self->x));
PyTuple_SET_ITEM(tup, 1, PyInt_FromLong(self->y));
return tup;
}
%pythoncode
%{
def __eq__(self,other): return (self.x==other.x and self.y==other.y)
def __ne__(self,other): return not (self==other)
def __str__(self): return str(self.Get())
def __repr__(self): return 'VECTOR2I'+str(self.Get())
def __len__(self): return len(self.Get())
def __getitem__(self, index): return self.Get()[index]
def __setitem__(self, index, val):
if index == 0:
self.x = val
elif index == 1:
self.y = val
else:
raise IndexError
def __nonzero__(self): return self.Get() != (0,0)
%}
}

View File

@ -34,34 +34,34 @@
def ToMM(iu):
if type(iu) in [int,float]:
return float(iu) / float(IU_PER_MM)
elif type(iu) in [wxPoint,wxSize]:
elif type(iu) in [wxPoint,wxSize,VECTOR2I]:
return tuple(map(ToMM,iu))
else:
raise TypeError("ToMM() excpects int, float, wxPoint or wxSize, instead got type " + str(type(iu)))
raise TypeError("ToMM() excpects int, float, wxPoint, wxSize or VECTOR2I, instead got type " + str(type(iu)))
def FromMM(mm):
if type(mm) in [int,float]:
return int(float(mm) * float(IU_PER_MM))
elif type(mm) in [wxPoint,wxSize]:
elif type(mm) in [wxPoint,wxSize,VECTOR2I]:
return tuple(map(FromMM,mm))
else:
raise TypeError("FromMM() expects int, float, wxPoint or wxSize, instead got type " + str(type(mm)))
raise TypeError("FromMM() expects int, float, wxPoint, wxSize or VECTOR2I, instead got type " + str(type(mm)))
def ToMils(iu):
if type(iu) in [int,float]:
return float(iu) / float(IU_PER_MILS)
elif type(iu) in [wxPoint,wxSize]:
elif type(iu) in [wxPoint,wxSize,VECTOR2I]:
return tuple(map(ToMils,iu))
else:
raise TypeError("ToMils() excpects int, float, wxPoint or wxSize, instead got type " + str(type(iu)))
raise TypeError("ToMils() excpects int, float, wxPoint, wxSize or VECTOR2I, instead got type " + str(type(iu)))
def FromMils(mils):
if type(mils) in [int,float]:
return int(float(mils)*float(IU_PER_MILS))
elif type(mils) in [wxPoint,wxSize]:
elif type(mils) in [wxPoint,wxSize,VECTOR2I]:
return tuple(map(FromMils,mils))
else:
raise TypeError("FromMils() excpects int, float, wxPoint or wxSize, instead got type " + str(type(mils)))
raise TypeError("FromMils() excpects int, float, wxPoint, wxSize or VECTOR2I, instead got type " + str(type(mils)))
def PutOnGridMM(value, gridSizeMM):
thresh = FromMM(gridSizeMM)
@ -83,6 +83,12 @@
def wxPointMils(mmx,mmy):
return wxPoint(FromMils(mmx),FromMils(mmy))
def VECTOR2I_MM(mmx,mmy):
return VECTOR2I(FromMM(mmx),FromMM(mmy))
def VECTOR2I_Mils(mmx,mmy):
return VECTOR2I(FromMils(mmx),FromMils(mmy))
def wxRectMM(x,y,wx,wy):
x = int(FromMM(x))
y = int(FromMM(y))

View File

@ -65,16 +65,16 @@ class TestPCBLoad(unittest.TestCase):
reference = footprint.Reference()
segments = [[p.x, p.y] for p in reference.TransformToSegmentList()]
expected_segments = [
[141901333, 69196857], [143340666, 69196857], [143340666, 69196857],
[143510000, 69142428], [143510000, 69142428], [143594666, 69088000],
[143594666, 69088000], [143679333, 68979142], [143679333, 68979142],
[143679333, 68761428], [143679333, 68761428], [143594666, 68652571],
[143594666, 68652571], [143510000, 68598142], [143510000, 68598142],
[143340666, 68543714], [143340666, 68543714], [141901333, 68543714],
[143679333, 67400714], [143679333, 68053857], [143679333, 67727285],
[141901333, 67727285], [141901333, 67727285], [142155333, 67836142],
[142155333, 67836142], [142324666, 67945000], [142324666, 67945000],
[142409333, 68053857]
[141901333, 69123378], [143340666, 69123378], [143340666, 69123378],
[143510000, 69068949], [143510000, 69068949], [143594666, 69014521],
[143594666, 69014521], [143679333, 68905663], [143679333, 68905663],
[143679333, 68687949], [143679333, 68687949], [143594666, 68579092],
[143594666, 68579092], [143510000, 68524663], [143510000, 68524663],
[143340666, 68470235], [143340666, 68470235], [141901333, 68470235],
[143679333, 67427928], [143679333, 68081071], [143679333, 67754500],
[141901333, 67754500], [141901333, 67754500], [142155333, 67863357],
[142155333, 67863357], [142324666, 67972214], [142324666, 67972214],
[142409333, 68081071]
]
self.assertEqual(segments, expected_segments)

View File

@ -44,8 +44,8 @@ class TestBoardClass(unittest.TestCase):
track = PCB_TRACK(pcb)
pcb.Add(track)
track.SetStart(wxPointMM(10.0, 10.0))
track.SetEnd(wxPointMM(20.0, 30.0))
track.SetStart(VECTOR2I_MM(10.0, 10.0))
track.SetEnd(VECTOR2I_MM(20.0, 30.0))
track.SetWidth(FromMM(0.5))
@ -67,16 +67,16 @@ class TestBoardClass(unittest.TestCase):
pad.SetShape(PAD_SHAPE_OVAL)
pad.SetSize(wxSizeMM(2.0, 3.0))
pad.SetPosition(wxPointMM(0,0))
pad.SetPosition(VECTOR2I_MM(0,0))
# easy case
p1 = pcb.GetPad(wxPointMM(0,0))
p1 = pcb.GetPad(VECTOR2I_MM(0,0))
# top side
p2 = pcb.GetPad(wxPointMM(0.9,0.0))
p2 = pcb.GetPad(VECTOR2I_MM(0.9,0.0))
# bottom side
p3 = pcb.GetPad(wxPointMM(0,1.4))
p3 = pcb.GetPad(VECTOR2I_MM(0,1.4))
# TODO: get pad == p1 evaluated as true instead
# of relying in the internal C++ object pointer