1 // Copyright (C) 2016-20xx CEA/DEN, EDF R&D -->
3 // File: ModelHighAPI_Dumper.cpp
4 // Created: 1 August 2016
5 // Author: Artem ZHIDKOV
7 //--------------------------------------------------------------------------------------
8 #include "ModelHighAPI_Dumper.h"
10 #include <GeomAPI_Pnt.h>
11 #include <GeomAPI_Dir.h>
13 #include <GeomDataAPI_Dir.h>
14 #include <GeomDataAPI_Point.h>
15 #include <GeomDataAPI_Point2D.h>
17 #include <ModelAPI_AttributeBoolean.h>
18 #include <ModelAPI_AttributeDouble.h>
19 #include <ModelAPI_AttributeInteger.h>
20 #include <ModelAPI_AttributeRefAttr.h>
21 #include <ModelAPI_AttributeRefAttrList.h>
22 #include <ModelAPI_AttributeReference.h>
23 #include <ModelAPI_AttributeRefList.h>
24 #include <ModelAPI_AttributeSelection.h>
25 #include <ModelAPI_AttributeSelectionList.h>
26 #include <ModelAPI_AttributeString.h>
27 #include <ModelAPI_CompositeFeature.h>
28 #include <ModelAPI_Document.h>
29 #include <ModelAPI_Entity.h>
30 #include <ModelAPI_Feature.h>
31 #include <ModelAPI_Result.h>
32 #include <ModelAPI_ResultPart.h>
34 #include <PartSetPlugin_Part.h>
36 #include <OSD_OpenFile.hxx>
40 #define DUMP_USER_DEFINED_NAMES
42 static int gCompositeStackDepth = 0;
44 ModelHighAPI_Dumper* ModelHighAPI_Dumper::mySelf = 0;
46 ModelHighAPI_Dumper::ModelHighAPI_Dumper()
51 void ModelHighAPI_Dumper::setInstance(ModelHighAPI_Dumper* theDumper)
57 ModelHighAPI_Dumper* ModelHighAPI_Dumper::getInstance()
62 #define CLEAR_STREAM(theStream) { \
63 std::ostringstream anOther; \
64 swap(theStream, anOther); \
67 void ModelHighAPI_Dumper::clear(bool bufferOnly)
69 CLEAR_STREAM(myDumpBuffer);
70 myDumpBuffer << std::setprecision(16);
75 CLEAR_STREAM(myFullDump);
76 myFullDump << std::setprecision(16);
80 myFeatureCount.clear();
81 myLastEntityWithName = EntityPtr();
85 void ModelHighAPI_Dumper::clearNotDumped()
87 myNotDumpedEntities.clear();
90 const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity, bool theSaveNotDumped)
92 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
93 if (aFound != myNames.end())
94 return aFound->second.first;
96 // entity is not found, store it
98 bool isUserDefined = false;
99 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
101 isUserDefined = true;
102 aName = aFeature->name();
103 const std::string& aKind = aFeature->getKind();
104 DocumentPtr aDoc = aFeature->document();
105 int& aNbFeatures = myFeatureCount[aDoc][aKind];
107 size_t anIndex = aName.find(aKind);
108 if (anIndex == 0 && aName[aKind.length()] == '_') { // name starts with "FeatureKind_"
109 std::string anIdStr = aName.substr(aKind.length() + 1, std::string::npos);
110 int anId = std::stoi(anIdStr);
112 // Check number of already registered objects of such kind. Index of current object
113 // should be the same to identify feature's name as automatically generated.
114 if (aNbFeatures + 1 == anId) {
115 isUserDefined = false;
116 //aNbFeatures = anId - 1;
123 myNames[theEntity] = std::pair<std::string, bool>(aName, isUserDefined);
124 if (theSaveNotDumped)
125 myNotDumpedEntities.insert(theEntity);
126 return myNames[theEntity].first;
129 const std::string& ModelHighAPI_Dumper::parentName(const FeaturePtr& theEntity)
131 const std::set<AttributePtr>& aRefs = theEntity->data()->refsToMe();
132 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
133 for (; aRefIt != aRefs.end(); ++aRefIt) {
134 CompositeFeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(
135 ModelAPI_Feature::feature((*aRefIt)->owner()));
137 return name(anOwner);
140 static const std::string DUMMY;
144 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc,
145 const std::string& theFileName)
147 // dump top level document feature
148 static const std::string aDocName("partSet");
149 myNames[theDoc] = std::pair<std::string, bool>(aDocName, false);
150 *this << aDocName << " = model.moduleDocument()" << std::endl;
152 // dump subfeatures and store result to file
153 return process(theDoc) && exportTo(theFileName);
156 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc)
159 std::list<FeaturePtr> aFeatures = theDoc->allFeatures();
160 std::list<FeaturePtr>::const_iterator aFeatIt = aFeatures.begin();
161 // firstly, dump all parameters
162 for (; aFeatIt != aFeatures.end(); ++ aFeatIt)
163 dumpParameter(*aFeatIt);
164 // dump all other features
165 for (aFeatIt = aFeatures.begin(); aFeatIt != aFeatures.end(); ++aFeatIt) {
166 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFeatIt);
167 if (aCompFeat) // iteratively process composite features
168 isOk = process(aCompFeat) && isOk;
169 else if (!isDumped(*aFeatIt)) // dump common feature
170 dumpFeature(*aFeatIt);
175 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite, bool isForce)
177 // increase composite features stack
178 ++gCompositeStackDepth;
179 // dump composite itself
180 if (!isDumped(theComposite) || isForce)
181 dumpFeature(theComposite, isForce);
183 // sub-part is processed independently, because it provides separate document
184 if (theComposite->getKind() == PartSetPlugin_Part::ID()) {
185 // decrease composite features stack because we run into separate document
186 --gCompositeStackDepth;
188 ResultPartPtr aPartResult =
189 std::dynamic_pointer_cast<ModelAPI_ResultPart>(theComposite->lastResult());
192 DocumentPtr aSubDoc = aPartResult->partDoc();
193 // set name of document
194 const std::string& aPartName = myNames[theComposite].first;
195 std::string aDocName = aPartName + "_doc";
196 myNames[aSubDoc] = std::pair<std::string, bool>(aDocName, false);
198 // dump document in a separate line
199 *this << aDocName << " = " << aPartName << ".document()" << std::endl;
200 // dump features in the document
201 return process(aSubDoc);
205 bool isOk = processSubs(theComposite);
206 // decrease composite features stack
207 --gCompositeStackDepth;
212 bool ModelHighAPI_Dumper::processSubs(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
216 // dump all sub-features;
217 int aNbSubs = theComposite->numberOfSubs();
218 //////////////////////////////////////
219 std::list<ObjectPtr> aList = theComposite->reflist("Features")->list();
220 //////////////////////////////////////
221 for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) {
222 FeaturePtr aFeature = theComposite->subFeature(anIndex);
223 if (isDumped(aFeature))
226 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
227 if (aCompFeat) // iteratively process composite features
228 isOk = process(aCompFeat) && isOk;
230 dumpFeature(aFeature, true);
233 // It is necessary for the sketch to create its result when complete (command "model.do()").
234 // This option is set by flat theDumpModelDo.
235 // However, nested sketches are rebuilt by parent feature, so, they do not need
236 // explicit call of "model.do()". This will be controlled by the depth of the stack.
237 if (theDumpModelDo && gCompositeStackDepth <= 1)
238 *this << "model.do()" << std::endl;
242 bool ModelHighAPI_Dumper::exportTo(const std::string& theFileName)
245 OSD_OpenStream(aFile, theFileName.c_str(), std::ofstream::out);
246 if (!aFile.is_open())
250 for (ModulesMap::const_iterator aModIt = myModules.begin();
251 aModIt != myModules.end(); ++aModIt) {
252 aFile << "from " << aModIt->first << " import ";
253 if (aModIt->second.empty() ||
254 aModIt->second.find(std::string()) != aModIt->second.end())
255 aFile << "*"; // import whole module
257 // import specific features
258 std::set<std::string>::const_iterator anObjIt = aModIt->second.begin();
260 for (++anObjIt; anObjIt != aModIt->second.end(); ++anObjIt)
261 aFile << ", " << *anObjIt;
265 if (!myModules.empty())
268 aFile << "import model" << std::endl << std::endl;
269 aFile << "model.begin()" << std::endl;
271 // dump collected data
272 aFile << myFullDump.str();
275 aFile << "model.end()" << std::endl;
283 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName,
284 const std::string& theObject)
286 myModules[theModuleName].insert(theObject);
289 void ModelHighAPI_Dumper::dumpEntitySetName()
291 if (!myLastEntityWithName)
294 #ifdef DUMP_USER_DEFINED_NAMES
295 const std::string& aName = name(myLastEntityWithName);
296 myDumpBuffer << aName;
297 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(myLastEntityWithName);
299 myDumpBuffer << ".feature()";
300 myDumpBuffer << ".data().setName(\"" << aName << "\")" << std::endl;
302 myNames[myLastEntityWithName].second = false; // don't dump "setName" for the entity twice
303 myLastEntityWithName = EntityPtr();
306 bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const
308 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
309 return aFound != myNames.end();
312 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
314 myDumpBuffer << theChar;
318 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
320 myDumpBuffer << theString;
324 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
326 myDumpBuffer << theString;
330 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const bool theValue)
332 myDumpBuffer << (theValue ? "True" : "False");
336 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
338 myDumpBuffer << theValue;
342 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
344 myDumpBuffer << theValue;
348 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
350 importModule("GeomAPI", "GeomAPI_Pnt");
351 myDumpBuffer << "GeomAPI_Pnt(" << thePoint->x() << ", "
352 << thePoint->y() << ", " << thePoint->z() << ")";
356 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
358 importModule("GeomAPI", "GeomAPI_Dir");
359 myDumpBuffer << "GeomAPI_Dir(" << theDir->x() << ", "
360 << theDir->y() << ", " << theDir->z() << ")";
364 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
365 const std::shared_ptr<GeomDataAPI_Dir>& theDir)
367 myDumpBuffer << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
371 static void dumpArray(std::ostringstream& theOutput, int theSize,
372 double* theValues, std::string* theTexts)
374 for (int i = 0; i < theSize; ++i) {
377 if (theTexts[i].empty())
378 theOutput << theValues[i];
380 theOutput << "\"" << theTexts[i] << "\"";
384 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
385 const std::shared_ptr<GeomDataAPI_Point>& thePoint)
387 static const int aSize = 3;
388 double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
389 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()};
390 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
394 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
395 const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
397 static const int aSize = 2;
398 double aValues[aSize] = {thePoint->x(), thePoint->y()};
399 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()};
400 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
404 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
405 const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
407 myDumpBuffer << (theAttrBool->value() ? "True" : "False");
411 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
412 const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
414 std::string aText = theAttrInt->text();
416 myDumpBuffer << theAttrInt->value();
418 myDumpBuffer << "\"" << aText << "\"";
422 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
423 const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
425 std::string aText = theAttrReal->text();
427 myDumpBuffer << theAttrReal->value();
429 myDumpBuffer << "\"" << aText << "\"";
433 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
434 const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
436 myDumpBuffer << "\"" << theAttrStr->value() << "\"";
440 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity)
442 myDumpBuffer << name(theEntity);
443 if (myNames[theEntity].second)
444 myLastEntityWithName = theEntity;
445 myNotDumpedEntities.erase(theEntity);
449 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ResultPtr& theResult)
451 FeaturePtr aFeature = ModelAPI_Feature::feature(theResult);
453 std::list<ResultPtr> aResults = aFeature->results();
454 for(std::list<ResultPtr>::const_iterator anIt = aResults.cbegin(); anIt != aResults.cend(); ++anIt, ++anIndex) {
455 if(theResult->isSame(*anIt)) {
459 myDumpBuffer << name(aFeature) << ".result()[" << anIndex << "]";
463 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ObjectPtr& theObject)
465 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
467 myDumpBuffer << name(aFeature);
471 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
480 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const AttributePtr& theAttr)
482 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttr->owner());
483 myDumpBuffer << name(anOwner) << "." << attributeGetter(anOwner, theAttr->id()) << "()";
487 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
488 const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
490 if (theRefAttr->isObject())
491 *this << theRefAttr->object();
493 *this << theRefAttr->attr();
497 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
498 const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList)
501 std::list<std::pair<ObjectPtr, AttributePtr> > aList = theRefAttrList->list();
502 bool isAdded = false;
503 std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aList.begin();
504 for (; anIt != aList.end(); ++anIt) {
506 myDumpBuffer << ", ";
510 *this << anIt->first;
511 else if (anIt->second)
512 * this << anIt->second;
518 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
519 const std::shared_ptr<ModelAPI_AttributeReference>& theReference)
521 *this << theReference->value();
525 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
526 const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList)
528 static const int aThreshold = 2;
529 // if number of elements in the list if greater than a threshold,
530 // dump it in a separate line with specific name
531 std::string aDumped = myDumpBuffer.str();
532 if (aDumped.empty() || theRefList->size() <= aThreshold) {
534 std::list<ObjectPtr> aList = theRefList->list();
535 bool isAdded = false;
536 std::list<ObjectPtr>::const_iterator anIt = aList.begin();
537 for (; anIt != aList.end(); ++anIt) {
539 myDumpBuffer << ", ";
547 // clear buffer and store list "as is"
548 CLEAR_STREAM(myDumpBuffer);
550 // save buffer and clear it again
551 std::string aDumpedList = myDumpBuffer.str();
552 CLEAR_STREAM(myDumpBuffer);
553 // obtain name of list
554 FeaturePtr anOwner = ModelAPI_Feature::feature(theRefList->owner());
555 std::string aListName = name(anOwner) + "_objects";
556 // store all previous data
557 myDumpBuffer << aListName << " = " << aDumpedList << std::endl
558 << aDumped << aListName;
563 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
564 const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
566 myDumpBuffer << "model.selection(";
568 if(!theAttrSelect->isInitialized()) {
573 GeomShapePtr aShape = theAttrSelect->value();
575 aShape = theAttrSelect->context()->shape();
583 myDumpBuffer << "\"" << aShape->shapeTypeStr() << "\", \"" << theAttrSelect->namingName() << "\")";
587 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
588 const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList)
593 std::string aShapeTypeStr;
595 bool isAdded = false;
597 for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) {
598 AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex);
599 aShape = anAttribute->value();
601 aShape = anAttribute->context()->shape();
609 myDumpBuffer << ", ";
613 myDumpBuffer << "model.selection(\"" << aShape->shapeTypeStr() << "\", \"" << anAttribute->namingName() << "\")";
622 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
623 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
625 theDumper.myDumpBuffer << theEndl;
626 theDumper.dumpEntitySetName();
628 // store all not-dumped entities first
629 std::set<EntityPtr> aNotDumped = theDumper.myNotDumpedEntities;
630 std::string aBufCopy = theDumper.myDumpBuffer.str();
631 theDumper.clear(true);
632 std::set<EntityPtr>::const_iterator anIt = aNotDumped.begin();
633 for (; anIt != aNotDumped.end(); ++anIt) {
634 // if the feature is composite, dump it with all subs
635 CompositeFeaturePtr aCompFeat =
636 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
638 theDumper.process(aCompFeat, true);
640 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
641 theDumper.dumpFeature(aFeature, true);
645 // avoid multiple empty lines
646 size_t anInd = std::string::npos;
647 while ((anInd = aBufCopy.find("\n\n\n")) != std::string::npos)
648 aBufCopy.erase(anInd, 1);
649 // then store currently dumped string
650 theDumper.myFullDump << aBufCopy;