X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FModelHighAPI%2FModelHighAPI_Tools.cpp;h=3aa3579ad19d4df58f63c5c2c0bdae55f275466d;hb=f03c02cd62fe508c65818d31fdf9dcf69f8cbf35;hp=ca56b3712df2f167df528aa1c69361351e5336e9;hpb=8bb6d0093e663565a2144ca82f082effa766b12b;p=modules%2Fshaper.git diff --git a/src/ModelHighAPI/ModelHighAPI_Tools.cpp b/src/ModelHighAPI/ModelHighAPI_Tools.cpp index ca56b3712..3aa3579ad 100644 --- a/src/ModelHighAPI/ModelHighAPI_Tools.cpp +++ b/src/ModelHighAPI/ModelHighAPI_Tools.cpp @@ -1,10 +1,22 @@ -// Name : ModelHighAPI_Tools.cpp -// Purpose: +// Copyright (C) 2014-2019 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 //-------------------------------------------------------------------------------------- @@ -28,10 +40,12 @@ #include #include #include +#include #include #include #include #include +#include //-------------------------------------------------------------------------------------- #include //-------------------------------------------------------------------------------------- @@ -160,8 +174,10 @@ 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->resultSubShapePair().first); // use only context + for (auto it = theValue.begin(); it != theValue.end(); ++it) { + if (it->resultSubShapePair().first) + theAttribute->append(it->resultSubShapePair().first); // use only context + } } //-------------------------------------------------------------------------------------- @@ -178,9 +194,9 @@ void fillAttribute(const std::list & theValue, theAttribute->clear(); if(!theValue.empty()) { - std::string aSelectionType; const ModelHighAPI_Selection& aSelection = theValue.front(); - theAttribute->setSelectionType(aSelection.shapeType()); + GeomAPI_Shape::ShapeType aSelectionType = getShapeType(aSelection); + theAttribute->setSelectionType(strByShapeType(aSelectionType)); } for (auto it = theValue.begin(); it != theValue.end(); ++it) @@ -193,18 +209,52 @@ 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 +} + +void fillAttribute(const ModelHighAPI_Double & theX, + const ModelHighAPI_Double & theY, + const ModelHighAPI_Double & theZ, + const std::shared_ptr & theAttribute) +{ + theX.fillAttribute(theAttribute, theX, theY, theZ); +} + + //================================================================================================== GeomAPI_Shape::ShapeType shapeTypeByStr(std::string theShapeTypeStr) { GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE; - std::transform(theShapeTypeStr.begin(), theShapeTypeStr.end(), theShapeTypeStr.begin(), ::tolower); + std::transform(theShapeTypeStr.begin(), theShapeTypeStr.end(), + theShapeTypeStr.begin(), ::tolower); if(theShapeTypeStr == "compound") { aShapeType = GeomAPI_Shape::COMPOUND; @@ -229,6 +279,41 @@ GeomAPI_Shape::ShapeType shapeTypeByStr(std::string theShapeTypeStr) return aShapeType; } +std::string strByShapeType(GeomAPI_Shape::ShapeType theShapeType) +{ + std::string aShapeTypeStr; + switch (theShapeType) { + case GeomAPI_Shape::COMPOUND: + aShapeTypeStr = "COMPOUND"; + break; + case GeomAPI_Shape::COMPSOLID: + aShapeTypeStr = "COMPSOLID"; + break; + case GeomAPI_Shape::SOLID: + aShapeTypeStr = "SOLID"; + break; + case GeomAPI_Shape::SHELL: + aShapeTypeStr = "SHELL"; + break; + case GeomAPI_Shape::FACE: + aShapeTypeStr = "FACE"; + break; + case GeomAPI_Shape::WIRE: + aShapeTypeStr = "WIRE"; + break; + case GeomAPI_Shape::EDGE: + aShapeTypeStr = "EDGE"; + break; + case GeomAPI_Shape::VERTEX: + aShapeTypeStr = "VERTEX"; + break; + default: + aShapeTypeStr = "SHAPE"; + break; + } + return aShapeTypeStr; +} + //================================================================================================== GeomAPI_Shape::ShapeType getShapeType(const ModelHighAPI_Selection& theSelection) { @@ -253,12 +338,51 @@ GeomAPI_Shape::ShapeType getShapeType(const ModelHighAPI_Selection& theSelection aShapeType = shapeTypeByStr(aType); break; } + case ModelHighAPI_Selection::VT_TypeInnerPointPair: { + TypeInnerPointPair aPair = theSelection.typeInnerPointPair(); + std::string aType = aPair.first; + aType = aType.substr(0, aType.find_first_of('_')); + aShapeType = shapeTypeByStr(aType); + break; + } + case ModelHighAPI_Selection::VT_WeakNamingPair: { + TypeWeakNamingPair aPair = theSelection.typeWeakNamingPair(); + std::string aType = aPair.first; + aShapeType = shapeTypeByStr(aType); + break; + } } return aShapeType; } -/// stores the features information, recoursively stores sub-documetns features +//-------------------------------------------------------------------------------------- +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, recursively stores sub-documents features std::string storeFeatures(const std::string& theDocName, DocumentPtr theDoc, std::map >& theStore, const bool theCompare) // if false => store @@ -271,45 +395,54 @@ std::string storeFeatures(const std::string& theDocName, DocumentPtr theDoc, } } // store the model features information: iterate all features - int aFeaturesCount = 0; // stores the number of compared features for this document to compate + int anObjectsCount = 0; // stores the number of compared features for this document to compare 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; + + // process all objects (features and folders) + std::list allObjects = theDoc->allObjects(); + std::list::iterator allIter = allObjects.begin(); + for(; allIter != allObjects.end(); allIter++) { + ObjectPtr anObject = *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::map::iterator + anObjFind = aDocFind->second.find(anObject->data()->name()); + if (anObjFind == aDocFind->second.end()) { + return "Document '" + theDocName + "' feature '" + anObject->data()->name() + "' not found"; } - std::string anError = aFeatFind->second.compare(aFeat); + std::string anError = anObjFind->second.compare(anObject); if (!anError.empty()) { + anError = "Document " + theDocName + " " + anError; return anError; } - aFeaturesCount++; - aProcessed.insert(aFeat->name()); + anObjectsCount++; + aProcessed.insert(anObject->data()->name()); } else { - theStore[theDocName][aFeat->name()] = ModelHighAPI_FeatureStore(aFeat); + theStore[theDocName][anObject->data()->name()] = ModelHighAPI_FeatureStore(anObject); } - // 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++) { - if ((*aRes)->groupName() == ModelAPI_ResultPart::group()) { // recoursively store features of sub-documents - 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; + + FeaturePtr aFeature = std::dynamic_pointer_cast(anObject); + if (aFeature) { + // iterate all results of this feature + std::list allResults; + ModelAPI_Tools::allResults(aFeature, allResults); + std::list::iterator aRes = allResults.begin(); + for(; aRes != allResults.end(); aRes++) { + // recursively 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) { + if (aDocFind->second.size() != anObjectsCount) { // search for disappeared feature std::string aLostName; std::map::iterator aLostIter; @@ -318,7 +451,7 @@ std::string storeFeatures(const std::string& theDocName, DocumentPtr theDoc, aLostName = aLostIter->first; } } - return "For document '" + theDocName + + return "For document '" + theDocName + "' the number of features is decreased, there is no feature '" + aLostName + "'"; } } @@ -326,50 +459,105 @@ std::string storeFeatures(const std::string& theDocName, DocumentPtr theDoc, } //================================================================================================== -bool checkPythonDump() +typedef std::map > Storage; + +static bool dumpToPython(SessionPtr theSession, + const char* theFilename, + const checkDumpType theSelectionType, + const std::string& theErrorMsgContext) { - SessionPtr aSession = ModelAPI_Session::get(); + // 2431: set PartSet as a current document + theSession->setActiveDocument(theSession->moduleDocument(), true); // dump all to the python file - aSession->startOperation("Check python dump"); - FeaturePtr aDump = aSession->moduleDocument()->addFeature("Dump"); + theSession->startOperation("Check python dump"); + FeaturePtr aDump = theSession->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(); + aDump->string("file_path")->setValue(theFilename); + aDump->string("file_format")->setValue("py"); + aDump->boolean("topological_naming")->setValue(theSelectionType & CHECK_NAMING); + aDump->boolean("geometric_selection")->setValue(theSelectionType & CHECK_GEOMETRICAL); + aDump->boolean("weak_naming")->setValue(theSelectionType & CHECK_WEAK); } bool isProblem = !aDump.get() || !aDump->error().empty(); // after "finish" dump will be removed - 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 anError("checkPythonDump", anError); - anError.send(); - return false; + if (isProblem && aDump.get()) { + std::cout << "Dump feature error " << aDump->error() << std::endl; + Events_InfoMessage anErrorMsg(theErrorMsgContext, aDump->error()); + anErrorMsg.send(); } + theSession->finishOperation(); + return !isProblem; +} + +static bool checkDump(SessionPtr theSession, + char* theFilename, + Storage& theStorage, + const std::string& theErrorMsgContext) +{ + // close all before importation of the script - aSession->closeAll(); + theSession->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); + static char aReadMode[] = "r"; + FILE* PyFileObject = _Py_fopen(theFilename, aReadMode); + PyRun_SimpleFileEx(PyFileObject, theFilename, 1); PyGILState_Release(gstate); /* release python thread */ // compare with the stored data - anError = storeFeatures( - aSession->moduleDocument()->kind(), aSession->moduleDocument(), aStore, true); + std::string anError = storeFeatures( + theSession->moduleDocument()->kind(), theSession->moduleDocument(), theStorage, true); if (!anError.empty()) { - std::cout< > 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; + } + + bool isOk = true; + if (theCheckType & CHECK_NAMING) { + // check dump with the selection by names + isOk = checkDump(aSession, aFileForNamingDump, aStore, anErrorByNaming); + } + if (theCheckType & CHECK_GEOMETRICAL) { + // check dump with the selection by geometry + isOk = isOk && checkDump(aSession, aFileForGeometryDump, aStore, anErrorByGeometry); + } + if (theCheckType & CHECK_WEAK) { + isOk = isOk && checkDump(aSession, aFileForWeakDump, aStore, anErrorByWeak); + } + + return isOk; +} + //--------------------------------------------------------------------------------------