1 // Copyright (C) 2014-2019 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 email : webmaster.salome@opencascade.com
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 #include <GeomDataAPI_Point2DArray.h>
31 //--------------------------------------------------------------------------------------
32 #include <ModelAPI_AttributeBoolean.h>
33 #include <ModelAPI_AttributeDocRef.h>
34 #include <ModelAPI_AttributeDouble.h>
35 #include <ModelAPI_AttributeDoubleArray.h>
36 #include <ModelAPI_AttributeIntArray.h>
37 #include <ModelAPI_AttributeInteger.h>
38 #include <ModelAPI_AttributeRefAttr.h>
39 #include <ModelAPI_AttributeRefAttrList.h>
40 #include <ModelAPI_AttributeReference.h>
41 #include <ModelAPI_AttributeRefList.h>
42 #include <ModelAPI_AttributeSelection.h>
43 #include <ModelAPI_AttributeSelectionList.h>
44 #include <ModelAPI_AttributeString.h>
45 #include <ModelAPI_AttributeStringArray.h>
46 #include <ModelAPI_AttributeDoubleArray.h>
47 #include <ModelAPI_Session.h>
48 #include <ModelAPI_Tools.h>
49 #include <ModelAPI_ResultPart.h>
50 #include <ModelAPI_Events.h>
51 //--------------------------------------------------------------------------------------
52 #include <Config_ModuleReader.h>
53 //--------------------------------------------------------------------------------------
54 #include "ModelHighAPI_Double.h"
55 #include "ModelHighAPI_Integer.h"
56 #include "ModelHighAPI_RefAttr.h"
57 #include "ModelHighAPI_Reference.h"
58 #include "ModelHighAPI_Selection.h"
60 #include <Events_InfoMessage.h>
62 // Have to be included before std headers
68 //--------------------------------------------------------------------------------------
69 void fillAttribute(const std::shared_ptr<GeomAPI_Pnt2d> & theValue,
70 const std::shared_ptr<GeomDataAPI_Point2D> & theAttribute)
72 theAttribute->setValue(theValue);
75 void fillAttribute(const std::shared_ptr<GeomDataAPI_Point2D> & theAttribute,
76 double theX, double theY)
78 theAttribute->setValue(theX, theY);
81 //--------------------------------------------------------------------------------------
82 void fillAttribute(const std::shared_ptr<GeomAPI_Dir> & theValue,
83 const std::shared_ptr<GeomDataAPI_Dir> & theAttribute)
85 theAttribute->setValue(theValue);
88 //--------------------------------------------------------------------------------------
89 void fillAttribute(const std::shared_ptr<GeomAPI_Pnt> & theValue,
90 const std::shared_ptr<GeomDataAPI_Point> & theAttribute)
92 theAttribute->setValue(theValue);
95 //--------------------------------------------------------------------------------------
96 void fillAttribute(bool theValue,
97 const std::shared_ptr<ModelAPI_AttributeBoolean> & theAttribute)
99 theAttribute->setValue(theValue);
102 //--------------------------------------------------------------------------------------
103 void fillAttribute(const ModelHighAPI_Double & theValue,
104 const std::shared_ptr<ModelAPI_AttributeDouble> & theAttribute)
106 theValue.fillAttribute(theAttribute);
108 void fillAttribute(double theValue,
109 const std::shared_ptr<ModelAPI_AttributeDouble> & theAttribute)
111 theAttribute->setValue(theValue);
114 //--------------------------------------------------------------------------------------
115 void fillAttribute(const ModelHighAPI_Integer & theValue,
116 const std::shared_ptr<ModelAPI_AttributeInteger> & theAttribute)
118 theValue.fillAttribute(theAttribute);
120 void fillAttribute(int theValue,
121 const std::shared_ptr<ModelAPI_AttributeInteger> & theAttribute)
123 theAttribute->setValue(theValue);
126 //--------------------------------------------------------------------------------------
127 void fillAttribute(const ModelHighAPI_RefAttr & theValue,
128 const std::shared_ptr<ModelAPI_AttributeRefAttr> & theAttribute)
130 theValue.fillAttribute(theAttribute);
133 //--------------------------------------------------------------------------------------
134 void fillAttribute(const std::list<ModelHighAPI_RefAttr> & theValue,
135 const std::shared_ptr<ModelAPI_AttributeRefAttrList> & theAttribute)
137 theAttribute->clear();
138 for (auto it = theValue.begin(); it != theValue.end(); ++it)
139 it->appendToList(theAttribute);
142 //--------------------------------------------------------------------------------------
143 void fillAttribute(const ModelHighAPI_Reference & theValue,
144 const std::shared_ptr<ModelAPI_AttributeReference> & theAttribute)
146 theValue.fillAttribute(theAttribute);
149 //--------------------------------------------------------------------------------------
150 void fillAttribute(const std::list<ModelHighAPI_Reference> & theValue,
151 const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
153 theAttribute->clear();
154 for (auto it = theValue.begin(); it != theValue.end(); ++it)
155 it->appendToList(theAttribute);
158 //--------------------------------------------------------------------------------------
159 void fillAttribute(const std::shared_ptr<ModelAPI_Object> & theValue,
160 const std::shared_ptr<ModelAPI_AttributeReference> & theAttribute)
162 theAttribute->setValue(theValue);
165 //--------------------------------------------------------------------------------------
166 void fillAttribute(const std::list<std::shared_ptr<ModelAPI_Object> > & theValue,
167 const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
169 theAttribute->clear();
170 for (auto it = theValue.begin(); it != theValue.end(); ++it)
171 theAttribute->append(*it);
175 void fillAttribute(const std::list<ModelHighAPI_Selection> & theValue,
176 const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
178 theAttribute->clear();
179 for (auto it = theValue.begin(); it != theValue.end(); ++it) {
180 if (it->resultSubShapePair().first)
181 theAttribute->append(it->resultSubShapePair().first); // use only context
185 //--------------------------------------------------------------------------------------
186 void fillAttribute(const ModelHighAPI_Selection & theValue,
187 const std::shared_ptr<ModelAPI_AttributeSelection> & theAttribute)
189 theValue.fillAttribute(theAttribute);
192 //--------------------------------------------------------------------------------------
193 void fillAttribute(const std::list<ModelHighAPI_Selection> & theValue,
194 const std::shared_ptr<ModelAPI_AttributeSelectionList> & theAttribute)
196 theAttribute->clear();
198 if(!theValue.empty() && theAttribute->selectionType().empty()) {
199 const ModelHighAPI_Selection& aSelection = theValue.front();
200 GeomAPI_Shape::ShapeType aSelectionType = getShapeType(aSelection);
201 theAttribute->setSelectionType(strByShapeType(aSelectionType));
204 for (auto it = theValue.begin(); it != theValue.end(); ++it)
205 it->appendToList(theAttribute);
208 //--------------------------------------------------------------------------------------
209 void fillAttribute(const std::string & theValue,
210 const std::shared_ptr<ModelAPI_AttributeString> & theAttribute)
212 theAttribute->setValue(theValue);
215 //--------------------------------------------------------------------------------------
216 void fillAttribute(const char * theValue,
217 const std::shared_ptr<ModelAPI_AttributeString> & theAttribute)
219 theAttribute->setValue(theValue);
222 //--------------------------------------------------------------------------------------
223 void fillAttribute(const std::list<std::string> & theValue,
224 const std::shared_ptr<ModelAPI_AttributeStringArray> & theAttribute)
226 theAttribute->setSize(int(theValue.size()));
229 for (auto it = theValue.begin(); it != theValue.end(); ++it, ++anIndex)
230 theAttribute->setValue(anIndex, *it);
233 //--------------------------------------------------------------------------------------
234 void fillAttribute(const std::list<ModelHighAPI_Integer> & theValue,
235 const std::shared_ptr<ModelAPI_AttributeIntArray> & theAttribute)
237 theAttribute->setSize(int(theValue.size()));
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
244 //--------------------------------------------------------------------------------------
245 void fillAttribute(const std::list<ModelHighAPI_Double> & theValue,
246 const std::shared_ptr<ModelAPI_AttributeDoubleArray> & theAttribute)
248 theAttribute->setSize(int(theValue.size()));
251 for (auto it = theValue.begin(); it != theValue.end(); ++it, ++anIndex)
252 theAttribute->setValue(anIndex, it->value()); // use only values, no text support in array
255 //--------------------------------------------------------------------------------------
256 void fillAttribute(const std::list<std::shared_ptr<GeomAPI_Pnt2d> > & theValue,
257 const std::shared_ptr<GeomDataAPI_Point2DArray> & theAttribute)
259 theAttribute->setSize(int(theValue.size()));
262 for (auto it = theValue.begin(); it != theValue.end(); ++it, ++anIndex)
263 theAttribute->setPnt(anIndex, *it);
266 //--------------------------------------------------------------------------------------
267 void fillAttribute(const ModelHighAPI_Double & theX,
268 const ModelHighAPI_Double & theY,
269 const ModelHighAPI_Double & theZ,
270 const std::shared_ptr<GeomDataAPI_Point> & theAttribute)
272 theX.fillAttribute(theAttribute, theX, theY, theZ);
275 //==================================================================================================
276 GeomAPI_Shape::ShapeType shapeTypeByStr(std::string theShapeTypeStr)
278 GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
280 std::transform(theShapeTypeStr.begin(), theShapeTypeStr.end(),
281 theShapeTypeStr.begin(), ::tolower);
283 if(theShapeTypeStr == "compound") {
284 aShapeType = GeomAPI_Shape::COMPOUND;
285 } else if(theShapeTypeStr == "compsolid") {
286 aShapeType = GeomAPI_Shape::COMPSOLID;
287 } else if(theShapeTypeStr == "solid") {
288 aShapeType = GeomAPI_Shape::SOLID;
289 } else if(theShapeTypeStr == "shell") {
290 aShapeType = GeomAPI_Shape::SHELL;
291 } else if(theShapeTypeStr == "face") {
292 aShapeType = GeomAPI_Shape::FACE;
293 } else if(theShapeTypeStr == "wire") {
294 aShapeType = GeomAPI_Shape::WIRE;
295 } else if(theShapeTypeStr == "edge") {
296 aShapeType = GeomAPI_Shape::EDGE;
297 } else if(theShapeTypeStr == "vertex") {
298 aShapeType = GeomAPI_Shape::VERTEX;
299 } else if(theShapeTypeStr == "shape") {
300 aShapeType = GeomAPI_Shape::SHAPE;
306 std::string strByShapeType(GeomAPI_Shape::ShapeType theShapeType)
308 std::string aShapeTypeStr;
309 switch (theShapeType) {
310 case GeomAPI_Shape::COMPOUND:
311 aShapeTypeStr = "COMPOUND";
313 case GeomAPI_Shape::COMPSOLID:
314 aShapeTypeStr = "COMPSOLID";
316 case GeomAPI_Shape::SOLID:
317 aShapeTypeStr = "SOLID";
319 case GeomAPI_Shape::SHELL:
320 aShapeTypeStr = "SHELL";
322 case GeomAPI_Shape::FACE:
323 aShapeTypeStr = "FACE";
325 case GeomAPI_Shape::WIRE:
326 aShapeTypeStr = "WIRE";
328 case GeomAPI_Shape::EDGE:
329 aShapeTypeStr = "EDGE";
331 case GeomAPI_Shape::VERTEX:
332 aShapeTypeStr = "VERTEX";
335 aShapeTypeStr = "SHAPE";
338 return aShapeTypeStr;
341 //==================================================================================================
342 GeomAPI_Shape::ShapeType getShapeType(const ModelHighAPI_Selection& theSelection)
344 GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
346 switch(theSelection.variantType()) {
347 case ModelHighAPI_Selection::VT_ResultSubShapePair: {
348 ResultSubShapePair aPair = theSelection.resultSubShapePair();
349 GeomShapePtr aShape = aPair.second;
351 aShape = aPair.first->shape();
356 aShapeType = aShape->shapeType();
359 case ModelHighAPI_Selection::VT_TypeSubShapeNamePair: {
360 TypeSubShapeNamePair aPair = theSelection.typeSubShapeNamePair();
361 std::string aType = aPair.first;
362 aShapeType = shapeTypeByStr(aType);
365 case ModelHighAPI_Selection::VT_TypeInnerPointPair: {
366 TypeInnerPointPair aPair = theSelection.typeInnerPointPair();
367 std::string aType = aPair.first;
368 aType = aType.substr(0, aType.find_first_of('_'));
369 aShapeType = shapeTypeByStr(aType);
372 case ModelHighAPI_Selection::VT_WeakNamingPair: {
373 TypeWeakNamingPair aPair = theSelection.typeWeakNamingPair();
374 std::string aType = aPair.first;
375 aShapeType = shapeTypeByStr(aType);
383 //--------------------------------------------------------------------------------------
384 ModelAPI_AttributeTables::ValueType valueTypeByStr(const std::string& theValueTypeStr)
386 std::string aType = theValueTypeStr;
387 std::transform(aType.begin(), aType.end(), aType.begin(), ::tolower);
388 if (aType == "boolean")
389 return ModelAPI_AttributeTables::BOOLEAN;
390 else if (aType == "integer")
391 return ModelAPI_AttributeTables::INTEGER;
392 else if (aType == "string")
393 return ModelAPI_AttributeTables::STRING;
394 return ModelAPI_AttributeTables::DOUBLE; // default
397 //--------------------------------------------------------------------------------------
398 std::string strByValueType(const ModelAPI_AttributeTables::ValueType theType)
401 case ModelAPI_AttributeTables::BOOLEAN: return "BOOLEAN";
402 case ModelAPI_AttributeTables::INTEGER: return "INTEGER";
403 case ModelAPI_AttributeTables::DOUBLE: return "DOUBLE";
404 case ModelAPI_AttributeTables::STRING: return "STRING";
406 return ""; // bad case
409 /// stores the features information, recursively stores sub-documents features
410 std::string storeFeatures(const std::string& theDocName, DocumentPtr theDoc,
411 std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> >& theStore,
412 const bool theCompare) // if false => store
414 std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> >::iterator aDocFind;
416 aDocFind = theStore.find(theDocName);
417 if (aDocFind == theStore.end()) {
418 return "Document '" + theDocName + "' not found";
421 // store the model features information: iterate all features
422 int anObjectsCount = 0; // stores the number of compared features for this document to compare
423 std::set<std::string> aProcessed; // processed features names (that are in the current document)
425 // process all objects (features and folders)
426 std::list<ObjectPtr> allObjects = theDoc->allObjects();
427 std::list<ObjectPtr>::iterator allIter = allObjects.begin();
428 for(; allIter != allObjects.end(); allIter++) {
429 ObjectPtr anObject = *allIter;
430 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
431 if (aFeature && aFeature->getKind() == "SketchConstraintCoincidenceInternal")
432 continue; // no need to dump and check internal constraints
435 std::map<std::string, ModelHighAPI_FeatureStore>::iterator
436 anObjFind = aDocFind->second.find(anObject->data()->name());
437 if (anObjFind == aDocFind->second.end()) {
438 return "Document '" + theDocName + "' feature '" + anObject->data()->name() + "' not found";
440 std::string anError = anObjFind->second.compare(anObject);
441 if (!anError.empty()) {
442 anError = "Document " + theDocName + " " + anError;
446 aProcessed.insert(anObject->data()->name());
448 theStore[theDocName][anObject->data()->name()] = ModelHighAPI_FeatureStore(anObject);
452 // iterate all results of this feature
453 std::list<ResultPtr> allResults;
454 ModelAPI_Tools::allResults(aFeature, allResults);
455 std::list<ResultPtr>::iterator aRes = allResults.begin();
456 for(; aRes != allResults.end(); aRes++) {
457 // recursively store features of sub-documents
458 if ((*aRes)->groupName() == ModelAPI_ResultPart::group()) {
459 DocumentPtr aDoc = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes)->partDoc();
461 std::string anError =
462 storeFeatures((*aRes)->data()->name(), aDoc, theStore, theCompare);
463 if (!anError.empty())
470 // checks the number of compared features
472 if (aDocFind->second.size() != anObjectsCount) {
473 // search for disappeared feature
474 std::string aLostName;
475 std::map<std::string, ModelHighAPI_FeatureStore>::iterator aLostIter;
476 for(aLostIter = aDocFind->second.begin(); aLostIter != aDocFind->second.end(); aLostIter++) {
477 if (aProcessed.find(aLostIter->first) == aProcessed.end()) {
478 aLostName = aLostIter->first;
481 return "For document '" + theDocName +
482 "' the number of features is decreased, there is no feature '" + aLostName + "'";
488 //==================================================================================================
489 typedef std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> > Storage;
491 static bool dumpToPython(SessionPtr theSession,
492 const char* theFilename,
493 const checkDumpType theSelectionType,
494 const std::string& theErrorMsgContext)
496 // 2431: set PartSet as a current document
497 theSession->setActiveDocument(theSession->moduleDocument(), true);
498 // dump all to the python file
499 theSession->startOperation("Check python dump");
500 FeaturePtr aDump = theSession->moduleDocument()->addFeature("Dump");
502 aDump->string("file_path")->setValue(theFilename);
503 aDump->string("file_format")->setValue("py");
504 aDump->boolean("topological_naming")->setValue((theSelectionType & CHECK_NAMING) != 0);
505 aDump->boolean("geometric_selection")->setValue((theSelectionType & CHECK_GEOMETRICAL) != 0);
506 aDump->boolean("weak_naming")->setValue((theSelectionType & CHECK_WEAK) != 0);
508 bool isProblem = !aDump.get() || !aDump->error().empty(); // after "finish" dump will be removed
509 if (isProblem && aDump.get()) {
510 std::cout << "Dump feature error " << aDump->error() << std::endl;
511 Events_InfoMessage anErrorMsg(theErrorMsgContext, aDump->error());
514 theSession->finishOperation();
518 static bool checkDump(SessionPtr theSession,
521 const std::string& theErrorMsgContext)
524 // close all before importation of the script
525 theSession->closeAll();
527 // execute the dumped
528 PyGILState_STATE gstate = PyGILState_Ensure(); /* acquire python thread */
529 static char aReadMode[] = "r";
530 FILE* PyFileObject = _Py_fopen(theFilename, aReadMode);
531 PyRun_SimpleFileEx(PyFileObject, theFilename, 1);
532 PyGILState_Release(gstate); /* release python thread */
534 // compare with the stored data
535 std::string anError = storeFeatures(
536 theSession->moduleDocument()->kind(), theSession->moduleDocument(), theStorage, true);
537 if (!anError.empty()) {
538 std::cout << anError << std::endl;
539 Events_InfoMessage anErrorMsg(theErrorMsgContext, anError);
547 bool checkPythonDump(const checkDumpType theCheckType)
549 static const std::string anErrorByNaming("checkPythonDump by naming");
550 static const std::string anErrorByGeometry("checkPythonDump by geometry");
551 static const std::string anErrorByWeak("checkPythonDump by weak naming");
553 static char aFileForNamingDump[] = "./check_dump.py";
554 static char aFileForGeometryDump[] = "./check_dump_geo.py";
555 static char aFileForWeakDump[] = "./check_dump_weak.py";
557 SessionPtr aSession = ModelAPI_Session::get();
558 // dump with the specified types
559 char* aFileName = theCheckType == CHECK_GEOMETRICAL ? aFileForGeometryDump :
560 (theCheckType == CHECK_WEAK ? aFileForWeakDump : aFileForNamingDump);
561 if (!dumpToPython(aSession, aFileName, theCheckType, anErrorByNaming))
564 // map from document name to feature name to feature data
565 std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> > aStore;
566 std::string anError = storeFeatures(
567 aSession->moduleDocument()->kind(), aSession->moduleDocument(), aStore, false);
568 if (!anError.empty()) {
569 Events_InfoMessage anErrorMsg(std::string("checkPythonDump"), anError);
575 if (theCheckType & CHECK_NAMING) {
576 // check dump with the selection by names
577 isOk = checkDump(aSession, aFileForNamingDump, aStore, anErrorByNaming);
579 if (theCheckType & CHECK_GEOMETRICAL) {
580 // check dump with the selection by geometry
581 isOk = isOk && checkDump(aSession, aFileForGeometryDump, aStore, anErrorByGeometry);
583 if (theCheckType & CHECK_WEAK) {
584 isOk = isOk && checkDump(aSession, aFileForWeakDump, aStore, anErrorByWeak);
590 //--------------------------------------------------------------------------------------