Salome HOME
Fixes for the issue #2686
[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 #include <ModelAPI_Events.h>
50 //--------------------------------------------------------------------------------------
51 #include <Config_ModuleReader.h>
52 //--------------------------------------------------------------------------------------
53 #include "ModelHighAPI_Double.h"
54 #include "ModelHighAPI_Integer.h"
55 #include "ModelHighAPI_RefAttr.h"
56 #include "ModelHighAPI_Reference.h"
57 #include "ModelHighAPI_Selection.h"
58
59 #include <Events_InfoMessage.h>
60
61 // Have to be included before std headers
62 #include <Python.h>
63
64 #include <algorithm>
65 #include <iostream>
66
67 //--------------------------------------------------------------------------------------
68 void fillAttribute(const std::shared_ptr<GeomAPI_Pnt2d> & theValue,
69                    const std::shared_ptr<GeomDataAPI_Point2D> & theAttribute)
70 {
71   theAttribute->setValue(theValue);
72 }
73
74 void fillAttribute(const std::shared_ptr<GeomDataAPI_Point2D> & theAttribute,
75                    double theX, double theY)
76 {
77   theAttribute->setValue(theX, theY);
78 }
79
80 //--------------------------------------------------------------------------------------
81 void fillAttribute(const std::shared_ptr<GeomAPI_Dir> & theValue,
82                    const std::shared_ptr<GeomDataAPI_Dir> & theAttribute)
83 {
84   theAttribute->setValue(theValue);
85 }
86
87 //--------------------------------------------------------------------------------------
88 void fillAttribute(const std::shared_ptr<GeomAPI_Pnt> & theValue,
89                    const std::shared_ptr<GeomDataAPI_Point> & theAttribute)
90 {
91   theAttribute->setValue(theValue);
92 }
93
94 //--------------------------------------------------------------------------------------
95 void fillAttribute(bool theValue,
96                    const std::shared_ptr<ModelAPI_AttributeBoolean> & theAttribute)
97 {
98   theAttribute->setValue(theValue);
99 }
100
101 //--------------------------------------------------------------------------------------
102 void fillAttribute(const ModelHighAPI_Double & theValue,
103                    const std::shared_ptr<ModelAPI_AttributeDouble> & theAttribute)
104 {
105   theValue.fillAttribute(theAttribute);
106 }
107 void fillAttribute(double theValue,
108                    const std::shared_ptr<ModelAPI_AttributeDouble> & theAttribute)
109 {
110   theAttribute->setValue(theValue);
111 }
112
113 //--------------------------------------------------------------------------------------
114 void fillAttribute(const ModelHighAPI_Integer & theValue,
115                    const std::shared_ptr<ModelAPI_AttributeInteger> & theAttribute)
116 {
117   theValue.fillAttribute(theAttribute);
118 }
119 void fillAttribute(int theValue,
120                    const std::shared_ptr<ModelAPI_AttributeInteger> & theAttribute)
121 {
122   theAttribute->setValue(theValue);
123 }
124
125 //--------------------------------------------------------------------------------------
126 void fillAttribute(const ModelHighAPI_RefAttr & theValue,
127                    const std::shared_ptr<ModelAPI_AttributeRefAttr> & theAttribute)
128 {
129   theValue.fillAttribute(theAttribute);
130 }
131
132 //--------------------------------------------------------------------------------------
133 void fillAttribute(const std::list<ModelHighAPI_RefAttr> & theValue,
134                    const std::shared_ptr<ModelAPI_AttributeRefAttrList> & theAttribute)
135 {
136   theAttribute->clear();
137   for (auto it = theValue.begin(); it != theValue.end(); ++it)
138     it->appendToList(theAttribute);
139 }
140
141 //--------------------------------------------------------------------------------------
142 void fillAttribute(const ModelHighAPI_Reference & theValue,
143                    const std::shared_ptr<ModelAPI_AttributeReference> & theAttribute)
144 {
145   theValue.fillAttribute(theAttribute);
146 }
147
148 //--------------------------------------------------------------------------------------
149 void fillAttribute(const std::list<ModelHighAPI_Reference> & theValue,
150                    const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
151 {
152   theAttribute->clear();
153   for (auto it = theValue.begin(); it != theValue.end(); ++it)
154     it->appendToList(theAttribute);
155 }
156
157 //--------------------------------------------------------------------------------------
158 void fillAttribute(const std::shared_ptr<ModelAPI_Object> & theValue,
159                    const std::shared_ptr<ModelAPI_AttributeReference> & theAttribute)
160 {
161   theAttribute->setValue(theValue);
162 }
163
164 //--------------------------------------------------------------------------------------
165 void fillAttribute(const std::list<std::shared_ptr<ModelAPI_Object> > & theValue,
166                    const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
167 {
168   theAttribute->clear();
169   for (auto it = theValue.begin(); it != theValue.end(); ++it)
170     theAttribute->append(*it);
171 }
172
173 MODELHIGHAPI_EXPORT
174 void fillAttribute(const std::list<ModelHighAPI_Selection> & theValue,
175                    const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
176 {
177   theAttribute->clear();
178   for (auto it = theValue.begin(); it != theValue.end(); ++it) {
179     if (it->resultSubShapePair().first)
180       theAttribute->append(it->resultSubShapePair().first); // use only context
181   }
182 }
183
184 //--------------------------------------------------------------------------------------
185 void fillAttribute(const ModelHighAPI_Selection & theValue,
186                    const std::shared_ptr<ModelAPI_AttributeSelection> & theAttribute)
187 {
188   theValue.fillAttribute(theAttribute);
189 }
190
191 //--------------------------------------------------------------------------------------
192 void fillAttribute(const std::list<ModelHighAPI_Selection> & theValue,
193                    const std::shared_ptr<ModelAPI_AttributeSelectionList> & theAttribute)
194 {
195   theAttribute->clear();
196
197   if(!theValue.empty()) {
198     const ModelHighAPI_Selection& aSelection = theValue.front();
199     GeomAPI_Shape::ShapeType aSelectionType = getShapeType(aSelection);
200     theAttribute->setSelectionType(strByShapeType(aSelectionType));
201   }
202
203   for (auto it = theValue.begin(); it != theValue.end(); ++it)
204     it->appendToList(theAttribute);
205 }
206
207 //--------------------------------------------------------------------------------------
208 void fillAttribute(const std::string & theValue,
209                    const std::shared_ptr<ModelAPI_AttributeString> & theAttribute)
210 {
211   theAttribute->setValue(theValue);
212 }
213
214 //--------------------------------------------------------------------------------------
215 void fillAttribute(const char * theValue,
216                    const std::shared_ptr<ModelAPI_AttributeString> & theAttribute)
217 {
218   theAttribute->setValue(theValue);
219 }
220
221 //--------------------------------------------------------------------------------------
222 void fillAttribute(const std::list<std::string> & theValue,
223                    const std::shared_ptr<ModelAPI_AttributeStringArray> & theAttribute)
224 {
225   theAttribute->setSize(int(theValue.size()));
226
227   int anIndex = 0;
228   for (auto it = theValue.begin(); it != theValue.end(); ++it, ++anIndex)
229     theAttribute->setValue(anIndex, *it);
230 }
231
232 //--------------------------------------------------------------------------------------
233 void fillAttribute(const std::list<ModelHighAPI_Integer> & theValue,
234                    const std::shared_ptr<ModelAPI_AttributeIntArray> & theAttribute)
235 {
236   theAttribute->setSize(int(theValue.size()));
237
238   int anIndex = 0;
239   for (auto it = theValue.begin(); it != theValue.end(); ++it, ++anIndex)
240     theAttribute->setValue(anIndex, it->intValue()); // use only values, no text support in array
241 }
242
243 void fillAttribute(const ModelHighAPI_Double & theX,
244                    const ModelHighAPI_Double & theY,
245                    const ModelHighAPI_Double & theZ,
246                    const std::shared_ptr<GeomDataAPI_Point> & theAttribute)
247 {
248   theX.fillAttribute(theAttribute, theX, theY, theZ);
249 }
250
251
252 //==================================================================================================
253 GeomAPI_Shape::ShapeType shapeTypeByStr(std::string theShapeTypeStr)
254 {
255   GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
256
257   std::transform(theShapeTypeStr.begin(), theShapeTypeStr.end(),
258                  theShapeTypeStr.begin(), ::tolower);
259
260   if(theShapeTypeStr == "compound") {
261     aShapeType = GeomAPI_Shape::COMPOUND;
262   } else if(theShapeTypeStr == "compsolid") {
263     aShapeType = GeomAPI_Shape::COMPSOLID;
264   } else if(theShapeTypeStr == "solid") {
265     aShapeType = GeomAPI_Shape::SOLID;
266   } else if(theShapeTypeStr == "shell") {
267     aShapeType = GeomAPI_Shape::SHELL;
268   } else if(theShapeTypeStr == "face") {
269     aShapeType = GeomAPI_Shape::FACE;
270   } else if(theShapeTypeStr == "wire") {
271     aShapeType = GeomAPI_Shape::WIRE;
272   } else if(theShapeTypeStr == "edge") {
273     aShapeType = GeomAPI_Shape::EDGE;
274   } else if(theShapeTypeStr == "vertex") {
275     aShapeType = GeomAPI_Shape::VERTEX;
276   } else if(theShapeTypeStr == "shape") {
277     aShapeType = GeomAPI_Shape::SHAPE;
278   }
279
280   return aShapeType;
281 }
282
283 std::string strByShapeType(GeomAPI_Shape::ShapeType theShapeType)
284 {
285   std::string aShapeTypeStr;
286   switch (theShapeType) {
287   case GeomAPI_Shape::COMPOUND:
288     aShapeTypeStr = "COMPOUND";
289     break;
290   case GeomAPI_Shape::COMPSOLID:
291     aShapeTypeStr = "COMPSOLID";
292     break;
293   case GeomAPI_Shape::SOLID:
294     aShapeTypeStr = "SOLID";
295     break;
296   case GeomAPI_Shape::SHELL:
297     aShapeTypeStr = "SHELL";
298     break;
299   case GeomAPI_Shape::FACE:
300     aShapeTypeStr = "FACE";
301     break;
302   case GeomAPI_Shape::WIRE:
303     aShapeTypeStr = "WIRE";
304     break;
305   case GeomAPI_Shape::EDGE:
306     aShapeTypeStr = "EDGE";
307     break;
308   case GeomAPI_Shape::VERTEX:
309     aShapeTypeStr = "VERTEX";
310     break;
311   default:
312     aShapeTypeStr = "SHAPE";
313     break;
314   }
315   return aShapeTypeStr;
316 }
317
318 //==================================================================================================
319 GeomAPI_Shape::ShapeType getShapeType(const ModelHighAPI_Selection& theSelection)
320 {
321   GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
322
323   switch(theSelection.variantType()) {
324     case ModelHighAPI_Selection::VT_ResultSubShapePair: {
325       ResultSubShapePair aPair = theSelection.resultSubShapePair();
326       GeomShapePtr aShape = aPair.second;
327       if(!aShape.get()) {
328         aShape = aPair.first->shape();
329       }
330       if(!aShape.get()) {
331         return aShapeType;
332       }
333       aShapeType = aShape->shapeType();
334       break;
335     }
336     case ModelHighAPI_Selection::VT_TypeSubShapeNamePair: {
337       TypeSubShapeNamePair aPair = theSelection.typeSubShapeNamePair();
338       std::string aType = aPair.first;
339       aShapeType = shapeTypeByStr(aType);
340       break;
341     }
342     case ModelHighAPI_Selection::VT_TypeInnerPointPair: {
343       TypeInnerPointPair aPair = theSelection.typeInnerPointPair();
344       std::string aType = aPair.first;
345       aType = aType.substr(0, aType.find_first_of('_'));
346       aShapeType = shapeTypeByStr(aType);
347       break;
348     }
349   }
350
351   return aShapeType;
352 }
353
354 //--------------------------------------------------------------------------------------
355 ModelAPI_AttributeTables::ValueType valueTypeByStr(const std::string& theValueTypeStr)
356 {
357   std::string aType = theValueTypeStr;
358   std::transform(aType.begin(), aType.end(), aType.begin(), ::tolower);
359   if (aType == "boolean")
360     return ModelAPI_AttributeTables::BOOLEAN;
361   else if (aType == "integer")
362     return ModelAPI_AttributeTables::INTEGER;
363   else if (aType == "string")
364     return ModelAPI_AttributeTables::STRING;
365   return ModelAPI_AttributeTables::DOUBLE; // default
366 }
367
368 //--------------------------------------------------------------------------------------
369 std::string strByValueType(const ModelAPI_AttributeTables::ValueType theType)
370 {
371   switch(theType) {
372   case ModelAPI_AttributeTables::BOOLEAN: return "BOOLEAN";
373   case ModelAPI_AttributeTables::INTEGER: return "INTEGER";
374   case ModelAPI_AttributeTables::DOUBLE: return "DOUBLE";
375   case ModelAPI_AttributeTables::STRING: return "STRING";
376   }
377   return ""; // bad case
378 }
379
380 /// stores the features information, recoursively stores sub-documetns features
381 std::string storeFeatures(const std::string& theDocName, DocumentPtr theDoc,
382   std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> >& theStore,
383   const bool theCompare) // if false => store
384 {
385   std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> >::iterator aDocFind;
386   if (theCompare) {
387      aDocFind = theStore.find(theDocName);
388      if (aDocFind == theStore.end()) {
389        return "Document '" + theDocName + "' not found";
390      }
391   }
392   // store the model features information: iterate all features
393   int anObjectsCount = 0; // stores the number of compared features for this document to compate
394   std::set<std::string> aProcessed; // processed features names (that are in the current document)
395
396   // process all objects (features and folders)
397   std::list<ObjectPtr> allObjects = theDoc->allObjects();
398   std::list<ObjectPtr>::iterator allIter = allObjects.begin();
399   for(; allIter != allObjects.end(); allIter++) {
400     ObjectPtr anObject = *allIter;
401     if (theCompare) {
402       std::map<std::string, ModelHighAPI_FeatureStore>::iterator
403         anObjFind = aDocFind->second.find(anObject->data()->name());
404       if (anObjFind == aDocFind->second.end()) {
405         return "Document '" + theDocName + "' feature '" + anObject->data()->name() + "' not found";
406       }
407       std::string anError = anObjFind->second.compare(anObject);
408       if (!anError.empty()) {
409         anError = "Document " + theDocName + " " + anError;
410         return anError;
411       }
412       anObjectsCount++;
413       aProcessed.insert(anObject->data()->name());
414     } else {
415       theStore[theDocName][anObject->data()->name()] = ModelHighAPI_FeatureStore(anObject);
416     }
417
418     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
419     if (aFeature) {
420       // iterate all results of this feature
421       std::list<ResultPtr> allResults;
422       ModelAPI_Tools::allResults(aFeature, allResults);
423       std::list<ResultPtr>::iterator aRes = allResults.begin();
424       for(; aRes != allResults.end(); aRes++) {
425         // recoursively store features of sub-documents
426         if ((*aRes)->groupName() == ModelAPI_ResultPart::group()) {
427           DocumentPtr aDoc = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes)->partDoc();
428           if (aDoc.get()) {
429             std::string anError =
430                 storeFeatures((*aRes)->data()->name(), aDoc, theStore, theCompare);
431             if (!anError.empty())
432               return anError;
433           }
434         }
435       }
436     }
437   }
438   // checks the number of compared features
439   if (theCompare) {
440     if (aDocFind->second.size() != anObjectsCount) {
441       // search for disappeared feature
442       std::string aLostName;
443       std::map<std::string, ModelHighAPI_FeatureStore>::iterator aLostIter;
444       for(aLostIter = aDocFind->second.begin(); aLostIter != aDocFind->second.end(); aLostIter++) {
445         if (aProcessed.find(aLostIter->first) == aProcessed.end()) {
446           aLostName = aLostIter->first;
447         }
448       }
449       return "For document '" + theDocName +
450         "' the number of features is decreased, there is no feature '" + aLostName + "'";
451     }
452   }
453   return ""; // ok
454 }
455
456 //==================================================================================================
457 typedef std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> > Storage;
458
459 static bool dumpToPython(SessionPtr theSession,
460                          const char* theFilename,
461                          const char* theSelectionType,
462                          const std::string& theErrorMsgContext)
463 {
464   // 2431: set PartSet as a current document
465   theSession->setActiveDocument(theSession->moduleDocument(), true);
466   // dump all to the python file
467   theSession->startOperation("Check python dump");
468   FeaturePtr aDump = theSession->moduleDocument()->addFeature("Dump");
469   if (aDump.get()) {
470     aDump->string("file_path")->setValue(theFilename);
471     aDump->string("file_format")->setValue("py");
472     aDump->string("selection_type")->setValue(theSelectionType);
473     aDump->execute();
474   }
475   bool isProblem = !aDump.get() || !aDump->error().empty(); // after "finish" dump will be removed
476   if (isProblem && aDump.get()) {
477     std::cout << "Dump feature error " << aDump->error() << std::endl;
478     Events_InfoMessage anErrorMsg(theErrorMsgContext, aDump->error());
479     anErrorMsg.send();
480   }
481   theSession->finishOperation();
482   return !isProblem;
483 }
484
485 static bool checkDump(SessionPtr theSession,
486                       char* theFilename,
487                       Storage& theStorage,
488                       const std::string& theErrorMsgContext)
489 {
490
491   // close all before importation of the script
492   theSession->closeAll();
493
494   // execute the dumped
495   PyGILState_STATE gstate = PyGILState_Ensure(); /* acquire python thread */
496   static char aReadMode[] = "r";
497   FILE* PyFileObject = _Py_fopen(theFilename, aReadMode);
498   PyRun_SimpleFileEx(PyFileObject, theFilename, 1);
499   PyGILState_Release(gstate); /* release python thread */
500
501   // compare with the stored data
502   std::string anError = storeFeatures(
503     theSession->moduleDocument()->kind(), theSession->moduleDocument(), theStorage, true);
504   if (!anError.empty()) {
505     std::cout << anError << std::endl;
506     Events_InfoMessage anErrorMsg(theErrorMsgContext, anError);
507     anErrorMsg.send();
508     return false;
509   }
510
511   return true;
512 }
513
514 bool checkPythonDump(const bool theWeakNameCheck)
515 {
516   static const std::string anErrorByNaming("checkPythonDump by naming");
517   static const std::string anErrorByGeometry("checkPythonDump by geometry");
518   static const std::string anErrorByWeak("checkPythonDump by weak naming");
519
520   static char aFileForNamingDump[] = "./check_dump_byname.py";
521   static char aFileForGeometryDump[] = "./check_dump_bygeom.py";
522   static char aFileForWeakDump[] = "./check_dump_weak.py";
523
524   SessionPtr aSession = ModelAPI_Session::get();
525   if (!theWeakNameCheck) {
526     // dump with the selection by names
527     if (!dumpToPython(aSession, aFileForNamingDump, "topological_naming", anErrorByNaming))
528       return false;
529     // dump with the selection by geometry
530     if (!dumpToPython(aSession, aFileForGeometryDump, "geometric_selection", anErrorByGeometry))
531       return false;
532   } else {
533     // dump with the selection by weak naming
534     if (!dumpToPython(aSession, aFileForWeakDump, "weak_naming", anErrorByWeak))
535       return false;
536   }
537
538    // map from document name to feature name to feature data
539   std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> > aStore;
540   std::string anError = storeFeatures(
541     aSession->moduleDocument()->kind(), aSession->moduleDocument(), aStore, false);
542   if (!anError.empty()) {
543     Events_InfoMessage anErrorMsg(std::string("checkPythonDump"), anError);
544     anErrorMsg.send();
545     return false;
546   }
547
548   bool isOk;
549   if (!theWeakNameCheck) {
550     // check dump with the selection by names
551     isOk = checkDump(aSession, aFileForNamingDump, aStore, anErrorByNaming);
552     // check dump with the selection by geometry
553     isOk = isOk && checkDump(aSession, aFileForGeometryDump, aStore, anErrorByGeometry);
554   } else {
555     isOk = checkDump(aSession, aFileForWeakDump, aStore, anErrorByWeak);
556   }
557
558   return isOk;
559 }
560
561 //--------------------------------------------------------------------------------------