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 <Config_PropManager.h>
12 #include <GeomAPI_Pnt.h>
13 #include <GeomAPI_Dir.h>
14 #include <GeomAPI_ShapeExplorer.h>
16 #include <GeomDataAPI_Dir.h>
17 #include <GeomDataAPI_Point.h>
18 #include <GeomDataAPI_Point2D.h>
20 #include <ModelAPI_AttributeBoolean.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_CompositeFeature.h>
32 #include <ModelAPI_Document.h>
33 #include <ModelAPI_Entity.h>
34 #include <ModelAPI_Feature.h>
35 #include <ModelAPI_Result.h>
36 #include <ModelAPI_ResultBody.h>
37 #include <ModelAPI_ResultConstruction.h>
38 #include <ModelAPI_ResultPart.h>
40 #include <PartSetPlugin_Part.h>
42 #include <OSD_OpenFile.hxx>
46 static int gCompositeStackDepth = 0;
48 ModelHighAPI_Dumper* ModelHighAPI_Dumper::mySelf = 0;
50 ModelHighAPI_Dumper::ModelHighAPI_Dumper()
55 void ModelHighAPI_Dumper::setInstance(ModelHighAPI_Dumper* theDumper)
61 ModelHighAPI_Dumper* ModelHighAPI_Dumper::getInstance()
66 void ModelHighAPI_Dumper::clear(bool bufferOnly)
69 myDumpBuffer << std::setprecision(16);
75 myFullDump << std::setprecision(16);
79 myFeatureCount.clear();
80 while (!myEntitiesStack.empty())
81 myEntitiesStack.pop();
85 void ModelHighAPI_Dumper::clearNotDumped()
87 myNotDumpedEntities.clear();
90 // Convert string to integer. If the string is not a number, return -1
91 static int toInt(const std::string& theString)
93 std::string::const_iterator aChar = theString.begin();
94 for (; aChar != theString.end(); ++aChar)
95 if (!std::isdigit(*aChar))
97 if (aChar != theString.end())
98 return -1; // not a number
99 return std::stoi(theString);
102 const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity,
103 bool theSaveNotDumped,
104 bool theUseEntityName)
106 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
107 if (aFound != myNames.end())
108 return aFound->second.myCurrentName;
110 // entity is not found, store it
112 bool isDefaultName = false;
113 std::ostringstream aDefaultName;
114 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
116 aName = aFeature->name();
117 const std::string& aKind = aFeature->getKind();
118 DocumentPtr aDoc = aFeature->document();
119 int& aNbFeatures = myFeatureCount[aDoc][aKind];
122 size_t anIndex = aName.find(aKind);
123 if (anIndex == 0 && aName[aKind.length()] == '_') { // name starts with "FeatureKind_"
124 std::string anIdStr = aName.substr(aKind.length() + 1);
125 int anId = toInt(anIdStr);
127 // Check number of already registered objects of such kind. Index of current object
128 // should be the same to identify feature's name as automatically generated.
129 if (aNbFeatures == anId) {
130 // name is not user-defined
131 isDefaultName = true;
135 // obtain default name for the feature
136 if (theUseEntityName)
137 aDefaultName << aName;
140 NbFeaturesMap::const_iterator aFIt = myFeatureCount.begin();
141 for (; aFIt != myFeatureCount.end(); ++aFIt) {
142 std::map<std::string, int>::const_iterator aFound = aFIt->second.find(aKind);
143 if (aFound != aFIt->second.end())
144 aFullIndex += aFound->second;
146 aDefaultName << aKind << "_" << aFullIndex;
150 myNames[theEntity] = EntityName(aDefaultName.str(), aName, isDefaultName);
151 if (theSaveNotDumped)
152 myNotDumpedEntities.insert(theEntity);
154 // store names of results
156 saveResultNames(aFeature);
158 return myNames[theEntity].myCurrentName;
161 const std::string& ModelHighAPI_Dumper::parentName(const FeaturePtr& theEntity)
163 const std::set<AttributePtr>& aRefs = theEntity->data()->refsToMe();
164 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
165 for (; aRefIt != aRefs.end(); ++aRefIt) {
166 CompositeFeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(
167 ModelAPI_Feature::feature((*aRefIt)->owner()));
169 return name(anOwner);
172 static const std::string DUMMY;
176 void ModelHighAPI_Dumper::saveResultNames(const FeaturePtr& theFeature)
178 // Default name of the feature
179 const std::string& aKind = theFeature->getKind();
180 DocumentPtr aDoc = theFeature->document();
181 int aNbFeatures = myFeatureCount[aDoc][aKind];
182 std::ostringstream aNameStream;
183 aNameStream << aKind << "_" << aNbFeatures;
184 std::string aFeatureName = aNameStream.str();
186 // Save only names of results which is not correspond to default feature name
187 const std::list<ResultPtr>& aResults = theFeature->results();
188 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
189 for (int i = 1; aResIt != aResults.end(); ++aResIt, ++i) {
190 bool isUserDefined = true;
191 std::string aResName = (*aResIt)->data()->name();
192 size_t anIndex = aResName.find(aFeatureName);
194 std::string aSuffix = aResName.substr(aFeatureName.length());
195 if (aSuffix.empty() && i == 1) // first result may not constain index in the name
196 isUserDefined = false;
198 if (aSuffix[0] == '_' && std::stoi(aSuffix.substr(1)) == i)
199 isUserDefined = false;
203 myNames[*aResIt] = EntityName(aResName,
204 (isUserDefined ? aResName : std::string()), !isUserDefined);
208 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc,
209 const std::string& theFileName)
211 // dump top level document feature
212 static const std::string aDocName("partSet");
213 myNames[theDoc] = EntityName(aDocName, std::string(), true);
214 *this << aDocName << " = model.moduleDocument()" << std::endl;
216 // dump subfeatures and store result to file
217 return process(theDoc) && exportTo(theFileName);
220 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc)
223 std::list<FeaturePtr> aFeatures = theDoc->allFeatures();
224 std::list<FeaturePtr>::const_iterator aFeatIt = aFeatures.begin();
225 // firstly, dump all parameters
226 for (; aFeatIt != aFeatures.end(); ++ aFeatIt)
227 dumpParameter(*aFeatIt);
228 // dump all other features
229 for (aFeatIt = aFeatures.begin(); aFeatIt != aFeatures.end(); ++aFeatIt) {
230 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFeatIt);
231 if (aCompFeat) // iteratively process composite features
232 isOk = process(aCompFeat) && isOk;
233 else if (!isDumped(*aFeatIt)) // dump common feature
234 dumpFeature(*aFeatIt);
239 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite, bool isForce)
241 // increase composite features stack
242 ++gCompositeStackDepth;
243 // dump composite itself
244 if (!isDumped(theComposite) || isForce)
245 dumpFeature(FeaturePtr(theComposite), isForce);
247 // sub-part is processed independently, because it provides separate document
248 if (theComposite->getKind() == PartSetPlugin_Part::ID()) {
249 // dump name of the part if it is different from default
250 if (!myEntitiesStack.empty())
253 // decrease composite features stack because we run into separate document
254 --gCompositeStackDepth;
256 ResultPartPtr aPartResult =
257 std::dynamic_pointer_cast<ModelAPI_ResultPart>(theComposite->lastResult());
260 DocumentPtr aSubDoc = aPartResult->partDoc();
263 // set name of document
264 const std::string& aPartName = myNames[theComposite].myCurrentName;
265 std::string aDocName = aPartName + "_doc";
266 myNames[aSubDoc] = EntityName(aDocName, std::string(), true);
268 // dump document in a separate line
269 *this << aDocName << " = " << aPartName << ".document()" << std::endl;
270 // dump features in the document
271 return process(aSubDoc);
275 bool isOk = processSubs(theComposite);
276 // decrease composite features stack
277 --gCompositeStackDepth;
282 bool ModelHighAPI_Dumper::processSubs(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
286 // dump all sub-features;
287 bool isSubDumped = false;
288 int aNbSubs = theComposite->numberOfSubs();
289 for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) {
290 FeaturePtr aFeature = theComposite->subFeature(anIndex);
291 if (isDumped(aFeature))
295 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
296 if (aCompFeat) // iteratively process composite features
297 isOk = process(aCompFeat) && isOk;
299 dumpFeature(aFeature, true);
302 bool isDumpSetName = !myEntitiesStack.empty() &&
303 myEntitiesStack.top().myEntity == EntityPtr(theComposite);
304 bool isForceModelDo = isSubDumped && isDumpSetName &&
305 (myEntitiesStack.top().myUserName || !myEntitiesStack.top().myResults.empty());
306 // It is necessary for the sketch to create its result when complete (command "model.do()").
307 // This option is set by flat theDumpModelDo.
308 // However, nested sketches are rebuilt by parent feature, so, they do not need
309 // explicit call of "model.do()". This will be controlled by the depth of the stack.
310 if (isForceModelDo || (theDumpModelDo && gCompositeStackDepth <= 1))
311 *this << "model.do()" << std::endl;
313 // dump "setName" for composite feature
319 void ModelHighAPI_Dumper::dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
320 const FeaturePtr& theSubFeature)
322 name(theSubFeature, false);
323 myNames[theSubFeature] = EntityName(theSubFeatureGet, theSubFeature->name(), false);
325 // store results if they have user-defined names or colors
326 std::list<ResultPtr> aResultsWithNameOrColor;
327 const std::list<ResultPtr>& aResults = theSubFeature->results();
328 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
329 for (; aResIt != aResults.end(); ++aResIt) {
330 std::string aResName = (*aResIt)->data()->name();
331 myNames[*aResIt] = EntityName(aResName, aResName, false);
332 aResultsWithNameOrColor.push_back(*aResIt);
335 // store just dumped entity to stack
336 myEntitiesStack.push(LastDumpedEntity(theSubFeature, true, aResultsWithNameOrColor));
341 bool ModelHighAPI_Dumper::exportTo(const std::string& theFileName)
344 OSD_OpenStream(aFile, theFileName.c_str(), std::ofstream::out);
345 if (!aFile.is_open())
349 for (ModulesMap::const_iterator aModIt = myModules.begin();
350 aModIt != myModules.end(); ++aModIt) {
351 aFile << "from " << aModIt->first << " import ";
352 if (aModIt->second.empty() ||
353 aModIt->second.find(std::string()) != aModIt->second.end())
354 aFile << "*"; // import whole module
356 // import specific features
357 std::set<std::string>::const_iterator anObjIt = aModIt->second.begin();
359 for (++anObjIt; anObjIt != aModIt->second.end(); ++anObjIt)
360 aFile << ", " << *anObjIt;
364 if (!myModules.empty())
367 aFile << "import model" << std::endl << std::endl;
368 aFile << "model.begin()" << std::endl;
370 // dump collected data
371 aFile << myFullDump.str();
372 aFile << myDumpBuffer.str();
375 aFile << "model.end()" << std::endl;
383 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName,
384 const std::string& theObject)
386 myModules[theModuleName].insert(theObject);
389 void ModelHighAPI_Dumper::dumpEntitySetName()
391 const LastDumpedEntity& aLastDumped = myEntitiesStack.top();
393 // dump "setName" for the entity
394 if (aLastDumped.myUserName) {
395 EntityName& anEntityNames = myNames[aLastDumped.myEntity];
396 if (!anEntityNames.myIsDefault)
397 myDumpBuffer << anEntityNames.myCurrentName << ".setName(\""
398 << anEntityNames.myUserName << "\")" << std::endl;
399 // don't dump "setName" for the entity twice
400 anEntityNames.myUserName.clear();
401 anEntityNames.myIsDefault = true;
403 // dump "setName" for results
404 std::list<ResultPtr>::const_iterator aResIt = aLastDumped.myResults.begin();
405 std::list<ResultPtr>::const_iterator aResEnd = aLastDumped.myResults.end();
406 for (; aResIt != aResEnd; ++aResIt) {
408 EntityName& anEntityNames = myNames[*aResIt];
409 if (!anEntityNames.myIsDefault) {
411 myDumpBuffer << ".setName(\"" << anEntityNames.myUserName << "\")" << std::endl;
412 // don't dump "setName" for the entity twice
413 anEntityNames.myUserName.clear();
414 anEntityNames.myIsDefault = true;
417 if (!isDefaultColor(*aResIt)) {
418 AttributeIntArrayPtr aColor = (*aResIt)->data()->intArray(ModelAPI_Result::COLOR_ID());
419 if (aColor && aColor->isInitialized()) {
421 myDumpBuffer << ".setColor(" << aColor->value(0) << ", " << aColor->value(1)
422 << ", " << aColor->value(2) << ")" << std::endl;
425 // set result deflection
426 if (!isDefaultDeflection(*aResIt)) {
427 AttributeDoublePtr aDeflectionAttr = (*aResIt)->data()->real(ModelAPI_Result::DEFLECTION_ID());
428 if(aDeflectionAttr.get() && aDeflectionAttr->isInitialized()) {
430 myDumpBuffer << ".setDeflection(" << aDeflectionAttr->value() << ")" << std::endl;
435 myEntitiesStack.pop();
438 bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const
440 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
441 return aFound != myNames.end();
444 bool ModelHighAPI_Dumper::isDefaultColor(const ResultPtr& theResult) const
446 AttributeIntArrayPtr aColor = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
447 if (!aColor || !aColor->isInitialized())
450 std::string aSection, aName, aDefault;
451 theResult->colorConfigInfo(aSection, aName, aDefault);
453 // dump current color
454 std::ostringstream aColorInfo;
455 aColorInfo << aColor->value(0) << "," << aColor->value(1) << "," << aColor->value(2);
457 return aDefault == aColorInfo.str();
460 bool ModelHighAPI_Dumper::isDefaultDeflection(const ResultPtr& theResult) const
462 AttributeDoublePtr aDeflectionAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
463 if(!aDeflectionAttr || !aDeflectionAttr->isInitialized()) {
467 double aCurrent = aDeflectionAttr->value();
468 double aDefault = -1;
470 bool isConstruction = false;
471 std::string aResultGroup = theResult->groupName();
472 if (aResultGroup == ModelAPI_ResultConstruction::group())
473 isConstruction = true;
474 else if (aResultGroup == ModelAPI_ResultBody::group()) {
475 GeomShapePtr aGeomShape = theResult->shape();
476 if (aGeomShape.get()) {
477 // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
478 // correction of deviation for them should not influence to the application performance
479 GeomAPI_ShapeExplorer anExp(aGeomShape, GeomAPI_Shape::FACE);
480 isConstruction = !anExp.more();
484 aDefault = Config_PropManager::real("Visualization", "construction_deflection",
485 ModelAPI_ResultConstruction::DEFAULT_DEFLECTION());
487 aDefault = Config_PropManager::real("Visualization", "body_deflection",
488 ModelAPI_ResultBody::DEFAULT_DEFLECTION());
490 return fabs(aCurrent - aDefault) < 1.e-12;
493 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
495 myDumpBuffer << theChar;
499 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
501 myDumpBuffer << theString;
505 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
507 myDumpBuffer << theString;
511 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const bool theValue)
513 myDumpBuffer << (theValue ? "True" : "False");
517 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
519 myDumpBuffer << theValue;
523 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
525 myDumpBuffer << theValue;
529 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
531 importModule("GeomAPI", "GeomAPI_Pnt");
532 myDumpBuffer << "GeomAPI_Pnt(" << thePoint->x() << ", "
533 << thePoint->y() << ", " << thePoint->z() << ")";
537 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
539 importModule("GeomAPI", "GeomAPI_Dir");
540 myDumpBuffer << "GeomAPI_Dir(" << theDir->x() << ", "
541 << theDir->y() << ", " << theDir->z() << ")";
545 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
546 const std::shared_ptr<GeomDataAPI_Dir>& theDir)
548 myDumpBuffer << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
552 static void dumpArray(std::ostringstream& theOutput, int theSize,
553 double* theValues, std::string* theTexts)
555 for (int i = 0; i < theSize; ++i) {
558 if (theTexts[i].empty())
559 theOutput << theValues[i];
561 theOutput << "\"" << theTexts[i] << "\"";
565 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
566 const std::shared_ptr<GeomDataAPI_Point>& thePoint)
568 static const int aSize = 3;
569 double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
570 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()};
571 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
575 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
576 const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
578 static const int aSize = 2;
579 double aValues[aSize] = {thePoint->x(), thePoint->y()};
580 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()};
581 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
585 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
586 const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
588 myDumpBuffer << (theAttrBool->value() ? "True" : "False");
592 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
593 const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
595 std::string aText = theAttrInt->text();
597 myDumpBuffer << theAttrInt->value();
599 myDumpBuffer << "\"" << aText << "\"";
603 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
604 const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
606 std::string aText = theAttrReal->text();
608 myDumpBuffer << theAttrReal->value();
610 myDumpBuffer << "\"" << aText << "\"";
614 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
615 const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
617 myDumpBuffer << "\"" << theAttrStr->value() << "\"";
621 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity)
623 myDumpBuffer << name(theEntity);
625 bool isUserDefinedName = !myNames[theEntity].myIsDefault;
626 // store results if they have user-defined names or colors
627 std::list<ResultPtr> aResultsWithNameOrColor;
628 const std::list<ResultPtr>& aResults = theEntity->results();
629 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
630 for (; aResIt != aResults.end(); ++aResIt)
631 if (!myNames[*aResIt].myIsDefault || !isDefaultColor(*aResIt) || !isDefaultDeflection(*aResIt))
632 aResultsWithNameOrColor.push_back(*aResIt);
633 // store just dumped entity to stack
634 myEntitiesStack.push(LastDumpedEntity(theEntity, isUserDefinedName, aResultsWithNameOrColor));
636 // remove entity from the list of not dumped items
637 myNotDumpedEntities.erase(theEntity);
641 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ResultPtr& theResult)
643 FeaturePtr aFeature = ModelAPI_Feature::feature(theResult);
645 std::list<ResultPtr> aResults = aFeature->results();
646 for(std::list<ResultPtr>::const_iterator anIt = aResults.cbegin(); anIt != aResults.cend(); ++anIt, ++anIndex) {
647 if(theResult->isSame(*anIt)) {
651 myDumpBuffer << name(aFeature) << ".result()[" << anIndex << "]";
655 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ObjectPtr& theObject)
657 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
659 myDumpBuffer << name(aFeature);
663 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
672 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const AttributePtr& theAttr)
674 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttr->owner());
676 std::string aWrapperPrefix, aWrapperSuffix;
677 // Check the attribute belongs to copied (in multi-translation or multi-rotation) feature.
678 // In this case we need to cast explicitly feature to appropriate type.
679 AttributeBooleanPtr isCopy = anOwner->boolean("Copy");
680 if (isCopy.get() && isCopy->value()) {
681 aWrapperPrefix = featureWrapper(anOwner) + "(";
682 aWrapperSuffix = ")";
683 importModule("SketchAPI");
686 myDumpBuffer << aWrapperPrefix << name(anOwner) << aWrapperSuffix
687 << "." << attributeGetter(anOwner, theAttr->id()) << "()";
691 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
692 const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
694 if (theRefAttr->isObject())
695 *this << theRefAttr->object();
697 *this << theRefAttr->attr();
701 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
702 const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList)
705 std::list<std::pair<ObjectPtr, AttributePtr> > aList = theRefAttrList->list();
706 bool isAdded = false;
707 std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aList.begin();
708 for (; anIt != aList.end(); ++anIt) {
710 myDumpBuffer << ", ";
714 *this << anIt->first;
715 else if (anIt->second)
716 * this << anIt->second;
722 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
723 const std::shared_ptr<ModelAPI_AttributeReference>& theReference)
725 *this << theReference->value();
729 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
730 const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList)
732 static const int aThreshold = 2;
733 // if number of elements in the list if greater than a threshold,
734 // dump it in a separate line with specific name
735 std::string aDumped = myDumpBuffer.str();
736 if (aDumped.empty() || theRefList->size() <= aThreshold) {
738 std::list<ObjectPtr> aList = theRefList->list();
739 bool isAdded = false;
740 std::list<ObjectPtr>::const_iterator anIt = aList.begin();
741 for (; anIt != aList.end(); ++anIt) {
743 myDumpBuffer << ", ";
751 // clear buffer and store list "as is"
752 myDumpBuffer.str("");
754 // save buffer and clear it again
755 std::string aDumpedList = myDumpBuffer.str();
756 myDumpBuffer.str("");
757 // obtain name of list
758 FeaturePtr anOwner = ModelAPI_Feature::feature(theRefList->owner());
759 std::string aListName = name(anOwner) + "_objects";
760 // store all previous data
761 myDumpBuffer << aListName << " = " << aDumpedList << std::endl
762 << aDumped << aListName;
767 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
768 const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
770 myDumpBuffer << "model.selection(";
772 if(!theAttrSelect->isInitialized()) {
777 GeomShapePtr aShape = theAttrSelect->value();
779 aShape = theAttrSelect->context()->shape();
787 myDumpBuffer << "\"" << aShape->shapeTypeStr() << "\", \"" << theAttrSelect->namingName() << "\")";
791 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
792 const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList)
797 std::string aShapeTypeStr;
799 bool isAdded = false;
801 for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) {
802 AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex);
803 aShape = anAttribute->value();
805 aShape = anAttribute->context()->shape();
813 myDumpBuffer << ", ";
817 myDumpBuffer << "model.selection(\"" << aShape->shapeTypeStr() << "\", \"" << anAttribute->namingName() << "\")";
826 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
827 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
829 theDumper.myDumpBuffer << theEndl;
831 if (!theDumper.myEntitiesStack.empty()) {
832 // Name for composite feature is dumped when all sub-entities are dumped
833 // (see method ModelHighAPI_Dumper::processSubs).
834 const ModelHighAPI_Dumper::LastDumpedEntity& aLastDumped = theDumper.myEntitiesStack.top();
835 CompositeFeaturePtr aComposite =
836 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aLastDumped.myEntity);
838 theDumper.dumpEntitySetName();
841 // store all not-dumped entities first
842 std::set<EntityPtr> aNotDumped = theDumper.myNotDumpedEntities;
843 std::string aBufCopy = theDumper.myDumpBuffer.str();
844 theDumper.clear(true);
845 std::set<EntityPtr>::const_iterator anIt = aNotDumped.begin();
846 for (; anIt != aNotDumped.end(); ++anIt) {
847 // if the feature is composite, dump it with all subs
848 CompositeFeaturePtr aCompFeat =
849 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
851 theDumper.process(aCompFeat, true);
853 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
854 theDumper.dumpFeature(aFeature, true);
858 // avoid multiple empty lines
859 size_t anInd = std::string::npos;
860 while ((anInd = aBufCopy.find("\n\n\n")) != std::string::npos)
861 aBufCopy.erase(anInd, 1);
862 // then store currently dumped string
863 theDumper.myFullDump << aBufCopy;