]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModelHighAPI/ModelHighAPI_FeatureStore.cpp
Salome HOME
Fix errors and adjust unit tests
[modules/shaper.git] / src / ModelHighAPI / ModelHighAPI_FeatureStore.cpp
1 // Copyright (C) 2016-20xx CEA/DEN, EDF R&D -->
2
3 // File:        ModelHighAPI_FeatureStore.cpp
4 // Created:     12 August 2016
5 // Author:      Mikhail PONIKAROV
6
7 #include <ModelHighAPI_FeatureStore.h>
8
9 #include <ModelAPI_Tools.h>
10 #include <ModelAPI_ResultPart.h>
11 #include <ModelAPI_Session.h>
12 #include <ModelAPI_AttributeBoolean.h>
13 #include <ModelAPI_AttributeDocRef.h>
14 #include <ModelAPI_AttributeDouble.h>
15 #include <ModelAPI_AttributeIntArray.h>
16 #include <ModelAPI_AttributeInteger.h>
17 #include <ModelAPI_AttributeRefAttr.h>
18 #include <ModelAPI_AttributeRefAttrList.h>
19 #include <ModelAPI_AttributeReference.h>
20 #include <ModelAPI_AttributeRefList.h>
21 #include <ModelAPI_AttributeSelection.h>
22 #include <ModelAPI_AttributeSelectionList.h>
23 #include <ModelAPI_AttributeString.h>
24 #include <ModelAPI_AttributeDoubleArray.h>
25 #include <ModelAPI_Validator.h>
26
27 #include <GeomDataAPI_Dir.h>
28 #include <GeomDataAPI_Point.h>
29 #include <GeomDataAPI_Point2D.h>
30 #include <GeomAlgoAPI_ShapeTools.h>
31 #include <GeomAPI_Pnt.h>
32
33 #include <TopoDS_Shape.hxx>
34 #include <TopExp_Explorer.hxx>
35
36 #include <ios>
37
38 ModelHighAPI_FeatureStore::ModelHighAPI_FeatureStore(FeaturePtr theFeature) {
39   storeData(theFeature->data(), myAttrs);
40   // iterate results to store
41   std::list<ResultPtr> allResults;
42   ModelAPI_Tools::allResults(theFeature, allResults);
43   std::list<ResultPtr>::iterator aRes = allResults.begin();
44   for(; aRes != allResults.end(); aRes++) {
45     std::map<std::string, std::string> aResDump;
46     storeData((*aRes)->data(), aResDump);
47     myRes.push_back(aResDump);
48   }
49 }
50
51 std::string ModelHighAPI_FeatureStore::compare(FeaturePtr theFeature) {
52   std::string anError = compareData(theFeature->data(), myAttrs);
53   if (!anError.empty()) {
54     return "Features '" + theFeature->name() + "' differ:" + anError;
55   }
56   std::list<ResultPtr> allResults;
57   ModelAPI_Tools::allResults(theFeature, allResults);
58   std::list<ResultPtr>::iterator aRes = allResults.begin();
59   std::list<std::map<std::string, std::string> >::iterator aResIter = myRes.begin();
60   for(; aRes != allResults.end() && aResIter != myRes.end(); aRes++, aResIter++) {
61     anError = compareData((*aRes)->data(), *aResIter);
62     if (!anError.empty())
63       return "Results of feature '" + theFeature->name() + "' '" + (*aRes)->data()->name() + 
64       "' differ:" + anError;
65   }
66   if (aRes != allResults.end()) {
67     return "Current model has more results '" + (*aRes)->data()->name() + "'";
68   }
69   if (aResIter != myRes.end()) {
70     return "Original model had more results '" + (*aResIter)["__name__"] + "'";
71   }
72   return ""; // ok
73 }
74
75 void ModelHighAPI_FeatureStore::storeData(std::shared_ptr<ModelAPI_Data> theData, 
76   std::map<std::string, std::string>& theAttrs)
77 {
78   theAttrs["__name__"] = theData->name(); // store name to keep also this information and output if needed
79   std::list<std::shared_ptr<ModelAPI_Attribute> > allAttrs = theData->attributes("");
80   std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = allAttrs.begin();
81   for(; anAttr != allAttrs.end(); anAttr++) {
82     theAttrs[(*anAttr)->id()] = dumpAttr(*anAttr);
83   }
84   ResultPtr aShapeOwner = std::dynamic_pointer_cast<ModelAPI_Result>(theData->owner());
85   if (aShapeOwner.get() && aShapeOwner->shape().get()) {
86     theAttrs["__shape__"] = dumpShape(aShapeOwner->shape());
87   }
88 }
89
90 std::string ModelHighAPI_FeatureStore::compareData(std::shared_ptr<ModelAPI_Data> theData, 
91   std::map<std::string, std::string>& theAttrs)
92 {
93   std::map<std::string, std::string> aThis;
94   storeData(theData, aThis);
95   std::map<std::string, std::string>::iterator aThisIter = aThis.begin();
96   for(; aThisIter != aThis.end(); aThisIter++) {
97     if (theAttrs.find(aThisIter->first) == theAttrs.end()) {
98       return "original model had no attribute '" + aThisIter->first + "'";
99     }
100     if (theAttrs[aThisIter->first] != aThisIter->second) {
101       return "attribute '" + aThisIter->first + "' is different (original != current) '" + 
102         theAttrs[aThisIter->first] + "' != '" + aThisIter->second + "'";
103     }
104   }
105   // iterate back to find lack attribute in the current model
106   std::map<std::string, std::string>::iterator anOrigIter = theAttrs.begin();
107   for(; anOrigIter != theAttrs.end(); anOrigIter++) {
108     if (aThis.find(anOrigIter->first) == aThis.end()) {
109       return "current model had no attribute '" + anOrigIter->first + "'";
110     }
111   }
112   return "";
113 }
114
115 std::string ModelHighAPI_FeatureStore::dumpAttr(const AttributePtr& theAttr) {
116   static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
117   FeaturePtr aFeatOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttr->owner());
118   if (aFeatOwner.get() && !aFactory->isCase(aFeatOwner, theAttr->id())) {
119     return "__notcase__";
120   }
121   std::string aType = theAttr->attributeType();
122   std::ostringstream aResult;
123   if (!theAttr->isInitialized()) {
124     if (aType == ModelAPI_AttributeBoolean::typeId()) {
125       // special case for Boolean attribute (mean it false if not initialized)
126       aResult << false;
127       return aResult.str();
128     } else if (aType == ModelAPI_AttributeString::typeId()) {
129       // special case for attribute "SolverError"
130       if (theAttr->id() == "SolverError" && 
131           std::dynamic_pointer_cast<ModelAPI_Feature>(theAttr->owner())->getKind() == "Sketch")
132         return "";
133     }
134
135     return "__notinitialized__";
136   }
137   if (aType == ModelAPI_AttributeDocRef::typeId()) {
138     AttributeDocRefPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDocRef>(theAttr);
139     DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
140     if (anAttr->value() != aDoc) {
141       ResultPtr aRes = ModelAPI_Tools::findPartResult(aDoc, anAttr->value());
142       if (aRes.get()) {
143         aResult<<aRes->data()->name(); // Part result name (the same as saved file name)
144       }
145     } else {
146       aResult<<aDoc->kind(); // PartSet
147     }
148   } else if (aType == ModelAPI_AttributeInteger::typeId()) {
149     AttributeIntegerPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(theAttr);
150     if (anAttr->text().empty())
151       aResult<<anAttr->value();
152     else
153       aResult<<anAttr->text();
154   } else if (aType == ModelAPI_AttributeDouble::typeId()) {
155     AttributeDoublePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttr);
156     if (anAttr->text().empty())
157       aResult<<std::fixed<<setprecision(7)<<anAttr->value();
158     else
159       aResult<<anAttr->text();
160   } else if (aType == ModelAPI_AttributeBoolean::typeId()) {
161     AttributeBooleanPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(theAttr);
162     aResult<<anAttr->value();
163   } else if (aType == ModelAPI_AttributeString::typeId()) {
164     AttributeStringPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttr);
165     aResult<<anAttr->value();
166   } else if (aType == ModelAPI_AttributeReference::typeId()) {
167     AttributeReferencePtr anAttr =
168       std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttr);
169     if (anAttr->value().get()) {
170       aResult<<anAttr->value()->data()->name();
171     } else {
172       aResult<<"__empty__";
173     }
174   } else if (aType == ModelAPI_AttributeSelection::typeId()) {
175     AttributeSelectionPtr anAttr = 
176       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttr);
177     aResult<<anAttr->namingName();
178   } else if (aType == ModelAPI_AttributeSelectionList::typeId()) {
179     AttributeSelectionListPtr anAttr = 
180       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttr);
181     for(int a = 0; a < anAttr->size(); a++) {
182       if (a != 0)
183         aResult<<" ";
184       aResult<<anAttr->value(a)->namingName();
185     }
186   } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
187     AttributeRefAttrPtr anAttr = 
188       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttr);
189     ObjectPtr anObj = anAttr->isObject() ? anAttr->object() : anAttr->attr()->owner();
190     if (anObj.get()) {
191       aResult<<anObj->data()->name();
192       if (!anAttr->isObject()) {
193         aResult<<" "<<anAttr->attr()->id();
194       }
195     } else {
196       aResult<<"__empty__";
197     }
198   } else if (aType == ModelAPI_AttributeRefList::typeId()) {
199     AttributeRefListPtr anAttr = 
200       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttr);
201     // for sketch sub-features the empty values may be skipped and order is not important
202     bool isSketchFeatures = anAttr->id() == "Features" && 
203       std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner())->getKind() == "Sketch";
204     std::list<ObjectPtr> aList = anAttr->list();
205     std::list<std::string> aResList; // list of resulting strings
206     for(std::list<ObjectPtr>::iterator aL = aList.begin(); aL != aList.end(); aL++) {
207       if (aL->get()) {
208         aResList.push_back((*aL)->data()->name());
209       } else if (!isSketchFeatures) {
210         aResList.push_back("__empty__");
211       }
212     }
213     if (isSketchFeatures)
214       aResList.sort();
215     for(std::list<std::string>::iterator aR = aResList.begin(); aR != aResList.end(); aR++) {
216       aResult<<*aR<<" ";
217     }
218   } else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
219     AttributeRefAttrListPtr anAttr = 
220       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttr);
221     std::list<std::pair<ObjectPtr, AttributePtr> > aList = anAttr->list();
222     std::list<std::pair<ObjectPtr, AttributePtr> >::iterator aL = aList.begin();
223     for(; aL != aList.end(); aL++) {
224       if (aL != aList.begin())
225         aResult<<" ";
226       ObjectPtr anObj = aL->second.get() ? aL->second->owner() : aL->first;
227       if (anObj.get()) {
228         aResult<<anObj->data()->name();
229         if (aL->second.get()) {
230           aResult<<" "<<aL->second->id();
231         }
232       } else {
233         aResult<<"__empty__";
234       }
235     }
236   } else if (aType == ModelAPI_AttributeIntArray::typeId()) {
237     AttributeIntArrayPtr anAttr = 
238       std::dynamic_pointer_cast<ModelAPI_AttributeIntArray>(theAttr);
239     for(int a = 0; a < anAttr->size(); a++)
240       aResult<<anAttr->value(a)<<" ";
241   } else if (aType == ModelAPI_AttributeDoubleArray::typeId()) {
242     AttributeDoubleArrayPtr anAttr = 
243       std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(theAttr);
244     for(int a = 0; a < anAttr->size(); a++)
245       aResult<<anAttr->value(a)<<" ";
246   } else if (aType == GeomDataAPI_Point::typeId()) {
247     AttributePointPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttr);
248     aResult<<anAttr->x()<<" "<<anAttr->y()<<" "<<anAttr->z();
249   } else if (aType == GeomDataAPI_Dir::typeId()) {
250     AttributeDirPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theAttr);
251     aResult<<anAttr->x()<<" "<<anAttr->y()<<" "<<anAttr->z();
252   } else if (aType == GeomDataAPI_Point2D::typeId()) {
253     AttributePoint2DPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttr);
254     aResult<<anAttr->x()<<" "<<anAttr->y()<<" ";
255   } else {
256     aResult<<"__unknownattribute__";
257   }
258   return aResult.str();
259 }
260
261 std::string ModelHighAPI_FeatureStore::dumpShape(std::shared_ptr<GeomAPI_Shape>& theShape) {
262   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
263   if (aShape.IsNull()) {
264     return "null";
265   }
266   std::ostringstream aResult;
267   // output the number of shapes of different types
268   TopAbs_ShapeEnum aType = TopAbs_COMPOUND;
269   for(; aType <= TopAbs_VERTEX; aType = TopAbs_ShapeEnum((int)aType + 1)) {
270     TopExp_Explorer anExp(aShape, aType);
271     int aCount = 0;
272     for(; anExp.More(); anExp.Next()) aCount++;
273     TopAbs::Print(aType, aResult);
274     aResult<<": "<<aCount<<std::endl;
275   }
276   // output the main characteristics
277   aResult<<"Volume: "<<setprecision(2)<<GeomAlgoAPI_ShapeTools::volume(theShape)<<std::endl;
278   std::shared_ptr<GeomAPI_Pnt> aCenter = GeomAlgoAPI_ShapeTools::centreOfMass(theShape);
279   aResult<<"Center of mass: "<<std::fixed<<setprecision(7)
280     <<aCenter->x()<<" "<<aCenter->y()<<" "<<aCenter->z()<<std::endl;
281   return aResult.str();
282 }