1 // Name : ModelHighAPI_Tools.cpp
5 // 07/06/16 - Sergey POKHODENKO - Creation of the file
7 //--------------------------------------------------------------------------------------
8 #include "ModelHighAPI_Tools.h"
9 #include <ModelHighAPI_FeatureStore.h>
10 //--------------------------------------------------------------------------------------
11 #include <GeomAPI_Dir.h>
12 #include <GeomAPI_Pnt.h>
13 #include <GeomAPI_Pnt2d.h>
14 //--------------------------------------------------------------------------------------
15 #include <GeomDataAPI_Dir.h>
16 #include <GeomDataAPI_Point.h>
17 #include <GeomDataAPI_Point2D.h>
18 //--------------------------------------------------------------------------------------
19 #include <ModelAPI_AttributeBoolean.h>
20 #include <ModelAPI_AttributeDocRef.h>
21 #include <ModelAPI_AttributeDouble.h>
22 #include <ModelAPI_AttributeIntArray.h>
23 #include <ModelAPI_AttributeInteger.h>
24 #include <ModelAPI_AttributeRefAttr.h>
25 #include <ModelAPI_AttributeRefAttrList.h>
26 #include <ModelAPI_AttributeReference.h>
27 #include <ModelAPI_AttributeRefList.h>
28 #include <ModelAPI_AttributeSelection.h>
29 #include <ModelAPI_AttributeSelectionList.h>
30 #include <ModelAPI_AttributeString.h>
31 #include <ModelAPI_AttributeDoubleArray.h>
32 #include <ModelAPI_Session.h>
33 #include <ModelAPI_Tools.h>
34 #include <ModelAPI_ResultPart.h>
35 //--------------------------------------------------------------------------------------
36 #include <Config_ModuleReader.h>
37 //--------------------------------------------------------------------------------------
38 #include "ModelHighAPI_Double.h"
39 #include "ModelHighAPI_Integer.h"
40 #include "ModelHighAPI_RefAttr.h"
41 #include "ModelHighAPI_Reference.h"
42 #include "ModelHighAPI_Selection.h"
44 #include <Events_InfoMessage.h>
46 // Have to be included before std headers
52 //--------------------------------------------------------------------------------------
53 void fillAttribute(const std::shared_ptr<GeomAPI_Pnt2d> & theValue,
54 const std::shared_ptr<GeomDataAPI_Point2D> & theAttribute)
56 theAttribute->setValue(theValue);
59 void fillAttribute(const std::shared_ptr<GeomDataAPI_Point2D> & theAttribute,
60 double theX, double theY)
62 theAttribute->setValue(theX, theY);
65 //--------------------------------------------------------------------------------------
66 void fillAttribute(const std::shared_ptr<GeomAPI_Dir> & theValue,
67 const std::shared_ptr<GeomDataAPI_Dir> & theAttribute)
69 theAttribute->setValue(theValue);
72 //--------------------------------------------------------------------------------------
73 void fillAttribute(const std::shared_ptr<GeomAPI_Pnt> & theValue,
74 const std::shared_ptr<GeomDataAPI_Point> & theAttribute)
76 theAttribute->setValue(theValue);
79 //--------------------------------------------------------------------------------------
80 void fillAttribute(bool theValue,
81 const std::shared_ptr<ModelAPI_AttributeBoolean> & theAttribute)
83 theAttribute->setValue(theValue);
86 //--------------------------------------------------------------------------------------
87 void fillAttribute(const ModelHighAPI_Double & theValue,
88 const std::shared_ptr<ModelAPI_AttributeDouble> & theAttribute)
90 theValue.fillAttribute(theAttribute);
92 void fillAttribute(double theValue,
93 const std::shared_ptr<ModelAPI_AttributeDouble> & theAttribute)
95 theAttribute->setValue(theValue);
98 //--------------------------------------------------------------------------------------
99 void fillAttribute(const ModelHighAPI_Integer & theValue,
100 const std::shared_ptr<ModelAPI_AttributeInteger> & theAttribute)
102 theValue.fillAttribute(theAttribute);
104 void fillAttribute(int theValue,
105 const std::shared_ptr<ModelAPI_AttributeInteger> & theAttribute)
107 theAttribute->setValue(theValue);
110 //--------------------------------------------------------------------------------------
111 void fillAttribute(const ModelHighAPI_RefAttr & theValue,
112 const std::shared_ptr<ModelAPI_AttributeRefAttr> & theAttribute)
114 theValue.fillAttribute(theAttribute);
117 //--------------------------------------------------------------------------------------
118 void fillAttribute(const std::list<ModelHighAPI_RefAttr> & theValue,
119 const std::shared_ptr<ModelAPI_AttributeRefAttrList> & theAttribute)
121 theAttribute->clear();
122 for (auto it = theValue.begin(); it != theValue.end(); ++it)
123 it->appendToList(theAttribute);
126 //--------------------------------------------------------------------------------------
127 void fillAttribute(const ModelHighAPI_Reference & theValue,
128 const std::shared_ptr<ModelAPI_AttributeReference> & theAttribute)
130 theValue.fillAttribute(theAttribute);
133 //--------------------------------------------------------------------------------------
134 void fillAttribute(const std::list<ModelHighAPI_Reference> & theValue,
135 const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
137 theAttribute->clear();
138 for (auto it = theValue.begin(); it != theValue.end(); ++it)
139 it->appendToList(theAttribute);
142 //--------------------------------------------------------------------------------------
143 void fillAttribute(const std::shared_ptr<ModelAPI_Object> & theValue,
144 const std::shared_ptr<ModelAPI_AttributeReference> & theAttribute)
146 theAttribute->setValue(theValue);
149 //--------------------------------------------------------------------------------------
150 void fillAttribute(const std::list<std::shared_ptr<ModelAPI_Object> > & theValue,
151 const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
153 theAttribute->clear();
154 for (auto it = theValue.begin(); it != theValue.end(); ++it)
155 theAttribute->append(*it);
159 void fillAttribute(const std::list<ModelHighAPI_Selection> & theValue,
160 const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
162 theAttribute->clear();
163 for (auto it = theValue.begin(); it != theValue.end(); ++it)
164 theAttribute->append(it->resultSubShapePair().first); // use only context
167 //--------------------------------------------------------------------------------------
168 void fillAttribute(const ModelHighAPI_Selection & theValue,
169 const std::shared_ptr<ModelAPI_AttributeSelection> & theAttribute)
171 theValue.fillAttribute(theAttribute);
174 //--------------------------------------------------------------------------------------
175 void fillAttribute(const std::list<ModelHighAPI_Selection> & theValue,
176 const std::shared_ptr<ModelAPI_AttributeSelectionList> & theAttribute)
178 theAttribute->clear();
180 if(!theValue.empty()) {
181 std::string aSelectionType;
182 const ModelHighAPI_Selection& aSelection = theValue.front();
183 theAttribute->setSelectionType(aSelection.shapeType());
186 for (auto it = theValue.begin(); it != theValue.end(); ++it)
187 it->appendToList(theAttribute);
190 //--------------------------------------------------------------------------------------
191 void fillAttribute(const std::string & theValue,
192 const std::shared_ptr<ModelAPI_AttributeString> & theAttribute)
194 theAttribute->setValue(theValue);
196 void fillAttribute(const char * theValue,
197 const std::shared_ptr<ModelAPI_AttributeString> & theAttribute)
199 theAttribute->setValue(theValue);
202 //==================================================================================================
203 GeomAPI_Shape::ShapeType shapeTypeByStr(std::string theShapeTypeStr)
205 GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
207 std::transform(theShapeTypeStr.begin(), theShapeTypeStr.end(), theShapeTypeStr.begin(), ::tolower);
209 if(theShapeTypeStr == "compound") {
210 aShapeType = GeomAPI_Shape::COMPOUND;
211 } else if(theShapeTypeStr == "compsolid") {
212 aShapeType = GeomAPI_Shape::COMPSOLID;
213 } else if(theShapeTypeStr == "solid") {
214 aShapeType = GeomAPI_Shape::SOLID;
215 } else if(theShapeTypeStr == "shell") {
216 aShapeType = GeomAPI_Shape::SHELL;
217 } else if(theShapeTypeStr == "face") {
218 aShapeType = GeomAPI_Shape::FACE;
219 } else if(theShapeTypeStr == "wire") {
220 aShapeType = GeomAPI_Shape::WIRE;
221 } else if(theShapeTypeStr == "edge") {
222 aShapeType = GeomAPI_Shape::EDGE;
223 } else if(theShapeTypeStr == "vertex") {
224 aShapeType = GeomAPI_Shape::VERTEX;
225 } else if(theShapeTypeStr == "shape") {
226 aShapeType = GeomAPI_Shape::SHAPE;
232 //==================================================================================================
233 GeomAPI_Shape::ShapeType getShapeType(const ModelHighAPI_Selection& theSelection)
235 GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
237 switch(theSelection.variantType()) {
238 case ModelHighAPI_Selection::VT_ResultSubShapePair: {
239 ResultSubShapePair aPair = theSelection.resultSubShapePair();
240 GeomShapePtr aShape = aPair.second;
242 aShape = aPair.first->shape();
247 aShapeType = aShape->shapeType();
250 case ModelHighAPI_Selection::VT_TypeSubShapeNamePair: {
251 TypeSubShapeNamePair aPair = theSelection.typeSubShapeNamePair();
252 std::string aType = aPair.first;
253 aShapeType = shapeTypeByStr(aType);
261 /// stores the features information, recoursively stores sub-documetns features
262 std::string storeFeatures(const std::string& theDocName, DocumentPtr theDoc,
263 std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> >& theStore,
264 const bool theCompare) // if false => store
266 std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> >::iterator aDocFind;
268 aDocFind = theStore.find(theDocName);
269 if (aDocFind == theStore.end()) {
270 return "Document '" + theDocName + "' not found";
273 // store the model features information: iterate all features
274 int aFeaturesCount = 0; // stores the number of compared features for this document to compate
275 std::set<std::string> aProcessed; // processed features names (that are in the current document)
276 std::list<FeaturePtr> allFeatures = theDoc->allFeatures();
277 std::list<FeaturePtr>::iterator allIter = allFeatures.begin();
278 for(; allIter != allFeatures.end(); allIter++) {
279 FeaturePtr aFeat = *allIter;
281 std::map<std::string, ModelHighAPI_FeatureStore>::iterator
282 aFeatFind = aDocFind->second.find(aFeat->name());
283 if (aFeatFind == aDocFind->second.end()) {
284 return "Document '" + theDocName + "' feature '" + aFeat->name() + "' not found";
286 std::string anError = aFeatFind->second.compare(aFeat);
287 if (!anError.empty()) {
291 aProcessed.insert(aFeat->name());
293 theStore[theDocName][aFeat->name()] = ModelHighAPI_FeatureStore(aFeat);
295 // iterate all results of this feature
296 std::list<ResultPtr> allResults;
297 ModelAPI_Tools::allResults(aFeat, allResults);
298 std::list<ResultPtr>::iterator aRes = allResults.begin();
299 for(; aRes != allResults.end(); aRes++) {
300 if ((*aRes)->groupName() == ModelAPI_ResultPart::group()) { // recoursively store features of sub-documents
301 DocumentPtr aDoc = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes)->partDoc();
303 std::string anError = storeFeatures((*aRes)->data()->name(), aDoc, theStore, theCompare);
304 if (!anError.empty())
310 // checks the number of compared features
312 if (aDocFind->second.size() != aFeaturesCount) {
313 // search for disappeared feature
314 std::string aLostName;
315 std::map<std::string, ModelHighAPI_FeatureStore>::iterator aLostIter;
316 for(aLostIter = aDocFind->second.begin(); aLostIter != aDocFind->second.end(); aLostIter++) {
317 if (aProcessed.find(aLostIter->first) == aProcessed.end()) {
318 aLostName = aLostIter->first;
321 return "For document '" + theDocName +
322 "' the number of features is decreased, there is no feature '" + aLostName + "'";
328 //==================================================================================================
329 bool checkPythonDump()
331 SessionPtr aSession = ModelAPI_Session::get();
332 // dump all to the python file
333 aSession->startOperation("Check python dump");
334 FeaturePtr aDump = aSession->moduleDocument()->addFeature("Dump");
336 aDump->string("file_path")->setValue("check_dump.py"); // to the current folder
337 aDump->string("file_format")->setValue("py"); // to the current folder
340 bool isProblem = !aDump.get() || !aDump->error().empty(); // after "finish" dump will be removed
341 aSession->finishOperation();
343 return false; // something is wrong during dump
345 // map from document name to feature name to feature data
346 std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> > aStore;
347 std::string anError = storeFeatures(
348 aSession->moduleDocument()->kind(), aSession->moduleDocument(), aStore, false);
349 if (!anError.empty()) {
350 Events_InfoMessage anError("checkPythonDump", anError);
354 // close all before importation of the script
355 aSession->closeAll();
356 // execute the dumped
357 PyGILState_STATE gstate = PyGILState_Ensure(); /* acquire python thread */
358 PyObject* PyFileObject = PyFile_FromString("./check_dump.py", "r");
359 PyRun_SimpleFileEx(PyFile_AsFile(PyFileObject), "./check_dump.py", 1);
360 PyGILState_Release(gstate); /* release python thread */
362 // compare with the stored data
363 anError = storeFeatures(
364 aSession->moduleDocument()->kind(), aSession->moduleDocument(), aStore, true);
365 if (!anError.empty()) {
366 std::cout<<anError<<std::endl;
367 Events_InfoMessage anError("checkPythonDump", anError);
375 //--------------------------------------------------------------------------------------