Salome HOME
Task 2.12. New entities: ellipses and arcs of ellipses (issue #3003)
[modules/shaper.git] / src / ModelHighAPI / ModelHighAPI_Tools.cpp
1 // Copyright (C) 2014-2019  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
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 #include <ModelAPI_Events.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() && theAttribute->selectionType().empty()) {
197     const ModelHighAPI_Selection& aSelection = theValue.front();
198     GeomAPI_Shape::ShapeType aSelectionType = getShapeType(aSelection);
199     theAttribute->setSelectionType(strByShapeType(aSelectionType));
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 std::string strByShapeType(GeomAPI_Shape::ShapeType theShapeType)
283 {
284   std::string aShapeTypeStr;
285   switch (theShapeType) {
286   case GeomAPI_Shape::COMPOUND:
287     aShapeTypeStr = "COMPOUND";
288     break;
289   case GeomAPI_Shape::COMPSOLID:
290     aShapeTypeStr = "COMPSOLID";
291     break;
292   case GeomAPI_Shape::SOLID:
293     aShapeTypeStr = "SOLID";
294     break;
295   case GeomAPI_Shape::SHELL:
296     aShapeTypeStr = "SHELL";
297     break;
298   case GeomAPI_Shape::FACE:
299     aShapeTypeStr = "FACE";
300     break;
301   case GeomAPI_Shape::WIRE:
302     aShapeTypeStr = "WIRE";
303     break;
304   case GeomAPI_Shape::EDGE:
305     aShapeTypeStr = "EDGE";
306     break;
307   case GeomAPI_Shape::VERTEX:
308     aShapeTypeStr = "VERTEX";
309     break;
310   default:
311     aShapeTypeStr = "SHAPE";
312     break;
313   }
314   return aShapeTypeStr;
315 }
316
317 //==================================================================================================
318 GeomAPI_Shape::ShapeType getShapeType(const ModelHighAPI_Selection& theSelection)
319 {
320   GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
321
322   switch(theSelection.variantType()) {
323     case ModelHighAPI_Selection::VT_ResultSubShapePair: {
324       ResultSubShapePair aPair = theSelection.resultSubShapePair();
325       GeomShapePtr aShape = aPair.second;
326       if(!aShape.get()) {
327         aShape = aPair.first->shape();
328       }
329       if(!aShape.get()) {
330         return aShapeType;
331       }
332       aShapeType = aShape->shapeType();
333       break;
334     }
335     case ModelHighAPI_Selection::VT_TypeSubShapeNamePair: {
336       TypeSubShapeNamePair aPair = theSelection.typeSubShapeNamePair();
337       std::string aType = aPair.first;
338       aShapeType = shapeTypeByStr(aType);
339       break;
340     }
341     case ModelHighAPI_Selection::VT_TypeInnerPointPair: {
342       TypeInnerPointPair aPair = theSelection.typeInnerPointPair();
343       std::string aType = aPair.first;
344       aType = aType.substr(0, aType.find_first_of('_'));
345       aShapeType = shapeTypeByStr(aType);
346       break;
347     }
348     case ModelHighAPI_Selection::VT_WeakNamingPair: {
349       TypeWeakNamingPair aPair = theSelection.typeWeakNamingPair();
350       std::string aType = aPair.first;
351       aShapeType = shapeTypeByStr(aType);
352       break;
353     }
354   }
355
356   return aShapeType;
357 }
358
359 //--------------------------------------------------------------------------------------
360 ModelAPI_AttributeTables::ValueType valueTypeByStr(const std::string& theValueTypeStr)
361 {
362   std::string aType = theValueTypeStr;
363   std::transform(aType.begin(), aType.end(), aType.begin(), ::tolower);
364   if (aType == "boolean")
365     return ModelAPI_AttributeTables::BOOLEAN;
366   else if (aType == "integer")
367     return ModelAPI_AttributeTables::INTEGER;
368   else if (aType == "string")
369     return ModelAPI_AttributeTables::STRING;
370   return ModelAPI_AttributeTables::DOUBLE; // default
371 }
372
373 //--------------------------------------------------------------------------------------
374 std::string strByValueType(const ModelAPI_AttributeTables::ValueType theType)
375 {
376   switch(theType) {
377   case ModelAPI_AttributeTables::BOOLEAN: return "BOOLEAN";
378   case ModelAPI_AttributeTables::INTEGER: return "INTEGER";
379   case ModelAPI_AttributeTables::DOUBLE: return "DOUBLE";
380   case ModelAPI_AttributeTables::STRING: return "STRING";
381   }
382   return ""; // bad case
383 }
384
385 /// stores the features information, recursively stores sub-documents features
386 std::string storeFeatures(const std::string& theDocName, DocumentPtr theDoc,
387   std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> >& theStore,
388   const bool theCompare) // if false => store
389 {
390   std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> >::iterator aDocFind;
391   if (theCompare) {
392      aDocFind = theStore.find(theDocName);
393      if (aDocFind == theStore.end()) {
394        return "Document '" + theDocName + "' not found";
395      }
396   }
397   // store the model features information: iterate all features
398   int anObjectsCount = 0; // stores the number of compared features for this document to compare
399   std::set<std::string> aProcessed; // processed features names (that are in the current document)
400
401   // process all objects (features and folders)
402   std::list<ObjectPtr> allObjects = theDoc->allObjects();
403   std::list<ObjectPtr>::iterator allIter = allObjects.begin();
404   for(; allIter != allObjects.end(); allIter++) {
405     ObjectPtr anObject = *allIter;
406     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
407     if (aFeature->getKind() == "SketchConstraintCoincidenceInternal")
408       continue; // no need to dump and check internal constraints
409
410     if (theCompare) {
411       std::map<std::string, ModelHighAPI_FeatureStore>::iterator
412         anObjFind = aDocFind->second.find(anObject->data()->name());
413       if (anObjFind == aDocFind->second.end()) {
414         return "Document '" + theDocName + "' feature '" + anObject->data()->name() + "' not found";
415       }
416       std::string anError = anObjFind->second.compare(anObject);
417       if (!anError.empty()) {
418         anError = "Document " + theDocName + " " + anError;
419         return anError;
420       }
421       anObjectsCount++;
422       aProcessed.insert(anObject->data()->name());
423     } else {
424       theStore[theDocName][anObject->data()->name()] = ModelHighAPI_FeatureStore(anObject);
425     }
426
427     if (aFeature) {
428       // iterate all results of this feature
429       std::list<ResultPtr> allResults;
430       ModelAPI_Tools::allResults(aFeature, allResults);
431       std::list<ResultPtr>::iterator aRes = allResults.begin();
432       for(; aRes != allResults.end(); aRes++) {
433         // recursively store features of sub-documents
434         if ((*aRes)->groupName() == ModelAPI_ResultPart::group()) {
435           DocumentPtr aDoc = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes)->partDoc();
436           if (aDoc.get()) {
437             std::string anError =
438                 storeFeatures((*aRes)->data()->name(), aDoc, theStore, theCompare);
439             if (!anError.empty())
440               return anError;
441           }
442         }
443       }
444     }
445   }
446   // checks the number of compared features
447   if (theCompare) {
448     if (aDocFind->second.size() != anObjectsCount) {
449       // search for disappeared feature
450       std::string aLostName;
451       std::map<std::string, ModelHighAPI_FeatureStore>::iterator aLostIter;
452       for(aLostIter = aDocFind->second.begin(); aLostIter != aDocFind->second.end(); aLostIter++) {
453         if (aProcessed.find(aLostIter->first) == aProcessed.end()) {
454           aLostName = aLostIter->first;
455         }
456       }
457       return "For document '" + theDocName +
458         "' the number of features is decreased, there is no feature '" + aLostName + "'";
459     }
460   }
461   return ""; // ok
462 }
463
464 //==================================================================================================
465 typedef std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> > Storage;
466
467 static bool dumpToPython(SessionPtr theSession,
468                          const char* theFilename,
469                          const checkDumpType theSelectionType,
470                          const std::string& theErrorMsgContext)
471 {
472   // 2431: set PartSet as a current document
473   theSession->setActiveDocument(theSession->moduleDocument(), true);
474   // dump all to the python file
475   theSession->startOperation("Check python dump");
476   FeaturePtr aDump = theSession->moduleDocument()->addFeature("Dump");
477   if (aDump.get()) {
478     aDump->string("file_path")->setValue(theFilename);
479     aDump->string("file_format")->setValue("py");
480     aDump->boolean("topological_naming")->setValue((theSelectionType & CHECK_NAMING) != 0);
481     aDump->boolean("geometric_selection")->setValue((theSelectionType & CHECK_GEOMETRICAL) != 0);
482     aDump->boolean("weak_naming")->setValue((theSelectionType & CHECK_WEAK) != 0);
483   }
484   bool isProblem = !aDump.get() || !aDump->error().empty(); // after "finish" dump will be removed
485   if (isProblem && aDump.get()) {
486     std::cout << "Dump feature error " << aDump->error() << std::endl;
487     Events_InfoMessage anErrorMsg(theErrorMsgContext, aDump->error());
488     anErrorMsg.send();
489   }
490   theSession->finishOperation();
491   return !isProblem;
492 }
493
494 static bool checkDump(SessionPtr theSession,
495                       char* theFilename,
496                       Storage& theStorage,
497                       const std::string& theErrorMsgContext)
498 {
499
500   // close all before importation of the script
501   theSession->closeAll();
502
503   // execute the dumped
504   PyGILState_STATE gstate = PyGILState_Ensure(); /* acquire python thread */
505   static char aReadMode[] = "r";
506   FILE* PyFileObject = _Py_fopen(theFilename, aReadMode);
507   PyRun_SimpleFileEx(PyFileObject, theFilename, 1);
508   PyGILState_Release(gstate); /* release python thread */
509
510   // compare with the stored data
511   std::string anError = storeFeatures(
512     theSession->moduleDocument()->kind(), theSession->moduleDocument(), theStorage, true);
513   if (!anError.empty()) {
514     std::cout << anError << std::endl;
515     Events_InfoMessage anErrorMsg(theErrorMsgContext, anError);
516     anErrorMsg.send();
517     return false;
518   }
519
520   return true;
521 }
522
523 bool checkPythonDump(const checkDumpType theCheckType)
524 {
525   static const std::string anErrorByNaming("checkPythonDump by naming");
526   static const std::string anErrorByGeometry("checkPythonDump by geometry");
527   static const std::string anErrorByWeak("checkPythonDump by weak naming");
528
529   static char aFileForNamingDump[] = "./check_dump.py";
530   static char aFileForGeometryDump[] = "./check_dump_geo.py";
531   static char aFileForWeakDump[] = "./check_dump_weak.py";
532
533   SessionPtr aSession = ModelAPI_Session::get();
534   // dump with the specified types
535   char* aFileName = theCheckType == CHECK_GEOMETRICAL ? aFileForGeometryDump :
536                    (theCheckType == CHECK_WEAK ? aFileForWeakDump : aFileForNamingDump);
537   if (!dumpToPython(aSession, aFileName, theCheckType, anErrorByNaming))
538     return false;
539
540    // map from document name to feature name to feature data
541   std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> > aStore;
542   std::string anError = storeFeatures(
543     aSession->moduleDocument()->kind(), aSession->moduleDocument(), aStore, false);
544   if (!anError.empty()) {
545     Events_InfoMessage anErrorMsg(std::string("checkPythonDump"), anError);
546     anErrorMsg.send();
547     return false;
548   }
549
550   bool isOk = true;
551   if (theCheckType & CHECK_NAMING) {
552     // check dump with the selection by names
553     isOk = checkDump(aSession, aFileForNamingDump, aStore, anErrorByNaming);
554   }
555   if (theCheckType & CHECK_GEOMETRICAL) {
556     // check dump with the selection by geometry
557     isOk = isOk && checkDump(aSession, aFileForGeometryDump, aStore, anErrorByGeometry);
558   }
559   if (theCheckType & CHECK_WEAK) {
560     isOk = isOk && checkDump(aSession, aFileForWeakDump, aStore, anErrorByWeak);
561   }
562
563   return isOk;
564 }
565
566 //--------------------------------------------------------------------------------------