Salome HOME
Task #3237: Allow usage of accented characters in ObjectBrowser
[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_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 <Locale_Convert.h>
50
51 #include <TopoDS_Shape.hxx>
52 #include <TopExp_Explorer.hxx>
53
54 #include <ios>
55 #include <cmath>
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<<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<<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     aResult<<anAttr->value();
248   } else if (aType == ModelAPI_AttributeReference::typeId()) {
249     AttributeReferencePtr anAttr =
250       std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttr);
251     if (anAttr->value().get()) {
252       aResult<< Locale::Convert::toString(anAttr->value()->data()->name());
253     } else {
254       aResult<<"__empty__";
255     }
256   } else if (aType == ModelAPI_AttributeSelection::typeId()) {
257     AttributeSelectionPtr anAttr =
258       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttr);
259     if (anAttr->context().get())
260       aResult<< Locale::Convert::toString(anAttr->namingName());
261     else
262       aResult<<"__notinitialized__";
263   } else if (aType == ModelAPI_AttributeSelectionList::typeId()) {
264     AttributeSelectionListPtr anAttr =
265       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttr);
266     for(int a = 0; a < anAttr->size(); a++) {
267       if (a != 0)
268         aResult<<" ";
269       aResult<< Locale::Convert::toString(anAttr->value(a)->namingName());
270     }
271   } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
272     AttributeRefAttrPtr anAttr =
273       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttr);
274     ObjectPtr anObj = anAttr->isObject() ? anAttr->object() : anAttr->attr()->owner();
275     if (anObj.get()) {
276       aResult<< Locale::Convert::toString(anObj->data()->name());
277       if (!anAttr->isObject()) {
278         aResult<<" "<<anAttr->attr()->id();
279       }
280     } else {
281       aResult<<"__empty__";
282     }
283   } else if (aType == ModelAPI_AttributeRefList::typeId()) {
284     AttributeRefListPtr anAttr =
285         std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttr);
286     // for sketch sub-features the empty values may be skipped and order is not important
287     bool isSketchFeatures = anAttr->id() == "Features" &&
288       std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner())->getKind() == "Sketch";
289     std::list<ObjectPtr> aList = anAttr->list();
290     std::list<std::string> aResList; // list of resulting strings
291     for(std::list<ObjectPtr>::iterator aL = aList.begin(); aL != aList.end(); aL++) {
292       if (aL->get()) {
293         if (isSketchFeatures) {
294           // do not control construction features of an ellipse and other
295           FeaturePtr aFeature = ModelAPI_Feature::feature(*aL);
296           //if (aFeature->getKind() == "SketchConstraintCoincidenceInternal")
297           //  continue; // skip internal constraints
298           std::string aStr = aFeature->getKind().substr(0, 16);
299           if (aStr == "SketchConstraint")
300             continue; // no need to dump and check constraints
301         }
302         aResList.push_back(Locale::Convert::toString((*aL)->data()->name()));
303       } else if (!isSketchFeatures) {
304         aResList.push_back("__empty__");
305       }
306     }
307     if (isSketchFeatures)
308       aResList.sort();
309     for(std::list<std::string>::iterator aR = aResList.begin(); aR != aResList.end(); aR++) {
310       aResult<<*aR<<" ";
311     }
312   } else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
313     AttributeRefAttrListPtr anAttr =
314       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttr);
315     std::list<std::pair<ObjectPtr, AttributePtr> > aList = anAttr->list();
316     std::list<std::pair<ObjectPtr, AttributePtr> >::iterator aL = aList.begin();
317     for(; aL != aList.end(); aL++) {
318       if (aL != aList.begin())
319         aResult<<" ";
320       ObjectPtr anObj = aL->second.get() ? aL->second->owner() : aL->first;
321       if (anObj.get()) {
322         aResult<< Locale::Convert::toString(anObj->data()->name());
323         if (aL->second.get()) {
324           aResult<<" "<<aL->second->id();
325         }
326       } else {
327         aResult<<"__empty__";
328       }
329     }
330   } else if (aType == ModelAPI_AttributeIntArray::typeId()) {
331     if (theAttr->id() == "Color") {
332       ResultConstructionPtr aResConstr =
333           std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theAttr->owner());
334       if (aResConstr.get())
335         return "__notinitialized__";
336     }
337     AttributeIntArrayPtr anAttr =
338       std::dynamic_pointer_cast<ModelAPI_AttributeIntArray>(theAttr);
339     for(int a = 0; a < anAttr->size(); a++)
340       aResult<<anAttr->value(a)<<" ";
341   } else if (aType == ModelAPI_AttributeDoubleArray::typeId()) {
342     AttributeDoubleArrayPtr anAttr =
343       std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(theAttr);
344     for(int a = 0; a < anAttr->size(); a++)
345       aResult<<anAttr->value(a)<<" ";
346   } else if (aType == ModelAPI_AttributeStringArray::typeId()) {
347     AttributeStringArrayPtr anAttr =
348       std::dynamic_pointer_cast<ModelAPI_AttributeStringArray>(theAttr);
349     for(int a = 0; a < anAttr->size(); a++)
350       aResult<<"'"<<anAttr->value(a)<<"'"<<" ";
351   } else if (aType == ModelAPI_AttributeTables::typeId()) {
352     AttributeTablesPtr anAttr =
353       std::dynamic_pointer_cast<ModelAPI_AttributeTables>(theAttr);
354     aResult<<anAttr->tables()<<"x"<<anAttr->rows()<<"x"<<anAttr->columns()<<" ";
355     for(int aTab = 0; aTab < anAttr->tables(); aTab++) {
356       for(int aRow = 0; aRow < anAttr->rows(); aRow++) {
357         for( int aCol = 0; aCol < anAttr->columns(); aCol++) {
358           switch(anAttr->type()) {
359           case ModelAPI_AttributeTables::BOOLEAN:
360             aResult<<anAttr->value(aRow, aCol, aTab).myBool<<" ";
361             break;
362           case ModelAPI_AttributeTables::INTEGER:
363             aResult<<anAttr->value(aRow, aCol, aTab).myInt<<" ";
364             break;
365           case ModelAPI_AttributeTables::DOUBLE:
366             aResult<<anAttr->value(aRow, aCol, aTab).myDouble<<" ";
367             break;
368           case ModelAPI_AttributeTables::STRING:
369             aResult<<"'"<<anAttr->value(aRow, aCol, aTab).myStr.c_str()<<"' ";
370             break;
371           }
372         }
373       }
374     }
375   } else if (aType == GeomDataAPI_Point::typeId()) {
376     AttributePointPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttr);
377     double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()};
378     dumpArray(aResult, aValues, 3);
379   } else if (aType == GeomDataAPI_Dir::typeId()) {
380     if (theAttr->id() == "DistanceDirection")
381       return "__notcase__";
382     AttributeDirPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theAttr);
383     double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()};
384     dumpArray(aResult, aValues, 3);
385   } else if (aType == GeomDataAPI_Point2D::typeId()) {
386     // do not dump flyout point for constraints as it may be changed unexpectedly,
387     // also do not dump selection points controlled by GUI
388     if (theAttr->id() == "ConstraintFlyoutValuePnt" ||
389         theAttr->id() == "SelectedPointA" || theAttr->id() == "SelectedPointB")
390       return "__notinitialized__";
391     AttributePoint2DPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttr);
392     double aValues[2] = {anAttr->x(), anAttr->y()};
393     dumpArray(aResult, aValues, 2);
394   } else {
395     aResult<<"__unknownattribute__";
396   }
397   return aResult.str();
398 }
399
400 std::string ModelHighAPI_FeatureStore::dumpShape(std::shared_ptr<GeomAPI_Shape>& theShape) {
401   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
402   if (aShape.IsNull()) {
403     return "null";
404   }
405   std::ostringstream aResult;
406   // output the number of shapes of different types
407   TopAbs_ShapeEnum aType = TopAbs_COMPOUND;
408   for(; aType <= TopAbs_VERTEX; aType = TopAbs_ShapeEnum((int)aType + 1)) {
409     TopExp_Explorer anExp(aShape, aType);
410     int aCount = 0;
411     for(; anExp.More(); anExp.Next()) aCount++;
412     TopAbs::Print(aType, aResult);
413     aResult<<": "<<aCount<<std::endl;
414   }
415   // output the main characteristics
416   double aVolume = GeomAlgoAPI_ShapeTools::volume(theShape);
417   if (aVolume > 1.e-5) {
418     aResult<<"Volume: ";
419     // volumes of too huge shapes write in the scientific format
420     if (aVolume >= 1.e5)
421       aResult<<std::scientific<<std::setprecision(7);
422     else
423       aResult<<std::fixed<<std::setprecision(3);
424     aResult<<aVolume<<std::endl;
425   }
426   std::shared_ptr<GeomAPI_Pnt> aCenter = GeomAlgoAPI_ShapeTools::centreOfMass(theShape);
427   aResult<<"Center of mass: ";
428   double aCenterVals[3] = {aCenter->x(), aCenter->y(), aCenter->z()};
429   dumpArray(aResult, aCenterVals, 3, 5);
430   aResult<<std::endl;
431   return aResult.str();
432 }