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 email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
20 #include "ModelHighAPI_Tools.h"
21 #include <ModelHighAPI_FeatureStore.h>
22 //--------------------------------------------------------------------------------------
23 #include <GeomAPI_Dir.h>
24 #include <GeomAPI_Pnt.h>
25 #include <GeomAPI_Pnt2d.h>
26 //--------------------------------------------------------------------------------------
27 #include <GeomDataAPI_Dir.h>
28 #include <GeomDataAPI_Point.h>
29 #include <GeomDataAPI_Point2D.h>
30 //--------------------------------------------------------------------------------------
31 #include <ModelAPI_AttributeBoolean.h>
32 #include <ModelAPI_AttributeDocRef.h>
33 #include <ModelAPI_AttributeDouble.h>
34 #include <ModelAPI_AttributeIntArray.h>
35 #include <ModelAPI_AttributeInteger.h>
36 #include <ModelAPI_AttributeRefAttr.h>
37 #include <ModelAPI_AttributeRefAttrList.h>
38 #include <ModelAPI_AttributeReference.h>
39 #include <ModelAPI_AttributeRefList.h>
40 #include <ModelAPI_AttributeSelection.h>
41 #include <ModelAPI_AttributeSelectionList.h>
42 #include <ModelAPI_AttributeString.h>
43 #include <ModelAPI_AttributeStringArray.h>
44 #include <ModelAPI_AttributeDoubleArray.h>
45 #include <ModelAPI_Session.h>
46 #include <ModelAPI_Tools.h>
47 #include <ModelAPI_ResultPart.h>
48 //--------------------------------------------------------------------------------------
49 #include <Config_ModuleReader.h>
50 //--------------------------------------------------------------------------------------
51 #include "ModelHighAPI_Double.h"
52 #include "ModelHighAPI_Integer.h"
53 #include "ModelHighAPI_RefAttr.h"
54 #include "ModelHighAPI_Reference.h"
55 #include "ModelHighAPI_Selection.h"
57 #include <Events_InfoMessage.h>
59 // Have to be included before std headers
65 //--------------------------------------------------------------------------------------
66 void fillAttribute(const std::shared_ptr<GeomAPI_Pnt2d> & theValue,
67 const std::shared_ptr<GeomDataAPI_Point2D> & theAttribute)
69 theAttribute->setValue(theValue);
72 void fillAttribute(const std::shared_ptr<GeomDataAPI_Point2D> & theAttribute,
73 double theX, double theY)
75 theAttribute->setValue(theX, theY);
78 //--------------------------------------------------------------------------------------
79 void fillAttribute(const std::shared_ptr<GeomAPI_Dir> & theValue,
80 const std::shared_ptr<GeomDataAPI_Dir> & theAttribute)
82 theAttribute->setValue(theValue);
85 //--------------------------------------------------------------------------------------
86 void fillAttribute(const std::shared_ptr<GeomAPI_Pnt> & theValue,
87 const std::shared_ptr<GeomDataAPI_Point> & theAttribute)
89 theAttribute->setValue(theValue);
92 //--------------------------------------------------------------------------------------
93 void fillAttribute(bool theValue,
94 const std::shared_ptr<ModelAPI_AttributeBoolean> & theAttribute)
96 theAttribute->setValue(theValue);
99 //--------------------------------------------------------------------------------------
100 void fillAttribute(const ModelHighAPI_Double & theValue,
101 const std::shared_ptr<ModelAPI_AttributeDouble> & theAttribute)
103 theValue.fillAttribute(theAttribute);
105 void fillAttribute(double theValue,
106 const std::shared_ptr<ModelAPI_AttributeDouble> & theAttribute)
108 theAttribute->setValue(theValue);
111 //--------------------------------------------------------------------------------------
112 void fillAttribute(const ModelHighAPI_Integer & theValue,
113 const std::shared_ptr<ModelAPI_AttributeInteger> & theAttribute)
115 theValue.fillAttribute(theAttribute);
117 void fillAttribute(int theValue,
118 const std::shared_ptr<ModelAPI_AttributeInteger> & theAttribute)
120 theAttribute->setValue(theValue);
123 //--------------------------------------------------------------------------------------
124 void fillAttribute(const ModelHighAPI_RefAttr & theValue,
125 const std::shared_ptr<ModelAPI_AttributeRefAttr> & theAttribute)
127 theValue.fillAttribute(theAttribute);
130 //--------------------------------------------------------------------------------------
131 void fillAttribute(const std::list<ModelHighAPI_RefAttr> & theValue,
132 const std::shared_ptr<ModelAPI_AttributeRefAttrList> & theAttribute)
134 theAttribute->clear();
135 for (auto it = theValue.begin(); it != theValue.end(); ++it)
136 it->appendToList(theAttribute);
139 //--------------------------------------------------------------------------------------
140 void fillAttribute(const ModelHighAPI_Reference & theValue,
141 const std::shared_ptr<ModelAPI_AttributeReference> & theAttribute)
143 theValue.fillAttribute(theAttribute);
146 //--------------------------------------------------------------------------------------
147 void fillAttribute(const std::list<ModelHighAPI_Reference> & theValue,
148 const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
150 theAttribute->clear();
151 for (auto it = theValue.begin(); it != theValue.end(); ++it)
152 it->appendToList(theAttribute);
155 //--------------------------------------------------------------------------------------
156 void fillAttribute(const std::shared_ptr<ModelAPI_Object> & theValue,
157 const std::shared_ptr<ModelAPI_AttributeReference> & theAttribute)
159 theAttribute->setValue(theValue);
162 //--------------------------------------------------------------------------------------
163 void fillAttribute(const std::list<std::shared_ptr<ModelAPI_Object> > & theValue,
164 const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
166 theAttribute->clear();
167 for (auto it = theValue.begin(); it != theValue.end(); ++it)
168 theAttribute->append(*it);
172 void fillAttribute(const std::list<ModelHighAPI_Selection> & theValue,
173 const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
175 theAttribute->clear();
176 for (auto it = theValue.begin(); it != theValue.end(); ++it)
177 theAttribute->append(it->resultSubShapePair().first); // use only context
180 //--------------------------------------------------------------------------------------
181 void fillAttribute(const ModelHighAPI_Selection & theValue,
182 const std::shared_ptr<ModelAPI_AttributeSelection> & theAttribute)
184 theValue.fillAttribute(theAttribute);
187 //--------------------------------------------------------------------------------------
188 void fillAttribute(const std::list<ModelHighAPI_Selection> & theValue,
189 const std::shared_ptr<ModelAPI_AttributeSelectionList> & theAttribute)
191 theAttribute->clear();
193 if(!theValue.empty()) {
194 std::string aSelectionType;
195 const ModelHighAPI_Selection& aSelection = theValue.front();
196 theAttribute->setSelectionType(aSelection.shapeType());
199 for (auto it = theValue.begin(); it != theValue.end(); ++it)
200 it->appendToList(theAttribute);
203 //--------------------------------------------------------------------------------------
204 void fillAttribute(const std::string & theValue,
205 const std::shared_ptr<ModelAPI_AttributeString> & theAttribute)
207 theAttribute->setValue(theValue);
210 //--------------------------------------------------------------------------------------
211 void fillAttribute(const char * theValue,
212 const std::shared_ptr<ModelAPI_AttributeString> & theAttribute)
214 theAttribute->setValue(theValue);
217 //--------------------------------------------------------------------------------------
218 void fillAttribute(const std::list<std::string> & theValue,
219 const std::shared_ptr<ModelAPI_AttributeStringArray> & theAttribute)
221 theAttribute->setSize(int(theValue.size()));
224 for (auto it = theValue.begin(); it != theValue.end(); ++it, ++anIndex)
225 theAttribute->setValue(anIndex, *it);
228 //--------------------------------------------------------------------------------------
229 void fillAttribute(const std::list<ModelHighAPI_Integer> & theValue,
230 const std::shared_ptr<ModelAPI_AttributeIntArray> & theAttribute)
232 theAttribute->setSize(int(theValue.size()));
235 for (auto it = theValue.begin(); it != theValue.end(); ++it, ++anIndex)
236 theAttribute->setValue(anIndex, it->intValue()); // use only values, no text support in array
239 //==================================================================================================
240 GeomAPI_Shape::ShapeType shapeTypeByStr(std::string theShapeTypeStr)
242 GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
244 std::transform(theShapeTypeStr.begin(), theShapeTypeStr.end(),
245 theShapeTypeStr.begin(), ::tolower);
247 if(theShapeTypeStr == "compound") {
248 aShapeType = GeomAPI_Shape::COMPOUND;
249 } else if(theShapeTypeStr == "compsolid") {
250 aShapeType = GeomAPI_Shape::COMPSOLID;
251 } else if(theShapeTypeStr == "solid") {
252 aShapeType = GeomAPI_Shape::SOLID;
253 } else if(theShapeTypeStr == "shell") {
254 aShapeType = GeomAPI_Shape::SHELL;
255 } else if(theShapeTypeStr == "face") {
256 aShapeType = GeomAPI_Shape::FACE;
257 } else if(theShapeTypeStr == "wire") {
258 aShapeType = GeomAPI_Shape::WIRE;
259 } else if(theShapeTypeStr == "edge") {
260 aShapeType = GeomAPI_Shape::EDGE;
261 } else if(theShapeTypeStr == "vertex") {
262 aShapeType = GeomAPI_Shape::VERTEX;
263 } else if(theShapeTypeStr == "shape") {
264 aShapeType = GeomAPI_Shape::SHAPE;
270 //==================================================================================================
271 GeomAPI_Shape::ShapeType getShapeType(const ModelHighAPI_Selection& theSelection)
273 GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
275 switch(theSelection.variantType()) {
276 case ModelHighAPI_Selection::VT_ResultSubShapePair: {
277 ResultSubShapePair aPair = theSelection.resultSubShapePair();
278 GeomShapePtr aShape = aPair.second;
280 aShape = aPair.first->shape();
285 aShapeType = aShape->shapeType();
288 case ModelHighAPI_Selection::VT_TypeSubShapeNamePair: {
289 TypeSubShapeNamePair aPair = theSelection.typeSubShapeNamePair();
290 std::string aType = aPair.first;
291 aShapeType = shapeTypeByStr(aType);
299 //--------------------------------------------------------------------------------------
300 ModelAPI_AttributeTables::ValueType valueTypeByStr(const std::string& theValueTypeStr)
302 std::string aType = theValueTypeStr;
303 std::transform(aType.begin(), aType.end(), aType.begin(), ::tolower);
304 if (aType == "boolean")
305 return ModelAPI_AttributeTables::BOOLEAN;
306 else if (aType == "integer")
307 return ModelAPI_AttributeTables::INTEGER;
308 else if (aType == "string")
309 return ModelAPI_AttributeTables::STRING;
310 return ModelAPI_AttributeTables::DOUBLE; // default
313 //--------------------------------------------------------------------------------------
314 std::string strByValueType(const ModelAPI_AttributeTables::ValueType theType)
317 case ModelAPI_AttributeTables::BOOLEAN: return "BOOLEAN";
318 case ModelAPI_AttributeTables::INTEGER: return "INTEGER";
319 case ModelAPI_AttributeTables::DOUBLE: return "DOUBLE";
320 case ModelAPI_AttributeTables::STRING: return "STRING";
322 return ""; // bad case
325 /// stores the features information, recoursively stores sub-documetns features
326 std::string storeFeatures(const std::string& theDocName, DocumentPtr theDoc,
327 std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> >& theStore,
328 const bool theCompare) // if false => store
330 std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> >::iterator aDocFind;
332 aDocFind = theStore.find(theDocName);
333 if (aDocFind == theStore.end()) {
334 return "Document '" + theDocName + "' not found";
337 // store the model features information: iterate all features
338 int aFeaturesCount = 0; // stores the number of compared features for this document to compate
339 std::set<std::string> aProcessed; // processed features names (that are in the current document)
340 std::list<FeaturePtr> allFeatures = theDoc->allFeatures();
341 std::list<FeaturePtr>::iterator allIter = allFeatures.begin();
342 for(; allIter != allFeatures.end(); allIter++) {
343 FeaturePtr aFeat = *allIter;
345 std::map<std::string, ModelHighAPI_FeatureStore>::iterator
346 aFeatFind = aDocFind->second.find(aFeat->name());
347 if (aFeatFind == aDocFind->second.end()) {
348 return "Document '" + theDocName + "' feature '" + aFeat->name() + "' not found";
350 std::string anError = aFeatFind->second.compare(aFeat);
351 if (!anError.empty()) {
352 anError = "Document " + theDocName + " " + anError;
356 aProcessed.insert(aFeat->name());
358 theStore[theDocName][aFeat->name()] = ModelHighAPI_FeatureStore(aFeat);
360 // iterate all results of this feature
361 std::list<ResultPtr> allResults;
362 ModelAPI_Tools::allResults(aFeat, allResults);
363 std::list<ResultPtr>::iterator aRes = allResults.begin();
364 for(; aRes != allResults.end(); aRes++) {
365 // recoursively store features of sub-documents
366 if ((*aRes)->groupName() == ModelAPI_ResultPart::group()) {
367 DocumentPtr aDoc = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes)->partDoc();
369 std::string anError = storeFeatures((*aRes)->data()->name(), aDoc, theStore, theCompare);
370 if (!anError.empty())
376 // checks the number of compared features
378 if (aDocFind->second.size() != aFeaturesCount) {
379 // search for disappeared feature
380 std::string aLostName;
381 std::map<std::string, ModelHighAPI_FeatureStore>::iterator aLostIter;
382 for(aLostIter = aDocFind->second.begin(); aLostIter != aDocFind->second.end(); aLostIter++) {
383 if (aProcessed.find(aLostIter->first) == aProcessed.end()) {
384 aLostName = aLostIter->first;
387 return "For document '" + theDocName +
388 "' the number of features is decreased, there is no feature '" + aLostName + "'";
394 //==================================================================================================
395 bool checkPythonDump()
397 SessionPtr aSession = ModelAPI_Session::get();
398 // dump all to the python file
399 aSession->startOperation("Check python dump");
400 FeaturePtr aDump = aSession->moduleDocument()->addFeature("Dump");
402 aDump->string("file_path")->setValue("check_dump.py"); // to the current folder
403 aDump->string("file_format")->setValue("py"); // to the current folder
406 bool isProblem = !aDump.get() || !aDump->error().empty(); // after "finish" dump will be removed
407 if (isProblem && aDump.get()) {
408 std::cout<<"Dump feature error "<<aDump->error()<<std::endl;
409 Events_InfoMessage anErrorMsg(std::string("checkPythonDump"), aDump->error());
412 aSession->finishOperation();
414 return false; // something is wrong during dump
417 // map from document name to feature name to feature data
418 std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> > aStore;
419 std::string anError = storeFeatures(
420 aSession->moduleDocument()->kind(), aSession->moduleDocument(), aStore, false);
421 if (!anError.empty()) {
422 Events_InfoMessage anErrorMsg(std::string("checkPythonDump"), anError);
426 // close all before importation of the script
427 aSession->closeAll();
428 // execute the dumped
429 PyGILState_STATE gstate = PyGILState_Ensure(); /* acquire python thread */
430 PyObject* PyFileObject = PyFile_FromString("./check_dump.py", "r");
431 PyRun_SimpleFileEx(PyFile_AsFile(PyFileObject), "./check_dump.py", 1);
432 PyGILState_Release(gstate); /* release python thread */
434 // compare with the stored data
435 anError = storeFeatures(
436 aSession->moduleDocument()->kind(), aSession->moduleDocument(), aStore, true);
437 if (!anError.empty()) {
438 std::cout<<anError<<std::endl;
439 Events_InfoMessage anErrorMsg(std::string("checkPythonDump"), anError);
447 //--------------------------------------------------------------------------------------