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 // store name to keep also this information and output if needed
83 theAttrs["__name__"] = theData->name();
84 std::list<std::shared_ptr<ModelAPI_Attribute> > allAttrs = theData->attributes("");
85 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = allAttrs.begin();
86 for(; anAttr != allAttrs.end(); anAttr++) {
87 theAttrs[(*anAttr)->id()] = dumpAttr(*anAttr);
89 ResultPtr aShapeOwner = std::dynamic_pointer_cast<ModelAPI_Result>(theData->owner());
90 if (aShapeOwner.get() && aShapeOwner->shape().get()) {
91 std::shared_ptr<GeomAPI_Shape> aShape = aShapeOwner->shape();
92 theAttrs["__shape__"] = dumpShape(aShape);
96 std::string ModelHighAPI_FeatureStore::compareData(std::shared_ptr<ModelAPI_Data> theData,
97 std::map<std::string, std::string>& theAttrs)
99 std::map<std::string, std::string> aThis;
100 storeData(theData, aThis);
101 std::map<std::string, std::string>::iterator aThisIter = aThis.begin();
102 for(; aThisIter != aThis.end(); aThisIter++) {
103 if (theAttrs.find(aThisIter->first) == theAttrs.end()) {
104 return "original model had no attribute '" + aThisIter->first + "'";
106 if (theAttrs[aThisIter->first] != aThisIter->second) {
107 return "attribute '" + aThisIter->first + "' is different (original != current) '" +
108 theAttrs[aThisIter->first] + "' != '" + aThisIter->second + "'";
111 // iterate back to find lack attribute in the current model
112 std::map<std::string, std::string>::iterator anOrigIter = theAttrs.begin();
113 for(; anOrigIter != theAttrs.end(); anOrigIter++) {
114 if (aThis.find(anOrigIter->first) == aThis.end()) {
115 return "current model had no attribute '" + anOrigIter->first + "'";
121 static void dumpArray(std::ostringstream& theOutput, const double theArray[],
122 int theSize, int thePrecision = PRECISION)
124 for (int i = 0; i < theSize; ++i) {
127 theOutput << std::fixed << setprecision(thePrecision)
128 << (fabs(theArray[i]) < TOLERANCE ? 0.0 : theArray[i]);
132 std::string ModelHighAPI_FeatureStore::dumpAttr(const AttributePtr& theAttr) {
133 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
134 FeaturePtr aFeatOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttr->owner());
135 if (aFeatOwner.get() && !aFactory->isCase(aFeatOwner, theAttr->id())) {
136 return "__notcase__";
138 std::string aType = theAttr->attributeType();
139 std::ostringstream aResult;
140 if (!theAttr->isInitialized()) {
141 if (aType == ModelAPI_AttributeBoolean::typeId()) {
142 // special case for Boolean attribute (mean it false if not initialized)
144 return aResult.str();
145 } else if (aType == ModelAPI_AttributeString::typeId()) {
146 // special case for attribute "SolverError"
147 if (theAttr->id() == "SolverError" &&
148 std::dynamic_pointer_cast<ModelAPI_Feature>(theAttr->owner())->getKind() == "Sketch")
152 return "__notinitialized__";
154 if (aType == ModelAPI_AttributeDocRef::typeId()) {
155 AttributeDocRefPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDocRef>(theAttr);
156 DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
157 if (anAttr->value() != aDoc) {
158 ResultPtr aRes = ModelAPI_Tools::findPartResult(aDoc, anAttr->value());
160 aResult<<aRes->data()->name(); // Part result name (the same as saved file name)
163 aResult<<aDoc->kind(); // PartSet
165 } else if (aType == ModelAPI_AttributeInteger::typeId()) {
166 AttributeIntegerPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(theAttr);
167 if (anAttr->text().empty())
168 aResult<<anAttr->value();
170 aResult<<anAttr->text();
171 } else if (aType == ModelAPI_AttributeDouble::typeId()) {
172 AttributeDoublePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttr);
173 int aPrecision = PRECISION;
174 // Special case - precision for the arc angle. It is calculated with tolerance 1e-4,
175 // so the value has only 4 correct digits
176 if (anAttr->id() == "ArcAngle")
178 if (anAttr->text().empty()) {
179 double aVal = anAttr->value();
180 dumpArray(aResult, &aVal, 1, aPrecision);
182 aResult<<anAttr->text();
183 } else if (aType == ModelAPI_AttributeBoolean::typeId()) {
184 AttributeBooleanPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(theAttr);
185 aResult<<anAttr->value();
186 } else if (aType == ModelAPI_AttributeString::typeId()) {
187 AttributeStringPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttr);
188 aResult<<anAttr->value();
189 } else if (aType == ModelAPI_AttributeReference::typeId()) {
190 AttributeReferencePtr anAttr =
191 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttr);
192 if (anAttr->value().get()) {
193 aResult<<anAttr->value()->data()->name();
195 aResult<<"__empty__";
197 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
198 AttributeSelectionPtr anAttr =
199 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttr);
200 aResult<<anAttr->namingName();
201 } else if (aType == ModelAPI_AttributeSelectionList::typeId()) {
202 AttributeSelectionListPtr anAttr =
203 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttr);
204 for(int a = 0; a < anAttr->size(); a++) {
207 aResult<<anAttr->value(a)->namingName();
209 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
210 AttributeRefAttrPtr anAttr =
211 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttr);
212 ObjectPtr anObj = anAttr->isObject() ? anAttr->object() : anAttr->attr()->owner();
214 aResult<<anObj->data()->name();
215 if (!anAttr->isObject()) {
216 aResult<<" "<<anAttr->attr()->id();
219 aResult<<"__empty__";
221 } else if (aType == ModelAPI_AttributeRefList::typeId()) {
222 AttributeRefListPtr anAttr =
223 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttr);
224 // for sketch sub-features the empty values may be skipped and order is not important
225 bool isSketchFeatures = anAttr->id() == "Features" &&
226 std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner())->getKind() == "Sketch";
227 std::list<ObjectPtr> aList = anAttr->list();
228 std::list<std::string> aResList; // list of resulting strings
229 for(std::list<ObjectPtr>::iterator aL = aList.begin(); aL != aList.end(); aL++) {
231 aResList.push_back((*aL)->data()->name());
232 } else if (!isSketchFeatures) {
233 aResList.push_back("__empty__");
236 if (isSketchFeatures)
238 for(std::list<std::string>::iterator aR = aResList.begin(); aR != aResList.end(); aR++) {
241 } else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
242 AttributeRefAttrListPtr anAttr =
243 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttr);
244 std::list<std::pair<ObjectPtr, AttributePtr> > aList = anAttr->list();
245 std::list<std::pair<ObjectPtr, AttributePtr> >::iterator aL = aList.begin();
246 for(; aL != aList.end(); aL++) {
247 if (aL != aList.begin())
249 ObjectPtr anObj = aL->second.get() ? aL->second->owner() : aL->first;
251 aResult<<anObj->data()->name();
252 if (aL->second.get()) {
253 aResult<<" "<<aL->second->id();
256 aResult<<"__empty__";
259 } else if (aType == ModelAPI_AttributeIntArray::typeId()) {
260 AttributeIntArrayPtr anAttr =
261 std::dynamic_pointer_cast<ModelAPI_AttributeIntArray>(theAttr);
262 for(int a = 0; a < anAttr->size(); a++)
263 aResult<<anAttr->value(a)<<" ";
264 } else if (aType == ModelAPI_AttributeDoubleArray::typeId()) {
265 AttributeDoubleArrayPtr anAttr =
266 std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(theAttr);
267 for(int a = 0; a < anAttr->size(); a++)
268 aResult<<anAttr->value(a)<<" ";
269 } else if (aType == GeomDataAPI_Point::typeId()) {
270 AttributePointPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttr);
271 double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()};
272 dumpArray(aResult, aValues, 3);
273 } else if (aType == GeomDataAPI_Dir::typeId()) {
274 AttributeDirPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theAttr);
275 double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()};
276 dumpArray(aResult, aValues, 3);
277 } else if (aType == GeomDataAPI_Point2D::typeId()) {
278 // do not dump flyout point for constraints as it may be changed unexpectedly
279 if (theAttr->id() == "ConstraintFlyoutValuePnt")
281 AttributePoint2DPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttr);
282 double aValues[2] = {anAttr->x(), anAttr->y()};
283 dumpArray(aResult, aValues, 2);
285 aResult<<"__unknownattribute__";
287 return aResult.str();
290 std::string ModelHighAPI_FeatureStore::dumpShape(std::shared_ptr<GeomAPI_Shape>& theShape) {
291 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
292 if (aShape.IsNull()) {
295 std::ostringstream aResult;
296 // output the number of shapes of different types
297 TopAbs_ShapeEnum aType = TopAbs_COMPOUND;
298 for(; aType <= TopAbs_VERTEX; aType = TopAbs_ShapeEnum((int)aType + 1)) {
299 TopExp_Explorer anExp(aShape, aType);
301 for(; anExp.More(); anExp.Next()) aCount++;
302 TopAbs::Print(aType, aResult);
303 aResult<<": "<<aCount<<std::endl;
305 // output the main characteristics
306 if (GeomAlgoAPI_ShapeTools::volume(theShape) > 1.e-7) {
307 aResult<<"Volume: "<<
308 std::fixed<<setprecision(3)<<GeomAlgoAPI_ShapeTools::volume(theShape)<<std::endl;
310 std::shared_ptr<GeomAPI_Pnt> aCenter = GeomAlgoAPI_ShapeTools::centreOfMass(theShape);
311 aResult<<"Center of mass: ";
312 double aCenterVals[3] = {aCenter->x(), aCenter->y(), aCenter->z()};
313 dumpArray(aResult, aCenterVals, 3);
315 return aResult.str();