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_AttributeStringArray.h>
25 #include <ModelAPI_AttributeDoubleArray.h>
26 #include <ModelAPI_AttributeTables.h>
27 #include <ModelAPI_Validator.h>
29 #include <GeomDataAPI_Dir.h>
30 #include <GeomDataAPI_Point.h>
31 #include <GeomDataAPI_Point2D.h>
32 #include <GeomAlgoAPI_ShapeTools.h>
33 #include <GeomAPI_Pnt.h>
35 #include <TopoDS_Shape.hxx>
36 #include <TopExp_Explorer.hxx>
42 #define TOLERANCE (1.e-7)
44 ModelHighAPI_FeatureStore::ModelHighAPI_FeatureStore(FeaturePtr theFeature) {
45 storeData(theFeature->data(), myAttrs);
46 // iterate results to store
47 std::list<ResultPtr> allResults;
48 ModelAPI_Tools::allResults(theFeature, allResults);
49 std::list<ResultPtr>::iterator aRes = allResults.begin();
50 for(; aRes != allResults.end(); aRes++) {
51 std::map<std::string, std::string> aResDump;
52 storeData((*aRes)->data(), aResDump);
53 myRes.push_back(aResDump);
57 std::string ModelHighAPI_FeatureStore::compare(FeaturePtr theFeature) {
58 std::string anError = compareData(theFeature->data(), myAttrs);
59 if (!anError.empty()) {
60 return "Features '" + theFeature->name() + "' differ:" + anError;
62 std::list<ResultPtr> allResults;
63 ModelAPI_Tools::allResults(theFeature, allResults);
64 std::list<ResultPtr>::iterator aRes = allResults.begin();
65 std::list<std::map<std::string, std::string> >::iterator aResIter = myRes.begin();
66 for(; aRes != allResults.end() && aResIter != myRes.end(); aRes++, aResIter++) {
67 anError = compareData((*aRes)->data(), *aResIter);
69 return "Results of feature '" + theFeature->name() + "' '" + (*aRes)->data()->name() +
70 "' differ:" + anError;
72 if (aRes != allResults.end()) {
73 return "Current model has more results '" + (*aRes)->data()->name() + "'";
75 if (aResIter != myRes.end()) {
76 return "Original model had more results '" + (*aResIter)["__name__"] + "'";
81 void ModelHighAPI_FeatureStore::storeData(std::shared_ptr<ModelAPI_Data> theData,
82 std::map<std::string, std::string>& theAttrs)
84 // store name to keep also this information and output if needed
85 theAttrs["__name__"] = theData->name();
86 std::list<std::shared_ptr<ModelAPI_Attribute> > allAttrs = theData->attributes("");
87 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = allAttrs.begin();
88 for(; anAttr != allAttrs.end(); anAttr++) {
89 theAttrs[(*anAttr)->id()] = dumpAttr(*anAttr);
91 ResultPtr aShapeOwner = std::dynamic_pointer_cast<ModelAPI_Result>(theData->owner());
92 if (aShapeOwner.get() && aShapeOwner->shape().get()) {
93 std::shared_ptr<GeomAPI_Shape> aShape = aShapeOwner->shape();
94 theAttrs["__shape__"] = dumpShape(aShape);
98 std::string ModelHighAPI_FeatureStore::compareData(std::shared_ptr<ModelAPI_Data> theData,
99 std::map<std::string, std::string>& theAttrs)
101 std::map<std::string, std::string> aThis;
102 storeData(theData, aThis);
103 std::map<std::string, std::string>::iterator aThisIter = aThis.begin();
104 for(; aThisIter != aThis.end(); aThisIter++) {
105 if (theAttrs.find(aThisIter->first) == theAttrs.end()) {
106 return "original model had no attribute '" + aThisIter->first + "'";
108 if (theAttrs[aThisIter->first] != aThisIter->second) {
109 return "attribute '" + aThisIter->first + "' is different (original != current) '" +
110 theAttrs[aThisIter->first] + "' != '" + aThisIter->second + "'";
113 // iterate back to find lack attribute in the current model
114 std::map<std::string, std::string>::iterator anOrigIter = theAttrs.begin();
115 for(; anOrigIter != theAttrs.end(); anOrigIter++) {
116 if (aThis.find(anOrigIter->first) == aThis.end()) {
117 return "current model had no attribute '" + anOrigIter->first + "'";
123 static void dumpArray(std::ostringstream& theOutput, const double theArray[],
124 int theSize, int thePrecision = PRECISION)
126 for (int i = 0; i < theSize; ++i) {
129 theOutput << std::fixed << setprecision(thePrecision)
130 << (fabs(theArray[i]) < TOLERANCE ? 0.0 : theArray[i]);
134 std::string ModelHighAPI_FeatureStore::dumpAttr(const AttributePtr& theAttr) {
135 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
136 FeaturePtr aFeatOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttr->owner());
137 if (aFeatOwner.get() && !aFactory->isCase(aFeatOwner, theAttr->id())) {
138 return "__notcase__";
140 std::string aType = theAttr->attributeType();
141 std::ostringstream aResult;
142 if (!theAttr->isInitialized()) {
143 if (aType == ModelAPI_AttributeBoolean::typeId()) {
144 // special case for Boolean attribute (mean it false if not initialized)
146 return aResult.str();
147 } else if (aType == ModelAPI_AttributeString::typeId()) {
148 // special case for attribute "SolverError"
149 if (theAttr->id() == "SolverError" &&
150 std::dynamic_pointer_cast<ModelAPI_Feature>(theAttr->owner())->getKind() == "Sketch")
154 return "__notinitialized__";
156 if (aType == ModelAPI_AttributeDocRef::typeId()) {
157 AttributeDocRefPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDocRef>(theAttr);
158 DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
159 if (anAttr->value() != aDoc) {
160 ResultPtr aRes = ModelAPI_Tools::findPartResult(aDoc, anAttr->value());
162 aResult<<aRes->data()->name(); // Part result name (the same as saved file name)
165 aResult<<aDoc->kind(); // PartSet
167 } else if (aType == ModelAPI_AttributeInteger::typeId()) {
168 AttributeIntegerPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(theAttr);
169 if (anAttr->text().empty())
170 aResult<<anAttr->value();
172 aResult<<anAttr->text();
173 } else if (aType == ModelAPI_AttributeDouble::typeId()) {
174 AttributeDoublePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttr);
175 int aPrecision = PRECISION;
176 // Special case - precision for the arc angle. It is calculated with tolerance 1e-4,
177 // so the value has only 4 correct digits
178 if (anAttr->id() == "ArcAngle")
180 if (anAttr->text().empty()) {
181 double aVal = anAttr->value();
182 dumpArray(aResult, &aVal, 1, aPrecision);
184 aResult<<anAttr->text();
185 } else if (aType == ModelAPI_AttributeBoolean::typeId()) {
186 AttributeBooleanPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(theAttr);
187 aResult<<anAttr->value();
188 } else if (aType == ModelAPI_AttributeString::typeId()) {
189 AttributeStringPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttr);
190 // do not dump solver DOF for sketch as it may be changed unexpectedly
191 if(anAttr->id() == "SolverDOF") {
194 aResult<<anAttr->value();
195 } else if (aType == ModelAPI_AttributeReference::typeId()) {
196 AttributeReferencePtr anAttr =
197 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttr);
198 if (anAttr->value().get()) {
199 aResult<<anAttr->value()->data()->name();
201 aResult<<"__empty__";
203 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
204 AttributeSelectionPtr anAttr =
205 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttr);
206 aResult<<anAttr->namingName();
207 } else if (aType == ModelAPI_AttributeSelectionList::typeId()) {
208 AttributeSelectionListPtr anAttr =
209 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttr);
210 for(int a = 0; a < anAttr->size(); a++) {
213 aResult<<anAttr->value(a)->namingName();
215 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
216 AttributeRefAttrPtr anAttr =
217 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttr);
218 ObjectPtr anObj = anAttr->isObject() ? anAttr->object() : anAttr->attr()->owner();
220 aResult<<anObj->data()->name();
221 if (!anAttr->isObject()) {
222 aResult<<" "<<anAttr->attr()->id();
225 aResult<<"__empty__";
227 } else if (aType == ModelAPI_AttributeRefList::typeId()) {
228 AttributeRefListPtr anAttr =
229 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttr);
230 // for sketch sub-features the empty values may be skipped and order is not important
231 bool isSketchFeatures = anAttr->id() == "Features" &&
232 std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner())->getKind() == "Sketch";
233 std::list<ObjectPtr> aList = anAttr->list();
234 std::list<std::string> aResList; // list of resulting strings
235 for(std::list<ObjectPtr>::iterator aL = aList.begin(); aL != aList.end(); aL++) {
237 aResList.push_back((*aL)->data()->name());
238 } else if (!isSketchFeatures) {
239 aResList.push_back("__empty__");
242 if (isSketchFeatures)
244 for(std::list<std::string>::iterator aR = aResList.begin(); aR != aResList.end(); aR++) {
247 } else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
248 AttributeRefAttrListPtr anAttr =
249 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttr);
250 std::list<std::pair<ObjectPtr, AttributePtr> > aList = anAttr->list();
251 std::list<std::pair<ObjectPtr, AttributePtr> >::iterator aL = aList.begin();
252 for(; aL != aList.end(); aL++) {
253 if (aL != aList.begin())
255 ObjectPtr anObj = aL->second.get() ? aL->second->owner() : aL->first;
257 aResult<<anObj->data()->name();
258 if (aL->second.get()) {
259 aResult<<" "<<aL->second->id();
262 aResult<<"__empty__";
265 } else if (aType == ModelAPI_AttributeIntArray::typeId()) {
266 AttributeIntArrayPtr anAttr =
267 std::dynamic_pointer_cast<ModelAPI_AttributeIntArray>(theAttr);
268 for(int a = 0; a < anAttr->size(); a++)
269 aResult<<anAttr->value(a)<<" ";
270 } else if (aType == ModelAPI_AttributeDoubleArray::typeId()) {
271 AttributeDoubleArrayPtr anAttr =
272 std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(theAttr);
273 for(int a = 0; a < anAttr->size(); a++)
274 aResult<<anAttr->value(a)<<" ";
275 } else if (aType == ModelAPI_AttributeStringArray::typeId()) {
276 AttributeStringArrayPtr anAttr =
277 std::dynamic_pointer_cast<ModelAPI_AttributeStringArray>(theAttr);
278 for(int a = 0; a < anAttr->size(); a++)
279 aResult<<"'"<<anAttr->value(a)<<"'"<<" ";
280 } else if (aType == ModelAPI_AttributeTables::typeId()) {
281 AttributeTablesPtr anAttr =
282 std::dynamic_pointer_cast<ModelAPI_AttributeTables>(theAttr);
283 aResult<<anAttr->tables()<<"x"<<anAttr->rows()<<"x"<<anAttr->columns()<<" ";
284 for(int aTab = 0; aTab < anAttr->tables(); aTab++) {
285 for(int aRow = 0; aRow < anAttr->rows(); aRow++) {
286 for( int aCol = 0; aCol < anAttr->columns(); aCol++) {
287 switch(anAttr->type()) {
288 case ModelAPI_AttributeTables::BOOLEAN:
289 aResult<<anAttr->value(aRow, aCol, aTab).myBool<<" ";
291 case ModelAPI_AttributeTables::INTEGER:
292 aResult<<anAttr->value(aRow, aCol, aTab).myInt<<" ";
294 case ModelAPI_AttributeTables::DOUBLE:
295 aResult<<anAttr->value(aRow, aCol, aTab).myDouble<<" ";
297 case ModelAPI_AttributeTables::STRING:
298 aResult<<"'"<<anAttr->value(aRow, aCol, aTab).myStr.c_str()<<"' ";
304 } else if (aType == GeomDataAPI_Point::typeId()) {
305 AttributePointPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttr);
306 double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()};
307 dumpArray(aResult, aValues, 3);
308 } else if (aType == GeomDataAPI_Dir::typeId()) {
309 AttributeDirPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theAttr);
310 double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()};
311 dumpArray(aResult, aValues, 3);
312 } else if (aType == GeomDataAPI_Point2D::typeId()) {
313 // do not dump flyout point for constraints as it may be changed unexpectedly
314 if (theAttr->id() == "ConstraintFlyoutValuePnt")
316 AttributePoint2DPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttr);
317 double aValues[2] = {anAttr->x(), anAttr->y()};
318 dumpArray(aResult, aValues, 2);
320 aResult<<"__unknownattribute__";
322 return aResult.str();
325 std::string ModelHighAPI_FeatureStore::dumpShape(std::shared_ptr<GeomAPI_Shape>& theShape) {
326 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
327 if (aShape.IsNull()) {
330 std::ostringstream aResult;
331 // output the number of shapes of different types
332 TopAbs_ShapeEnum aType = TopAbs_COMPOUND;
333 for(; aType <= TopAbs_VERTEX; aType = TopAbs_ShapeEnum((int)aType + 1)) {
334 TopExp_Explorer anExp(aShape, aType);
336 for(; anExp.More(); anExp.Next()) aCount++;
337 TopAbs::Print(aType, aResult);
338 aResult<<": "<<aCount<<std::endl;
340 // output the main characteristics
341 if (GeomAlgoAPI_ShapeTools::volume(theShape) > 1.e-7) {
342 aResult<<"Volume: "<<
343 std::fixed<<setprecision(3)<<GeomAlgoAPI_ShapeTools::volume(theShape)<<std::endl;
345 std::shared_ptr<GeomAPI_Pnt> aCenter = GeomAlgoAPI_ShapeTools::centreOfMass(theShape);
346 aResult<<"Center of mass: ";
347 double aCenterVals[3] = {aCenter->x(), aCenter->y(), aCenter->z()};
348 dumpArray(aResult, aCenterVals, 3);
350 return aResult.str();