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_ResultCompSolid.h>
39 #include <ModelAPI_ResultConstruction.h>
40 #include <ModelAPI_ResultPart.h>
42 #include <PartSetPlugin_Part.h>
44 #include <OSD_OpenFile.hxx>
48 static int gCompositeStackDepth = 0;
50 ModelHighAPI_Dumper* ModelHighAPI_Dumper::mySelf = 0;
52 ModelHighAPI_Dumper::ModelHighAPI_Dumper()
57 void ModelHighAPI_Dumper::setInstance(ModelHighAPI_Dumper* theDumper)
63 ModelHighAPI_Dumper* ModelHighAPI_Dumper::getInstance()
68 void ModelHighAPI_Dumper::clear(bool bufferOnly)
71 myDumpBuffer << std::setprecision(16);
77 myFullDump << std::setprecision(16);
81 myFeatureCount.clear();
82 while (!myEntitiesStack.empty())
83 myEntitiesStack.pop();
87 void ModelHighAPI_Dumper::clearNotDumped()
89 myNotDumpedEntities.clear();
92 // Convert string to integer. If the string is not a number, return -1
93 static int toInt(const std::string& theString)
95 std::string::const_iterator aChar = theString.begin();
96 for (; aChar != theString.end(); ++aChar)
97 if (!std::isdigit(*aChar))
99 if (aChar != theString.end())
100 return -1; // not a number
101 return std::stoi(theString);
104 const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity,
105 bool theSaveNotDumped,
106 bool theUseEntityName)
108 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
109 if (aFound != myNames.end())
110 return aFound->second.myCurrentName;
112 // entity is not found, store it
114 bool isDefaultName = false;
115 std::ostringstream aDefaultName;
116 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
118 aName = aFeature->name();
119 const std::string& aKind = aFeature->getKind();
120 DocumentPtr aDoc = aFeature->document();
121 int& aNbFeatures = myFeatureCount[aDoc][aKind];
124 size_t anIndex = aName.find(aKind);
125 if (anIndex == 0 && aName[aKind.length()] == '_') { // name starts with "FeatureKind_"
126 std::string anIdStr = aName.substr(aKind.length() + 1);
127 int anId = toInt(anIdStr);
129 // Check number of already registered objects of such kind. Index of current object
130 // should be the same to identify feature's name as automatically generated.
131 if (aNbFeatures == anId) {
132 // name is not user-defined
133 isDefaultName = true;
137 // obtain default name for the feature
138 if (theUseEntityName)
139 aDefaultName << aName;
142 NbFeaturesMap::const_iterator aFIt = myFeatureCount.begin();
143 for (; aFIt != myFeatureCount.end(); ++aFIt) {
144 std::map<std::string, int>::const_iterator aFound = aFIt->second.find(aKind);
145 if (aFound != aFIt->second.end())
146 aFullIndex += aFound->second;
148 aDefaultName << aKind << "_" << aFullIndex;
152 myNames[theEntity] = EntityName(aDefaultName.str(), aName, isDefaultName);
153 if (theSaveNotDumped)
154 myNotDumpedEntities.insert(theEntity);
156 // store names of results
158 saveResultNames(aFeature);
160 return myNames[theEntity].myCurrentName;
163 const std::string& ModelHighAPI_Dumper::parentName(const FeaturePtr& theEntity)
165 const std::set<AttributePtr>& aRefs = theEntity->data()->refsToMe();
166 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
167 for (; aRefIt != aRefs.end(); ++aRefIt) {
168 CompositeFeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(
169 ModelAPI_Feature::feature((*aRefIt)->owner()));
171 return name(anOwner);
174 static const std::string DUMMY;
178 void ModelHighAPI_Dumper::saveResultNames(const FeaturePtr& theFeature)
180 // Default name of the feature
181 const std::string& aKind = theFeature->getKind();
182 DocumentPtr aDoc = theFeature->document();
183 int aNbFeatures = myFeatureCount[aDoc][aKind];
184 std::ostringstream aNameStream;
185 aNameStream << aKind << "_" << aNbFeatures;
186 std::string aFeatureName = aNameStream.str();
188 // Save only names of results which is not correspond to default feature name
189 const std::list<ResultPtr>& aResults = theFeature->results();
190 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
191 for (int i = 1; aResIt != aResults.end(); ++aResIt, ++i) {
192 bool isUserDefined = true;
193 std::string aResName = (*aResIt)->data()->name();
194 size_t anIndex = aResName.find(aFeatureName);
196 std::string aSuffix = aResName.substr(aFeatureName.length());
197 if (aSuffix.empty() && i == 1) // first result may not constain index in the name
198 isUserDefined = false;
200 if (aSuffix[0] == '_' && std::stoi(aSuffix.substr(1)) == i)
201 isUserDefined = false;
205 myNames[*aResIt] = EntityName(aResName,
206 (isUserDefined ? aResName : std::string()), !isUserDefined);
208 // check names of sub-results for CompSolid
209 ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aResIt);
211 int aNbSubs = aCompSolid->numberOfSubs();
212 for (int j = 0; j < aNbSubs; ++j) {
213 isUserDefined = true;
214 ResultPtr aSub = aCompSolid->subResult(j);
215 std::string aSubName = aSub->data()->name();
216 size_t anIndex = aSubName.find(aResName);
218 std::string aSuffix = aSubName.substr(aResName.length());
219 if (aSuffix.empty() && aNbSubs == 1) // first result may not constain index in the name
220 isUserDefined = false;
222 if (aSuffix[0] == '_' && std::stoi(aSuffix.substr(1)) == j + 1)
223 isUserDefined = false;
227 myNames[aSub] = EntityName(aSubName,
228 (isUserDefined ? aSubName : std::string()), !isUserDefined);
234 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc,
235 const std::string& theFileName)
237 // dump top level document feature
238 static const std::string aDocName("partSet");
239 myNames[theDoc] = EntityName(aDocName, std::string(), true);
240 *this << aDocName << " = model.moduleDocument()" << std::endl;
242 // dump subfeatures and store result to file
243 return process(theDoc) && exportTo(theFileName);
246 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc)
249 std::list<FeaturePtr> aFeatures = theDoc->allFeatures();
250 std::list<FeaturePtr>::const_iterator aFeatIt = aFeatures.begin();
251 // firstly, dump all parameters
252 for (; aFeatIt != aFeatures.end(); ++ aFeatIt)
253 dumpParameter(*aFeatIt);
254 // dump all other features
255 for (aFeatIt = aFeatures.begin(); aFeatIt != aFeatures.end(); ++aFeatIt) {
256 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFeatIt);
257 if (aCompFeat) // iteratively process composite features
258 isOk = process(aCompFeat) && isOk;
259 else if (!isDumped(*aFeatIt)) // dump common feature
260 dumpFeature(*aFeatIt);
265 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
268 // increase composite features stack
269 ++gCompositeStackDepth;
270 // dump composite itself
271 if (!isDumped(theComposite) || isForce)
272 dumpFeature(FeaturePtr(theComposite), isForce);
274 // sub-part is processed independently, because it provides separate document
275 if (theComposite->getKind() == PartSetPlugin_Part::ID()) {
276 // dump name of the part if it is different from default
277 if (!myEntitiesStack.empty())
280 // decrease composite features stack because we run into separate document
281 --gCompositeStackDepth;
283 ResultPartPtr aPartResult =
284 std::dynamic_pointer_cast<ModelAPI_ResultPart>(theComposite->lastResult());
287 DocumentPtr aSubDoc = aPartResult->partDoc();
290 // set name of document
291 const std::string& aPartName = myNames[theComposite].myCurrentName;
292 std::string aDocName = aPartName + "_doc";
293 myNames[aSubDoc] = EntityName(aDocName, std::string(), true);
295 // dump document in a separate line
296 *this << aDocName << " = " << aPartName << ".document()" << std::endl;
297 // dump features in the document
298 bool aRes = process(aSubDoc);
299 *this << "model.do()" << std::endl;
304 bool isOk = processSubs(theComposite);
305 // decrease composite features stack
306 --gCompositeStackDepth;
311 bool ModelHighAPI_Dumper::processSubs(
312 const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
316 // dump all sub-features;
317 bool isSubDumped = false;
318 int aNbSubs = theComposite->numberOfSubs();
319 for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) {
320 FeaturePtr aFeature = theComposite->subFeature(anIndex);
321 if (isDumped(aFeature))
325 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
326 if (aCompFeat) // iteratively process composite features
327 isOk = process(aCompFeat) && isOk;
329 dumpFeature(aFeature, true);
332 bool isDumpSetName = !myEntitiesStack.empty() &&
333 myEntitiesStack.top().myEntity == EntityPtr(theComposite);
334 bool isForceModelDo = isSubDumped && isDumpSetName &&
335 (myEntitiesStack.top().myUserName || !myEntitiesStack.top().myResults.empty());
336 // It is necessary for the sketch to create its result when complete (command "model.do()").
337 // This option is set by flat theDumpModelDo.
338 // However, nested sketches are rebuilt by parent feature, so, they do not need
339 // explicit call of "model.do()". This will be controlled by the depth of the stack.
340 if (isForceModelDo || (theDumpModelDo && gCompositeStackDepth <= 1))
341 *this << "model.do()" << std::endl;
343 // dump "setName" for composite feature
349 void ModelHighAPI_Dumper::dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
350 const FeaturePtr& theSubFeature)
352 name(theSubFeature, false);
353 myNames[theSubFeature] = EntityName(theSubFeatureGet, theSubFeature->name(), false);
355 // store results if they have user-defined names or colors
356 std::list<ResultPtr> aResultsWithNameOrColor;
357 const std::list<ResultPtr>& aResults = theSubFeature->results();
358 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
359 for (; aResIt != aResults.end(); ++aResIt) {
360 std::string aResName = (*aResIt)->data()->name();
361 myNames[*aResIt] = EntityName(aResName, aResName, false);
362 aResultsWithNameOrColor.push_back(*aResIt);
365 // store just dumped entity to stack
366 myEntitiesStack.push(LastDumpedEntity(theSubFeature, true, aResultsWithNameOrColor));
371 bool ModelHighAPI_Dumper::exportTo(const std::string& theFileName)
374 OSD_OpenStream(aFile, theFileName.c_str(), std::ofstream::out);
375 if (!aFile.is_open())
379 for (ModulesMap::const_iterator aModIt = myModules.begin();
380 aModIt != myModules.end(); ++aModIt) {
381 aFile << "from " << aModIt->first << " import ";
382 if (aModIt->second.empty() ||
383 aModIt->second.find(std::string()) != aModIt->second.end())
384 aFile << "*"; // import whole module
386 // import specific features
387 std::set<std::string>::const_iterator anObjIt = aModIt->second.begin();
389 for (++anObjIt; anObjIt != aModIt->second.end(); ++anObjIt)
390 aFile << ", " << *anObjIt;
394 if (!myModules.empty())
397 aFile << "from salome.shaper import model" << std::endl << std::endl;
398 aFile << "model.begin()" << std::endl;
400 // dump collected data
401 aFile << myFullDump.str();
402 aFile << myDumpBuffer.str();
405 aFile << "model.end()" << std::endl;
413 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName,
414 const std::string& theObject)
416 myModules[theModuleName].insert(theObject);
419 void ModelHighAPI_Dumper::dumpEntitySetName()
421 const LastDumpedEntity& aLastDumped = myEntitiesStack.top();
423 // dump "setName" for the entity
424 if (aLastDumped.myUserName) {
425 EntityName& anEntityNames = myNames[aLastDumped.myEntity];
426 if (!anEntityNames.myIsDefault)
427 myDumpBuffer << anEntityNames.myCurrentName << ".setName(\""
428 << anEntityNames.myUserName << "\")" << std::endl;
429 // don't dump "setName" for the entity twice
430 anEntityNames.myUserName.clear();
431 anEntityNames.myIsDefault = true;
433 // dump "setName" for results
434 std::list<ResultPtr>::const_iterator aResIt = aLastDumped.myResults.begin();
435 std::list<ResultPtr>::const_iterator aResEnd = aLastDumped.myResults.end();
436 for (; aResIt != aResEnd; ++aResIt) {
438 EntityName& anEntityNames = myNames[*aResIt];
439 if (!anEntityNames.myIsDefault) {
441 myDumpBuffer << ".setName(\"" << anEntityNames.myUserName << "\")" << std::endl;
442 // don't dump "setName" for the entity twice
443 anEntityNames.myUserName.clear();
444 anEntityNames.myIsDefault = true;
447 if (!isDefaultColor(*aResIt)) {
448 AttributeIntArrayPtr aColor = (*aResIt)->data()->intArray(ModelAPI_Result::COLOR_ID());
449 if (aColor && aColor->isInitialized()) {
451 myDumpBuffer << ".setColor(" << aColor->value(0) << ", " << aColor->value(1)
452 << ", " << aColor->value(2) << ")" << std::endl;
455 // set result deflection
456 if (!isDefaultDeflection(*aResIt)) {
457 AttributeDoublePtr aDeflectionAttr =
458 (*aResIt)->data()->real(ModelAPI_Result::DEFLECTION_ID());
459 if(aDeflectionAttr.get() && aDeflectionAttr->isInitialized()) {
461 myDumpBuffer << ".setDeflection(" << aDeflectionAttr->value() << ")" << std::endl;
466 myNames[aLastDumped.myEntity].myIsDumped = true;
467 myEntitiesStack.pop();
470 bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const
472 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
473 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
474 return aFound != myNames.end() || myFeaturesToSkip.find(aFeature) != myFeaturesToSkip.end();
477 bool ModelHighAPI_Dumper::isDefaultColor(const ResultPtr& theResult) const
479 AttributeIntArrayPtr aColor = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
480 if (!aColor || !aColor->isInitialized())
483 std::string aSection, aName, aDefault;
484 theResult->colorConfigInfo(aSection, aName, aDefault);
486 // dump current color
487 std::ostringstream aColorInfo;
488 aColorInfo << aColor->value(0) << "," << aColor->value(1) << "," << aColor->value(2);
490 return aDefault == aColorInfo.str();
493 bool ModelHighAPI_Dumper::isDefaultDeflection(const ResultPtr& theResult) const
495 AttributeDoublePtr aDeflectionAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
496 if(!aDeflectionAttr || !aDeflectionAttr->isInitialized()) {
500 double aCurrent = aDeflectionAttr->value();
501 double aDefault = -1;
503 bool isConstruction = false;
504 std::string aResultGroup = theResult->groupName();
505 if (aResultGroup == ModelAPI_ResultConstruction::group())
506 isConstruction = true;
507 else if (aResultGroup == ModelAPI_ResultBody::group()) {
508 GeomShapePtr aGeomShape = theResult->shape();
509 if (aGeomShape.get()) {
510 // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
511 // correction of deviation for them should not influence to the application performance
512 GeomAPI_ShapeExplorer anExp(aGeomShape, GeomAPI_Shape::FACE);
513 isConstruction = !anExp.more();
517 aDefault = Config_PropManager::real("Visualization", "construction_deflection",
518 ModelAPI_ResultConstruction::DEFAULT_DEFLECTION());
520 aDefault = Config_PropManager::real("Visualization", "body_deflection",
521 ModelAPI_ResultBody::DEFAULT_DEFLECTION());
523 return fabs(aCurrent - aDefault) < 1.e-12;
526 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
528 myDumpBuffer << theChar;
532 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
534 myDumpBuffer << theString;
538 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
540 myDumpBuffer << theString;
544 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const bool theValue)
546 myDumpBuffer << (theValue ? "True" : "False");
550 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
552 myDumpBuffer << theValue;
556 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
558 myDumpBuffer << theValue;
562 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
564 importModule("GeomAPI", "GeomAPI_Pnt");
565 myDumpBuffer << "GeomAPI_Pnt(" << thePoint->x() << ", "
566 << thePoint->y() << ", " << thePoint->z() << ")";
570 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
572 importModule("GeomAPI", "GeomAPI_Dir");
573 myDumpBuffer << "GeomAPI_Dir(" << theDir->x() << ", "
574 << theDir->y() << ", " << theDir->z() << ")";
578 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
579 const std::shared_ptr<GeomDataAPI_Dir>& theDir)
581 myDumpBuffer << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
585 static void dumpArray(std::ostringstream& theOutput, int theSize,
586 double* theValues, std::string* theTexts)
588 for (int i = 0; i < theSize; ++i) {
591 if (theTexts[i].empty())
592 theOutput << theValues[i];
594 theOutput << "\"" << theTexts[i] << "\"";
598 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
599 const std::shared_ptr<GeomDataAPI_Point>& thePoint)
601 static const int aSize = 3;
602 double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
603 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()};
604 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
608 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
609 const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
611 static const int aSize = 2;
612 double aValues[aSize] = {thePoint->x(), thePoint->y()};
613 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()};
614 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
618 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
619 const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
621 myDumpBuffer << (theAttrBool->value() ? "True" : "False");
625 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
626 const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
628 std::string aText = theAttrInt->text();
630 myDumpBuffer << theAttrInt->value();
632 myDumpBuffer << "\"" << aText << "\"";
636 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
637 const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
639 std::string aText = theAttrReal->text();
641 myDumpBuffer << theAttrReal->value();
643 myDumpBuffer << "\"" << aText << "\"";
647 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
648 const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
650 myDumpBuffer << "\"" << theAttrStr->value() << "\"";
654 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity)
656 myDumpBuffer << name(theEntity);
658 if (!myNames[theEntity].myIsDumped) {
659 bool isUserDefinedName = !myNames[theEntity].myIsDefault;
660 // store results if they have user-defined names or colors
661 std::list<ResultPtr> aResultsWithNameOrColor;
662 const std::list<ResultPtr>& aResults = theEntity->results();
663 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
664 for (; aResIt != aResults.end(); ++aResIt) {
665 if (!myNames[*aResIt].myIsDefault || !isDefaultColor(*aResIt) ||
666 !isDefaultDeflection(*aResIt))
667 aResultsWithNameOrColor.push_back(*aResIt);
669 ResultCompSolidPtr aCompSolid =
670 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aResIt);
672 int aNbSubs = aCompSolid->numberOfSubs();
673 for (int i = 0; i < aNbSubs; ++i) {
674 ResultPtr aCurRes = aCompSolid->subResult(i);
675 if (!myNames[aCurRes].myIsDefault || !isDefaultColor(aCurRes) ||
676 !isDefaultDeflection(aCurRes))
677 aResultsWithNameOrColor.push_back(aCurRes);
681 // store just dumped entity to stack
682 if (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theEntity)
683 myEntitiesStack.push(
684 LastDumpedEntity(theEntity, isUserDefinedName, aResultsWithNameOrColor));
687 // remove entity from the list of not dumped items
688 myNotDumpedEntities.erase(theEntity);
692 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ResultPtr& theResult)
694 FeaturePtr aFeature = ModelAPI_Feature::feature(theResult);
697 std::list<ResultPtr> aResults = aFeature->results();
698 for(std::list<ResultPtr>::const_iterator
699 anIt = aResults.cbegin(); anIt != aResults.cend(); ++anIt, ++anIndex) {
700 if(theResult->isSame(*anIt)) {
704 ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*anIt);
706 int aNbSubs = aCompSolid->numberOfSubs();
707 for (aSubIndex = 0; aSubIndex < aNbSubs; ++aSubIndex)
708 if (theResult->isSame(aCompSolid->subResult(aSubIndex)))
710 if (aSubIndex < aNbSubs)
716 myDumpBuffer << name(aFeature);
718 myDumpBuffer << ".result()";
720 myDumpBuffer << ".results()[" << anIndex << "]";
722 if (aSubIndex >= 0) {
723 myDumpBuffer << ".subResult(" << aSubIndex << ")";
728 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ObjectPtr& theObject)
730 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
732 myDumpBuffer << name(aFeature);
736 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
745 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const AttributePtr& theAttr)
747 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttr->owner());
749 std::string aWrapperPrefix, aWrapperSuffix;
750 // Check the attribute belongs to copied (in multi-translation or multi-rotation) feature.
751 // In this case we need to cast explicitly feature to appropriate type.
752 AttributeBooleanPtr isCopy = anOwner->boolean("Copy");
753 if (isCopy.get() && isCopy->value()) {
754 aWrapperPrefix = featureWrapper(anOwner) + "(";
755 aWrapperSuffix = ")";
756 importModule("SketchAPI");
759 myDumpBuffer << aWrapperPrefix << name(anOwner) << aWrapperSuffix
760 << "." << attributeGetter(anOwner, theAttr->id()) << "()";
764 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
765 const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
767 if (theRefAttr->isObject())
768 *this << theRefAttr->object();
770 *this << theRefAttr->attr();
774 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
775 const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList)
778 std::list<std::pair<ObjectPtr, AttributePtr> > aList = theRefAttrList->list();
779 bool isAdded = false;
780 std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aList.begin();
781 for (; anIt != aList.end(); ++anIt) {
783 myDumpBuffer << ", ";
787 *this << anIt->first;
788 else if (anIt->second)
789 * this << anIt->second;
795 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
796 const std::shared_ptr<ModelAPI_AttributeReference>& theReference)
798 *this << theReference->value();
802 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
803 const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList)
805 static const int aThreshold = 2;
806 // if number of elements in the list if greater than a threshold,
807 // dump it in a separate line with specific name
808 std::string aDumped = myDumpBuffer.str();
809 if (aDumped.empty() || theRefList->size() <= aThreshold) {
811 std::list<ObjectPtr> aList = theRefList->list();
812 bool isAdded = false;
813 std::list<ObjectPtr>::const_iterator anIt = aList.begin();
814 for (; anIt != aList.end(); ++anIt) {
816 myDumpBuffer << ", ";
824 // clear buffer and store list "as is"
825 myDumpBuffer.str("");
827 // save buffer and clear it again
828 std::string aDumpedList = myDumpBuffer.str();
829 myDumpBuffer.str("");
830 // obtain name of list
831 FeaturePtr anOwner = ModelAPI_Feature::feature(theRefList->owner());
832 std::string aListName = name(anOwner) + "_objects";
833 // store all previous data
834 myDumpBuffer << aListName << " = " << aDumpedList << std::endl
835 << aDumped << aListName;
840 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
841 const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
843 myDumpBuffer << "model.selection(";
845 if(!theAttrSelect->isInitialized()) {
850 GeomShapePtr aShape = theAttrSelect->value();
852 aShape = theAttrSelect->context()->shape();
860 myDumpBuffer << "\"" << aShape->shapeTypeStr() << "\", \"" <<
861 theAttrSelect->namingName() << "\")";
865 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
866 const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList)
871 std::string aShapeTypeStr;
873 bool isAdded = false;
875 for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) {
876 AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex);
877 aShape = anAttribute->value();
879 aShape = anAttribute->context()->shape();
887 myDumpBuffer << ", ";
891 myDumpBuffer << "model.selection(\"" <<
892 aShape->shapeTypeStr() << "\", \"" << anAttribute->namingName() << "\")";
899 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
900 const std::shared_ptr<ModelAPI_AttributeStringArray>& theArray)
903 for(int anIndex = 0; anIndex < theArray->size(); ++anIndex) {
907 myDumpBuffer<<"\""<<theArray->value(anIndex)<<"\"";
915 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
916 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
918 theDumper.myDumpBuffer << theEndl;
920 if (!theDumper.myEntitiesStack.empty()) {
922 // all copies have been stored into stack, pop them all
925 // Name for composite feature is dumped when all sub-entities are dumped
926 // (see method ModelHighAPI_Dumper::processSubs).
927 const ModelHighAPI_Dumper::LastDumpedEntity& aLastDumped = theDumper.myEntitiesStack.top();
928 CompositeFeaturePtr aComposite =
929 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aLastDumped.myEntity);
931 theDumper.dumpEntitySetName();
932 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aLastDumped.myEntity);
934 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
935 isCopy = aCopyAttr.get() && aCopyAttr->value();
941 // store all not-dumped entities first
942 std::set<EntityPtr> aNotDumped = theDumper.myNotDumpedEntities;
943 std::string aBufCopy = theDumper.myDumpBuffer.str();
944 theDumper.clear(true);
945 std::set<EntityPtr>::const_iterator anIt = aNotDumped.begin();
946 for (; anIt != aNotDumped.end(); ++anIt) {
947 // if the feature is composite, dump it with all subs
948 CompositeFeaturePtr aCompFeat =
949 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
951 theDumper.process(aCompFeat, true);
953 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
954 theDumper.dumpFeature(aFeature, true);
958 // avoid multiple empty lines
959 size_t anInd = std::string::npos;
960 while ((anInd = aBufCopy.find("\n\n\n")) != std::string::npos)
961 aBufCopy.erase(anInd, 1);
962 // then store currently dumped string
963 theDumper.myFullDump << aBufCopy;