1 // Copyright (C) 2014-2019 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 email : webmaster.salome@opencascade.com
20 #include <ModelHighAPI_FeatureStore.h>
22 #include <ModelAPI_Tools.h>
23 #include <ModelAPI_ResultPart.h>
24 #include <ModelAPI_Session.h>
25 #include <ModelAPI_AttributeBoolean.h>
26 #include <ModelAPI_AttributeDocRef.h>
27 #include <ModelAPI_AttributeDouble.h>
28 #include <ModelAPI_AttributeIntArray.h>
29 #include <ModelAPI_AttributeInteger.h>
30 #include <ModelAPI_AttributeRefAttr.h>
31 #include <ModelAPI_AttributeRefAttrList.h>
32 #include <ModelAPI_AttributeReference.h>
33 #include <ModelAPI_AttributeRefList.h>
34 #include <ModelAPI_AttributeSelection.h>
35 #include <ModelAPI_AttributeSelectionList.h>
36 #include <ModelAPI_AttributeString.h>
37 #include <ModelAPI_AttributeStringArray.h>
38 #include <ModelAPI_AttributeDoubleArray.h>
39 #include <ModelAPI_AttributeTables.h>
40 #include <ModelAPI_Validator.h>
42 #include <GeomDataAPI_Dir.h>
43 #include <GeomDataAPI_Point.h>
44 #include <GeomDataAPI_Point2D.h>
45 #include <GeomAlgoAPI_ShapeTools.h>
46 #include <GeomAPI_Pnt.h>
48 #include <TopoDS_Shape.hxx>
49 #include <TopExp_Explorer.hxx>
55 #define TOLERANCE (1.e-7)
57 ModelHighAPI_FeatureStore::ModelHighAPI_FeatureStore(ObjectPtr theObject) {
58 storeData(theObject->data(), myAttrs);
60 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
62 // iterate results to store
63 std::list<ResultPtr> allResults;
64 ModelAPI_Tools::allResults(aFeature, allResults);
65 std::list<ResultPtr>::iterator aRes = allResults.begin();
66 for(; aRes != allResults.end(); aRes++) {
67 std::map<std::string, std::string> aResDump;
68 storeData((*aRes)->data(), aResDump);
69 myRes.push_back(aResDump);
74 std::string ModelHighAPI_FeatureStore::compare(ObjectPtr theObject) {
75 std::string anError = compareData(theObject->data(), myAttrs);
76 if (!anError.empty()) {
77 return "Features '" + theObject->data()->name() + "' differ:" + anError;
80 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
82 std::list<ResultPtr> allResults;
83 ModelAPI_Tools::allResults(aFeature, allResults);
84 std::list<ResultPtr>::iterator aRes = allResults.begin();
85 std::list<std::map<std::string, std::string> >::iterator aResIter = myRes.begin();
86 for(; aRes != allResults.end() && aResIter != myRes.end(); aRes++, aResIter++) {
87 anError = compareData((*aRes)->data(), *aResIter);
89 return "Results of feature '" + aFeature->name() + "' '" + (*aRes)->data()->name() +
90 "' differ:" + anError;
92 if (aRes != allResults.end()) {
93 return "Current model has more results '" + (*aRes)->data()->name() + "'";
95 if (aResIter != myRes.end()) {
96 return "Original model had more results '" + (*aResIter)["__name__"] + "'";
102 void ModelHighAPI_FeatureStore::storeData(std::shared_ptr<ModelAPI_Data> theData,
103 std::map<std::string, std::string>& theAttrs)
105 // store name to keep also this information and output if needed
106 theAttrs["__name__"] = theData->name();
107 std::list<std::shared_ptr<ModelAPI_Attribute> > allAttrs = theData->attributes("");
108 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = allAttrs.begin();
109 for(; anAttr != allAttrs.end(); anAttr++) {
110 theAttrs[(*anAttr)->id()] = dumpAttr(*anAttr);
112 ResultPtr aShapeOwner = std::dynamic_pointer_cast<ModelAPI_Result>(theData->owner());
113 if (aShapeOwner.get() && aShapeOwner->shape().get()) {
114 std::shared_ptr<GeomAPI_Shape> aShape = aShapeOwner->shape();
115 theAttrs["__shape__"] = dumpShape(aShape);
119 std::string ModelHighAPI_FeatureStore::compareData(std::shared_ptr<ModelAPI_Data> theData,
120 std::map<std::string, std::string>& theAttrs)
122 std::map<std::string, std::string> aThis;
123 storeData(theData, aThis);
124 std::map<std::string, std::string>::iterator aThisIter = aThis.begin();
125 for(; aThisIter != aThis.end(); aThisIter++) {
126 if (theAttrs.find(aThisIter->first) == theAttrs.end()) {
127 return "original model had no attribute '" + aThisIter->first + "'";
129 if (theAttrs[aThisIter->first] != aThisIter->second) {
130 return "attribute '" + aThisIter->first + "' is different (original != current) '" +
131 theAttrs[aThisIter->first] + "' != '" + aThisIter->second + "'";
134 // iterate back to find lack attribute in the current model
135 std::map<std::string, std::string>::iterator anOrigIter = theAttrs.begin();
136 for(; anOrigIter != theAttrs.end(); anOrigIter++) {
137 if (aThis.find(anOrigIter->first) == aThis.end()) {
138 return "current model had no attribute '" + anOrigIter->first + "'";
144 static void dumpArray(std::ostringstream& theOutput, const double theArray[],
145 int theSize, int thePrecision = PRECISION)
147 for (int i = 0; i < theSize; ++i) {
150 theOutput << std::fixed << setprecision(thePrecision)
151 << (fabs(theArray[i]) < TOLERANCE ? 0.0 : theArray[i]);
155 std::string ModelHighAPI_FeatureStore::dumpAttr(const AttributePtr& theAttr) {
156 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
157 FeaturePtr aFeatOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttr->owner());
158 if (aFeatOwner.get() && !aFactory->isCase(aFeatOwner, theAttr->id())) {
159 return "__notcase__";
161 std::string aType = theAttr->attributeType();
163 // do not check selection of the filter,
164 // because there is neither parametric update nor dump support yet.
165 FiltersFeaturePtr aFilter = std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(aFeatOwner);
166 if (aFilter && (aType == ModelAPI_AttributeSelection::typeId() ||
167 aType == ModelAPI_AttributeSelectionList::typeId()))
168 return "__filter_selection__";
170 std::ostringstream aResult;
171 if (!theAttr->isInitialized()) {
172 if (aType == ModelAPI_AttributeBoolean::typeId()) {
173 // special case for Boolean attribute (mean it false if not initialized)
175 return aResult.str();
176 } else if (aType == ModelAPI_AttributeString::typeId()) {
177 // special case for attribute "SolverError"
178 if (theAttr->id() == "SolverError" &&
179 std::dynamic_pointer_cast<ModelAPI_Feature>(theAttr->owner())->getKind() == "Sketch")
183 return "__notinitialized__";
185 if (aType == ModelAPI_AttributeDocRef::typeId()) {
186 AttributeDocRefPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDocRef>(theAttr);
187 DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
188 if (anAttr->value() != aDoc) {
189 ResultPtr aRes = ModelAPI_Tools::findPartResult(aDoc, anAttr->value());
191 aResult<<aRes->data()->name(); // Part result name (the same as saved file name)
194 aResult<<aDoc->kind(); // PartSet
196 } else if (aType == ModelAPI_AttributeInteger::typeId()) {
197 AttributeIntegerPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(theAttr);
198 // do not dump a type of ConstraintAngle, because it can be changed due dumping
199 if (anAttr->id() == "AngleType") {
202 if (anAttr->text().empty())
203 aResult<<anAttr->value();
205 aResult<<anAttr->text();
206 } else if (aType == ModelAPI_AttributeDouble::typeId()) {
207 AttributeDoublePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttr);
208 if (anAttr->id() == "ConstraintValue") {
209 // do not dump a value of constraint if it is ConstraintAngle,
210 // because this value depends on the angle type
211 FeaturePtr anOwner = ModelAPI_Feature::feature(anAttr->owner());
212 if (anOwner && anOwner->getKind() == "SketchConstraintAngle")
215 int aPrecision = PRECISION;
216 // Special case - precision for the arc angle. It is calculated with tolerance 1e-4,
217 // so the value has only 4 correct digits
218 if (anAttr->id() == "ArcAngle")
220 if (anAttr->text().empty()) {
221 double aVal = anAttr->value();
222 dumpArray(aResult, &aVal, 1, aPrecision);
224 aResult<<anAttr->text();
225 } else if (aType == ModelAPI_AttributeBoolean::typeId()) {
226 AttributeBooleanPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(theAttr);
227 // do not dump internal flags of ConstraintAngle
228 if (anAttr->id() == "AngleReversedLine1" || anAttr->id() == "AngleReversedLine2") {
231 aResult<<anAttr->value();
232 } else if (aType == ModelAPI_AttributeString::typeId()) {
233 AttributeStringPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttr);
234 // do not dump solver DOF for sketch as it may be changed unexpectedly
235 if(anAttr->id() == "SolverDOF") {
238 aResult<<anAttr->value();
239 } else if (aType == ModelAPI_AttributeReference::typeId()) {
240 AttributeReferencePtr anAttr =
241 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttr);
242 if (anAttr->value().get()) {
243 aResult<<anAttr->value()->data()->name();
245 aResult<<"__empty__";
247 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
248 AttributeSelectionPtr anAttr =
249 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttr);
250 if (anAttr->context().get())
251 aResult<<anAttr->namingName();
253 aResult<<"__notinitialized__";
254 } else if (aType == ModelAPI_AttributeSelectionList::typeId()) {
255 AttributeSelectionListPtr anAttr =
256 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttr);
257 for(int a = 0; a < anAttr->size(); a++) {
260 aResult<<anAttr->value(a)->namingName();
262 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
263 AttributeRefAttrPtr anAttr =
264 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttr);
265 ObjectPtr anObj = anAttr->isObject() ? anAttr->object() : anAttr->attr()->owner();
267 aResult<<anObj->data()->name();
268 if (!anAttr->isObject()) {
269 aResult<<" "<<anAttr->attr()->id();
272 aResult<<"__empty__";
274 } else if (aType == ModelAPI_AttributeRefList::typeId()) {
275 AttributeRefListPtr anAttr =
276 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttr);
277 // for sketch sub-features the empty values may be skipped and order is not important
278 bool isSketchFeatures = anAttr->id() == "Features" &&
279 std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner())->getKind() == "Sketch";
280 std::list<ObjectPtr> aList = anAttr->list();
281 std::list<std::string> aResList; // list of resulting strings
282 for(std::list<ObjectPtr>::iterator aL = aList.begin(); aL != aList.end(); aL++) {
284 aResList.push_back((*aL)->data()->name());
285 } else if (!isSketchFeatures) {
286 aResList.push_back("__empty__");
289 if (isSketchFeatures)
291 for(std::list<std::string>::iterator aR = aResList.begin(); aR != aResList.end(); aR++) {
294 } else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
295 AttributeRefAttrListPtr anAttr =
296 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttr);
297 std::list<std::pair<ObjectPtr, AttributePtr> > aList = anAttr->list();
298 std::list<std::pair<ObjectPtr, AttributePtr> >::iterator aL = aList.begin();
299 for(; aL != aList.end(); aL++) {
300 if (aL != aList.begin())
302 ObjectPtr anObj = aL->second.get() ? aL->second->owner() : aL->first;
304 aResult<<anObj->data()->name();
305 if (aL->second.get()) {
306 aResult<<" "<<aL->second->id();
309 aResult<<"__empty__";
312 } else if (aType == ModelAPI_AttributeIntArray::typeId()) {
313 AttributeIntArrayPtr anAttr =
314 std::dynamic_pointer_cast<ModelAPI_AttributeIntArray>(theAttr);
315 for(int a = 0; a < anAttr->size(); a++)
316 aResult<<anAttr->value(a)<<" ";
317 } else if (aType == ModelAPI_AttributeDoubleArray::typeId()) {
318 AttributeDoubleArrayPtr anAttr =
319 std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(theAttr);
320 for(int a = 0; a < anAttr->size(); a++)
321 aResult<<anAttr->value(a)<<" ";
322 } else if (aType == ModelAPI_AttributeStringArray::typeId()) {
323 AttributeStringArrayPtr anAttr =
324 std::dynamic_pointer_cast<ModelAPI_AttributeStringArray>(theAttr);
325 for(int a = 0; a < anAttr->size(); a++)
326 aResult<<"'"<<anAttr->value(a)<<"'"<<" ";
327 } else if (aType == ModelAPI_AttributeTables::typeId()) {
328 AttributeTablesPtr anAttr =
329 std::dynamic_pointer_cast<ModelAPI_AttributeTables>(theAttr);
330 aResult<<anAttr->tables()<<"x"<<anAttr->rows()<<"x"<<anAttr->columns()<<" ";
331 for(int aTab = 0; aTab < anAttr->tables(); aTab++) {
332 for(int aRow = 0; aRow < anAttr->rows(); aRow++) {
333 for( int aCol = 0; aCol < anAttr->columns(); aCol++) {
334 switch(anAttr->type()) {
335 case ModelAPI_AttributeTables::BOOLEAN:
336 aResult<<anAttr->value(aRow, aCol, aTab).myBool<<" ";
338 case ModelAPI_AttributeTables::INTEGER:
339 aResult<<anAttr->value(aRow, aCol, aTab).myInt<<" ";
341 case ModelAPI_AttributeTables::DOUBLE:
342 aResult<<anAttr->value(aRow, aCol, aTab).myDouble<<" ";
344 case ModelAPI_AttributeTables::STRING:
345 aResult<<"'"<<anAttr->value(aRow, aCol, aTab).myStr.c_str()<<"' ";
351 } else if (aType == GeomDataAPI_Point::typeId()) {
352 AttributePointPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttr);
353 double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()};
354 dumpArray(aResult, aValues, 3);
355 } else if (aType == GeomDataAPI_Dir::typeId()) {
356 AttributeDirPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theAttr);
357 double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()};
358 dumpArray(aResult, aValues, 3);
359 } else if (aType == GeomDataAPI_Point2D::typeId()) {
360 // do not dump flyout point for constraints as it may be changed unexpectedly
361 if (theAttr->id() == "ConstraintFlyoutValuePnt")
362 return "__notinitialized__";
363 AttributePoint2DPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttr);
364 double aValues[2] = {anAttr->x(), anAttr->y()};
365 dumpArray(aResult, aValues, 2);
367 aResult<<"__unknownattribute__";
369 return aResult.str();
372 std::string ModelHighAPI_FeatureStore::dumpShape(std::shared_ptr<GeomAPI_Shape>& theShape) {
373 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
374 if (aShape.IsNull()) {
377 std::ostringstream aResult;
378 // output the number of shapes of different types
379 TopAbs_ShapeEnum aType = TopAbs_COMPOUND;
380 for(; aType <= TopAbs_VERTEX; aType = TopAbs_ShapeEnum((int)aType + 1)) {
381 TopExp_Explorer anExp(aShape, aType);
383 for(; anExp.More(); anExp.Next()) aCount++;
384 TopAbs::Print(aType, aResult);
385 aResult<<": "<<aCount<<std::endl;
387 // output the main characteristics
388 if (GeomAlgoAPI_ShapeTools::volume(theShape) > 1.e-5) {
389 aResult<<"Volume: "<<
390 std::fixed<<setprecision(3)<<GeomAlgoAPI_ShapeTools::volume(theShape)<<std::endl;
392 std::shared_ptr<GeomAPI_Pnt> aCenter = GeomAlgoAPI_ShapeTools::centreOfMass(theShape);
393 aResult<<"Center of mass: ";
394 double aCenterVals[3] = {aCenter->x(), aCenter->y(), aCenter->z()};
395 dumpArray(aResult, aCenterVals, 3, 5);
397 return aResult.str();