1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2 // Name : ModelHighAPI_Tools.cpp
6 // 07/06/16 - Sergey POKHODENKO - Creation of the file
8 //--------------------------------------------------------------------------------------
9 #include "ModelHighAPI_Tools.h"
10 #include <ModelHighAPI_FeatureStore.h>
11 //--------------------------------------------------------------------------------------
12 #include <GeomAPI_Dir.h>
13 #include <GeomAPI_Pnt.h>
14 #include <GeomAPI_Pnt2d.h>
15 //--------------------------------------------------------------------------------------
16 #include <GeomDataAPI_Dir.h>
17 #include <GeomDataAPI_Point.h>
18 #include <GeomDataAPI_Point2D.h>
19 //--------------------------------------------------------------------------------------
20 #include <ModelAPI_AttributeBoolean.h>
21 #include <ModelAPI_AttributeDocRef.h>
22 #include <ModelAPI_AttributeDouble.h>
23 #include <ModelAPI_AttributeIntArray.h>
24 #include <ModelAPI_AttributeInteger.h>
25 #include <ModelAPI_AttributeRefAttr.h>
26 #include <ModelAPI_AttributeRefAttrList.h>
27 #include <ModelAPI_AttributeReference.h>
28 #include <ModelAPI_AttributeRefList.h>
29 #include <ModelAPI_AttributeSelection.h>
30 #include <ModelAPI_AttributeSelectionList.h>
31 #include <ModelAPI_AttributeString.h>
32 #include <ModelAPI_AttributeDoubleArray.h>
33 #include <ModelAPI_Session.h>
34 #include <ModelAPI_Tools.h>
35 #include <ModelAPI_ResultPart.h>
36 //--------------------------------------------------------------------------------------
37 #include <Config_ModuleReader.h>
38 //--------------------------------------------------------------------------------------
39 #include "ModelHighAPI_Double.h"
40 #include "ModelHighAPI_Integer.h"
41 #include "ModelHighAPI_RefAttr.h"
42 #include "ModelHighAPI_Reference.h"
43 #include "ModelHighAPI_Selection.h"
45 #include <Events_InfoMessage.h>
47 // Have to be included before std headers
53 //--------------------------------------------------------------------------------------
54 void fillAttribute(const std::shared_ptr<GeomAPI_Pnt2d> & theValue,
55 const std::shared_ptr<GeomDataAPI_Point2D> & theAttribute)
57 theAttribute->setValue(theValue);
60 void fillAttribute(const std::shared_ptr<GeomDataAPI_Point2D> & theAttribute,
61 double theX, double theY)
63 theAttribute->setValue(theX, theY);
66 //--------------------------------------------------------------------------------------
67 void fillAttribute(const std::shared_ptr<GeomAPI_Dir> & theValue,
68 const std::shared_ptr<GeomDataAPI_Dir> & theAttribute)
70 theAttribute->setValue(theValue);
73 //--------------------------------------------------------------------------------------
74 void fillAttribute(const std::shared_ptr<GeomAPI_Pnt> & theValue,
75 const std::shared_ptr<GeomDataAPI_Point> & theAttribute)
77 theAttribute->setValue(theValue);
80 //--------------------------------------------------------------------------------------
81 void fillAttribute(bool theValue,
82 const std::shared_ptr<ModelAPI_AttributeBoolean> & theAttribute)
84 theAttribute->setValue(theValue);
87 //--------------------------------------------------------------------------------------
88 void fillAttribute(const ModelHighAPI_Double & theValue,
89 const std::shared_ptr<ModelAPI_AttributeDouble> & theAttribute)
91 theValue.fillAttribute(theAttribute);
93 void fillAttribute(double theValue,
94 const std::shared_ptr<ModelAPI_AttributeDouble> & theAttribute)
96 theAttribute->setValue(theValue);
99 //--------------------------------------------------------------------------------------
100 void fillAttribute(const ModelHighAPI_Integer & theValue,
101 const std::shared_ptr<ModelAPI_AttributeInteger> & theAttribute)
103 theValue.fillAttribute(theAttribute);
105 void fillAttribute(int theValue,
106 const std::shared_ptr<ModelAPI_AttributeInteger> & theAttribute)
108 theAttribute->setValue(theValue);
111 //--------------------------------------------------------------------------------------
112 void fillAttribute(const ModelHighAPI_RefAttr & theValue,
113 const std::shared_ptr<ModelAPI_AttributeRefAttr> & theAttribute)
115 theValue.fillAttribute(theAttribute);
118 //--------------------------------------------------------------------------------------
119 void fillAttribute(const std::list<ModelHighAPI_RefAttr> & theValue,
120 const std::shared_ptr<ModelAPI_AttributeRefAttrList> & theAttribute)
122 theAttribute->clear();
123 for (auto it = theValue.begin(); it != theValue.end(); ++it)
124 it->appendToList(theAttribute);
127 //--------------------------------------------------------------------------------------
128 void fillAttribute(const ModelHighAPI_Reference & theValue,
129 const std::shared_ptr<ModelAPI_AttributeReference> & theAttribute)
131 theValue.fillAttribute(theAttribute);
134 //--------------------------------------------------------------------------------------
135 void fillAttribute(const std::list<ModelHighAPI_Reference> & theValue,
136 const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
138 theAttribute->clear();
139 for (auto it = theValue.begin(); it != theValue.end(); ++it)
140 it->appendToList(theAttribute);
143 //--------------------------------------------------------------------------------------
144 void fillAttribute(const std::shared_ptr<ModelAPI_Object> & theValue,
145 const std::shared_ptr<ModelAPI_AttributeReference> & theAttribute)
147 theAttribute->setValue(theValue);
150 //--------------------------------------------------------------------------------------
151 void fillAttribute(const std::list<std::shared_ptr<ModelAPI_Object> > & theValue,
152 const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
154 theAttribute->clear();
155 for (auto it = theValue.begin(); it != theValue.end(); ++it)
156 theAttribute->append(*it);
160 void fillAttribute(const std::list<ModelHighAPI_Selection> & theValue,
161 const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
163 theAttribute->clear();
164 for (auto it = theValue.begin(); it != theValue.end(); ++it)
165 theAttribute->append(it->resultSubShapePair().first); // use only context
168 //--------------------------------------------------------------------------------------
169 void fillAttribute(const ModelHighAPI_Selection & theValue,
170 const std::shared_ptr<ModelAPI_AttributeSelection> & theAttribute)
172 theValue.fillAttribute(theAttribute);
175 //--------------------------------------------------------------------------------------
176 void fillAttribute(const std::list<ModelHighAPI_Selection> & theValue,
177 const std::shared_ptr<ModelAPI_AttributeSelectionList> & theAttribute)
179 theAttribute->clear();
181 if(!theValue.empty()) {
182 std::string aSelectionType;
183 const ModelHighAPI_Selection& aSelection = theValue.front();
184 theAttribute->setSelectionType(aSelection.shapeType());
187 for (auto it = theValue.begin(); it != theValue.end(); ++it)
188 it->appendToList(theAttribute);
191 //--------------------------------------------------------------------------------------
192 void fillAttribute(const std::string & theValue,
193 const std::shared_ptr<ModelAPI_AttributeString> & theAttribute)
195 theAttribute->setValue(theValue);
197 void fillAttribute(const char * theValue,
198 const std::shared_ptr<ModelAPI_AttributeString> & theAttribute)
200 theAttribute->setValue(theValue);
203 //==================================================================================================
204 GeomAPI_Shape::ShapeType shapeTypeByStr(std::string theShapeTypeStr)
206 GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
208 std::transform(theShapeTypeStr.begin(), theShapeTypeStr.end(),
209 theShapeTypeStr.begin(), ::tolower);
211 if(theShapeTypeStr == "compound") {
212 aShapeType = GeomAPI_Shape::COMPOUND;
213 } else if(theShapeTypeStr == "compsolid") {
214 aShapeType = GeomAPI_Shape::COMPSOLID;
215 } else if(theShapeTypeStr == "solid") {
216 aShapeType = GeomAPI_Shape::SOLID;
217 } else if(theShapeTypeStr == "shell") {
218 aShapeType = GeomAPI_Shape::SHELL;
219 } else if(theShapeTypeStr == "face") {
220 aShapeType = GeomAPI_Shape::FACE;
221 } else if(theShapeTypeStr == "wire") {
222 aShapeType = GeomAPI_Shape::WIRE;
223 } else if(theShapeTypeStr == "edge") {
224 aShapeType = GeomAPI_Shape::EDGE;
225 } else if(theShapeTypeStr == "vertex") {
226 aShapeType = GeomAPI_Shape::VERTEX;
227 } else if(theShapeTypeStr == "shape") {
228 aShapeType = GeomAPI_Shape::SHAPE;
234 //==================================================================================================
235 GeomAPI_Shape::ShapeType getShapeType(const ModelHighAPI_Selection& theSelection)
237 GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
239 switch(theSelection.variantType()) {
240 case ModelHighAPI_Selection::VT_ResultSubShapePair: {
241 ResultSubShapePair aPair = theSelection.resultSubShapePair();
242 GeomShapePtr aShape = aPair.second;
244 aShape = aPair.first->shape();
249 aShapeType = aShape->shapeType();
252 case ModelHighAPI_Selection::VT_TypeSubShapeNamePair: {
253 TypeSubShapeNamePair aPair = theSelection.typeSubShapeNamePair();
254 std::string aType = aPair.first;
255 aShapeType = shapeTypeByStr(aType);
263 /// stores the features information, recoursively stores sub-documetns features
264 std::string storeFeatures(const std::string& theDocName, DocumentPtr theDoc,
265 std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> >& theStore,
266 const bool theCompare) // if false => store
268 std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> >::iterator aDocFind;
270 aDocFind = theStore.find(theDocName);
271 if (aDocFind == theStore.end()) {
272 return "Document '" + theDocName + "' not found";
275 // store the model features information: iterate all features
276 int aFeaturesCount = 0; // stores the number of compared features for this document to compate
277 std::set<std::string> aProcessed; // processed features names (that are in the current document)
278 std::list<FeaturePtr> allFeatures = theDoc->allFeatures();
279 std::list<FeaturePtr>::iterator allIter = allFeatures.begin();
280 for(; allIter != allFeatures.end(); allIter++) {
281 FeaturePtr aFeat = *allIter;
283 std::map<std::string, ModelHighAPI_FeatureStore>::iterator
284 aFeatFind = aDocFind->second.find(aFeat->name());
285 if (aFeatFind == aDocFind->second.end()) {
286 return "Document '" + theDocName + "' feature '" + aFeat->name() + "' not found";
288 std::string anError = aFeatFind->second.compare(aFeat);
289 if (!anError.empty()) {
293 aProcessed.insert(aFeat->name());
295 theStore[theDocName][aFeat->name()] = ModelHighAPI_FeatureStore(aFeat);
297 // iterate all results of this feature
298 std::list<ResultPtr> allResults;
299 ModelAPI_Tools::allResults(aFeat, allResults);
300 std::list<ResultPtr>::iterator aRes = allResults.begin();
301 for(; aRes != allResults.end(); aRes++) {
302 // recoursively store features of sub-documents
303 if ((*aRes)->groupName() == ModelAPI_ResultPart::group()) {
304 DocumentPtr aDoc = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes)->partDoc();
306 std::string anError = storeFeatures((*aRes)->data()->name(), aDoc, theStore, theCompare);
307 if (!anError.empty())
313 // checks the number of compared features
315 if (aDocFind->second.size() != aFeaturesCount) {
316 // search for disappeared feature
317 std::string aLostName;
318 std::map<std::string, ModelHighAPI_FeatureStore>::iterator aLostIter;
319 for(aLostIter = aDocFind->second.begin(); aLostIter != aDocFind->second.end(); aLostIter++) {
320 if (aProcessed.find(aLostIter->first) == aProcessed.end()) {
321 aLostName = aLostIter->first;
324 return "For document '" + theDocName +
325 "' the number of features is decreased, there is no feature '" + aLostName + "'";
331 //==================================================================================================
332 bool checkPythonDump()
334 SessionPtr aSession = ModelAPI_Session::get();
335 // dump all to the python file
336 aSession->startOperation("Check python dump");
337 FeaturePtr aDump = aSession->moduleDocument()->addFeature("Dump");
339 aDump->string("file_path")->setValue("check_dump.py"); // to the current folder
340 aDump->string("file_format")->setValue("py"); // to the current folder
343 bool isProblem = !aDump.get() || !aDump->error().empty(); // after "finish" dump will be removed
344 aSession->finishOperation();
346 return false; // something is wrong during dump
348 // map from document name to feature name to feature data
349 std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> > aStore;
350 std::string anError = storeFeatures(
351 aSession->moduleDocument()->kind(), aSession->moduleDocument(), aStore, false);
352 if (!anError.empty()) {
353 Events_InfoMessage anErrorMsg(std::string("checkPythonDump"), anError);
357 // close all before importation of the script
358 aSession->closeAll();
359 // execute the dumped
360 PyGILState_STATE gstate = PyGILState_Ensure(); /* acquire python thread */
361 PyObject* PyFileObject = PyFile_FromString("./check_dump.py", "r");
362 PyRun_SimpleFileEx(PyFile_AsFile(PyFileObject), "./check_dump.py", 1);
363 PyGILState_Release(gstate); /* release python thread */
365 // compare with the stored data
366 anError = storeFeatures(
367 aSession->moduleDocument()->kind(), aSession->moduleDocument(), aStore, true);
368 if (!anError.empty()) {
369 std::cout<<anError<<std::endl;
370 Events_InfoMessage anErrorMsg(std::string("checkPythonDump"), anError);
378 //--------------------------------------------------------------------------------------