Salome HOME
b7b5f1dabdf9b13b5dd191606a3c6aef4d9adbc0
[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 void fillAttribute(const ModelHighAPI_Double & theX,
243                    const ModelHighAPI_Double & theY,
244                    const ModelHighAPI_Double & theZ,
245                    const std::shared_ptr<GeomDataAPI_Point> & theAttribute)
246 {
247   theX.fillAttribute(theAttribute, theX, theY, theZ);
248 }
249
250
251 //==================================================================================================
252 GeomAPI_Shape::ShapeType shapeTypeByStr(std::string theShapeTypeStr)
253 {
254   GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
255
256   std::transform(theShapeTypeStr.begin(), theShapeTypeStr.end(),
257                  theShapeTypeStr.begin(), ::tolower);
258
259   if(theShapeTypeStr == "compound") {
260     aShapeType = GeomAPI_Shape::COMPOUND;
261   } else if(theShapeTypeStr == "compsolid") {
262     aShapeType = GeomAPI_Shape::COMPSOLID;
263   } else if(theShapeTypeStr == "solid") {
264     aShapeType = GeomAPI_Shape::SOLID;
265   } else if(theShapeTypeStr == "shell") {
266     aShapeType = GeomAPI_Shape::SHELL;
267   } else if(theShapeTypeStr == "face") {
268     aShapeType = GeomAPI_Shape::FACE;
269   } else if(theShapeTypeStr == "wire") {
270     aShapeType = GeomAPI_Shape::WIRE;
271   } else if(theShapeTypeStr == "edge") {
272     aShapeType = GeomAPI_Shape::EDGE;
273   } else if(theShapeTypeStr == "vertex") {
274     aShapeType = GeomAPI_Shape::VERTEX;
275   } else if(theShapeTypeStr == "shape") {
276     aShapeType = GeomAPI_Shape::SHAPE;
277   }
278
279   return aShapeType;
280 }
281
282 //==================================================================================================
283 GeomAPI_Shape::ShapeType getShapeType(const ModelHighAPI_Selection& theSelection)
284 {
285   GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
286
287   switch(theSelection.variantType()) {
288     case ModelHighAPI_Selection::VT_ResultSubShapePair: {
289       ResultSubShapePair aPair = theSelection.resultSubShapePair();
290       GeomShapePtr aShape = aPair.second;
291       if(!aShape.get()) {
292         aShape = aPair.first->shape();
293       }
294       if(!aShape.get()) {
295         return aShapeType;
296       }
297       aShapeType = aShape->shapeType();
298       break;
299     }
300     case ModelHighAPI_Selection::VT_TypeSubShapeNamePair: {
301       TypeSubShapeNamePair aPair = theSelection.typeSubShapeNamePair();
302       std::string aType = aPair.first;
303       aShapeType = shapeTypeByStr(aType);
304       break;
305     }
306   }
307
308   return aShapeType;
309 }
310
311 //--------------------------------------------------------------------------------------
312 ModelAPI_AttributeTables::ValueType valueTypeByStr(const std::string& theValueTypeStr)
313 {
314   std::string aType = theValueTypeStr;
315   std::transform(aType.begin(), aType.end(), aType.begin(), ::tolower);
316   if (aType == "boolean")
317     return ModelAPI_AttributeTables::BOOLEAN;
318   else if (aType == "integer")
319     return ModelAPI_AttributeTables::INTEGER;
320   else if (aType == "string")
321     return ModelAPI_AttributeTables::STRING;
322   return ModelAPI_AttributeTables::DOUBLE; // default
323 }
324
325 //--------------------------------------------------------------------------------------
326 std::string strByValueType(const ModelAPI_AttributeTables::ValueType theType)
327 {
328   switch(theType) {
329   case ModelAPI_AttributeTables::BOOLEAN: return "BOOLEAN";
330   case ModelAPI_AttributeTables::INTEGER: return "INTEGER";
331   case ModelAPI_AttributeTables::DOUBLE: return "DOUBLE";
332   case ModelAPI_AttributeTables::STRING: return "STRING";
333   }
334   return ""; // bad case
335 }
336
337 /// stores the features information, recoursively stores sub-documetns features
338 std::string storeFeatures(const std::string& theDocName, DocumentPtr theDoc,
339   std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> >& theStore,
340   const bool theCompare) // if false => store
341 {
342   std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> >::iterator aDocFind;
343   if (theCompare) {
344      aDocFind = theStore.find(theDocName);
345      if (aDocFind == theStore.end()) {
346        return "Document '" + theDocName + "' not found";
347      }
348   }
349   // store the model features information: iterate all features
350   int anObjectsCount = 0; // stores the number of compared features for this document to compate
351   std::set<std::string> aProcessed; // processed features names (that are in the current document)
352
353   // process all objects (features and folders)
354   std::list<ObjectPtr> allObjects = theDoc->allObjects();
355   std::list<ObjectPtr>::iterator allIter = allObjects.begin();
356   for(; allIter != allObjects.end(); allIter++) {
357     ObjectPtr anObject = *allIter;
358     if (theCompare) {
359       std::map<std::string, ModelHighAPI_FeatureStore>::iterator
360         anObjFind = aDocFind->second.find(anObject->data()->name());
361       if (anObjFind == aDocFind->second.end()) {
362         return "Document '" + theDocName + "' feature '" + anObject->data()->name() + "' not found";
363       }
364       std::string anError = anObjFind->second.compare(anObject);
365       if (!anError.empty()) {
366         anError = "Document " + theDocName + " " + anError;
367         return anError;
368       }
369       anObjectsCount++;
370       aProcessed.insert(anObject->data()->name());
371     } else {
372       theStore[theDocName][anObject->data()->name()] = ModelHighAPI_FeatureStore(anObject);
373     }
374
375     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
376     if (aFeature) {
377       // iterate all results of this feature
378       std::list<ResultPtr> allResults;
379       ModelAPI_Tools::allResults(aFeature, allResults);
380       std::list<ResultPtr>::iterator aRes = allResults.begin();
381       for(; aRes != allResults.end(); aRes++) {
382         // recoursively store features of sub-documents
383         if ((*aRes)->groupName() == ModelAPI_ResultPart::group()) {
384           DocumentPtr aDoc = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes)->partDoc();
385           if (aDoc.get()) {
386             std::string anError =
387                 storeFeatures((*aRes)->data()->name(), aDoc, theStore, theCompare);
388             if (!anError.empty())
389               return anError;
390           }
391         }
392       }
393     }
394   }
395   // checks the number of compared features
396   if (theCompare) {
397     if (aDocFind->second.size() != anObjectsCount) {
398       // search for disappeared feature
399       std::string aLostName;
400       std::map<std::string, ModelHighAPI_FeatureStore>::iterator aLostIter;
401       for(aLostIter = aDocFind->second.begin(); aLostIter != aDocFind->second.end(); aLostIter++) {
402         if (aProcessed.find(aLostIter->first) == aProcessed.end()) {
403           aLostName = aLostIter->first;
404         }
405       }
406       return "For document '" + theDocName +
407         "' the number of features is decreased, there is no feature '" + aLostName + "'";
408     }
409   }
410   return ""; // ok
411 }
412
413 //==================================================================================================
414 bool checkPythonDump()
415 {
416   SessionPtr aSession = ModelAPI_Session::get();
417   // 2431: set PartSet as a current document
418   aSession->setActiveDocument(aSession->moduleDocument(), true);
419   // dump all to the python file
420   aSession->startOperation("Check python dump");
421   FeaturePtr aDump = aSession->moduleDocument()->addFeature("Dump");
422   if (aDump.get()) {
423     aDump->string("file_path")->setValue("check_dump.py"); // to the current folder
424     aDump->string("file_format")->setValue("py"); // to the current folder
425     aDump->execute();
426   }
427   bool isProblem = !aDump.get() || !aDump->error().empty(); // after "finish" dump will be removed
428   if (isProblem && aDump.get()) {
429     std::cout<<"Dump feature error "<<aDump->error()<<std::endl;
430     Events_InfoMessage anErrorMsg(std::string("checkPythonDump"), aDump->error());
431     anErrorMsg.send();
432   }
433   aSession->finishOperation();
434   if (isProblem) {
435     return false; // something is wrong during dump
436   }
437
438    // map from document name to feature name to feature data
439   std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> > aStore;
440   std::string anError = storeFeatures(
441     aSession->moduleDocument()->kind(), aSession->moduleDocument(), aStore, false);
442   if (!anError.empty()) {
443     Events_InfoMessage anErrorMsg(std::string("checkPythonDump"), anError);
444     anErrorMsg.send();
445     return false;
446   }
447   // close all before importation of the script
448   aSession->closeAll();
449   // execute the dumped
450   PyGILState_STATE gstate = PyGILState_Ensure(); /* acquire python thread */
451   static char aDumpName[] = "./check_dump.py";
452   static char aReadMode[] = "r";
453   PyObject* PyFileObject = PyFile_FromString(aDumpName, aReadMode);
454   PyRun_SimpleFileEx(PyFile_AsFile(PyFileObject), aDumpName, 1);
455   PyGILState_Release(gstate); /* release python thread */
456
457   // compare with the stored data
458   anError = storeFeatures(
459     aSession->moduleDocument()->kind(), aSession->moduleDocument(), aStore, true);
460   if (!anError.empty()) {
461     std::cout<<anError<<std::endl;
462     Events_InfoMessage anErrorMsg(std::string("checkPythonDump"), anError);
463     anErrorMsg.send();
464     return false;
465   }
466
467   return true;
468 }
469
470 //--------------------------------------------------------------------------------------