Salome HOME
Task #3230: Sketcher: create a curve passing through selected points or vertices...
[modules/shaper.git] / src / ModelHighAPI / ModelHighAPI_Tools.cpp
index ffdab15225c33898e756327f385a0401c7d900ea..936775e47ec5e48f9366e107d97dbf8b4f214c2b 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"
 #include <GeomDataAPI_Dir.h>
 #include <GeomDataAPI_Point.h>
 #include <GeomDataAPI_Point2D.h>
+#include <GeomDataAPI_Point2DArray.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>
@@ -194,7 +195,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));
@@ -240,6 +241,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,
@@ -248,7 +272,6 @@ void fillAttribute(const ModelHighAPI_Double & theX,
   theX.fillAttribute(theAttribute, theX, theY, theZ);
 }
 
-
 //==================================================================================================
 GeomAPI_Shape::ShapeType shapeTypeByStr(std::string theShapeTypeStr)
 {
@@ -346,6 +369,12 @@ GeomAPI_Shape::ShapeType getShapeType(const ModelHighAPI_Selection& theSelection
       aShapeType = shapeTypeByStr(aType);
       break;
     }
+    case ModelHighAPI_Selection::VT_WeakNamingPair: {
+      TypeWeakNamingPair aPair = theSelection.typeWeakNamingPair();
+      std::string aType = aPair.first;
+      aShapeType = shapeTypeByStr(aType);
+      break;
+    }
   }
 
   return aShapeType;
@@ -377,7 +406,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
@@ -390,7 +419,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)
@@ -398,6 +427,14 @@ std::string storeFeatures(const std::string& theDocName, DocumentPtr theDoc,
   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
         anObjFind = aDocFind->second.find(anObject->data()->name());
@@ -415,14 +452,13 @@ 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;
       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()) {
@@ -457,8 +493,8 @@ std::string storeFeatures(const std::string& theDocName, DocumentPtr theDoc,
 typedef std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> > Storage;
 
 static bool dumpToPython(SessionPtr theSession,
-                         const char* theFilename,
-                         const char* theSelectionType,
+                         const std::string& theFilename,
+                         const checkDumpType theSelectionType,
                          const std::string& theErrorMsgContext)
 {
   // 2431: set PartSet as a current document
@@ -469,8 +505,9 @@ static bool dumpToPython(SessionPtr theSession,
   if (aDump.get()) {
     aDump->string("file_path")->setValue(theFilename);
     aDump->string("file_format")->setValue("py");
-    aDump->string("selection_type")->setValue(theSelectionType);
-    aDump->execute();
+    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()) {
@@ -483,7 +520,7 @@ static bool dumpToPython(SessionPtr theSession,
 }
 
 static bool checkDump(SessionPtr theSession,
-                      char* theFilename,
+                      const char* theFilename,
                       Storage& theStorage,
                       const std::string& theErrorMsgContext)
 {
@@ -511,20 +548,30 @@ static bool checkDump(SessionPtr theSession,
   return true;
 }
 
-bool checkPythonDump()
+bool checkPyDump(const std::string& theFilenameNaming,
+                 const std::string& theFilenameGeo,
+                 const std::string& theFilenameWeak,
+                 const checkDumpType theCheckType)
 {
   static const std::string anErrorByNaming("checkPythonDump by naming");
   static const std::string anErrorByGeometry("checkPythonDump by geometry");
-
-  static char aFileForNamingDump[] = "./check_dump_byname.py";
-  static char aFileForGeometryDump[] = "./check_dump_bygeom.py";
+  static const std::string anErrorByWeak("checkPythonDump by weak naming");
+
+#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 selection by names
-  if (!dumpToPython(aSession, aFileForNamingDump, "topological_naming", anErrorByNaming))
-    return false;
-  // dump with the selection by geometry
-  if (!dumpToPython(aSession, aFileForGeometryDump, "geometric_selection", anErrorByGeometry))
+  // dump with the specified types
+  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
@@ -537,10 +584,18 @@ bool checkPythonDump()
     return false;
   }
 
-  // check dump with the selection by names
-  bool isOk = checkDump(aSession, aFileForNamingDump, aStore, anErrorByNaming);
-  // check dump with the selection by geometry
-  isOk = isOk && checkDump(aSession, aFileForGeometryDump, aStore, anErrorByGeometry);
+  bool isOk = true;
+  if (theCheckType & CHECK_NAMING) {
+    // check dump with the selection by names
+    isOk = checkDump(aSession, aFileForNamingDump.c_str(), aStore, anErrorByNaming);
+  }
+  if (theCheckType & CHECK_GEOMETRICAL) {
+    // check dump with the selection by geometry
+    isOk = isOk && checkDump(aSession, aFileForGeometryDump.c_str(), aStore, anErrorByGeometry);
+  }
+  if (theCheckType & CHECK_WEAK) {
+    isOk = isOk && checkDump(aSession, aFileForWeakDump.c_str(), aStore, anErrorByWeak);
+  }
 
   return isOk;
 }