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_AttributeStringArray.h>
32 #include <ModelAPI_CompositeFeature.h>
33 #include <ModelAPI_Document.h>
34 #include <ModelAPI_Entity.h>
35 #include <ModelAPI_Feature.h>
36 #include <ModelAPI_Result.h>
37 #include <ModelAPI_ResultBody.h>
38 #include <ModelAPI_ResultConstruction.h>
39 #include <ModelAPI_ResultPart.h>
41 #include <PartSetPlugin_Part.h>
43 #include <OSD_OpenFile.hxx>
47 static int gCompositeStackDepth = 0;
49 ModelHighAPI_Dumper* ModelHighAPI_Dumper::mySelf = 0;
51 ModelHighAPI_Dumper::ModelHighAPI_Dumper()
56 void ModelHighAPI_Dumper::setInstance(ModelHighAPI_Dumper* theDumper)
62 ModelHighAPI_Dumper* ModelHighAPI_Dumper::getInstance()
67 void ModelHighAPI_Dumper::clear(bool bufferOnly)
70 myDumpBuffer << std::setprecision(16);
76 myFullDump << std::setprecision(16);
80 myFeatureCount.clear();
81 while (!myEntitiesStack.empty())
82 myEntitiesStack.pop();
86 void ModelHighAPI_Dumper::clearNotDumped()
88 myNotDumpedEntities.clear();
91 // Convert string to integer. If the string is not a number, return -1
92 static int toInt(const std::string& theString)
94 std::string::const_iterator aChar = theString.begin();
95 for (; aChar != theString.end(); ++aChar)
96 if (!std::isdigit(*aChar))
98 if (aChar != theString.end())
99 return -1; // not a number
100 return std::stoi(theString);
103 const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity,
104 bool theSaveNotDumped,
105 bool theUseEntityName)
107 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
108 if (aFound != myNames.end())
109 return aFound->second.myCurrentName;
111 // entity is not found, store it
113 bool isDefaultName = false;
114 std::ostringstream aDefaultName;
115 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
117 aName = aFeature->name();
118 const std::string& aKind = aFeature->getKind();
119 DocumentPtr aDoc = aFeature->document();
120 int& aNbFeatures = myFeatureCount[aDoc][aKind];
123 size_t anIndex = aName.find(aKind);
124 if (anIndex == 0 && aName[aKind.length()] == '_') { // name starts with "FeatureKind_"
125 std::string anIdStr = aName.substr(aKind.length() + 1);
126 int anId = toInt(anIdStr);
128 // Check number of already registered objects of such kind. Index of current object
129 // should be the same to identify feature's name as automatically generated.
130 if (aNbFeatures == anId) {
131 // name is not user-defined
132 isDefaultName = true;
136 // obtain default name for the feature
137 if (theUseEntityName)
138 aDefaultName << aName;
141 NbFeaturesMap::const_iterator aFIt = myFeatureCount.begin();
142 for (; aFIt != myFeatureCount.end(); ++aFIt) {
143 std::map<std::string, int>::const_iterator aFound = aFIt->second.find(aKind);
144 if (aFound != aFIt->second.end())
145 aFullIndex += aFound->second;
147 aDefaultName << aKind << "_" << aFullIndex;
151 myNames[theEntity] = EntityName(aDefaultName.str(), aName, isDefaultName);
152 if (theSaveNotDumped)
153 myNotDumpedEntities.insert(theEntity);
155 // store names of results
157 saveResultNames(aFeature);
159 return myNames[theEntity].myCurrentName;
162 const std::string& ModelHighAPI_Dumper::parentName(const FeaturePtr& theEntity)
164 const std::set<AttributePtr>& aRefs = theEntity->data()->refsToMe();
165 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
166 for (; aRefIt != aRefs.end(); ++aRefIt) {
167 CompositeFeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(
168 ModelAPI_Feature::feature((*aRefIt)->owner()));
170 return name(anOwner);
173 static const std::string DUMMY;
177 void ModelHighAPI_Dumper::saveResultNames(const FeaturePtr& theFeature)
179 // Default name of the feature
180 const std::string& aKind = theFeature->getKind();
181 DocumentPtr aDoc = theFeature->document();
182 int aNbFeatures = myFeatureCount[aDoc][aKind];
183 std::ostringstream aNameStream;
184 aNameStream << aKind << "_" << aNbFeatures;
185 std::string aFeatureName = aNameStream.str();
187 // Save only names of results which is not correspond to default feature name
188 const std::list<ResultPtr>& aResults = theFeature->results();
189 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
190 for (int i = 1; aResIt != aResults.end(); ++aResIt, ++i) {
191 bool isUserDefined = true;
192 std::string aResName = (*aResIt)->data()->name();
193 size_t anIndex = aResName.find(aFeatureName);
195 std::string aSuffix = aResName.substr(aFeatureName.length());
196 if (aSuffix.empty() && i == 1) // first result may not constain index in the name
197 isUserDefined = false;
199 if (aSuffix[0] == '_' && std::stoi(aSuffix.substr(1)) == i)
200 isUserDefined = false;
204 myNames[*aResIt] = EntityName(aResName,
205 (isUserDefined ? aResName : std::string()), !isUserDefined);
209 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc,
210 const std::string& theFileName)
212 // dump top level document feature
213 static const std::string aDocName("partSet");
214 myNames[theDoc] = EntityName(aDocName, std::string(), true);
215 *this << aDocName << " = model.moduleDocument()" << std::endl;
217 // dump subfeatures and store result to file
218 return process(theDoc) && exportTo(theFileName);
221 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc)
224 std::list<FeaturePtr> aFeatures = theDoc->allFeatures();
225 std::list<FeaturePtr>::const_iterator aFeatIt = aFeatures.begin();
226 // firstly, dump all parameters
227 for (; aFeatIt != aFeatures.end(); ++ aFeatIt)
228 dumpParameter(*aFeatIt);
229 // dump all other features
230 for (aFeatIt = aFeatures.begin(); aFeatIt != aFeatures.end(); ++aFeatIt) {
231 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFeatIt);
232 if (aCompFeat) // iteratively process composite features
233 isOk = process(aCompFeat) && isOk;
234 else if (!isDumped(*aFeatIt)) // dump common feature
235 dumpFeature(*aFeatIt);
240 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
243 // increase composite features stack
244 ++gCompositeStackDepth;
245 // dump composite itself
246 if (!isDumped(theComposite) || isForce)
247 dumpFeature(FeaturePtr(theComposite), isForce);
249 // sub-part is processed independently, because it provides separate document
250 if (theComposite->getKind() == PartSetPlugin_Part::ID()) {
251 // dump name of the part if it is different from default
252 if (!myEntitiesStack.empty())
255 // decrease composite features stack because we run into separate document
256 --gCompositeStackDepth;
258 ResultPartPtr aPartResult =
259 std::dynamic_pointer_cast<ModelAPI_ResultPart>(theComposite->lastResult());
262 DocumentPtr aSubDoc = aPartResult->partDoc();
265 // set name of document
266 const std::string& aPartName = myNames[theComposite].myCurrentName;
267 std::string aDocName = aPartName + "_doc";
268 myNames[aSubDoc] = EntityName(aDocName, std::string(), true);
270 // dump document in a separate line
271 *this << aDocName << " = " << aPartName << ".document()" << std::endl;
272 // dump features in the document
273 return process(aSubDoc);
277 bool isOk = processSubs(theComposite);
278 // decrease composite features stack
279 --gCompositeStackDepth;
284 bool ModelHighAPI_Dumper::processSubs(
285 const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
289 // dump all sub-features;
290 bool isSubDumped = false;
291 int aNbSubs = theComposite->numberOfSubs();
292 for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) {
293 FeaturePtr aFeature = theComposite->subFeature(anIndex);
294 if (isDumped(aFeature))
298 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
299 if (aCompFeat) // iteratively process composite features
300 isOk = process(aCompFeat) && isOk;
302 dumpFeature(aFeature, true);
305 bool isDumpSetName = !myEntitiesStack.empty() &&
306 myEntitiesStack.top().myEntity == EntityPtr(theComposite);
307 bool isForceModelDo = isSubDumped && isDumpSetName &&
308 (myEntitiesStack.top().myUserName || !myEntitiesStack.top().myResults.empty());
309 // It is necessary for the sketch to create its result when complete (command "model.do()").
310 // This option is set by flat theDumpModelDo.
311 // However, nested sketches are rebuilt by parent feature, so, they do not need
312 // explicit call of "model.do()". This will be controlled by the depth of the stack.
313 if (isForceModelDo || (theDumpModelDo && gCompositeStackDepth <= 1))
314 *this << "model.do()" << std::endl;
316 // dump "setName" for composite feature
322 void ModelHighAPI_Dumper::dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
323 const FeaturePtr& theSubFeature)
325 name(theSubFeature, false);
326 myNames[theSubFeature] = EntityName(theSubFeatureGet, theSubFeature->name(), false);
328 // store results if they have user-defined names or colors
329 std::list<ResultPtr> aResultsWithNameOrColor;
330 const std::list<ResultPtr>& aResults = theSubFeature->results();
331 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
332 for (; aResIt != aResults.end(); ++aResIt) {
333 std::string aResName = (*aResIt)->data()->name();
334 myNames[*aResIt] = EntityName(aResName, aResName, false);
335 aResultsWithNameOrColor.push_back(*aResIt);
338 // store just dumped entity to stack
339 myEntitiesStack.push(LastDumpedEntity(theSubFeature, true, aResultsWithNameOrColor));
344 bool ModelHighAPI_Dumper::exportTo(const std::string& theFileName)
347 OSD_OpenStream(aFile, theFileName.c_str(), std::ofstream::out);
348 if (!aFile.is_open())
352 for (ModulesMap::const_iterator aModIt = myModules.begin();
353 aModIt != myModules.end(); ++aModIt) {
354 aFile << "from " << aModIt->first << " import ";
355 if (aModIt->second.empty() ||
356 aModIt->second.find(std::string()) != aModIt->second.end())
357 aFile << "*"; // import whole module
359 // import specific features
360 std::set<std::string>::const_iterator anObjIt = aModIt->second.begin();
362 for (++anObjIt; anObjIt != aModIt->second.end(); ++anObjIt)
363 aFile << ", " << *anObjIt;
367 if (!myModules.empty())
370 aFile << "import model" << std::endl << std::endl;
371 aFile << "model.begin()" << std::endl;
373 // dump collected data
374 aFile << myFullDump.str();
375 aFile << myDumpBuffer.str();
378 aFile << "model.end()" << std::endl;
386 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName,
387 const std::string& theObject)
389 myModules[theModuleName].insert(theObject);
392 void ModelHighAPI_Dumper::dumpEntitySetName()
394 const LastDumpedEntity& aLastDumped = myEntitiesStack.top();
396 // dump "setName" for the entity
397 if (aLastDumped.myUserName) {
398 EntityName& anEntityNames = myNames[aLastDumped.myEntity];
399 if (!anEntityNames.myIsDefault)
400 myDumpBuffer << anEntityNames.myCurrentName << ".setName(\""
401 << anEntityNames.myUserName << "\")" << std::endl;
402 // don't dump "setName" for the entity twice
403 anEntityNames.myUserName.clear();
404 anEntityNames.myIsDefault = true;
406 // dump "setName" for results
407 std::list<ResultPtr>::const_iterator aResIt = aLastDumped.myResults.begin();
408 std::list<ResultPtr>::const_iterator aResEnd = aLastDumped.myResults.end();
409 for (; aResIt != aResEnd; ++aResIt) {
411 EntityName& anEntityNames = myNames[*aResIt];
412 if (!anEntityNames.myIsDefault) {
414 myDumpBuffer << ".setName(\"" << anEntityNames.myUserName << "\")" << std::endl;
415 // don't dump "setName" for the entity twice
416 anEntityNames.myUserName.clear();
417 anEntityNames.myIsDefault = true;
420 if (!isDefaultColor(*aResIt)) {
421 AttributeIntArrayPtr aColor = (*aResIt)->data()->intArray(ModelAPI_Result::COLOR_ID());
422 if (aColor && aColor->isInitialized()) {
424 myDumpBuffer << ".setColor(" << aColor->value(0) << ", " << aColor->value(1)
425 << ", " << aColor->value(2) << ")" << std::endl;
428 // set result deflection
429 if (!isDefaultDeflection(*aResIt)) {
430 AttributeDoublePtr aDeflectionAttr =
431 (*aResIt)->data()->real(ModelAPI_Result::DEFLECTION_ID());
432 if(aDeflectionAttr.get() && aDeflectionAttr->isInitialized()) {
434 myDumpBuffer << ".setDeflection(" << aDeflectionAttr->value() << ")" << std::endl;
439 myEntitiesStack.pop();
442 bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const
444 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
445 return aFound != myNames.end();
448 bool ModelHighAPI_Dumper::isDefaultColor(const ResultPtr& theResult) const
450 AttributeIntArrayPtr aColor = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
451 if (!aColor || !aColor->isInitialized())
454 std::string aSection, aName, aDefault;
455 theResult->colorConfigInfo(aSection, aName, aDefault);
457 // dump current color
458 std::ostringstream aColorInfo;
459 aColorInfo << aColor->value(0) << "," << aColor->value(1) << "," << aColor->value(2);
461 return aDefault == aColorInfo.str();
464 bool ModelHighAPI_Dumper::isDefaultDeflection(const ResultPtr& theResult) const
466 AttributeDoublePtr aDeflectionAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
467 if(!aDeflectionAttr || !aDeflectionAttr->isInitialized()) {
471 double aCurrent = aDeflectionAttr->value();
472 double aDefault = -1;
474 bool isConstruction = false;
475 std::string aResultGroup = theResult->groupName();
476 if (aResultGroup == ModelAPI_ResultConstruction::group())
477 isConstruction = true;
478 else if (aResultGroup == ModelAPI_ResultBody::group()) {
479 GeomShapePtr aGeomShape = theResult->shape();
480 if (aGeomShape.get()) {
481 // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
482 // correction of deviation for them should not influence to the application performance
483 GeomAPI_ShapeExplorer anExp(aGeomShape, GeomAPI_Shape::FACE);
484 isConstruction = !anExp.more();
488 aDefault = Config_PropManager::real("Visualization", "construction_deflection",
489 ModelAPI_ResultConstruction::DEFAULT_DEFLECTION());
491 aDefault = Config_PropManager::real("Visualization", "body_deflection",
492 ModelAPI_ResultBody::DEFAULT_DEFLECTION());
494 return fabs(aCurrent - aDefault) < 1.e-12;
497 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
499 myDumpBuffer << theChar;
503 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
505 myDumpBuffer << theString;
509 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
511 myDumpBuffer << theString;
515 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const bool theValue)
517 myDumpBuffer << (theValue ? "True" : "False");
521 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
523 myDumpBuffer << theValue;
527 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
529 myDumpBuffer << theValue;
533 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
535 importModule("GeomAPI", "GeomAPI_Pnt");
536 myDumpBuffer << "GeomAPI_Pnt(" << thePoint->x() << ", "
537 << thePoint->y() << ", " << thePoint->z() << ")";
541 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
543 importModule("GeomAPI", "GeomAPI_Dir");
544 myDumpBuffer << "GeomAPI_Dir(" << theDir->x() << ", "
545 << theDir->y() << ", " << theDir->z() << ")";
549 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
550 const std::shared_ptr<GeomDataAPI_Dir>& theDir)
552 myDumpBuffer << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
556 static void dumpArray(std::ostringstream& theOutput, int theSize,
557 double* theValues, std::string* theTexts)
559 for (int i = 0; i < theSize; ++i) {
562 if (theTexts[i].empty())
563 theOutput << theValues[i];
565 theOutput << "\"" << theTexts[i] << "\"";
569 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
570 const std::shared_ptr<GeomDataAPI_Point>& thePoint)
572 static const int aSize = 3;
573 double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
574 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()};
575 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
579 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
580 const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
582 static const int aSize = 2;
583 double aValues[aSize] = {thePoint->x(), thePoint->y()};
584 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()};
585 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
589 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
590 const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
592 myDumpBuffer << (theAttrBool->value() ? "True" : "False");
596 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
597 const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
599 std::string aText = theAttrInt->text();
601 myDumpBuffer << theAttrInt->value();
603 myDumpBuffer << "\"" << aText << "\"";
607 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
608 const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
610 std::string aText = theAttrReal->text();
612 myDumpBuffer << theAttrReal->value();
614 myDumpBuffer << "\"" << aText << "\"";
618 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
619 const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
621 myDumpBuffer << "\"" << theAttrStr->value() << "\"";
625 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity)
627 myDumpBuffer << name(theEntity);
629 bool isUserDefinedName = !myNames[theEntity].myIsDefault;
630 // store results if they have user-defined names or colors
631 std::list<ResultPtr> aResultsWithNameOrColor;
632 const std::list<ResultPtr>& aResults = theEntity->results();
633 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
634 for (; aResIt != aResults.end(); ++aResIt)
635 if (!myNames[*aResIt].myIsDefault || !isDefaultColor(*aResIt) || !isDefaultDeflection(*aResIt))
636 aResultsWithNameOrColor.push_back(*aResIt);
637 // store just dumped entity to stack
638 myEntitiesStack.push(LastDumpedEntity(theEntity, isUserDefinedName, aResultsWithNameOrColor));
640 // remove entity from the list of not dumped items
641 myNotDumpedEntities.erase(theEntity);
645 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ResultPtr& theResult)
647 FeaturePtr aFeature = ModelAPI_Feature::feature(theResult);
649 std::list<ResultPtr> aResults = aFeature->results();
650 for(std::list<ResultPtr>::const_iterator
651 anIt = aResults.cbegin(); anIt != aResults.cend(); ++anIt, ++anIndex) {
652 if(theResult->isSame(*anIt)) {
657 myDumpBuffer << name(aFeature);
659 myDumpBuffer << ".result()";
661 myDumpBuffer << ".results()[" << anIndex << "]";
666 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ObjectPtr& theObject)
668 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
670 myDumpBuffer << name(aFeature);
674 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
683 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const AttributePtr& theAttr)
685 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttr->owner());
687 std::string aWrapperPrefix, aWrapperSuffix;
688 // Check the attribute belongs to copied (in multi-translation or multi-rotation) feature.
689 // In this case we need to cast explicitly feature to appropriate type.
690 AttributeBooleanPtr isCopy = anOwner->boolean("Copy");
691 if (isCopy.get() && isCopy->value()) {
692 aWrapperPrefix = featureWrapper(anOwner) + "(";
693 aWrapperSuffix = ")";
694 importModule("SketchAPI");
697 myDumpBuffer << aWrapperPrefix << name(anOwner) << aWrapperSuffix
698 << "." << attributeGetter(anOwner, theAttr->id()) << "()";
702 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
703 const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
705 if (theRefAttr->isObject())
706 *this << theRefAttr->object();
708 *this << theRefAttr->attr();
712 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
713 const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList)
716 std::list<std::pair<ObjectPtr, AttributePtr> > aList = theRefAttrList->list();
717 bool isAdded = false;
718 std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aList.begin();
719 for (; anIt != aList.end(); ++anIt) {
721 myDumpBuffer << ", ";
725 *this << anIt->first;
726 else if (anIt->second)
727 * this << anIt->second;
733 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
734 const std::shared_ptr<ModelAPI_AttributeReference>& theReference)
736 *this << theReference->value();
740 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
741 const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList)
743 static const int aThreshold = 2;
744 // if number of elements in the list if greater than a threshold,
745 // dump it in a separate line with specific name
746 std::string aDumped = myDumpBuffer.str();
747 if (aDumped.empty() || theRefList->size() <= aThreshold) {
749 std::list<ObjectPtr> aList = theRefList->list();
750 bool isAdded = false;
751 std::list<ObjectPtr>::const_iterator anIt = aList.begin();
752 for (; anIt != aList.end(); ++anIt) {
754 myDumpBuffer << ", ";
762 // clear buffer and store list "as is"
763 myDumpBuffer.str("");
765 // save buffer and clear it again
766 std::string aDumpedList = myDumpBuffer.str();
767 myDumpBuffer.str("");
768 // obtain name of list
769 FeaturePtr anOwner = ModelAPI_Feature::feature(theRefList->owner());
770 std::string aListName = name(anOwner) + "_objects";
771 // store all previous data
772 myDumpBuffer << aListName << " = " << aDumpedList << std::endl
773 << aDumped << aListName;
778 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
779 const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
781 myDumpBuffer << "model.selection(";
783 if(!theAttrSelect->isInitialized()) {
788 GeomShapePtr aShape = theAttrSelect->value();
790 aShape = theAttrSelect->context()->shape();
798 myDumpBuffer << "\"" << aShape->shapeTypeStr() << "\", \"" <<
799 theAttrSelect->namingName() << "\")";
803 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
804 const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList)
809 std::string aShapeTypeStr;
811 bool isAdded = false;
813 for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) {
814 AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex);
815 aShape = anAttribute->value();
817 aShape = anAttribute->context()->shape();
825 myDumpBuffer << ", ";
829 myDumpBuffer << "model.selection(\"" <<
830 aShape->shapeTypeStr() << "\", \"" << anAttribute->namingName() << "\")";
837 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
838 const std::shared_ptr<ModelAPI_AttributeStringArray>& theArray)
841 for(int anIndex = 0; anIndex < theArray->size(); ++anIndex) {
845 myDumpBuffer<<"\""<<theArray->value(anIndex)<<"\"";
853 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
854 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
856 theDumper.myDumpBuffer << theEndl;
858 if (!theDumper.myEntitiesStack.empty()) {
859 // Name for composite feature is dumped when all sub-entities are dumped
860 // (see method ModelHighAPI_Dumper::processSubs).
861 const ModelHighAPI_Dumper::LastDumpedEntity& aLastDumped = theDumper.myEntitiesStack.top();
862 CompositeFeaturePtr aComposite =
863 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aLastDumped.myEntity);
865 theDumper.dumpEntitySetName();
868 // store all not-dumped entities first
869 std::set<EntityPtr> aNotDumped = theDumper.myNotDumpedEntities;
870 std::string aBufCopy = theDumper.myDumpBuffer.str();
871 theDumper.clear(true);
872 std::set<EntityPtr>::const_iterator anIt = aNotDumped.begin();
873 for (; anIt != aNotDumped.end(); ++anIt) {
874 // if the feature is composite, dump it with all subs
875 CompositeFeaturePtr aCompFeat =
876 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
878 theDumper.process(aCompFeat, true);
880 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
881 theDumper.dumpFeature(aFeature, true);
885 // avoid multiple empty lines
886 size_t anInd = std::string::npos;
887 while ((anInd = aBufCopy.find("\n\n\n")) != std::string::npos)
888 aBufCopy.erase(anInd, 1);
889 // then store currently dumped string
890 theDumper.myFullDump << aBufCopy;