From f853ca3afac47ae3b7e610af9d4eacef8f8739a5 Mon Sep 17 00:00:00 2001 From: spo Date: Wed, 8 Jun 2016 16:17:36 +0300 Subject: [PATCH] Add Integer attribute support --- src/ModelHighAPI/CMakeLists.txt | 3 ++ src/ModelHighAPI/ModelHighAPI.i | 20 +++++++++ src/ModelHighAPI/ModelHighAPI_Integer.cpp | 49 ++++++++++++++++++++++ src/ModelHighAPI/ModelHighAPI_Integer.h | 50 +++++++++++++++++++++++ src/ModelHighAPI/ModelHighAPI_Tools.cpp | 8 ++++ src/ModelHighAPI/ModelHighAPI_Tools.h | 5 +++ src/ModelHighAPI/ModelHighAPI_swig.h | 1 + src/ModelHighAPI/Test/TestDouble.py | 4 +- src/ModelHighAPI/Test/TestInteger.py | 18 ++++++++ src/SketchAPI/SketchAPI_Line.h | 2 +- src/SketchAPI/SketchAPI_Sketch.h | 1 + 11 files changed, 157 insertions(+), 4 deletions(-) create mode 100644 src/ModelHighAPI/ModelHighAPI_Integer.cpp create mode 100644 src/ModelHighAPI/ModelHighAPI_Integer.h create mode 100644 src/ModelHighAPI/Test/TestInteger.py diff --git a/src/ModelHighAPI/CMakeLists.txt b/src/ModelHighAPI/CMakeLists.txt index c495be0d1..4a68b50e0 100644 --- a/src/ModelHighAPI/CMakeLists.txt +++ b/src/ModelHighAPI/CMakeLists.txt @@ -5,6 +5,7 @@ INCLUDE(Common) SET(PROJECT_HEADERS ModelHighAPI.h ModelHighAPI_Double.h + ModelHighAPI_Integer.h ModelHighAPI_Interface.h ModelHighAPI_Macro.h ModelHighAPI_Selection.h @@ -13,6 +14,7 @@ SET(PROJECT_HEADERS SET(PROJECT_SOURCES ModelHighAPI_Double.cpp + ModelHighAPI_Integer.cpp ModelHighAPI_Interface.cpp ModelHighAPI_Selection.cpp ModelHighAPI_Tools.cpp @@ -73,6 +75,7 @@ INCLUDE(UnitTest) ADD_UNIT_TESTS( TestDouble.py + TestInteger.py ) # ADD_SUBDIRECTORY (Test) diff --git a/src/ModelHighAPI/ModelHighAPI.i b/src/ModelHighAPI/ModelHighAPI.i index 63199a34b..f8d01f872 100644 --- a/src/ModelHighAPI/ModelHighAPI.i +++ b/src/ModelHighAPI/ModelHighAPI.i @@ -20,6 +20,7 @@ %shared_ptr(ModelHighAPI_Interface) // typemaps + %typemap(in) const ModelHighAPI_Double & (ModelHighAPI_Double temp) { if (PyFloat_Check($input) || PyInt_Check($input) || PyLong_Check($input)) { temp = ModelHighAPI_Double(PyFloat_AsDouble($input)); @@ -38,8 +39,27 @@ $1 = (PyFloat_Check($input) || PyInt_Check($input) || PyLong_Check($input) || PyString_Check($input)) ? 1 : 0; } +%typemap(in) const ModelHighAPI_Integer & (ModelHighAPI_Integer temp) { + if (PyInt_Check($input)) { + temp = ModelHighAPI_Integer(static_cast(PyInt_AsLong($input))); + $1 = &temp; + } else if (PyString_Check($input)) { + temp = ModelHighAPI_Integer(PyString_AsString($input)); + $1 = &temp; + } else if ((SWIG_ConvertPtr($input, (void **)&$1, $1_descriptor, SWIG_POINTER_EXCEPTION)) == 0) { + } else { + PyErr_SetString(PyExc_ValueError, "argument must be ModelHighAPI_Integer, int or string."); + return NULL; + } +} + +%typecheck(SWIG_TYPECHECK_POINTER) ModelHighAPI_Integer, const ModelHighAPI_Integer & { + $1 = (PyInt_Check($input) || PyString_Check($input)) ? 1 : 0; +} + // all supported interfaces %include "ModelHighAPI_Double.h" +%include "ModelHighAPI_Integer.h" %include "ModelHighAPI_Interface.h" %include "ModelHighAPI_Macro.h" %include "ModelHighAPI_Selection.h" diff --git a/src/ModelHighAPI/ModelHighAPI_Integer.cpp b/src/ModelHighAPI/ModelHighAPI_Integer.cpp new file mode 100644 index 000000000..68a612302 --- /dev/null +++ b/src/ModelHighAPI/ModelHighAPI_Integer.cpp @@ -0,0 +1,49 @@ +// Name : ModelHighAPI_Integer.cpp +// Purpose: +// +// History: +// 29/03/16 - Sergey POKHODENKO - Creation of the file + +//-------------------------------------------------------------------------------------- +#include "ModelHighAPI_Integer.h" + +#include +//-------------------------------------------------------------------------------------- + +//-------------------------------------------------------------------------------------- +ModelHighAPI_Integer::ModelHighAPI_Integer(int theValue) +: myValue(theValue) +{ +} + +ModelHighAPI_Integer::ModelHighAPI_Integer(const std::string & theValue) +: myValue(theValue) +{ +} + +ModelHighAPI_Integer::ModelHighAPI_Integer(const char * theValue) +: myValue(theValue) +{ +} + +ModelHighAPI_Integer::~ModelHighAPI_Integer() +{ +} + +//-------------------------------------------------------------------------------------- +struct fill_visitor : boost::static_visitor +{ + mutable std::shared_ptr myAttribute; + + fill_visitor(const std::shared_ptr & theAttribute) + : myAttribute(theAttribute) {} + + void operator()(int theValue) const { myAttribute->setValue(theValue); } + void operator()(const std::string & theValue) const { myAttribute->setText(theValue); } +}; + +void ModelHighAPI_Integer::fillAttribute( + const std::shared_ptr & theAttribute) const +{ + boost::apply_visitor(fill_visitor(theAttribute), myValue); +} diff --git a/src/ModelHighAPI/ModelHighAPI_Integer.h b/src/ModelHighAPI/ModelHighAPI_Integer.h new file mode 100644 index 000000000..7650b1256 --- /dev/null +++ b/src/ModelHighAPI/ModelHighAPI_Integer.h @@ -0,0 +1,50 @@ +// Name : ModelHighAPI_Integer.h +// Purpose: +// +// History: +// 29/03/16 - Sergey POKHODENKO - Creation of the file + +#ifndef SRC_MODELHIGHAPI_MODELHIGHAPI_INTEGER_H_ +#define SRC_MODELHIGHAPI_MODELHIGHAPI_INTEGER_H_ + +//-------------------------------------------------------------------------------------- +#include "ModelHighAPI.h" + +#include +#include + +#include +//-------------------------------------------------------------------------------------- +class ModelAPI_AttributeInteger; +//-------------------------------------------------------------------------------------- +/**\class ModelHighAPI_Integer + * \ingroup CPPHighAPI + * \brief Class for filling ModelAPI_AttributeInteger + */ +class ModelHighAPI_Integer +{ +public: + /// Constructor for int + MODELHIGHAPI_EXPORT + ModelHighAPI_Integer(int theValue = 0.); + /// Constructor for std::string + MODELHIGHAPI_EXPORT + ModelHighAPI_Integer(const std::string & theValue); + /// Constructor for char * + MODELHIGHAPI_EXPORT + ModelHighAPI_Integer(const char * theValue); + /// Destructor + MODELHIGHAPI_EXPORT + virtual ~ModelHighAPI_Integer(); + + /// Fill attribute values + MODELHIGHAPI_EXPORT + virtual void fillAttribute(const std::shared_ptr & theAttribute) const; + +private: + boost::variant myValue; +}; + +//-------------------------------------------------------------------------------------- +//-------------------------------------------------------------------------------------- +#endif /* SRC_MODELHIGHAPI_MODELHIGHAPI_INTEGER_H_ */ diff --git a/src/ModelHighAPI/ModelHighAPI_Tools.cpp b/src/ModelHighAPI/ModelHighAPI_Tools.cpp index 46bf241bd..4e7efcac7 100644 --- a/src/ModelHighAPI/ModelHighAPI_Tools.cpp +++ b/src/ModelHighAPI/ModelHighAPI_Tools.cpp @@ -29,6 +29,7 @@ #include //-------------------------------------------------------------------------------------- #include "ModelHighAPI_Double.h" +#include "ModelHighAPI_Integer.h" #include "ModelHighAPI_Selection.h" //-------------------------------------------------------------------------------------- @@ -72,6 +73,13 @@ void fillAttribute(const ModelHighAPI_Double & theValue, theValue.fillAttribute(theAttribute); } +//-------------------------------------------------------------------------------------- +void fillAttribute(const ModelHighAPI_Integer & theValue, + const std::shared_ptr & theAttribute) +{ + theValue.fillAttribute(theAttribute); +} + //-------------------------------------------------------------------------------------- void fillAttribute(const ModelHighAPI_Selection & theValue, const std::shared_ptr & theAttribute) diff --git a/src/ModelHighAPI/ModelHighAPI_Tools.h b/src/ModelHighAPI/ModelHighAPI_Tools.h index 91163898d..0dc48aa93 100644 --- a/src/ModelHighAPI/ModelHighAPI_Tools.h +++ b/src/ModelHighAPI/ModelHighAPI_Tools.h @@ -36,6 +36,7 @@ class ModelAPI_AttributeSelectionList; class ModelAPI_AttributeString; //-------------------------------------------------------------------------------------- class ModelHighAPI_Double; +class ModelHighAPI_Integer; class ModelHighAPI_Selection; //-------------------------------------------------------------------------------------- MODELHIGHAPI_EXPORT @@ -63,6 +64,10 @@ MODELHIGHAPI_EXPORT void fillAttribute(const ModelHighAPI_Double & theValue, const std::shared_ptr & theAttribute); +MODELHIGHAPI_EXPORT +void fillAttribute(const ModelHighAPI_Integer & theValue, + const std::shared_ptr & theAttribute); + MODELHIGHAPI_EXPORT void fillAttribute(const ModelHighAPI_Selection & theValue, const std::shared_ptr & theAttribute); diff --git a/src/ModelHighAPI/ModelHighAPI_swig.h b/src/ModelHighAPI/ModelHighAPI_swig.h index 7fa918cd0..284d46f19 100644 --- a/src/ModelHighAPI/ModelHighAPI_swig.h +++ b/src/ModelHighAPI/ModelHighAPI_swig.h @@ -11,6 +11,7 @@ #include "ModelHighAPI.h" #include "ModelHighAPI_Double.h" + #include "ModelHighAPI_Integer.h" #include "ModelHighAPI_Interface.h" #include "ModelHighAPI_Macro.h" #include "ModelHighAPI_Selection.h" diff --git a/src/ModelHighAPI/Test/TestDouble.py b/src/ModelHighAPI/Test/TestDouble.py index d34c8d420..6735af347 100644 --- a/src/ModelHighAPI/Test/TestDouble.py +++ b/src/ModelHighAPI/Test/TestDouble.py @@ -9,12 +9,10 @@ class DoubleTestCase(unittest.TestCase): def test_create_from_double(self): from_double = ModelHighAPI.ModelHighAPI_Double(100.) -# self.assertEqual(100., from_double.value()) def test_create_from_text(self): from_string = ModelHighAPI.ModelHighAPI_Double("200 + x") -# self.assertEqual("200 + x", from_string.text()) - + if __name__ == "__main__": unittest.main() diff --git a/src/ModelHighAPI/Test/TestInteger.py b/src/ModelHighAPI/Test/TestInteger.py new file mode 100644 index 000000000..c20b264e4 --- /dev/null +++ b/src/ModelHighAPI/Test/TestInteger.py @@ -0,0 +1,18 @@ +import unittest + +import ModelHighAPI + +class IntegerTestCase(unittest.TestCase): + + def test_create_default(self): + default = ModelHighAPI.ModelHighAPI_Integer() + + def test_create_from_integer(self): + from_integer = ModelHighAPI.ModelHighAPI_Integer(100) + + def test_create_from_text(self): + from_string = ModelHighAPI.ModelHighAPI_Integer("200 + x") + + +if __name__ == "__main__": + unittest.main() diff --git a/src/SketchAPI/SketchAPI_Line.h b/src/SketchAPI/SketchAPI_Line.h index bdedb921b..c2f3d98f5 100644 --- a/src/SketchAPI/SketchAPI_Line.h +++ b/src/SketchAPI/SketchAPI_Line.h @@ -14,7 +14,7 @@ #include -#include +#include "SketchAPI_SketchEntity.h" //-------------------------------------------------------------------------------------- class ModelHighAPI_Selection; //-------------------------------------------------------------------------------------- diff --git a/src/SketchAPI/SketchAPI_Sketch.h b/src/SketchAPI/SketchAPI_Sketch.h index cc8e3f64e..26ea81b9d 100644 --- a/src/SketchAPI/SketchAPI_Sketch.h +++ b/src/SketchAPI/SketchAPI_Sketch.h @@ -17,6 +17,7 @@ #include //-------------------------------------------------------------------------------------- class ModelAPI_CompositeFeature; +class ModelHighAPI_Double; class ModelHighAPI_Selection; class SketchAPI_Line; //-------------------------------------------------------------------------------------- -- 2.39.2