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 if (isSketchFeatures) {
285 // do not control construction features of an ellipse and other
286 FeaturePtr aFeature = ModelAPI_Feature::feature(*aL);
287 if (aFeature->getKind() == "SketchConstraintCoincidenceInternal")
288 continue; // skip internal constraints
290 aResList.push_back((*aL)->data()->name());
291 } else if (!isSketchFeatures) {
292 aResList.push_back("__empty__");
295 if (isSketchFeatures)
297 for(std::list<std::string>::iterator aR = aResList.begin(); aR != aResList.end(); aR++) {
300 } else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
301 AttributeRefAttrListPtr anAttr =
302 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttr);
303 std::list<std::pair<ObjectPtr, AttributePtr> > aList = anAttr->list();
304 std::list<std::pair<ObjectPtr, AttributePtr> >::iterator aL = aList.begin();
305 for(; aL != aList.end(); aL++) {
306 if (aL != aList.begin())
308 ObjectPtr anObj = aL->second.get() ? aL->second->owner() : aL->first;
310 aResult<<anObj->data()->name();
311 if (aL->second.get()) {
312 aResult<<" "<<aL->second->id();
315 aResult<<"__empty__";
318 } else if (aType == ModelAPI_AttributeIntArray::typeId()) {
319 AttributeIntArrayPtr anAttr =
320 std::dynamic_pointer_cast<ModelAPI_AttributeIntArray>(theAttr);
321 for(int a = 0; a < anAttr->size(); a++)
322 aResult<<anAttr->value(a)<<" ";
323 } else if (aType == ModelAPI_AttributeDoubleArray::typeId()) {
324 AttributeDoubleArrayPtr anAttr =
325 std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(theAttr);
326 for(int a = 0; a < anAttr->size(); a++)
327 aResult<<anAttr->value(a)<<" ";
328 } else if (aType == ModelAPI_AttributeStringArray::typeId()) {
329 AttributeStringArrayPtr anAttr =
330 std::dynamic_pointer_cast<ModelAPI_AttributeStringArray>(theAttr);
331 for(int a = 0; a < anAttr->size(); a++)
332 aResult<<"'"<<anAttr->value(a)<<"'"<<" ";
333 } else if (aType == ModelAPI_AttributeTables::typeId()) {
334 AttributeTablesPtr anAttr =
335 std::dynamic_pointer_cast<ModelAPI_AttributeTables>(theAttr);
336 aResult<<anAttr->tables()<<"x"<<anAttr->rows()<<"x"<<anAttr->columns()<<" ";
337 for(int aTab = 0; aTab < anAttr->tables(); aTab++) {
338 for(int aRow = 0; aRow < anAttr->rows(); aRow++) {
339 for( int aCol = 0; aCol < anAttr->columns(); aCol++) {
340 switch(anAttr->type()) {
341 case ModelAPI_AttributeTables::BOOLEAN:
342 aResult<<anAttr->value(aRow, aCol, aTab).myBool<<" ";
344 case ModelAPI_AttributeTables::INTEGER:
345 aResult<<anAttr->value(aRow, aCol, aTab).myInt<<" ";
347 case ModelAPI_AttributeTables::DOUBLE:
348 aResult<<anAttr->value(aRow, aCol, aTab).myDouble<<" ";
350 case ModelAPI_AttributeTables::STRING:
351 aResult<<"'"<<anAttr->value(aRow, aCol, aTab).myStr.c_str()<<"' ";
357 } else if (aType == GeomDataAPI_Point::typeId()) {
358 AttributePointPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttr);
359 double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()};
360 dumpArray(aResult, aValues, 3);
361 } else if (aType == GeomDataAPI_Dir::typeId()) {
362 AttributeDirPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theAttr);
363 double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()};
364 dumpArray(aResult, aValues, 3);
365 } else if (aType == GeomDataAPI_Point2D::typeId()) {
366 // do not dump flyout point for constraints as it may be changed unexpectedly
367 if (theAttr->id() == "ConstraintFlyoutValuePnt")
368 return "__notinitialized__";
369 AttributePoint2DPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttr);
370 double aValues[2] = {anAttr->x(), anAttr->y()};
371 dumpArray(aResult, aValues, 2);
373 aResult<<"__unknownattribute__";
375 return aResult.str();
378 std::string ModelHighAPI_FeatureStore::dumpShape(std::shared_ptr<GeomAPI_Shape>& theShape) {
379 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
380 if (aShape.IsNull()) {
383 std::ostringstream aResult;
384 // output the number of shapes of different types
385 TopAbs_ShapeEnum aType = TopAbs_COMPOUND;
386 for(; aType <= TopAbs_VERTEX; aType = TopAbs_ShapeEnum((int)aType + 1)) {
387 TopExp_Explorer anExp(aShape, aType);
389 for(; anExp.More(); anExp.Next()) aCount++;
390 TopAbs::Print(aType, aResult);
391 aResult<<": "<<aCount<<std::endl;
393 // output the main characteristics
394 if (GeomAlgoAPI_ShapeTools::volume(theShape) > 1.e-5) {
395 aResult<<"Volume: "<<
396 std::fixed<<setprecision(3)<<GeomAlgoAPI_ShapeTools::volume(theShape)<<std::endl;
398 std::shared_ptr<GeomAPI_Pnt> aCenter = GeomAlgoAPI_ShapeTools::centreOfMass(theShape);
399 aResult<<"Center of mass: ";
400 double aCenterVals[3] = {aCenter->x(), aCenter->y(), aCenter->z()};
401 dumpArray(aResult, aCenterVals, 3, 5);
403 return aResult.str();