Salome HOME
Issue #2387: Sketcher conservation of constraints
[modules/shaper.git] / src / ModelHighAPI / ModelHighAPI_FeatureStore.cpp
index ac702d9689d881026d432b8d1d4a452534bb3e47..f0806abc45101a31433907385a62b9f688faf41b 100644 (file)
@@ -1,8 +1,22 @@
-// Copyright (C) 2016-20xx CEA/DEN, EDF R&D -->
-
-// File:        ModelHighAPI_FeatureStore.cpp
-// Created:     12 August 2016
-// Author:      Mikhail PONIKAROV
+// 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>
+//
 
 #include <ModelHighAPI_FeatureStore.h>
 
@@ -21,7 +35,9 @@
 #include <ModelAPI_AttributeSelection.h>
 #include <ModelAPI_AttributeSelectionList.h>
 #include <ModelAPI_AttributeString.h>
+#include <ModelAPI_AttributeStringArray.h>
 #include <ModelAPI_AttributeDoubleArray.h>
+#include <ModelAPI_AttributeTables.h>
 #include <ModelAPI_Validator.h>
 
 #include <GeomDataAPI_Dir.h>
 #include <ios>
 #include <cmath>
 
-#define PRECISION 7
+#define PRECISION 6
 #define TOLERANCE (1.e-7)
 
