Salome HOME
Removing of warnings.
[modules/shaper.git] / src / ModelHighAPI / ModelHighAPI_FeatureStore.cpp
index c9ff7f1f41a7eeb8c08827883e70d59c0f7aeefa..c42072510617eaa52e828893798bc993e3092c8b 100644 (file)
@@ -21,7 +21,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 <TopoDS_Shape.hxx>
 #include <TopExp_Explorer.hxx>
 
+#include <ios>
+#include <cmath>
+
+#define PRECISION 6
+#define TOLERANCE (1.e-7)
+
 ModelHighAPI_FeatureStore::ModelHighAPI_FeatureStore(FeaturePtr theFeature) {
   storeData(theFeature->data(), myAttrs);
   // iterate results to store
@@ -58,7 +66,7 @@ std::string ModelHighAPI_FeatureStore::compare(FeaturePtr theFeature) {
   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() + 
+      return "Results of feature '" + theFeature->name() + "' '" + (*aRes)->data()->name() +
       "' differ:" + anError;
   }
   if (aRes != allResults.end()) {
@@ -70,10 +78,11 @@ std::string ModelHighAPI_FeatureStore::compare(FeaturePtr theFeature) {
   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++) {
@@ -81,11 +90,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;
@@ -96,7 +106,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 + "'";
     }
   }
@@ -110,17 +120,39 @@ std::string ModelHighAPI_FeatureStore::compareData(std::shared_ptr<ModelAPI_Data
   return "";
 }
 
+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(thePrecision)
+              << (fabs(theArray[i]) < TOLERANCE ? 0.0 : theArray[i]);
+  }
+}
+
 std::string ModelHighAPI_FeatureStore::dumpAttr(const AttributePtr& theAttr) {
   static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
   FeaturePtr aFeatOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttr->owner());
   if (aFeatOwner.get() && !aFactory->isCase(aFeatOwner, theAttr->id())) {
     return "__notcase__";
   }
+  std::string aType = theAttr->attributeType();
+  std::ostringstream aResult;
   if (!theAttr->isInitialized()) {
+    if (aType == ModelAPI_AttributeBoolean::typeId()) {
+      // special case for Boolean attribute (mean it false if not initialized)
+      aResult << false;
+      return aResult.str();
+    } else if (aType == ModelAPI_AttributeString::typeId()) {
+      // special case for attribute "SolverError"
+      if (theAttr->id() == "SolverError" &&
+          std::dynamic_pointer_cast<ModelAPI_Feature>(theAttr->owner())->getKind() == "Sketch")
+        return "";
+    }
+
     return "__notinitialized__";
   }
-  std::string aType = theAttr->attributeType();
-  std::ostringstream aResult;
   if (aType == ModelAPI_AttributeDocRef::typeId()) {
     AttributeDocRefPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDocRef>(theAttr);
     DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
@@ -140,9 +172,15 @@ 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);
-    if (anAttr->text().empty())
-      aResult<<anAttr->value();
-    else
+    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, aPrecision);
+    } else
       aResult<<anAttr->text();
   } else if (aType == ModelAPI_AttributeBoolean::typeId()) {
     AttributeBooleanPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(theAttr);
@@ -159,11 +197,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)
@@ -171,7 +209,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()) {
@@ -183,20 +221,27 @@ 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" &&
+      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
     for(std::list<ObjectPtr>::iterator aL = aList.begin(); aL != aList.end(); aL++) {
-      if (aL != aList.begin())
-        aResult<<" ";
       if (aL->get()) {
-        aResult<<(*aL)->data()->name();
-      } else {
-        aResult<<"__empty__";
+        aResList.push_back((*aL)->data()->name());
+      } else if (!isSketchFeatures) {
+        aResList.push_back("__empty__");
       }
     }
+    if (isSketchFeatures)
+      aResList.sort();
+    for(std::list<std::string>::iterator aR = aResList.begin(); aR != aResList.end(); aR++) {
+      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();
@@ -214,24 +259,59 @@ 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);
-    aResult<<anAttr->x()<<" "<<anAttr->y()<<" "<<anAttr->z();
+    double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()};
+    dumpArray(aResult, aValues, 3);
   } else if (aType == GeomDataAPI_Dir::typeId()) {
     AttributeDirPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theAttr);
-    aResult<<anAttr->x()<<" "<<anAttr->y()<<" "<<anAttr->z();
+    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 "";
     AttributePoint2DPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttr);
-    aResult<<anAttr->x()<<" "<<anAttr->y()<<" ";
+    double aValues[2] = {anAttr->x(), anAttr->y()};
+    dumpArray(aResult, aValues, 2);
   } else {
     aResult<<"__unknownattribute__";
   }
@@ -254,9 +334,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: "
-    <<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);
+  aResult<<std::endl;
   return aResult.str();
 }