1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
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"
59 #include <Events_InfoMessage.h>
61 // Have to be included before std headers
67 //--------------------------------------------------------------------------------------
68 void fillAttribute(const std::shared_ptr<GeomAPI_Pnt2d> & theValue,
69 const std::shared_ptr<GeomDataAPI_Point2D> & theAttribute)
71 theAttribute->setValue(theValue);
74 void fillAttribute(const std::shared_ptr<GeomDataAPI_Point2D> & theAttribute,
75 double theX, double theY)
77 theAttribute->setValue(theX, theY);
80 //--------------------------------------------------------------------------------------
81 void fillAttribute(const std::shared_ptr<GeomAPI_Dir> & theValue,
82 const std::shared_ptr<GeomDataAPI_Dir> & theAttribute)
84 theAttribute->setValue(theValue);
87 //--------------------------------------------------------------------------------------
88 void fillAttribute(const std::shared_ptr<GeomAPI_Pnt> & theValue,
89 const std::shared_ptr<GeomDataAPI_Point> & theAttribute)
91 theAttribute->setValue(theValue);
94 //--------------------------------------------------------------------------------------
95 void fillAttribute(bool theValue,
96 const std::shared_ptr<ModelAPI_AttributeBoolean> & theAttribute)
98 theAttribute->setValue(theValue);
101 //--------------------------------------------------------------------------------------
102 void fillAttribute(const ModelHighAPI_Double & theValue,
103 const std::shared_ptr<ModelAPI_AttributeDouble> & theAttribute)
105 theValue.fillAttribute(theAttribute);
107 void fillAttribute(double theValue,
108 const std::shared_ptr<ModelAPI_AttributeDouble> & theAttribute)
110 theAttribute->setValue(theValue);
113 //--------------------------------------------------------------------------------------
114 void fillAttribute(const ModelHighAPI_Integer & theValue,
115 const std::shared_ptr<ModelAPI_AttributeInteger> & theAttribute)
117 theValue.fillAttribute(theAttribute);
119 void fillAttribute(int theValue,
120 const std::shared_ptr<ModelAPI_AttributeInteger> & theAttribute)
122 theAttribute->setValue(theValue);
125 //--------------------------------------------------------------------------------------
126 void fillAttribute(const ModelHighAPI_RefAttr & theValue,
127 const std::shared_ptr<ModelAPI_AttributeRefAttr> & theAttribute)
129 theValue.fillAttribute(theAttribute);
132 //--------------------------------------------------------------------------------------
133 void fillAttribute(const std::list<ModelHighAPI_RefAttr> & theValue,
134 const std::shared_ptr<ModelAPI_AttributeRefAttrList> & theAttribute)
136 theAttribute->clear();
137 for (auto it = theValue.begin(); it != theValue.end(); ++it)
138 it->appendToList(theAttribute);
141 //--------------------------------------------------------------------------------------
142 void fillAttribute(const ModelHighAPI_Reference & theValue,
143 const std::shared_ptr<ModelAPI_AttributeReference> & theAttribute)
145 theValue.fillAttribute(theAttribute);
148 //--------------------------------------------------------------------------------------
149 void fillAttribute(const std::list<ModelHighAPI_Reference> & theValue,
150 const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
152 theAttribute->clear();
153 for (auto it = theValue.begin(); it != theValue.end(); ++it)
154 it->appendToList(theAttribute);
157 //--------------------------------------------------------------------------------------
158 void fillAttribute(const std::shared_ptr<ModelAPI_Object> & theValue,
159 const std::shared_ptr<ModelAPI_AttributeReference> & theAttribute)
161 theAttribute->setValue(theValue);
164 //--------------------------------------------------------------------------------------
165 void fillAttribute(const std::list<std::shared_ptr<ModelAPI_Object> > & theValue,
166 const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
168 theAttribute->clear();
169 for (auto it = theValue.begin(); it != theValue.end(); ++it)
170 theAttribute->append(*it);
174 void fillAttribute(const std::list<ModelHighAPI_Selection> & theValue,
175 const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
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
184 //--------------------------------------------------------------------------------------
185 void fillAttribute(const ModelHighAPI_Selection & theValue,
186 const std::shared_ptr<ModelAPI_AttributeSelection> & theAttribute)
188 theValue.fillAttribute(theAttribute);
191 //--------------------------------------------------------------------------------------
192 void fillAttribute(const std::list<ModelHighAPI_Selection> & theValue,
193 const std::shared_ptr<ModelAPI_AttributeSelectionList> & theAttribute)
195 theAttribute->clear();
197 if(!theValue.empty()) {
198 const ModelHighAPI_Selection& aSelection = theValue.front();
199 GeomAPI_Shape::ShapeType aSelectionType = getShapeType(aSelection);
200 theAttribute->setSelectionType(strByShapeType(aSelectionType));
203 for (auto it = theValue.begin(); it != theValue.end(); ++it)
204 it->appendToList(theAttribute);
207 //--------------------------------------------------------------------------------------
208 void fillAttribute(const std::string & theValue,
209 const std::shared_ptr<ModelAPI_AttributeString> & theAttribute)
211 theAttribute->setValue(theValue);
214 //--------------------------------------------------------------------------------------
215 void fillAttribute(const char * theValue,
216 const std::shared_ptr<ModelAPI_AttributeString> & theAttribute)
218 theAttribute->setValue(theValue);
221 //--------------------------------------------------------------------------------------
222 void fillAttribute(const std::list<std::string> & theValue,
223 const std::shared_ptr<ModelAPI_AttributeStringArray> & theAttribute)
225 theAttribute->setSize(int(theValue.size()));
228 for (auto it = theValue.begin(); it != theValue.end(); ++it, ++anIndex)
229 theAttribute->setValue(anIndex, *it);
232 //--------------------------------------------------------------------------------------
233 void fillAttribute(const std::list<ModelHighAPI_Integer> & theValue,
234 const std::shared_ptr<ModelAPI_AttributeIntArray> & theAttribute)
236 theAttribute->setSize(int(theValue.size()));
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
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)
248 theX.fillAttribute(theAttribute, theX, theY, theZ);
252 //==================================================================================================
253 GeomAPI_Shape::ShapeType shapeTypeByStr(std::string theShapeTypeStr)
255 GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
257 std::transform(theShapeTypeStr.begin(), theShapeTypeStr.end(),
258 theShapeTypeStr.begin(), ::tolower);
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;
283 std::string strByShapeType(GeomAPI_Shape::ShapeType theShapeType)
285 std::string aShapeTypeStr;
286 switch (theShapeType) {
287 case GeomAPI_Shape::COMPOUND:
288 aShapeTypeStr = "COMPOUND";
290 case GeomAPI_Shape::COMPSOLID:
291 aShapeTypeStr = "COMPSOLID";
293 case GeomAPI_Shape::SOLID:
294 aShapeTypeStr = "SOLID";
296 case GeomAPI_Shape::SHELL:
297 aShapeTypeStr = "SHELL";
299 case GeomAPI_Shape::FACE:
300 aShapeTypeStr = "FACE";
302 case GeomAPI_Shape::WIRE:
303 aShapeTypeStr = "WIRE";
305 case GeomAPI_Shape::EDGE:
306 aShapeTypeStr = "EDGE";
308 case GeomAPI_Shape::VERTEX:
309 aShapeTypeStr = "VERTEX";
312 aShapeTypeStr = "SHAPE";
315 return aShapeTypeStr;
318 //==================================================================================================
319 GeomAPI_Shape::ShapeType getShapeType(const ModelHighAPI_Selection& theSelection)
321 GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
323 switch(theSelection.variantType()) {
324 case ModelHighAPI_Selection::VT_ResultSubShapePair: {
325 ResultSubShapePair aPair = theSelection.resultSubShapePair();
326 GeomShapePtr aShape = aPair.second;
328 aShape = aPair.first->shape();
333 aShapeType = aShape->shapeType();
336 case ModelHighAPI_Selection::VT_TypeSubShapeNamePair: {
337 TypeSubShapeNamePair aPair = theSelection.typeSubShapeNamePair();
338 std::string aType = aPair.first;
339 aShapeType = shapeTypeByStr(aType);
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);
354 //--------------------------------------------------------------------------------------
355 ModelAPI_AttributeTables::ValueType valueTypeByStr(const std::string& theValueTypeStr)
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
368 //--------------------------------------------------------------------------------------
369 std::string strByValueType(const ModelAPI_AttributeTables::ValueType 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";
377 return ""; // bad case
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
385 std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> >::iterator aDocFind;
387 aDocFind = theStore.find(theDocName);
388 if (aDocFind == theStore.end()) {
389 return "Document '" + theDocName + "' not found";
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)
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;
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";
407 std::string anError = anObjFind->second.compare(anObject);
408 if (!anError.empty()) {
409 anError = "Document " + theDocName + " " + anError;
413 aProcessed.insert(anObject->data()->name());
415 theStore[theDocName][anObject->data()->name()] = ModelHighAPI_FeatureStore(anObject);
418 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
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();
429 std::string anError =
430 storeFeatures((*aRes)->data()->name(), aDoc, theStore, theCompare);
431 if (!anError.empty())
438 // checks the number of compared features
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;
449 return "For document '" + theDocName +
450 "' the number of features is decreased, there is no feature '" + aLostName + "'";
456 //==================================================================================================
457 typedef std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> > Storage;
459 static bool dumpToPython(SessionPtr theSession,
460 const char* theFilename,
461 const char* theSelectionType,
462 const std::string& theErrorMsgContext)
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");
470 aDump->string("file_path")->setValue(theFilename);
471 aDump->string("file_format")->setValue("py");
472 aDump->string("selection_type")->setValue(theSelectionType);
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());
481 theSession->finishOperation();
485 static bool checkDump(SessionPtr theSession,
488 const std::string& theErrorMsgContext)
491 // close all before importation of the script
492 theSession->closeAll();
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 */
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);
514 bool checkPythonDump(const bool theWeakNameCheck)
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");
520 static char aFileForNamingDump[] = "./check_dump_byname.py";
521 static char aFileForGeometryDump[] = "./check_dump_bygeom.py";
522 static char aFileForWeakDump[] = "./check_dump_weak.py";
524 SessionPtr aSession = ModelAPI_Session::get();
525 if (!theWeakNameCheck) {
526 // dump with the selection by names
527 if (!dumpToPython(aSession, aFileForNamingDump, "topological_naming", anErrorByNaming))
529 // dump with the selection by geometry
530 if (!dumpToPython(aSession, aFileForGeometryDump, "geometric_selection", anErrorByGeometry))
533 // dump with the selection by weak naming
534 if (!dumpToPython(aSession, aFileForWeakDump, "weak_naming", anErrorByWeak))
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);
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);
555 isOk = checkDump(aSession, aFileForWeakDump, aStore, anErrorByWeak);
561 //--------------------------------------------------------------------------------------