-ModelHighAPI_FeatureStore::ModelHighAPI_FeatureStore(FeaturePtr theFeature) {
-  storeData(theFeature->data(), myAttrs);
-  // iterate results to store
-  std::list<ResultPtr> allResults;
-  ModelAPI_Tools::allResults(theFeature, allResults);
-  std::list<ResultPtr>::iterator aRes = allResults.begin();
-  for(; aRes != allResults.end(); aRes++) {
-    std::map<std::string, std::string> aResDump;
-    storeData((*aRes)->data(), aResDump);
-    myRes.push_back(aResDump);
+ModelHighAPI_FeatureStore::ModelHighAPI_FeatureStore(ObjectPtr theObject) {
+  storeData(theObject->data(), myAttrs);
+
+  FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
+  if (aFeature) {
+    // iterate results to store
+    std::list<ResultPtr> allResults;
+    ModelAPI_Tools::allResults(aFeature, allResults);
+    std::list<ResultPtr>::iterator aRes = allResults.begin();
+    for(; aRes != allResults.end(); aRes++) {
+      std::map<std::string, std::string> aResDump;
+      storeData((*aRes)->data(), aResDump);
+      myRes.push_back(aResDump);
+    }
   }
 }
 
-std::string ModelHighAPI_FeatureStore::compare(FeaturePtr theFeature) {
-  std::string anError = compareData(theFeature->data(), myAttrs);
+std::string ModelHighAPI_FeatureStore::compare(ObjectPtr theObject) {
+  std::string anError = compareData(theObject->data(), myAttrs);
   if (!anError.empty()) {
-    return "Features '" + theFeature->name() + "' differ:" + anError;
-  }
-  std::list<ResultPtr> allResults;
-  ModelAPI_Tools::allResults(theFeature, allResults);
-  std::list<ResultPtr>::iterator aRes = allResults.begin();
-  std::list<std::map<std::string, std::string> >::iterator aResIter = myRes.begin();
-  for(; aRes != allResults.end() && aResIter != myRes.end(); aRes++, aResIter++) {
-    anError = compareData((*aRes)->data(), *aResIter);
-    if (!anError.empty())
-      return "Results of feature '" + theFeature->name() + "' '" + (*aRes)->data()->name() + 
-      "' differ:" + anError;
+    return "Features '" + theObject->data()->name() + "' differ:" + anError;
   }
-  if (aRes != allResults.end()) {
-    return "Current model has more results '" + (*aRes)->data()->name() + "'";
-  }
-  if (aResIter != myRes.end()) {
-    return "Original model had more results '" + (*aResIter)["__name__"] + "'";
+
+  FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
+  if (aFeature) {
+    std::list<ResultPtr> allResults;
+    ModelAPI_Tools::allResults(aFeature, allResults);
+    std::list<ResultPtr>::iterator aRes = allResults.begin();
+    std::list<std::map<std::string, std::string> >::iterator aResIter = myRes.begin();
+    for(; aRes != allResults.end() && aResIter != myRes.end(); aRes++, aResIter++) {
+      anError = compareData((*aRes)->data(), *aResIter);
+      if (!anError.empty())
+        return "Results of feature '" + aFeature->name() + "' '" + (*aRes)->data()->name() +
+        "' differ:" + anError;
+    }
+    if (aRes != allResults.end()) {
+      return "Current model has more results '" + (*aRes)->data()->name() + "'";
+    }
+    if (aResIter != myRes.end()) {
+      return "Original model had more results '" + (*aResIter)["__name__"] + "'";
+    }
   }
   return ""; // ok
 }
 
-void ModelHighAPI_FeatureStore::storeData(std::shared_ptr<ModelAPI_Data> theData, 
+void ModelHighAPI_FeatureStore::storeData(std::shared_ptr<ModelAPI_Data> theData,
   std::map<std::string, std::string>& theAttrs)
 {
-  theAttrs["__name__"] = theData->name(); // store name to keep also this information and output if needed
+  // store name to keep also this information and output if needed
+  theAttrs["__name__"] = theData->name();
   std::list<std::shared_ptr<ModelAPI_Attribute> > allAttrs = theData->attributes("");
   std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = allAttrs.begin();
   for(; anAttr != allAttrs.end(); anAttr++) {
@@ -87,11 +112,12 @@ void ModelHighAPI_FeatureStore::storeData(std::shared_ptr<ModelAPI_Data> theData
   }
   ResultPtr aShapeOwner = std::dynamic_pointer_cast<ModelAPI_Result>(theData->owner());
   if (aShapeOwner.get() && aShapeOwner->shape().get()) {
-    theAttrs["__shape__"] = dumpShape(aShapeOwner->shape());
+    std::shared_ptr<GeomAPI_Shape> aShape = aShapeOwner->shape();
+    theAttrs["__shape__"] = dumpShape(aShape);
   }
 }
 
-std::string ModelHighAPI_FeatureStore::compareData(std::shared_ptr<ModelAPI_Data> theData, 
+std::string ModelHighAPI_FeatureStore::compareData(std::shared_ptr<ModelAPI_Data> theData,
   std::map<std::string, std::string>& theAttrs)
 {
   std::map<std::string, std::string> aThis;
@@ -102,7 +128,7 @@ std::string ModelHighAPI_FeatureStore::compareData(std::shared_ptr<ModelAPI_Data
       return "original model had no attribute '" + aThisIter->first + "'";
     }
     if (theAttrs[aThisIter->first] != aThisIter->second) {
-      return "attribute '" + aThisIter->first + "' is different (original != current) '" + 
+      return "attribute '" + aThisIter->first + "' is different (original != current) '" +
         theAttrs[aThisIter->first] + "' != '" + aThisIter->second + "'";
     }
   }
@@ -116,12 +142,13 @@ std::string ModelHighAPI_FeatureStore::compareData(std::shared_ptr<ModelAPI_Data
   return "";
 }
 
-static void dumpArray(std::ostringstream& theOutput, const double theArray[], int theSize)
+static void dumpArray(std::ostringstream& theOutput, const double theArray[],
+                      int theSize, int thePrecision = PRECISION)
 {
   for (int i = 0; i < theSize; ++i) {
     if (i > 0)
       theOutput << " ";
-    theOutput << std::fixed << setprecision(PRECISION)
+    theOutput << std::fixed << setprecision(thePrecision)
               << (fabs(theArray[i]) < TOLERANCE ? 0.0 : theArray[i]);
   }
 }
@@ -141,7 +168,7 @@ std::string ModelHighAPI_FeatureStore::dumpAttr(const AttributePtr& theAttr) {
       return aResult.str();
     } else if (aType == ModelAPI_AttributeString::typeId()) {
       // special case for attribute "SolverError"
-      if (theAttr->id() == "SolverError" && 
+      if (theAttr->id() == "SolverError" &&
           std::dynamic_pointer_cast<ModelAPI_Feature>(theAttr->owner())->getKind() == "Sketch")
         return "";
     }
@@ -167,9 +194,14 @@ std::string ModelHighAPI_FeatureStore::dumpAttr(const AttributePtr& theAttr) {
       aResult<<anAttr->text();
   } else if (aType == ModelAPI_AttributeDouble::typeId()) {
     AttributeDoublePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttr);
+    int aPrecision = PRECISION;
+    // Special case - precision for the arc angle. It is calculated with tolerance 1e-4,
+    // so the value has only 4 correct digits
+    if (anAttr->id() == "ArcAngle")
+      aPrecision = 1;
     if (anAttr->text().empty()) {
       double aVal = anAttr->value();
-      dumpArray(aResult, &aVal, 1);
+      dumpArray(aResult, &aVal, 1, aPrecision);
     } else
       aResult<<anAttr->text();
   } else if (aType == ModelAPI_AttributeBoolean::typeId()) {
@@ -177,6 +209,10 @@ std::string ModelHighAPI_FeatureStore::dumpAttr(const AttributePtr& theAttr) {
     aResult<<anAttr->value();
   } else if (aType == ModelAPI_AttributeString::typeId()) {
     AttributeStringPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttr);
+    // do not dump solver DOF for sketch as it may be changed unexpectedly
+    if(anAttr->id() == "SolverDOF") {
+      return "";
+    }
     aResult<<anAttr->value();
   } else if (aType == ModelAPI_AttributeReference::typeId()) {
     AttributeReferencePtr anAttr =
@@ -187,11 +223,11 @@ std::string ModelHighAPI_FeatureStore::dumpAttr(const AttributePtr& theAttr) {
       aResult<<"__empty__";
     }
   } else if (aType == ModelAPI_AttributeSelection::typeId()) {
-    AttributeSelectionPtr anAttr = 
+    AttributeSelectionPtr anAttr =
       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttr);
     aResult<<anAttr->namingName();
   } else if (aType == ModelAPI_AttributeSelectionList::typeId()) {
-    AttributeSelectionListPtr anAttr = 
+    AttributeSelectionListPtr anAttr =
       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttr);
     for(int a = 0; a < anAttr->size(); a++) {
       if (a != 0)
@@ -199,7 +235,7 @@ std::string ModelHighAPI_FeatureStore::dumpAttr(const AttributePtr& theAttr) {
       aResult<<anAttr->value(a)->namingName();
     }
   } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
-    AttributeRefAttrPtr anAttr = 
+    AttributeRefAttrPtr anAttr =
       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttr);
     ObjectPtr anObj = anAttr->isObject() ? anAttr->object() : anAttr->attr()->owner();
     if (anObj.get()) {
@@ -211,10 +247,10 @@ std::string ModelHighAPI_FeatureStore::dumpAttr(const AttributePtr& theAttr) {
       aResult<<"__empty__";
     }
   } else if (aType == ModelAPI_AttributeRefList::typeId()) {
-    AttributeRefListPtr anAttr = 
+    AttributeRefListPtr anAttr =
       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttr);
     // for sketch sub-features the empty values may be skipped and order is not important
-    bool isSketchFeatures = anAttr->id() == "Features" && 
+    bool isSketchFeatures = anAttr->id() == "Features" &&
       std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner())->getKind() == "Sketch";
     std::list<ObjectPtr> aList = anAttr->list();
     std::list<std::string> aResList; // list of resulting strings
@@ -231,7 +267,7 @@ std::string ModelHighAPI_FeatureStore::dumpAttr(const AttributePtr& theAttr) {
       aResult<<*aR<<" ";
     }
   } else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
-    AttributeRefAttrListPtr anAttr = 
+    AttributeRefAttrListPtr anAttr =
       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttr);
     std::list<std::pair<ObjectPtr, AttributePtr> > aList = anAttr->list();
     std::list<std::pair<ObjectPtr, AttributePtr> >::iterator aL = aList.begin();
@@ -249,15 +285,44 @@ std::string ModelHighAPI_FeatureStore::dumpAttr(const AttributePtr& theAttr) {
       }
     }
   } else if (aType == ModelAPI_AttributeIntArray::typeId()) {
-    AttributeIntArrayPtr anAttr = 
+    AttributeIntArrayPtr anAttr =
       std::dynamic_pointer_cast<ModelAPI_AttributeIntArray>(theAttr);
     for(int a = 0; a < anAttr->size(); a++)
       aResult<<anAttr->value(a)<<" ";
   } else if (aType == ModelAPI_AttributeDoubleArray::typeId()) {
-    AttributeDoubleArrayPtr anAttr = 
+    AttributeDoubleArrayPtr anAttr =
       std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(theAttr);
     for(int a = 0; a < anAttr->size(); a++)
       aResult<<anAttr->value(a)<<" ";
+  } else if (aType == ModelAPI_AttributeStringArray::typeId()) {
+    AttributeStringArrayPtr anAttr =
+      std::dynamic_pointer_cast<ModelAPI_AttributeStringArray>(theAttr);
+    for(int a = 0; a < anAttr->size(); a++)
+      aResult<<"'"<<anAttr->value(a)<<"'"<<" ";
+  } else if (aType == ModelAPI_AttributeTables::typeId()) {
+    AttributeTablesPtr anAttr =
+      std::dynamic_pointer_cast<ModelAPI_AttributeTables>(theAttr);
+    aResult<<anAttr->tables()<<"x"<<anAttr->rows()<<"x"<<anAttr->columns()<<" ";
+    for(int aTab = 0; aTab < anAttr->tables(); aTab++) {
+      for(int aRow = 0; aRow < anAttr->rows(); aRow++) {
+        for( int aCol = 0; aCol < anAttr->columns(); aCol++) {
+          switch(anAttr->type()) {
+          case ModelAPI_AttributeTables::BOOLEAN:
+            aResult<<anAttr->value(aRow, aCol, aTab).myBool<<" ";
+            break;
+          case ModelAPI_AttributeTables::INTEGER:
+            aResult<<anAttr->value(aRow, aCol, aTab).myInt<<" ";
+            break;
+          case ModelAPI_AttributeTables::DOUBLE:
+            aResult<<anAttr->value(aRow, aCol, aTab).myDouble<<" ";
+            break;
+          case ModelAPI_AttributeTables::STRING:
+            aResult<<"'"<<anAttr->value(aRow, aCol, aTab).myStr.c_str()<<"' ";
+            break;
+          }
+        }
+      }
+    }
   } else if (aType == GeomDataAPI_Point::typeId()) {
     AttributePointPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttr);
     double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()};
