1 // Copyright (C) 2014-2020 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 <Locale_Convert.h>
33 //--------------------------------------------------------------------------------------
34 #include <ModelAPI_AttributeBoolean.h>
35 #include <ModelAPI_AttributeDocRef.h>
36 #include <ModelAPI_AttributeDouble.h>
37 #include <ModelAPI_AttributeDoubleArray.h>
38 #include <ModelAPI_AttributeIntArray.h>
39 #include <ModelAPI_AttributeInteger.h>
40 #include <ModelAPI_AttributeRefAttr.h>
41 #include <ModelAPI_AttributeRefAttrList.h>
42 #include <ModelAPI_AttributeReference.h>
43 #include <ModelAPI_AttributeRefList.h>
44 #include <ModelAPI_AttributeSelection.h>
45 #include <ModelAPI_AttributeSelectionList.h>
46 #include <ModelAPI_AttributeString.h>
47 #include <ModelAPI_AttributeStringArray.h>
48 #include <ModelAPI_AttributeDoubleArray.h>
49 #include <ModelAPI_Session.h>
50 #include <ModelAPI_Tools.h>
51 #include <ModelAPI_ResultPart.h>
52 #include <ModelAPI_Events.h>
53 //--------------------------------------------------------------------------------------
54 #include <Config_ModuleReader.h>
55 //--------------------------------------------------------------------------------------
56 #include "ModelHighAPI_Double.h"
57 #include "ModelHighAPI_Integer.h"
58 #include "ModelHighAPI_RefAttr.h"
59 #include "ModelHighAPI_Reference.h"
60 #include "ModelHighAPI_Selection.h"
62 #include <Events_InfoMessage.h>
64 // Have to be included before std headers
70 //--------------------------------------------------------------------------------------
71 void fillAttribute(const std::shared_ptr<GeomAPI_Pnt2d> & theValue,
72 const std::shared_ptr<GeomDataAPI_Point2D> & theAttribute)
74 theAttribute->setValue(theValue);
77 void fillAttribute(const std::shared_ptr<GeomDataAPI_Point2D> & theAttribute,
78 double theX, double theY)
80 theAttribute->setValue(theX, theY);
83 //--------------------------------------------------------------------------------------
84 void fillAttribute(const std::shared_ptr<GeomAPI_Dir> & theValue,
85 const std::shared_ptr<GeomDataAPI_Dir> & theAttribute)
87 theAttribute->setValue(theValue);
90 //--------------------------------------------------------------------------------------
91 void fillAttribute(const std::shared_ptr<GeomAPI_Pnt> & theValue,
92 const std::shared_ptr<GeomDataAPI_Point> & theAttribute)
94 theAttribute->setValue(theValue);
97 //--------------------------------------------------------------------------------------
98 void fillAttribute(bool theValue,
99 const std::shared_ptr<ModelAPI_AttributeBoolean> & theAttribute)
101 theAttribute->setValue(theValue);
104 //--------------------------------------------------------------------------------------
105 void fillAttribute(const ModelHighAPI_Double & theValue,
106 const std::shared_ptr<ModelAPI_AttributeDouble> & theAttribute)
108 theValue.fillAttribute(theAttribute);
110 void fillAttribute(double theValue,
111 const std::shared_ptr<ModelAPI_AttributeDouble> & theAttribute)
113 theAttribute->setValue(theValue);
116 //--------------------------------------------------------------------------------------
117 void fillAttribute(const ModelHighAPI_Integer & theValue,
118 const std::shared_ptr<ModelAPI_AttributeInteger> & theAttribute)
120 theValue.fillAttribute(theAttribute);
122 void fillAttribute(int theValue,
123 const std::shared_ptr<ModelAPI_AttributeInteger> & theAttribute)
125 theAttribute->setValue(theValue);
128 //--------------------------------------------------------------------------------------
129 void fillAttribute(const ModelHighAPI_RefAttr & theValue,
130 const std::shared_ptr<ModelAPI_AttributeRefAttr> & theAttribute)
132 theValue.fillAttribute(theAttribute);
135 //--------------------------------------------------------------------------------------
136 void fillAttribute(const std::list<ModelHighAPI_RefAttr> & theValue,
137 const std::shared_ptr<ModelAPI_AttributeRefAttrList> & theAttribute)
139 theAttribute->clear();
140 for (auto it = theValue.begin(); it != theValue.end(); ++it)
141 it->appendToList(theAttribute);
144 //--------------------------------------------------------------------------------------
145 void fillAttribute(const ModelHighAPI_Reference & theValue,
146 const std::shared_ptr<ModelAPI_AttributeReference> & theAttribute)
148 theValue.fillAttribute(theAttribute);
151 //--------------------------------------------------------------------------------------
152 void fillAttribute(const std::list<ModelHighAPI_Reference> & theValue,
153 const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
155 theAttribute->clear();
156 for (auto it = theValue.begin(); it != theValue.end(); ++it)
157 it->appendToList(theAttribute);
160 //--------------------------------------------------------------------------------------
161 void fillAttribute(const std::shared_ptr<ModelAPI_Object> & theValue,
162 const std::shared_ptr<ModelAPI_AttributeReference> & theAttribute)
164 theAttribute->setValue(theValue);
167 //--------------------------------------------------------------------------------------
168 void fillAttribute(const std::list<std::shared_ptr<ModelAPI_Object> > & theValue,
169 const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
171 int aSize = theAttribute->size();
172 // keep objects at the beginning of the list if they the same
173 auto it = theValue.begin();
174 for (int anIndex = 0; it != theValue.end() && anIndex < aSize; ++it, ++anIndex)
175 if (theAttribute->object(anIndex) != *it) {
176 // remove the tail of the list
177 while (++anIndex <= aSize)
178 theAttribute->removeLast();
181 // append the rest of elements
182 for (; it != theValue.end(); ++it)
183 theAttribute->append(*it);
187 void fillAttribute(const std::list<ModelHighAPI_Selection> & theValue,
188 const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
190 theAttribute->clear();
191 for (auto it = theValue.begin(); it != theValue.end(); ++it) {
192 if (it->resultSubShapePair().first)
193 theAttribute->append(it->resultSubShapePair().first); // use only context
197 //--------------------------------------------------------------------------------------
198 void fillAttribute(const ModelHighAPI_Selection & theValue,
199 const std::shared_ptr<ModelAPI_AttributeSelection> & theAttribute)
201 theValue.fillAttribute(theAttribute);
204 //--------------------------------------------------------------------------------------
205 void fillAttribute(const std::list<ModelHighAPI_Selection> & theValue,
206 const std::shared_ptr<ModelAPI_AttributeSelectionList> & theAttribute)
208 theAttribute->clear();
210 if(!theValue.empty() && theAttribute->selectionType().empty()) {
211 const ModelHighAPI_Selection& aSelection = theValue.front();
212 GeomAPI_Shape::ShapeType aSelectionType = getShapeType(aSelection);
213 theAttribute->setSelectionType(strByShapeType(aSelectionType));
216 for (auto it = theValue.begin(); it != theValue.end(); ++it)
217 it->appendToList(theAttribute);
220 //--------------------------------------------------------------------------------------
221 void fillAttribute(const std::string & theValue,
222 const std::shared_ptr<ModelAPI_AttributeString> & theAttribute)
224 theAttribute->setValue(theValue);
227 //--------------------------------------------------------------------------------------
228 void fillAttribute(const char * theValue,
229 const std::shared_ptr<ModelAPI_AttributeString> & theAttribute)
231 theAttribute->setValue(theValue);
234 //--------------------------------------------------------------------------------------
235 void fillAttribute(const std::list<std::string> & theValue,
236 const std::shared_ptr<ModelAPI_AttributeStringArray> & theAttribute)
238 theAttribute->setSize(int(theValue.size()));
241 for (auto it = theValue.begin(); it != theValue.end(); ++it, ++anIndex)
242 theAttribute->setValue(anIndex, *it);
245 //--------------------------------------------------------------------------------------
246 void fillAttribute(const std::list<ModelHighAPI_Integer> & theValue,
247 const std::shared_ptr<ModelAPI_AttributeIntArray> & theAttribute)
249 theAttribute->setSize(int(theValue.size()));
252 for (auto it = theValue.begin(); it != theValue.end(); ++it, ++anIndex)
253 theAttribute->setValue(anIndex, it->intValue()); // use only values, no text support in array
256 //--------------------------------------------------------------------------------------
257 void fillAttribute(const std::list<ModelHighAPI_Double> & theValue,
258 const std::shared_ptr<ModelAPI_AttributeDoubleArray> & theAttribute)
260 theAttribute->setSize(int(theValue.size()));
263 for (auto it = theValue.begin(); it != theValue.end(); ++it, ++anIndex)
264 theAttribute->setValue(anIndex, it->value()); // use only values, no text support in array
267 //--------------------------------------------------------------------------------------
268 void fillAttribute(const std::list<std::shared_ptr<GeomAPI_Pnt2d> > & theValue,
269 const std::shared_ptr<GeomDataAPI_Point2DArray> & theAttribute)
271 theAttribute->setSize(int(theValue.size()));
274 for (auto it = theValue.begin(); it != theValue.end(); ++it, ++anIndex)
275 theAttribute->setPnt(anIndex, *it);
278 //--------------------------------------------------------------------------------------
279 void fillAttribute(const ModelHighAPI_Double & theX,
280 const ModelHighAPI_Double & theY,
281 const ModelHighAPI_Double & theZ,
282 const std::shared_ptr<GeomDataAPI_Point> & theAttribute)
284 theX.fillAttribute(theAttribute, theX, theY, theZ);
287 //==================================================================================================
288 GeomAPI_Shape::ShapeType shapeTypeByStr(std::string theShapeTypeStr)
290 GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
292 std::transform(theShapeTypeStr.begin(), theShapeTypeStr.end(), theShapeTypeStr.begin(),
293 [](char c) { return static_cast<char>(::tolower(c)); });
295 if(theShapeTypeStr == "compound") {
296 aShapeType = GeomAPI_Shape::COMPOUND;
297 } else if(theShapeTypeStr == "compsolid") {
298 aShapeType = GeomAPI_Shape::COMPSOLID;
299 } else if(theShapeTypeStr == "solid") {
300 aShapeType = GeomAPI_Shape::SOLID;
301 } else if(theShapeTypeStr == "shell") {
302 aShapeType = GeomAPI_Shape::SHELL;
303 } else if(theShapeTypeStr == "face") {
304 aShapeType = GeomAPI_Shape::FACE;
305 } else if(theShapeTypeStr == "wire") {
306 aShapeType = GeomAPI_Shape::WIRE;
307 } else if(theShapeTypeStr == "edge") {
308 aShapeType = GeomAPI_Shape::EDGE;
309 } else if(theShapeTypeStr == "vertex") {
310 aShapeType = GeomAPI_Shape::VERTEX;
311 } else if(theShapeTypeStr == "shape") {
312 aShapeType = GeomAPI_Shape::SHAPE;
318 std::string strByShapeType(GeomAPI_Shape::ShapeType theShapeType)
320 std::string aShapeTypeStr;
321 switch (theShapeType) {
322 case GeomAPI_Shape::COMPOUND:
323 aShapeTypeStr = "COMPOUND";
325 case GeomAPI_Shape::COMPSOLID:
326 aShapeTypeStr = "COMPSOLID";
328 case GeomAPI_Shape::SOLID:
329 aShapeTypeStr = "SOLID";
331 case GeomAPI_Shape::SHELL:
332 aShapeTypeStr = "SHELL";
334 case GeomAPI_Shape::FACE:
335 aShapeTypeStr = "FACE";
337 case GeomAPI_Shape::WIRE:
338 aShapeTypeStr = "WIRE";
340 case GeomAPI_Shape::EDGE:
341 aShapeTypeStr = "EDGE";
343 case GeomAPI_Shape::VERTEX:
344 aShapeTypeStr = "VERTEX";
347 aShapeTypeStr = "SHAPE";
350 return aShapeTypeStr;
353 //==================================================================================================
354 GeomAPI_Shape::ShapeType getShapeType(const ModelHighAPI_Selection& theSelection)
356 GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
358 switch(theSelection.variantType()) {
359 case ModelHighAPI_Selection::VT_ResultSubShapePair: {
360 ResultSubShapePair aPair = theSelection.resultSubShapePair();
361 GeomShapePtr aShape = aPair.second;
362 if(!aShape.get() && aPair.first.get()) {
363 aShape = aPair.first->shape();
368 aShapeType = aShape->shapeType();
371 case ModelHighAPI_Selection::VT_TypeSubShapeNamePair: {
372 TypeSubShapeNamePair aPair = theSelection.typeSubShapeNamePair();
373 std::string aType = aPair.first;
374 aShapeType = shapeTypeByStr(aType);
377 case ModelHighAPI_Selection::VT_TypeInnerPointPair: {
378 TypeInnerPointPair aPair = theSelection.typeInnerPointPair();
379 std::string aType = aPair.first;
380 aType = aType.substr(0, aType.find_first_of('_'));
381 aShapeType = shapeTypeByStr(aType);
384 case ModelHighAPI_Selection::VT_WeakNamingPair: {
385 TypeWeakNamingPair aPair = theSelection.typeWeakNamingPair();
386 std::string aType = aPair.first;
387 aShapeType = shapeTypeByStr(aType);
391 break; // do nothing [to avoid compilation warning]
397 //--------------------------------------------------------------------------------------
398 ModelAPI_AttributeTables::ValueType valueTypeByStr(const std::string& theValueTypeStr)
400 std::string aType = theValueTypeStr;
401 std::transform(aType.begin(), aType.end(), aType.begin(),
402 [](char c) { return static_cast<char>(::tolower(c)); });
403 if (aType == "boolean")
404 return ModelAPI_AttributeTables::BOOLEAN;
405 else if (aType == "integer")
406 return ModelAPI_AttributeTables::INTEGER;
407 else if (aType == "string")
408 return ModelAPI_AttributeTables::STRING;
409 return ModelAPI_AttributeTables::DOUBLE; // default
412 //--------------------------------------------------------------------------------------
413 std::string strByValueType(const ModelAPI_AttributeTables::ValueType theType)
416 case ModelAPI_AttributeTables::BOOLEAN: return "BOOLEAN";
417 case ModelAPI_AttributeTables::INTEGER: return "INTEGER";
418 case ModelAPI_AttributeTables::DOUBLE: return "DOUBLE";
419 case ModelAPI_AttributeTables::STRING: return "STRING";
421 return ""; // bad case
424 /// stores the features information, recursively stores sub-documents features
425 std::string storeFeatures(const std::wstring& theDocName, DocumentPtr theDoc,
426 std::map<std::wstring, std::map<std::wstring, ModelHighAPI_FeatureStore> >& theStore,
427 const bool theCompare) // if false => store
429 std::map<std::wstring, std::map<std::wstring, ModelHighAPI_FeatureStore> >::iterator aDocFind;
431 aDocFind = theStore.find(theDocName);
432 if (aDocFind == theStore.end()) {
433 return "Document '" + Locale::Convert::toString(theDocName) + "' not found";
436 // store the model features information: iterate all features
437 size_t anObjectsCount = 0; // stores the number of compared features for this document to compare
438 std::set<std::wstring> aProcessed; // processed features names (that are in the current document)
440 // process all objects (features and folders)
441 std::list<ObjectPtr> allObjects = theDoc->allObjects();
442 std::list<ObjectPtr>::iterator allIter = allObjects.begin();
443 for(; allIter != allObjects.end(); allIter++) {
444 ObjectPtr anObject = *allIter;
445 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
446 //if (aFeature && aFeature->getKind() == "SketchConstraintCoincidenceInternal")
449 std::string aStr = aFeature->getKind().substr(0, 16);
450 if (aStr == "SketchConstraint")
451 continue; // no need to dump and check constraints
454 std::map<std::wstring, ModelHighAPI_FeatureStore>::iterator
455 anObjFind = aDocFind->second.find(anObject->data()->name());
456 if (anObjFind == aDocFind->second.end()) {
457 return "Document '" + Locale::Convert::toString(theDocName)
458 + "' feature '" + Locale::Convert::toString(anObject->data()->name()) + "' not found";
460 std::string anError = anObjFind->second.compare(anObject);
461 if (!anError.empty()) {
462 anError = "Document " + Locale::Convert::toString(theDocName) + " " + anError;
466 aProcessed.insert(anObject->data()->name());
468 theStore[theDocName][anObject->data()->name()] = ModelHighAPI_FeatureStore(anObject);
472 // iterate all results of this feature
473 std::list<ResultPtr> allResults;
474 ModelAPI_Tools::allResults(aFeature, allResults);
475 std::list<ResultPtr>::iterator aRes = allResults.begin();
476 for(; aRes != allResults.end(); aRes++) {
477 // recursively store features of sub-documents
478 if ((*aRes)->groupName() == ModelAPI_ResultPart::group()) {
479 DocumentPtr aDoc = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes)->partDoc();
481 std::string anError =
482 storeFeatures((*aRes)->data()->name(), aDoc, theStore, theCompare);
483 if (!anError.empty())
490 // checks the number of compared features
492 if (aDocFind->second.size() != anObjectsCount) {
493 // search for disappeared feature
494 std::wstring aLostName;
495 std::map<std::wstring, ModelHighAPI_FeatureStore>::iterator aLostIter;
496 for(aLostIter = aDocFind->second.begin(); aLostIter != aDocFind->second.end(); aLostIter++) {
497 if (aProcessed.find(aLostIter->first) == aProcessed.end()) {
498 aLostName = aLostIter->first;
501 return "For document '" + Locale::Convert::toString(theDocName) +
502 "' the number of features is decreased, there is no feature '" +
503 Locale::Convert::toString(aLostName) + "'";
509 //==================================================================================================
510 typedef std::map<std::wstring, std::map<std::wstring, ModelHighAPI_FeatureStore> > Storage;
512 static bool dumpToPython(SessionPtr theSession,
513 const std::string& theFilename,
514 const checkDumpType theSelectionType,
515 const std::string& theErrorMsgContext)
517 // 2431: set PartSet as a current document
518 theSession->setActiveDocument(theSession->moduleDocument(), true);
519 // dump all to the python file
520 theSession->startOperation("Check python dump");
521 FeaturePtr aDump = theSession->moduleDocument()->addFeature("Dump");
523 aDump->string("file_path")->setValue(theFilename);
524 aDump->string("file_format")->setValue("py");
525 aDump->boolean("topological_naming")->setValue((theSelectionType & CHECK_NAMING) != 0);
526 aDump->boolean("geometric_selection")->setValue((theSelectionType & CHECK_GEOMETRICAL) != 0);
527 aDump->boolean("weak_naming")->setValue((theSelectionType & CHECK_WEAK) != 0);
529 bool isProblem = !aDump.get() || !aDump->error().empty(); // after "finish" dump will be removed
530 if (isProblem && aDump.get()) {
531 std::cout << "Dump feature error " << aDump->error() << std::endl;
532 Events_InfoMessage anErrorMsg(theErrorMsgContext, aDump->error());
535 theSession->finishOperation();
539 static bool checkDump(SessionPtr theSession,
540 const char* theFilename,
542 const std::string& theErrorMsgContext)
545 // close all before importation of the script
546 theSession->closeAll();
548 // execute the dumped
549 PyGILState_STATE gstate = PyGILState_Ensure(); /* acquire python thread */
550 static char aReadMode[] = "r";
551 FILE* PyFileObject = _Py_fopen(theFilename, aReadMode);
552 PyRun_SimpleFileEx(PyFileObject, theFilename, 1);
553 PyGILState_Release(gstate); /* release python thread */
555 // compare with the stored data
556 std::string anError =
557 storeFeatures(Locale::Convert::toWString(theSession->moduleDocument()->kind()),
558 theSession->moduleDocument(), theStorage, true);
559 if (!anError.empty()) {
560 std::cout << anError << std::endl;
561 Events_InfoMessage anErrorMsg(theErrorMsgContext, anError);
571 const std::string&, const std::string&, const std::string&,
573 const std::string& theFilenameNaming,
574 const std::string& theFilenameGeo,
575 const std::string& theFilenameWeak,
577 const checkDumpType theCheckType)
579 static const std::string anErrorByNaming("checkPythonDump by naming");
580 static const std::string anErrorByGeometry("checkPythonDump by geometry");
581 static const std::string anErrorByWeak("checkPythonDump by weak naming");
584 std::string aFileForNamingDump("./check_dump.py");
585 std::string aFileForGeometryDump("./check_dump_geo.py");
586 std::string aFileForWeakDump("./check_dump_weak.py");
588 std::string aFileForNamingDump = theFilenameNaming;
589 std::string aFileForGeometryDump = theFilenameGeo;
590 std::string aFileForWeakDump = theFilenameWeak;
593 SessionPtr aSession = ModelAPI_Session::get();
594 // dump with the specified types
595 std::string aFileName = theCheckType == CHECK_GEOMETRICAL ? aFileForGeometryDump :
596 (theCheckType == CHECK_WEAK ? aFileForWeakDump : aFileForNamingDump);
597 if (!dumpToPython(aSession, aFileName, theCheckType, anErrorByNaming))
600 // map from document name to feature name to feature data
601 std::map<std::wstring, std::map<std::wstring, ModelHighAPI_FeatureStore> > aStore;
602 std::string anError =
603 storeFeatures(Locale::Convert::toWString(aSession->moduleDocument()->kind()),
604 aSession->moduleDocument(), aStore, false);
605 if (!anError.empty()) {
606 Events_InfoMessage anErrorMsg(std::string("checkPythonDump"), anError);
612 if (theCheckType & CHECK_NAMING) {
613 // check dump with the selection by names
614 isOk = checkDump(aSession, aFileForNamingDump.c_str(), aStore, anErrorByNaming);
616 if (theCheckType & CHECK_GEOMETRICAL) {
617 // check dump with the selection by geometry
618 isOk = isOk && checkDump(aSession, aFileForGeometryDump.c_str(), aStore, anErrorByGeometry);
620 if (theCheckType & CHECK_WEAK) {
621 isOk = isOk && checkDump(aSession, aFileForWeakDump.c_str(), aStore, anErrorByWeak);
627 //--------------------------------------------------------------------------------------