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