diff --git a/common/swig/kicad.i b/common/swig/kicad.i index 5d441bfce4..566a47fdf4 100644 --- a/common/swig/kicad.i +++ b/common/swig/kicad.i @@ -154,6 +154,7 @@ typedef long time_t; // Contains VECTOR2I %include math.i +%template(VECTOR_VECTOR2I) std::vector; // ignore warning from nested classes #pragma SWIG nowarn=325 @@ -174,6 +175,7 @@ typedef long time_t; #include %include + %extend UTF8 { const char* Cast_to_CChar() { return (self->c_str()); } diff --git a/common/swig/math.i b/common/swig/math.i index 8a3e36e3ff..7b49fa7506 100644 --- a/common/swig/math.i +++ b/common/swig/math.i @@ -34,3 +34,35 @@ #include %include %template(VECTOR2I) VECTOR2; + +%extend VECTOR2 +{ + 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) + + %} +} diff --git a/pcbnew/python/swig/units.i b/pcbnew/python/swig/units.i index fd560d50a2..59bb0450ef 100644 --- a/pcbnew/python/swig/units.i +++ b/pcbnew/python/swig/units.i @@ -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))