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>
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);
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;
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);
63 return "Results of feature '" + theFeature->name() + "' '" + (*aRes)->data()->name() +
64 "' differ:" + anError;
66 if (aRes != allResults.end()) {
67 return "Current model has more results '" + (*aRes)->data()->name() + "'";
69 if (aResIter != myRes.end()) {
70 return "Original model had more results '" + (*aResIter)["__name__"] + "'";
75 void ModelHighAPI_FeatureStore::storeData(std::shared_ptr<ModelAPI_Data> theData,
76 std::map<std::string, std::string>& theAttrs)
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);
84 ResultPtr aShapeOwner = std::dynamic_pointer_cast<ModelAPI_Result>(theData->owner());
85 if (aShapeOwner.get() && aShapeOwner->shape().get()) {
86 theAttrs["__shape__"] = dumpShape(aShapeOwner->shape());
90 std::string ModelHighAPI_FeatureStore::compareData(std::shared_ptr<ModelAPI_Data> theData,
91 std::map<std::string, std::string>& theAttrs)
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 + "'";
100 if (theAttrs[aThisIter->first] != aThisIter->second) {
101 return "attribute '" + aThisIter->first + "' is different (original != current) '" +
102 theAttrs[aThisIter->first] + "' != '" + aThisIter->second + "'";
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 + "'";
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__";
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)
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")
135 return "__notinitialized__";
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());
143 aResult<<aRes->data()->name(); // Part result name (the same as saved file name)
146 aResult<<aDoc->kind(); // PartSet
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();
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();
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();
172 aResult<<"__empty__";
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++) {
184 aResult<<anAttr->value(a)->namingName();
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();
191 aResult<<anObj->data()->name();
192 if (!anAttr->isObject()) {
193 aResult<<" "<<anAttr->attr()->id();
196 aResult<<"__empty__";
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++) {
208 aResList.push_back((*aL)->data()->name());
209 } else if (!isSketchFeatures) {
210 aResList.push_back("__empty__");
213 if (isSketchFeatures)
215 for(std::list<std::string>::iterator aR = aResList.begin(); aR != aResList.end(); aR++) {
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())
226 ObjectPtr anObj = aL->second.get() ? aL->second->owner() : aL->first;
228 aResult<<anObj->data()->name();
229 if (aL->second.get()) {
230 aResult<<" "<<aL->second->id();
233 aResult<<"__empty__";
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()<<" ";
256 aResult<<"__unknownattribute__";
258 return aResult.str();
261 std::string ModelHighAPI_FeatureStore::dumpShape(std::shared_ptr<GeomAPI_Shape>& theShape) {
262 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
263 if (aShape.IsNull()) {
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);
272 for(; anExp.More(); anExp.Next()) aCount++;
273 TopAbs::Print(aType, aResult);
274 aResult<<": "<<aCount<<std::endl;
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();