Salome HOME
abb75c38bb4ba6086c27985a56d5db18f005b569
[modules/shaper.git] / src / ModelHighAPI / ModelHighAPI_Tools.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2 // Name   : ModelHighAPI_Tools.cpp
3 // Purpose: 
4 //
5 // History:
6 // 07/06/16 - Sergey POKHODENKO - Creation of the file
7
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_AttributeDoubleArray.h>
33 #include <ModelAPI_Session.h>
34 #include <ModelAPI_Tools.h>
35 #include <ModelAPI_ResultPart.h>
36 //--------------------------------------------------------------------------------------
37 #include <Config_ModuleReader.h>
38 //--------------------------------------------------------------------------------------
39 #include "ModelHighAPI_Double.h"
40 #include "ModelHighAPI_Integer.h"
41 #include "ModelHighAPI_RefAttr.h"
42 #include "ModelHighAPI_Reference.h"
43 #include "ModelHighAPI_Selection.h"
44
45 #include <Events_InfoMessage.h>
46
47 // Have to be included before std headers
48 #include <Python.h>
49
50 #include <algorithm>
51 #include <iostream>
52
53 //--------------------------------------------------------------------------------------
54 void fillAttribute(const std::shared_ptr<GeomAPI_Pnt2d> & theValue,
55                    const std::shared_ptr<GeomDataAPI_Point2D> & theAttribute)
56 {
57   theAttribute->setValue(theValue);
58 }
59
60 void fillAttribute(const std::shared_ptr<GeomDataAPI_Point2D> & theAttribute,
61                    double theX, double theY)
62 {
63   theAttribute->setValue(theX, theY);
64 }
65
66 //--------------------------------------------------------------------------------------
67 void fillAttribute(const std::shared_ptr<GeomAPI_Dir> & theValue,
68                    const std::shared_ptr<GeomDataAPI_Dir> & theAttribute)
69 {
70   theAttribute->setValue(theValue);
71 }
72
73 //--------------------------------------------------------------------------------------
74 void fillAttribute(const std::shared_ptr<GeomAPI_Pnt> & theValue,
75                    const std::shared_ptr<GeomDataAPI_Point> & theAttribute)
76 {
77   theAttribute->setValue(theValue);
78 }
79
80 //--------------------------------------------------------------------------------------
81 void fillAttribute(bool theValue,
82                    const std::shared_ptr<ModelAPI_AttributeBoolean> & theAttribute)
83 {
84   theAttribute->setValue(theValue);
85 }
86
87 //--------------------------------------------------------------------------------------
88 void fillAttribute(const ModelHighAPI_Double & theValue,
89                    const std::shared_ptr<ModelAPI_AttributeDouble> & theAttribute)
90 {
91   theValue.fillAttribute(theAttribute);
92 }
93 void fillAttribute(double theValue,
94                    const std::shared_ptr<ModelAPI_AttributeDouble> & theAttribute)
95 {
96   theAttribute->setValue(theValue);
97 }
98
99 //--------------------------------------------------------------------------------------
100 void fillAttribute(const ModelHighAPI_Integer & theValue,
101                    const std::shared_ptr<ModelAPI_AttributeInteger> & theAttribute)
102 {
103   theValue.fillAttribute(theAttribute);
104 }
105 void fillAttribute(int theValue,
106                    const std::shared_ptr<ModelAPI_AttributeInteger> & theAttribute)
107 {
108   theAttribute->setValue(theValue);
109 }
110
111 //--------------------------------------------------------------------------------------
112 void fillAttribute(const ModelHighAPI_RefAttr & theValue,
113                    const std::shared_ptr<ModelAPI_AttributeRefAttr> & theAttribute)
114 {
115   theValue.fillAttribute(theAttribute);
116 }
117
118 //--------------------------------------------------------------------------------------
119 void fillAttribute(const std::list<ModelHighAPI_RefAttr> & theValue,
120                    const std::shared_ptr<ModelAPI_AttributeRefAttrList> & theAttribute)
121 {
122   theAttribute->clear();
123   for (auto it = theValue.begin(); it != theValue.end(); ++it)
124     it->appendToList(theAttribute);
125 }
126
127 //--------------------------------------------------------------------------------------
128 void fillAttribute(const ModelHighAPI_Reference & theValue,
129                    const std::shared_ptr<ModelAPI_AttributeReference> & theAttribute)
130 {
131   theValue.fillAttribute(theAttribute);
132 }
133
134 //--------------------------------------------------------------------------------------
135 void fillAttribute(const std::list<ModelHighAPI_Reference> & theValue,
136                    const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
137 {
138   theAttribute->clear();
139   for (auto it = theValue.begin(); it != theValue.end(); ++it)
140     it->appendToList(theAttribute);
141 }
142
143 //--------------------------------------------------------------------------------------
144 void fillAttribute(const std::shared_ptr<ModelAPI_Object> & theValue,
145                    const std::shared_ptr<ModelAPI_AttributeReference> & theAttribute)
146 {
147   theAttribute->setValue(theValue);
148 }
149
150 //--------------------------------------------------------------------------------------
151 void fillAttribute(const std::list<std::shared_ptr<ModelAPI_Object> > & theValue,
152                    const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
153 {
154   theAttribute->clear();
155   for (auto it = theValue.begin(); it != theValue.end(); ++it)
156     theAttribute->append(*it);
157 }
158
159 MODELHIGHAPI_EXPORT
160 void fillAttribute(const std::list<ModelHighAPI_Selection> & theValue,
161                    const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
162 {
163   theAttribute->clear();
164   for (auto it = theValue.begin(); it != theValue.end(); ++it)
165     theAttribute->append(it->resultSubShapePair().first); // use only context
166 }
167
168 //--------------------------------------------------------------------------------------
169 void fillAttribute(const ModelHighAPI_Selection & theValue,
170                    const std::shared_ptr<ModelAPI_AttributeSelection> & theAttribute)
171 {
172   theValue.fillAttribute(theAttribute);
173 }
174
175 //--------------------------------------------------------------------------------------
176 void fillAttribute(const std::list<ModelHighAPI_Selection> & theValue,
177                    const std::shared_ptr<ModelAPI_AttributeSelectionList> & theAttribute)
178 {
179   theAttribute->clear();
180
181   if(!theValue.empty()) {
182     std::string aSelectionType;
183     const ModelHighAPI_Selection& aSelection = theValue.front();
184     theAttribute->setSelectionType(aSelection.shapeType());
185   }
186
187   for (auto it = theValue.begin(); it != theValue.end(); ++it)
188     it->appendToList(theAttribute);
189 }
190
191 //--------------------------------------------------------------------------------------
192 void fillAttribute(const std::string & theValue,
193                    const std::shared_ptr<ModelAPI_AttributeString> & theAttribute)
194 {
195   theAttribute->setValue(theValue);
196 }
197 void fillAttribute(const char * theValue,
198                    const std::shared_ptr<ModelAPI_AttributeString> & theAttribute)
199 {
200   theAttribute->setValue(theValue);
201 }
202
203 //==================================================================================================
204 GeomAPI_Shape::ShapeType shapeTypeByStr(std::string theShapeTypeStr)
205 {
206   GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
207
208   std::transform(theShapeTypeStr.begin(), theShapeTypeStr.end(), 
209                  theShapeTypeStr.begin(), ::tolower);
210
211   if(theShapeTypeStr == "compound") {
212     aShapeType = GeomAPI_Shape::COMPOUND;
213   } else if(theShapeTypeStr == "compsolid") {
214     aShapeType = GeomAPI_Shape::COMPSOLID;
215   } else if(theShapeTypeStr == "solid") {
216     aShapeType = GeomAPI_Shape::SOLID;
217   } else if(theShapeTypeStr == "shell") {
218     aShapeType = GeomAPI_Shape::SHELL;
219   } else if(theShapeTypeStr == "face") {
220     aShapeType = GeomAPI_Shape::FACE;
221   } else if(theShapeTypeStr == "wire") {
222     aShapeType = GeomAPI_Shape::WIRE;
223   } else if(theShapeTypeStr == "edge") {
224     aShapeType = GeomAPI_Shape::EDGE;
225   } else if(theShapeTypeStr == "vertex") {
226     aShapeType = GeomAPI_Shape::VERTEX;
227   } else if(theShapeTypeStr == "shape") {
228     aShapeType = GeomAPI_Shape::SHAPE;
229   }
230
231   return aShapeType;
232 }
233
234 //==================================================================================================
235 GeomAPI_Shape::ShapeType getShapeType(const ModelHighAPI_Selection& theSelection)
236 {
237   GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
238
239   switch(theSelection.variantType()) {
240     case ModelHighAPI_Selection::VT_ResultSubShapePair: {
241       ResultSubShapePair aPair = theSelection.resultSubShapePair();
242       GeomShapePtr aShape = aPair.second;
243       if(!aShape.get()) {
244         aShape = aPair.first->shape();
245       }
246       if(!aShape.get()) {
247         return aShapeType;
248       }
249       aShapeType = aShape->shapeType();
250       break;
251     }
252     case ModelHighAPI_Selection::VT_TypeSubShapeNamePair: {
253       TypeSubShapeNamePair aPair = theSelection.typeSubShapeNamePair();
254       std::string aType = aPair.first;
255       aShapeType = shapeTypeByStr(aType);
256       break;
257     }
258   }
259
260   return aShapeType;
261 }
262
263 /// stores the features information, recoursively stores sub-documetns features
264 std::string storeFeatures(const std::string& theDocName, DocumentPtr theDoc,
265   std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> >& theStore,
266   const bool theCompare) // if false => store
267 {
268   std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> >::iterator aDocFind;
269   if (theCompare) {
270      aDocFind = theStore.find(theDocName);
271      if (aDocFind == theStore.end()) {
272        return "Document '" + theDocName + "' not found";
273      }
274   }
275   // store the model features information: iterate all features
276   int aFeaturesCount = 0; // stores the number of compared features for this document to compate
277   std::set<std::string> aProcessed; // processed features names (that are in the current document)
278   std::list<FeaturePtr> allFeatures = theDoc->allFeatures();
279   std::list<FeaturePtr>::iterator allIter = allFeatures.begin();
280   for(; allIter != allFeatures.end(); allIter++) {
281     FeaturePtr aFeat = *allIter;
282     if (theCompare) {
283       std::map<std::string, ModelHighAPI_FeatureStore>::iterator 
284         aFeatFind = aDocFind->second.find(aFeat->name());
285       if (aFeatFind == aDocFind->second.end()) {
286         return "Document '" + theDocName + "' feature '" + aFeat->name() + "' not found";
287       }
288       std::string anError = aFeatFind->second.compare(aFeat);
289       if (!anError.empty()) {
290         return anError;
291       }
292       aFeaturesCount++;
293       aProcessed.insert(aFeat->name());
294     } else {
295       theStore[theDocName][aFeat->name()] = ModelHighAPI_FeatureStore(aFeat);
296     }
297     // iterate all results of this feature
298     std::list<ResultPtr> allResults;
299     ModelAPI_Tools::allResults(aFeat, allResults);
300     std::list<ResultPtr>::iterator aRes = allResults.begin();
301     for(; aRes != allResults.end(); aRes++) {
302       // recoursively store features of sub-documents
303       if ((*aRes)->groupName() == ModelAPI_ResultPart::group()) { 
304         DocumentPtr aDoc = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes)->partDoc();
305         if (aDoc.get()) {
306           std::string anError = storeFeatures((*aRes)->data()->name(), aDoc, theStore, theCompare);
307           if (!anError.empty())
308             return anError;
309         }
310       }
311     }
312   }
313   // checks the number of compared features
314   if (theCompare) {
315     if (aDocFind->second.size() != aFeaturesCount) {
316       // search for disappeared feature
317       std::string aLostName;
318       std::map<std::string, ModelHighAPI_FeatureStore>::iterator aLostIter;
319       for(aLostIter = aDocFind->second.begin(); aLostIter != aDocFind->second.end(); aLostIter++) {
320         if (aProcessed.find(aLostIter->first) == aProcessed.end()) {
321           aLostName = aLostIter->first;
322         }
323       }
324       return "For document '" + theDocName + 
325         "' the number of features is decreased, there is no feature '" + aLostName + "'";
326     }
327   }
328   return ""; // ok
329 }
330
331 //==================================================================================================
332 bool checkPythonDump()
333 {
334   SessionPtr aSession = ModelAPI_Session::get();
335   // dump all to the python file
336   aSession->startOperation("Check python dump");
337   FeaturePtr aDump = aSession->moduleDocument()->addFeature("Dump");
338   if (aDump.get()) {
339     aDump->string("file_path")->setValue("check_dump.py"); // to the current folder
340     aDump->string("file_format")->setValue("py"); // to the current folder
341     aDump->execute();
342   }
343   bool isProblem = !aDump.get() || !aDump->error().empty(); // after "finish" dump will be removed
344   aSession->finishOperation();
345   if (isProblem)
346     return false; // something is wrong during dump
347
348    // map from document name to feature name to feature data
349   std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> > aStore;
350   std::string anError = storeFeatures(
351     aSession->moduleDocument()->kind(), aSession->moduleDocument(), aStore, false);
352   if (!anError.empty()) {
353     Events_InfoMessage anErrorMsg(std::string("checkPythonDump"), anError);
354     anErrorMsg.send();
355     return false;
356   }
357   // close all before importation of the script
358   aSession->closeAll();
359   // execute the dumped
360   PyGILState_STATE gstate = PyGILState_Ensure(); /* acquire python thread */
361   PyObject* PyFileObject = PyFile_FromString("./check_dump.py", "r");
362   PyRun_SimpleFileEx(PyFile_AsFile(PyFileObject), "./check_dump.py", 1);
363   PyGILState_Release(gstate); /* release python thread */
364
365   // compare with the stored data
366   anError = storeFeatures(
367     aSession->moduleDocument()->kind(), aSession->moduleDocument(), aStore, true);
368   if (!anError.empty()) {
369     std::cout<<anError<<std::endl;
370     Events_InfoMessage anErrorMsg(std::string("checkPythonDump"), anError);
371     anErrorMsg.send();
372     return false;
373   }
374
375   return true;
376 }
377
378 //--------------------------------------------------------------------------------------