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