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