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