1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "ModelHighAPI_Tools.h"
22 #include <ModelHighAPI_FeatureStore.h>
23 //--------------------------------------------------------------------------------------
24 #include <GeomAPI_Dir.h>
25 #include <GeomAPI_Pnt.h>
26 #include <GeomAPI_Pnt2d.h>
27 //--------------------------------------------------------------------------------------
28 #include <GeomDataAPI_Dir.h>
29 #include <GeomDataAPI_Point.h>
30 #include <GeomDataAPI_Point2D.h>
31 //--------------------------------------------------------------------------------------
32 #include <ModelAPI_AttributeBoolean.h>
33 #include <ModelAPI_AttributeDocRef.h>
34 #include <ModelAPI_AttributeDouble.h>
35 #include <ModelAPI_AttributeIntArray.h>
36 #include <ModelAPI_AttributeInteger.h>
37 #include <ModelAPI_AttributeRefAttr.h>
38 #include <ModelAPI_AttributeRefAttrList.h>
39 #include <ModelAPI_AttributeReference.h>
40 #include <ModelAPI_AttributeRefList.h>
41 #include <ModelAPI_AttributeSelection.h>
42 #include <ModelAPI_AttributeSelectionList.h>
43 #include <ModelAPI_AttributeString.h>
44 #include <ModelAPI_AttributeStringArray.h>
45 #include <ModelAPI_AttributeDoubleArray.h>
46 #include <ModelAPI_Session.h>
47 #include <ModelAPI_Tools.h>
48 #include <ModelAPI_ResultPart.h>
49 //--------------------------------------------------------------------------------------
50 #include <Config_ModuleReader.h>
51 //--------------------------------------------------------------------------------------
52 #include "ModelHighAPI_Double.h"
53 #include "ModelHighAPI_Integer.h"
54 #include "ModelHighAPI_RefAttr.h"
55 #include "ModelHighAPI_Reference.h"
56 #include "ModelHighAPI_Selection.h"
58 #include <Events_InfoMessage.h>
60 // Have to be included before std headers
66 //--------------------------------------------------------------------------------------
67 void fillAttribute(const std::shared_ptr<GeomAPI_Pnt2d> & theValue,
68 const std::shared_ptr<GeomDataAPI_Point2D> & theAttribute)
70 theAttribute->setValue(theValue);
73 void fillAttribute(const std::shared_ptr<GeomDataAPI_Point2D> & theAttribute,
74 double theX, double theY)
76 theAttribute->setValue(theX, theY);
79 //--------------------------------------------------------------------------------------
80 void fillAttribute(const std::shared_ptr<GeomAPI_Dir> & theValue,
81 const std::shared_ptr<GeomDataAPI_Dir> & theAttribute)
83 theAttribute->setValue(theValue);
86 //--------------------------------------------------------------------------------------
87 void fillAttribute(const std::shared_ptr<GeomAPI_Pnt> & theValue,
88 const std::shared_ptr<GeomDataAPI_Point> & theAttribute)
90 theAttribute->setValue(theValue);
93 //--------------------------------------------------------------------------------------
94 void fillAttribute(bool theValue,
95 const std::shared_ptr<ModelAPI_AttributeBoolean> & theAttribute)
97 theAttribute->setValue(theValue);
100 //--------------------------------------------------------------------------------------
101 void fillAttribute(const ModelHighAPI_Double & theValue,
102 const std::shared_ptr<ModelAPI_AttributeDouble> & theAttribute)
104 theValue.fillAttribute(theAttribute);
106 void fillAttribute(double theValue,
107 const std::shared_ptr<ModelAPI_AttributeDouble> & theAttribute)
109 theAttribute->setValue(theValue);
112 //--------------------------------------------------------------------------------------
113 void fillAttribute(const ModelHighAPI_Integer & theValue,
114 const std::shared_ptr<ModelAPI_AttributeInteger> & theAttribute)
116 theValue.fillAttribute(theAttribute);
118 void fillAttribute(int theValue,
119 const std::shared_ptr<ModelAPI_AttributeInteger> & theAttribute)
121 theAttribute->setValue(theValue);
124 //--------------------------------------------------------------------------------------
125 void fillAttribute(const ModelHighAPI_RefAttr & theValue,
126 const std::shared_ptr<ModelAPI_AttributeRefAttr> & theAttribute)
128 theValue.fillAttribute(theAttribute);
131 //--------------------------------------------------------------------------------------
132 void fillAttribute(const std::list<ModelHighAPI_RefAttr> & theValue,
133 const std::shared_ptr<ModelAPI_AttributeRefAttrList> & theAttribute)
135 theAttribute->clear();
136 for (auto it = theValue.begin(); it != theValue.end(); ++it)
137 it->appendToList(theAttribute);
140 //--------------------------------------------------------------------------------------
141 void fillAttribute(const ModelHighAPI_Reference & theValue,
142 const std::shared_ptr<ModelAPI_AttributeReference> & theAttribute)
144 theValue.fillAttribute(theAttribute);
147 //--------------------------------------------------------------------------------------
148 void fillAttribute(const std::list<ModelHighAPI_Reference> & theValue,
149 const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
151 theAttribute->clear();
152 for (auto it = theValue.begin(); it != theValue.end(); ++it)
153 it->appendToList(theAttribute);
156 //--------------------------------------------------------------------------------------
157 void fillAttribute(const std::shared_ptr<ModelAPI_Object> & theValue,
158 const std::shared_ptr<ModelAPI_AttributeReference> & theAttribute)
160 theAttribute->setValue(theValue);
163 //--------------------------------------------------------------------------------------
164 void fillAttribute(const std::list<std::shared_ptr<ModelAPI_Object> > & theValue,
165 const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
167 theAttribute->clear();
168 for (auto it = theValue.begin(); it != theValue.end(); ++it)
169 theAttribute->append(*it);
173 void fillAttribute(const std::list<ModelHighAPI_Selection> & theValue,
174 const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
176 theAttribute->clear();
177 for (auto it = theValue.begin(); it != theValue.end(); ++it)
178 theAttribute->append(it->resultSubShapePair().first); // use only context
181 //--------------------------------------------------------------------------------------
182 void fillAttribute(const ModelHighAPI_Selection & theValue,
183 const std::shared_ptr<ModelAPI_AttributeSelection> & theAttribute)
185 theValue.fillAttribute(theAttribute);
188 //--------------------------------------------------------------------------------------
189 void fillAttribute(const std::list<ModelHighAPI_Selection> & theValue,
190 const std::shared_ptr<ModelAPI_AttributeSelectionList> & theAttribute)
192 theAttribute->clear();
194 if(!theValue.empty()) {
195 std::string aSelectionType;
196 const ModelHighAPI_Selection& aSelection = theValue.front();
197 theAttribute->setSelectionType(aSelection.shapeType());
200 for (auto it = theValue.begin(); it != theValue.end(); ++it)
201 it->appendToList(theAttribute);
204 //--------------------------------------------------------------------------------------
205 void fillAttribute(const std::string & theValue,
206 const std::shared_ptr<ModelAPI_AttributeString> & theAttribute)
208 theAttribute->setValue(theValue);
211 //--------------------------------------------------------------------------------------
212 void fillAttribute(const char * theValue,
213 const std::shared_ptr<ModelAPI_AttributeString> & theAttribute)
215 theAttribute->setValue(theValue);
218 //--------------------------------------------------------------------------------------
219 void fillAttribute(const std::list<std::string> & theValue,
220 const std::shared_ptr<ModelAPI_AttributeStringArray> & theAttribute)
222 theAttribute->setSize(int(theValue.size()));
225 for (auto it = theValue.begin(); it != theValue.end(); ++it, ++anIndex)
226 theAttribute->setValue(anIndex, *it);
229 //--------------------------------------------------------------------------------------
230 void fillAttribute(const std::list<ModelHighAPI_Integer> & theValue,
231 const std::shared_ptr<ModelAPI_AttributeIntArray> & theAttribute)
233 theAttribute->setSize(int(theValue.size()));
236 for (auto it = theValue.begin(); it != theValue.end(); ++it, ++anIndex)
237 theAttribute->setValue(anIndex, it->intValue()); // use only values, no text support in array
240 //==================================================================================================
241 GeomAPI_Shape::ShapeType shapeTypeByStr(std::string theShapeTypeStr)
243 GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
245 std::transform(theShapeTypeStr.begin(), theShapeTypeStr.end(),
246 theShapeTypeStr.begin(), ::tolower);
248 if(theShapeTypeStr == "compound") {
249 aShapeType = GeomAPI_Shape::COMPOUND;
250 } else if(theShapeTypeStr == "compsolid") {
251 aShapeType = GeomAPI_Shape::COMPSOLID;
252 } else if(theShapeTypeStr == "solid") {
253 aShapeType = GeomAPI_Shape::SOLID;
254 } else if(theShapeTypeStr == "shell") {
255 aShapeType = GeomAPI_Shape::SHELL;
256 } else if(theShapeTypeStr == "face") {
257 aShapeType = GeomAPI_Shape::FACE;
258 } else if(theShapeTypeStr == "wire") {
259 aShapeType = GeomAPI_Shape::WIRE;
260 } else if(theShapeTypeStr == "edge") {
261 aShapeType = GeomAPI_Shape::EDGE;
262 } else if(theShapeTypeStr == "vertex") {
263 aShapeType = GeomAPI_Shape::VERTEX;
264 } else if(theShapeTypeStr == "shape") {
265 aShapeType = GeomAPI_Shape::SHAPE;
271 //==================================================================================================
272 GeomAPI_Shape::ShapeType getShapeType(const ModelHighAPI_Selection& theSelection)
274 GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
276 switch(theSelection.variantType()) {
277 case ModelHighAPI_Selection::VT_ResultSubShapePair: {
278 ResultSubShapePair aPair = theSelection.resultSubShapePair();
279 GeomShapePtr aShape = aPair.second;
281 aShape = aPair.first->shape();
286 aShapeType = aShape->shapeType();
289 case ModelHighAPI_Selection::VT_TypeSubShapeNamePair: {
290 TypeSubShapeNamePair aPair = theSelection.typeSubShapeNamePair();
291 std::string aType = aPair.first;
292 aShapeType = shapeTypeByStr(aType);
300 //--------------------------------------------------------------------------------------
301 ModelAPI_AttributeTables::ValueType valueTypeByStr(const std::string& theValueTypeStr)
303 std::string aType = theValueTypeStr;
304 std::transform(aType.begin(), aType.end(), aType.begin(), ::tolower);
305 if (aType == "boolean")
306 return ModelAPI_AttributeTables::BOOLEAN;
307 else if (aType == "integer")
308 return ModelAPI_AttributeTables::INTEGER;
309 else if (aType == "string")
310 return ModelAPI_AttributeTables::STRING;
311 return ModelAPI_AttributeTables::DOUBLE; // default
314 //--------------------------------------------------------------------------------------
315 std::string strByValueType(const ModelAPI_AttributeTables::ValueType theType)
318 case ModelAPI_AttributeTables::BOOLEAN: return "BOOLEAN";
319 case ModelAPI_AttributeTables::INTEGER: return "INTEGER";
320 case ModelAPI_AttributeTables::DOUBLE: return "DOUBLE";
321 case ModelAPI_AttributeTables::STRING: return "STRING";
323 return ""; // bad case
326 /// stores the features information, recoursively stores sub-documetns features
327 std::string storeFeatures(const std::string& theDocName, DocumentPtr theDoc,
328 std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> >& theStore,
329 const bool theCompare) // if false => store
331 std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> >::iterator aDocFind;
333 aDocFind = theStore.find(theDocName);
334 if (aDocFind == theStore.end()) {
335 return "Document '" + theDocName + "' not found";
338 // store the model features information: iterate all features
339 int aFeaturesCount = 0; // stores the number of compared features for this document to compate
340 std::set<std::string> aProcessed; // processed features names (that are in the current document)
341 std::list<FeaturePtr> allFeatures = theDoc->allFeatures();
342 std::list<FeaturePtr>::iterator allIter = allFeatures.begin();
343 for(; allIter != allFeatures.end(); allIter++) {
344 FeaturePtr aFeat = *allIter;
346 std::map<std::string, ModelHighAPI_FeatureStore>::iterator
347 aFeatFind = aDocFind->second.find(aFeat->name());
348 if (aFeatFind == aDocFind->second.end()) {
349 return "Document '" + theDocName + "' feature '" + aFeat->name() + "' not found";
351 std::string anError = aFeatFind->second.compare(aFeat);
352 if (!anError.empty()) {
353 anError = "Document " + theDocName + " " + anError;
357 aProcessed.insert(aFeat->name());
359 theStore[theDocName][aFeat->name()] = ModelHighAPI_FeatureStore(aFeat);
361 // iterate all results of this feature
362 std::list<ResultPtr> allResults;
363 ModelAPI_Tools::allResults(aFeat, allResults);
364 std::list<ResultPtr>::iterator aRes = allResults.begin();
365 for(; aRes != allResults.end(); aRes++) {
366 // recoursively store features of sub-documents
367 if ((*aRes)->groupName() == ModelAPI_ResultPart::group()) {
368 DocumentPtr aDoc = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes)->partDoc();
370 std::string anError = storeFeatures((*aRes)->data()->name(), aDoc, theStore, theCompare);
371 if (!anError.empty())
377 // checks the number of compared features
379 if (aDocFind->second.size() != aFeaturesCount) {
380 // search for disappeared feature
381 std::string aLostName;
382 std::map<std::string, ModelHighAPI_FeatureStore>::iterator aLostIter;
383 for(aLostIter = aDocFind->second.begin(); aLostIter != aDocFind->second.end(); aLostIter++) {
384 if (aProcessed.find(aLostIter->first) == aProcessed.end()) {
385 aLostName = aLostIter->first;
388 return "For document '" + theDocName +
389 "' the number of features is decreased, there is no feature '" + aLostName + "'";
395 //==================================================================================================
396 bool checkPythonDump()
398 SessionPtr aSession = ModelAPI_Session::get();
399 // dump all to the python file
400 aSession->startOperation("Check python dump");
401 FeaturePtr aDump = aSession->moduleDocument()->addFeature("Dump");
403 aDump->string("file_path")->setValue("check_dump.py"); // to the current folder
404 aDump->string("file_format")->setValue("py"); // to the current folder
407 bool isProblem = !aDump.get() || !aDump->error().empty(); // after "finish" dump will be removed
408 if (isProblem && aDump.get()) {
409 std::cout<<"Dump feature error "<<aDump->error()<<std::endl;
410 Events_InfoMessage anErrorMsg(std::string("checkPythonDump"), aDump->error());
413 aSession->finishOperation();
415 return false; // something is wrong during dump
418 // map from document name to feature name to feature data
419 std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> > aStore;
420 std::string anError = storeFeatures(
421 aSession->moduleDocument()->kind(), aSession->moduleDocument(), aStore, false);
422 if (!anError.empty()) {
423 Events_InfoMessage anErrorMsg(std::string("checkPythonDump"), anError);
427 // close all before importation of the script
428 aSession->closeAll();
429 // execute the dumped
430 PyGILState_STATE gstate = PyGILState_Ensure(); /* acquire python thread */
431 PyObject* PyFileObject = PyFile_FromString("./check_dump.py", "r");
432 PyRun_SimpleFileEx(PyFile_AsFile(PyFileObject), "./check_dump.py", 1);
433 PyGILState_Release(gstate); /* release python thread */
435 // compare with the stored data
436 anError = storeFeatures(
437 aSession->moduleDocument()->kind(), aSession->moduleDocument(), aStore, true);
438 if (!anError.empty()) {
439 std::cout<<anError<<std::endl;
440 Events_InfoMessage anErrorMsg(std::string("checkPythonDump"), anError);
448 //--------------------------------------------------------------------------------------