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