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>
57 #define MAX_PRECISION 12
59 #define TOLERANCE (1.e-6)
61 ModelHighAPI_FeatureStore::ModelHighAPI_FeatureStore(ObjectPtr theObject) {
62 storeData(theObject->data(), myAttrs);
64 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
66 // iterate results to store
67 std::list<ResultPtr> allResults;
68 ModelAPI_Tools::allResults(aFeature, allResults);
69 std::list<ResultPtr>::iterator aRes = allResults.begin();
70 for(; aRes != allResults.end(); aRes++) {
71 std::map<std::string, std::string> aResDump;
72 storeData((*aRes)->data(), aResDump);
73 myRes.push_back(aResDump);
78 std::string ModelHighAPI_FeatureStore::compare(ObjectPtr theObject) {
79 std::string anError = compareData(theObject->data(), myAttrs);
80 if (!anError.empty()) {
81 return "Features '" + Locale::Convert::toString(theObject->data()->name()) +
82 "' differ:" + anError;
85 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
87 std::list<ResultPtr> allResults;
88 ModelAPI_Tools::allResults(aFeature, allResults);
89 std::list<ResultPtr>::iterator aRes = allResults.begin();
90 std::list<std::map<std::string, std::string> >::iterator aResIter = myRes.begin();
91 for(; aRes != allResults.end() && aResIter != myRes.end(); aRes++, aResIter++) {
92 anError = compareData((*aRes)->data(), *aResIter);
94 return "Results of feature '" + Locale::Convert::toString(aFeature->name()) +
95 "' '" + Locale::Convert::toString((*aRes)->data()->name()) +
96 "' differ:" + anError;
98 if (aRes != allResults.end()) {
99 return "Current model has more results '" +
100 Locale::Convert::toString((*aRes)->data()->name()) + "'";
102 if (aResIter != myRes.end()) {
103 return "Original model had more results '" + (*aResIter)["__name__"] + "'";
109 void ModelHighAPI_FeatureStore::storeData(std::shared_ptr<ModelAPI_Data> theData,
110 std::map<std::string, std::string>& theAttrs)
112 // store name to keep also this information and output if needed
113 theAttrs["__name__"] = Locale::Convert::toString(theData->name());
114 std::list<std::shared_ptr<ModelAPI_Attribute> > allAttrs = theData->attributes("");
115 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = allAttrs.begin();
116 for(; anAttr != allAttrs.end(); anAttr++) {
117 theAttrs[(*anAttr)->id()] = dumpAttr(*anAttr);
119 ResultPtr aShapeOwner = std::dynamic_pointer_cast<ModelAPI_Result>(theData->owner());
120 if (aShapeOwner.get() && aShapeOwner->shape().get()) {
121 std::shared_ptr<GeomAPI_Shape> aShape = aShapeOwner->shape();
122 theAttrs["__shape__"] = dumpShape(aShape);
126 std::string ModelHighAPI_FeatureStore::compareData(std::shared_ptr<ModelAPI_Data> theData,
127 std::map<std::string, std::string>& theAttrs)
129 std::map<std::string, std::string> aThis;
130 storeData(theData, aThis);
131 std::map<std::string, std::string>::iterator aThisIter = aThis.begin();
132 for(; aThisIter != aThis.end(); aThisIter++) {
133 if (theAttrs.find(aThisIter->first) == theAttrs.end()) {
134 return "original model had no attribute '" + aThisIter->first + "'";
136 if (aThisIter->first == "__shape__") {
137 // Compare the shape dump
138 if (theAttrs[aThisIter->first] != aThisIter->second) {
139 return compareShape(theAttrs[aThisIter->first], aThisIter->second);
142 else if (theAttrs[aThisIter->first] != aThisIter->second) {
143 return "attribute '" + aThisIter->first + "' is different (original != current) '" +
144 theAttrs[aThisIter->first] + "' != '" + aThisIter->second + "'";
147 // iterate back to find lack attribute in the current model
148 std::map<std::string, std::string>::iterator anOrigIter = theAttrs.begin();
149 for(; anOrigIter != theAttrs.end(); anOrigIter++) {
150 if (aThis.find(anOrigIter->first) == aThis.end()) {
151 return "current model had no attribute '" + anOrigIter->first + "'";
157 std::string ModelHighAPI_FeatureStore::compareShape(const std::string &theOrig, const std::string &theCurrent)
159 std::istringstream issOrig(theOrig), issCurr(theCurrent);
160 std::string strOrig, strCurr;
162 // Compare the topological information (as a single string)
163 bool bDifferent = (!std::getline(issOrig, strOrig) || !std::getline(issCurr, strCurr) || strOrig != strCurr);
166 auto cmpDouble = [](double aOrig, double aCurr, double tol=1.e-5) {
167 return (fabs(aOrig) < tol ? fabs(aCurr) < tol : ((fabs(aOrig-aCurr) / fabs(aOrig)) < tol));
169 auto cmpPoint = [](double pOrig[], double pCurr[], double tol=1.e-4) {
170 return (sqrt((pOrig[0]-pCurr[0])*(pOrig[0]-pCurr[0]) + (pOrig[1]-pCurr[1])*(pOrig[1]-pCurr[1]) + (pOrig[2]-pCurr[2])*(pOrig[2]-pCurr[2])) < tol);
173 // Compare the physical properties (Volume, Area, Center of Mass as doubles with tolerance)
174 double volOrig{}, areaOrig{}, pntOrig[3]={0.,0.,0.};
175 double volCurr{}, areaCurr{}, pntCurr[3]={0.,0.,0.};
176 issOrig >> volOrig >> areaOrig >> pntOrig[0] >> pntOrig[1] >> pntOrig[2];
177 issCurr >> volCurr >> areaCurr >> pntCurr[0] >> pntCurr[1] >> pntCurr[2];
178 bDifferent = (!cmpDouble(volOrig, volCurr) || !cmpDouble(areaOrig, areaCurr) || !cmpPoint(pntOrig, pntCurr));
181 return "attribute '__shape__' is different (original != current) '" + theOrig + "' != '" + theCurrent + "'";
186 static void dumpArray(std::ostringstream& theOutput, const double theArray[],
187 int theSize, int thePrecision = PRECISION)
189 for (int i = 0; i < theSize; ++i) {
192 theOutput << std::fixed << std::setprecision(thePrecision)
193 << (fabs(theArray[i]) < TOLERANCE ? 0.0 : theArray[i]);
197 std::string ModelHighAPI_FeatureStore::dumpAttr(const AttributePtr& theAttr) {
198 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
199 FeaturePtr aFeatOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttr->owner());
200 if (aFeatOwner.get() && !aFactory->isCase(aFeatOwner, theAttr->id())) {
201 return "__notcase__";
203 std::string aType = theAttr->attributeType();
205 // do not check selection of the filter,
206 // because there is neither parametric update nor dump support yet.
207 FiltersFeaturePtr aFilter = std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(aFeatOwner);
208 if (aFilter && (aType == ModelAPI_AttributeSelection::typeId() ||
209 aType == ModelAPI_AttributeSelectionList::typeId()))
210 return "__filter_selection__";
212 std::ostringstream aResult;
213 if (!theAttr->isInitialized()) {
214 if (aType == ModelAPI_AttributeBoolean::typeId()) {
215 // special case for Boolean attribute (mean it false if not initialized)
217 return aResult.str();
218 } else if (aType == ModelAPI_AttributeString::typeId()) {
219 // special case for attribute "SolverError"
220 if (theAttr->id() == "SolverError" &&
221 std::dynamic_pointer_cast<ModelAPI_Feature>(theAttr->owner())->getKind() == "Sketch")
225 return "__notinitialized__";
227 if (aType == ModelAPI_AttributeDocRef::typeId()) {
228 AttributeDocRefPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDocRef>(theAttr);
229 DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
230 if (anAttr->value() != aDoc) {
231 ResultPtr aRes = ModelAPI_Tools::findPartResult(aDoc, anAttr->value());
233 // Part result name (the same as saved file name)
234 aResult<< Locale::Convert::toString(aRes->data()->name());
237 aResult<<aDoc->kind(); // PartSet
239 } else if (aType == ModelAPI_AttributeInteger::typeId()) {
240 AttributeIntegerPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(theAttr);
241 // do not dump a type of ConstraintAngle, because it can be changed due dumping
242 if (anAttr->id() == "AngleType" || anAttr->id() == "AngleTypePrevious") {
244 } else if (anAttr->id() == "LocationType") {
245 return "__notinitialized__";
247 if (anAttr->text().empty())
248 aResult<<anAttr->value();
250 aResult << Locale::Convert::toString(anAttr->text());
251 } else if (aType == ModelAPI_AttributeDouble::typeId()) {
252 AttributeDoublePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttr);
253 if (anAttr->id() == "ConstraintValue") {
254 // do not dump a value of constraint if it is ConstraintAngle,
255 // because this value depends on the angle type
256 FeaturePtr anOwner = ModelAPI_Feature::feature(anAttr->owner());
257 if (anOwner && anOwner->getKind() == "SketchConstraintAngle")
260 int aPrecision = PRECISION;
261 // Special case - precision for the arc angle. It is calculated with tolerance 1e-4,
262 // so the value has only 4 correct digits
263 if (anAttr->id() == "ArcAngle")
265 if (anAttr->text().empty()) {
266 double aVal = anAttr->value();
267 dumpArray(aResult, &aVal, 1, aPrecision);
269 aResult << Locale::Convert::toString(anAttr->text());
270 } else if (aType == ModelAPI_AttributeBoolean::typeId()) {
271 AttributeBooleanPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(theAttr);
272 // do not dump internal flags of ConstraintAngle
273 if (anAttr->id() == "AngleReversedLine1" || anAttr->id() == "AngleReversedLine2") {
276 aResult<<anAttr->value();
277 } else if (aType == ModelAPI_AttributeString::typeId()) {
278 AttributeStringPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttr);
279 // do not dump solver DOF for sketch as it may be changed unexpectedly
280 if (anAttr->id() == "SolverDOF") {
283 // do not dump file path for Image as it is changed after DumpPython
284 if (aFeatOwner->getKind() == "ImportImage" &&
285 anAttr->id() == "file_path") {
288 aResult<<anAttr->value();
289 } else if (aType == ModelAPI_AttributeReference::typeId()) {
290 AttributeReferencePtr anAttr =
291 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttr);
292 if (anAttr->value().get()) {
293 aResult<< Locale::Convert::toString(anAttr->value()->data()->name());
295 aResult<<"__empty__";
297 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
298 AttributeSelectionPtr anAttr =
299 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttr);
300 if (anAttr->context().get())
301 aResult<< Locale::Convert::toString(anAttr->namingName());
303 aResult<<"__notinitialized__";
304 } else if (aType == ModelAPI_AttributeSelectionList::typeId()) {
305 AttributeSelectionListPtr anAttr =
306 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttr);
307 for(int a = 0; a < anAttr->size(); a++) {
310 aResult<< Locale::Convert::toString(anAttr->value(a)->namingName());
312 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
313 AttributeRefAttrPtr anAttr =
314 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttr);
315 ObjectPtr anObj = anAttr->isObject() ? anAttr->object() : anAttr->attr()->owner();
317 aResult<< Locale::Convert::toString(anObj->data()->name());
318 if (!anAttr->isObject()) {
319 aResult<<" "<<anAttr->attr()->id();
322 aResult<<"__empty__";
324 } else if (aType == ModelAPI_AttributeRefList::typeId()) {
325 AttributeRefListPtr anAttr =
326 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttr);
327 // for sketch sub-features the empty values may be skipped and order is not important
328 bool isSketchFeatures = anAttr->id() == "Features" &&
329 std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner())->getKind() == "Sketch";
330 std::list<ObjectPtr> aList = anAttr->list();
331 std::list<std::string> aResList; // list of resulting strings
332 for(std::list<ObjectPtr>::iterator aL = aList.begin(); aL != aList.end(); aL++) {
334 if (isSketchFeatures) {
335 // do not control construction features of an ellipse and other
336 FeaturePtr aFeature = ModelAPI_Feature::feature(*aL);
337 //if (aFeature->getKind() == "SketchConstraintCoincidenceInternal")
338 // continue; // skip internal constraints
339 std::string aStr = aFeature->getKind().substr(0, 16);
340 if (aStr == "SketchConstraint")
341 continue; // no need to dump and check constraints
343 aResList.push_back(Locale::Convert::toString((*aL)->data()->name()));
344 } else if (!isSketchFeatures) {
345 aResList.push_back("__empty__");
348 if (isSketchFeatures)
350 for(std::list<std::string>::iterator aR = aResList.begin(); aR != aResList.end(); aR++) {
353 } else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
354 AttributeRefAttrListPtr anAttr =
355 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttr);
356 std::list<std::pair<ObjectPtr, AttributePtr> > aList = anAttr->list();
357 std::list<std::pair<ObjectPtr, AttributePtr> >::iterator aL = aList.begin();
358 for(; aL != aList.end(); aL++) {
359 if (aL != aList.begin())
361 ObjectPtr anObj = aL->second.get() ? aL->second->owner() : aL->first;
363 aResult<< Locale::Convert::toString(anObj->data()->name());
364 if (aL->second.get()) {
365 aResult<<" "<<aL->second->id();
368 aResult<<"__empty__";
371 } else if (aType == ModelAPI_AttributeIntArray::typeId()) {
372 if (theAttr->id() == "Color") {
373 ResultConstructionPtr aResConstr =
374 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theAttr->owner());
375 if (aResConstr.get())
376 return "__notinitialized__";
378 AttributeIntArrayPtr anAttr =
379 std::dynamic_pointer_cast<ModelAPI_AttributeIntArray>(theAttr);
380 for(int a = 0; a < anAttr->size(); a++)
381 aResult<<anAttr->value(a)<<" ";
382 } else if (aType == ModelAPI_AttributeDoubleArray::typeId()) {
383 AttributeDoubleArrayPtr anAttr =
384 std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(theAttr);
385 for(int a = 0; a < anAttr->size(); a++)
386 aResult<<anAttr->value(a)<<" ";
387 } else if (aType == ModelAPI_AttributeStringArray::typeId()) {
388 AttributeStringArrayPtr anAttr =
389 std::dynamic_pointer_cast<ModelAPI_AttributeStringArray>(theAttr);
390 for(int a = 0; a < anAttr->size(); a++)
391 aResult<<"'"<<anAttr->value(a)<<"'"<<" ";
392 } else if (aType == ModelAPI_AttributeTables::typeId()) {
393 AttributeTablesPtr anAttr =
394 std::dynamic_pointer_cast<ModelAPI_AttributeTables>(theAttr);
395 aResult<<anAttr->tables()<<"x"<<anAttr->rows()<<"x"<<anAttr->columns()<<" ";
396 for(int aTab = 0; aTab < anAttr->tables(); aTab++) {
397 for(int aRow = 0; aRow < anAttr->rows(); aRow++) {
398 for( int aCol = 0; aCol < anAttr->columns(); aCol++) {
399 switch(anAttr->type()) {
400 case ModelAPI_AttributeTables::BOOLEAN:
401 aResult<<anAttr->value(aRow, aCol, aTab).myBool<<" ";
403 case ModelAPI_AttributeTables::INTEGER:
404 aResult<<anAttr->value(aRow, aCol, aTab).myInt<<" ";
406 case ModelAPI_AttributeTables::DOUBLE:
407 aResult<<anAttr->value(aRow, aCol, aTab).myDouble<<" ";
409 case ModelAPI_AttributeTables::STRING:
410 aResult<<"'"<<anAttr->value(aRow, aCol, aTab).myStr.c_str()<<"' ";
416 } else if (aType == GeomDataAPI_Point::typeId()) {
417 AttributePointPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttr);
418 double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()};
419 dumpArray(aResult, aValues, 3);
420 } else if (aType == GeomDataAPI_Dir::typeId()) {
421 if (theAttr->id() == "DistanceDirection")
422 return "__notcase__";
423 AttributeDirPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theAttr);
424 double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()};
425 dumpArray(aResult, aValues, 3);
426 } else if (aType == GeomDataAPI_Point2D::typeId()) {
427 // do not dump flyout point for constraints as it may be changed unexpectedly,
428 // also do not dump selection points controlled by GUI
429 if (theAttr->id() == "ConstraintFlyoutValuePnt" ||
430 theAttr->id() == "SelectedPointA" || theAttr->id() == "SelectedPointB")
431 return "__notinitialized__";
432 AttributePoint2DPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttr);
433 double aValues[2] = {anAttr->x(), anAttr->y()};
434 dumpArray(aResult, aValues, 2);
436 aResult<<"__unknownattribute__";
438 return aResult.str();
441 std::string ModelHighAPI_FeatureStore::dumpShape(std::shared_ptr<GeomAPI_Shape>& theShape) {
442 if (theShape->isNull()) {
445 std::ostringstream aResult;
446 // output the number of shapes of different types
447 GeomAPI_Shape::ShapeType aType = GeomAPI_Shape::COMPOUND;
448 for (; aType <= GeomAPI_Shape::VERTEX; aType = GeomAPI_Shape::ShapeType((int)aType + 1)) {
449 GeomAPI_ShapeExplorer anExp(theShape, aType);
451 std::string aTypeStr = anExp.current()->shapeTypeStr();
453 for (; anExp.more(); anExp.next()) aCount++;
454 aResult << aCount << " ";
462 // output the main characteristics
463 double aVolume = GeomAlgoAPI_ShapeTools::volume(theShape);
464 if (aVolume > 1.e-5) {
465 aResult << std::fixed << std::setprecision(MAX_PRECISION) << aVolume << " ";
469 double anArea = GeomAlgoAPI_ShapeTools::area(theShape);
471 aResult << std::fixed << std::setprecision(MAX_PRECISION) << anArea << " ";
474 std::shared_ptr<GeomAPI_Pnt> aCenter = GeomAlgoAPI_ShapeTools::centreOfMass(theShape);
475 aResult << std::fixed << std::setprecision(MAX_PRECISION) << aCenter->x() << " " << aCenter->y() << " " << aCenter->z() << std::endl;
476 return aResult.str();