@@ -267,6 +332,9 @@ std::string ModelHighAPI_FeatureStore::dumpAttr(const AttributePtr& theAttr) {
     double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()};
     dumpArray(aResult, aValues, 3);
   } else if (aType == GeomDataAPI_Point2D::typeId()) {
+    // do not dump flyout point for constraints as it may be changed unexpectedly
+    if (theAttr->id() == "ConstraintFlyoutValuePnt")
+      return "__notinitialized__";
     AttributePoint2DPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttr);
     double aValues[2] = {anAttr->x(), anAttr->y()};
     dumpArray(aResult, aValues, 2);
@@ -292,9 +360,14 @@ std::string ModelHighAPI_FeatureStore::dumpShape(std::shared_ptr<GeomAPI_Shape>&
     aResult<<": "<<aCount<<std::endl;
   }
   // output the main characteristics
-  aResult<<"Volume: "<<setprecision(2)<<GeomAlgoAPI_ShapeTools::volume(theShape)<<std::endl;
+  if (GeomAlgoAPI_ShapeTools::volume(theShape) > 1.e-7) {
+    aResult<<"Volume: "<<
+      std::fixed<<setprecision(3)<<GeomAlgoAPI_ShapeTools::volume(theShape)<<std::endl;
+  }
   std::shared_ptr<GeomAPI_Pnt> aCenter = GeomAlgoAPI_ShapeTools::centreOfMass(theShape);
-  aResult<<"Center of mass: "<<std::fixed<<setprecision(PRECISION)
-    <<aCenter->x()<<" "<<aCenter->y()<<" "<<aCenter->z()<<std::endl;
+  aResult<<"Center of mass: ";
+  double aCenterVals[3] = {aCenter->x(), aCenter->y(), aCenter->z()};
+  dumpArray(aResult, aCenterVals, 3, 5);
+  aResult<<std::endl;
   return aResult.str();
 }