Salome HOME
Dump Python in the High Level Parameterized Geometry API (issue #1648)
[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 7
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)
121 {
122   for (int i = 0; i < theSize; ++i) {
123     if (i > 0)
124       theOutput << " ";
125     theOutput << std::fixed << setprecision(PRECISION)
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     if (anAttr->text().empty()) {
172       double aVal = anAttr->value();
173       dumpArray(aResult, &aVal, 1);
174     } else
175       aResult<<anAttr->text();
176   } else if (aType == ModelAPI_AttributeBoolean::typeId()) {
177     AttributeBooleanPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(theAttr);
178     aResult<<anAttr->value();
179   } else if (aType == ModelAPI_AttributeString::typeId()) {
180     AttributeStringPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttr);
181     aResult<<anAttr->value();
182   } else if (aType == ModelAPI_AttributeReference::typeId()) {
183     AttributeReferencePtr anAttr =
184       std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttr);
185     if (anAttr->value().get()) {
186       aResult<<anAttr->value()->data()->name();
187     } else {
188       aResult<<"__empty__";
189     }
190   } else if (aType == ModelAPI_AttributeSelection::typeId()) {
191     AttributeSelectionPtr anAttr = 
192       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttr);
193     aResult<<anAttr->namingName();
194   } else if (aType == ModelAPI_AttributeSelectionList::typeId()) {
195     AttributeSelectionListPtr anAttr = 
196       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttr);
197     for(int a = 0; a < anAttr->size(); a++) {
198       if (a != 0)
199         aResult<<" ";
200       aResult<<anAttr->value(a)->namingName();
201     }
202   } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
203     AttributeRefAttrPtr anAttr = 
204       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttr);
205     ObjectPtr anObj = anAttr->isObject() ? anAttr->object() : anAttr->attr()->owner();
206     if (anObj.get()) {
207       aResult<<anObj->data()->name();
208       if (!anAttr->isObject()) {
209         aResult<<" "<<anAttr->attr()->id();
210       }
211     } else {
212       aResult<<"__empty__";
213     }
214   } else if (aType == ModelAPI_AttributeRefList::typeId()) {
215     AttributeRefListPtr anAttr = 
216       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttr);
217     // for sketch sub-features the empty values may be skipped and order is not important
218     bool isSketchFeatures = anAttr->id() == "Features" && 
219       std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner())->getKind() == "Sketch";
220     std::list<ObjectPtr> aList = anAttr->list();
221     std::list<std::string> aResList; // list of resulting strings
222     for(std::list<ObjectPtr>::iterator aL = aList.begin(); aL != aList.end(); aL++) {
223       if (aL->get()) {
224         aResList.push_back((*aL)->data()->name());
225       } else if (!isSketchFeatures) {
226         aResList.push_back("__empty__");
227       }
228     }
229     if (isSketchFeatures)
230       aResList.sort();
231     for(std::list<std::string>::iterator aR = aResList.begin(); aR != aResList.end(); aR++) {
232       aResult<<*aR<<" ";
233     }
234   } else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
235     AttributeRefAttrListPtr anAttr = 
236       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttr);
237     std::list<std::pair<ObjectPtr, AttributePtr> > aList = anAttr->list();
238     std::list<std::pair<ObjectPtr, AttributePtr> >::iterator aL = aList.begin();
239     for(; aL != aList.end(); aL++) {
240       if (aL != aList.begin())
241         aResult<<" ";
242       ObjectPtr anObj = aL->second.get() ? aL->second->owner() : aL->first;
243       if (anObj.get()) {
244         aResult<<anObj->data()->name();
245         if (aL->second.get()) {
246           aResult<<" "<<aL->second->id();
247         }
248       } else {
249         aResult<<"__empty__";
250       }
251     }
252   } else if (aType == ModelAPI_AttributeIntArray::typeId()) {
253     AttributeIntArrayPtr anAttr = 
254       std::dynamic_pointer_cast<ModelAPI_AttributeIntArray>(theAttr);
255     for(int a = 0; a < anAttr->size(); a++)
256       aResult<<anAttr->value(a)<<" ";
257   } else if (aType == ModelAPI_AttributeDoubleArray::typeId()) {
258     AttributeDoubleArrayPtr anAttr = 
259       std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(theAttr);
260     for(int a = 0; a < anAttr->size(); a++)
261       aResult<<anAttr->value(a)<<" ";
262   } else if (aType == GeomDataAPI_Point::typeId()) {
263     AttributePointPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttr);
264     double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()};
265     dumpArray(aResult, aValues, 3);
266   } else if (aType == GeomDataAPI_Dir::typeId()) {
267     AttributeDirPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theAttr);
268     double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()};
269     dumpArray(aResult, aValues, 3);
270   } else if (aType == GeomDataAPI_Point2D::typeId()) {
271     // do not dump flyout point for constraints as it may be changed unexpectedly
272     if (theAttr->id() == "ConstraintFlyoutValuePnt")
273       return "";
274     AttributePoint2DPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttr);
275     double aValues[2] = {anAttr->x(), anAttr->y()};
276     dumpArray(aResult, aValues, 2);
277   } else {
278     aResult<<"__unknownattribute__";
279   }
280   return aResult.str();
281 }
282
283 std::string ModelHighAPI_FeatureStore::dumpShape(std::shared_ptr<GeomAPI_Shape>& theShape) {
284   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
285   if (aShape.IsNull()) {
286     return "null";
287   }
288   std::ostringstream aResult;
289   // output the number of shapes of different types
290   TopAbs_ShapeEnum aType = TopAbs_COMPOUND;
291   for(; aType <= TopAbs_VERTEX; aType = TopAbs_ShapeEnum((int)aType + 1)) {
292     TopExp_Explorer anExp(aShape, aType);
293     int aCount = 0;
294     for(; anExp.More(); anExp.Next()) aCount++;
295     TopAbs::Print(aType, aResult);
296     aResult<<": "<<aCount<<std::endl;
297   }
298   // output the main characteristics
299   aResult<<"Volume: "<<setprecision(2)<<GeomAlgoAPI_ShapeTools::volume(theShape)<<std::endl;
300   std::shared_ptr<GeomAPI_Pnt> aCenter = GeomAlgoAPI_ShapeTools::centreOfMass(theShape);
301   aResult<<"Center of mass: "<<std::fixed<<setprecision(PRECISION)
302     <<aCenter->x()<<" "<<aCenter->y()<<" "<<aCenter->z()<<std::endl;
303   return aResult.str();
304 }