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