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 static int gCompositeStackDepth = 0;
42 ModelHighAPI_Dumper* ModelHighAPI_Dumper::mySelf = 0;
44 ModelHighAPI_Dumper::ModelHighAPI_Dumper()
49 void ModelHighAPI_Dumper::setInstance(ModelHighAPI_Dumper* theDumper)
55 ModelHighAPI_Dumper* ModelHighAPI_Dumper::getInstance()
60 void ModelHighAPI_Dumper::clear(bool bufferOnly)
63 myDumpBuffer << std::setprecision(16);
69 myFullDump << std::setprecision(16);
73 myFeatureCount.clear();
74 while (!myEntitiesStack.empty())
75 myEntitiesStack.pop();
79 void ModelHighAPI_Dumper::clearNotDumped()
81 myNotDumpedEntities.clear();
84 const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity,
85 bool theSaveNotDumped,
86 bool theUseEntityName)
88 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
89 if (aFound != myNames.end())
90 return aFound->second.first;
92 // entity is not found, store it
94 std::ostringstream aDefaultName;
95 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
97 aName = aFeature->name();
98 const std::string& aKind = aFeature->getKind();
99 DocumentPtr aDoc = aFeature->document();
100 int& aNbFeatures = myFeatureCount[aDoc][aKind];
103 size_t anIndex = aName.find(aKind);
104 if (anIndex == 0 && aName[aKind.length()] == '_') { // name starts with "FeatureKind_"
105 std::string anIdStr = aName.substr(aKind.length() + 1);
106 int anId = std::stoi(anIdStr);
108 // Check number of already registered objects of such kind. Index of current object
109 // should be the same to identify feature's name as automatically generated.
110 if (aNbFeatures == anId) {
111 // name is not user-defined
116 // obtain default name for the feature
117 if (theUseEntityName)
118 aDefaultName << aName;
121 NbFeaturesMap::const_iterator aFIt = myFeatureCount.begin();
122 for (; aFIt != myFeatureCount.end(); ++aFIt) {
123 std::map<std::string, int>::const_iterator aFound = aFIt->second.find(aKind);
124 if (aFound != aFIt->second.end())
125 aFullIndex += aFound->second;
127 aDefaultName << aKind << "_" << aFullIndex;
130 // store names of results
131 saveResultNames(aFeature);
134 myNames[theEntity] = std::pair<std::string, std::string>(aDefaultName.str(), aName);
135 if (theSaveNotDumped)
136 myNotDumpedEntities.insert(theEntity);
137 return myNames[theEntity].first;
140 const std::string& ModelHighAPI_Dumper::parentName(const FeaturePtr& theEntity)
142 const std::set<AttributePtr>& aRefs = theEntity->data()->refsToMe();
143 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
144 for (; aRefIt != aRefs.end(); ++aRefIt) {
145 CompositeFeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(
146 ModelAPI_Feature::feature((*aRefIt)->owner()));
148 return name(anOwner);
151 static const std::string DUMMY;
155 void ModelHighAPI_Dumper::saveResultNames(const FeaturePtr& theFeature)
157 std::string aFeatureName = theFeature->name();
158 const std::list<ResultPtr>& aResults = theFeature->results();
159 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
160 for (int i = 1; aResIt != aResults.end(); ++aResIt, ++i) {
161 bool isUserDefined = true;
162 std::string aResName = (*aResIt)->data()->name();
163 size_t anIndex = aResName.find(aFeatureName);
165 std::string aSuffix = aResName.substr(aFeatureName.length());
166 if (aSuffix.empty() && i == 1) // first result may not constain index in the name
167 isUserDefined = false;
169 if (aSuffix[0] == '_' && std::stoi(aSuffix.substr(1)) == i)
170 isUserDefined = false;
174 myNames[*aResIt] = std::pair<std::string, std::string>(aResName,
175 isUserDefined ? aResName : std::string());
179 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc,
180 const std::string& theFileName)
182 // dump top level document feature
183 static const std::string aDocName("partSet");
184 myNames[theDoc] = std::pair<std::string, std::string>(aDocName, std::string());
185 *this << aDocName << " = model.moduleDocument()" << std::endl;
187 // dump subfeatures and store result to file
188 return process(theDoc) && exportTo(theFileName);
191 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc)
194 std::list<FeaturePtr> aFeatures = theDoc->allFeatures();
195 std::list<FeaturePtr>::const_iterator aFeatIt = aFeatures.begin();
196 // firstly, dump all parameters
197 for (; aFeatIt != aFeatures.end(); ++ aFeatIt)
198 dumpParameter(*aFeatIt);
199 // dump all other features
200 for (aFeatIt = aFeatures.begin(); aFeatIt != aFeatures.end(); ++aFeatIt) {
201 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFeatIt);
202 if (aCompFeat) // iteratively process composite features
203 isOk = process(aCompFeat) && isOk;
204 else if (!isDumped(*aFeatIt)) // dump common feature
205 dumpFeature(*aFeatIt);
210 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite, bool isForce)
212 // increase composite features stack
213 ++gCompositeStackDepth;
214 // dump composite itself
215 if (!isDumped(theComposite) || isForce)
216 dumpFeature(theComposite, isForce);
218 // sub-part is processed independently, because it provides separate document
219 if (theComposite->getKind() == PartSetPlugin_Part::ID()) {
220 // decrease composite features stack because we run into separate document
221 --gCompositeStackDepth;
223 ResultPartPtr aPartResult =
224 std::dynamic_pointer_cast<ModelAPI_ResultPart>(theComposite->lastResult());
227 DocumentPtr aSubDoc = aPartResult->partDoc();
230 // set name of document
231 const std::string& aPartName = myNames[theComposite].first;
232 std::string aDocName = aPartName + "_doc";
233 myNames[aSubDoc] = std::pair<std::string, std::string>(aDocName, std::string());
235 // dump document in a separate line
236 *this << aDocName << " = " << aPartName << ".document()" << std::endl;
237 // dump features in the document
238 return process(aSubDoc);
242 bool isOk = processSubs(theComposite);
243 // decrease composite features stack
244 --gCompositeStackDepth;
249 bool ModelHighAPI_Dumper::processSubs(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
253 // dump all sub-features;
254 int aNbSubs = theComposite->numberOfSubs();
255 for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) {
256 FeaturePtr aFeature = theComposite->subFeature(anIndex);
257 if (isDumped(aFeature))
260 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
261 if (aCompFeat) // iteratively process composite features
262 isOk = process(aCompFeat) && isOk;
264 dumpFeature(aFeature, true);
267 bool isDumpSetName = !myEntitiesStack.empty() &&
268 myEntitiesStack.top().myEntity == EntityPtr(theComposite);
269 bool isForceModelDo = isDumpSetName &&
270 (myEntitiesStack.top().myUserName || !myEntitiesStack.top().myResultsWithName.empty());
271 // It is necessary for the sketch to create its result when complete (command "model.do()").
272 // This option is set by flat theDumpModelDo.
273 // However, nested sketches are rebuilt by parent feature, so, they do not need
274 // explicit call of "model.do()". This will be controlled by the depth of the stack.
275 if (isForceModelDo || (theDumpModelDo && gCompositeStackDepth <= 1))
276 *this << "model.do()" << std::endl;
278 // dump "setName" for composite feature
284 bool ModelHighAPI_Dumper::exportTo(const std::string& theFileName)
287 OSD_OpenStream(aFile, theFileName.c_str(), std::ofstream::out);
288 if (!aFile.is_open())
292 for (ModulesMap::const_iterator aModIt = myModules.begin();
293 aModIt != myModules.end(); ++aModIt) {
294 aFile << "from " << aModIt->first << " import ";
295 if (aModIt->second.empty() ||
296 aModIt->second.find(std::string()) != aModIt->second.end())
297 aFile << "*"; // import whole module
299 // import specific features
300 std::set<std::string>::const_iterator anObjIt = aModIt->second.begin();
302 for (++anObjIt; anObjIt != aModIt->second.end(); ++anObjIt)
303 aFile << ", " << *anObjIt;
307 if (!myModules.empty())
310 aFile << "import model" << std::endl << std::endl;
311 aFile << "model.begin()" << std::endl;
313 // dump collected data
314 aFile << myFullDump.str();
315 aFile << myDumpBuffer.str();
318 aFile << "model.end()" << std::endl;
326 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName,
327 const std::string& theObject)
329 myModules[theModuleName].insert(theObject);
332 void ModelHighAPI_Dumper::dumpEntitySetName()
334 const LastDumpedEntity& aLastDumped = myEntitiesStack.top();
336 // dump "setName" for the entity
337 if (aLastDumped.myUserName) {
338 std::pair<std::string, std::string> anEntityNames = myNames[aLastDumped.myEntity];
339 if (!anEntityNames.second.empty())
340 myDumpBuffer << anEntityNames.first << ".setName(\"" << anEntityNames.second << "\")" << std::endl;
341 anEntityNames.second.clear(); // don't dump "setName" for the entity twice
343 // dump "setName" for results
344 std::list<ResultPtr>::const_iterator aResIt = aLastDumped.myResultsWithName.begin();
345 std::list<ResultPtr>::const_iterator aResEnd = aLastDumped.myResultsWithName.end();
346 for (; aResIt != aResEnd; ++aResIt) {
347 std::pair<std::string, std::string> anEntityNames = myNames[*aResIt];
348 if (!anEntityNames.second.empty()) {
350 myDumpBuffer << ".result().data().setName(\"" << anEntityNames.second << "\")" << std::endl;
351 anEntityNames.second.clear(); // don't dump "setName" for the entity twice
355 myEntitiesStack.pop();
358 bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const
360 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
361 return aFound != myNames.end();
364 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
366 myDumpBuffer << theChar;
370 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
372 myDumpBuffer << theString;
376 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
378 myDumpBuffer << theString;
382 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const bool theValue)
384 myDumpBuffer << (theValue ? "True" : "False");
388 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
390 myDumpBuffer << theValue;
394 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
396 myDumpBuffer << theValue;
400 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
402 importModule("GeomAPI", "GeomAPI_Pnt");
403 myDumpBuffer << "GeomAPI_Pnt(" << thePoint->x() << ", "
404 << thePoint->y() << ", " << thePoint->z() << ")";
408 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
410 importModule("GeomAPI", "GeomAPI_Dir");
411 myDumpBuffer << "GeomAPI_Dir(" << theDir->x() << ", "
412 << theDir->y() << ", " << theDir->z() << ")";
416 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
417 const std::shared_ptr<GeomDataAPI_Dir>& theDir)
419 myDumpBuffer << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
423 static void dumpArray(std::ostringstream& theOutput, int theSize,
424 double* theValues, std::string* theTexts)
426 for (int i = 0; i < theSize; ++i) {
429 if (theTexts[i].empty())
430 theOutput << theValues[i];
432 theOutput << "\"" << theTexts[i] << "\"";
436 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
437 const std::shared_ptr<GeomDataAPI_Point>& thePoint)
439 static const int aSize = 3;
440 double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
441 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()};
442 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
446 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
447 const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
449 static const int aSize = 2;
450 double aValues[aSize] = {thePoint->x(), thePoint->y()};
451 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()};
452 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
456 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
457 const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
459 myDumpBuffer << (theAttrBool->value() ? "True" : "False");
463 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
464 const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
466 std::string aText = theAttrInt->text();
468 myDumpBuffer << theAttrInt->value();
470 myDumpBuffer << "\"" << aText << "\"";
474 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
475 const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
477 std::string aText = theAttrReal->text();
479 myDumpBuffer << theAttrReal->value();
481 myDumpBuffer << "\"" << aText << "\"";
485 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
486 const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
488 myDumpBuffer << "\"" << theAttrStr->value() << "\"";
492 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity)
494 myDumpBuffer << name(theEntity);
496 bool isUserDefindName = !myNames[theEntity].second.empty();
497 // store results if they have user-defined names
498 std::list<ResultPtr> aResultsWithUserName;
499 const std::list<ResultPtr>& aResults = theEntity->results();
500 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
501 for (; aResIt != aResults.end(); ++aResIt)
502 if (!myNames[*aResIt].second.empty())
503 aResultsWithUserName.push_back(*aResIt);
504 // store just dumped entity to stack
505 myEntitiesStack.push(LastDumpedEntity(theEntity, isUserDefindName, aResultsWithUserName));
507 // remove entity from the list of not dumped items
508 myNotDumpedEntities.erase(theEntity);
512 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ResultPtr& theResult)
514 FeaturePtr aFeature = ModelAPI_Feature::feature(theResult);
516 std::list<ResultPtr> aResults = aFeature->results();
517 for(std::list<ResultPtr>::const_iterator anIt = aResults.cbegin(); anIt != aResults.cend(); ++anIt, ++anIndex) {
518 if(theResult->isSame(*anIt)) {
522 myDumpBuffer << name(aFeature) << ".result()[" << anIndex << "]";
526 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ObjectPtr& theObject)
528 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
530 myDumpBuffer << name(aFeature);
534 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
543 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const AttributePtr& theAttr)
545 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttr->owner());
546 myDumpBuffer << name(anOwner) << "." << attributeGetter(anOwner, theAttr->id()) << "()";
550 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
551 const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
553 if (theRefAttr->isObject())
554 *this << theRefAttr->object();
556 *this << theRefAttr->attr();
560 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
561 const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList)
564 std::list<std::pair<ObjectPtr, AttributePtr> > aList = theRefAttrList->list();
565 bool isAdded = false;
566 std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aList.begin();
567 for (; anIt != aList.end(); ++anIt) {
569 myDumpBuffer << ", ";
573 *this << anIt->first;
574 else if (anIt->second)
575 * this << anIt->second;
581 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
582 const std::shared_ptr<ModelAPI_AttributeReference>& theReference)
584 *this << theReference->value();
588 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
589 const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList)
591 static const int aThreshold = 2;
592 // if number of elements in the list if greater than a threshold,
593 // dump it in a separate line with specific name
594 std::string aDumped = myDumpBuffer.str();
595 if (aDumped.empty() || theRefList->size() <= aThreshold) {
597 std::list<ObjectPtr> aList = theRefList->list();
598 bool isAdded = false;
599 std::list<ObjectPtr>::const_iterator anIt = aList.begin();
600 for (; anIt != aList.end(); ++anIt) {
602 myDumpBuffer << ", ";
610 // clear buffer and store list "as is"
611 myDumpBuffer.str("");
613 // save buffer and clear it again
614 std::string aDumpedList = myDumpBuffer.str();
615 myDumpBuffer.str("");
616 // obtain name of list
617 FeaturePtr anOwner = ModelAPI_Feature::feature(theRefList->owner());
618 std::string aListName = name(anOwner) + "_objects";
619 // store all previous data
620 myDumpBuffer << aListName << " = " << aDumpedList << std::endl
621 << aDumped << aListName;
626 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
627 const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
629 myDumpBuffer << "model.selection(";
631 if(!theAttrSelect->isInitialized()) {
636 GeomShapePtr aShape = theAttrSelect->value();
638 aShape = theAttrSelect->context()->shape();
646 myDumpBuffer << "\"" << aShape->shapeTypeStr() << "\", \"" << theAttrSelect->namingName() << "\")";
650 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
651 const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList)
656 std::string aShapeTypeStr;
658 bool isAdded = false;
660 for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) {
661 AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex);
662 aShape = anAttribute->value();
664 aShape = anAttribute->context()->shape();
672 myDumpBuffer << ", ";
676 myDumpBuffer << "model.selection(\"" << aShape->shapeTypeStr() << "\", \"" << anAttribute->namingName() << "\")";
685 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
686 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
688 theDumper.myDumpBuffer << theEndl;
690 if (!theDumper.myEntitiesStack.empty()) {
691 // Name for composite feature is dumped when all sub-entities are dumped
692 // (see method ModelHighAPI_Dumper::processSubs).
693 const ModelHighAPI_Dumper::LastDumpedEntity& aLastDumped = theDumper.myEntitiesStack.top();
694 CompositeFeaturePtr aComposite =
695 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aLastDumped.myEntity);
697 theDumper.dumpEntitySetName();
700 // store all not-dumped entities first
701 std::set<EntityPtr> aNotDumped = theDumper.myNotDumpedEntities;
702 std::string aBufCopy = theDumper.myDumpBuffer.str();
703 theDumper.clear(true);
704 std::set<EntityPtr>::const_iterator anIt = aNotDumped.begin();
705 for (; anIt != aNotDumped.end(); ++anIt) {
706 // if the feature is composite, dump it with all subs
707 CompositeFeaturePtr aCompFeat =
708 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
710 theDumper.process(aCompFeat, true);
712 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
713 theDumper.dumpFeature(aFeature, true);
717 // avoid multiple empty lines
718 size_t anInd = std::string::npos;
719 while ((anInd = aBufCopy.find("\n\n\n")) != std::string::npos)
720 aBufCopy.erase(anInd, 1);
721 // then store currently dumped string
722 theDumper.myFullDump << aBufCopy;