Salome HOME
Merge branch 'Pre_2.8.0_development'
[modules/shaper.git] / src / ModelHighAPI / ModelHighAPI_Tools.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
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"
57
58 #include <Events_InfoMessage.h>
59
60 // Have to be included before std headers
61 #include <Python.h>
62
63 #include <algorithm>
64 #include <iostream>
65
66 //--------------------------------------------------------------------------------------
67 void fillAttribute(const std::shared_ptr<GeomAPI_Pnt2d> & theValue,
68                    const std::shared_ptr<GeomDataAPI_Point2D> & theAttribute)
69 {
70   theAttribute->setValue(theValue);
71 }
72
73 void fillAttribute(const std::shared_ptr<GeomDataAPI_Point2D> & theAttribute,
74                    double theX, double theY)
75 {
76   theAttribute->setValue(theX, theY);
77 }
78
79 //--------------------------------------------------------------------------------------
80 void fillAttribute(const std::shared_ptr<GeomAPI_Dir> & theValue,
81                    const std::shared_ptr<GeomDataAPI_Dir> & theAttribute)
82 {
83   theAttribute->setValue(theValue);
84 }
85
86 //--------------------------------------------------------------------------------------
87 void fillAttribute(const std::shared_ptr<GeomAPI_Pnt> & theValue,
88                    const std::shared_ptr<GeomDataAPI_Point> & theAttribute)
89 {
90   theAttribute->setValue(theValue);
91 }
92
93 //--------------------------------------------------------------------------------------
94 void fillAttribute(bool theValue,
95                    const std::shared_ptr<ModelAPI_AttributeBoolean> & theAttribute)
96 {
97   theAttribute->setValue(theValue);
98 }
99
100 //--------------------------------------------------------------------------------------
101 void fillAttribute(const ModelHighAPI_Double & theValue,
102                    const std::shared_ptr<ModelAPI_AttributeDouble> & theAttribute)
103 {
104   theValue.fillAttribute(theAttribute);
105 }
106 void fillAttribute(double theValue,
107                    const std::shared_ptr<ModelAPI_AttributeDouble> & theAttribute)
108 {
109   theAttribute->setValue(theValue);
110 }
111
112 //--------------------------------------------------------------------------------------
113 void fillAttribute(const ModelHighAPI_Integer & theValue,
114                    const std::shared_ptr<ModelAPI_AttributeInteger> & theAttribute)
115 {
116   theValue.fillAttribute(theAttribute);
117 }
118 void fillAttribute(int theValue,
119                    const std::shared_ptr<ModelAPI_AttributeInteger> & theAttribute)
120 {
121   theAttribute->setValue(theValue);
122 }
123
124 //--------------------------------------------------------------------------------------
125 void fillAttribute(const ModelHighAPI_RefAttr & theValue,
126                    const std::shared_ptr<ModelAPI_AttributeRefAttr> & theAttribute)
127 {
128   theValue.fillAttribute(theAttribute);
129 }
130
131 //--------------------------------------------------------------------------------------
132 void fillAttribute(const std::list<ModelHighAPI_RefAttr> & theValue,
133                    const std::shared_ptr<ModelAPI_AttributeRefAttrList> & theAttribute)
134 {
135   theAttribute->clear();
136   for (auto it = theValue.begin(); it != theValue.end(); ++it)
137     it->appendToList(theAttribute);
138 }
139
140 //--------------------------------------------------------------------------------------
141 void fillAttribute(const ModelHighAPI_Reference & theValue,
142                    const std::shared_ptr<ModelAPI_AttributeReference> & theAttribute)
143 {
144   theValue.fillAttribute(theAttribute);
145 }
146
147 //--------------------------------------------------------------------------------------
148 void fillAttribute(const std::list<ModelHighAPI_Reference> & theValue,
149                    const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
150 {
151   theAttribute->clear();
152   for (auto it = theValue.begin(); it != theValue.end(); ++it)
153     it->appendToList(theAttribute);
154 }
155
156 //--------------------------------------------------------------------------------------
157 void fillAttribute(const std::shared_ptr<ModelAPI_Object> & theValue,
158                    const std::shared_ptr<ModelAPI_AttributeReference> & theAttribute)
159 {
160   theAttribute->setValue(theValue);
161 }
162
163 //--------------------------------------------------------------------------------------
164 void fillAttribute(const std::list<std::shared_ptr<ModelAPI_Object> > & theValue,
165                    const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
166 {
167   theAttribute->clear();
168   for (auto it = theValue.begin(); it != theValue.end(); ++it)
169     theAttribute->append(*it);
170 }
171
172 MODELHIGHAPI_EXPORT
173 void fillAttribute(const std::list<ModelHighAPI_Selection> & theValue,
174                    const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
175 {
176   theAttribute->clear();
177   for (auto it = theValue.begin(); it != theValue.end(); ++it)
178     theAttribute->append(it->resultSubShapePair().first); // use only context
179 }
180
181 //--------------------------------------------------------------------------------------
182 void fillAttribute(const ModelHighAPI_Selection & theValue,
183                    const std::shared_ptr<ModelAPI_AttributeSelection> & theAttribute)
184 {
185   theValue.fillAttribute(theAttribute);
186 }
187
188 //--------------------------------------------------------------------------------------
189 void fillAttribute(const std::list<ModelHighAPI_Selection> & theValue,
190                    const std::shared_ptr<ModelAPI_AttributeSelectionList> & theAttribute)
191 {
192   theAttribute->clear();
193
194   if(!theValue.empty()) {
195     std::string aSelectionType;
196     const ModelHighAPI_Selection& aSelection = theValue.front();
197     theAttribute->setSelectionType(aSelection.shapeType());
198   }
199
200   for (auto it = theValue.begin(); it != theValue.end(); ++it)
201     it->appendToList(theAttribute);
202 }
203
204 //--------------------------------------------------------------------------------------
205 void fillAttribute(const std::string & theValue,
206                    const std::shared_ptr<ModelAPI_AttributeString> & theAttribute)
207 {
208   theAttribute->setValue(theValue);
209 }
210
211 //--------------------------------------------------------------------------------------
212 void fillAttribute(const char * theValue,
213                    const std::shared_ptr<ModelAPI_AttributeString> & theAttribute)
214 {
215   theAttribute->setValue(theValue);
216 }
217
218 //--------------------------------------------------------------------------------------
219 void fillAttribute(const std::list<std::string> & theValue,
220                    const std::shared_ptr<ModelAPI_AttributeStringArray> & theAttribute)
221 {
222   theAttribute->setSize(int(theValue.size()));
223
224   int anIndex = 0;
225   for (auto it = theValue.begin(); it != theValue.end(); ++it, ++anIndex)
226     theAttribute->setValue(anIndex, *it);
227 }
228
229 //--------------------------------------------------------------------------------------
230 void fillAttribute(const std::list<ModelHighAPI_Integer> & theValue,
231                    const std::shared_ptr<ModelAPI_AttributeIntArray> & theAttribute)
232 {
233   theAttribute->setSize(int(theValue.size()));
234
235   int anIndex = 0;
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
238 }
239
240 //==================================================================================================
241 GeomAPI_Shape::ShapeType shapeTypeByStr(std::string theShapeTypeStr)
242 {
243   GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
244
245   std::transform(theShapeTypeStr.begin(), theShapeTypeStr.end(),
246                  theShapeTypeStr.begin(), ::tolower);
247
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;
266   }
267
268   return aShapeType;
269 }
270
271 //==================================================================================================
272 GeomAPI_Shape::ShapeType getShapeType(const ModelHighAPI_Selection& theSelection)
273 {
274   GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
275
276   switch(theSelection.variantType()) {
277     case ModelHighAPI_Selection::VT_ResultSubShapePair: {
278       ResultSubShapePair aPair = theSelection.resultSubShapePair();
279       GeomShapePtr aShape = aPair.second;
280       if(!aShape.get()) {
281         aShape = aPair.first->shape();
282       }
283       if(!aShape.get()) {
284         return aShapeType;
285       }
286       aShapeType = aShape->shapeType();
287       break;
288     }
289     case ModelHighAPI_Selection::VT_TypeSubShapeNamePair: {
290       TypeSubShapeNamePair aPair = theSelection.typeSubShapeNamePair();
291       std::string aType = aPair.first;
292       aShapeType = shapeTypeByStr(aType);
293       break;
294     }
295   }
296
297   return aShapeType;
298 }
299
300 //--------------------------------------------------------------------------------------
301 ModelAPI_AttributeTables::ValueType valueTypeByStr(const std::string& theValueTypeStr)
302 {
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
312 }
313
314 //--------------------------------------------------------------------------------------
315 std::string strByValueType(const ModelAPI_AttributeTables::ValueType theType)
316 {
317   switch(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";
322   }
323   return ""; // bad case
324 }
325
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
330 {
331   std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> >::iterator aDocFind;
332   if (theCompare) {
333      aDocFind = theStore.find(theDocName);
334      if (aDocFind == theStore.end()) {
335        return "Document '" + theDocName + "' not found";
336      }
337   }
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;
345     if (theCompare) {
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";
350       }
351       std::string anError = aFeatFind->second.compare(aFeat);
352       if (!anError.empty()) {
353         anError = "Document " + theDocName + " " + anError;
354         return anError;
355       }
356       aFeaturesCount++;
357       aProcessed.insert(aFeat->name());
358     } else {
359       theStore[theDocName][aFeat->name()] = ModelHighAPI_FeatureStore(aFeat);
360     }
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();
369         if (aDoc.get()) {
370           std::string anError = storeFeatures((*aRes)->data()->name(), aDoc, theStore, theCompare);
371           if (!anError.empty())
372             return anError;
373         }
374       }
375     }
376   }
377   // checks the number of compared features
378   if (theCompare) {
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;
386         }
387       }
388       return "For document '" + theDocName +
389         "' the number of features is decreased, there is no feature '" + aLostName + "'";
390     }
391   }
392   return ""; // ok
393 }
394
395 //==================================================================================================
396 bool checkPythonDump()
397 {
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");
402   if (aDump.get()) {
403     aDump->string("file_path")->setValue("check_dump.py"); // to the current folder
404     aDump->string("file_format")->setValue("py"); // to the current folder
405     aDump->execute();
406   }
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());
411     anErrorMsg.send();
412   }
413   aSession->finishOperation();
414   if (isProblem) {
415     return false; // something is wrong during dump
416   }
417
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);
424     anErrorMsg.send();
425     return false;
426   }
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 */
434
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);
441     anErrorMsg.send();
442     return false;
443   }
444
445   return true;
446 }
447
448 //--------------------------------------------------------------------------------------