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