Salome HOME
Merge branch 'V9_5_BR'
[modules/shaper.git] / src / ModelHighAPI / ModelHighAPI_FeatureStore.cpp
1 // Copyright (C) 2014-2020  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_ResultConstruction.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 << std::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
164   // do not check selection of the filter,
165   // because there is neither parametric update nor dump support yet.
166   FiltersFeaturePtr aFilter = std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(aFeatOwner);
167   if (aFilter && (aType == ModelAPI_AttributeSelection::typeId() ||
168                   aType == ModelAPI_AttributeSelectionList::typeId()))
169     return "__filter_selection__";
170
171   std::ostringstream aResult;
172   if (!theAttr->isInitialized()) {
173     if (aType == ModelAPI_AttributeBoolean::typeId()) {
174       // special case for Boolean attribute (mean it false if not initialized)
175       aResult << false;
176       return aResult.str();
177     } else if (aType == ModelAPI_AttributeString::typeId()) {
178       // special case for attribute "SolverError"
179       if (theAttr->id() == "SolverError" &&
180           std::dynamic_pointer_cast<ModelAPI_Feature>(theAttr->owner())->getKind() == "Sketch")
181         return "";
182     }
183
184     return "__notinitialized__";
185   }
186   if (aType == ModelAPI_AttributeDocRef::typeId()) {
187     AttributeDocRefPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDocRef>(theAttr);
188     DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
189     if (anAttr->value() != aDoc) {
190       ResultPtr aRes = ModelAPI_Tools::findPartResult(aDoc, anAttr->value());
191       if (aRes.get()) {
192         aResult<<aRes->data()->name(); // Part result name (the same as saved file name)
193       }
194     } else {
195       aResult<<aDoc->kind(); // PartSet
196     }
197   } else if (aType == ModelAPI_AttributeInteger::typeId()) {
198     AttributeIntegerPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(theAttr);
199     // do not dump a type of ConstraintAngle, because it can be changed due dumping
200     if (anAttr->id() == "AngleType" || anAttr->id() == "AngleTypePrevious") {
201       return "";
202     } else if (anAttr->id() == "LocationType") {
203       return "__notinitialized__";
204     }
205     if (anAttr->text().empty())
206       aResult<<anAttr->value();
207     else
208       aResult<<anAttr->text();
209   } else if (aType == ModelAPI_AttributeDouble::typeId()) {
210     AttributeDoublePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttr);
211     if (anAttr->id() == "ConstraintValue") {
212       // do not dump a value of constraint if it is ConstraintAngle,
213       // because this value depends on the angle type
214       FeaturePtr anOwner = ModelAPI_Feature::feature(anAttr->owner());
215       if (anOwner && anOwner->getKind() == "SketchConstraintAngle")
216         return "";
217     }
218     int aPrecision = PRECISION;
219     // Special case - precision for the arc angle. It is calculated with tolerance 1e-4,
220     // so the value has only 4 correct digits
221     if (anAttr->id() == "ArcAngle")
222       aPrecision = 1;
223     if (anAttr->text().empty()) {
224       double aVal = anAttr->value();
225       dumpArray(aResult, &aVal, 1, aPrecision);
226     } else
227       aResult<<anAttr->text();
228   } else if (aType == ModelAPI_AttributeBoolean::typeId()) {
229     AttributeBooleanPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(theAttr);
230     // do not dump internal flags of ConstraintAngle
231     if (anAttr->id() == "AngleReversedLine1" || anAttr->id() == "AngleReversedLine2") {
232       return "";
233     }
234     aResult<<anAttr->value();
235   } else if (aType == ModelAPI_AttributeString::typeId()) {
236     AttributeStringPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttr);
237     // do not dump solver DOF for sketch as it may be changed unexpectedly
238     if(anAttr->id() == "SolverDOF") {
239       return "";
240     }
241     aResult<<anAttr->value();
242   } else if (aType == ModelAPI_AttributeReference::typeId()) {
243     AttributeReferencePtr anAttr =
244       std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttr);
245     if (anAttr->value().get()) {
246       aResult<<anAttr->value()->data()->name();
247     } else {
248       aResult<<"__empty__";
249     }
250   } else if (aType == ModelAPI_AttributeSelection::typeId()) {
251     AttributeSelectionPtr anAttr =
252       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttr);
253     if (anAttr->context().get())
254       aResult<<anAttr->namingName();
255     else
256       aResult<<"__notinitialized__";
257   } else if (aType == ModelAPI_AttributeSelectionList::typeId()) {
258     AttributeSelectionListPtr anAttr =
259       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttr);
260     for(int a = 0; a < anAttr->size(); a++) {
261       if (a != 0)
262         aResult<<" ";
263       aResult<<anAttr->value(a)->namingName();
264     }
265   } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
266     AttributeRefAttrPtr anAttr =
267       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttr);
268     ObjectPtr anObj = anAttr->isObject() ? anAttr->object() : anAttr->attr()->owner();
269     if (anObj.get()) {
270       aResult<<anObj->data()->name();
271       if (!anAttr->isObject()) {
272         aResult<<" "<<anAttr->attr()->id();
273       }
274     } else {
275       aResult<<"__empty__";
276     }
277   } else if (aType == ModelAPI_AttributeRefList::typeId()) {
278     AttributeRefListPtr anAttr =
279         std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttr);
280     // for sketch sub-features the empty values may be skipped and order is not important
281     bool isSketchFeatures = anAttr->id() == "Features" &&
282       std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner())->getKind() == "Sketch";
283     std::list<ObjectPtr> aList = anAttr->list();
284     std::list<std::string> aResList; // list of resulting strings
285     for(std::list<ObjectPtr>::iterator aL = aList.begin(); aL != aList.end(); aL++) {
286       if (aL->get()) {
287         if (isSketchFeatures) {
288           // do not control construction features of an ellipse and other
289           FeaturePtr aFeature = ModelAPI_Feature::feature(*aL);
290           //if (aFeature->getKind() == "SketchConstraintCoincidenceInternal")
291           //  continue; // skip internal constraints
292           std::string aStr = aFeature->getKind().substr(0, 16);
293           if (aStr == "SketchConstraint")
294             continue; // no need to dump and check constraints
295         }
296         aResList.push_back((*aL)->data()->name());
297       } else if (!isSketchFeatures) {
298         aResList.push_back("__empty__");
299       }
300     }
301     if (isSketchFeatures)
302       aResList.sort();
303     for(std::list<std::string>::iterator aR = aResList.begin(); aR != aResList.end(); aR++) {
304       aResult<<*aR<<" ";
305     }
306   } else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
307     AttributeRefAttrListPtr anAttr =
308       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttr);
309     std::list<std::pair<ObjectPtr, AttributePtr> > aList = anAttr->list();
310     std::list<std::pair<ObjectPtr, AttributePtr> >::iterator aL = aList.begin();
311     for(; aL != aList.end(); aL++) {
312       if (aL != aList.begin())
313         aResult<<" ";
314       ObjectPtr anObj = aL->second.get() ? aL->second->owner() : aL->first;
315       if (anObj.get()) {
316         aResult<<anObj->data()->name();
317         if (aL->second.get()) {
318           aResult<<" "<<aL->second->id();
319         }
320       } else {
321         aResult<<"__empty__";
322       }
323     }
324   } else if (aType == ModelAPI_AttributeIntArray::typeId()) {
325     if (theAttr->id() == "Color") {
326       ResultConstructionPtr aResConstr =
327           std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theAttr->owner());
328       if (aResConstr.get())
329         return "__notinitialized__";
330     }
331     AttributeIntArrayPtr anAttr =
332       std::dynamic_pointer_cast<ModelAPI_AttributeIntArray>(theAttr);
333     for(int a = 0; a < anAttr->size(); a++)
334       aResult<<anAttr->value(a)<<" ";
335   } else if (aType == ModelAPI_AttributeDoubleArray::typeId()) {
336     AttributeDoubleArrayPtr anAttr =
337       std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(theAttr);
338     for(int a = 0; a < anAttr->size(); a++)
339       aResult<<anAttr->value(a)<<" ";
340   } else if (aType == ModelAPI_AttributeStringArray::typeId()) {
341     AttributeStringArrayPtr anAttr =
342       std::dynamic_pointer_cast<ModelAPI_AttributeStringArray>(theAttr);
343     for(int a = 0; a < anAttr->size(); a++)
344       aResult<<"'"<<anAttr->value(a)<<"'"<<" ";
345   } else if (aType == ModelAPI_AttributeTables::typeId()) {
346     AttributeTablesPtr anAttr =
347       std::dynamic_pointer_cast<ModelAPI_AttributeTables>(theAttr);
348     aResult<<anAttr->tables()<<"x"<<anAttr->rows()<<"x"<<anAttr->columns()<<" ";
349     for(int aTab = 0; aTab < anAttr->tables(); aTab++) {
350       for(int aRow = 0; aRow < anAttr->rows(); aRow++) {
351         for( int aCol = 0; aCol < anAttr->columns(); aCol++) {
352           switch(anAttr->type()) {
353           case ModelAPI_AttributeTables::BOOLEAN:
354             aResult<<anAttr->value(aRow, aCol, aTab).myBool<<" ";
355             break;
356           case ModelAPI_AttributeTables::INTEGER:
357             aResult<<anAttr->value(aRow, aCol, aTab).myInt<<" ";
358             break;
359           case ModelAPI_AttributeTables::DOUBLE:
360             aResult<<anAttr->value(aRow, aCol, aTab).myDouble<<" ";
361             break;
362           case ModelAPI_AttributeTables::STRING:
363             aResult<<"'"<<anAttr->value(aRow, aCol, aTab).myStr.c_str()<<"' ";
364             break;
365           }
366         }
367       }
368     }
369   } else if (aType == GeomDataAPI_Point::typeId()) {
370     AttributePointPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttr);
371     double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()};
372     dumpArray(aResult, aValues, 3);
373   } else if (aType == GeomDataAPI_Dir::typeId()) {
374     if (theAttr->id() == "DistanceDirection")
375       return "__notcase__";
376     AttributeDirPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theAttr);
377     double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()};
378     dumpArray(aResult, aValues, 3);
379   } else if (aType == GeomDataAPI_Point2D::typeId()) {
380     // do not dump flyout point for constraints as it may be changed unexpectedly,
381     // also do not dump selection points controlled by GUI
382     if (theAttr->id() == "ConstraintFlyoutValuePnt" ||
383         theAttr->id() == "SelectedPointA" || theAttr->id() == "SelectedPointB")
384       return "__notinitialized__";
385     AttributePoint2DPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttr);
386     double aValues[2] = {anAttr->x(), anAttr->y()};
387     dumpArray(aResult, aValues, 2);
388   } else {
389     aResult<<"__unknownattribute__";
390   }
391   return aResult.str();
392 }
393
394 std::string ModelHighAPI_FeatureStore::dumpShape(std::shared_ptr<GeomAPI_Shape>& theShape) {
395   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
396   if (aShape.IsNull()) {
397     return "null";
398   }
399   std::ostringstream aResult;
400   // output the number of shapes of different types
401   TopAbs_ShapeEnum aType = TopAbs_COMPOUND;
402   for(; aType <= TopAbs_VERTEX; aType = TopAbs_ShapeEnum((int)aType + 1)) {
403     TopExp_Explorer anExp(aShape, aType);
404     int aCount = 0;
405     for(; anExp.More(); anExp.Next()) aCount++;
406     TopAbs::Print(aType, aResult);
407     aResult<<": "<<aCount<<std::endl;
408   }
409   // output the main characteristics
410   double aVolume = GeomAlgoAPI_ShapeTools::volume(theShape);
411   if (aVolume > 1.e-5) {
412     aResult<<"Volume: ";
413     // volumes of too huge shapes write in the scientific format
414     if (aVolume >= 1.e5)
415       aResult<<std::scientific<<std::setprecision(7);
416     else
417       aResult<<std::fixed<<std::setprecision(3);
418     aResult<<aVolume<<std::endl;
419   }
420   std::shared_ptr<GeomAPI_Pnt> aCenter = GeomAlgoAPI_ShapeTools::centreOfMass(theShape);
421   aResult<<"Center of mass: ";
422   double aCenterVals[3] = {aCenter->x(), aCenter->y(), aCenter->z()};
423   dumpArray(aResult, aCenterVals, 3, 5);
424   aResult<<std::endl;
425   return aResult.str();
426 }