1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2 // Name : ModelHighAPI_Tools.cpp
6 // 07/06/16 - Sergey POKHODENKO - Creation of the file
8 //--------------------------------------------------------------------------------------
9 #include "ModelHighAPI_Tools.h"
10 #include <ModelHighAPI_FeatureStore.h>
11 //--------------------------------------------------------------------------------------
12 #include <GeomAPI_Dir.h>
13 #include <GeomAPI_Pnt.h>
14 #include <GeomAPI_Pnt2d.h>
15 //--------------------------------------------------------------------------------------
16 #include <GeomDataAPI_Dir.h>
17 #include <GeomDataAPI_Point.h>
18 #include <GeomDataAPI_Point2D.h>
19 //--------------------------------------------------------------------------------------
20 #include <ModelAPI_AttributeBoolean.h>
21 #include <ModelAPI_AttributeDocRef.h>
22 #include <ModelAPI_AttributeDouble.h>
23 #include <ModelAPI_AttributeIntArray.h>
24 #include <ModelAPI_AttributeInteger.h>
25 #include <ModelAPI_AttributeRefAttr.h>
26 #include <ModelAPI_AttributeRefAttrList.h>
27 #include <ModelAPI_AttributeReference.h>
28 #include <ModelAPI_AttributeRefList.h>
29 #include <ModelAPI_AttributeSelection.h>
30 #include <ModelAPI_AttributeSelectionList.h>
31 #include <ModelAPI_AttributeString.h>
32 #include <ModelAPI_AttributeStringArray.h>
33 #include <ModelAPI_AttributeDoubleArray.h>
34 #include <ModelAPI_Session.h>
35 #include <ModelAPI_Tools.h>
36 #include <ModelAPI_ResultPart.h>
37 //--------------------------------------------------------------------------------------
38 #include <Config_ModuleReader.h>
39 //--------------------------------------------------------------------------------------
40 #include "ModelHighAPI_Double.h"
41 #include "ModelHighAPI_Integer.h"
42 #include "ModelHighAPI_RefAttr.h"
43 #include "ModelHighAPI_Reference.h"
44 #include "ModelHighAPI_Selection.h"
46 #include <Events_InfoMessage.h>
48 // Have to be included before std headers
54 //--------------------------------------------------------------------------------------
55 void fillAttribute(const std::shared_ptr<GeomAPI_Pnt2d> & theValue,
56 const std::shared_ptr<GeomDataAPI_Point2D> & theAttribute)
58 theAttribute->setValue(theValue);
61 void fillAttribute(const std::shared_ptr<GeomDataAPI_Point2D> & theAttribute,
62 double theX, double theY)
64 theAttribute->setValue(theX, theY);
67 //--------------------------------------------------------------------------------------
68 void fillAttribute(const std::shared_ptr<GeomAPI_Dir> & theValue,
69 const std::shared_ptr<GeomDataAPI_Dir> & theAttribute)
71 theAttribute->setValue(theValue);
74 //--------------------------------------------------------------------------------------
75 void fillAttribute(const std::shared_ptr<GeomAPI_Pnt> & theValue,
76 const std::shared_ptr<GeomDataAPI_Point> & theAttribute)
78 theAttribute->setValue(theValue);
81 //--------------------------------------------------------------------------------------
82 void fillAttribute(bool theValue,
83 const std::shared_ptr<ModelAPI_AttributeBoolean> & theAttribute)
85 theAttribute->setValue(theValue);
88 //--------------------------------------------------------------------------------------
89 void fillAttribute(const ModelHighAPI_Double & theValue,
90 const std::shared_ptr<ModelAPI_AttributeDouble> & theAttribute)
92 theValue.fillAttribute(theAttribute);
94 void fillAttribute(double theValue,
95 const std::shared_ptr<ModelAPI_AttributeDouble> & theAttribute)
97 theAttribute->setValue(theValue);
100 //--------------------------------------------------------------------------------------
101 void fillAttribute(const ModelHighAPI_Integer & theValue,
102 const std::shared_ptr<ModelAPI_AttributeInteger> & theAttribute)
104 theValue.fillAttribute(theAttribute);
106 void fillAttribute(int theValue,
107 const std::shared_ptr<ModelAPI_AttributeInteger> & theAttribute)
109 theAttribute->setValue(theValue);
112 //--------------------------------------------------------------------------------------
113 void fillAttribute(const ModelHighAPI_RefAttr & theValue,
114 const std::shared_ptr<ModelAPI_AttributeRefAttr> & theAttribute)
116 theValue.fillAttribute(theAttribute);
119 //--------------------------------------------------------------------------------------
120 void fillAttribute(const std::list<ModelHighAPI_RefAttr> & theValue,
121 const std::shared_ptr<ModelAPI_AttributeRefAttrList> & theAttribute)
123 theAttribute->clear();
124 for (auto it = theValue.begin(); it != theValue.end(); ++it)
125 it->appendToList(theAttribute);
128 //--------------------------------------------------------------------------------------
129 void fillAttribute(const ModelHighAPI_Reference & theValue,
130 const std::shared_ptr<ModelAPI_AttributeReference> & theAttribute)
132 theValue.fillAttribute(theAttribute);
135 //--------------------------------------------------------------------------------------
136 void fillAttribute(const std::list<ModelHighAPI_Reference> & theValue,
137 const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
139 theAttribute->clear();
140 for (auto it = theValue.begin(); it != theValue.end(); ++it)
141 it->appendToList(theAttribute);
144 //--------------------------------------------------------------------------------------
145 void fillAttribute(const std::shared_ptr<ModelAPI_Object> & theValue,
146 const std::shared_ptr<ModelAPI_AttributeReference> & theAttribute)
148 theAttribute->setValue(theValue);
151 //--------------------------------------------------------------------------------------
152 void fillAttribute(const std::list<std::shared_ptr<ModelAPI_Object> > & theValue,
153 const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
155 theAttribute->clear();
156 for (auto it = theValue.begin(); it != theValue.end(); ++it)
157 theAttribute->append(*it);
161 void fillAttribute(const std::list<ModelHighAPI_Selection> & theValue,
162 const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
164 theAttribute->clear();
165 for (auto it = theValue.begin(); it != theValue.end(); ++it)
166 theAttribute->append(it->resultSubShapePair().first); // use only context
169 //--------------------------------------------------------------------------------------
170 void fillAttribute(const ModelHighAPI_Selection & theValue,
171 const std::shared_ptr<ModelAPI_AttributeSelection> & theAttribute)
173 theValue.fillAttribute(theAttribute);
176 //--------------------------------------------------------------------------------------
177 void fillAttribute(const std::list<ModelHighAPI_Selection> & theValue,
178 const std::shared_ptr<ModelAPI_AttributeSelectionList> & theAttribute)
180 theAttribute->clear();
182 if(!theValue.empty()) {
183 std::string aSelectionType;
184 const ModelHighAPI_Selection& aSelection = theValue.front();
185 theAttribute->setSelectionType(aSelection.shapeType());
188 for (auto it = theValue.begin(); it != theValue.end(); ++it)
189 it->appendToList(theAttribute);
192 //--------------------------------------------------------------------------------------
193 void fillAttribute(const std::string & theValue,
194 const std::shared_ptr<ModelAPI_AttributeString> & theAttribute)
196 theAttribute->setValue(theValue);
199 //--------------------------------------------------------------------------------------
200 void fillAttribute(const char * theValue,
201 const std::shared_ptr<ModelAPI_AttributeString> & theAttribute)
203 theAttribute->setValue(theValue);
206 //--------------------------------------------------------------------------------------
207 void fillAttribute(const std::list<std::string> & theValue,
208 const std::shared_ptr<ModelAPI_AttributeStringArray> & theAttribute)
210 theAttribute->setSize(int(theValue.size()));
213 for (auto it = theValue.begin(); it != theValue.end(); ++it, ++anIndex)
214 theAttribute->setValue(anIndex, *it);
217 //--------------------------------------------------------------------------------------
218 void fillAttribute(const std::list<ModelHighAPI_Integer> & theValue,
219 const std::shared_ptr<ModelAPI_AttributeIntArray> & theAttribute)
221 theAttribute->setSize(int(theValue.size()));
224 for (auto it = theValue.begin(); it != theValue.end(); ++it, ++anIndex)
225 theAttribute->setValue(anIndex, it->intValue()); // use only values, no text support in array
228 //==================================================================================================
229 GeomAPI_Shape::ShapeType shapeTypeByStr(std::string theShapeTypeStr)
231 GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
233 std::transform(theShapeTypeStr.begin(), theShapeTypeStr.end(),
234 theShapeTypeStr.begin(), ::tolower);
236 if(theShapeTypeStr == "compound") {
237 aShapeType = GeomAPI_Shape::COMPOUND;
238 } else if(theShapeTypeStr == "compsolid") {
239 aShapeType = GeomAPI_Shape::COMPSOLID;
240 } else if(theShapeTypeStr == "solid") {
241 aShapeType = GeomAPI_Shape::SOLID;
242 } else if(theShapeTypeStr == "shell") {
243 aShapeType = GeomAPI_Shape::SHELL;
244 } else if(theShapeTypeStr == "face") {
245 aShapeType = GeomAPI_Shape::FACE;
246 } else if(theShapeTypeStr == "wire") {
247 aShapeType = GeomAPI_Shape::WIRE;
248 } else if(theShapeTypeStr == "edge") {
249 aShapeType = GeomAPI_Shape::EDGE;
250 } else if(theShapeTypeStr == "vertex") {
251 aShapeType = GeomAPI_Shape::VERTEX;
252 } else if(theShapeTypeStr == "shape") {
253 aShapeType = GeomAPI_Shape::SHAPE;
259 //==================================================================================================
260 GeomAPI_Shape::ShapeType getShapeType(const ModelHighAPI_Selection& theSelection)
262 GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
264 switch(theSelection.variantType()) {
265 case ModelHighAPI_Selection::VT_ResultSubShapePair: {
266 ResultSubShapePair aPair = theSelection.resultSubShapePair();
267 GeomShapePtr aShape = aPair.second;
269 aShape = aPair.first->shape();
274 aShapeType = aShape->shapeType();
277 case ModelHighAPI_Selection::VT_TypeSubShapeNamePair: {
278 TypeSubShapeNamePair aPair = theSelection.typeSubShapeNamePair();
279 std::string aType = aPair.first;
280 aShapeType = shapeTypeByStr(aType);
288 //--------------------------------------------------------------------------------------
289 ModelAPI_AttributeTables::ValueType valueTypeByStr(const std::string& theValueTypeStr)
291 std::string aType = theValueTypeStr;
292 std::transform(aType.begin(), aType.end(), aType.begin(), ::tolower);
293 if (aType == "boolean")
294 return ModelAPI_AttributeTables::BOOLEAN;
295 else if (aType == "integer")
296 return ModelAPI_AttributeTables::INTEGER;
297 else if (aType == "string")
298 return ModelAPI_AttributeTables::STRING;
299 return ModelAPI_AttributeTables::DOUBLE; // default
302 //--------------------------------------------------------------------------------------
303 std::string strByValueType(const ModelAPI_AttributeTables::ValueType theType)
306 case ModelAPI_AttributeTables::BOOLEAN: return "BOOLEAN";
307 case ModelAPI_AttributeTables::INTEGER: return "INTEGER";
308 case ModelAPI_AttributeTables::DOUBLE: return "DOUBLE";
309 case ModelAPI_AttributeTables::STRING: return "STRING";
311 return ""; // bad case
314 /// stores the features information, recoursively stores sub-documetns features
315 std::string storeFeatures(const std::string& theDocName, DocumentPtr theDoc,
316 std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> >& theStore,
317 const bool theCompare) // if false => store
319 std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> >::iterator aDocFind;
321 aDocFind = theStore.find(theDocName);
322 if (aDocFind == theStore.end()) {
323 return "Document '" + theDocName + "' not found";
326 // store the model features information: iterate all features
327 int aFeaturesCount = 0; // stores the number of compared features for this document to compate
328 std::set<std::string> aProcessed; // processed features names (that are in the current document)
329 std::list<FeaturePtr> allFeatures = theDoc->allFeatures();
330 std::list<FeaturePtr>::iterator allIter = allFeatures.begin();
331 for(; allIter != allFeatures.end(); allIter++) {
332 FeaturePtr aFeat = *allIter;
334 std::map<std::string, ModelHighAPI_FeatureStore>::iterator
335 aFeatFind = aDocFind->second.find(aFeat->name());
336 if (aFeatFind == aDocFind->second.end()) {
337 return "Document '" + theDocName + "' feature '" + aFeat->name() + "' not found";
339 std::string anError = aFeatFind->second.compare(aFeat);
340 if (!anError.empty()) {
344 aProcessed.insert(aFeat->name());
346 theStore[theDocName][aFeat->name()] = ModelHighAPI_FeatureStore(aFeat);
348 // iterate all results of this feature
349 std::list<ResultPtr> allResults;
350 ModelAPI_Tools::allResults(aFeat, allResults);
351 std::list<ResultPtr>::iterator aRes = allResults.begin();
352 for(; aRes != allResults.end(); aRes++) {
353 // recoursively store features of sub-documents
354 if ((*aRes)->groupName() == ModelAPI_ResultPart::group()) {
355 DocumentPtr aDoc = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes)->partDoc();
357 std::string anError = storeFeatures((*aRes)->data()->name(), aDoc, theStore, theCompare);
358 if (!anError.empty())
364 // checks the number of compared features
366 if (aDocFind->second.size() != aFeaturesCount) {
367 // search for disappeared feature
368 std::string aLostName;
369 std::map<std::string, ModelHighAPI_FeatureStore>::iterator aLostIter;
370 for(aLostIter = aDocFind->second.begin(); aLostIter != aDocFind->second.end(); aLostIter++) {
371 if (aProcessed.find(aLostIter->first) == aProcessed.end()) {
372 aLostName = aLostIter->first;
375 return "For document '" + theDocName +
376 "' the number of features is decreased, there is no feature '" + aLostName + "'";
382 //==================================================================================================
383 bool checkPythonDump()
385 SessionPtr aSession = ModelAPI_Session::get();
386 // dump all to the python file
387 aSession->startOperation("Check python dump");
388 FeaturePtr aDump = aSession->moduleDocument()->addFeature("Dump");
390 aDump->string("file_path")->setValue("check_dump.py"); // to the current folder
391 aDump->string("file_format")->setValue("py"); // to the current folder
394 bool isProblem = !aDump.get() || !aDump->error().empty(); // after "finish" dump will be removed
395 aSession->finishOperation();
397 return false; // something is wrong during dump
399 // map from document name to feature name to feature data
400 std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> > aStore;
401 std::string anError = storeFeatures(
402 aSession->moduleDocument()->kind(), aSession->moduleDocument(), aStore, false);
403 if (!anError.empty()) {
404 Events_InfoMessage anErrorMsg(std::string("checkPythonDump"), anError);
408 // close all before importation of the script
409 aSession->closeAll();
410 // execute the dumped
411 PyGILState_STATE gstate = PyGILState_Ensure(); /* acquire python thread */
412 PyObject* PyFileObject = PyFile_FromString("./check_dump.py", "r");
413 PyRun_SimpleFileEx(PyFile_AsFile(PyFileObject), "./check_dump.py", 1);
414 PyGILState_Release(gstate); /* release python thread */
416 // compare with the stored data
417 anError = storeFeatures(
418 aSession->moduleDocument()->kind(), aSession->moduleDocument(), aStore, true);
419 if (!anError.empty()) {
420 std::cout<<anError<<std::endl;
421 Events_InfoMessage anErrorMsg(std::string("checkPythonDump"), anError);
429 //--------------------------------------------------------------------------------------