1 // Copyright (C) 2014-2023 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-7)
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 dumpArray(std::ostringstream& theOutput, const double theArray[],
151 int theSize, int thePrecision = PRECISION)
153 for (int i = 0; i < theSize; ++i) {
156 theOutput << std::fixed << std::setprecision(thePrecision)
157 << (fabs(theArray[i]) < TOLERANCE ? 0.0 : theArray[i]);
161 std::string ModelHighAPI_FeatureStore::dumpAttr(const AttributePtr& theAttr) {
162 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
163 FeaturePtr aFeatOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttr->owner());
164 if (aFeatOwner.get() && !aFactory->isCase(aFeatOwner, theAttr->id())) {
165 return "__notcase__";
167 std::string aType = theAttr->attributeType();
169 // do not check selection of the filter,
170 // because there is neither parametric update nor dump support yet.
171 FiltersFeaturePtr aFilter = std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(aFeatOwner);
172 if (aFilter && (aType == ModelAPI_AttributeSelection::typeId() ||
173 aType == ModelAPI_AttributeSelectionList::typeId()))
174 return "__filter_selection__";
176 std::ostringstream aResult;
177 if (!theAttr->isInitialized()) {
178 if (aType == ModelAPI_AttributeBoolean::typeId()) {
179 // special case for Boolean attribute (mean it false if not initialized)
181 return aResult.str();
182 } else if (aType == ModelAPI_AttributeString::typeId()) {
183 // special case for attribute "SolverError"
184 if (theAttr->id() == "SolverError" &&
185 std::dynamic_pointer_cast<ModelAPI_Feature>(theAttr->owner())->getKind() == "Sketch")
189 return "__notinitialized__";
191 if (aType == ModelAPI_AttributeDocRef::typeId()) {
192 AttributeDocRefPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDocRef>(theAttr);
193 DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
194 if (anAttr->value() != aDoc) {
195 ResultPtr aRes = ModelAPI_Tools::findPartResult(aDoc, anAttr->value());
197 // Part result name (the same as saved file name)
198 aResult<< Locale::Convert::toString(aRes->data()->name());
201 aResult<<aDoc->kind(); // PartSet
203 } else if (aType == ModelAPI_AttributeInteger::typeId()) {
204 AttributeIntegerPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(theAttr);
205 // do not dump a type of ConstraintAngle, because it can be changed due dumping
206 if (anAttr->id() == "AngleType" || anAttr->id() == "AngleTypePrevious") {
208 } else if (anAttr->id() == "LocationType") {
209 return "__notinitialized__";
211 if (anAttr->text().empty())
212 aResult<<anAttr->value();
214 aResult << Locale::Convert::toString(anAttr->text());
215 } else if (aType == ModelAPI_AttributeDouble::typeId()) {
216 AttributeDoublePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttr);
217 if (anAttr->id() == "ConstraintValue") {
218 // do not dump a value of constraint if it is ConstraintAngle,
219 // because this value depends on the angle type
220 FeaturePtr anOwner = ModelAPI_Feature::feature(anAttr->owner());
221 if (anOwner && anOwner->getKind() == "SketchConstraintAngle")
224 int aPrecision = PRECISION;
225 // Special case - precision for the arc angle. It is calculated with tolerance 1e-4,
226 // so the value has only 4 correct digits
227 if (anAttr->id() == "ArcAngle")
229 if (anAttr->text().empty()) {
230 double aVal = anAttr->value();
231 dumpArray(aResult, &aVal, 1, aPrecision);
233 aResult << Locale::Convert::toString(anAttr->text());
234 } else if (aType == ModelAPI_AttributeBoolean::typeId()) {
235 AttributeBooleanPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(theAttr);
236 // do not dump internal flags of ConstraintAngle
237 if (anAttr->id() == "AngleReversedLine1" || anAttr->id() == "AngleReversedLine2") {
240 aResult<<anAttr->value();
241 } else if (aType == ModelAPI_AttributeString::typeId()) {
242 AttributeStringPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttr);
243 // do not dump solver DOF for sketch as it may be changed unexpectedly
244 if (anAttr->id() == "SolverDOF") {
247 // do not dump file path for Image as it is changed after DumpPython
248 if (aFeatOwner->getKind() == "ImportImage" &&
249 anAttr->id() == "file_path") {
252 aResult<<anAttr->value();
253 } else if (aType == ModelAPI_AttributeReference::typeId()) {
254 AttributeReferencePtr anAttr =
255 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttr);
256 if (anAttr->value().get()) {
257 aResult<< Locale::Convert::toString(anAttr->value()->data()->name());
259 aResult<<"__empty__";
261 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
262 AttributeSelectionPtr anAttr =
263 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttr);
264 if (anAttr->context().get())
265 aResult<< Locale::Convert::toString(anAttr->namingName());
267 aResult<<"__notinitialized__";
268 } else if (aType == ModelAPI_AttributeSelectionList::typeId()) {
269 AttributeSelectionListPtr anAttr =
270 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttr);
271 for(int a = 0; a < anAttr->size(); a++) {
274 aResult<< Locale::Convert::toString(anAttr->value(a)->namingName());
276 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
277 AttributeRefAttrPtr anAttr =
278 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttr);
279 ObjectPtr anObj = anAttr->isObject() ? anAttr->object() : anAttr->attr()->owner();
281 aResult<< Locale::Convert::toString(anObj->data()->name());
282 if (!anAttr->isObject()) {
283 aResult<<" "<<anAttr->attr()->id();
286 aResult<<"__empty__";
288 } else if (aType == ModelAPI_AttributeRefList::typeId()) {
289 AttributeRefListPtr anAttr =
290 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttr);
291 // for sketch sub-features the empty values may be skipped and order is not important
292 bool isSketchFeatures = anAttr->id() == "Features" &&
293 std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner())->getKind() == "Sketch";
294 std::list<ObjectPtr> aList = anAttr->list();
295 std::list<std::string> aResList; // list of resulting strings
296 for(std::list<ObjectPtr>::iterator aL = aList.begin(); aL != aList.end(); aL++) {
298 if (isSketchFeatures) {
299 // do not control construction features of an ellipse and other
300 FeaturePtr aFeature = ModelAPI_Feature::feature(*aL);
301 //if (aFeature->getKind() == "SketchConstraintCoincidenceInternal")
302 // continue; // skip internal constraints
303 std::string aStr = aFeature->getKind().substr(0, 16);
304 if (aStr == "SketchConstraint")
305 continue; // no need to dump and check constraints
307 aResList.push_back(Locale::Convert::toString((*aL)->data()->name()));
308 } else if (!isSketchFeatures) {
309 aResList.push_back("__empty__");
312 if (isSketchFeatures)
314 for(std::list<std::string>::iterator aR = aResList.begin(); aR != aResList.end(); aR++) {
317 } else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
318 AttributeRefAttrListPtr anAttr =
319 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttr);
320 std::list<std::pair<ObjectPtr, AttributePtr> > aList = anAttr->list();
321 std::list<std::pair<ObjectPtr, AttributePtr> >::iterator aL = aList.begin();
322 for(; aL != aList.end(); aL++) {
323 if (aL != aList.begin())
325 ObjectPtr anObj = aL->second.get() ? aL->second->owner() : aL->first;
327 aResult<< Locale::Convert::toString(anObj->data()->name());
328 if (aL->second.get()) {
329 aResult<<" "<<aL->second->id();
332 aResult<<"__empty__";
335 } else if (aType == ModelAPI_AttributeIntArray::typeId()) {
336 if (theAttr->id() == "Color") {
337 ResultConstructionPtr aResConstr =
338 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theAttr->owner());
339 if (aResConstr.get())
340 return "__notinitialized__";
342 AttributeIntArrayPtr anAttr =
343 std::dynamic_pointer_cast<ModelAPI_AttributeIntArray>(theAttr);
344 for(int a = 0; a < anAttr->size(); a++)
345 aResult<<anAttr->value(a)<<" ";
346 } else if (aType == ModelAPI_AttributeDoubleArray::typeId()) {
347 AttributeDoubleArrayPtr anAttr =
348 std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(theAttr);
349 for(int a = 0; a < anAttr->size(); a++)
350 aResult<<anAttr->value(a)<<" ";
351 } else if (aType == ModelAPI_AttributeStringArray::typeId()) {
352 AttributeStringArrayPtr anAttr =
353 std::dynamic_pointer_cast<ModelAPI_AttributeStringArray>(theAttr);
354 for(int a = 0; a < anAttr->size(); a++)
355 aResult<<"'"<<anAttr->value(a)<<"'"<<" ";
356 } else if (aType == ModelAPI_AttributeTables::typeId()) {
357 AttributeTablesPtr anAttr =
358 std::dynamic_pointer_cast<ModelAPI_AttributeTables>(theAttr);
359 aResult<<anAttr->tables()<<"x"<<anAttr->rows()<<"x"<<anAttr->columns()<<" ";
360 for(int aTab = 0; aTab < anAttr->tables(); aTab++) {
361 for(int aRow = 0; aRow < anAttr->rows(); aRow++) {
362 for( int aCol = 0; aCol < anAttr->columns(); aCol++) {
363 switch(anAttr->type()) {
364 case ModelAPI_AttributeTables::BOOLEAN:
365 aResult<<anAttr->value(aRow, aCol, aTab).myBool<<" ";
367 case ModelAPI_AttributeTables::INTEGER:
368 aResult<<anAttr->value(aRow, aCol, aTab).myInt<<" ";
370 case ModelAPI_AttributeTables::DOUBLE:
371 aResult<<anAttr->value(aRow, aCol, aTab).myDouble<<" ";
373 case ModelAPI_AttributeTables::STRING:
374 aResult<<"'"<<anAttr->value(aRow, aCol, aTab).myStr.c_str()<<"' ";
380 } else if (aType == GeomDataAPI_Point::typeId()) {
381 AttributePointPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttr);
382 double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()};
383 dumpArray(aResult, aValues, 3);
384 } else if (aType == GeomDataAPI_Dir::typeId()) {
385 if (theAttr->id() == "DistanceDirection")
386 return "__notcase__";
387 AttributeDirPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theAttr);
388 double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()};
389 dumpArray(aResult, aValues, 3);
390 } else if (aType == GeomDataAPI_Point2D::typeId()) {
391 // do not dump flyout point for constraints as it may be changed unexpectedly,
392 // also do not dump selection points controlled by GUI
393 if (theAttr->id() == "ConstraintFlyoutValuePnt" ||
394 theAttr->id() == "SelectedPointA" || theAttr->id() == "SelectedPointB")
395 return "__notinitialized__";
396 AttributePoint2DPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttr);
397 double aValues[2] = {anAttr->x(), anAttr->y()};
398 dumpArray(aResult, aValues, 2);
400 aResult<<"__unknownattribute__";
402 return aResult.str();
405 std::string ModelHighAPI_FeatureStore::dumpShape(std::shared_ptr<GeomAPI_Shape>& theShape) {
406 if (theShape->isNull()) {
409 std::ostringstream aResult;
410 // output the number of shapes of different types
411 GeomAPI_Shape::ShapeType aType = GeomAPI_Shape::COMPOUND;
412 for (; aType <= GeomAPI_Shape::VERTEX; aType = GeomAPI_Shape::ShapeType((int)aType + 1)) {
413 GeomAPI_ShapeExplorer anExp(theShape, aType);
415 std::string aTypeStr = anExp.current()->shapeTypeStr();
417 for (; anExp.more(); anExp.next()) aCount++;
418 aResult << aTypeStr.c_str() << ": " << aCount << std::endl;
421 // output the main characteristics
422 double aVolume = GeomAlgoAPI_ShapeTools::volume(theShape);
423 if (aVolume > 1.e-5) {
425 // volumes of too huge shapes write in the scientific format
427 aResult<<std::scientific<<std::setprecision(7);
429 aResult<<std::fixed<<std::setprecision(3);
430 aResult<<aVolume<<std::endl;
432 double anArea = GeomAlgoAPI_ShapeTools::area(theShape);
433 if (anArea > 1.e-5) {
435 // volumes of too huge shapes write in the scientific format
437 aResult << std::scientific << std::setprecision(7);
439 aResult << std::fixed << std::setprecision(3);
440 aResult << anArea << std::endl;
442 std::shared_ptr<GeomAPI_Pnt> aCenter = GeomAlgoAPI_ShapeTools::centreOfMass(theShape);
443 aResult<<"Center of mass: ";
444 double aCenterVals[3] = {aCenter->x(), aCenter->y(), aCenter->z()};
445 dumpArray(aResult, aCenterVals, 3, 5);
447 return aResult.str();