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 if (!theAttr->isInitialized()) {
122 return "__notinitialized__";
124 std::string aType = theAttr->attributeType();
125 std::ostringstream aResult;
126 if (aType == ModelAPI_AttributeDocRef::typeId()) {
127 AttributeDocRefPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDocRef>(theAttr);
128 DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
129 if (anAttr->value() != aDoc) {
130 ResultPtr aRes = ModelAPI_Tools::findPartResult(aDoc, anAttr->value());
132 aResult<<aRes->data()->name(); // Part result name (the same as saved file name)
135 aResult<<aDoc->kind(); // PartSet
137 } else if (aType == ModelAPI_AttributeInteger::typeId()) {
138 AttributeIntegerPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(theAttr);
139 if (anAttr->text().empty())
140 aResult<<anAttr->value();
142 aResult<<anAttr->text();
143 } else if (aType == ModelAPI_AttributeDouble::typeId()) {
144 AttributeDoublePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttr);
145 if (anAttr->text().empty())
146 aResult<<anAttr->value();
148 aResult<<anAttr->text();
149 } else if (aType == ModelAPI_AttributeBoolean::typeId()) {
150 AttributeBooleanPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(theAttr);
151 aResult<<anAttr->value();
152 } else if (aType == ModelAPI_AttributeString::typeId()) {
153 AttributeStringPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttr);
154 aResult<<anAttr->value();
155 } else if (aType == ModelAPI_AttributeReference::typeId()) {
156 AttributeReferencePtr anAttr =
157 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttr);
158 if (anAttr->value().get()) {
159 aResult<<anAttr->value()->data()->name();
161 aResult<<"__empty__";
163 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
164 AttributeSelectionPtr anAttr =
165 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttr);
166 aResult<<anAttr->namingName();
167 } else if (aType == ModelAPI_AttributeSelectionList::typeId()) {
168 AttributeSelectionListPtr anAttr =
169 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttr);
170 for(int a = 0; a < anAttr->size(); a++) {
173 aResult<<anAttr->value(a)->namingName();
175 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
176 AttributeRefAttrPtr anAttr =
177 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttr);
178 ObjectPtr anObj = anAttr->isObject() ? anAttr->object() : anAttr->attr()->owner();
180 aResult<<anObj->data()->name();
181 if (!anAttr->isObject()) {
182 aResult<<" "<<anAttr->attr()->id();
185 aResult<<"__empty__";
187 } else if (aType == ModelAPI_AttributeRefList::typeId()) {
188 AttributeRefListPtr anAttr =
189 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttr);
190 std::list<ObjectPtr> aList = anAttr->list();
191 for(std::list<ObjectPtr>::iterator aL = aList.begin(); aL != aList.end(); aL++) {
192 if (aL != aList.begin())
195 aResult<<(*aL)->data()->name();
197 aResult<<"__empty__";
200 } else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
201 AttributeRefAttrListPtr anAttr =
202 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttr);
203 std::list<std::pair<ObjectPtr, AttributePtr> > aList = anAttr->list();
204 std::list<std::pair<ObjectPtr, AttributePtr> >::iterator aL = aList.begin();
205 for(; aL != aList.end(); aL++) {
206 if (aL != aList.begin())
208 ObjectPtr anObj = aL->second.get() ? aL->second->owner() : aL->first;
210 aResult<<anObj->data()->name();
211 if (aL->second.get()) {
212 aResult<<" "<<aL->second->id();
215 aResult<<"__empty__";
218 } else if (aType == ModelAPI_AttributeIntArray::typeId()) {
219 AttributeIntArrayPtr anAttr =
220 std::dynamic_pointer_cast<ModelAPI_AttributeIntArray>(theAttr);
221 for(int a = 0; a < anAttr->size(); a++)
222 aResult<<anAttr->value(a)<<" ";
223 } else if (aType == ModelAPI_AttributeDoubleArray::typeId()) {
224 AttributeDoubleArrayPtr anAttr =
225 std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(theAttr);
226 for(int a = 0; a < anAttr->size(); a++)
227 aResult<<anAttr->value(a)<<" ";
228 } else if (aType == GeomDataAPI_Point::typeId()) {
229 AttributePointPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttr);
230 aResult<<anAttr->x()<<" "<<anAttr->y()<<" "<<anAttr->z();
231 } else if (aType == GeomDataAPI_Dir::typeId()) {
232 AttributeDirPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theAttr);
233 aResult<<anAttr->x()<<" "<<anAttr->y()<<" "<<anAttr->z();
234 } else if (aType == GeomDataAPI_Point2D::typeId()) {
235 AttributePoint2DPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttr);
236 aResult<<anAttr->x()<<" "<<anAttr->y()<<" ";
238 aResult<<"__unknownattribute__";
240 return aResult.str();
243 std::string ModelHighAPI_FeatureStore::dumpShape(std::shared_ptr<GeomAPI_Shape>& theShape) {
244 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
245 if (aShape.IsNull()) {
248 std::ostringstream aResult;
249 // output the number of shapes of different types
250 TopAbs_ShapeEnum aType = TopAbs_COMPOUND;
251 for(; aType <= TopAbs_VERTEX; aType = TopAbs_ShapeEnum((int)aType + 1)) {
252 TopExp_Explorer anExp(aShape, aType);
254 for(; anExp.More(); anExp.Next()) aCount++;
255 TopAbs::Print(aType, aResult);
256 aResult<<": "<<aCount<<std::endl;
258 // output the main characteristics
259 aResult<<"Volume: "<<setprecision(2)<<GeomAlgoAPI_ShapeTools::volume(theShape)<<std::endl;
260 std::shared_ptr<GeomAPI_Pnt> aCenter = GeomAlgoAPI_ShapeTools::centreOfMass(theShape);
261 aResult<<"Center of mass: "<<std::fixed<<setprecision(7)
262 <<aCenter->x()<<" "<<aCenter->y()<<" "<<aCenter->z()<<std::endl;
263 return aResult.str();