1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <ModelHighAPI_FeatureStore.h>
23 #include <ModelAPI_Tools.h>
24 #include <ModelAPI_ResultPart.h>
25 #include <ModelAPI_Session.h>
26 #include <ModelAPI_AttributeBoolean.h>
27 #include <ModelAPI_AttributeDocRef.h>
28 #include <ModelAPI_AttributeDouble.h>
29 #include <ModelAPI_AttributeIntArray.h>
30 #include <ModelAPI_AttributeInteger.h>
31 #include <ModelAPI_AttributeRefAttr.h>
32 #include <ModelAPI_AttributeRefAttrList.h>
33 #include <ModelAPI_AttributeReference.h>
34 #include <ModelAPI_AttributeRefList.h>
35 #include <ModelAPI_AttributeSelection.h>
36 #include <ModelAPI_AttributeSelectionList.h>
37 #include <ModelAPI_AttributeString.h>
38 #include <ModelAPI_AttributeStringArray.h>
39 #include <ModelAPI_AttributeDoubleArray.h>
40 #include <ModelAPI_AttributeTables.h>
41 #include <ModelAPI_Validator.h>
43 #include <GeomDataAPI_Dir.h>
44 #include <GeomDataAPI_Point.h>
45 #include <GeomDataAPI_Point2D.h>
46 #include <GeomAlgoAPI_ShapeTools.h>
47 #include <GeomAPI_Pnt.h>
49 #include <TopoDS_Shape.hxx>
50 #include <TopExp_Explorer.hxx>
56 #define TOLERANCE (1.e-7)
58 ModelHighAPI_FeatureStore::ModelHighAPI_FeatureStore(FeaturePtr theFeature) {
59 storeData(theFeature->data(), myAttrs);
60 // iterate results to store
61 std::list<ResultPtr> allResults;
62 ModelAPI_Tools::allResults(theFeature, allResults);
63 std::list<ResultPtr>::iterator aRes = allResults.begin();
64 for(; aRes != allResults.end(); aRes++) {
65 std::map<std::string, std::string> aResDump;
66 storeData((*aRes)->data(), aResDump);
67 myRes.push_back(aResDump);
71 std::string ModelHighAPI_FeatureStore::compare(FeaturePtr theFeature) {
72 std::string anError = compareData(theFeature->data(), myAttrs);
73 if (!anError.empty()) {
74 return "Features '" + theFeature->name() + "' differ:" + anError;
76 std::list<ResultPtr> allResults;
77 ModelAPI_Tools::allResults(theFeature, allResults);
78 std::list<ResultPtr>::iterator aRes = allResults.begin();
79 std::list<std::map<std::string, std::string> >::iterator aResIter = myRes.begin();
80 for(; aRes != allResults.end() && aResIter != myRes.end(); aRes++, aResIter++) {
81 anError = compareData((*aRes)->data(), *aResIter);
83 return "Results of feature '" + theFeature->name() + "' '" + (*aRes)->data()->name() +
84 "' differ:" + anError;
86 if (aRes != allResults.end()) {
87 return "Current model has more results '" + (*aRes)->data()->name() + "'";
89 if (aResIter != myRes.end()) {
90 return "Original model had more results '" + (*aResIter)["__name__"] + "'";
95 void ModelHighAPI_FeatureStore::storeData(std::shared_ptr<ModelAPI_Data> theData,
96 std::map<std::string, std::string>& theAttrs)
98 // store name to keep also this information and output if needed
99 theAttrs["__name__"] = theData->name();
100 std::list<std::shared_ptr<ModelAPI_Attribute> > allAttrs = theData->attributes("");
101 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = allAttrs.begin();
102 for(; anAttr != allAttrs.end(); anAttr++) {
103 theAttrs[(*anAttr)->id()] = dumpAttr(*anAttr);
105 ResultPtr aShapeOwner = std::dynamic_pointer_cast<ModelAPI_Result>(theData->owner());
106 if (aShapeOwner.get() && aShapeOwner->shape().get()) {
107 std::shared_ptr<GeomAPI_Shape> aShape = aShapeOwner->shape();
108 theAttrs["__shape__"] = dumpShape(aShape);
112 std::string ModelHighAPI_FeatureStore::compareData(std::shared_ptr<ModelAPI_Data> theData,
113 std::map<std::string, std::string>& theAttrs)
115 std::map<std::string, std::string> aThis;
116 storeData(theData, aThis);
117 std::map<std::string, std::string>::iterator aThisIter = aThis.begin();
118 for(; aThisIter != aThis.end(); aThisIter++) {
119 if (theAttrs.find(aThisIter->first) == theAttrs.end()) {
120 return "original model had no attribute '" + aThisIter->first + "'";
122 if (theAttrs[aThisIter->first] != aThisIter->second) {
123 return "attribute '" + aThisIter->first + "' is different (original != current) '" +
124 theAttrs[aThisIter->first] + "' != '" + aThisIter->second + "'";
127 // iterate back to find lack attribute in the current model
128 std::map<std::string, std::string>::iterator anOrigIter = theAttrs.begin();
129 for(; anOrigIter != theAttrs.end(); anOrigIter++) {
130 if (aThis.find(anOrigIter->first) == aThis.end()) {
131 return "current model had no attribute '" + anOrigIter->first + "'";
137 static void dumpArray(std::ostringstream& theOutput, const double theArray[],
138 int theSize, int thePrecision = PRECISION)
140 for (int i = 0; i < theSize; ++i) {
143 theOutput << std::fixed << setprecision(thePrecision)
144 << (fabs(theArray[i]) < TOLERANCE ? 0.0 : theArray[i]);
148 std::string ModelHighAPI_FeatureStore::dumpAttr(const AttributePtr& theAttr) {
149 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
150 FeaturePtr aFeatOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttr->owner());
151 if (aFeatOwner.get() && !aFactory->isCase(aFeatOwner, theAttr->id())) {
152 return "__notcase__";
154 std::string aType = theAttr->attributeType();
155 std::ostringstream aResult;
156 if (!theAttr->isInitialized()) {
157 if (aType == ModelAPI_AttributeBoolean::typeId()) {
158 // special case for Boolean attribute (mean it false if not initialized)
160 return aResult.str();
161 } else if (aType == ModelAPI_AttributeString::typeId()) {
162 // special case for attribute "SolverError"
163 if (theAttr->id() == "SolverError" &&
164 std::dynamic_pointer_cast<ModelAPI_Feature>(theAttr->owner())->getKind() == "Sketch")
168 return "__notinitialized__";
170 if (aType == ModelAPI_AttributeDocRef::typeId()) {
171 AttributeDocRefPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDocRef>(theAttr);
172 DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
173 if (anAttr->value() != aDoc) {
174 ResultPtr aRes = ModelAPI_Tools::findPartResult(aDoc, anAttr->value());
176 aResult<<aRes->data()->name(); // Part result name (the same as saved file name)
179 aResult<<aDoc->kind(); // PartSet
181 } else if (aType == ModelAPI_AttributeInteger::typeId()) {
182 AttributeIntegerPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(theAttr);
183 if (anAttr->text().empty())
184 aResult<<anAttr->value();
186 aResult<<anAttr->text();
187 } else if (aType == ModelAPI_AttributeDouble::typeId()) {
188 AttributeDoublePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttr);
189 int aPrecision = PRECISION;
190 // Special case - precision for the arc angle. It is calculated with tolerance 1e-4,
191 // so the value has only 4 correct digits
192 if (anAttr->id() == "ArcAngle")
194 if (anAttr->text().empty()) {
195 double aVal = anAttr->value();
196 dumpArray(aResult, &aVal, 1, aPrecision);
198 aResult<<anAttr->text();
199 } else if (aType == ModelAPI_AttributeBoolean::typeId()) {
200 AttributeBooleanPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(theAttr);
201 aResult<<anAttr->value();
202 } else if (aType == ModelAPI_AttributeString::typeId()) {
203 AttributeStringPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttr);
204 // do not dump solver DOF for sketch as it may be changed unexpectedly
205 if(anAttr->id() == "SolverDOF") {
208 aResult<<anAttr->value();
209 } else if (aType == ModelAPI_AttributeReference::typeId()) {
210 AttributeReferencePtr anAttr =
211 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttr);
212 if (anAttr->value().get()) {
213 aResult<<anAttr->value()->data()->name();
215 aResult<<"__empty__";
217 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
218 AttributeSelectionPtr anAttr =
219 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttr);
220 aResult<<anAttr->namingName();
221 } else if (aType == ModelAPI_AttributeSelectionList::typeId()) {
222 AttributeSelectionListPtr anAttr =
223 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttr);
224 for(int a = 0; a < anAttr->size(); a++) {
227 aResult<<anAttr->value(a)->namingName();
229 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
230 AttributeRefAttrPtr anAttr =
231 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttr);
232 ObjectPtr anObj = anAttr->isObject() ? anAttr->object() : anAttr->attr()->owner();
234 aResult<<anObj->data()->name();
235 if (!anAttr->isObject()) {
236 aResult<<" "<<anAttr->attr()->id();
239 aResult<<"__empty__";
241 } else if (aType == ModelAPI_AttributeRefList::typeId()) {
242 AttributeRefListPtr anAttr =
243 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttr);
244 // for sketch sub-features the empty values may be skipped and order is not important
245 bool isSketchFeatures = anAttr->id() == "Features" &&
246 std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner())->getKind() == "Sketch";
247 std::list<ObjectPtr> aList = anAttr->list();
248 std::list<std::string> aResList; // list of resulting strings
249 for(std::list<ObjectPtr>::iterator aL = aList.begin(); aL != aList.end(); aL++) {
251 aResList.push_back((*aL)->data()->name());
252 } else if (!isSketchFeatures) {
253 aResList.push_back("__empty__");
256 if (isSketchFeatures)
258 for(std::list<std::string>::iterator aR = aResList.begin(); aR != aResList.end(); aR++) {
261 } else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
262 AttributeRefAttrListPtr anAttr =
263 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttr);
264 std::list<std::pair<ObjectPtr, AttributePtr> > aList = anAttr->list();
265 std::list<std::pair<ObjectPtr, AttributePtr> >::iterator aL = aList.begin();
266 for(; aL != aList.end(); aL++) {
267 if (aL != aList.begin())
269 ObjectPtr anObj = aL->second.get() ? aL->second->owner() : aL->first;
271 aResult<<anObj->data()->name();
272 if (aL->second.get()) {
273 aResult<<" "<<aL->second->id();
276 aResult<<"__empty__";
279 } else if (aType == ModelAPI_AttributeIntArray::typeId()) {
280 AttributeIntArrayPtr anAttr =
281 std::dynamic_pointer_cast<ModelAPI_AttributeIntArray>(theAttr);
282 for(int a = 0; a < anAttr->size(); a++)
283 aResult<<anAttr->value(a)<<" ";
284 } else if (aType == ModelAPI_AttributeDoubleArray::typeId()) {
285 AttributeDoubleArrayPtr anAttr =
286 std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(theAttr);
287 for(int a = 0; a < anAttr->size(); a++)
288 aResult<<anAttr->value(a)<<" ";
289 } else if (aType == ModelAPI_AttributeStringArray::typeId()) {
290 AttributeStringArrayPtr anAttr =
291 std::dynamic_pointer_cast<ModelAPI_AttributeStringArray>(theAttr);
292 for(int a = 0; a < anAttr->size(); a++)
293 aResult<<"'"<<anAttr->value(a)<<"'"<<" ";
294 } else if (aType == ModelAPI_AttributeTables::typeId()) {
295 AttributeTablesPtr anAttr =
296 std::dynamic_pointer_cast<ModelAPI_AttributeTables>(theAttr);
297 aResult<<anAttr->tables()<<"x"<<anAttr->rows()<<"x"<<anAttr->columns()<<" ";
298 for(int aTab = 0; aTab < anAttr->tables(); aTab++) {
299 for(int aRow = 0; aRow < anAttr->rows(); aRow++) {
300 for( int aCol = 0; aCol < anAttr->columns(); aCol++) {
301 switch(anAttr->type()) {
302 case ModelAPI_AttributeTables::BOOLEAN:
303 aResult<<anAttr->value(aRow, aCol, aTab).myBool<<" ";
305 case ModelAPI_AttributeTables::INTEGER:
306 aResult<<anAttr->value(aRow, aCol, aTab).myInt<<" ";
308 case ModelAPI_AttributeTables::DOUBLE:
309 aResult<<anAttr->value(aRow, aCol, aTab).myDouble<<" ";
311 case ModelAPI_AttributeTables::STRING:
312 aResult<<"'"<<anAttr->value(aRow, aCol, aTab).myStr.c_str()<<"' ";
318 } else if (aType == GeomDataAPI_Point::typeId()) {
319 AttributePointPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttr);
320 double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()};
321 dumpArray(aResult, aValues, 3);
322 } else if (aType == GeomDataAPI_Dir::typeId()) {
323 AttributeDirPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theAttr);
324 double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()};
325 dumpArray(aResult, aValues, 3);
326 } else if (aType == GeomDataAPI_Point2D::typeId()) {
327 // do not dump flyout point for constraints as it may be changed unexpectedly
328 if (theAttr->id() == "ConstraintFlyoutValuePnt")
330 AttributePoint2DPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttr);
331 double aValues[2] = {anAttr->x(), anAttr->y()};
332 dumpArray(aResult, aValues, 2);
334 aResult<<"__unknownattribute__";
336 return aResult.str();
339 std::string ModelHighAPI_FeatureStore::dumpShape(std::shared_ptr<GeomAPI_Shape>& theShape) {
340 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
341 if (aShape.IsNull()) {
344 std::ostringstream aResult;
345 // output the number of shapes of different types
346 TopAbs_ShapeEnum aType = TopAbs_COMPOUND;
347 for(; aType <= TopAbs_VERTEX; aType = TopAbs_ShapeEnum((int)aType + 1)) {
348 TopExp_Explorer anExp(aShape, aType);
350 for(; anExp.More(); anExp.Next()) aCount++;
351 TopAbs::Print(aType, aResult);
352 aResult<<": "<<aCount<<std::endl;
354 // output the main characteristics
355 if (GeomAlgoAPI_ShapeTools::volume(theShape) > 1.e-7) {
356 aResult<<"Volume: "<<
357 std::fixed<<setprecision(3)<<GeomAlgoAPI_ShapeTools::volume(theShape)<<std::endl;
359 std::shared_ptr<GeomAPI_Pnt> aCenter = GeomAlgoAPI_ShapeTools::centreOfMass(theShape);
360 aResult<<"Center of mass: ";
361 double aCenterVals[3] = {aCenter->x(), aCenter->y(), aCenter->z()};
362 dumpArray(aResult, aCenterVals, 3, 5);
364 return aResult.str();