Salome HOME
Added a unit test for Parts remove and duplicate API
authormpv <mpv@opencascade.com>
Tue, 13 Dec 2016 07:31:36 +0000 (10:31 +0300)
committermpv <mpv@opencascade.com>
Tue, 13 Dec 2016 07:31:36 +0000 (10:31 +0300)
src/PartSetAPI/CMakeLists.txt
src/PartSetAPI/PartSetAPI_Part.cpp
src/PartSetAPI/PartSetAPI_Part.h
src/PartSetAPI/Test/TestRemoveDuplicate.py [new file with mode: 0644]

index 8d75a233af4fb09660886de4a9307c9b398fc2b5..2a112976e2b62e3a21d3012647896771da5d4778 100644 (file)
@@ -1,6 +1,7 @@
 ## Copyright (C) 2014-20xx CEA/DEN, EDF R&D
 
 INCLUDE(Common)
+INCLUDE(UnitTest)
 
 SET(PROJECT_HEADERS
   PartSetAPI.h
@@ -67,3 +68,5 @@ ENDIF(WIN32)
 INSTALL(TARGETS _PartSetAPI DESTINATION ${SHAPER_INSTALL_SWIG})
 INSTALL(TARGETS PartSetAPI DESTINATION ${SHAPER_INSTALL_BIN})
 INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/PartSetAPI.py DESTINATION ${SHAPER_INSTALL_SWIG})
+
+ADD_UNIT_TESTS(TestRemoveDuplicate.py)
index 63b3def93b085a5e39e54cdd75969212cb934e46..be16425840d87428415456b76e96d55584e567b6 100644 (file)
@@ -9,7 +9,9 @@
 #include "PartSetAPI_Part.h"
 //--------------------------------------------------------------------------------------
 #include <ModelAPI_ResultPart.h>
+#include <ModelAPI_Session.h>
 #include <ModelHighAPI_Dumper.h>
+#include <ModelHighAPI_Selection.h>
 //--------------------------------------------------------------------------------------
 #include <PartSetPlugin_Duplicate.h>
 #include <PartSetPlugin_Remove.h>
@@ -47,15 +49,46 @@ PartPtr addPart(const std::shared_ptr<ModelAPI_Document> & thePart)
   return PartPtr(new PartSetAPI_Part(aFeature));
 }
 
-PartPtr duplicatePart(const std::shared_ptr<ModelAPI_Document> & thePart)
+PartPtr duplicatePart(const ModelHighAPI_Interface& thePart)
 {
-  std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(PartSetPlugin_Duplicate::ID());
-  aFeature->execute();
-  return PartPtr(new PartSetAPI_Part(aFeature));
+  PartPtr aResult;
+  // only the active part is duplicated, so, activate it
+  FeaturePtr aPartFeature = thePart.feature();
+  if (aPartFeature) {
+    // duplication of the current part is performed
+    DocumentPtr aPartSet = ModelAPI_Session::get()->moduleDocument();
+    aPartSet->setCurrentFeature(aPartFeature, false);
+    DocumentPtr aPartDoc =
+      std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult())->partDoc();
+    if (aPartDoc.get()) {
+      ModelAPI_Session::get()->setActiveDocument(aPartDoc);
+      FeaturePtr aDuplicate = aPartDoc->addFeature(PartSetPlugin_Duplicate::ID());
+      //aDuplicate->execute(); // it is executed at creation because it is action
+      FeaturePtr aNewPart = aPartSet->currentFeature(false); // a copy becomes a current
+      if (aNewPart.get()) {
+        aResult = PartPtr(new PartSetAPI_Part(aNewPart));
+        DocumentPtr aNewDoc = std::dynamic_pointer_cast<ModelAPI_ResultPart>(
+          aNewPart->firstResult())->partDoc();
+        ModelAPI_Session::get()->setActiveDocument(aNewDoc);
+      }
+    }
+  }
+  return aResult;
 }
 
