From f1615c22ae84380d8954006a37fcfff9357b7e61 Mon Sep 17 00:00:00 2001 From: spo Date: Thu, 16 Jun 2016 12:00:17 +0300 Subject: [PATCH] Add Mirror and fix tests --- src/ModelHighAPI/ModelHighAPI_Interface.cpp | 5 ++ src/ModelHighAPI/ModelHighAPI_Interface.h | 6 +++ src/ModelHighAPI/ModelHighAPI_RefAttr.cpp | 5 +- src/ModelHighAPI/ModelHighAPI_Tools.cpp | 9 ++++ src/ModelHighAPI/ModelHighAPI_Tools.h | 5 ++ src/PythonAPI/Test/TestSketcherAddMirror.py | 6 +-- src/SketchAPI/CMakeLists.txt | 2 + src/SketchAPI/SketchAPI.i | 43 +++++++++++++++- src/SketchAPI/SketchAPI_Mirror.cpp | 39 +++++++++++++++ src/SketchAPI/SketchAPI_Mirror.h | 55 +++++++++++++++++++++ src/SketchAPI/SketchAPI_Sketch.cpp | 10 ++++ src/SketchAPI/SketchAPI_Sketch.h | 10 +++- src/SketchAPI/SketchAPI_swig.h | 1 + 13 files changed, 185 insertions(+), 11 deletions(-) create mode 100644 src/SketchAPI/SketchAPI_Mirror.cpp create mode 100644 src/SketchAPI/SketchAPI_Mirror.h diff --git a/src/ModelHighAPI/ModelHighAPI_Interface.cpp b/src/ModelHighAPI/ModelHighAPI_Interface.cpp index 4fd119a3a..e200c9c65 100644 --- a/src/ModelHighAPI/ModelHighAPI_Interface.cpp +++ b/src/ModelHighAPI/ModelHighAPI_Interface.cpp @@ -55,6 +55,11 @@ std::list ModelHighAPI_Interface::result() const return aSelectionList; } +std::shared_ptr ModelHighAPI_Interface::defaultResult() const +{ + return feature()->lastResult(); +} + void ModelHighAPI_Interface::throwException(const std::string & theDescription) { Events_Error::send(theDescription); diff --git a/src/ModelHighAPI/ModelHighAPI_Interface.h b/src/ModelHighAPI/ModelHighAPI_Interface.h index fd536ca33..97084a50b 100644 --- a/src/ModelHighAPI/ModelHighAPI_Interface.h +++ b/src/ModelHighAPI/ModelHighAPI_Interface.h @@ -15,6 +15,7 @@ #include //-------------------------------------------------------------------------------------- class ModelAPI_Feature; +class ModelAPI_Result; class ModelHighAPI_Selection; //-------------------------------------------------------------------------------------- /**\class ModelHighAPI_Interface @@ -43,10 +44,15 @@ public: MODELHIGHAPI_EXPORT void execute(); + // TODO(spo): rename to selectAll() /// Return all objects of the feature MODELHIGHAPI_EXPORT virtual std::list result() const; + /// Return default result. Default implementation feature()->lastResult() + MODELHIGHAPI_EXPORT + virtual std::shared_ptr defaultResult() const; + /// Throw exception to event loop MODELHIGHAPI_EXPORT void throwException(const std::string & theDescription); diff --git a/src/ModelHighAPI/ModelHighAPI_RefAttr.cpp b/src/ModelHighAPI/ModelHighAPI_RefAttr.cpp index 4f00429c1..85b4b7acb 100644 --- a/src/ModelHighAPI/ModelHighAPI_RefAttr.cpp +++ b/src/ModelHighAPI/ModelHighAPI_RefAttr.cpp @@ -13,8 +13,6 @@ #include #include "ModelHighAPI_Interface.h" //-------------------------------------------------------------------------------------- -#include -//-------------------------------------------------------------------------------------- ModelHighAPI_RefAttr::ModelHighAPI_RefAttr() { } @@ -33,9 +31,8 @@ ModelHighAPI_RefAttr::ModelHighAPI_RefAttr( ModelHighAPI_RefAttr::ModelHighAPI_RefAttr( const std::shared_ptr & theValue) -: myValue(std::shared_ptr(theValue->feature()->firstResult())) +: myValue(std::shared_ptr(theValue->defaultResult())) { - // TODO(spo): make firstResult() a member of ModelHighAPI_Interface and use it } ModelHighAPI_RefAttr::~ModelHighAPI_RefAttr() diff --git a/src/ModelHighAPI/ModelHighAPI_Tools.cpp b/src/ModelHighAPI/ModelHighAPI_Tools.cpp index 9250494b1..2c300e15c 100644 --- a/src/ModelHighAPI/ModelHighAPI_Tools.cpp +++ b/src/ModelHighAPI/ModelHighAPI_Tools.cpp @@ -102,6 +102,15 @@ void fillAttribute(const std::list & theValue, it->appendToList(theAttribute); } +//-------------------------------------------------------------------------------------- +void fillAttribute(const std::list > & theValue, + const std::shared_ptr & theAttribute) +{ + theAttribute->clear(); + for (auto it = theValue.begin(); it != theValue.end(); ++it) + theAttribute->append(*it); +} + //-------------------------------------------------------------------------------------- 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 ae35740a3..f53e37a1b 100644 --- a/src/ModelHighAPI/ModelHighAPI_Tools.h +++ b/src/ModelHighAPI/ModelHighAPI_Tools.h @@ -34,6 +34,7 @@ class ModelAPI_AttributeRefList; class ModelAPI_AttributeSelection; class ModelAPI_AttributeSelectionList; class ModelAPI_AttributeString; +class ModelAPI_Object; //-------------------------------------------------------------------------------------- class ModelHighAPI_Double; class ModelHighAPI_Integer; @@ -81,6 +82,10 @@ MODELHIGHAPI_EXPORT void fillAttribute(const std::list & theValue, const std::shared_ptr & theAttribute); +MODELHIGHAPI_EXPORT +void fillAttribute(const std::list > & theValue, + const std::shared_ptr & theAttribute); + MODELHIGHAPI_EXPORT void fillAttribute(const ModelHighAPI_Selection & theValue, const std::shared_ptr & theAttribute); diff --git a/src/PythonAPI/Test/TestSketcherAddMirror.py b/src/PythonAPI/Test/TestSketcherAddMirror.py index 8fc614670..e74b8ea3f 100644 --- a/src/PythonAPI/Test/TestSketcherAddMirror.py +++ b/src/PythonAPI/Test/TestSketcherAddMirror.py @@ -14,15 +14,13 @@ class SketcherAddLine(SketcherTestCase): model.do() def test_add_mirror(self): - mirror = self.sketch.addMirror( - self.line.result(), self.circle_1.result() - ) + mirror = self.sketch.addMirror(self.line, [self.circle_1]) model.do() mirrored_objects = mirror.mirroredObjects() mirrored_circle = mirrored_objects.object(0) circle_feature = ModelAPI_Feature.feature(mirrored_circle) center = geomDataAPI_Point2D(circle_feature.attribute("CircleCenter")) - self.assertEqual(center.x(), -30, msg="%s" % center.x()) + self.assertEqual(center.x(), -30) # dir(mirrored_circle) # center = geomDataAPI_Point2D(mirrored_circle.data().attribute("CircleCenter")) # self.assertEqual(center.x(), -30, msg="%s"%(dir(mirrored_circle))) diff --git a/src/SketchAPI/CMakeLists.txt b/src/SketchAPI/CMakeLists.txt index 57792bb13..9d6ded27c 100644 --- a/src/SketchAPI/CMakeLists.txt +++ b/src/SketchAPI/CMakeLists.txt @@ -7,6 +7,7 @@ SET(PROJECT_HEADERS SketchAPI_Arc.h SketchAPI_Circle.h SketchAPI_Line.h + SketchAPI_Mirror.h SketchAPI_Sketch.h SketchAPI_SketchEntity.h SketchAPI_Point.h @@ -16,6 +17,7 @@ SET(PROJECT_SOURCES SketchAPI_Arc.cpp SketchAPI_Circle.cpp SketchAPI_Line.cpp + SketchAPI_Mirror.cpp SketchAPI_Sketch.cpp SketchAPI_SketchEntity.cpp SketchAPI_Point.cpp diff --git a/src/SketchAPI/SketchAPI.i b/src/SketchAPI/SketchAPI.i index 7cbd61f4c..7359f1d9a 100644 --- a/src/SketchAPI/SketchAPI.i +++ b/src/SketchAPI/SketchAPI.i @@ -23,12 +23,16 @@ %shared_ptr(SketchAPI_Arc) %shared_ptr(SketchAPI_Circle) %shared_ptr(SketchAPI_Line) +%shared_ptr(SketchAPI_Mirror) %shared_ptr(SketchAPI_Sketch) %shared_ptr(SketchAPI_SketchEntity) %shared_ptr(SketchAPI_Point) -// fix compilarion error: ‘res2’ was not declared in this scope +// TODO(spo): move typemaps into ModelHighAPI package + +// fix compilarion error: ‘res*’ was not declared in this scope %typemap(freearg) const std::list & {} +%typemap(freearg) const std::list > & {} %typemap(in) const std::list & (std::list temp) { std::shared_ptr * temp_attribute; @@ -55,10 +59,47 @@ } } +%typemap(in) const std::list > & (std::list > temp) { + std::shared_ptr * temp_object; + std::shared_ptr * temp_interface; + int newmem = 0; + if (PySequence_Check($input)) { + for (Py_ssize_t i = 0; i < PySequence_Size($input); ++i) { + PyObject * item = PySequence_GetItem($input, i); + if ((SWIG_ConvertPtrAndOwn(item, (void **)&temp_object, $descriptor(std::shared_ptr *), SWIG_POINTER_EXCEPTION, &newmem)) == 0) { + if (!temp_object) { + PyErr_SetString(PyExc_TypeError, "argument must be list of ModelHighAPI_Interface or ModelAPI_Object."); + return NULL; + } + temp.push_back(*temp_object); + if (newmem & SWIG_CAST_NEW_MEMORY) { + delete temp_object; + } + } else + if ((SWIG_ConvertPtrAndOwn(item, (void **)&temp_interface, $descriptor(std::shared_ptr *), SWIG_POINTER_EXCEPTION, &newmem)) == 0) { + if (!temp_interface) { + PyErr_SetString(PyExc_TypeError, "argument must be list of ModelHighAPI_Interface or ModelAPI_Object."); + return NULL; + } + temp.push_back((*temp_interface)->defaultResult()); + if (newmem & SWIG_CAST_NEW_MEMORY) { + delete temp_interface; + } + } + Py_DECREF(item); + } + $1 = &temp; + } else { + PyErr_SetString(PyExc_ValueError, "argument must be list of ModelHighAPI_Interface or ModelAPI_Object."); + return NULL; + } +} + // all supported interfaces (the order is very important according dependencies: base class first) %include "SketchAPI_SketchEntity.h" %include "SketchAPI_Point.h" %include "SketchAPI_Line.h" %include "SketchAPI_Circle.h" %include "SketchAPI_Arc.h" +%include "SketchAPI_Mirror.h" %include "SketchAPI_Sketch.h" diff --git a/src/SketchAPI/SketchAPI_Mirror.cpp b/src/SketchAPI/SketchAPI_Mirror.cpp new file mode 100644 index 000000000..d6166e79c --- /dev/null +++ b/src/SketchAPI/SketchAPI_Mirror.cpp @@ -0,0 +1,39 @@ +// Name : SketchAPI_Mirror.cpp +// Purpose: +// +// History: +// 16/06/16 - Sergey POKHODENKO - Creation of the file + +//-------------------------------------------------------------------------------------- +#include "SketchAPI_Mirror.h" +//-------------------------------------------------------------------------------------- +#include +#include +//-------------------------------------------------------------------------------------- +SketchAPI_Mirror::SketchAPI_Mirror( + const std::shared_ptr & theFeature) +: SketchAPI_SketchEntity(theFeature) +{ + initialize(); +} + +SketchAPI_Mirror::SketchAPI_Mirror( + const std::shared_ptr & theFeature, + const ModelHighAPI_RefAttr & theMirrorLine, + const std::list > & theObjects) +: SketchAPI_SketchEntity(theFeature) +{ + if (initialize()) { + fillAttribute(theMirrorLine, mirrorLine()); + fillAttribute(theObjects, mirrorList()); + + execute(); + } +} + +SketchAPI_Mirror::~SketchAPI_Mirror() +{ + +} + +//-------------------------------------------------------------------------------------- diff --git a/src/SketchAPI/SketchAPI_Mirror.h b/src/SketchAPI/SketchAPI_Mirror.h new file mode 100644 index 000000000..9f3848d39 --- /dev/null +++ b/src/SketchAPI/SketchAPI_Mirror.h @@ -0,0 +1,55 @@ +// Name : SketchAPI_Mirror.h +// Purpose: +// +// History: +// 16/06/16 - Sergey POKHODENKO - Creation of the file + +#ifndef SRC_SKETCHAPI_SKETCHAPI_MIRROR_H_ +#define SRC_SKETCHAPI_SKETCHAPI_MIRROR_H_ + +//-------------------------------------------------------------------------------------- +#include "SketchAPI.h" + +#include + +#include + +#include "SketchAPI_SketchEntity.h" +//-------------------------------------------------------------------------------------- +class ModelAPI_Object; +class ModelHighAPI_RefAttr; +//-------------------------------------------------------------------------------------- +/**\class SketchAPI_Mirror + * \ingroup CPPHighAPI + * \brief Interface for Mirror feature + */ +class SketchAPI_Mirror : public SketchAPI_SketchEntity +{ +public: + /// Constructor without values + SKETCHAPI_EXPORT + explicit SketchAPI_Mirror(const std::shared_ptr & theFeature); + /// Constructor with values + SKETCHAPI_EXPORT + SketchAPI_Mirror(const std::shared_ptr & theFeature, + const ModelHighAPI_RefAttr & theMirrorLine, + const std::list > & theObjects); + /// Destructor + SKETCHAPI_EXPORT + virtual ~SketchAPI_Mirror(); + + INTERFACE_4(SketchPlugin_ConstraintMirror::ID(), + mirrorLine, SketchPlugin_ConstraintMirror::ENTITY_A(), ModelAPI_AttributeRefAttr, /** Mirror line */, + mirrorList, SketchPlugin_ConstraintMirror::MIRROR_LIST_ID(), ModelAPI_AttributeRefList, /** Mirror list */, + sourceObjects, SketchPlugin_ConstraintMirror::ENTITY_B(), ModelAPI_AttributeRefList, /** Source objects */, + mirroredObjects, SketchPlugin_ConstraintMirror::ENTITY_C(), ModelAPI_AttributeRefList, /** Mirrored objects */ + ) + +}; + +//! Pointer on Mirror object +typedef std::shared_ptr MirrorPtr; + +//-------------------------------------------------------------------------------------- +//-------------------------------------------------------------------------------------- +#endif /* SRC_SKETCHAPI_SKETCHAPI_MIRROR_H_ */ diff --git a/src/SketchAPI/SketchAPI_Sketch.cpp b/src/SketchAPI/SketchAPI_Sketch.cpp index bf9fdd1c5..ab58eec60 100644 --- a/src/SketchAPI/SketchAPI_Sketch.cpp +++ b/src/SketchAPI/SketchAPI_Sketch.cpp @@ -34,6 +34,7 @@ #include "SketchAPI_Arc.h" #include "SketchAPI_Circle.h" #include "SketchAPI_Line.h" +#include "SketchAPI_Mirror.h" #include "SketchAPI_Point.h" //-------------------------------------------------------------------------------------- SketchAPI_Sketch::SketchAPI_Sketch( @@ -313,6 +314,15 @@ std::shared_ptr SketchAPI_Sketch::addArc(const std::string & theE return ArcPtr(new SketchAPI_Arc(aFeature, theExternalName)); } +//-------------------------------------------------------------------------------------- +std::shared_ptr SketchAPI_Sketch::addMirror( + const ModelHighAPI_RefAttr & theMirrorLine, + const std::list > & theObjects) +{ + std::shared_ptr aFeature = compositeFeature()->addFeature(SketchPlugin_ConstraintMirror::ID()); + return MirrorPtr(new SketchAPI_Mirror(aFeature, theMirrorLine, theObjects)); +} + //-------------------------------------------------------------------------------------- std::shared_ptr SketchAPI_Sketch::setAngle( const ModelHighAPI_RefAttr & theLine1, diff --git a/src/SketchAPI/SketchAPI_Sketch.h b/src/SketchAPI/SketchAPI_Sketch.h index 50d17a364..8525b4388 100644 --- a/src/SketchAPI/SketchAPI_Sketch.h +++ b/src/SketchAPI/SketchAPI_Sketch.h @@ -19,12 +19,14 @@ #include //-------------------------------------------------------------------------------------- class ModelAPI_CompositeFeature; +class ModelAPI_Object; class ModelHighAPI_Double; class ModelHighAPI_RefAttr; class ModelHighAPI_Selection; class SketchAPI_Arc; class SketchAPI_Circle; class SketchAPI_Line; +class SketchAPI_Mirror; class SketchAPI_Point; //-------------------------------------------------------------------------------------- /**\class SketchAPI_Sketch @@ -181,6 +183,12 @@ public: SKETCHAPI_EXPORT std::shared_ptr addArc(const std::string & theExternalName); + /// Add mirror + SKETCHAPI_EXPORT + std::shared_ptr addMirror( + const ModelHighAPI_RefAttr & theMirrorLine, + const std::list > & theObjects); + /// Set angle SKETCHAPI_EXPORT std::shared_ptr setAngle( @@ -236,8 +244,6 @@ public: const ModelHighAPI_RefAttr & thePoint, const ModelHighAPI_RefAttr & theLine); - // TODO(spo): setMirror - /// Set parallel SKETCHAPI_EXPORT std::shared_ptr setParallel( diff --git a/src/SketchAPI/SketchAPI_swig.h b/src/SketchAPI/SketchAPI_swig.h index 32a60738e..c65c853f2 100644 --- a/src/SketchAPI/SketchAPI_swig.h +++ b/src/SketchAPI/SketchAPI_swig.h @@ -13,6 +13,7 @@ #include "SketchAPI_Arc.h" #include "SketchAPI_Circle.h" #include "SketchAPI_Line.h" + #include "SketchAPI_Mirror.h" #include "SketchAPI_Sketch.h" #include "SketchAPI_SketchEntity.h" #include "SketchAPI_Point.h" -- 2.39.2