Salome HOME
f11d8c09902f42f2e2e36c8b0968ff4b96a4ac49
[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     const ModelHighAPI_Selection& aSelection = theValue.front();
198     std::string aSelectionType = aSelection.shapeType();
199     GeomAPI_Shape::ShapeType aType = GeomAPI_Shape::shapeTypeByStr(aSelectionType);
200     if (aType != GeomAPI_Shape::SHAPE || aSelectionType == "SHAPE")
201       theAttribute->setSelectionType(aSelectionType);
202   }
203
204   for (auto it = theValue.begin(); it != theValue.end(); ++it)
205     it->appendToList(theAttribute);
206 }
207
208 //--------------------------------------------------------------------------------------
209 void fillAttribute(const std::string & theValue,
210                    const std::shared_ptr<ModelAPI_AttributeString> & theAttribute)
211 {
212   theAttribute->setValue(theValue);
213 }
214
215 //--------------------------------------------------------------------------------------
216 void fillAttribute(const char * theValue,
217                    const std::shared_ptr<ModelAPI_AttributeString> & theAttribute)
218 {
219   theAttribute->setValue(theValue);
220 }
221
222 //--------------------------------------------------------------------------------------
223 void fillAttribute(const std::list<std::string> & theValue,
224                    const std::shared_ptr<ModelAPI_AttributeStringArray> & theAttribute)
225 {
226   theAttribute->setSize(int(theValue.size()));
227
228   int anIndex = 0;
229   for (auto it = theValue.begin(); it != theValue.end(); ++it, ++anIndex)
230     theAttribute->setValue(anIndex, *it);
231 }
232
233 //--------------------------------------------------------------------------------------
234 void fillAttribute(const std::list<ModelHighAPI_Integer> & theValue,
235                    const std::shared_ptr<ModelAPI_AttributeIntArray> & theAttribute)
236 {
237   theAttribute->setSize(int(theValue.size()));
238
239   int anIndex = 0;
240   for (auto it = theValue.begin(); it != theValue.end(); ++it, ++anIndex)
241     theAttribute->setValue(anIndex, it->intValue()); // use only values, no text support in array
242 }
243
244 void fillAttribute(const ModelHighAPI_Double & theX,
245                    const ModelHighAPI_Double & theY,
246                    const ModelHighAPI_Double & theZ,
247                    const std::shared_ptr<GeomDataAPI_Point> & theAttribute)
248 {
249   theX.fillAttribute(theAttribute, theX, theY, theZ);
250 }
251
252
253 //==================================================================================================
254 GeomAPI_Shape::ShapeType shapeTypeByStr(std::string theShapeTypeStr)
255 {
256   GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
257
258   std::transform(theShapeTypeStr.begin(), theShapeTypeStr.end(),
259                  theShapeTypeStr.begin(), ::tolower);
260
261   if(theShapeTypeStr == "compound") {
262     aShapeType = GeomAPI_Shape::COMPOUND;
263   } else if(theShapeTypeStr == "compsolid") {
264     aShapeType = GeomAPI_Shape::COMPSOLID;
265   } else if(theShapeTypeStr == "solid") {
266     aShapeType = GeomAPI_Shape::SOLID;
267   } else if(theShapeTypeStr == "shell") {
268     aShapeType = GeomAPI_Shape::SHELL;
269   } else if(theShapeTypeStr == "face") {
270     aShapeType = GeomAPI_Shape::FACE;
271   } else if(theShapeTypeStr == "wire") {
272     aShapeType = GeomAPI_Shape::WIRE;
273   } else if(theShapeTypeStr == "edge") {
274     aShapeType = GeomAPI_Shape::EDGE;
275   } else if(theShapeTypeStr == "vertex") {
276     aShapeType = GeomAPI_Shape::VERTEX;
277   } else if(theShapeTypeStr == "shape") {
278     aShapeType = GeomAPI_Shape::SHAPE;
279   }
280
281   return aShapeType;
282 }
283
284 //==================================================================================================
285 GeomAPI_Shape::ShapeType getShapeType(const ModelHighAPI_Selection& theSelection)
286 {
287   GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
288
289   switch(theSelection.variantType()) {
290     case ModelHighAPI_Selection::VT_ResultSubShapePair: {
291       ResultSubShapePair aPair = theSelection.resultSubShapePair();
292       GeomShapePtr aShape = aPair.second;
293       if(!aShape.get()) {
294         aShape = aPair.first->shape();
295       }
296       if(!aShape.get()) {
297         return aShapeType;
298       }
299       aShapeType = aShape->shapeType();
300       break;
301     }
302     case ModelHighAPI_Selection::VT_TypeSubShapeNamePair: {
303       TypeSubShapeNamePair aPair = theSelection.typeSubShapeNamePair();
304       std::string aType = aPair.first;
305       aShapeType = shapeTypeByStr(aType);
306       break;
307     }
308     case ModelHighAPI_Selection::VT_TypeInnerPointPair: {
309       TypeInnerPointPair aPair = theSelection.typeInnerPointPair();
310       std::string aType = aPair.first;
311       aType = aType.substr(0, aType.find_first_of('_'));
312       aShapeType = shapeTypeByStr(aType);
313       break;
314     }
315   }
316
317   return aShapeType;
318 }
319
320 //--------------------------------------------------------------------------------------
321 ModelAPI_AttributeTables::ValueType valueTypeByStr(const std::string& theValueTypeStr)
322 {
323   std::string aType = theValueTypeStr;
324   std::transform(aType.begin(), aType.end(), aType.begin(), ::tolower);
325   if (aType == "boolean")
326     return ModelAPI_AttributeTables::BOOLEAN;
327   else if (aType == "integer")
328     return ModelAPI_AttributeTables::INTEGER;
329   else if (aType == "string")
330     return ModelAPI_AttributeTables::STRING;
331   return ModelAPI_AttributeTables::DOUBLE; // default
332 }
333
334 //--------------------------------------------------------------------------------------
335 std::string strByValueType(const ModelAPI_AttributeTables::ValueType theType)
336 {
337   switch(theType) {
338   case ModelAPI_AttributeTables::BOOLEAN: return "BOOLEAN";
339   case ModelAPI_AttributeTables::INTEGER: return "INTEGER";
340   case ModelAPI_AttributeTables::DOUBLE: return "DOUBLE";
341   case ModelAPI_AttributeTables::STRING: return "STRING";
342   }
343   return ""; // bad case
344 }
345
346 /// stores the features information, recoursively stores sub-documetns features
347 std::string storeFeatures(const std::string& theDocName, DocumentPtr theDoc,
348   std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> >& theStore,
349   const bool theCompare) // if false => store
350 {
351   std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> >::iterator aDocFind;
352   if (theCompare) {
353      aDocFind = theStore.find(theDocName);
354      if (aDocFind == theStore.end()) {
355        return "Document '" + theDocName + "' not found";
356      }
357   }
358   // store the model features information: iterate all features
359   int anObjectsCount = 0; // stores the number of compared features for this document to compate
360   std::set<std::string> aProcessed; // processed features names (that are in the current document)
361
362   // process all objects (features and folders)
363   std::list<ObjectPtr> allObjects = theDoc->allObjects();
364   std::list<ObjectPtr>::iterator allIter = allObjects.begin();
365   for(; allIter != allObjects.end(); allIter++) {
366     ObjectPtr anObject = *allIter;
367     if (theCompare) {
368       std::map<std::string, ModelHighAPI_FeatureStore>::iterator
369         anObjFind = aDocFind->second.find(anObject->data()->name());
370       if (anObjFind == aDocFind->second.end()) {
371         return "Document '" + theDocName + "' feature '" + anObject->data()->name() + "' not found";
372       }
373       std::string anError = anObjFind->second.compare(anObject);
374       if (!anError.empty()) {
375         anError = "Document " + theDocName + " " + anError;
376         return anError;
377       }
378       anObjectsCount++;
379       aProcessed.insert(anObject->data()->name());
380     } else {
381       theStore[theDocName][anObject->data()->name()] = ModelHighAPI_FeatureStore(anObject);
382     }
383
384     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
385     if (aFeature) {
386       // iterate all results of this feature
387       std::list<ResultPtr> allResults;
388       ModelAPI_Tools::allResults(aFeature, allResults);
389       std::list<ResultPtr>::iterator aRes = allResults.begin();
390       for(; aRes != allResults.end(); aRes++) {
391         // recoursively store features of sub-documents
392         if ((*aRes)->groupName() == ModelAPI_ResultPart::group()) {
393           DocumentPtr aDoc = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes)->partDoc();
394           if (aDoc.get()) {
395             std::string anError =
396                 storeFeatures((*aRes)->data()->name(), aDoc, theStore, theCompare);
397             if (!anError.empty())
398               return anError;
399           }
400         }
401       }
402     }
403   }
404   // checks the number of compared features
405   if (theCompare) {
406     if (aDocFind->second.size() != anObjectsCount) {
407       // search for disappeared feature
408       std::string aLostName;
409       std::map<std::string, ModelHighAPI_FeatureStore>::iterator aLostIter;
410       for(aLostIter = aDocFind->second.begin(); aLostIter != aDocFind->second.end(); aLostIter++) {
411         if (aProcessed.find(aLostIter->first) == aProcessed.end()) {
412           aLostName = aLostIter->first;
413         }
414       }
415       return "For document '" + theDocName +
416         "' the number of features is decreased, there is no feature '" + aLostName + "'";
417     }
418   }
419   return ""; // ok
420 }
421
422 //==================================================================================================
423 typedef std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> > Storage;
424
425 static bool dumpToPython(SessionPtr theSession,
426                          const char* theFilename,
427                          bool theDumpByGeom,
428                          const std::string& theErrorMsgContext)
429 {
430   // 2431: set PartSet as a current document
431   theSession->setActiveDocument(theSession->moduleDocument(), true);
432   // dump all to the python file
433   theSession->startOperation("Check python dump");
434   FeaturePtr aDump = theSession->moduleDocument()->addFeature("Dump");
435   if (aDump.get()) {
436     aDump->string("file_path")->setValue(theFilename);
437     aDump->string("file_format")->setValue("py");
438     aDump->boolean("geometric_dump")->setValue(theDumpByGeom);
439     aDump->execute();
440   }
441   bool isProblem = !aDump.get() || !aDump->error().empty(); // after "finish" dump will be removed
442   if (isProblem && aDump.get()) {
443     std::cout << "Dump feature error " << aDump->error() << std::endl;
444     Events_InfoMessage anErrorMsg(theErrorMsgContext, aDump->error());
445     anErrorMsg.send();
446   }
447   theSession->finishOperation();
448   return !isProblem;
449 }
450
451 static bool checkDump(SessionPtr theSession,
452                       char* theFilename,
453                       Storage& theStorage,
454                       const std::string& theErrorMsgContext)
455 {
456
457   // close all before importation of the script
458   theSession->closeAll();
459
460
461   // execute the dumped
462   PyGILState_STATE gstate = PyGILState_Ensure(); /* acquire python thread */
463   static char aReadMode[] = "r";
464   PyObject* PyFileObject = PyFile_FromString(theFilename, aReadMode);
465   PyRun_SimpleFileEx(PyFile_AsFile(PyFileObject), theFilename, 1);
466   PyGILState_Release(gstate); /* release python thread */
467
468   // compare with the stored data
469   std::string anError = storeFeatures(
470     theSession->moduleDocument()->kind(), theSession->moduleDocument(), theStorage, true);
471   if (!anError.empty()) {
472     std::cout << anError << std::endl;
473     Events_InfoMessage anErrorMsg(theErrorMsgContext, anError);
474     anErrorMsg.send();
475     return false;
476   }
477
478   return true;
479 }
480
481 bool checkPythonDump()
482 {
483   static const std::string anErrorByNaming("checkPythonDump by naming");
484   static const std::string anErrorByGeometry("checkPythonDump by geometry");
485
486   static char aFileForNamingDump[] = "./check_dump_byname.py";
487   static char aFileForGeometryDump[] = "./check_dump_bygeom.py";
488
489   SessionPtr aSession = ModelAPI_Session::get();
490   // dump with the selection by names
491   if (!dumpToPython(aSession, aFileForNamingDump, false, anErrorByNaming))
492     return false;
493   // dump with the selection by geometry
494   if (!dumpToPython(aSession, aFileForGeometryDump, true, anErrorByGeometry))
495     return false;
496
497    // map from document name to feature name to feature data
498   std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> > aStore;
499   std::string anError = storeFeatures(
500     aSession->moduleDocument()->kind(), aSession->moduleDocument(), aStore, false);
501   if (!anError.empty()) {
502     Events_InfoMessage anErrorMsg(std::string("checkPythonDump"), anError);
503     anErrorMsg.send();
504     return false;
505   }
506
507   // check dump with the selection by names
508   bool isOk = checkDump(aSession, aFileForNamingDump, aStore, anErrorByNaming);
509   // check dump with the selection by geometry
510   isOk = isOk && checkDump(aSession, aFileForGeometryDump, aStore, anErrorByGeometry);
511
512   return isOk;
513 }
514
515 //--------------------------------------------------------------------------------------