X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FModelHighAPI%2FModelHighAPI_Tools.cpp;h=d03130b59b2f0b01c2b934f22457d97eb10ea545;hb=08f1aef6629e6a63cc4671d271ded4de6e826948;hp=46bf241bd0ad867b36057eaacec6b8ae94624c75;hpb=421e05a30e1122e497be417f55cfcaaece238394;p=modules%2Fshaper.git diff --git a/src/ModelHighAPI/ModelHighAPI_Tools.cpp b/src/ModelHighAPI/ModelHighAPI_Tools.cpp index 46bf241bd..d03130b59 100644 --- a/src/ModelHighAPI/ModelHighAPI_Tools.cpp +++ b/src/ModelHighAPI/ModelHighAPI_Tools.cpp @@ -1,11 +1,25 @@ -// Name : ModelHighAPI_Tools.cpp -// Purpose: +// Copyright (C) 2014-2017 CEA/DEN, EDF R&D +// +// 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 // -// History: -// 07/06/16 - Sergey POKHODENKO - Creation of the file -//-------------------------------------------------------------------------------------- #include "ModelHighAPI_Tools.h" +#include //-------------------------------------------------------------------------------------- #include #include @@ -27,10 +41,28 @@ #include #include #include +#include +#include +#include +#include +#include +//-------------------------------------------------------------------------------------- +#include //-------------------------------------------------------------------------------------- #include "ModelHighAPI_Double.h" +#include "ModelHighAPI_Integer.h" +#include "ModelHighAPI_RefAttr.h" +#include "ModelHighAPI_Reference.h" #include "ModelHighAPI_Selection.h" +#include + +// Have to be included before std headers +#include + +#include +#include + //-------------------------------------------------------------------------------------- void fillAttribute(const std::shared_ptr & theValue, const std::shared_ptr & theAttribute) @@ -71,6 +103,82 @@ void fillAttribute(const ModelHighAPI_Double & theValue, { theValue.fillAttribute(theAttribute); } +void fillAttribute(double theValue, + const std::shared_ptr & theAttribute) +{ + theAttribute->setValue(theValue); +} + +//-------------------------------------------------------------------------------------- +void fillAttribute(const ModelHighAPI_Integer & theValue, + const std::shared_ptr & theAttribute) +{ + theValue.fillAttribute(theAttribute); +} +void fillAttribute(int theValue, + const std::shared_ptr & theAttribute) +{ + theAttribute->setValue(theValue); +} + +//-------------------------------------------------------------------------------------- +void fillAttribute(const ModelHighAPI_RefAttr & theValue, + const std::shared_ptr & theAttribute) +{ + theValue.fillAttribute(theAttribute); +} + +//-------------------------------------------------------------------------------------- +void fillAttribute(const std::list & theValue, + const std::shared_ptr & theAttribute) +{ + theAttribute->clear(); + for (auto it = theValue.begin(); it != theValue.end(); ++it) + it->appendToList(theAttribute); +} + +//-------------------------------------------------------------------------------------- +void fillAttribute(const ModelHighAPI_Reference & theValue, + const std::shared_ptr & theAttribute) +{ + theValue.fillAttribute(theAttribute); +} + +//-------------------------------------------------------------------------------------- +void fillAttribute(const std::list & theValue, + const std::shared_ptr & theAttribute) +{ + theAttribute->clear(); + for (auto it = theValue.begin(); it != theValue.end(); ++it) + it->appendToList(theAttribute); +} + +//-------------------------------------------------------------------------------------- +void fillAttribute(const std::shared_ptr & theValue, + const std::shared_ptr & theAttribute) +{ + theAttribute->setValue(theValue); +} + +//-------------------------------------------------------------------------------------- +void fillAttribute(const std::list > & theValue, + const std::shared_ptr & theAttribute) +{ + theAttribute->clear(); + for (auto it = theValue.begin(); it != theValue.end(); ++it) + theAttribute->append(*it); +} + +MODELHIGHAPI_EXPORT +void fillAttribute(const std::list & theValue, + const std::shared_ptr & theAttribute) +{ + theAttribute->clear(); + for (auto it = theValue.begin(); it != theValue.end(); ++it) { + if (it->resultSubShapePair().first) + theAttribute->append(it->resultSubShapePair().first); // use only context + } +} //-------------------------------------------------------------------------------------- void fillAttribute(const ModelHighAPI_Selection & theValue, @@ -84,6 +192,13 @@ void fillAttribute(const std::list & theValue, const std::shared_ptr & theAttribute) { theAttribute->clear(); + + if(!theValue.empty()) { + std::string aSelectionType; + const ModelHighAPI_Selection& aSelection = theValue.front(); + theAttribute->setSelectionType(aSelection.shapeType()); + } + for (auto it = theValue.begin(); it != theValue.end(); ++it) it->appendToList(theAttribute); } @@ -94,4 +209,242 @@ void fillAttribute(const std::string & theValue, { theAttribute->setValue(theValue); } + +//-------------------------------------------------------------------------------------- +void fillAttribute(const char * theValue, + const std::shared_ptr & theAttribute) +{ + theAttribute->setValue(theValue); +} + +//-------------------------------------------------------------------------------------- +void fillAttribute(const std::list & theValue, + const std::shared_ptr & theAttribute) +{ + theAttribute->setSize(int(theValue.size())); + + int anIndex = 0; + for (auto it = theValue.begin(); it != theValue.end(); ++it, ++anIndex) + theAttribute->setValue(anIndex, *it); +} + +//-------------------------------------------------------------------------------------- +void fillAttribute(const std::list & theValue, + const std::shared_ptr & theAttribute) +{ + theAttribute->setSize(int(theValue.size())); + + int anIndex = 0; + for (auto it = theValue.begin(); it != theValue.end(); ++it, ++anIndex) + theAttribute->setValue(anIndex, it->intValue()); // use only values, no text support in array +} + +//================================================================================================== +GeomAPI_Shape::ShapeType shapeTypeByStr(std::string theShapeTypeStr) +{ + GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE; + + std::transform(theShapeTypeStr.begin(), theShapeTypeStr.end(), + theShapeTypeStr.begin(), ::tolower); + + if(theShapeTypeStr == "compound") { + aShapeType = GeomAPI_Shape::COMPOUND; + } else if(theShapeTypeStr == "compsolid") { + aShapeType = GeomAPI_Shape::COMPSOLID; + } else if(theShapeTypeStr == "solid") { + aShapeType = GeomAPI_Shape::SOLID; + } else if(theShapeTypeStr == "shell") { + aShapeType = GeomAPI_Shape::SHELL; + } else if(theShapeTypeStr == "face") { + aShapeType = GeomAPI_Shape::FACE; + } else if(theShapeTypeStr == "wire") { + aShapeType = GeomAPI_Shape::WIRE; + } else if(theShapeTypeStr == "edge") { + aShapeType = GeomAPI_Shape::EDGE; + } else if(theShapeTypeStr == "vertex") { + aShapeType = GeomAPI_Shape::VERTEX; + } else if(theShapeTypeStr == "shape") { + aShapeType = GeomAPI_Shape::SHAPE; + } + + return aShapeType; +} + +//================================================================================================== +GeomAPI_Shape::ShapeType getShapeType(const ModelHighAPI_Selection& theSelection) +{ + GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE; + + switch(theSelection.variantType()) { + case ModelHighAPI_Selection::VT_ResultSubShapePair: { + ResultSubShapePair aPair = theSelection.resultSubShapePair(); + GeomShapePtr aShape = aPair.second; + if(!aShape.get()) { + aShape = aPair.first->shape(); + } + if(!aShape.get()) { + return aShapeType; + } + aShapeType = aShape->shapeType(); + break; + } + case ModelHighAPI_Selection::VT_TypeSubShapeNamePair: { + TypeSubShapeNamePair aPair = theSelection.typeSubShapeNamePair(); + std::string aType = aPair.first; + aShapeType = shapeTypeByStr(aType); + break; + } + } + + return aShapeType; +} + +//-------------------------------------------------------------------------------------- +ModelAPI_AttributeTables::ValueType valueTypeByStr(const std::string& theValueTypeStr) +{ + std::string aType = theValueTypeStr; + std::transform(aType.begin(), aType.end(), aType.begin(), ::tolower); + if (aType == "boolean") + return ModelAPI_AttributeTables::BOOLEAN; + else if (aType == "integer") + return ModelAPI_AttributeTables::INTEGER; + else if (aType == "string") + return ModelAPI_AttributeTables::STRING; + return ModelAPI_AttributeTables::DOUBLE; // default +} + +//-------------------------------------------------------------------------------------- +std::string strByValueType(const ModelAPI_AttributeTables::ValueType theType) +{ + switch(theType) { + case ModelAPI_AttributeTables::BOOLEAN: return "BOOLEAN"; + case ModelAPI_AttributeTables::INTEGER: return "INTEGER"; + case ModelAPI_AttributeTables::DOUBLE: return "DOUBLE"; + case ModelAPI_AttributeTables::STRING: return "STRING"; + } + return ""; // bad case +} + +/// stores the features information, recoursively stores sub-documetns features +std::string storeFeatures(const std::string& theDocName, DocumentPtr theDoc, + std::map >& theStore, + const bool theCompare) // if false => store +{ + std::map >::iterator aDocFind; + if (theCompare) { + aDocFind = theStore.find(theDocName); + if (aDocFind == theStore.end()) { + return "Document '" + theDocName + "' not found"; + } + } + // store the model features information: iterate all features + int aFeaturesCount = 0; // stores the number of compared features for this document to compate + std::set aProcessed; // processed features names (that are in the current document) + std::list allFeatures = theDoc->allFeatures(); + std::list::iterator allIter = allFeatures.begin(); + for(; allIter != allFeatures.end(); allIter++) { + FeaturePtr aFeat = *allIter; + if (theCompare) { + std::map::iterator + aFeatFind = aDocFind->second.find(aFeat->name()); + if (aFeatFind == aDocFind->second.end()) { + return "Document '" + theDocName + "' feature '" + aFeat->name() + "' not found"; + } + std::string anError = aFeatFind->second.compare(aFeat); + if (!anError.empty()) { + anError = "Document " + theDocName + " " + anError; + return anError; + } + aFeaturesCount++; + aProcessed.insert(aFeat->name()); + } else { + theStore[theDocName][aFeat->name()] = ModelHighAPI_FeatureStore(aFeat); + } + // iterate all results of this feature + std::list allResults; + ModelAPI_Tools::allResults(aFeat, allResults); + std::list::iterator aRes = allResults.begin(); + for(; aRes != allResults.end(); aRes++) { + // recoursively store features of sub-documents + if ((*aRes)->groupName() == ModelAPI_ResultPart::group()) { + DocumentPtr aDoc = std::dynamic_pointer_cast(*aRes)->partDoc(); + if (aDoc.get()) { + std::string anError = storeFeatures((*aRes)->data()->name(), aDoc, theStore, theCompare); + if (!anError.empty()) + return anError; + } + } + } + } + // checks the number of compared features + if (theCompare) { + if (aDocFind->second.size() != aFeaturesCount) { + // search for disappeared feature + std::string aLostName; + std::map::iterator aLostIter; + for(aLostIter = aDocFind->second.begin(); aLostIter != aDocFind->second.end(); aLostIter++) { + if (aProcessed.find(aLostIter->first) == aProcessed.end()) { + aLostName = aLostIter->first; + } + } + return "For document '" + theDocName + + "' the number of features is decreased, there is no feature '" + aLostName + "'"; + } + } + return ""; // ok +} + +//================================================================================================== +bool checkPythonDump() +{ + SessionPtr aSession = ModelAPI_Session::get(); + // dump all to the python file + aSession->startOperation("Check python dump"); + FeaturePtr aDump = aSession->moduleDocument()->addFeature("Dump"); + if (aDump.get()) { + aDump->string("file_path")->setValue("check_dump.py"); // to the current folder + aDump->string("file_format")->setValue("py"); // to the current folder + aDump->execute(); + } + bool isProblem = !aDump.get() || !aDump->error().empty(); // after "finish" dump will be removed + if (isProblem && aDump.get()) { + std::cout<<"Dump feature error "<error()<error()); + anErrorMsg.send(); + } + aSession->finishOperation(); + if (isProblem) { + return false; // something is wrong during dump + } + + // map from document name to feature name to feature data + std::map > aStore; + std::string anError = storeFeatures( + aSession->moduleDocument()->kind(), aSession->moduleDocument(), aStore, false); + if (!anError.empty()) { + Events_InfoMessage anErrorMsg(std::string("checkPythonDump"), anError); + anErrorMsg.send(); + return false; + } + // close all before importation of the script + aSession->closeAll(); + // execute the dumped + PyGILState_STATE gstate = PyGILState_Ensure(); /* acquire python thread */ + PyObject* PyFileObject = PyFile_FromString("./check_dump.py", "r"); + PyRun_SimpleFileEx(PyFile_AsFile(PyFileObject), "./check_dump.py", 1); + PyGILState_Release(gstate); /* release python thread */ + + // compare with the stored data + anError = storeFeatures( + aSession->moduleDocument()->kind(), aSession->moduleDocument(), aStore, true); + if (!anError.empty()) { + std::cout<