1 // Copyright (C) 2016-20xx CEA/DEN, EDF R&D -->
3 // File: ModelHighAPI_FeatureStore.cpp
4 // Created: 12 August 2016
5 // Author: Mikhail PONIKAROV
7 #include <ModelHighAPI_FeatureStore.h>
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>
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>
33 #include <TopoDS_Shape.hxx>
34 #include <TopExp_Explorer.hxx>
40 #define TOLERANCE (1.e-7)
42 ModelHighAPI_FeatureStore::ModelHighAPI_FeatureStore(FeaturePtr theFeature) {
43 storeData(theFeature->data(), myAttrs);
44 // iterate results to store
45 std::list<ResultPtr> allResults;
46 ModelAPI_Tools::allResults(theFeature, allResults);
47 std::list<ResultPtr>::iterator aRes = allResults.begin();
48 for(; aRes != allResults.end(); aRes++) {
49 std::map<std::string, std::string> aResDump;
50 storeData((*aRes)->data(), aResDump);
51 myRes.push_back(aResDump);
55 std::string ModelHighAPI_FeatureStore::compare(FeaturePtr theFeature) {
56 std::string anError = compareData(theFeature->data(), myAttrs);
57 if (!anError.empty()) {
58 return "Features '" + theFeature->name() + "' differ:" + anError;
60 std::list<ResultPtr> allResults;
61 ModelAPI_Tools::allResults(theFeature, allResults);
62 std::list<ResultPtr>::iterator aRes = allResults.begin();
63 std::list<std::map<std::string, std::string> >::iterator aResIter = myRes.begin();
64 for(; aRes != allResults.end() && aResIter != myRes.end(); aRes++, aResIter++) {
65 anError = compareData((*aRes)->data(), *aResIter);
67 return "Results of feature '" + theFeature->name() + "' '" + (*aRes)->data()->name() +
68 "' differ:" + anError;
70 if (aRes != allResults.end()) {
71 return "Current model has more results '" + (*aRes)->data()->name() + "'";
73 if (aResIter != myRes.end()) {
74 return "Original model had more results '" + (*aResIter)["__name__"] + "'";
79 void ModelHighAPI_FeatureStore::storeData(std::shared_ptr<ModelAPI_Data> theData,
80 std::map<std::string, std::string>& theAttrs)
82 theAttrs["__name__"] = theData->name(); // store name to keep also this information and output if needed
83 std::list<std::shared_ptr<ModelAPI_Attribute> > allAttrs = theData->attributes("");
84 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = allAttrs.begin();
85 for(; anAttr != allAttrs.end(); anAttr++) {
86 theAttrs[(*anAttr)->id()] = dumpAttr(*anAttr);
88 ResultPtr aShapeOwner = std::dynamic_pointer_cast<ModelAPI_Result>(theData->owner());
89 if (aShapeOwner.get() && aShapeOwner->shape().get()) {
90 std::shared_ptr<GeomAPI_Shape> aShape = aShapeOwner->shape();
91 theAttrs["__shape__"] = dumpShape(aShape);
95 std::string ModelHighAPI_FeatureStore::compareData(std::shared_ptr<ModelAPI_Data> theData,
96 std::map<std::string, std::string>& theAttrs)
98 std::map<std::string, std::string> aThis;
99 storeData(theData, aThis);
100 std::map<std::string, std::string>::iterator aThisIter = aThis.begin();
101 for(; aThisIter != aThis.end(); aThisIter++) {
102 if (theAttrs.find(aThisIter->first) == theAttrs.end()) {
103 return "original model had no attribute '" + aThisIter->first + "'";
105 if (theAttrs[aThisIter->first] != aThisIter->second) {
106 return "attribute '" + aThisIter->first + "' is different (original != current) '" +
107 theAttrs[aThisIter->first] + "' != '" + aThisIter->second + "'";
110 // iterate back to find lack attribute in the current model
111 std::map<std::string, std::string>::iterator anOrigIter = theAttrs.begin();
112 for(; anOrigIter != theAttrs.end(); anOrigIter++) {
113 if (aThis.find(anOrigIter->first) == aThis.end()) {
114 return "current model had no attribute '" + anOrigIter->first + "'";
120 static void dumpArray(std::ostringstream& theOutput, const double theArray[], int theSize)
122 for (int i = 0; i < theSize; ++i) {
125 theOutput << std::fixed << setprecision(PRECISION)
126 << (fabs(theArray[i]) < TOLERANCE ? 0.0 : theArray[i]);
130 std::string ModelHighAPI_FeatureStore::dumpAttr(const AttributePtr& theAttr) {
131 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
132 FeaturePtr aFeatOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttr->owner());
133 if (aFeatOwner.get() && !aFactory->isCase(aFeatOwner, theAttr->id())) {
134 return "__notcase__";
136 std::string aType = theAttr->attributeType();
137 std::ostringstream aResult;
138 if (!theAttr->isInitialized()) {
139 if (aType == ModelAPI_AttributeBoolean::typeId()) {
140 // special case for Boolean attribute (mean it false if not initialized)
142 return aResult.str();
143 } else if (aType == ModelAPI_AttributeString::typeId()) {
144 // special case for attribute "SolverError"
145 if (theAttr->id() == "SolverError" &&
146 std::dynamic_pointer_cast<ModelAPI_Feature>(theAttr->owner())->getKind() == "Sketch")
150 return "__notinitialized__";
152 if (aType == ModelAPI_AttributeDocRef::typeId()) {
153 AttributeDocRefPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDocRef>(theAttr);
154 DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
155 if (anAttr->value() != aDoc) {
156 ResultPtr aRes = ModelAPI_Tools::findPartResult(aDoc, anAttr->value());
158 aResult<<aRes->data()->name(); // Part result name (the same as saved file name)
161 aResult<<aDoc->kind(); // PartSet
163 } else if (aType == ModelAPI_AttributeInteger::typeId()) {
164 AttributeIntegerPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(theAttr);
165 if (anAttr->text().empty())
166 aResult<<anAttr->value();
168 aResult<<anAttr->text();
169 } else if (aType == ModelAPI_AttributeDouble::typeId()) {
170 AttributeDoublePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttr);
171 if (anAttr->text().empty()) {
172 double aVal = anAttr->value();
173 dumpArray(aResult, &aVal, 1);
175 aResult<<anAttr->text();
176 } else if (aType == ModelAPI_AttributeBoolean::typeId()) {
177 AttributeBooleanPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(theAttr);
178 aResult<<anAttr->value();
179 } else if (aType == ModelAPI_AttributeString::typeId()) {
180 AttributeStringPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttr);
181 aResult<<anAttr->value();
182 } else if (aType == ModelAPI_AttributeReference::typeId()) {
183 AttributeReferencePtr anAttr =
184 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttr);
185 if (anAttr->value().get()) {
186 aResult<<anAttr->value()->data()->name();
188 aResult<<"__empty__";
190 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
191 AttributeSelectionPtr anAttr =
192 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttr);
193 aResult<<anAttr->namingName();
194 } else if (aType == ModelAPI_AttributeSelectionList::typeId()) {
195 AttributeSelectionListPtr anAttr =
196 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttr);
197 for(int a = 0; a < anAttr->size(); a++) {
200 aResult<<anAttr->value(a)->namingName();
202 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
203 AttributeRefAttrPtr anAttr =
204 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttr);
205 ObjectPtr anObj = anAttr->isObject() ? anAttr->object() : anAttr->attr()->owner();
207 aResult<<anObj->data()->name();
208 if (!anAttr->isObject()) {
209 aResult<<" "<<anAttr->attr()->id();
212 aResult<<"__empty__";
214 } else if (aType == ModelAPI_AttributeRefList::typeId()) {
215 AttributeRefListPtr anAttr =
216 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttr);
217 // for sketch sub-features the empty values may be skipped and order is not important
218 bool isSketchFeatures = anAttr->id() == "Features" &&
219 std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner())->getKind() == "Sketch";
220 std::list<ObjectPtr> aList = anAttr->list();
221 std::list<std::string> aResList; // list of resulting strings
222 for(std::list<ObjectPtr>::iterator aL = aList.begin(); aL != aList.end(); aL++) {
224 aResList.push_back((*aL)->data()->name());
225 } else if (!isSketchFeatures) {
226 aResList.push_back("__empty__");
229 if (isSketchFeatures)
231 for(std::list<std::string>::iterator aR = aResList.begin(); aR != aResList.end(); aR++) {
234 } else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
235 AttributeRefAttrListPtr anAttr =
236 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttr);
237 std::list<std::pair<ObjectPtr, AttributePtr> > aList = anAttr->list();
238 std::list<std::pair<ObjectPtr, AttributePtr> >::iterator aL = aList.begin();
239 for(; aL != aList.end(); aL++) {
240 if (aL != aList.begin())
242 ObjectPtr anObj = aL->second.get() ? aL->second->owner() : aL->first;
244 aResult<<anObj->data()->name();
245 if (aL->second.get()) {
246 aResult<<" "<<aL->second->id();
249 aResult<<"__empty__";
252 } else if (aType == ModelAPI_AttributeIntArray::typeId()) {
253 AttributeIntArrayPtr anAttr =
254 std::dynamic_pointer_cast<ModelAPI_AttributeIntArray>(theAttr);
255 for(int a = 0; a < anAttr->size(); a++)
256 aResult<<anAttr->value(a)<<" ";
257 } else if (aType == ModelAPI_AttributeDoubleArray::typeId()) {
258 AttributeDoubleArrayPtr anAttr =
259 std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(theAttr);
260 for(int a = 0; a < anAttr->size(); a++)
261 aResult<<anAttr->value(a)<<" ";
262 } else if (aType == GeomDataAPI_Point::typeId()) {
263 AttributePointPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttr);
264 double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()};
265 dumpArray(aResult, aValues, 3);
266 } else if (aType == GeomDataAPI_Dir::typeId()) {
267 AttributeDirPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theAttr);
268 double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()};
269 dumpArray(aResult, aValues, 3);
270 } else if (aType == GeomDataAPI_Point2D::typeId()) {
271 AttributePoint2DPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttr);
272 double aValues[2] = {anAttr->x(), anAttr->y()};
273 dumpArray(aResult, aValues, 2);
275 aResult<<"__unknownattribute__";
277 return aResult.str();
280 std::string ModelHighAPI_FeatureStore::dumpShape(std::shared_ptr<GeomAPI_Shape>& theShape) {
281 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
282 if (aShape.IsNull()) {
285 std::ostringstream aResult;
286 // output the number of shapes of different types
287 TopAbs_ShapeEnum aType = TopAbs_COMPOUND;
288 for(; aType <= TopAbs_VERTEX; aType = TopAbs_ShapeEnum((int)aType + 1)) {
289 TopExp_Explorer anExp(aShape, aType);
291 for(; anExp.More(); anExp.Next()) aCount++;
292 TopAbs::Print(aType, aResult);
293 aResult<<": "<<aCount<<std::endl;
295 // output the main characteristics
296 aResult<<"Volume: "<<setprecision(2)<<GeomAlgoAPI_ShapeTools::volume(theShape)<<std::endl;
297 std::shared_ptr<GeomAPI_Pnt> aCenter = GeomAlgoAPI_ShapeTools::centreOfMass(theShape);
298 aResult<<"Center of mass: "<<std::fixed<<setprecision(PRECISION)
299 <<aCenter->x()<<" "<<aCenter->y()<<" "<<aCenter->z()<<std::endl;
300 return aResult.str();