X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2FModelHighAPI%2FModelHighAPI_FeatureStore.cpp;h=ff5ce54351c21cbf97446b1dc7436480df758a6d;hb=71e8d4816ea8ce20dda49fde68b137fcc7f6f47d;hp=f45f90017fcb98620c307b6c01fc393e35f02749;hpb=93734cc9fd26b29a340e45e78f34331b5bc87923;p=modules%2Fshaper.git diff --git a/src/ModelHighAPI/ModelHighAPI_FeatureStore.cpp b/src/ModelHighAPI/ModelHighAPI_FeatureStore.cpp index f45f90017..ff5ce5435 100644 --- a/src/ModelHighAPI/ModelHighAPI_FeatureStore.cpp +++ b/src/ModelHighAPI/ModelHighAPI_FeatureStore.cpp @@ -1,12 +1,26 @@ -// Copyright (C) 2016-20xx CEA/DEN, EDF R&D --> - -// File: ModelHighAPI_FeatureStore.cpp -// Created: 12 August 2016 -// Author: Mikhail PONIKAROV +// Copyright (C) 2014-2024 CEA, EDF +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// #include #include +#include #include #include #include @@ -21,61 +35,81 @@ #include #include #include +#include #include +#include #include #include #include #include #include +#include #include -#include -#include +#include #include +#include +#include +#include + +#define PRECISION 5 +#define TOLERANCE (1.e-6) + +ModelHighAPI_FeatureStore::ModelHighAPI_FeatureStore(ObjectPtr theObject) { + storeData(theObject->data(), myAttrs); -ModelHighAPI_FeatureStore::ModelHighAPI_FeatureStore(FeaturePtr theFeature) { - storeData(theFeature->data(), myAttrs); - // iterate results to store - std::list allResults; - ModelAPI_Tools::allResults(theFeature, allResults); - std::list::iterator aRes = allResults.begin(); - for(; aRes != allResults.end(); aRes++) { - std::map aResDump; - storeData((*aRes)->data(), aResDump); - myRes.push_back(aResDump); + FeaturePtr aFeature = std::dynamic_pointer_cast(theObject); + if (aFeature) { + // iterate results to store + std::list allResults; + ModelAPI_Tools::allResults(aFeature, allResults); + std::list::iterator aRes = allResults.begin(); + for(; aRes != allResults.end(); aRes++) { + std::map aResDump; + storeData((*aRes)->data(), aResDump); + myRes.push_back(aResDump); + } } } -std::string ModelHighAPI_FeatureStore::compare(FeaturePtr theFeature) { - std::string anError = compareData(theFeature->data(), myAttrs); +std::string ModelHighAPI_FeatureStore::compare(ObjectPtr theObject) { + std::string anError = compareData(theObject->data(), myAttrs); if (!anError.empty()) { - return "Features '" + theFeature->name() + "' differ:" + anError; - } - std::list allResults; - ModelAPI_Tools::allResults(theFeature, allResults); - std::list::iterator aRes = allResults.begin(); - std::list >::iterator aResIter = myRes.begin(); - for(; aRes != allResults.end() && aResIter != myRes.end(); aRes++, aResIter++) { - anError = compareData((*aRes)->data(), *aResIter); - if (!anError.empty()) - return "Results of feature '" + theFeature->name() + "' '" + (*aRes)->data()->name() + + return "Features '" + Locale::Convert::toString(theObject->data()->name()) + "' differ:" + anError; } - if (aRes != allResults.end()) { - return "Current model has more results '" + (*aRes)->data()->name() + "'"; - } - if (aResIter != myRes.end()) { - return "Original model had more results '" + (*aResIter)["__name__"] + "'"; + + FeaturePtr aFeature = std::dynamic_pointer_cast(theObject); + if (aFeature) { + std::list allResults; + ModelAPI_Tools::allResults(aFeature, allResults); + std::list::iterator aRes = allResults.begin(); + std::list >::iterator aResIter = myRes.begin(); + for(; aRes != allResults.end() && aResIter != myRes.end(); aRes++, aResIter++) { + anError = compareData((*aRes)->data(), *aResIter); + if (!anError.empty()) + return "Results of feature '" + Locale::Convert::toString(aFeature->name()) + + "' '" + Locale::Convert::toString((*aRes)->data()->name()) + + "' differ:" + anError; + } + if (aRes != allResults.end()) { + return "Current model has more results '" + + Locale::Convert::toString((*aRes)->data()->name()) + "'"; + } + if (aResIter != myRes.end()) { + return "Original model had more results '" + (*aResIter)["__name__"] + "'"; + } } return ""; // ok } -void ModelHighAPI_FeatureStore::storeData(std::shared_ptr theData, +void ModelHighAPI_FeatureStore::storeData(std::shared_ptr theData, std::map& theAttrs) { - theAttrs["__name__"] = theData->name(); // store name to keep also this information and output if needed + // store name to keep also this information and output if needed + theAttrs["__name__"] = Locale::Convert::toString(theData->name()); std::list > allAttrs = theData->attributes(""); std::list >::iterator anAttr = allAttrs.begin(); for(; anAttr != allAttrs.end(); anAttr++) { @@ -83,11 +117,12 @@ void ModelHighAPI_FeatureStore::storeData(std::shared_ptr theData } ResultPtr aShapeOwner = std::dynamic_pointer_cast(theData->owner()); if (aShapeOwner.get() && aShapeOwner->shape().get()) { - theAttrs["__shape__"] = dumpShape(aShapeOwner->shape()); + std::shared_ptr aShape = aShapeOwner->shape(); + theAttrs["__shape__"] = dumpShape(aShape); } } -std::string ModelHighAPI_FeatureStore::compareData(std::shared_ptr theData, +std::string ModelHighAPI_FeatureStore::compareData(std::shared_ptr theData, std::map& theAttrs) { std::map aThis; @@ -98,7 +133,7 @@ std::string ModelHighAPI_FeatureStore::compareData(std::shared_ptrfirst + "'"; } if (theAttrs[aThisIter->first] != aThisIter->second) { - return "attribute '" + aThisIter->first + "' is different (original != current) '" + + return "attribute '" + aThisIter->first + "' is different (original != current) '" + theAttrs[aThisIter->first] + "' != '" + aThisIter->second + "'"; } } @@ -112,72 +147,159 @@ std::string ModelHighAPI_FeatureStore::compareData(std::shared_ptr= nDigits) + { + int idx = numdigits - nDigits; + theOutput << std::fixed << std::setprecision(0) << (0 < idx && idx < 20 ? std::round(x / pow10[idx]) * pow10[idx] : x); + } + else + { + int prec = (numdigits < 0 ? nDigits : nDigits - numdigits); + theOutput << std::fixed << std::setprecision(prec) << x; + } +} + + +static void dumpArray(std::ostringstream& theOutput, const double theArray[], + int theSize, int thePrecision = PRECISION) +{ + for (int i = 0; i < theSize; ++i) { + if (i > 0) + theOutput << " "; + theOutput << std::fixed << std::setprecision(thePrecision) + << (fabs(theArray[i]) < TOLERANCE ? 0.0 : theArray[i]); + //dumpFloat(theOutput, (fabs(theArray[i]) < TOLERANCE ? 0.0 : theArray[i]), thePrecision); + } +} + std::string ModelHighAPI_FeatureStore::dumpAttr(const AttributePtr& theAttr) { static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators(); FeaturePtr aFeatOwner = std::dynamic_pointer_cast(theAttr->owner()); if (aFeatOwner.get() && !aFactory->isCase(aFeatOwner, theAttr->id())) { return "__notcase__"; } + std::string aType = theAttr->attributeType(); + + // do not check selection of the filter, + // because there is neither parametric update nor dump support yet. + FiltersFeaturePtr aFilter = std::dynamic_pointer_cast(aFeatOwner); + if (aFilter && (aType == ModelAPI_AttributeSelection::typeId() || + aType == ModelAPI_AttributeSelectionList::typeId())) + return "__filter_selection__"; + + std::ostringstream aResult; if (!theAttr->isInitialized()) { + if (aType == ModelAPI_AttributeBoolean::typeId()) { + // special case for Boolean attribute (mean it false if not initialized) + aResult << false; + return aResult.str(); + } else if (aType == ModelAPI_AttributeString::typeId()) { + // special case for attribute "SolverError" + if (theAttr->id() == "SolverError" && + std::dynamic_pointer_cast(theAttr->owner())->getKind() == "Sketch") + return ""; + } + return "__notinitialized__"; } - std::string aType = theAttr->attributeType(); - std::ostringstream aResult; if (aType == ModelAPI_AttributeDocRef::typeId()) { AttributeDocRefPtr anAttr = std::dynamic_pointer_cast(theAttr); DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument(); if (anAttr->value() != aDoc) { ResultPtr aRes = ModelAPI_Tools::findPartResult(aDoc, anAttr->value()); if (aRes.get()) { - aResult<data()->name(); // Part result name (the same as saved file name) + // Part result name (the same as saved file name) + aResult<< Locale::Convert::toString(aRes->data()->name()); } } else { aResult<kind(); // PartSet } } else if (aType == ModelAPI_AttributeInteger::typeId()) { AttributeIntegerPtr anAttr = std::dynamic_pointer_cast(theAttr); + // do not dump a type of ConstraintAngle, because it can be changed due dumping + if (anAttr->id() == "AngleType" || anAttr->id() == "AngleTypePrevious") { + return ""; + } else if (anAttr->id() == "LocationType") { + return "__notinitialized__"; + } if (anAttr->text().empty()) aResult<value(); else - aResult<text(); + aResult << Locale::Convert::toString(anAttr->text()); } else if (aType == ModelAPI_AttributeDouble::typeId()) { AttributeDoublePtr anAttr = std::dynamic_pointer_cast(theAttr); - if (anAttr->text().empty()) - aResult<value(); - else - aResult<text(); + if (anAttr->id() == "ConstraintValue") { + // do not dump a value of constraint if it is ConstraintAngle, + // because this value depends on the angle type + FeaturePtr anOwner = ModelAPI_Feature::feature(anAttr->owner()); + if (anOwner && anOwner->getKind() == "SketchConstraintAngle") + return ""; + } + int aPrecision = PRECISION; + // Special case - precision for the arc angle. It is calculated with tolerance 1e-4, + // so the value has only 4 correct digits + if (anAttr->id() == "ArcAngle") + aPrecision = 1; + if (anAttr->text().empty()) { + double aVal = anAttr->value(); + dumpArray(aResult, &aVal, 1, aPrecision); + } else + aResult << Locale::Convert::toString(anAttr->text()); } else if (aType == ModelAPI_AttributeBoolean::typeId()) { AttributeBooleanPtr anAttr = std::dynamic_pointer_cast(theAttr); + // do not dump internal flags of ConstraintAngle + if (anAttr->id() == "AngleReversedLine1" || anAttr->id() == "AngleReversedLine2") { + return ""; + } aResult<value(); } else if (aType == ModelAPI_AttributeString::typeId()) { AttributeStringPtr anAttr = std::dynamic_pointer_cast(theAttr); + // do not dump solver DOF for sketch as it may be changed unexpectedly + if (anAttr->id() == "SolverDOF") { + return ""; + } + // do not dump file path for Image as it is changed after DumpPython + if (aFeatOwner->getKind() == "ImportImage" && + anAttr->id() == "file_path") { + return ""; + } aResult<value(); } else if (aType == ModelAPI_AttributeReference::typeId()) { AttributeReferencePtr anAttr = std::dynamic_pointer_cast(theAttr); if (anAttr->value().get()) { - aResult<value()->data()->name(); + aResult<< Locale::Convert::toString(anAttr->value()->data()->name()); } else { aResult<<"__empty__"; } } else if (aType == ModelAPI_AttributeSelection::typeId()) { - AttributeSelectionPtr anAttr = + AttributeSelectionPtr anAttr = std::dynamic_pointer_cast(theAttr); - aResult<namingName(); + if (anAttr->context().get()) + aResult<< Locale::Convert::toString(anAttr->namingName()); + else + aResult<<"__notinitialized__"; } else if (aType == ModelAPI_AttributeSelectionList::typeId()) { - AttributeSelectionListPtr anAttr = + AttributeSelectionListPtr anAttr = std::dynamic_pointer_cast(theAttr); for(int a = 0; a < anAttr->size(); a++) { if (a != 0) aResult<<" "; - aResult<value(a)->namingName(); + aResult<< Locale::Convert::toString(anAttr->value(a)->namingName()); } } else if (aType == ModelAPI_AttributeRefAttr::typeId()) { - AttributeRefAttrPtr anAttr = + AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast(theAttr); ObjectPtr anObj = anAttr->isObject() ? anAttr->object() : anAttr->attr()->owner(); if (anObj.get()) { - aResult<data()->name(); + aResult<< Locale::Convert::toString(anObj->data()->name()); if (!anAttr->isObject()) { aResult<<" "<attr()->id(); } @@ -185,16 +307,25 @@ std::string ModelHighAPI_FeatureStore::dumpAttr(const AttributePtr& theAttr) { aResult<<"__empty__"; } } else if (aType == ModelAPI_AttributeRefList::typeId()) { - AttributeRefListPtr anAttr = - std::dynamic_pointer_cast(theAttr); + AttributeRefListPtr anAttr = + std::dynamic_pointer_cast(theAttr); // for sketch sub-features the empty values may be skipped and order is not important - bool isSketchFeatures = anAttr->id() == "Features" && + bool isSketchFeatures = anAttr->id() == "Features" && std::dynamic_pointer_cast(anAttr->owner())->getKind() == "Sketch"; std::list aList = anAttr->list(); std::list aResList; // list of resulting strings for(std::list::iterator aL = aList.begin(); aL != aList.end(); aL++) { if (aL->get()) { - aResList.push_back((*aL)->data()->name()); + if (isSketchFeatures) { + // do not control construction features of an ellipse and other + FeaturePtr aFeature = ModelAPI_Feature::feature(*aL); + //if (aFeature->getKind() == "SketchConstraintCoincidenceInternal") + // continue; // skip internal constraints + std::string aStr = aFeature->getKind().substr(0, 16); + if (aStr == "SketchConstraint") + continue; // no need to dump and check constraints + } + aResList.push_back(Locale::Convert::toString((*aL)->data()->name())); } else if (!isSketchFeatures) { aResList.push_back("__empty__"); } @@ -205,7 +336,7 @@ std::string ModelHighAPI_FeatureStore::dumpAttr(const AttributePtr& theAttr) { aResult<<*aR<<" "; } } else if (aType == ModelAPI_AttributeRefAttrList::typeId()) { - AttributeRefAttrListPtr anAttr = + AttributeRefAttrListPtr anAttr = std::dynamic_pointer_cast(theAttr); std::list > aList = anAttr->list(); std::list >::iterator aL = aList.begin(); @@ -214,7 +345,7 @@ std::string ModelHighAPI_FeatureStore::dumpAttr(const AttributePtr& theAttr) { aResult<<" "; ObjectPtr anObj = aL->second.get() ? aL->second->owner() : aL->first; if (anObj.get()) { - aResult<data()->name(); + aResult<< Locale::Convert::toString(anObj->data()->name()); if (aL->second.get()) { aResult<<" "<second->id(); } @@ -223,24 +354,69 @@ std::string ModelHighAPI_FeatureStore::dumpAttr(const AttributePtr& theAttr) { } } } else if (aType == ModelAPI_AttributeIntArray::typeId()) { - AttributeIntArrayPtr anAttr = + if (theAttr->id() == "Color") { + ResultConstructionPtr aResConstr = + std::dynamic_pointer_cast(theAttr->owner()); + if (aResConstr.get()) + return "__notinitialized__"; + } + AttributeIntArrayPtr anAttr = std::dynamic_pointer_cast(theAttr); for(int a = 0; a < anAttr->size(); a++) aResult<value(a)<<" "; } else if (aType == ModelAPI_AttributeDoubleArray::typeId()) { - AttributeDoubleArrayPtr anAttr = + AttributeDoubleArrayPtr anAttr = std::dynamic_pointer_cast(theAttr); for(int a = 0; a < anAttr->size(); a++) aResult<value(a)<<" "; + } else if (aType == ModelAPI_AttributeStringArray::typeId()) { + AttributeStringArrayPtr anAttr = + std::dynamic_pointer_cast(theAttr); + for(int a = 0; a < anAttr->size(); a++) + aResult<<"'"<value(a)<<"'"<<" "; + } else if (aType == ModelAPI_AttributeTables::typeId()) { + AttributeTablesPtr anAttr = + std::dynamic_pointer_cast(theAttr); + aResult<tables()<<"x"<rows()<<"x"<columns()<<" "; + for(int aTab = 0; aTab < anAttr->tables(); aTab++) { + for(int aRow = 0; aRow < anAttr->rows(); aRow++) { + for( int aCol = 0; aCol < anAttr->columns(); aCol++) { + switch(anAttr->type()) { + case ModelAPI_AttributeTables::BOOLEAN: + aResult<value(aRow, aCol, aTab).myBool<<" "; + break; + case ModelAPI_AttributeTables::INTEGER: + aResult<value(aRow, aCol, aTab).myInt<<" "; + break; + case ModelAPI_AttributeTables::DOUBLE: + aResult<value(aRow, aCol, aTab).myDouble<<" "; + break; + case ModelAPI_AttributeTables::STRING: + aResult<<"'"<value(aRow, aCol, aTab).myStr.c_str()<<"' "; + break; + } + } + } + } } else if (aType == GeomDataAPI_Point::typeId()) { AttributePointPtr anAttr = std::dynamic_pointer_cast(theAttr); - aResult<x()<<" "<y()<<" "<z(); + double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()}; + dumpArray(aResult, aValues, 3); } else if (aType == GeomDataAPI_Dir::typeId()) { + if (theAttr->id() == "DistanceDirection") + return "__notcase__"; AttributeDirPtr anAttr = std::dynamic_pointer_cast(theAttr); - aResult<x()<<" "<y()<<" "<z(); + double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()}; + dumpArray(aResult, aValues, 3); } else if (aType == GeomDataAPI_Point2D::typeId()) { + // do not dump flyout point for constraints as it may be changed unexpectedly, + // also do not dump selection points controlled by GUI + if (theAttr->id() == "ConstraintFlyoutValuePnt" || + theAttr->id() == "SelectedPointA" || theAttr->id() == "SelectedPointB") + return "__notinitialized__"; AttributePoint2DPtr anAttr = std::dynamic_pointer_cast(theAttr); - aResult<x()<<" "<y()<<" "; + double aValues[2] = {anAttr->x(), anAttr->y()}; + dumpArray(aResult, aValues, 2); } else { aResult<<"__unknownattribute__"; } @@ -248,24 +424,40 @@ std::string ModelHighAPI_FeatureStore::dumpAttr(const AttributePtr& theAttr) { } std::string ModelHighAPI_FeatureStore::dumpShape(std::shared_ptr& theShape) { - TopoDS_Shape aShape = theShape->impl(); - if (aShape.IsNull()) { + if (theShape->isNull()) { return "null"; } std::ostringstream aResult; // output the number of shapes of different types - TopAbs_ShapeEnum aType = TopAbs_COMPOUND; - for(; aType <= TopAbs_VERTEX; aType = TopAbs_ShapeEnum((int)aType + 1)) { - TopExp_Explorer anExp(aShape, aType); - int aCount = 0; - for(; anExp.More(); anExp.Next()) aCount++; - TopAbs::Print(aType, aResult); - aResult<<": "<shapeTypeStr(); + int aCount = 0; + for (; anExp.more(); anExp.next()) aCount++; + aResult << aTypeStr.c_str() << ": " << aCount << std::endl; + } } // output the main characteristics - aResult<<"Volume: "< 1.e-5) { + aResult<<"Volume: "; + // volumes of too huge shapes write in the scientific format + dumpFloat(aResult, aVolume, 6); + aResult< 1.e-5) { + aResult << "Area: "; + // volumes of too huge shapes write in the scientific format + dumpFloat(aResult, anArea, 6); + aResult << std::endl; + } std::shared_ptr aCenter = GeomAlgoAPI_ShapeTools::centreOfMass(theShape); - aResult<<"Center of mass: "<x()<<" "<y()<<" "<z()<x(), aCenter->y(), aCenter->z()}; + dumpArray(aResult, aCenterVals, 3, 5); + aResult<