Salome HOME
updated copyright message
[modules/shaper.git] / src / ModelHighAPI / ModelHighAPI_Tools.cpp
index 3aa3579ad19d4df58f63c5c2c0bdae55f275466d..67824fd334e2164555f31f22f34cb7a77136d9ef 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2019  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2023  CEA, EDF
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 #include <GeomDataAPI_Dir.h>
 #include <GeomDataAPI_Point.h>
 #include <GeomDataAPI_Point2D.h>
+#include <GeomDataAPI_Point2DArray.h>
+//--------------------------------------------------------------------------------------
+#include <GeomAlgoAPI_Tools.h>
+//--------------------------------------------------------------------------------------
+#include <Locale_Convert.h>
 //--------------------------------------------------------------------------------------
 #include <ModelAPI_AttributeBoolean.h>
 #include <ModelAPI_AttributeDocRef.h>
 #include <ModelAPI_AttributeDouble.h>
+#include <ModelAPI_AttributeDoubleArray.h>
 #include <ModelAPI_AttributeIntArray.h>
 #include <ModelAPI_AttributeInteger.h>
 #include <ModelAPI_AttributeRefAttr.h>
@@ -164,8 +170,18 @@ void fillAttribute(const std::shared_ptr<ModelAPI_Object> & theValue,
 void fillAttribute(const std::list<std::shared_ptr<ModelAPI_Object> > & theValue,
                    const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
 {
-  theAttribute->clear();
-  for (auto it = theValue.begin(); it != theValue.end(); ++it)
+  int aSize = theAttribute->size();
+  // keep objects at the beginning of the list if they the same
+  auto it = theValue.begin();
+  for (int anIndex = 0; it != theValue.end() && anIndex < aSize; ++it, ++anIndex)
+    if (theAttribute->object(anIndex) != *it) {
+      // remove the tail of the list
+      while (++anIndex <= aSize)
+        theAttribute->removeLast();
+      break;
+    }
+  // append the rest of elements
+  for (; it != theValue.end(); ++it)
     theAttribute->append(*it);
 }
 
@@ -193,7 +209,7 @@ void fillAttribute(const std::list<ModelHighAPI_Selection> & theValue,
 {
   theAttribute->clear();
 
-  if(!theValue.empty()) {
+  if(!theValue.empty() && theAttribute->selectionType().empty()) {
     const ModelHighAPI_Selection& aSelection = theValue.front();
     GeomAPI_Shape::ShapeType aSelectionType = getShapeType(aSelection);
     theAttribute->setSelectionType(strByShapeType(aSelectionType));
@@ -210,6 +226,13 @@ void fillAttribute(const std::string & theValue,
   theAttribute->setValue(theValue);
 }
 
+//--------------------------------------------------------------------------------------
+void fillAttribute(const std::wstring & theValue,
+  const std::shared_ptr<ModelAPI_AttributeString> & theAttribute)
+{
+  theAttribute->setValue(theValue);
+}
+
 //--------------------------------------------------------------------------------------
 void fillAttribute(const char * theValue,
                    const std::shared_ptr<ModelAPI_AttributeString> & theAttribute)
@@ -239,6 +262,29 @@ void fillAttribute(const std::list<ModelHighAPI_Integer> & theValue,
     theAttribute->setValue(anIndex, it->intValue()); // use only values, no text support in array
 }
 
+//--------------------------------------------------------------------------------------
+void fillAttribute(const std::list<ModelHighAPI_Double> & theValue,
+                   const std::shared_ptr<ModelAPI_AttributeDoubleArray> & theAttribute)
+{
+  theAttribute->setSize(int(theValue.size()));
+
+  int anIndex = 0;
+  for (auto it = theValue.begin(); it != theValue.end(); ++it, ++anIndex)
+    theAttribute->setValue(anIndex, it->value()); // use only values, no text support in array
+}
+
+//--------------------------------------------------------------------------------------
+void fillAttribute(const std::list<std::shared_ptr<GeomAPI_Pnt2d> > & theValue,
+                   const std::shared_ptr<GeomDataAPI_Point2DArray> & theAttribute)
+{
+  theAttribute->setSize(int(theValue.size()));
+
+  int anIndex = 0;
+  for (auto it = theValue.begin(); it != theValue.end(); ++it, ++anIndex)
+    theAttribute->setPnt(anIndex, *it);
+}
+
+//--------------------------------------------------------------------------------------
 void fillAttribute(const ModelHighAPI_Double & theX,
                    const ModelHighAPI_Double & theY,
                    const ModelHighAPI_Double & theZ,
@@ -247,14 +293,13 @@ void fillAttribute(const ModelHighAPI_Double & theX,
   theX.fillAttribute(theAttribute, theX, theY, theZ);
 }
 
-
 //==================================================================================================
 GeomAPI_Shape::ShapeType shapeTypeByStr(std::string theShapeTypeStr)
 {
   GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
 
-  std::transform(theShapeTypeStr.begin(), theShapeTypeStr.end(),
-                 theShapeTypeStr.begin(), ::tolower);
+  std::transform(theShapeTypeStr.begin(), theShapeTypeStr.end(), theShapeTypeStr.begin(),
+                 [](char c) { return static_cast<char>(::tolower(c)); });
 
   if(theShapeTypeStr == "compound") {
     aShapeType = GeomAPI_Shape::COMPOUND;
@@ -323,7 +368,7 @@ GeomAPI_Shape::ShapeType getShapeType(const ModelHighAPI_Selection& theSelection
     case ModelHighAPI_Selection::VT_ResultSubShapePair: {
       ResultSubShapePair aPair = theSelection.resultSubShapePair();
       GeomShapePtr aShape = aPair.second;
-      if(!aShape.get()) {
+      if(!aShape.get() && aPair.first.get()) {
         aShape = aPair.first->shape();
       }
       if(!aShape.get()) {
@@ -351,6 +396,8 @@ GeomAPI_Shape::ShapeType getShapeType(const ModelHighAPI_Selection& theSelection
       aShapeType = shapeTypeByStr(aType);
       break;
     }
+    default:
+      break; // do nothing [to avoid compilation warning]
   }
 
   return aShapeType;
@@ -360,7 +407,8 @@ GeomAPI_Shape::ShapeType getShapeType(const ModelHighAPI_Selection& theSelection
 ModelAPI_AttributeTables::ValueType valueTypeByStr(const std::string& theValueTypeStr)
 {
   std::string aType = theValueTypeStr;
-  std::transform(aType.begin(), aType.end(), aType.begin(), ::tolower);
+  std::transform(aType.begin(), aType.end(), aType.begin(),
+                 [](char c) { return static_cast<char>(::tolower(c)); });
   if (aType == "boolean")
     return ModelAPI_AttributeTables::BOOLEAN;
   else if (aType == "integer")
@@ -383,35 +431,44 @@ std::string strByValueType(const ModelAPI_AttributeTables::ValueType theType)
 }
 
 /// 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,
+std::string storeFeatures(const std::wstring& theDocName, DocumentPtr theDoc,
+  std::map<std::wstring, std::map<std::wstring, ModelHighAPI_FeatureStore> >& theStore,
   const bool theCompare) // if false => store
 {
-  std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> >::iterator aDocFind;
+  std::map<std::wstring, std::map<std::wstring, ModelHighAPI_FeatureStore> >::iterator aDocFind;
   if (theCompare) {
      aDocFind = theStore.find(theDocName);
      if (aDocFind == theStore.end()) {
-       return "Document '" + theDocName + "' not found";
+       return "Document '" + Locale::Convert::toString(theDocName) + "' not found";
      }
   }
   // store the model features information: iterate all features
-  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)
+  size_t anObjectsCount = 0; // stores the number of compared features for this document to compare
+  std::set<std::wstring> aProcessed; // processed features names (that are in the current document)
 
   // 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;
+    FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
+    //if (aFeature && aFeature->getKind() == "SketchConstraintCoincidenceInternal")
+
+    if (aFeature) {
+      std::string aStr = aFeature->getKind().substr(0, 16);
+      if (aStr == "SketchConstraint")
+        continue; // no need to dump and check constraints
+    }
     if (theCompare) {
-      std::map<std::string, ModelHighAPI_FeatureStore>::iterator
+      std::map<std::wstring, ModelHighAPI_FeatureStore>::iterator
         anObjFind = aDocFind->second.find(anObject->data()->name());
       if (anObjFind == aDocFind->second.end()) {
-        return "Document '" + theDocName + "' feature '" + anObject->data()->name() + "' not found";
+        return "Document '" + Locale::Convert::toString(theDocName)
+          + "' feature '" + Locale::Convert::toString(anObject->data()->name()) + "' not found";
       }
       std::string anError = anObjFind->second.compare(anObject);
       if (!anError.empty()) {
-        anError = "Document " + theDocName + " " + anError;
+        anError = "Document " + Locale::Convert::toString(theDocName) + " " + anError;
         return anError;
       }
       anObjectsCount++;
@@ -420,7 +477,6 @@ std::string storeFeatures(const std::string& theDocName, DocumentPtr theDoc,
       theStore[theDocName][anObject->data()->name()] = ModelHighAPI_FeatureStore(anObject);
     }
 
-    FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
     if (aFeature) {
       // iterate all results of this feature
       std::list<ResultPtr> allResults;
@@ -444,25 +500,26 @@ std::string storeFeatures(const std::string& theDocName, DocumentPtr theDoc,
   if (theCompare) {
     if (aDocFind->second.size() != anObjectsCount) {
       // search for disappeared feature
-      std::string aLostName;
-      std::map<std::string, ModelHighAPI_FeatureStore>::iterator aLostIter;
+      std::wstring aLostName;
+      std::map<std::wstring, ModelHighAPI_FeatureStore>::iterator aLostIter;
       for(aLostIter = aDocFind->second.begin(); aLostIter != aDocFind->second.end(); aLostIter++) {
         if (aProcessed.find(aLostIter->first) == aProcessed.end()) {
           aLostName = aLostIter->first;
         }
       }
-      return "For document '" + theDocName +
-        "' the number of features is decreased, there is no feature '" + aLostName + "'";
+      return "For document '" + Locale::Convert::toString(theDocName) +
+        "' the number of features is decreased, there is no feature '" +
+        Locale::Convert::toString(aLostName) + "'";
     }
   }
   return ""; // ok
 }
 
 //==================================================================================================
-typedef std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> > Storage;
+typedef std::map<std::wstring, std::map<std::wstring, ModelHighAPI_FeatureStore> > Storage;
 
 static bool dumpToPython(SessionPtr theSession,
-                         const char* theFilename,
+                         const std::string& theFilename,
                          const checkDumpType theSelectionType,
                          const std::string& theErrorMsgContext)
 {
@@ -474,9 +531,11 @@ static bool dumpToPython(SessionPtr theSession,
   if (aDump.get()) {
     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);
+    std::string aTrek = GeomAlgoAPI_Tools::File_Tools::path(theFilename);
+    aDump->string("dump_dir")->setValue(aTrek);
+    aDump->boolean("topological_naming")->setValue((theSelectionType & CHECK_NAMING) != 0);
+    aDump->boolean("geometric_selection")->setValue((theSelectionType & CHECK_GEOMETRICAL) != 0);
+    aDump->boolean("weak_naming")->setValue((theSelectionType & CHECK_WEAK) != 0);
   }
   bool isProblem = !aDump.get() || !aDump->error().empty(); // after "finish" dump will be removed
   if (isProblem && aDump.get()) {
@@ -489,7 +548,7 @@ static bool dumpToPython(SessionPtr theSession,
 }
 
 static bool checkDump(SessionPtr theSession,
-                      char* theFilename,
+                      const char* theFilename,
                       Storage& theStorage,
                       const std::string& theErrorMsgContext)
 {
@@ -500,13 +559,14 @@ static bool checkDump(SessionPtr theSession,
   // execute the dumped
   PyGILState_STATE gstate = PyGILState_Ensure(); /* acquire python thread */
   static char aReadMode[] = "r";
-  FILE* PyFileObject = _Py_fopen(theFilename, aReadMode);
+  FILE* PyFileObject = 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);
+  std::string anError =
+    storeFeatures(Locale::Convert::toWString(theSession->moduleDocument()->kind()),
+    theSession->moduleDocument(), theStorage, true);
   if (!anError.empty()) {
     std::cout << anError << std::endl;
     Events_InfoMessage anErrorMsg(theErrorMsgContext, anError);
@@ -517,27 +577,42 @@ static bool checkDump(SessionPtr theSession,
   return true;
 }
 
-bool checkPythonDump(const checkDumpType theCheckType)
+bool checkPyDump(
+#ifdef _DEBUG
+  const std::string&, const std::string&, const std::string&,
+#else
+  const std::string& theFilenameNaming,
+  const std::string& theFilenameGeo,
+  const std::string& theFilenameWeak,
+#endif
+  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";
+#ifdef _DEBUG
+  std::string aFileForNamingDump("./check_dump.py");
+  std::string aFileForGeometryDump("./check_dump_geo.py");
+  std::string aFileForWeakDump("./check_dump_weak.py");
+#else
+  std::string aFileForNamingDump = theFilenameNaming;
+  std::string aFileForGeometryDump = theFilenameGeo;
+  std::string aFileForWeakDump = theFilenameWeak;
+#endif
 
   SessionPtr aSession = ModelAPI_Session::get();
   // dump with the specified types
-  char* aFileName = theCheckType == CHECK_GEOMETRICAL ? aFileForGeometryDump :
-                   (theCheckType == CHECK_WEAK ? aFileForWeakDump : aFileForNamingDump);
+  std::string 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(
-    aSession->moduleDocument()->kind(), aSession->moduleDocument(), aStore, false);
+  std::map<std::wstring, std::map<std::wstring, ModelHighAPI_FeatureStore> > aStore;
+  std::string anError =
+    storeFeatures(Locale::Convert::toWString(aSession->moduleDocument()->kind()),
+    aSession->moduleDocument(), aStore, false);
   if (!anError.empty()) {
     Events_InfoMessage anErrorMsg(std::string("checkPythonDump"), anError);
     anErrorMsg.send();
@@ -547,14 +622,14 @@ bool checkPythonDump(const checkDumpType theCheckType)
   bool isOk = true;
   if (theCheckType & CHECK_NAMING) {
     // check dump with the selection by names
-    isOk = checkDump(aSession, aFileForNamingDump, aStore, anErrorByNaming);
+    isOk = checkDump(aSession, aFileForNamingDump.c_str(), aStore, anErrorByNaming);
   }
   if (theCheckType & CHECK_GEOMETRICAL) {
     // check dump with the selection by geometry
-    isOk = isOk && checkDump(aSession, aFileForGeometryDump, aStore, anErrorByGeometry);
+    isOk = isOk && checkDump(aSession, aFileForGeometryDump.c_str(), aStore, anErrorByGeometry);
   }
   if (theCheckType & CHECK_WEAK) {
-    isOk = isOk && checkDump(aSession, aFileForWeakDump, aStore, anErrorByWeak);
+    isOk = isOk && checkDump(aSession, aFileForWeakDump.c_str(), aStore, anErrorByWeak);
   }
 
   return isOk;