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 aResult<<anAttr->value();
191 } else if (aType == ModelAPI_AttributeReference::typeId()) {
192 AttributeReferencePtr anAttr =
193 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttr);
194 if (anAttr->value().get()) {
195 aResult<<anAttr->value()->data()->name();
197 aResult<<"__empty__";
199 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
200 AttributeSelectionPtr anAttr =
201 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttr);
202 aResult<<anAttr->namingName();
203 } else if (aType == ModelAPI_AttributeSelectionList::typeId()) {
204 AttributeSelectionListPtr anAttr =
205 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttr);
206 for(int a = 0; a < anAttr->size(); a++) {
209 aResult<<anAttr->value(a)->namingName();
211 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
212 AttributeRefAttrPtr anAttr =
213 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttr);
214 ObjectPtr anObj = anAttr->isObject() ? anAttr->object() : anAttr->attr()->owner();
216 aResult<<anObj->data()->name();
217 if (!anAttr->isObject()) {
218 aResult<<" "<<anAttr->attr()->id();
221 aResult<<"__empty__";
223 } else if (aType == ModelAPI_AttributeRefList::typeId()) {
224 AttributeRefListPtr anAttr =
225 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttr);
226 // for sketch sub-features the empty values may be skipped and order is not important
227 bool isSketchFeatures = anAttr->id() == "Features" &&
228 std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner())->getKind() == "Sketch";
229 std::list<ObjectPtr> aList = anAttr->list();
230 std::list<std::string> aResList; // list of resulting strings
231 for(std::list<ObjectPtr>::iterator aL = aList.begin(); aL != aList.end(); aL++) {
233 aResList.push_back((*aL)->data()->name());
234 } else if (!isSketchFeatures) {
235 aResList.push_back("__empty__");
238 if (isSketchFeatures)
240 for(std::list<std::string>::iterator aR = aResList.begin(); aR != aResList.end(); aR++) {
243 } else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
244 AttributeRefAttrListPtr anAttr =
245 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttr);
246 std::list<std::pair<ObjectPtr, AttributePtr> > aList = anAttr->list();
247 std::list<std::pair<ObjectPtr, AttributePtr> >::iterator aL = aList.begin();
248 for(; aL != aList.end(); aL++) {
249 if (aL != aList.begin())
251 ObjectPtr anObj = aL->second.get() ? aL->second->owner() : aL->first;
253 aResult<<anObj->data()->name();
254 if (aL->second.get()) {
255 aResult<<" "<<aL->second->id();
258 aResult<<"__empty__";
261 } else if (aType == ModelAPI_AttributeIntArray::typeId()) {
262 AttributeIntArrayPtr anAttr =
263 std::dynamic_pointer_cast<ModelAPI_AttributeIntArray>(theAttr);
264 for(int a = 0; a < anAttr->size(); a++)
265 aResult<<anAttr->value(a)<<" ";
266 } else if (aType == ModelAPI_AttributeDoubleArray::typeId()) {
267 AttributeDoubleArrayPtr anAttr =
268 std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(theAttr);
269 for(int a = 0; a < anAttr->size(); a++)
270 aResult<<anAttr->value(a)<<" ";
271 } else if (aType == ModelAPI_AttributeStringArray::typeId()) {
272 AttributeStringArrayPtr anAttr =
273 std::dynamic_pointer_cast<ModelAPI_AttributeStringArray>(theAttr);
274 for(int a = 0; a < anAttr->size(); a++)
275 aResult<<"'"<<anAttr->value(a)<<"'"<<" ";
276 } else if (aType == ModelAPI_AttributeTables::typeId()) {
277 AttributeTablesPtr anAttr =
278 std::dynamic_pointer_cast<ModelAPI_AttributeTables>(theAttr);
279 aResult<<anAttr->tables()<<"x"<<anAttr->rows()<<"x"<<anAttr->columns()<<" ";
280 for(int aTab = 0; aTab < anAttr->tables(); aTab++) {
281 for(int aRow = 0; aRow < anAttr->rows(); aRow++) {
282 for( int aCol = 0; aCol < anAttr->columns(); aCol++) {
283 switch(anAttr->type()) {
284 case ModelAPI_AttributeTables::BOOLEAN:
285 aResult<<anAttr->value(aRow, aCol, aTab).myBool<<" ";
287 case ModelAPI_AttributeTables::INTEGER:
288 aResult<<anAttr->value(aRow, aCol, aTab).myInt<<" ";
290 case ModelAPI_AttributeTables::DOUBLE:
291 aResult<<anAttr->value(aRow, aCol, aTab).myDouble<<" ";
293 case ModelAPI_AttributeTables::STRING:
294 aResult<<"'"<<anAttr->value(aRow, aCol, aTab).myStr.c_str()<<"' ";
300 } else if (aType == GeomDataAPI_Point::typeId()) {
301 AttributePointPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttr);
302 double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()};
303 dumpArray(aResult, aValues, 3);
304 } else if (aType == GeomDataAPI_Dir::typeId()) {
305 AttributeDirPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theAttr);
306 double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()};
307 dumpArray(aResult, aValues, 3);
308 } else if (aType == GeomDataAPI_Point2D::typeId()) {
309 // do not dump flyout point for constraints as it may be changed unexpectedly
310 if (theAttr->id() == "ConstraintFlyoutValuePnt")
312 AttributePoint2DPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttr);
313 double aValues[2] = {anAttr->x(), anAttr->y()};
314 dumpArray(aResult, aValues, 2);
316 aResult<<"__unknownattribute__";
318 return aResult.str();
321 std::string ModelHighAPI_FeatureStore::dumpShape(std::shared_ptr<GeomAPI_Shape>& theShape) {
322 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
323 if (aShape.IsNull()) {
326 std::ostringstream aResult;
327 // output the number of shapes of different types
328 TopAbs_ShapeEnum aType = TopAbs_COMPOUND;
329 for(; aType <= TopAbs_VERTEX; aType = TopAbs_ShapeEnum((int)aType + 1)) {
330 TopExp_Explorer anExp(aShape, aType);
332 for(; anExp.More(); anExp.Next()) aCount++;
333 TopAbs::Print(aType, aResult);
334 aResult<<": "<<aCount<<std::endl;
336 // output the main characteristics
337 if (GeomAlgoAPI_ShapeTools::volume(theShape) > 1.e-7) {
338 aResult<<"Volume: "<<
339 std::fixed<<setprecision(3)<<GeomAlgoAPI_ShapeTools::volume(theShape)<<std::endl;
341 std::shared_ptr<GeomAPI_Pnt> aCenter = GeomAlgoAPI_ShapeTools::centreOfMass(theShape);
342 aResult<<"Center of mass: ";
343 double aCenterVals[3] = {aCenter->x(), aCenter->y(), aCenter->z()};
344 dumpArray(aResult, aCenterVals, 3);
346 return aResult.str();