Salome HOME
Issue #2481: Application error when create fillet
[modules/shaper.git] / src / ModelHighAPI / ModelHighAPI_Tools.cpp
index 76b914b19ab3d0ba9d588e5426da67e674612f29..ce7245fba164afd585ffec1eabb1f85f06ab2123 100644 (file)
@@ -1,11 +1,23 @@
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
-// Name   : ModelHighAPI_Tools.cpp
-// Purpose:
+// Copyright (C) 2014-2017  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
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// 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
+//
+// See http://www.salome-platform.org/ or
+// email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
 //
-// History:
-// 07/06/16 - Sergey POKHODENKO - Creation of the file
 
-//--------------------------------------------------------------------------------------
 #include "ModelHighAPI_Tools.h"
 #include <ModelHighAPI_FeatureStore.h>
 //--------------------------------------------------------------------------------------
@@ -29,6 +41,7 @@
 #include <ModelAPI_AttributeSelection.h>
 #include <ModelAPI_AttributeSelectionList.h>
 #include <ModelAPI_AttributeString.h>
+#include <ModelAPI_AttributeStringArray.h>
 #include <ModelAPI_AttributeDoubleArray.h>
 #include <ModelAPI_Session.h>
 #include <ModelAPI_Tools.h>
@@ -161,8 +174,10 @@ void fillAttribute(const std::list<ModelHighAPI_Selection> & theValue,
                    const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
 {
   theAttribute->clear();
-  for (auto it = theValue.begin(); it != theValue.end(); ++it)
-    theAttribute->append(it->resultSubShapePair().first); // use only context
+  for (auto it = theValue.begin(); it != theValue.end(); ++it) {
+    if (it->resultSubShapePair().first)
+      theAttribute->append(it->resultSubShapePair().first); // use only context
+  }
 }
 
 //--------------------------------------------------------------------------------------
@@ -194,12 +209,36 @@ void fillAttribute(const std::string & theValue,
 {
   theAttribute->setValue(theValue);
 }
+
+//--------------------------------------------------------------------------------------
 void fillAttribute(const char * theValue,
                    const std::shared_ptr<ModelAPI_AttributeString> & theAttribute)
 {
   theAttribute->setValue(theValue);
 }
 
+//--------------------------------------------------------------------------------------
+void fillAttribute(const std::list<std::string> & theValue,
+                   const std::shared_ptr<ModelAPI_AttributeStringArray> & theAttribute)
+{
+  theAttribute->setSize(int(theValue.size()));
+
+  int anIndex = 0;
+  for (auto it = theValue.begin(); it != theValue.end(); ++it, ++anIndex)
+    theAttribute->setValue(anIndex, *it);
+}
+
+//--------------------------------------------------------------------------------------
+void fillAttribute(const std::list<ModelHighAPI_Integer> & theValue,
+                   const std::shared_ptr<ModelAPI_AttributeIntArray> & theAttribute)
+{
+  theAttribute->setSize(int(theValue.size()));
+
+  int anIndex = 0;
+  for (auto it = theValue.begin(); it != theValue.end(); ++it, ++anIndex)
+    theAttribute->setValue(anIndex, it->intValue()); // use only values, no text support in array
+}
+
 //==================================================================================================
 GeomAPI_Shape::ShapeType shapeTypeByStr(std::string theShapeTypeStr)
 {
@@ -260,6 +299,32 @@ GeomAPI_Shape::ShapeType getShapeType(const ModelHighAPI_Selection& theSelection
   return aShapeType;
 }
 
+//--------------------------------------------------------------------------------------
+ModelAPI_AttributeTables::ValueType valueTypeByStr(const std::string& theValueTypeStr)
+{
+  std::string aType = theValueTypeStr;
+  std::transform(aType.begin(), aType.end(), aType.begin(), ::tolower);
+  if (aType == "boolean")
+    return ModelAPI_AttributeTables::BOOLEAN;
+  else if (aType == "integer")
+    return ModelAPI_AttributeTables::INTEGER;
+  else if (aType == "string")
+    return ModelAPI_AttributeTables::STRING;
+  return ModelAPI_AttributeTables::DOUBLE; // default
+}
+
+//--------------------------------------------------------------------------------------
+std::string strByValueType(const ModelAPI_AttributeTables::ValueType theType)
+{
+  switch(theType) {
+  case ModelAPI_AttributeTables::BOOLEAN: return "BOOLEAN";
+  case ModelAPI_AttributeTables::INTEGER: return "INTEGER";
+  case ModelAPI_AttributeTables::DOUBLE: return "DOUBLE";
+  case ModelAPI_AttributeTables::STRING: return "STRING";
+  }
+  return ""; // bad case
+}
+
 /// stores the features information, recoursively stores sub-documetns features
 std::string storeFeatures(const std::string& theDocName, DocumentPtr theDoc,
   std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> >& theStore,
@@ -273,46 +338,54 @@ std::string storeFeatures(const std::string& theDocName, DocumentPtr theDoc,
      }
   }
   // store the model features information: iterate all features
-  int aFeaturesCount = 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 compate
   std::set<std::string> aProcessed; // processed features names (that are in the current document)
-  std::list<FeaturePtr> allFeatures = theDoc->allFeatures();
-  std::list<FeaturePtr>::iterator allIter = allFeatures.begin();
-  for(; allIter != allFeatures.end(); allIter++) {
-    FeaturePtr aFeat = *allIter;
+
+  // process all objects (features and folders)
+  std::list<ObjectPtr> allObjects = theDoc->allObjects();
+  std::list<ObjectPtr>::iterator allIter = allObjects.begin();
+  for(; allIter != allObjects.end(); allIter++) {
+    ObjectPtr anObject = *allIter;
     if (theCompare) {
       std::map<std::string, ModelHighAPI_FeatureStore>::iterator
-        aFeatFind = aDocFind->second.find(aFeat->name());
-      if (aFeatFind == aDocFind->second.end()) {
-        return "Document '" + theDocName + "' feature '" + aFeat->name() + "' not found";
+        anObjFind = aDocFind->second.find(anObject->data()->name());
+      if (anObjFind == aDocFind->second.end()) {
+        return "Document '" + theDocName + "' feature '" + anObject->data()->name() + "' not found";
       }
-      std::string anError = aFeatFind->second.compare(aFeat);
+      std::string anError = anObjFind->second.compare(anObject);
       if (!anError.empty()) {
+        anError = "Document " + theDocName + " " + anError;
         return anError;
       }
-      aFeaturesCount++;
-      aProcessed.insert(aFeat->name());
+      anObjectsCount++;
+      aProcessed.insert(anObject->data()->name());
     } else {
-      theStore[theDocName][aFeat->name()] = ModelHighAPI_FeatureStore(aFeat);
+      theStore[theDocName][anObject->data()->name()] = ModelHighAPI_FeatureStore(anObject);
     }
-    // iterate all results of this feature
-    std::list<ResultPtr> allResults;
-    ModelAPI_Tools::allResults(aFeat, allResults);
-    std::list<ResultPtr>::iterator aRes = allResults.begin();
-    for(; aRes != allResults.end(); aRes++) {
-      // recoursively 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);
-          if (!anError.empty())
-            return anError;
+
+    FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
+    if (aFeature) {
+      // iterate all results of this feature
+      std::list<ResultPtr> allResults;
+      ModelAPI_Tools::allResults(aFeature, allResults);
+      std::list<ResultPtr>::iterator aRes = allResults.begin();
+      for(; aRes != allResults.end(); aRes++) {
+        // recoursively 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);
+            if (!anError.empty())
+              return anError;
+          }
         }
       }
     }
   }
   // checks the number of compared features
   if (theCompare) {
-    if (aDocFind->second.size() != aFeaturesCount) {
+    if (aDocFind->second.size() != anObjectsCount) {
       // search for disappeared feature
       std::string aLostName;
       std::map<std::string, ModelHighAPI_FeatureStore>::iterator aLostIter;
@@ -332,6 +405,8 @@ std::string storeFeatures(const std::string& theDocName, DocumentPtr theDoc,
 bool checkPythonDump()
 {
   SessionPtr aSession = ModelAPI_Session::get();
+  // 2431: set PartSet as a current document
+  aSession->setActiveDocument(aSession->moduleDocument(), true);
   // dump all to the python file
   aSession->startOperation("Check python dump");
   FeaturePtr aDump = aSession->moduleDocument()->addFeature("Dump");
@@ -341,9 +416,15 @@ bool checkPythonDump()
     aDump->execute();
   }
   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());
+    anErrorMsg.send();
+  }
   aSession->finishOperation();
-  if (isProblem)
+  if (isProblem) {
     return false; // something is wrong during dump
+  }
 
    // map from document name to feature name to feature data
   std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> > aStore;
@@ -358,8 +439,10 @@ bool checkPythonDump()
   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);
+  static char aDumpName[] = "./check_dump.py";
+  static char aReadMode[] = "r";
+  PyObject* PyFileObject = PyFile_FromString(aDumpName, aReadMode);
+  PyRun_SimpleFileEx(PyFile_AsFile(PyFileObject), aDumpName, 1);
   PyGILState_Release(gstate); /* release python thread */
 
   // compare with the stored data