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