Salome HOME
Update Python dump to write both standard naming and geometrical selection at once
[modules/shaper.git] / src / ModelHighAPI / ModelHighAPI_Tools.cpp
index 5fe41aee56b05ba374e5443a35f8490d494e7908..3aa3579ad19d4df58f63c5c2c0bdae55f275466d 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2019  CEA/DEN, EDF R&D
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 //
 // You should have received a copy of the GNU Lesser General Public
 // License along with this library; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-// See http://www.salome-platform.org/ or
-// email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 
 #include "ModelHighAPI_Tools.h"
@@ -46,6 +45,7 @@
 #include <ModelAPI_Session.h>
 #include <ModelAPI_Tools.h>
 #include <ModelAPI_ResultPart.h>
+#include <ModelAPI_Events.h>
 //--------------------------------------------------------------------------------------
 #include <Config_ModuleReader.h>
 //--------------------------------------------------------------------------------------
@@ -194,9 +194,9 @@ void fillAttribute(const std::list<ModelHighAPI_Selection> & theValue,
   theAttribute->clear();
 
   if(!theValue.empty()) {
-    std::string aSelectionType;
     const ModelHighAPI_Selection& aSelection = theValue.front();
-    theAttribute->setSelectionType(aSelection.shapeType());
+    GeomAPI_Shape::ShapeType aSelectionType = getShapeType(aSelection);
+    theAttribute->setSelectionType(strByShapeType(aSelectionType));
   }
 
   for (auto it = theValue.begin(); it != theValue.end(); ++it)
@@ -239,6 +239,15 @@ void fillAttribute(const std::list<ModelHighAPI_Integer> & theValue,
     theAttribute->setValue(anIndex, it->intValue()); // use only values, no text support in array
 }
 
+void fillAttribute(const ModelHighAPI_Double & theX,
+                   const ModelHighAPI_Double & theY,
+                   const ModelHighAPI_Double & theZ,
+                   const std::shared_ptr<GeomDataAPI_Point> & theAttribute)
+{
+  theX.fillAttribute(theAttribute, theX, theY, theZ);
+}
+
+
 //==================================================================================================
 GeomAPI_Shape::ShapeType shapeTypeByStr(std::string theShapeTypeStr)
 {
@@ -270,6 +279,41 @@ GeomAPI_Shape::ShapeType shapeTypeByStr(std::string theShapeTypeStr)
   return aShapeType;
 }
 
+std::string strByShapeType(GeomAPI_Shape::ShapeType theShapeType)
+{
+  std::string aShapeTypeStr;
+  switch (theShapeType) {
+  case GeomAPI_Shape::COMPOUND:
+    aShapeTypeStr = "COMPOUND";
+    break;
+  case GeomAPI_Shape::COMPSOLID:
+    aShapeTypeStr = "COMPSOLID";
+    break;
+  case GeomAPI_Shape::SOLID:
+    aShapeTypeStr = "SOLID";
+    break;
+  case GeomAPI_Shape::SHELL:
+    aShapeTypeStr = "SHELL";
+    break;
+  case GeomAPI_Shape::FACE:
+    aShapeTypeStr = "FACE";
+    break;
+  case GeomAPI_Shape::WIRE:
+    aShapeTypeStr = "WIRE";
+    break;
+  case GeomAPI_Shape::EDGE:
+    aShapeTypeStr = "EDGE";
+    break;
+  case GeomAPI_Shape::VERTEX:
+    aShapeTypeStr = "VERTEX";
+    break;
+  default:
+    aShapeTypeStr = "SHAPE";
+    break;
+  }
+  return aShapeTypeStr;
+}
+
 //==================================================================================================
 GeomAPI_Shape::ShapeType getShapeType(const ModelHighAPI_Selection& theSelection)
 {
@@ -294,6 +338,19 @@ GeomAPI_Shape::ShapeType getShapeType(const ModelHighAPI_Selection& theSelection
       aShapeType = shapeTypeByStr(aType);
       break;
     }
+    case ModelHighAPI_Selection::VT_TypeInnerPointPair: {
+      TypeInnerPointPair aPair = theSelection.typeInnerPointPair();
+      std::string aType = aPair.first;
+      aType = aType.substr(0, aType.find_first_of('_'));
+      aShapeType = shapeTypeByStr(aType);
+      break;
+    }
+    case ModelHighAPI_Selection::VT_WeakNamingPair: {
+      TypeWeakNamingPair aPair = theSelection.typeWeakNamingPair();
+      std::string aType = aPair.first;
+      aShapeType = shapeTypeByStr(aType);
+      break;
+    }
   }
 
   return aShapeType;
@@ -325,7 +382,7 @@ std::string strByValueType(const ModelAPI_AttributeTables::ValueType theType)
   return ""; // bad case
 }
 