-void removePart(const std::shared_ptr<ModelAPI_Document> & thePart)
+void removePart(const ModelHighAPI_Interface& thePart)
 {
-  std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(PartSetPlugin_Remove::ID());
-  aFeature->execute();
+  FeaturePtr aPartFeature = thePart.feature();
+  if (aPartFeature) {
+    // duplication of the current part is performed
+    DocumentPtr aPartSet = ModelAPI_Session::get()->moduleDocument();
+    aPartSet->setCurrentFeature(aPartFeature, false);
+    DocumentPtr aPartDoc =
+      std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult())->partDoc();
+    if (aPartDoc.get()) {
+      ModelAPI_Session::get()->setActiveDocument(aPartDoc);
+      aPartDoc->addFeature(PartSetPlugin_Remove::ID());
+      ModelAPI_Session::get()->setActiveDocument(aPartSet);
+    }
+  }
 }
index ccdcfdb5a2fce2f6773dc577c6f7315f8d4fd0d1..90977f01cffae1db546f95ab40af293707588ba4 100644 (file)
@@ -56,13 +56,13 @@ PartPtr addPart(const std::shared_ptr<ModelAPI_Document> & thePartSet);
  * \brief Duplicate Part feature
  */
 PARTSETAPI_EXPORT
-PartPtr duplicatePart(const std::shared_ptr<ModelAPI_Document> & thePart);
+PartPtr duplicatePart(const ModelHighAPI_Interface& thePart);
 
 /**\ingroup CPPHighAPI
  * \brief Remove Part feature
  */
 PARTSETAPI_EXPORT
-void removePart(const std::shared_ptr<ModelAPI_Document> & thePart);
+void removePart(const ModelHighAPI_Interface& thePart);
 
 //--------------------------------------------------------------------------------------
 //--------------------------------------------------------------------------------------
diff --git a/src/PartSetAPI/Test/TestRemoveDuplicate.py b/src/PartSetAPI/Test/TestRemoveDuplicate.py
new file mode 100644 (file)
index 0000000..466d310
--- /dev/null
@@ -0,0 +1,51 @@
+# Created a Part Remove/Duplicate API functionality since from low level and python dump it is not
+# tested: feature history infor is not stored.
+
+import model
+
+# prepare axis created by PartSet and Part points
+model.begin()
+PartSet = model.moduleDocument()
+Part_1 = model.addPart(PartSet)
+Part_1_doc = Part_1.document()
+Point_2 = model.addPoint(Part_1_doc, 100, 50, 20)
+Axis_4 = model.addAxis(Part_1_doc, model.selection("VERTEX", "PartSet/Origin"), model.selection("VERTEX", "Point_1"))
+model.do()
+
+# Duplicate this part twice, duplicate the first Copy also
+Copy_1 = model.duplicatePart(Part_1)
+Copy_2 = model.duplicatePart(Part_1)
+Copy_3 = model.duplicatePart(Copy_1)
+model.do()
+
+# check the intermediate result
+assert PartSet.size("Parts") == 4
+i = 0
+for n in ["Part_1", "Part_3", "Part_2", "Part_4"]:
+  assert PartSet.object("Parts", i).data().name() == n
+  i = i + 1
+
+# Remove the middle Part
+model.removePart(Copy_2)
+model.do()
+
+# check the intermediate result
+assert PartSet.size("Parts") == 3
+i = 0
+for n in ["Part_1", "Part_2", "Part_4"]:
+  assert PartSet.object("Parts", i).data().name() == n
+  i = i + 1
+
+# Remove a first copy
+model.removePart(Copy_1)
+model.end()
+
+# Undo the last remove
+model.undo()
+
+# Check the result
+assert PartSet.size("Parts") == 3
+i = 0
+for n in ["Part_1", "Part_2", "Part_4"]:
+  assert PartSet.object("Parts", i).data().name() == n
+  i = i + 1