Salome HOME
Sketcher: implement possibility to set zero distance
[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 '" + 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 << 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") {
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         }
293         aResList.push_back((*aL)->data()->name());
294       } else if (!isSketchFeatures) {
295         aResList.push_back("__empty__");
296       }
297     }
298     if (isSketchFeatures)
299       aResList.sort();
300     for(std::list<std::string>::iterator aR = aResList.begin(); aR != aResList.end(); aR++) {
301       aResult<<*aR<<" ";
302     }
303   } else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
304     AttributeRefAttrListPtr anAttr =
305       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttr);
306     std::list<std::pair<ObjectPtr, AttributePtr> > aList = anAttr->list();
307     std::list<std::pair<ObjectPtr, AttributePtr> >::iterator aL = aList.begin();
308     for(; aL != aList.end(); aL++) {
309       if (aL != aList.begin())
310         aResult<<" ";
311       ObjectPtr anObj = aL->second.get() ? aL->second->owner() : aL->first;
312       if (anObj.get()) {
313         aResult<<anObj->data()->name();
314         if (aL->second.get()) {
315           aResult<<" "<<aL->second->id();
316         }
317       } else {
318         aResult<<"__empty__";
319       }
320     }
321   } else if (aType == ModelAPI_AttributeIntArray::typeId()) {
322     if (theAttr->id() == "Color") {
323       ResultConstructionPtr aResConstr =
324           std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theAttr->owner());
325       if (aResConstr.get())
326         return "__notinitialized__";
327     }
328     AttributeIntArrayPtr anAttr =
329       std::dynamic_pointer_cast<ModelAPI_AttributeIntArray>(theAttr);
330     for(int a = 0; a < anAttr->size(); a++)
331       aResult<<anAttr->value(a)<<" ";
332   } else if (aType == ModelAPI_AttributeDoubleArray::typeId()) {
333     AttributeDoubleArrayPtr anAttr =
334       std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(theAttr);
335     for(int a = 0; a < anAttr->size(); a++)
336       aResult<<anAttr->value(a)<<" ";
337   } else if (aType == ModelAPI_AttributeStringArray::typeId()) {
338     AttributeStringArrayPtr anAttr =
339       std::dynamic_pointer_cast<ModelAPI_AttributeStringArray>(theAttr);
340     for(int a = 0; a < anAttr->size(); a++)
341       aResult<<"'"<<anAttr->value(a)<<"'"<<" ";
342   } else if (aType == ModelAPI_AttributeTables::typeId()) {
343     AttributeTablesPtr anAttr =
344       std::dynamic_pointer_cast<ModelAPI_AttributeTables>(theAttr);
345     aResult<<anAttr->tables()<<"x"<<anAttr->rows()<<"x"<<anAttr->columns()<<" ";
346     for(int aTab = 0; aTab < anAttr->tables(); aTab++) {
347       for(int aRow = 0; aRow < anAttr->rows(); aRow++) {
348         for( int aCol = 0; aCol < anAttr->columns(); aCol++) {
349           switch(anAttr->type()) {
350           case ModelAPI_AttributeTables::BOOLEAN:
351             aResult<<anAttr->value(aRow, aCol, aTab).myBool<<" ";
352             break;
353           case ModelAPI_AttributeTables::INTEGER:
354             aResult<<anAttr->value(aRow, aCol, aTab).myInt<<" ";
355             break;
356           case ModelAPI_AttributeTables::DOUBLE:
357             aResult<<anAttr->value(aRow, aCol, aTab).myDouble<<" ";
358             break;
359           case ModelAPI_AttributeTables::STRING:
360             aResult<<"'"<<anAttr->value(aRow, aCol, aTab).myStr.c_str()<<"' ";
361             break;
362           }
363         }
364       }
365     }
366   } else if (aType == GeomDataAPI_Point::typeId()) {
367     AttributePointPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttr);
368     double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()};
369     dumpArray(aResult, aValues, 3);
370   } else if (aType == GeomDataAPI_Dir::typeId()) {
371     if (theAttr->id() == "DistanceDirection")
372       return "__notcase__";
373     AttributeDirPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theAttr);
374     double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()};
375     dumpArray(aResult, aValues, 3);
376   } else if (aType == GeomDataAPI_Point2D::typeId()) {
377     // do not dump flyout point for constraints as it may be changed unexpectedly
378     if (theAttr->id() == "ConstraintFlyoutValuePnt")
379       return "__notinitialized__";
380     AttributePoint2DPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttr);
381     double aValues[2] = {anAttr->x(), anAttr->y()};
382     dumpArray(aResult, aValues, 2);
383   } else {
384     aResult<<"__unknownattribute__";
385   }
386   return aResult.str();
387 }
388
389 std::string ModelHighAPI_FeatureStore::dumpShape(std::shared_ptr<GeomAPI_Shape>& theShape) {
390   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
391   if (aShape.IsNull()) {
392     return "null";
393   }
394   std::ostringstream aResult;
395   // output the number of shapes of different types
396   TopAbs_ShapeEnum aType = TopAbs_COMPOUND;
397   for(; aType <= TopAbs_VERTEX; aType = TopAbs_ShapeEnum((int)aType + 1)) {
398     TopExp_Explorer anExp(aShape, aType);
399     int aCount = 0;
400     for(; anExp.More(); anExp.Next()) aCount++;
401     TopAbs::Print(aType, aResult);
402     aResult<<": "<<aCount<<std::endl;
403   }
404   // output the main characteristics
405   double aVolume = GeomAlgoAPI_ShapeTools::volume(theShape);
406   if (aVolume > 1.e-5) {
407     aResult<<"Volume: ";
408     // volumes of too huge shapes write in the scientific format
409     if (aVolume >= 1.e5)
410       aResult<<std::scientific<<setprecision(7);
411     else
412       aResult<<std::fixed<<setprecision(3);
413     aResult<<aVolume<<std::endl;
414   }
415   std::shared_ptr<GeomAPI_Pnt> aCenter = GeomAlgoAPI_ShapeTools::centreOfMass(theShape);
416   aResult<<"Center of mass: ";
417   double aCenterVals[3] = {aCenter->x(), aCenter->y(), aCenter->z()};
418   dumpArray(aResult, aCenterVals, 3, 5);
419   aResult<<std::endl;
420   return aResult.str();
421 }