Salome HOME
Merge remote-tracking branch 'remotes/origin/Dev_FolderFeature'
[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     if (it->resultSubShapePair().first)
179       theAttribute->append(it->resultSubShapePair().first); // use only context
180   }
181 }
182
183 //--------------------------------------------------------------------------------------
184 void fillAttribute(const ModelHighAPI_Selection & theValue,
185                    const std::shared_ptr<ModelAPI_AttributeSelection> & theAttribute)
186 {
187   theValue.fillAttribute(theAttribute);
188 }
189
190 //--------------------------------------------------------------------------------------
191 void fillAttribute(const std::list<ModelHighAPI_Selection> & theValue,
192                    const std::shared_ptr<ModelAPI_AttributeSelectionList> & theAttribute)
193 {
194   theAttribute->clear();
195
196   if(!theValue.empty()) {
197     std::string aSelectionType;
198     const ModelHighAPI_Selection& aSelection = theValue.front();
199     theAttribute->setSelectionType(aSelection.shapeType());
200   }
201
202   for (auto it = theValue.begin(); it != theValue.end(); ++it)
203     it->appendToList(theAttribute);
204 }
205
206 //--------------------------------------------------------------------------------------
207 void fillAttribute(const std::string & theValue,
208                    const std::shared_ptr<ModelAPI_AttributeString> & theAttribute)
209 {
210   theAttribute->setValue(theValue);
211 }
212
213 //--------------------------------------------------------------------------------------
214 void fillAttribute(const char * theValue,
215                    const std::shared_ptr<ModelAPI_AttributeString> & theAttribute)
216 {
217   theAttribute->setValue(theValue);
218 }
219
220 //--------------------------------------------------------------------------------------
221 void fillAttribute(const std::list<std::string> & theValue,
222                    const std::shared_ptr<ModelAPI_AttributeStringArray> & theAttribute)
223 {
224   theAttribute->setSize(int(theValue.size()));
225
226   int anIndex = 0;
227   for (auto it = theValue.begin(); it != theValue.end(); ++it, ++anIndex)
228     theAttribute->setValue(anIndex, *it);
229 }
230
231 //--------------------------------------------------------------------------------------
232 void fillAttribute(const std::list<ModelHighAPI_Integer> & theValue,
233                    const std::shared_ptr<ModelAPI_AttributeIntArray> & theAttribute)
234 {
235   theAttribute->setSize(int(theValue.size()));
236
237   int anIndex = 0;
238   for (auto it = theValue.begin(); it != theValue.end(); ++it, ++anIndex)
239     theAttribute->setValue(anIndex, it->intValue()); // use only values, no text support in array
240 }
241
242 //==================================================================================================
243 GeomAPI_Shape::ShapeType shapeTypeByStr(std::string theShapeTypeStr)
244 {
245   GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
246
247   std::transform(theShapeTypeStr.begin(), theShapeTypeStr.end(),
248                  theShapeTypeStr.begin(), ::tolower);
249
250   if(theShapeTypeStr == "compound") {
251     aShapeType = GeomAPI_Shape::COMPOUND;
252   } else if(theShapeTypeStr == "compsolid") {
253     aShapeType = GeomAPI_Shape::COMPSOLID;
254   } else if(theShapeTypeStr == "solid") {
255     aShapeType = GeomAPI_Shape::SOLID;
256   } else if(theShapeTypeStr == "shell") {
257     aShapeType = GeomAPI_Shape::SHELL;
258   } else if(theShapeTypeStr == "face") {
259     aShapeType = GeomAPI_Shape::FACE;
260   } else if(theShapeTypeStr == "wire") {
261     aShapeType = GeomAPI_Shape::WIRE;
262   } else if(theShapeTypeStr == "edge") {
263     aShapeType = GeomAPI_Shape::EDGE;
264   } else if(theShapeTypeStr == "vertex") {
265     aShapeType = GeomAPI_Shape::VERTEX;
266   } else if(theShapeTypeStr == "shape") {
267     aShapeType = GeomAPI_Shape::SHAPE;
268   }
269
270   return aShapeType;
271 }
272
273 //==================================================================================================
274 GeomAPI_Shape::ShapeType getShapeType(const ModelHighAPI_Selection& theSelection)
275 {
276   GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
277
278   switch(theSelection.variantType()) {
279     case ModelHighAPI_Selection::VT_ResultSubShapePair: {
280       ResultSubShapePair aPair = theSelection.resultSubShapePair();
281       GeomShapePtr aShape = aPair.second;
282       if(!aShape.get()) {
283         aShape = aPair.first->shape();
284       }
285       if(!aShape.get()) {
286         return aShapeType;
287       }
288       aShapeType = aShape->shapeType();
289       break;
290     }
291     case ModelHighAPI_Selection::VT_TypeSubShapeNamePair: {
292       TypeSubShapeNamePair aPair = theSelection.typeSubShapeNamePair();
293       std::string aType = aPair.first;
294       aShapeType = shapeTypeByStr(aType);
295       break;
296     }
297   }
298
299   return aShapeType;
300 }
301
302 //--------------------------------------------------------------------------------------
303 ModelAPI_AttributeTables::ValueType valueTypeByStr(const std::string& theValueTypeStr)
304 {
305   std::string aType = theValueTypeStr;
306   std::transform(aType.begin(), aType.end(), aType.begin(), ::tolower);
307   if (aType == "boolean")
308     return ModelAPI_AttributeTables::BOOLEAN;
309   else if (aType == "integer")
310     return ModelAPI_AttributeTables::INTEGER;
311   else if (aType == "string")
312     return ModelAPI_AttributeTables::STRING;
313   return ModelAPI_AttributeTables::DOUBLE; // default
314 }
315
316 //--------------------------------------------------------------------------------------
317 std::string strByValueType(const ModelAPI_AttributeTables::ValueType theType)
318 {
319   switch(theType) {
320   case ModelAPI_AttributeTables::BOOLEAN: return "BOOLEAN";
321   case ModelAPI_AttributeTables::INTEGER: return "INTEGER";
322   case ModelAPI_AttributeTables::DOUBLE: return "DOUBLE";
323   case ModelAPI_AttributeTables::STRING: return "STRING";
324   }
325   return ""; // bad case
326 }
327
328 /// stores the features information, recoursively stores sub-documetns features
329 std::string storeFeatures(const std::string& theDocName, DocumentPtr theDoc,
330   std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> >& theStore,
331   const bool theCompare) // if false => store
332 {
333   std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> >::iterator aDocFind;
334   if (theCompare) {
335      aDocFind = theStore.find(theDocName);
336      if (aDocFind == theStore.end()) {
337        return "Document '" + theDocName + "' not found";
338      }
339   }
340   // store the model features information: iterate all features
341   int anObjectsCount = 0; // stores the number of compared features for this document to compate
342   std::set<std::string> aProcessed; // processed features names (that are in the current document)
343
344   // process all objects (features and folders)
345   std::list<ObjectPtr> allObjects = theDoc->allObjects();
346   std::list<ObjectPtr>::iterator allIter = allObjects.begin();
347   for(; allIter != allObjects.end(); allIter++) {
348     ObjectPtr anObject = *allIter;
349     if (theCompare) {
350       std::map<std::string, ModelHighAPI_FeatureStore>::iterator
351         anObjFind = aDocFind->second.find(anObject->data()->name());
352       if (anObjFind == aDocFind->second.end()) {
353         return "Document '" + theDocName + "' feature '" + anObject->data()->name() + "' not found";
354       }
355       std::string anError = anObjFind->second.compare(anObject);
356       if (!anError.empty()) {
357         anError = "Document " + theDocName + " " + anError;
358         return anError;
359       }
360       anObjectsCount++;
361       aProcessed.insert(anObject->data()->name());
362     } else {
363       theStore[theDocName][anObject->data()->name()] = ModelHighAPI_FeatureStore(anObject);
364     }
365
366     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
367     if (aFeature) {
368       // iterate all results of this feature
369       std::list<ResultPtr> allResults;
370       ModelAPI_Tools::allResults(aFeature, allResults);
371       std::list<ResultPtr>::iterator aRes = allResults.begin();
372       for(; aRes != allResults.end(); aRes++) {
373         // recoursively store features of sub-documents
374         if ((*aRes)->groupName() == ModelAPI_ResultPart::group()) {
375           DocumentPtr aDoc = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes)->partDoc();
376           if (aDoc.get()) {
377             std::string anError =
378                 storeFeatures((*aRes)->data()->name(), aDoc, theStore, theCompare);
379             if (!anError.empty())
380               return anError;
381           }
382         }
383       }
384     }
385   }
386   // checks the number of compared features
387   if (theCompare) {
388     if (aDocFind->second.size() != anObjectsCount) {
389       // search for disappeared feature
390       std::string aLostName;
391       std::map<std::string, ModelHighAPI_FeatureStore>::iterator aLostIter;
392       for(aLostIter = aDocFind->second.begin(); aLostIter != aDocFind->second.end(); aLostIter++) {
393         if (aProcessed.find(aLostIter->first) == aProcessed.end()) {
394           aLostName = aLostIter->first;
395         }
396       }
397       return "For document '" + theDocName +
398         "' the number of features is decreased, there is no feature '" + aLostName + "'";
399     }
400   }
401   return ""; // ok
402 }
403
404 //==================================================================================================
405 bool checkPythonDump()
406 {
407   SessionPtr aSession = ModelAPI_Session::get();
408   // dump all to the python file
409   aSession->startOperation("Check python dump");
410   FeaturePtr aDump = aSession->moduleDocument()->addFeature("Dump");
411   if (aDump.get()) {
412     aDump->string("file_path")->setValue("check_dump.py"); // to the current folder
413     aDump->string("file_format")->setValue("py"); // to the current folder
414     aDump->execute();
415   }
416   bool isProblem = !aDump.get() || !aDump->error().empty(); // after "finish" dump will be removed
417   if (isProblem && aDump.get()) {
418     std::cout<<"Dump feature error "<<aDump->error()<<std::endl;
419     Events_InfoMessage anErrorMsg(std::string("checkPythonDump"), aDump->error());
420     anErrorMsg.send();
421   }
422   aSession->finishOperation();
423   if (isProblem) {
424     return false; // something is wrong during dump
425   }
426
427    // map from document name to feature name to feature data
428   std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> > aStore;
429   std::string anError = storeFeatures(
430     aSession->moduleDocument()->kind(), aSession->moduleDocument(), aStore, false);
431   if (!anError.empty()) {
432     Events_InfoMessage anErrorMsg(std::string("checkPythonDump"), anError);
433     anErrorMsg.send();
434     return false;
435   }
436   // close all before importation of the script
437   aSession->closeAll();
438   // execute the dumped
439   PyGILState_STATE gstate = PyGILState_Ensure(); /* acquire python thread */
440   PyObject* PyFileObject = PyFile_FromString("./check_dump.py", "r");
441   PyRun_SimpleFileEx(PyFile_AsFile(PyFileObject), "./check_dump.py", 1);
442   PyGILState_Release(gstate); /* release python thread */
443
444   // compare with the stored data
445   anError = storeFeatures(
446     aSession->moduleDocument()->kind(), aSession->moduleDocument(), aStore, true);
447   if (!anError.empty()) {
448     std::cout<<anError<<std::endl;
449     Events_InfoMessage anErrorMsg(std::string("checkPythonDump"), anError);
450     anErrorMsg.send();
451     return false;
452   }
453
454   return true;
455 }
456
457 //--------------------------------------------------------------------------------------