Salome HOME
Removed Sketch DOF from checking at Python dump as it may be changed unexpectedly
[modules/shaper.git] / src / ModelHighAPI / ModelHighAPI_FeatureStore.cpp
1 // Copyright (C) 2016-20xx CEA/DEN, EDF R&D -->
2
3 // File:        ModelHighAPI_FeatureStore.cpp
4 // Created:     12 August 2016
5 // Author:      Mikhail PONIKAROV
6
7 #include <ModelHighAPI_FeatureStore.h>
8
9 #include <ModelAPI_Tools.h>
10 #include <ModelAPI_ResultPart.h>
11 #include <ModelAPI_Session.h>
12 #include <ModelAPI_AttributeBoolean.h>
13 #include <ModelAPI_AttributeDocRef.h>
14 #include <ModelAPI_AttributeDouble.h>
15 #include <ModelAPI_AttributeIntArray.h>
16 #include <ModelAPI_AttributeInteger.h>
17 #include <ModelAPI_AttributeRefAttr.h>
18 #include <ModelAPI_AttributeRefAttrList.h>
19 #include <ModelAPI_AttributeReference.h>
20 #include <ModelAPI_AttributeRefList.h>
21 #include <ModelAPI_AttributeSelection.h>
22 #include <ModelAPI_AttributeSelectionList.h>
23 #include <ModelAPI_AttributeString.h>
24 #include <ModelAPI_AttributeStringArray.h>
25 #include <ModelAPI_AttributeDoubleArray.h>
26 #include <ModelAPI_AttributeTables.h>
27 #include <ModelAPI_Validator.h>
28
29 #include <GeomDataAPI_Dir.h>
30 #include <GeomDataAPI_Point.h>
31 #include <GeomDataAPI_Point2D.h>
32 #include <GeomAlgoAPI_ShapeTools.h>
33 #include <GeomAPI_Pnt.h>
34
35 #include <TopoDS_Shape.hxx>
36 #include <TopExp_Explorer.hxx>
37
38 #include <ios>
39 #include <cmath>
40
41 #define PRECISION 6
42 #define TOLERANCE (1.e-7)
43
44 ModelHighAPI_FeatureStore::ModelHighAPI_FeatureStore(FeaturePtr theFeature) {
45   storeData(theFeature->data(), myAttrs);
46   // iterate results to store
47   std::list<ResultPtr> allResults;
48   ModelAPI_Tools::allResults(theFeature, allResults);
49   std::list<ResultPtr>::iterator aRes = allResults.begin();
50   for(; aRes != allResults.end(); aRes++) {
51     std::map<std::string, std::string> aResDump;
52     storeData((*aRes)->data(), aResDump);
53     myRes.push_back(aResDump);
54   }
55 }
56
57 std::string ModelHighAPI_FeatureStore::compare(FeaturePtr theFeature) {
58   std::string anError = compareData(theFeature->data(), myAttrs);
59   if (!anError.empty()) {
60     return "Features '" + theFeature->name() + "' differ:" + anError;
61   }
62   std::list<ResultPtr> allResults;
63   ModelAPI_Tools::allResults(theFeature, allResults);
64   std::list<ResultPtr>::iterator aRes = allResults.begin();
65   std::list<std::map<std::string, std::string> >::iterator aResIter = myRes.begin();
66   for(; aRes != allResults.end() && aResIter != myRes.end(); aRes++, aResIter++) {
67     anError = compareData((*aRes)->data(), *aResIter);
68     if (!anError.empty())
69       return "Results of feature '" + theFeature->name() + "' '" + (*aRes)->data()->name() +
70       "' differ:" + anError;
71   }
72   if (aRes != allResults.end()) {
73     return "Current model has more results '" + (*aRes)->data()->name() + "'";
74   }
75   if (aResIter != myRes.end()) {
76     return "Original model had more results '" + (*aResIter)["__name__"] + "'";
77   }
78   return ""; // ok
79 }
80
81 void ModelHighAPI_FeatureStore::storeData(std::shared_ptr<ModelAPI_Data> theData,
82   std::map<std::string, std::string>& theAttrs)
83 {
84   // store name to keep also this information and output if needed
85   theAttrs["__name__"] = theData->name();
86   std::list<std::shared_ptr<ModelAPI_Attribute> > allAttrs = theData->attributes("");
87   std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = allAttrs.begin();
88   for(; anAttr != allAttrs.end(); anAttr++) {
89     theAttrs[(*anAttr)->id()] = dumpAttr(*anAttr);
90   }
91   ResultPtr aShapeOwner = std::dynamic_pointer_cast<ModelAPI_Result>(theData->owner());
92   if (aShapeOwner.get() && aShapeOwner->shape().get()) {
93     std::shared_ptr<GeomAPI_Shape> aShape = aShapeOwner->shape();
94     theAttrs["__shape__"] = dumpShape(aShape);
95   }
96 }
97
98 std::string ModelHighAPI_FeatureStore::compareData(std::shared_ptr<ModelAPI_Data> theData,
99   std::map<std::string, std::string>& theAttrs)
100 {
101   std::map<std::string, std::string> aThis;
102   storeData(theData, aThis);
103   std::map<std::string, std::string>::iterator aThisIter = aThis.begin();
104   for(; aThisIter != aThis.end(); aThisIter++) {
105     if (theAttrs.find(aThisIter->first) == theAttrs.end()) {
106       return "original model had no attribute '" + aThisIter->first + "'";
107     }
108     if (theAttrs[aThisIter->first] != aThisIter->second) {
109       return "attribute '" + aThisIter->first + "' is different (original != current) '" +
110         theAttrs[aThisIter->first] + "' != '" + aThisIter->second + "'";
111     }
112   }
113   // iterate back to find lack attribute in the current model
114   std::map<std::string, std::string>::iterator anOrigIter = theAttrs.begin();
115   for(; anOrigIter != theAttrs.end(); anOrigIter++) {
116     if (aThis.find(anOrigIter->first) == aThis.end()) {
117       return "current model had no attribute '" + anOrigIter->first + "'";
118     }
119   }
120   return "";
121 }
122
123 static void dumpArray(std::ostringstream& theOutput, const double theArray[],
124                       int theSize, int thePrecision = PRECISION)
125 {
126   for (int i = 0; i < theSize; ++i) {
127     if (i > 0)
128       theOutput << " ";
129     theOutput << std::fixed << setprecision(thePrecision)
130               << (fabs(theArray[i]) < TOLERANCE ? 0.0 : theArray[i]);
131   }
132 }
133
134 std::string ModelHighAPI_FeatureStore::dumpAttr(const AttributePtr& theAttr) {
135   static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
136   FeaturePtr aFeatOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttr->owner());
137   if (aFeatOwner.get() && !aFactory->isCase(aFeatOwner, theAttr->id())) {
138     return "__notcase__";
139   }
140   std::string aType = theAttr->attributeType();
141   std::ostringstream aResult;
142   if (!theAttr->isInitialized()) {
143     if (aType == ModelAPI_AttributeBoolean::typeId()) {
144       // special case for Boolean attribute (mean it false if not initialized)
145       aResult << false;
146       return aResult.str();
147     } else if (aType == ModelAPI_AttributeString::typeId()) {
148       // special case for attribute "SolverError"
149       if (theAttr->id() == "SolverError" &&
150           std::dynamic_pointer_cast<ModelAPI_Feature>(theAttr->owner())->getKind() == "Sketch")
151         return "";
152     }
153
154     return "__notinitialized__";
155   }
156   if (aType == ModelAPI_AttributeDocRef::typeId()) {
157     AttributeDocRefPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDocRef>(theAttr);
158     DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
159     if (anAttr->value() != aDoc) {
160       ResultPtr aRes = ModelAPI_Tools::findPartResult(aDoc, anAttr->value());
161       if (aRes.get()) {
162         aResult<<aRes->data()->name(); // Part result name (the same as saved file name)
163       }
164     } else {
165       aResult<<aDoc->kind(); // PartSet
166     }
167   } else if (aType == ModelAPI_AttributeInteger::typeId()) {
168     AttributeIntegerPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(theAttr);
169     if (anAttr->text().empty())
170       aResult<<anAttr->value();
171     else
172       aResult<<anAttr->text();
173   } else if (aType == ModelAPI_AttributeDouble::typeId()) {
174     AttributeDoublePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttr);
175     int aPrecision = PRECISION;
176     // Special case - precision for the arc angle. It is calculated with tolerance 1e-4,
177     // so the value has only 4 correct digits
178     if (anAttr->id() == "ArcAngle")
179       aPrecision = 1;
180     if (anAttr->text().empty()) {
181       double aVal = anAttr->value();
182       dumpArray(aResult, &aVal, 1, aPrecision);
183     } else
184       aResult<<anAttr->text();
185   } else if (aType == ModelAPI_AttributeBoolean::typeId()) {
186     AttributeBooleanPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(theAttr);
187     aResult<<anAttr->value();
188   } else if (aType == ModelAPI_AttributeString::typeId()) {
189     AttributeStringPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttr);
190     // do not dump solver DOF for sketch as it may be changed unexpectedly
191     if(anAttr->id() == "SolverDOF") {
192       return "";
193     }
194     aResult<<anAttr->value();
195   } else if (aType == ModelAPI_AttributeReference::typeId()) {
196     AttributeReferencePtr anAttr =
197       std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttr);
198     if (anAttr->value().get()) {
199       aResult<<anAttr->value()->data()->name();
200     } else {
201       aResult<<"__empty__";
202     }
203   } else if (aType == ModelAPI_AttributeSelection::typeId()) {
204     AttributeSelectionPtr anAttr =
205       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttr);
206     aResult<<anAttr->namingName();
207   } else if (aType == ModelAPI_AttributeSelectionList::typeId()) {
208     AttributeSelectionListPtr anAttr =
209       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttr);
210     for(int a = 0; a < anAttr->size(); a++) {
211       if (a != 0)
212         aResult<<" ";
213       aResult<<anAttr->value(a)->namingName();
214     }
215   } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
216     AttributeRefAttrPtr anAttr =
217       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttr);
218     ObjectPtr anObj = anAttr->isObject() ? anAttr->object() : anAttr->attr()->owner();
219     if (anObj.get()) {
220       aResult<<anObj->data()->name();
221       if (!anAttr->isObject()) {
222         aResult<<" "<<anAttr->attr()->id();
223       }
224     } else {
225       aResult<<"__empty__";
226     }
227   } else if (aType == ModelAPI_AttributeRefList::typeId()) {
228     AttributeRefListPtr anAttr =
229       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttr);
230     // for sketch sub-features the empty values may be skipped and order is not important
231     bool isSketchFeatures = anAttr->id() == "Features" &&
232       std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner())->getKind() == "Sketch";
233     std::list<ObjectPtr> aList = anAttr->list();
234     std::list<std::string> aResList; // list of resulting strings
235     for(std::list<ObjectPtr>::iterator aL = aList.begin(); aL != aList.end(); aL++) {
236       if (aL->get()) {
237         aResList.push_back((*aL)->data()->name());
238       } else if (!isSketchFeatures) {
239         aResList.push_back("__empty__");
240       }
241     }
242     if (isSketchFeatures)
243       aResList.sort();
244     for(std::list<std::string>::iterator aR = aResList.begin(); aR != aResList.end(); aR++) {
245       aResult<<*aR<<" ";
246     }
247   } else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
248     AttributeRefAttrListPtr anAttr =
249       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttr);
250     std::list<std::pair<ObjectPtr, AttributePtr> > aList = anAttr->list();
251     std::list<std::pair<ObjectPtr, AttributePtr> >::iterator aL = aList.begin();
252     for(; aL != aList.end(); aL++) {
253       if (aL != aList.begin())
254         aResult<<" ";
255       ObjectPtr anObj = aL->second.get() ? aL->second->owner() : aL->first;
256       if (anObj.get()) {
257         aResult<<anObj->data()->name();
258         if (aL->second.get()) {
259           aResult<<" "<<aL->second->id();
260         }
261       } else {
262         aResult<<"__empty__";
263       }
264     }
265   } else if (aType == ModelAPI_AttributeIntArray::typeId()) {
266     AttributeIntArrayPtr anAttr =
267       std::dynamic_pointer_cast<ModelAPI_AttributeIntArray>(theAttr);
268     for(int a = 0; a < anAttr->size(); a++)
269       aResult<<anAttr->value(a)<<" ";
270   } else if (aType == ModelAPI_AttributeDoubleArray::typeId()) {
271     AttributeDoubleArrayPtr anAttr =
272       std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(theAttr);
273     for(int a = 0; a < anAttr->size(); a++)
274       aResult<<anAttr->value(a)<<" ";
275   } else if (aType == ModelAPI_AttributeStringArray::typeId()) {
276     AttributeStringArrayPtr anAttr =
277       std::dynamic_pointer_cast<ModelAPI_AttributeStringArray>(theAttr);
278     for(int a = 0; a < anAttr->size(); a++)
279       aResult<<"'"<<anAttr->value(a)<<"'"<<" ";
280   } else if (aType == ModelAPI_AttributeTables::typeId()) {
281     AttributeTablesPtr anAttr =
282       std::dynamic_pointer_cast<ModelAPI_AttributeTables>(theAttr);
283     aResult<<anAttr->tables()<<"x"<<anAttr->rows()<<"x"<<anAttr->columns()<<" ";
284     for(int aTab = 0; aTab < anAttr->tables(); aTab++) {
285       for(int aRow = 0; aRow < anAttr->rows(); aRow++) {
286         for( int aCol = 0; aCol < anAttr->columns(); aCol++) {
287           switch(anAttr->type()) {
288           case ModelAPI_AttributeTables::BOOLEAN:
289             aResult<<anAttr->value(aRow, aCol, aTab).myBool<<" ";
290             break;
291           case ModelAPI_AttributeTables::INTEGER:
292             aResult<<anAttr->value(aRow, aCol, aTab).myInt<<" ";
293             break;
294           case ModelAPI_AttributeTables::DOUBLE:
295             aResult<<anAttr->value(aRow, aCol, aTab).myDouble<<" ";
296             break;
297           case ModelAPI_AttributeTables::STRING:
298             aResult<<"'"<<anAttr->value(aRow, aCol, aTab).myStr.c_str()<<"' ";
299             break;
300           }
301         }
302       }
303     }
304   } else if (aType == GeomDataAPI_Point::typeId()) {
305     AttributePointPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttr);
306     double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()};
307     dumpArray(aResult, aValues, 3);
308   } else if (aType == GeomDataAPI_Dir::typeId()) {
309     AttributeDirPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theAttr);
310     double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()};
311     dumpArray(aResult, aValues, 3);
312   } else if (aType == GeomDataAPI_Point2D::typeId()) {
313     // do not dump flyout point for constraints as it may be changed unexpectedly
314     if (theAttr->id() == "ConstraintFlyoutValuePnt")
315       return "";
316     AttributePoint2DPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttr);
317     double aValues[2] = {anAttr->x(), anAttr->y()};
318     dumpArray(aResult, aValues, 2);
319   } else {
320     aResult<<"__unknownattribute__";
321   }
322   return aResult.str();
323 }
324
325 std::string ModelHighAPI_FeatureStore::dumpShape(std::shared_ptr<GeomAPI_Shape>& theShape) {
326   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
327   if (aShape.IsNull()) {
328     return "null";
329   }
330   std::ostringstream aResult;
331   // output the number of shapes of different types
332   TopAbs_ShapeEnum aType = TopAbs_COMPOUND;
333   for(; aType <= TopAbs_VERTEX; aType = TopAbs_ShapeEnum((int)aType + 1)) {
334     TopExp_Explorer anExp(aShape, aType);
335     int aCount = 0;
336     for(; anExp.More(); anExp.Next()) aCount++;
337     TopAbs::Print(aType, aResult);
338     aResult<<": "<<aCount<<std::endl;
339   }
340   // output the main characteristics
341   if (GeomAlgoAPI_ShapeTools::volume(theShape) > 1.e-7) {
342     aResult<<"Volume: "<<
343       std::fixed<<setprecision(3)<<GeomAlgoAPI_ShapeTools::volume(theShape)<<std::endl;
344   }
345   std::shared_ptr<GeomAPI_Pnt> aCenter = GeomAlgoAPI_ShapeTools::centreOfMass(theShape);
346   aResult<<"Center of mass: ";
347   double aCenterVals[3] = {aCenter->x(), aCenter->y(), aCenter->z()};
348   dumpArray(aResult, aCenterVals, 3);
349   aResult<<std::endl;
350   return aResult.str();
351 }