-/// stores the features information, recoursively stores sub-documetns features
+/// stores the features information, recursively stores sub-documents features
 std::string storeFeatures(const std::string& theDocName, DocumentPtr theDoc,
   std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> >& theStore,
   const bool theCompare) // if false => store
@@ -338,7 +395,7 @@ std::string storeFeatures(const std::string& theDocName, DocumentPtr theDoc,
      }
   }
   // store the model features information: iterate all features
-  int anObjectsCount = 0; // stores the number of compared features for this document to compate
+  int anObjectsCount = 0; // stores the number of compared features for this document to compare
   std::set<std::string> aProcessed; // processed features names (that are in the current document)
 
   // process all objects (features and folders)
@@ -370,11 +427,12 @@ std::string storeFeatures(const std::string& theDocName, DocumentPtr theDoc,
       ModelAPI_Tools::allResults(aFeature, allResults);
       std::list<ResultPtr>::iterator aRes = allResults.begin();
       for(; aRes != allResults.end(); aRes++) {
-        // recoursively store features of sub-documents
+        // recursively store features of sub-documents
         if ((*aRes)->groupName() == ModelAPI_ResultPart::group()) {
           DocumentPtr aDoc = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes)->partDoc();
           if (aDoc.get()) {
-            std::string anError = storeFeatures((*aRes)->data()->name(), aDoc, theStore, theCompare);
+            std::string anError =
+                storeFeatures((*aRes)->data()->name(), aDoc, theStore, theCompare);
             if (!anError.empty())
               return anError;
           }
@@ -401,28 +459,81 @@ std::string storeFeatures(const std::string& theDocName, DocumentPtr theDoc,
 }
 
 //==================================================================================================
-bool checkPythonDump()
+typedef std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> > Storage;
+
+static bool dumpToPython(SessionPtr theSession,
+                         const char* theFilename,
+                         const checkDumpType theSelectionType,
+                         const std::string& theErrorMsgContext)
 {
-  SessionPtr aSession = ModelAPI_Session::get();
+  // 2431: set PartSet as a current document
+  theSession->setActiveDocument(theSession->moduleDocument(), true);
   // dump all to the python file
-  aSession->startOperation("Check python dump");
-  FeaturePtr aDump = aSession->moduleDocument()->addFeature("Dump");
+  theSession->startOperation("Check python dump");
+  FeaturePtr aDump = theSession->moduleDocument()->addFeature("Dump");
   if (aDump.get()) {
-    aDump->string("file_path")->setValue("check_dump.py"); // to the current folder
-    aDump->string("file_format")->setValue("py"); // to the current folder
-    aDump->execute();
+    aDump->string("file_path")->setValue(theFilename);
+    aDump->string("file_format")->setValue("py");
+    aDump->boolean("topological_naming")->setValue(theSelectionType & CHECK_NAMING);
+    aDump->boolean("geometric_selection")->setValue(theSelectionType & CHECK_GEOMETRICAL);
+    aDump->boolean("weak_naming")->setValue(theSelectionType & CHECK_WEAK);
   }
   bool isProblem = !aDump.get() || !aDump->error().empty(); // after "finish" dump will be removed
   if (isProblem && aDump.get()) {
-    std::cout<<"Dump feature error "<<aDump->error()<<std::endl;
-    Events_InfoMessage anErrorMsg(std::string("checkPythonDump"), aDump->error());
+    std::cout << "Dump feature error " << aDump->error() << std::endl;
+    Events_InfoMessage anErrorMsg(theErrorMsgContext, aDump->error());
     anErrorMsg.send();
   }
-  aSession->finishOperation();
-  if (isProblem) {
-    return false; // something is wrong during dump
+  theSession->finishOperation();
+  return !isProblem;
+}
+
+static bool checkDump(SessionPtr theSession,
+                      char* theFilename,
+                      Storage& theStorage,
+                      const std::string& theErrorMsgContext)
+{
+
+  // close all before importation of the script
+  theSession->closeAll();
+
+  // execute the dumped
+  PyGILState_STATE gstate = PyGILState_Ensure(); /* acquire python thread */
+  static char aReadMode[] = "r";
+  FILE* PyFileObject = _Py_fopen(theFilename, aReadMode);
+  PyRun_SimpleFileEx(PyFileObject, theFilename, 1);
+  PyGILState_Release(gstate); /* release python thread */
+
+  // compare with the stored data
+  std::string anError = storeFeatures(
+    theSession->moduleDocument()->kind(), theSession->moduleDocument(), theStorage, true);
+  if (!anError.empty()) {
+    std::cout << anError << std::endl;
+    Events_InfoMessage anErrorMsg(theErrorMsgContext, anError);
+    anErrorMsg.send();
+    return false;
   }
 
+  return true;
+}
+
+bool checkPythonDump(const checkDumpType theCheckType)
+{
+  static const std::string anErrorByNaming("checkPythonDump by naming");
+  static const std::string anErrorByGeometry("checkPythonDump by geometry");
+  static const std::string anErrorByWeak("checkPythonDump by weak naming");
+
+  static char aFileForNamingDump[] = "./check_dump.py";
+  static char aFileForGeometryDump[] = "./check_dump_geo.py";
+  static char aFileForWeakDump[] = "./check_dump_weak.py";
+
+  SessionPtr aSession = ModelAPI_Session::get();
+  // dump with the specified types
+  char* aFileName = theCheckType == CHECK_GEOMETRICAL ? aFileForGeometryDump :
+                   (theCheckType == CHECK_WEAK ? aFileForWeakDump : aFileForNamingDump);
+  if (!dumpToPython(aSession, aFileName, theCheckType, anErrorByNaming))
+    return false;
+
    // map from document name to feature name to feature data
   std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> > aStore;
   std::string anError = storeFeatures(
@@ -432,25 +543,21 @@ bool checkPythonDump()
     anErrorMsg.send();
     return false;
   }
-  // close all before importation of the script
-  aSession->closeAll();
-  // execute the dumped
-  PyGILState_STATE gstate = PyGILState_Ensure(); /* acquire python thread */
-  PyObject* PyFileObject = PyFile_FromString("./check_dump.py", "r");
-  PyRun_SimpleFileEx(PyFile_AsFile(PyFileObject), "./check_dump.py", 1);
-  PyGILState_Release(gstate); /* release python thread */
 
-  // compare with the stored data
-  anError = storeFeatures(
-    aSession->moduleDocument()->kind(), aSession->moduleDocument(), aStore, true);
-  if (!anError.empty()) {
-    std::cout<<anError<<std::endl;
-    Events_InfoMessage anErrorMsg(std::string("checkPythonDump"), anError);
-    anErrorMsg.send();
-    return false;
+  bool isOk = true;
+  if (theCheckType & CHECK_NAMING) {
+    // check dump with the selection by names
+    isOk = checkDump(aSession, aFileForNamingDump, aStore, anErrorByNaming);
+  }
+  if (theCheckType & CHECK_GEOMETRICAL) {
+    // check dump with the selection by geometry
+    isOk = isOk && checkDump(aSession, aFileForGeometryDump, aStore, anErrorByGeometry);
+  }
+  if (theCheckType & CHECK_WEAK) {
+    isOk = isOk && checkDump(aSession, aFileForWeakDump, aStore, anErrorByWeak);
   }
 
-  return true;
+  return isOk;
 }
 
 //--------------------------------------------------------------------------------------