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 theAttrs["__shape__"] = dumpShape(aShapeOwner->shape());
94 std::string ModelHighAPI_FeatureStore::compareData(std::shared_ptr<ModelAPI_Data> theData,
95 std::map<std::string, std::string>& theAttrs)
97 std::map<std::string, std::string> aThis;
98 storeData(theData, aThis);
99 std::map<std::string, std::string>::iterator aThisIter = aThis.begin();
100 for(; aThisIter != aThis.end(); aThisIter++) {
101 if (theAttrs.find(aThisIter->first) == theAttrs.end()) {
102 return "original model had no attribute '" + aThisIter->first + "'";
104 if (theAttrs[aThisIter->first] != aThisIter->second) {
105 return "attribute '" + aThisIter->first + "' is different (original != current) '" +
106 theAttrs[aThisIter->first] + "' != '" + aThisIter->second + "'";
109 // iterate back to find lack attribute in the current model
110 std::map<std::string, std::string>::iterator anOrigIter = theAttrs.begin();
111 for(; anOrigIter != theAttrs.end(); anOrigIter++) {
112 if (aThis.find(anOrigIter->first) == aThis.end()) {
113 return "current model had no attribute '" + anOrigIter->first + "'";
119 static void dumpArray(std::ostringstream& theOutput, const double theArray[], int theSize)
121 for (int i = 0; i < theSize; ++i) {
124 theOutput << std::fixed << setprecision(PRECISION)
125 << (fabs(theArray[i]) < TOLERANCE ? 0.0 : theArray[i]);
129 std::string ModelHighAPI_FeatureStore::dumpAttr(const AttributePtr& theAttr) {
130 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
131 FeaturePtr aFeatOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttr->owner());
132 if (aFeatOwner.get() && !aFactory->isCase(aFeatOwner, theAttr->id())) {
133 return "__notcase__";
135 std::string aType = theAttr->attributeType();
136 std::ostringstream aResult;
137 if (!theAttr->isInitialized()) {
138 if (aType == ModelAPI_AttributeBoolean::typeId()) {
139 // special case for Boolean attribute (mean it false if not initialized)
141 return aResult.str();
142 } else if (aType == ModelAPI_AttributeString::typeId()) {
143 // special case for attribute "SolverError"
144 if (theAttr->id() == "SolverError" &&
145 std::dynamic_pointer_cast<ModelAPI_Feature>(theAttr->owner())->getKind() == "Sketch")
149 return "__notinitialized__";
151 if (aType == ModelAPI_AttributeDocRef::typeId()) {
152 AttributeDocRefPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDocRef>(theAttr);
153 DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
154 if (anAttr->value() != aDoc) {
155 ResultPtr aRes = ModelAPI_Tools::findPartResult(aDoc, anAttr->value());
157 aResult<<aRes->data()->name(); // Part result name (the same as saved file name)
160 aResult<<aDoc->kind(); // PartSet
162 } else if (aType == ModelAPI_AttributeInteger::typeId()) {
163 AttributeIntegerPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(theAttr);
164 if (anAttr->text().empty())
165 aResult<<anAttr->value();
167 aResult<<anAttr->text();
168 } else if (aType == ModelAPI_AttributeDouble::typeId()) {
169 AttributeDoublePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttr);
170 if (anAttr->text().empty()) {
171 double aVal = anAttr->value();
172 dumpArray(aResult, &aVal, 1);
174 aResult<<anAttr->text();
175 } else if (aType == ModelAPI_AttributeBoolean::typeId()) {
176 AttributeBooleanPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(theAttr);
177 aResult<<anAttr->value();
178 } else if (aType == ModelAPI_AttributeString::typeId()) {
179 AttributeStringPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttr);
180 aResult<<anAttr->value();
181 } else if (aType == ModelAPI_AttributeReference::typeId()) {
182 AttributeReferencePtr anAttr =
183 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttr);
184 if (anAttr->value().get()) {
185 aResult<<anAttr->value()->data()->name();
187 aResult<<"__empty__";
189 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
190 AttributeSelectionPtr anAttr =
191 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttr);
192 aResult<<anAttr->namingName();
193 } else if (aType == ModelAPI_AttributeSelectionList::typeId()) {
194 AttributeSelectionListPtr anAttr =
195 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttr);
196 for(int a = 0; a < anAttr->size(); a++) {
199 aResult<<anAttr->value(a)->namingName();
201 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
202 AttributeRefAttrPtr anAttr =
203 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttr);
204 ObjectPtr anObj = anAttr->isObject() ? anAttr->object() : anAttr->attr()->owner();
206 aResult<<anObj->data()->name();
207 if (!anAttr->isObject()) {
208 aResult<<" "<<anAttr->attr()->id();
211 aResult<<"__empty__";
213 } else if (aType == ModelAPI_AttributeRefList::typeId()) {
214 AttributeRefListPtr anAttr =
215 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttr);
216 // for sketch sub-features the empty values may be skipped and order is not important
217 bool isSketchFeatures = anAttr->id() == "Features" &&
218 std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner())->getKind() == "Sketch";
219 std::list<ObjectPtr> aList = anAttr->list();
220 std::list<std::string> aResList; // list of resulting strings
221 for(std::list<ObjectPtr>::iterator aL = aList.begin(); aL != aList.end(); aL++) {
223 aResList.push_back((*aL)->data()->name());
224 } else if (!isSketchFeatures) {
225 aResList.push_back("__empty__");
228 if (isSketchFeatures)
230 for(std::list<std::string>::iterator aR = aResList.begin(); aR != aResList.end(); aR++) {
233 } else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
234 AttributeRefAttrListPtr anAttr =
235 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttr);
236 std::list<std::pair<ObjectPtr, AttributePtr> > aList = anAttr->list();
237 std::list<std::pair<ObjectPtr, AttributePtr> >::iterator aL = aList.begin();
238 for(; aL != aList.end(); aL++) {
239 if (aL != aList.begin())
241 ObjectPtr anObj = aL->second.get() ? aL->second->owner() : aL->first;
243 aResult<<anObj->data()->name();
244 if (aL->second.get()) {
245 aResult<<" "<<aL->second->id();
248 aResult<<"__empty__";
251 } else if (aType == ModelAPI_AttributeIntArray::typeId()) {
252 AttributeIntArrayPtr anAttr =
253 std::dynamic_pointer_cast<ModelAPI_AttributeIntArray>(theAttr);
254 for(int a = 0; a < anAttr->size(); a++)
255 aResult<<anAttr->value(a)<<" ";
256 } else if (aType == ModelAPI_AttributeDoubleArray::typeId()) {
257 AttributeDoubleArrayPtr anAttr =
258 std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(theAttr);
259 for(int a = 0; a < anAttr->size(); a++)
260 aResult<<anAttr->value(a)<<" ";
261 } else if (aType == GeomDataAPI_Point::typeId()) {
262 AttributePointPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttr);
263 double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()};
264 dumpArray(aResult, aValues, 3);
265 } else if (aType == GeomDataAPI_Dir::typeId()) {
266 AttributeDirPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theAttr);
267 double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()};
268 dumpArray(aResult, aValues, 3);
269 } else if (aType == GeomDataAPI_Point2D::typeId()) {
270 AttributePoint2DPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttr);
271 double aValues[2] = {anAttr->x(), anAttr->y()};
272 dumpArray(aResult, aValues, 2);
274 aResult<<"__unknownattribute__";
276 return aResult.str();
279 std::string ModelHighAPI_FeatureStore::dumpShape(std::shared_ptr<GeomAPI_Shape>& theShape) {
280 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
281 if (aShape.IsNull()) {
284 std::ostringstream aResult;
285 // output the number of shapes of different types
286 TopAbs_ShapeEnum aType = TopAbs_COMPOUND;
287 for(; aType <= TopAbs_VERTEX; aType = TopAbs_ShapeEnum((int)aType + 1)) {
288 TopExp_Explorer anExp(aShape, aType);
290 for(; anExp.More(); anExp.Next()) aCount++;
291 TopAbs::Print(aType, aResult);
292 aResult<<": "<<aCount<<std::endl;
294 // output the main characteristics
295 aResult<<"Volume: "<<setprecision(2)<<GeomAlgoAPI_ShapeTools::volume(theShape)<<std::endl;
296 std::shared_ptr<GeomAPI_Pnt> aCenter = GeomAlgoAPI_ShapeTools::centreOfMass(theShape);
297 aResult<<"Center of mass: "<<std::fixed<<setprecision(PRECISION)
298 <<aCenter->x()<<" "<<aCenter->y()<<" "<<aCenter->z()<<std::endl;
299 return aResult.str();