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 const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity,
91 bool theSaveNotDumped,
92 bool theUseEntityName)
94 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
95 if (aFound != myNames.end())
96 return aFound->second.myCurrentName;
98 // entity is not found, store it
100 bool isDefaultName = false;
101 std::ostringstream aDefaultName;
102 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
104 aName = aFeature->name();
105 const std::string& aKind = aFeature->getKind();
106 DocumentPtr aDoc = aFeature->document();
107 int& aNbFeatures = myFeatureCount[aDoc][aKind];
110 size_t anIndex = aName.find(aKind);
111 if (anIndex == 0 && aName[aKind.length()] == '_') { // name starts with "FeatureKind_"
112 std::string anIdStr = aName.substr(aKind.length() + 1);
113 int anId = std::stoi(anIdStr);
115 // Check number of already registered objects of such kind. Index of current object
116 // should be the same to identify feature's name as automatically generated.
117 if (aNbFeatures == anId) {
118 // name is not user-defined
119 isDefaultName = true;
123 // obtain default name for the feature
124 if (theUseEntityName)
125 aDefaultName << aName;
128 NbFeaturesMap::const_iterator aFIt = myFeatureCount.begin();
129 for (; aFIt != myFeatureCount.end(); ++aFIt) {
130 std::map<std::string, int>::const_iterator aFound = aFIt->second.find(aKind);
131 if (aFound != aFIt->second.end())
132 aFullIndex += aFound->second;
134 aDefaultName << aKind << "_" << aFullIndex;
138 myNames[theEntity] = EntityName(aDefaultName.str(), aName, isDefaultName);
139 if (theSaveNotDumped)
140 myNotDumpedEntities.insert(theEntity);
142 // store names of results
144 saveResultNames(aFeature);
146 return myNames[theEntity].myCurrentName;
149 const std::string& ModelHighAPI_Dumper::parentName(const FeaturePtr& theEntity)
151 const std::set<AttributePtr>& aRefs = theEntity->data()->refsToMe();
152 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
153 for (; aRefIt != aRefs.end(); ++aRefIt) {
154 CompositeFeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(
155 ModelAPI_Feature::feature((*aRefIt)->owner()));
157 return name(anOwner);
160 static const std::string DUMMY;
164 void ModelHighAPI_Dumper::saveResultNames(const FeaturePtr& theFeature)
166 // Default name of the feature
167 const std::string& aKind = theFeature->getKind();
168 DocumentPtr aDoc = theFeature->document();
169 int aNbFeatures = myFeatureCount[aDoc][aKind];
170 std::ostringstream aNameStream;
171 aNameStream << aKind << "_" << aNbFeatures;
172 std::string aFeatureName = aNameStream.str();
174 // Save only names of results which is not correspond to default feature name
175 const std::list<ResultPtr>& aResults = theFeature->results();
176 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
177 for (int i = 1; aResIt != aResults.end(); ++aResIt, ++i) {
178 bool isUserDefined = true;
179 std::string aResName = (*aResIt)->data()->name();
180 size_t anIndex = aResName.find(aFeatureName);
182 std::string aSuffix = aResName.substr(aFeatureName.length());
183 if (aSuffix.empty() && i == 1) // first result may not constain index in the name
184 isUserDefined = false;
186 if (aSuffix[0] == '_' && std::stoi(aSuffix.substr(1)) == i)
187 isUserDefined = false;
191 myNames[*aResIt] = EntityName(aResName,
192 (isUserDefined ? aResName : std::string()), !isUserDefined);
196 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc,
197 const std::string& theFileName)
199 // dump top level document feature
200 static const std::string aDocName("partSet");
201 myNames[theDoc] = EntityName(aDocName, std::string(), true);
202 *this << aDocName << " = model.moduleDocument()" << std::endl;
204 // dump subfeatures and store result to file
205 return process(theDoc) && exportTo(theFileName);
208 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc)
211 std::list<FeaturePtr> aFeatures = theDoc->allFeatures();
212 std::list<FeaturePtr>::const_iterator aFeatIt = aFeatures.begin();
213 // firstly, dump all parameters
214 for (; aFeatIt != aFeatures.end(); ++ aFeatIt)
215 dumpParameter(*aFeatIt);
216 // dump all other features
217 for (aFeatIt = aFeatures.begin(); aFeatIt != aFeatures.end(); ++aFeatIt) {
218 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFeatIt);
219 if (aCompFeat) // iteratively process composite features
220 isOk = process(aCompFeat) && isOk;
221 else if (!isDumped(*aFeatIt)) // dump common feature
222 dumpFeature(*aFeatIt);
227 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite, bool isForce)
229 // increase composite features stack
230 ++gCompositeStackDepth;
231 // dump composite itself
232 if (!isDumped(theComposite) || isForce)
233 dumpFeature(FeaturePtr(theComposite), isForce);
235 // sub-part is processed independently, because it provides separate document
236 if (theComposite->getKind() == PartSetPlugin_Part::ID()) {
237 // decrease composite features stack because we run into separate document
238 --gCompositeStackDepth;
240 ResultPartPtr aPartResult =
241 std::dynamic_pointer_cast<ModelAPI_ResultPart>(theComposite->lastResult());
244 DocumentPtr aSubDoc = aPartResult->partDoc();
247 // set name of document
248 const std::string& aPartName = myNames[theComposite].myCurrentName;
249 std::string aDocName = aPartName + "_doc";
250 myNames[aSubDoc] = EntityName(aDocName, std::string(), true);
252 // dump document in a separate line
253 *this << aDocName << " = " << aPartName << ".document()" << std::endl;
254 // dump features in the document
255 return process(aSubDoc);
259 bool isOk = processSubs(theComposite);
260 // decrease composite features stack
261 --gCompositeStackDepth;
266 bool ModelHighAPI_Dumper::processSubs(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
270 // dump all sub-features;
271 bool isSubDumped = false;
272 int aNbSubs = theComposite->numberOfSubs();
273 for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) {
274 FeaturePtr aFeature = theComposite->subFeature(anIndex);
275 if (isDumped(aFeature))
279 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
280 if (aCompFeat) // iteratively process composite features
281 isOk = process(aCompFeat) && isOk;
283 dumpFeature(aFeature, true);
286 bool isDumpSetName = !myEntitiesStack.empty() &&
287 myEntitiesStack.top().myEntity == EntityPtr(theComposite);
288 bool isForceModelDo = isSubDumped && isDumpSetName &&
289 (myEntitiesStack.top().myUserName || !myEntitiesStack.top().myResults.empty());
290 // It is necessary for the sketch to create its result when complete (command "model.do()").
291 // This option is set by flat theDumpModelDo.
292 // However, nested sketches are rebuilt by parent feature, so, they do not need
293 // explicit call of "model.do()". This will be controlled by the depth of the stack.
294 if (isForceModelDo || (theDumpModelDo && gCompositeStackDepth <= 1))
295 *this << "model.do()" << std::endl;
297 // dump "setName" for composite feature
303 void ModelHighAPI_Dumper::dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
304 const FeaturePtr& theSubFeature)
306 name(theSubFeature, false);
307 myNames[theSubFeature] = EntityName(theSubFeatureGet, theSubFeature->name(), false);
309 // store results if they have user-defined names or colors
310 std::list<ResultPtr> aResultsWithNameOrColor;
311 const std::list<ResultPtr>& aResults = theSubFeature->results();
312 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
313 for (; aResIt != aResults.end(); ++aResIt) {
314 std::string aResName = (*aResIt)->data()->name();
315 myNames[*aResIt] = EntityName(aResName, aResName, false);
316 aResultsWithNameOrColor.push_back(*aResIt);
319 // store just dumped entity to stack
320 myEntitiesStack.push(LastDumpedEntity(theSubFeature, true, aResultsWithNameOrColor));
325 bool ModelHighAPI_Dumper::exportTo(const std::string& theFileName)
328 OSD_OpenStream(aFile, theFileName.c_str(), std::ofstream::out);
329 if (!aFile.is_open())
333 for (ModulesMap::const_iterator aModIt = myModules.begin();
334 aModIt != myModules.end(); ++aModIt) {
335 aFile << "from " << aModIt->first << " import ";
336 if (aModIt->second.empty() ||
337 aModIt->second.find(std::string()) != aModIt->second.end())
338 aFile << "*"; // import whole module
340 // import specific features
341 std::set<std::string>::const_iterator anObjIt = aModIt->second.begin();
343 for (++anObjIt; anObjIt != aModIt->second.end(); ++anObjIt)
344 aFile << ", " << *anObjIt;
348 if (!myModules.empty())
351 aFile << "import model" << std::endl << std::endl;
352 aFile << "model.begin()" << std::endl;
354 // dump collected data
355 aFile << myFullDump.str();
356 aFile << myDumpBuffer.str();
359 aFile << "model.end()" << std::endl;
367 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName,
368 const std::string& theObject)
370 myModules[theModuleName].insert(theObject);
373 void ModelHighAPI_Dumper::dumpEntitySetName()
375 const LastDumpedEntity& aLastDumped = myEntitiesStack.top();
377 // dump "setName" for the entity
378 if (aLastDumped.myUserName) {
379 EntityName& anEntityNames = myNames[aLastDumped.myEntity];
380 if (!anEntityNames.myIsDefault)
381 myDumpBuffer << anEntityNames.myCurrentName << ".setName(\""
382 << anEntityNames.myUserName << "\")" << std::endl;
383 // don't dump "setName" for the entity twice
384 anEntityNames.myUserName.clear();
385 anEntityNames.myIsDefault = true;
387 // dump "setName" for results
388 std::list<ResultPtr>::const_iterator aResIt = aLastDumped.myResults.begin();
389 std::list<ResultPtr>::const_iterator aResEnd = aLastDumped.myResults.end();
390 for (; aResIt != aResEnd; ++aResIt) {
392 EntityName& anEntityNames = myNames[*aResIt];
393 if (!anEntityNames.myIsDefault) {
395 myDumpBuffer << ".setName(\"" << anEntityNames.myUserName << "\")" << std::endl;
396 // don't dump "setName" for the entity twice
397 anEntityNames.myUserName.clear();
398 anEntityNames.myIsDefault = true;
401 if (!isDefaultColor(*aResIt)) {
402 AttributeIntArrayPtr aColor = (*aResIt)->data()->intArray(ModelAPI_Result::COLOR_ID());
403 if (aColor && aColor->isInitialized()) {
405 myDumpBuffer << ".setColor(" << aColor->value(0) << ", " << aColor->value(1)
406 << ", " << aColor->value(2) << ")" << std::endl;
409 // set result deflection
410 if (!isDefaultDeflection(*aResIt)) {
411 AttributeDoublePtr aDeflectionAttr = (*aResIt)->data()->real(ModelAPI_Result::DEFLECTION_ID());
412 if(aDeflectionAttr.get() && aDeflectionAttr->isInitialized()) {
414 myDumpBuffer << ".setDeflection(" << aDeflectionAttr->value() << ")" << std::endl;
419 myEntitiesStack.pop();
422 bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const
424 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
425 return aFound != myNames.end();
428 bool ModelHighAPI_Dumper::isDefaultColor(const ResultPtr& theResult) const
430 AttributeIntArrayPtr aColor = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
431 if (!aColor || !aColor->isInitialized())
434 std::string aSection, aName, aDefault;
435 theResult->colorConfigInfo(aSection, aName, aDefault);
437 // dump current color
438 std::ostringstream aColorInfo;
439 aColorInfo << aColor->value(0) << "," << aColor->value(1) << "," << aColor->value(2);
441 return aDefault == aColorInfo.str();
444 bool ModelHighAPI_Dumper::isDefaultDeflection(const ResultPtr& theResult) const
446 AttributeDoublePtr aDeflectionAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
447 if(!aDeflectionAttr || !aDeflectionAttr->isInitialized()) {
451 double aCurrent = aDeflectionAttr->value();
452 double aDefault = -1;
454 bool isConstruction = false;
455 std::string aResultGroup = theResult->groupName();
456 if (aResultGroup == ModelAPI_ResultConstruction::group())
457 isConstruction = true;
458 else if (aResultGroup == ModelAPI_ResultBody::group()) {
459 GeomShapePtr aGeomShape = theResult->shape();
460 if (aGeomShape.get()) {
461 // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
462 // correction of deviation for them should not influence to the application performance
463 GeomAPI_ShapeExplorer anExp(aGeomShape, GeomAPI_Shape::FACE);
464 isConstruction = !anExp.more();
468 aDefault = Config_PropManager::real("Visualization", "construction_deflection",
469 ModelAPI_ResultConstruction::DEFAULT_DEFLECTION());
471 aDefault = Config_PropManager::real("Visualization", "body_deflection",
472 ModelAPI_ResultBody::DEFAULT_DEFLECTION());
474 return fabs(aCurrent - aDefault) < 1.e-12;
477 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
479 myDumpBuffer << theChar;
483 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
485 myDumpBuffer << theString;
489 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
491 myDumpBuffer << theString;
495 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const bool theValue)
497 myDumpBuffer << (theValue ? "True" : "False");
501 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
503 myDumpBuffer << theValue;
507 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
509 myDumpBuffer << theValue;
513 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
515 importModule("GeomAPI", "GeomAPI_Pnt");
516 myDumpBuffer << "GeomAPI_Pnt(" << thePoint->x() << ", "
517 << thePoint->y() << ", " << thePoint->z() << ")";
521 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
523 importModule("GeomAPI", "GeomAPI_Dir");
524 myDumpBuffer << "GeomAPI_Dir(" << theDir->x() << ", "
525 << theDir->y() << ", " << theDir->z() << ")";
529 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
530 const std::shared_ptr<GeomDataAPI_Dir>& theDir)
532 myDumpBuffer << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
536 static void dumpArray(std::ostringstream& theOutput, int theSize,
537 double* theValues, std::string* theTexts)
539 for (int i = 0; i < theSize; ++i) {
542 if (theTexts[i].empty())
543 theOutput << theValues[i];
545 theOutput << "\"" << theTexts[i] << "\"";
549 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
550 const std::shared_ptr<GeomDataAPI_Point>& thePoint)
552 static const int aSize = 3;
553 double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
554 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()};
555 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
559 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
560 const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
562 static const int aSize = 2;
563 double aValues[aSize] = {thePoint->x(), thePoint->y()};
564 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()};
565 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
569 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
570 const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
572 myDumpBuffer << (theAttrBool->value() ? "True" : "False");
576 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
577 const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
579 std::string aText = theAttrInt->text();
581 myDumpBuffer << theAttrInt->value();
583 myDumpBuffer << "\"" << aText << "\"";
587 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
588 const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
590 std::string aText = theAttrReal->text();
592 myDumpBuffer << theAttrReal->value();
594 myDumpBuffer << "\"" << aText << "\"";
598 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
599 const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
601 myDumpBuffer << "\"" << theAttrStr->value() << "\"";
605 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity)
607 myDumpBuffer << name(theEntity);
609 bool isUserDefinedName = !myNames[theEntity].myIsDefault;
610 // store results if they have user-defined names or colors
611 std::list<ResultPtr> aResultsWithNameOrColor;
612 const std::list<ResultPtr>& aResults = theEntity->results();
613 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
614 for (; aResIt != aResults.end(); ++aResIt)
615 if (!myNames[*aResIt].myIsDefault || !isDefaultColor(*aResIt) || !isDefaultDeflection(*aResIt))
616 aResultsWithNameOrColor.push_back(*aResIt);
617 // store just dumped entity to stack
618 myEntitiesStack.push(LastDumpedEntity(theEntity, isUserDefinedName, aResultsWithNameOrColor));
620 // remove entity from the list of not dumped items
621 myNotDumpedEntities.erase(theEntity);
625 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ResultPtr& theResult)
627 FeaturePtr aFeature = ModelAPI_Feature::feature(theResult);
629 std::list<ResultPtr> aResults = aFeature->results();
630 for(std::list<ResultPtr>::const_iterator anIt = aResults.cbegin(); anIt != aResults.cend(); ++anIt, ++anIndex) {
631 if(theResult->isSame(*anIt)) {
635 myDumpBuffer << name(aFeature) << ".result()[" << anIndex << "]";
639 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ObjectPtr& theObject)
641 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
643 myDumpBuffer << name(aFeature);
647 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
656 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const AttributePtr& theAttr)
658 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttr->owner());
660 std::string aWrapperPrefix, aWrapperSuffix;
661 // Check the attribute belongs to copied (in multi-translation or multi-rotation) feature.
662 // In this case we need to cast explicitly feature to appropriate type.
663 AttributeBooleanPtr isCopy = anOwner->boolean("Copy");
664 if (isCopy.get() && isCopy->value()) {
665 aWrapperPrefix = featureWrapper(anOwner) + "(";
666 aWrapperSuffix = ")";
667 importModule("SketchAPI");
670 myDumpBuffer << aWrapperPrefix << name(anOwner) << aWrapperSuffix
671 << "." << attributeGetter(anOwner, theAttr->id()) << "()";
675 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
676 const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
678 if (theRefAttr->isObject())
679 *this << theRefAttr->object();
681 *this << theRefAttr->attr();
685 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
686 const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList)
689 std::list<std::pair<ObjectPtr, AttributePtr> > aList = theRefAttrList->list();
690 bool isAdded = false;
691 std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aList.begin();
692 for (; anIt != aList.end(); ++anIt) {
694 myDumpBuffer << ", ";
698 *this << anIt->first;
699 else if (anIt->second)
700 * this << anIt->second;
706 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
707 const std::shared_ptr<ModelAPI_AttributeReference>& theReference)
709 *this << theReference->value();
713 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
714 const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList)
716 static const int aThreshold = 2;
717 // if number of elements in the list if greater than a threshold,
718 // dump it in a separate line with specific name
719 std::string aDumped = myDumpBuffer.str();
720 if (aDumped.empty() || theRefList->size() <= aThreshold) {
722 std::list<ObjectPtr> aList = theRefList->list();
723 bool isAdded = false;
724 std::list<ObjectPtr>::const_iterator anIt = aList.begin();
725 for (; anIt != aList.end(); ++anIt) {
727 myDumpBuffer << ", ";
735 // clear buffer and store list "as is"
736 myDumpBuffer.str("");
738 // save buffer and clear it again
739 std::string aDumpedList = myDumpBuffer.str();
740 myDumpBuffer.str("");
741 // obtain name of list
742 FeaturePtr anOwner = ModelAPI_Feature::feature(theRefList->owner());
743 std::string aListName = name(anOwner) + "_objects";
744 // store all previous data
745 myDumpBuffer << aListName << " = " << aDumpedList << std::endl
746 << aDumped << aListName;
751 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
752 const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
754 myDumpBuffer << "model.selection(";
756 if(!theAttrSelect->isInitialized()) {
761 GeomShapePtr aShape = theAttrSelect->value();
763 aShape = theAttrSelect->context()->shape();
771 myDumpBuffer << "\"" << aShape->shapeTypeStr() << "\", \"" << theAttrSelect->namingName() << "\")";
775 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
776 const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList)
781 std::string aShapeTypeStr;
783 bool isAdded = false;
785 for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) {
786 AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex);
787 aShape = anAttribute->value();
789 aShape = anAttribute->context()->shape();
797 myDumpBuffer << ", ";
801 myDumpBuffer << "model.selection(\"" << aShape->shapeTypeStr() << "\", \"" << anAttribute->namingName() << "\")";
810 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
811 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
813 theDumper.myDumpBuffer << theEndl;
815 if (!theDumper.myEntitiesStack.empty()) {
816 // Name for composite feature is dumped when all sub-entities are dumped
817 // (see method ModelHighAPI_Dumper::processSubs).
818 const ModelHighAPI_Dumper::LastDumpedEntity& aLastDumped = theDumper.myEntitiesStack.top();
819 CompositeFeaturePtr aComposite =
820 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aLastDumped.myEntity);
822 theDumper.dumpEntitySetName();
825 // store all not-dumped entities first
826 std::set<EntityPtr> aNotDumped = theDumper.myNotDumpedEntities;
827 std::string aBufCopy = theDumper.myDumpBuffer.str();
828 theDumper.clear(true);
829 std::set<EntityPtr>::const_iterator anIt = aNotDumped.begin();
830 for (; anIt != aNotDumped.end(); ++anIt) {
831 // if the feature is composite, dump it with all subs
832 CompositeFeaturePtr aCompFeat =
833 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
835 theDumper.process(aCompFeat, true);
837 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
838 theDumper.dumpFeature(aFeature, true);
842 // avoid multiple empty lines
843 size_t anInd = std::string::npos;
844 while ((anInd = aBufCopy.find("\n\n\n")) != std::string::npos)
845 aBufCopy.erase(anInd, 1);
846 // then store currently dumped string
847 theDumper.myFullDump << aBufCopy;