]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModelHighAPI/ModelHighAPI_FeatureStore.cpp
Salome HOME
Added python dump checking for every unit test where it is useful.
[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 ModelHighAPI_FeatureStore::ModelHighAPI_FeatureStore(FeaturePtr theFeature) {
37   storeData(theFeature->data(), myAttrs);
38   // iterate results to store
39   std::list<ResultPtr> allResults;
40   ModelAPI_Tools::allResults(theFeature, allResults);
41   std::list<ResultPtr>::iterator aRes = allResults.begin();
42   for(; aRes != allResults.end(); aRes++) {
43     std::map<std::string, std::string> aResDump;
44     storeData((*aRes)->data(), aResDump);
45     myRes.push_back(aResDump);
46   }
47 }
48
49 std::string ModelHighAPI_FeatureStore::compare(FeaturePtr theFeature) {
50   std::string anError = compareData(theFeature->data(), myAttrs);
51   if (!anError.empty()) {
52     return "Features '" + theFeature->name() + "' differ:" + anError;
53   }
54   std::list<ResultPtr> allResults;
55   ModelAPI_Tools::allResults(theFeature, allResults);
56   std::list<ResultPtr>::iterator aRes = allResults.begin();
57   std::list<std::map<std::string, std::string> >::iterator aResIter = myRes.begin();
58   for(; aRes != allResults.end() && aResIter != myRes.end(); aRes++, aResIter++) {
59     anError = compareData((*aRes)->data(), *aResIter);
60     if (!anError.empty())
61       return "Results of feature '" + theFeature->name() + "' '" + (*aRes)->data()->name() + 
62       "' differ:" + anError;
63   }
64   if (aRes != allResults.end()) {
65     return "Current model has more results '" + (*aRes)->data()->name() + "'";
66   }
67   if (aResIter != myRes.end()) {
68     return "Original model had more results '" + (*aResIter)["__name__"] + "'";
69   }
70   return ""; // ok
71 }
72
73 void ModelHighAPI_FeatureStore::storeData(std::shared_ptr<ModelAPI_Data> theData, 
74   std::map<std::string, std::string>& theAttrs)
75 {
76   theAttrs["__name__"] = theData->name(); // store name to keep also this information and output if needed
77   std::list<std::shared_ptr<ModelAPI_Attribute> > allAttrs = theData->attributes("");
78   std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = allAttrs.begin();
79   for(; anAttr != allAttrs.end(); anAttr++) {
80     theAttrs[(*anAttr)->id()] = dumpAttr(*anAttr);
81   }
82   ResultPtr aShapeOwner = std::dynamic_pointer_cast<ModelAPI_Result>(theData->owner());
83   if (aShapeOwner.get() && aShapeOwner->shape().get()) {
84     theAttrs["__shape__"] = dumpShape(aShapeOwner->shape());
85   }
86 }
87
88 std::string ModelHighAPI_FeatureStore::compareData(std::shared_ptr<ModelAPI_Data> theData, 
89   std::map<std::string, std::string>& theAttrs)
90 {
91   std::map<std::string, std::string> aThis;
92   storeData(theData, aThis);
93   std::map<std::string, std::string>::iterator aThisIter = aThis.begin();
94   for(; aThisIter != aThis.end(); aThisIter++) {
95     if (theAttrs.find(aThisIter->first) == theAttrs.end()) {
96       return "original model had no attribute '" + aThisIter->first + "'";
97     }
98     if (theAttrs[aThisIter->first] != aThisIter->second) {
99       return "attribute '" + aThisIter->first + "' is different (original != current) '" + 
100         theAttrs[aThisIter->first] + "' != '" + aThisIter->second + "'";
101     }
102   }
103   // iterate back to find lack attribute in the current model
104   std::map<std::string, std::string>::iterator anOrigIter = theAttrs.begin();
105   for(; anOrigIter != theAttrs.end(); anOrigIter++) {
106     if (aThis.find(anOrigIter->first) == aThis.end()) {
107       return "current model had no attribute '" + anOrigIter->first + "'";
108     }
109   }
110   return "";
111 }
112
113 std::string ModelHighAPI_FeatureStore::dumpAttr(const AttributePtr& theAttr) {
114   static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
115   FeaturePtr aFeatOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttr->owner());
116   if (aFeatOwner.get() && !aFactory->isCase(aFeatOwner, theAttr->id())) {
117     return "__notcase__";
118   }
119   if (!theAttr->isInitialized()) {
120     return "__notinitialized__";
121   }
122   std::string aType = theAttr->attributeType();
123   std::ostringstream aResult;
124   if (aType == ModelAPI_AttributeDocRef::typeId()) {
125     AttributeDocRefPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDocRef>(theAttr);
126     DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
127     if (anAttr->value() != aDoc) {
128       ResultPtr aRes = ModelAPI_Tools::findPartResult(aDoc, anAttr->value());
129       if (aRes.get()) {
130         aResult<<aRes->data()->name(); // Part result name (the same as saved file name)
131       }
132     } else {
133       aResult<<aDoc->kind(); // PartSet
134     }
135   } else if (aType == ModelAPI_AttributeInteger::typeId()) {
136     AttributeIntegerPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(theAttr);
137     if (anAttr->text().empty())
138       aResult<<anAttr->value();
139     else
140       aResult<<anAttr->text();
141   } else if (aType == ModelAPI_AttributeDouble::typeId()) {
142     AttributeDoublePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttr);
143     if (anAttr->text().empty())
144       aResult<<anAttr->value();
145     else
146       aResult<<anAttr->text();
147   } else if (aType == ModelAPI_AttributeBoolean::typeId()) {
148     AttributeBooleanPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(theAttr);
149     aResult<<anAttr->value();
150   } else if (aType == ModelAPI_AttributeString::typeId()) {
151     AttributeStringPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttr);
152     aResult<<anAttr->value();
153   } else if (aType == ModelAPI_AttributeReference::typeId()) {
154     AttributeReferencePtr anAttr =
155       std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttr);
156     if (anAttr->value().get()) {
157       aResult<<anAttr->value()->data()->name();
158     } else {
159       aResult<<"__empty__";
160     }
161   } else if (aType == ModelAPI_AttributeSelection::typeId()) {
162     AttributeSelectionPtr anAttr = 
163       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttr);
164     aResult<<anAttr->namingName();
165   } else if (aType == ModelAPI_AttributeSelectionList::typeId()) {
166     AttributeSelectionListPtr anAttr = 
167       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttr);
168     for(int a = 0; a < anAttr->size(); a++) {
169       if (a != 0)
170         aResult<<" ";
171       aResult<<anAttr->value(a)->namingName();
172     }
173   } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
174     AttributeRefAttrPtr anAttr = 
175       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttr);
176     ObjectPtr anObj = anAttr->isObject() ? anAttr->object() : anAttr->attr()->owner();
177     if (anObj.get()) {
178       aResult<<anObj->data()->name();
179       if (!anAttr->isObject()) {
180         aResult<<" "<<anAttr->attr()->id();
181       }
182     } else {
183       aResult<<"__empty__";
184     }
185   } else if (aType == ModelAPI_AttributeRefList::typeId()) {
186     AttributeRefListPtr anAttr = 
187       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttr);
188     std::list<ObjectPtr> aList = anAttr->list();
189     for(std::list<ObjectPtr>::iterator aL = aList.begin(); aL != aList.end(); aL++) {
190       if (aL != aList.begin())
191         aResult<<" ";
192       if (aL->get()) {
193         aResult<<(*aL)->data()->name();
194       } else {
195         aResult<<"__empty__";
196       }
197     }
198   } else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
199     AttributeRefAttrListPtr anAttr = 
200       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttr);
201     std::list<std::pair<ObjectPtr, AttributePtr> > aList = anAttr->list();
202     std::list<std::pair<ObjectPtr, AttributePtr> >::iterator aL = aList.begin();
203     for(; aL != aList.end(); aL++) {
204       if (aL != aList.begin())
205         aResult<<" ";
206       ObjectPtr anObj = aL->second.get() ? aL->second->owner() : aL->first;
207       if (anObj.get()) {
208         aResult<<anObj->data()->name();
209         if (aL->second.get()) {
210           aResult<<" "<<aL->second->id();
211         }
212       } else {
213         aResult<<"__empty__";
214       }
215     }
216   } else if (aType == ModelAPI_AttributeIntArray::typeId()) {
217     AttributeIntArrayPtr anAttr = 
218       std::dynamic_pointer_cast<ModelAPI_AttributeIntArray>(theAttr);
219     for(int a = 0; a < anAttr->size(); a++)
220       aResult<<anAttr->value(a)<<" ";
221   } else if (aType == ModelAPI_AttributeDoubleArray::typeId()) {
222     AttributeDoubleArrayPtr anAttr = 
223       std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(theAttr);
224     for(int a = 0; a < anAttr->size(); a++)
225       aResult<<anAttr->value(a)<<" ";
226   } else if (aType == GeomDataAPI_Point::typeId()) {
227     AttributePointPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttr);
228     aResult<<anAttr->x()<<" "<<anAttr->y()<<" "<<anAttr->z();
229   } else if (aType == GeomDataAPI_Dir::typeId()) {
230     AttributeDirPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theAttr);
231     aResult<<anAttr->x()<<" "<<anAttr->y()<<" "<<anAttr->z();
232   } else if (aType == GeomDataAPI_Point2D::typeId()) {
233     AttributePoint2DPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttr);
234     aResult<<anAttr->x()<<" "<<anAttr->y()<<" ";
235   } else {
236     aResult<<"__unknownattribute__";
237   }
238   return aResult.str();
239 }
240
241 std::string ModelHighAPI_FeatureStore::dumpShape(std::shared_ptr<GeomAPI_Shape>& theShape) {
242   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
243   if (aShape.IsNull()) {
244     return "null";
245   }
246   std::ostringstream aResult;
247   // output the number of shapes of different types
248   TopAbs_ShapeEnum aType = TopAbs_COMPOUND;
249   for(; aType <= TopAbs_VERTEX; aType = TopAbs_ShapeEnum((int)aType + 1)) {
250     TopExp_Explorer anExp(aShape, aType);
251     int aCount = 0;
252     for(; anExp.More(); anExp.Next()) aCount++;
253     TopAbs::Print(aType, aResult);
254     aResult<<": "<<aCount<<std::endl;
255   }
256   // output the main characteristics
257   aResult<<"Volume: "<<setprecision(2)<<GeomAlgoAPI_ShapeTools::volume(theShape)<<std::endl;
258   std::shared_ptr<GeomAPI_Pnt> aCenter = GeomAlgoAPI_ShapeTools::centreOfMass(theShape);
259   aResult<<"Center of mass: "
260     <<aCenter->x()<<" "<<aCenter->y()<<" "<<aCenter->z()<<std::endl;
261   return aResult.str();
262 }