1 // Copyright (C) 2014-2024 CEA, EDF
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_ResultConstruction.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_ShapeExplorer.h>
48 #include <GeomAPI_Pnt.h>
50 #include <Locale_Convert.h>
58 #define TOLERANCE (1.e-6)
60 ModelHighAPI_FeatureStore::ModelHighAPI_FeatureStore(ObjectPtr theObject) {
61 storeData(theObject->data(), myAttrs);
63 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
65 // iterate results to store
66 std::list<ResultPtr> allResults;
67 ModelAPI_Tools::allResults(aFeature, allResults);
68 std::list<ResultPtr>::iterator aRes = allResults.begin();
69 for(; aRes != allResults.end(); aRes++) {
70 std::map<std::string, std::string> aResDump;
71 storeData((*aRes)->data(), aResDump);
72 myRes.push_back(aResDump);
77 std::string ModelHighAPI_FeatureStore::compare(ObjectPtr theObject) {
78 std::string anError = compareData(theObject->data(), myAttrs);
79 if (!anError.empty()) {
80 return "Features '" + Locale::Convert::toString(theObject->data()->name()) +
81 "' differ:" + anError;
84 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
86 std::list<ResultPtr> allResults;
87 ModelAPI_Tools::allResults(aFeature, allResults);
88 std::list<ResultPtr>::iterator aRes = allResults.begin();
89 std::list<std::map<std::string, std::string> >::iterator aResIter = myRes.begin();
90 for(; aRes != allResults.end() && aResIter != myRes.end(); aRes++, aResIter++) {
91 anError = compareData((*aRes)->data(), *aResIter);
93 return "Results of feature '" + Locale::Convert::toString(aFeature->name()) +
94 "' '" + Locale::Convert::toString((*aRes)->data()->name()) +
95 "' differ:" + anError;
97 if (aRes != allResults.end()) {
98 return "Current model has more results '" +
99 Locale::Convert::toString((*aRes)->data()->name()) + "'";
101 if (aResIter != myRes.end()) {
102 return "Original model had more results '" + (*aResIter)["__name__"] + "'";
108 void ModelHighAPI_FeatureStore::storeData(std::shared_ptr<ModelAPI_Data> theData,
109 std::map<std::string, std::string>& theAttrs)
111 // store name to keep also this information and output if needed
112 theAttrs["__name__"] = Locale::Convert::toString(theData->name());
113 std::list<std::shared_ptr<ModelAPI_Attribute> > allAttrs = theData->attributes("");
114 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = allAttrs.begin();
115 for(; anAttr != allAttrs.end(); anAttr++) {
116 theAttrs[(*anAttr)->id()] = dumpAttr(*anAttr);
118 ResultPtr aShapeOwner = std::dynamic_pointer_cast<ModelAPI_Result>(theData->owner());
119 if (aShapeOwner.get() && aShapeOwner->shape().get()) {
120 std::shared_ptr<GeomAPI_Shape> aShape = aShapeOwner->shape();
121 theAttrs["__shape__"] = dumpShape(aShape);
125 std::string ModelHighAPI_FeatureStore::compareData(std::shared_ptr<ModelAPI_Data> theData,
126 std::map<std::string, std::string>& theAttrs)
128 std::map<std::string, std::string> aThis;
129 storeData(theData, aThis);
130 std::map<std::string, std::string>::iterator aThisIter = aThis.begin();
131 for(; aThisIter != aThis.end(); aThisIter++) {
132 if (theAttrs.find(aThisIter->first) == theAttrs.end()) {
133 return "original model had no attribute '" + aThisIter->first + "'";
135 if (theAttrs[aThisIter->first] != aThisIter->second) {
136 return "attribute '" + aThisIter->first + "' is different (original != current) '" +
137 theAttrs[aThisIter->first] + "' != '" + aThisIter->second + "'";
140 // iterate back to find lack attribute in the current model
141 std::map<std::string, std::string>::iterator anOrigIter = theAttrs.begin();
142 for(; anOrigIter != theAttrs.end(); anOrigIter++) {
143 if (aThis.find(anOrigIter->first) == aThis.end()) {
144 return "current model had no attribute '" + anOrigIter->first + "'";
150 static void dumpFloat(std::ostringstream& theOutput, double x, int nDigits)
152 static double pow10[] = {
153 1.0, 10., 100., 1.e3, 1.e4, 1.e5, 1.e6, 1.e7, 1.e8, 1.e9,
154 1.e10, 1.e11, 1.e12, 1.e13, 1.e14, 1.e16, 1.e17, 1.e18, 1.e19
156 int numdigits = log10(fabs(x)) + 1;
157 if (numdigits >= nDigits)
159 int idx = numdigits - nDigits;
160 theOutput << std::fixed << std::setprecision(0) << (0 < idx && idx < 20 ? std::round(x / pow10[idx]) * pow10[idx] : x);
164 int prec = (numdigits < 0 ? nDigits : nDigits - numdigits);
165 theOutput << std::fixed << std::setprecision(prec) << x;
170 static void dumpArray(std::ostringstream& theOutput, const double theArray[],
171 int theSize, int thePrecision = PRECISION)
173 for (int i = 0; i < theSize; ++i) {
176 theOutput << std::fixed << std::setprecision(thePrecision)
177 << (fabs(theArray[i]) < TOLERANCE ? 0.0 : theArray[i]);
178 //dumpFloat(theOutput, (fabs(theArray[i]) < TOLERANCE ? 0.0 : theArray[i]), thePrecision);
182 std::string ModelHighAPI_FeatureStore::dumpAttr(const AttributePtr& theAttr) {
183 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
184 FeaturePtr aFeatOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttr->owner());
185 if (aFeatOwner.get() && !aFactory->isCase(aFeatOwner, theAttr->id())) {
186 return "__notcase__";
188 std::string aType = theAttr->attributeType();
190 // do not check selection of the filter,
191 // because there is neither parametric update nor dump support yet.
192 FiltersFeaturePtr aFilter = std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(aFeatOwner);
193 if (aFilter && (aType == ModelAPI_AttributeSelection::typeId() ||
194 aType == ModelAPI_AttributeSelectionList::typeId()))
195 return "__filter_selection__";
197 std::ostringstream aResult;
198 if (!theAttr->isInitialized()) {
199 if (aType == ModelAPI_AttributeBoolean::typeId()) {
200 // special case for Boolean attribute (mean it false if not initialized)
202 return aResult.str();
203 } else if (aType == ModelAPI_AttributeString::typeId()) {
204 // special case for attribute "SolverError"
205 if (theAttr->id() == "SolverError" &&
206 std::dynamic_pointer_cast<ModelAPI_Feature>(theAttr->owner())->getKind() == "Sketch")
210 return "__notinitialized__";
212 if (aType == ModelAPI_AttributeDocRef::typeId()) {
213 AttributeDocRefPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDocRef>(theAttr);
214 DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
215 if (anAttr->value() != aDoc) {
216 ResultPtr aRes = ModelAPI_Tools::findPartResult(aDoc, anAttr->value());
218 // Part result name (the same as saved file name)
219 aResult<< Locale::Convert::toString(aRes->data()->name());
222 aResult<<aDoc->kind(); // PartSet
224 } else if (aType == ModelAPI_AttributeInteger::typeId()) {
225 AttributeIntegerPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(theAttr);
226 // do not dump a type of ConstraintAngle, because it can be changed due dumping
227 if (anAttr->id() == "AngleType" || anAttr->id() == "AngleTypePrevious") {
229 } else if (anAttr->id() == "LocationType") {
230 return "__notinitialized__";
232 if (anAttr->text().empty())
233 aResult<<anAttr->value();
235 aResult << Locale::Convert::toString(anAttr->text());
236 } else if (aType == ModelAPI_AttributeDouble::typeId()) {
237 AttributeDoublePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttr);
238 if (anAttr->id() == "ConstraintValue") {
239 // do not dump a value of constraint if it is ConstraintAngle,
240 // because this value depends on the angle type
241 FeaturePtr anOwner = ModelAPI_Feature::feature(anAttr->owner());
242 if (anOwner && anOwner->getKind() == "SketchConstraintAngle")
245 int aPrecision = PRECISION;
246 // Special case - precision for the arc angle. It is calculated with tolerance 1e-4,
247 // so the value has only 4 correct digits
248 if (anAttr->id() == "ArcAngle")
250 if (anAttr->text().empty()) {
251 double aVal = anAttr->value();
252 dumpArray(aResult, &aVal, 1, aPrecision);
254 aResult << Locale::Convert::toString(anAttr->text());
255 } else if (aType == ModelAPI_AttributeBoolean::typeId()) {
256 AttributeBooleanPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(theAttr);
257 // do not dump internal flags of ConstraintAngle
258 if (anAttr->id() == "AngleReversedLine1" || anAttr->id() == "AngleReversedLine2") {
261 aResult<<anAttr->value();
262 } else if (aType == ModelAPI_AttributeString::typeId()) {
263 AttributeStringPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttr);
264 // do not dump solver DOF for sketch as it may be changed unexpectedly
265 if (anAttr->id() == "SolverDOF") {
268 // do not dump file path for Image as it is changed after DumpPython
269 if (aFeatOwner->getKind() == "ImportImage" &&
270 anAttr->id() == "file_path") {
273 aResult<<anAttr->value();
274 } else if (aType == ModelAPI_AttributeReference::typeId()) {
275 AttributeReferencePtr anAttr =
276 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttr);
277 if (anAttr->value().get()) {
278 aResult<< Locale::Convert::toString(anAttr->value()->data()->name());
280 aResult<<"__empty__";
282 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
283 AttributeSelectionPtr anAttr =
284 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttr);
285 if (anAttr->context().get())
286 aResult<< Locale::Convert::toString(anAttr->namingName());
288 aResult<<"__notinitialized__";
289 } else if (aType == ModelAPI_AttributeSelectionList::typeId()) {
290 AttributeSelectionListPtr anAttr =
291 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttr);
292 for(int a = 0; a < anAttr->size(); a++) {
295 aResult<< Locale::Convert::toString(anAttr->value(a)->namingName());
297 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
298 AttributeRefAttrPtr anAttr =
299 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttr);
300 ObjectPtr anObj = anAttr->isObject() ? anAttr->object() : anAttr->attr()->owner();
302 aResult<< Locale::Convert::toString(anObj->data()->name());
303 if (!anAttr->isObject()) {
304 aResult<<" "<<anAttr->attr()->id();
307 aResult<<"__empty__";
309 } else if (aType == ModelAPI_AttributeRefList::typeId()) {
310 AttributeRefListPtr anAttr =
311 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttr);
312 // for sketch sub-features the empty values may be skipped and order is not important
313 bool isSketchFeatures = anAttr->id() == "Features" &&
314 std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner())->getKind() == "Sketch";
315 std::list<ObjectPtr> aList = anAttr->list();
316 std::list<std::string> aResList; // list of resulting strings
317 for(std::list<ObjectPtr>::iterator aL = aList.begin(); aL != aList.end(); aL++) {
319 if (isSketchFeatures) {
320 // do not control construction features of an ellipse and other
321 FeaturePtr aFeature = ModelAPI_Feature::feature(*aL);
322 //if (aFeature->getKind() == "SketchConstraintCoincidenceInternal")
323 // continue; // skip internal constraints
324 std::string aStr = aFeature->getKind().substr(0, 16);
325 if (aStr == "SketchConstraint")
326 continue; // no need to dump and check constraints
328 aResList.push_back(Locale::Convert::toString((*aL)->data()->name()));
329 } else if (!isSketchFeatures) {
330 aResList.push_back("__empty__");
333 if (isSketchFeatures)
335 for(std::list<std::string>::iterator aR = aResList.begin(); aR != aResList.end(); aR++) {
338 } else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
339 AttributeRefAttrListPtr anAttr =
340 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttr);
341 std::list<std::pair<ObjectPtr, AttributePtr> > aList = anAttr->list();
342 std::list<std::pair<ObjectPtr, AttributePtr> >::iterator aL = aList.begin();
343 for(; aL != aList.end(); aL++) {
344 if (aL != aList.begin())
346 ObjectPtr anObj = aL->second.get() ? aL->second->owner() : aL->first;
348 aResult<< Locale::Convert::toString(anObj->data()->name());
349 if (aL->second.get()) {
350 aResult<<" "<<aL->second->id();
353 aResult<<"__empty__";
356 } else if (aType == ModelAPI_AttributeIntArray::typeId()) {
357 if (theAttr->id() == "Color") {
358 ResultConstructionPtr aResConstr =
359 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theAttr->owner());
360 if (aResConstr.get())
361 return "__notinitialized__";
363 AttributeIntArrayPtr anAttr =
364 std::dynamic_pointer_cast<ModelAPI_AttributeIntArray>(theAttr);
365 for(int a = 0; a < anAttr->size(); a++)
366 aResult<<anAttr->value(a)<<" ";
367 } else if (aType == ModelAPI_AttributeDoubleArray::typeId()) {
368 AttributeDoubleArrayPtr anAttr =
369 std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(theAttr);
370 for(int a = 0; a < anAttr->size(); a++)
371 aResult<<anAttr->value(a)<<" ";
372 } else if (aType == ModelAPI_AttributeStringArray::typeId()) {
373 AttributeStringArrayPtr anAttr =
374 std::dynamic_pointer_cast<ModelAPI_AttributeStringArray>(theAttr);
375 for(int a = 0; a < anAttr->size(); a++)
376 aResult<<"'"<<anAttr->value(a)<<"'"<<" ";
377 } else if (aType == ModelAPI_AttributeTables::typeId()) {
378 AttributeTablesPtr anAttr =
379 std::dynamic_pointer_cast<ModelAPI_AttributeTables>(theAttr);
380 aResult<<anAttr->tables()<<"x"<<anAttr->rows()<<"x"<<anAttr->columns()<<" ";
381 for(int aTab = 0; aTab < anAttr->tables(); aTab++) {
382 for(int aRow = 0; aRow < anAttr->rows(); aRow++) {
383 for( int aCol = 0; aCol < anAttr->columns(); aCol++) {
384 switch(anAttr->type()) {
385 case ModelAPI_AttributeTables::BOOLEAN:
386 aResult<<anAttr->value(aRow, aCol, aTab).myBool<<" ";
388 case ModelAPI_AttributeTables::INTEGER:
389 aResult<<anAttr->value(aRow, aCol, aTab).myInt<<" ";
391 case ModelAPI_AttributeTables::DOUBLE:
392 aResult<<anAttr->value(aRow, aCol, aTab).myDouble<<" ";
394 case ModelAPI_AttributeTables::STRING:
395 aResult<<"'"<<anAttr->value(aRow, aCol, aTab).myStr.c_str()<<"' ";
401 } else if (aType == GeomDataAPI_Point::typeId()) {
402 AttributePointPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttr);
403 double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()};
404 dumpArray(aResult, aValues, 3);
405 } else if (aType == GeomDataAPI_Dir::typeId()) {
406 if (theAttr->id() == "DistanceDirection")
407 return "__notcase__";
408 AttributeDirPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theAttr);
409 double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()};
410 dumpArray(aResult, aValues, 3);
411 } else if (aType == GeomDataAPI_Point2D::typeId()) {
412 // do not dump flyout point for constraints as it may be changed unexpectedly,
413 // also do not dump selection points controlled by GUI
414 if (theAttr->id() == "ConstraintFlyoutValuePnt" ||
415 theAttr->id() == "SelectedPointA" || theAttr->id() == "SelectedPointB")
416 return "__notinitialized__";
417 AttributePoint2DPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttr);
418 double aValues[2] = {anAttr->x(), anAttr->y()};
419 dumpArray(aResult, aValues, 2);
421 aResult<<"__unknownattribute__";
423 return aResult.str();
426 std::string ModelHighAPI_FeatureStore::dumpShape(std::shared_ptr<GeomAPI_Shape>& theShape) {
427 if (theShape->isNull()) {
430 std::ostringstream aResult;
431 // output the number of shapes of different types
432 GeomAPI_Shape::ShapeType aType = GeomAPI_Shape::COMPOUND;
433 for (; aType <= GeomAPI_Shape::VERTEX; aType = GeomAPI_Shape::ShapeType((int)aType + 1)) {
434 GeomAPI_ShapeExplorer anExp(theShape, aType);
436 std::string aTypeStr = anExp.current()->shapeTypeStr();
438 for (; anExp.more(); anExp.next()) aCount++;
439 aResult << aTypeStr.c_str() << ": " << aCount << std::endl;
442 // output the main characteristics
443 double aVolume = GeomAlgoAPI_ShapeTools::volume(theShape);
444 if (aVolume > 1.e-5) {
446 // volumes of too huge shapes write in the scientific format
447 dumpFloat(aResult, aVolume, 6);
450 double anArea = GeomAlgoAPI_ShapeTools::area(theShape);
451 if (anArea > 1.e-5) {
453 // volumes of too huge shapes write in the scientific format
454 dumpFloat(aResult, anArea, 6);
455 aResult << std::endl;
457 std::shared_ptr<GeomAPI_Pnt> aCenter = GeomAlgoAPI_ShapeTools::centreOfMass(theShape);
458 aResult<<"Center of mass: ";
459 double aCenterVals[3] = {aCenter->x(), aCenter->y(), aCenter->z()};
460 dumpArray(aResult, aCenterVals, 3, 5);
462 return aResult.str();