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>
49 //--------------------------------------------------------------------------------------
50 void fillAttribute(const std::shared_ptr<GeomAPI_Pnt2d> & theValue,
51 const std::shared_ptr<GeomDataAPI_Point2D> & theAttribute)
53 theAttribute->setValue(theValue);
56 void fillAttribute(const std::shared_ptr<GeomDataAPI_Point2D> & theAttribute,
57 double theX, double theY)
59 theAttribute->setValue(theX, theY);
62 //--------------------------------------------------------------------------------------
63 void fillAttribute(const std::shared_ptr<GeomAPI_Dir> & theValue,
64 const std::shared_ptr<GeomDataAPI_Dir> & theAttribute)
66 theAttribute->setValue(theValue);
69 //--------------------------------------------------------------------------------------
70 void fillAttribute(const std::shared_ptr<GeomAPI_Pnt> & theValue,
71 const std::shared_ptr<GeomDataAPI_Point> & theAttribute)
73 theAttribute->setValue(theValue);
76 //--------------------------------------------------------------------------------------
77 void fillAttribute(bool theValue,
78 const std::shared_ptr<ModelAPI_AttributeBoolean> & theAttribute)
80 theAttribute->setValue(theValue);
83 //--------------------------------------------------------------------------------------
84 void fillAttribute(const ModelHighAPI_Double & theValue,
85 const std::shared_ptr<ModelAPI_AttributeDouble> & theAttribute)
87 theValue.fillAttribute(theAttribute);
89 void fillAttribute(double theValue,
90 const std::shared_ptr<ModelAPI_AttributeDouble> & theAttribute)
92 theAttribute->setValue(theValue);
95 //--------------------------------------------------------------------------------------
96 void fillAttribute(const ModelHighAPI_Integer & theValue,
97 const std::shared_ptr<ModelAPI_AttributeInteger> & theAttribute)
99 theValue.fillAttribute(theAttribute);
101 void fillAttribute(int theValue,
102 const std::shared_ptr<ModelAPI_AttributeInteger> & theAttribute)
104 theAttribute->setValue(theValue);
107 //--------------------------------------------------------------------------------------
108 void fillAttribute(const ModelHighAPI_RefAttr & theValue,
109 const std::shared_ptr<ModelAPI_AttributeRefAttr> & theAttribute)
111 theValue.fillAttribute(theAttribute);
114 //--------------------------------------------------------------------------------------
115 void fillAttribute(const std::list<ModelHighAPI_RefAttr> & theValue,
116 const std::shared_ptr<ModelAPI_AttributeRefAttrList> & theAttribute)
118 theAttribute->clear();
119 for (auto it = theValue.begin(); it != theValue.end(); ++it)
120 it->appendToList(theAttribute);
123 //--------------------------------------------------------------------------------------
124 void fillAttribute(const ModelHighAPI_Reference & theValue,
125 const std::shared_ptr<ModelAPI_AttributeReference> & theAttribute)
127 theValue.fillAttribute(theAttribute);
130 //--------------------------------------------------------------------------------------
131 void fillAttribute(const std::list<ModelHighAPI_Reference> & theValue,
132 const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
134 theAttribute->clear();
135 for (auto it = theValue.begin(); it != theValue.end(); ++it)
136 it->appendToList(theAttribute);
139 //--------------------------------------------------------------------------------------
140 void fillAttribute(const std::shared_ptr<ModelAPI_Object> & theValue,
141 const std::shared_ptr<ModelAPI_AttributeReference> & theAttribute)
143 theAttribute->setValue(theValue);
146 //--------------------------------------------------------------------------------------
147 void fillAttribute(const std::list<std::shared_ptr<ModelAPI_Object> > & theValue,
148 const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
150 theAttribute->clear();
151 for (auto it = theValue.begin(); it != theValue.end(); ++it)
152 theAttribute->append(*it);
156 void fillAttribute(const std::list<ModelHighAPI_Selection> & theValue,
157 const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
159 theAttribute->clear();
160 for (auto it = theValue.begin(); it != theValue.end(); ++it)
161 theAttribute->append(it->resultSubShapePair().first); // use only context
164 //--------------------------------------------------------------------------------------
165 void fillAttribute(const ModelHighAPI_Selection & theValue,
166 const std::shared_ptr<ModelAPI_AttributeSelection> & theAttribute)
168 theValue.fillAttribute(theAttribute);
171 //--------------------------------------------------------------------------------------
172 void fillAttribute(const std::list<ModelHighAPI_Selection> & theValue,
173 const std::shared_ptr<ModelAPI_AttributeSelectionList> & theAttribute)
175 theAttribute->clear();
176 for (auto it = theValue.begin(); it != theValue.end(); ++it)
177 it->appendToList(theAttribute);
180 //--------------------------------------------------------------------------------------
181 void fillAttribute(const std::string & theValue,
182 const std::shared_ptr<ModelAPI_AttributeString> & theAttribute)
184 theAttribute->setValue(theValue);
186 void fillAttribute(const char * theValue,
187 const std::shared_ptr<ModelAPI_AttributeString> & theAttribute)
189 theAttribute->setValue(theValue);
192 //==================================================================================================
193 GeomAPI_Shape::ShapeType shapeTypeByStr(std::string theShapeTypeStr)
195 GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
197 std::transform(theShapeTypeStr.begin(), theShapeTypeStr.end(), theShapeTypeStr.begin(), ::tolower);
199 if(theShapeTypeStr == "compound") {
200 aShapeType = GeomAPI_Shape::COMPOUND;
201 } else if(theShapeTypeStr == "compsolid") {
202 aShapeType = GeomAPI_Shape::COMPSOLID;
203 } else if(theShapeTypeStr == "solid") {
204 aShapeType = GeomAPI_Shape::SOLID;
205 } else if(theShapeTypeStr == "shell") {
206 aShapeType = GeomAPI_Shape::SHELL;
207 } else if(theShapeTypeStr == "face") {
208 aShapeType = GeomAPI_Shape::FACE;
209 } else if(theShapeTypeStr == "wire") {
210 aShapeType = GeomAPI_Shape::WIRE;
211 } else if(theShapeTypeStr == "edge") {
212 aShapeType = GeomAPI_Shape::EDGE;
213 } else if(theShapeTypeStr == "vertex") {
214 aShapeType = GeomAPI_Shape::VERTEX;
215 } else if(theShapeTypeStr == "shape") {
216 aShapeType = GeomAPI_Shape::SHAPE;
222 //==================================================================================================
223 GeomAPI_Shape::ShapeType getShapeType(const ModelHighAPI_Selection& theSelection)
225 GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
227 switch(theSelection.variantType()) {
228 case ModelHighAPI_Selection::VT_ResultSubShapePair: {
229 ResultSubShapePair aPair = theSelection.resultSubShapePair();
230 GeomShapePtr aShape = aPair.second;
232 aShape = aPair.first->shape();
237 aShapeType = aShape->shapeType();
240 case ModelHighAPI_Selection::VT_TypeSubShapeNamePair: {
241 TypeSubShapeNamePair aPair = theSelection.typeSubShapeNamePair();
242 std::string aType = aPair.first;
243 aShapeType = shapeTypeByStr(aType);
251 /// stores the features information, recoursively stores sub-documetns features
252 std::string storeFeatures(const std::string& theDocName, DocumentPtr theDoc,
253 std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> >& theStore,
254 const bool theCompare) // if false => store
256 std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> >::iterator aDocFind;
258 aDocFind = theStore.find(theDocName);
259 if (aDocFind == theStore.end()) {
260 return "Document '" + theDocName + "' not found";
263 // store the model features information: iterate all features
264 int aFeaturesCount = 0; // stores the number of compared features for this document to compate
265 std::set<std::string> aProcessed; // processed features names (that are in the current document)
266 std::list<FeaturePtr> allFeatures = theDoc->allFeatures();
267 std::list<FeaturePtr>::iterator allIter = allFeatures.begin();
268 for(; allIter != allFeatures.end(); allIter++) {
269 FeaturePtr aFeat = *allIter;
271 std::map<std::string, ModelHighAPI_FeatureStore>::iterator
272 aFeatFind = aDocFind->second.find(aFeat->name());
273 if (aFeatFind == aDocFind->second.end()) {
274 return "Document '" + theDocName + "' feature '" + aFeat->name() + "' not found";
276 std::string anError = aFeatFind->second.compare(aFeat);
277 if (!anError.empty()) {
281 aProcessed.insert(aFeat->name());
283 theStore[theDocName][aFeat->name()] = ModelHighAPI_FeatureStore(aFeat);
285 // iterate all results of this feature
286 std::list<ResultPtr> allResults;
287 ModelAPI_Tools::allResults(aFeat, allResults);
288 std::list<ResultPtr>::iterator aRes = allResults.begin();
289 for(; aRes != allResults.end(); aRes++) {
290 if ((*aRes)->groupName() == ModelAPI_ResultPart::group()) { // recoursively store features of sub-documents
291 DocumentPtr aDoc = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes)->partDoc();
293 std::string anError = storeFeatures((*aRes)->data()->name(), aDoc, theStore, theCompare);
294 if (!anError.empty())
300 // checks the number of compared features
302 if (aDocFind->second.size() != aFeaturesCount) {
303 // search for disappeared feature
304 std::string aLostName;
305 std::map<std::string, ModelHighAPI_FeatureStore>::iterator aLostIter;
306 for(aLostIter = aDocFind->second.begin(); aLostIter != aDocFind->second.end(); aLostIter++) {
307 if (aProcessed.find(aLostIter->first) == aProcessed.end()) {
308 aLostName = aLostIter->first;
311 return "For document '" + theDocName +
312 "' the number of features becomes smaller, there is no feature '" + aLostName + "'";
318 //==================================================================================================
319 bool checkPythonDump()
321 SessionPtr aSession = ModelAPI_Session::get();
322 // dump all to the python file
323 aSession->startOperation("Check python dump");
324 FeaturePtr aDump = aSession->moduleDocument()->addFeature("Dump");
326 aDump->string("file_path")->setValue("check_dump.py"); // to the current folder
327 aDump->string("file_format")->setValue("py"); // to the current folder
330 bool isProblem = !aDump.get() || !aDump->error().empty(); // after "finish" dump will be removed
331 aSession->finishOperation();
333 return false; // something is wrong during dump
335 // map from document name to feature name to feature data
336 std::map<std::string, std::map<std::string, ModelHighAPI_FeatureStore> > aStore;
337 std::string anError = storeFeatures(
338 aSession->moduleDocument()->kind(), aSession->moduleDocument(), aStore, false);
339 if (!anError.empty()) {
340 Events_InfoMessage anError("checkPythonDump", anError);
344 // close all before importation of the script
345 aSession->closeAll();
346 // execute the dumped
347 Config_ModuleReader::loadScript("check_dump");
349 // compare with the stored data
350 anError = storeFeatures(
351 aSession->moduleDocument()->kind(), aSession->moduleDocument(), aStore, true);
352 if (!anError.empty()) {
353 std::cout<<anError<<std::endl;
354 Events_InfoMessage anError("checkPythonDump", anError);
362 //--------------------------------------------------------------------------------------