1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "ModelHighAPI_Dumper.h"
23 #include <Config_PropManager.h>
25 #include <GeomAPI_Pnt.h>
26 #include <GeomAPI_Dir.h>
27 #include <GeomAPI_ShapeExplorer.h>
29 #include <GeomDataAPI_Dir.h>
30 #include <GeomDataAPI_Point.h>
31 #include <GeomDataAPI_Point2D.h>
33 #include <ModelAPI_AttributeBoolean.h>
34 #include <ModelAPI_AttributeDouble.h>
35 #include <ModelAPI_AttributeIntArray.h>
36 #include <ModelAPI_AttributeInteger.h>
37 #include <ModelAPI_AttributeRefAttr.h>
38 #include <ModelAPI_AttributeRefAttrList.h>
39 #include <ModelAPI_AttributeReference.h>
40 #include <ModelAPI_AttributeRefList.h>
41 #include <ModelAPI_AttributeSelection.h>
42 #include <ModelAPI_AttributeSelectionList.h>
43 #include <ModelAPI_AttributeString.h>
44 #include <ModelAPI_AttributeStringArray.h>
45 #include <ModelAPI_CompositeFeature.h>
46 #include <ModelAPI_Document.h>
47 #include <ModelAPI_Entity.h>
48 #include <ModelAPI_Feature.h>
49 #include <ModelAPI_Result.h>
50 #include <ModelAPI_ResultBody.h>
51 #include <ModelAPI_ResultCompSolid.h>
52 #include <ModelAPI_ResultConstruction.h>
53 #include <ModelAPI_ResultPart.h>
54 #include <ModelAPI_Tools.h>
56 #include <PartSetPlugin_Part.h>
58 #include <OSD_OpenFile.hxx>
62 static int gCompositeStackDepth = 0;
64 ModelHighAPI_Dumper* ModelHighAPI_Dumper::mySelf = 0;
66 ModelHighAPI_Dumper::ModelHighAPI_Dumper()
71 void ModelHighAPI_Dumper::setInstance(ModelHighAPI_Dumper* theDumper)
77 ModelHighAPI_Dumper* ModelHighAPI_Dumper::getInstance()
82 void ModelHighAPI_Dumper::clear(bool bufferOnly)
85 myDumpBuffer << std::setprecision(16);
91 myFullDump << std::setprecision(16);
95 myFeatureCount.clear();
96 while (!myEntitiesStack.empty())
97 myEntitiesStack.pop();
101 void ModelHighAPI_Dumper::clearNotDumped()
103 myNotDumpedEntities.clear();
106 // Convert string to integer. If the string is not a number, return -1
107 static int toInt(const std::string& theString)
109 std::string::const_iterator aChar = theString.begin();
110 for (; aChar != theString.end(); ++aChar)
111 if (!std::isdigit(*aChar))
113 if (aChar != theString.end())
114 return -1; // not a number
115 return std::stoi(theString);
118 const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity,
119 bool theSaveNotDumped,
120 bool theUseEntityName)
122 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
123 if (aFound != myNames.end())
124 return aFound->second.myCurrentName;
126 // entity is not found, store it
128 bool isDefaultName = false;
129 std::ostringstream aDefaultName;
130 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
132 aName = aFeature->name();
133 const std::string& aKind = aFeature->getKind();
134 DocumentPtr aDoc = aFeature->document();
135 int& aNbFeatures = myFeatureCount[aDoc][aKind];
138 size_t anIndex = aName.find(aKind);
139 if (anIndex == 0 && aName[aKind.length()] == '_') { // name starts with "FeatureKind_"
140 std::string anIdStr = aName.substr(aKind.length() + 1);
141 int anId = toInt(anIdStr);
143 // Check number of already registered objects of such kind. Index of current object
144 // should be the same to identify feature's name as automatically generated.
145 if (aNbFeatures == anId) {
146 // name is not user-defined
147 isDefaultName = true;
151 // obtain default name for the feature
152 if (theUseEntityName)
153 aDefaultName << aName;
156 NbFeaturesMap::const_iterator aFIt = myFeatureCount.begin();
157 for (; aFIt != myFeatureCount.end(); ++aFIt) {
158 std::map<std::string, int>::const_iterator aFound = aFIt->second.find(aKind);
159 if (aFound != aFIt->second.end())
160 aFullIndex += aFound->second;
162 aDefaultName << aKind << "_" << aFullIndex;
166 myNames[theEntity] = EntityName(aDefaultName.str(), aName, isDefaultName);
167 if (theSaveNotDumped)
168 myNotDumpedEntities.insert(theEntity);
170 // store names of results
172 saveResultNames(aFeature);
174 return myNames[theEntity].myCurrentName;
177 const std::string& ModelHighAPI_Dumper::parentName(const FeaturePtr& theEntity)
179 const std::set<AttributePtr>& aRefs = theEntity->data()->refsToMe();
180 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
181 for (; aRefIt != aRefs.end(); ++aRefIt) {
182 CompositeFeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(
183 ModelAPI_Feature::feature((*aRefIt)->owner()));
185 return name(anOwner);
188 static const std::string DUMMY;
192 void ModelHighAPI_Dumper::saveResultNames(const FeaturePtr& theFeature)
194 // Default name of the feature
195 bool isFeatureDefaultName = myNames[theFeature].myIsDefault;
197 // Save only names of results which is not correspond to default feature name
198 const std::list<ResultPtr>& aResults = theFeature->results();
199 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
200 for (int i = 0; aResIt != aResults.end(); ++aResIt, ++i) {
201 std::string aDefaultName = ModelAPI_Tools::getDefaultName(*aResIt, i);
202 std::string aResName = (*aResIt)->data()->name();
204 bool isUserDefined = !(isFeatureDefaultName && aDefaultName == aResName);
206 myNames[*aResIt] = EntityName(aResName,
207 (isUserDefined ? aResName : std::string()), !isUserDefined);
209 // check names of sub-results for CompSolid
210 ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aResIt);
212 int aNbSubs = aCompSolid->numberOfSubs();
213 for (int j = 0; j < aNbSubs; ++j) {
214 ResultPtr aSub = aCompSolid->subResult(j);
215 std::string aSubName = aSub->data()->name();
216 aDefaultName = ModelAPI_Tools::getDefaultName(aSub, j);
218 bool isUserDefinedSubName = isUserDefined || aDefaultName != aSubName;
219 myNames[aSub] = EntityName(aSubName,
220 (isUserDefinedSubName ? aSubName : std::string()), !isUserDefinedSubName);
226 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc,
227 const std::string& theFileName)
229 // dump top level document feature
230 static const std::string aDocName("partSet");
231 myNames[theDoc] = EntityName(aDocName, std::string(), true);
232 *this << aDocName << " = model.moduleDocument()" << std::endl;
234 // dump subfeatures and store result to file
235 return process(theDoc) && exportTo(theFileName);
238 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc)
241 std::list<FeaturePtr> aFeatures = theDoc->allFeatures();
242 std::list<FeaturePtr>::const_iterator aFeatIt = aFeatures.begin();
243 // firstly, dump all parameters
244 for (; aFeatIt != aFeatures.end(); ++ aFeatIt)
245 dumpParameter(*aFeatIt);
246 // dump all other features
247 for (aFeatIt = aFeatures.begin(); aFeatIt != aFeatures.end(); ++aFeatIt) {
248 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFeatIt);
249 if (aCompFeat) // iteratively process composite features
250 isOk = process(aCompFeat) && isOk;
251 else if (!isDumped(*aFeatIt)) // dump common feature
252 dumpFeature(*aFeatIt);
257 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
260 // increase composite features stack
261 ++gCompositeStackDepth;
262 // dump composite itself
263 if (!isDumped(theComposite) || isForce)
264 dumpFeature(FeaturePtr(theComposite), isForce);
266 // sub-part is processed independently, because it provides separate document
267 if (theComposite->getKind() == PartSetPlugin_Part::ID()) {
268 // dump name of the part if it is different from default
269 if (!myEntitiesStack.empty())
272 // decrease composite features stack because we run into separate document
273 --gCompositeStackDepth;
275 ResultPartPtr aPartResult =
276 std::dynamic_pointer_cast<ModelAPI_ResultPart>(theComposite->lastResult());
279 DocumentPtr aSubDoc = aPartResult->partDoc();
282 // set name of document
283 const std::string& aPartName = myNames[theComposite].myCurrentName;
284 std::string aDocName = aPartName + "_doc";
285 myNames[aSubDoc] = EntityName(aDocName, std::string(), true);
287 // dump document in a separate line
288 *this << aDocName << " = " << aPartName << ".document()" << std::endl;
289 // dump features in the document
290 bool aRes = process(aSubDoc);
291 *this << "model.do()" << std::endl;
296 bool isOk = processSubs(theComposite);
297 // decrease composite features stack
298 --gCompositeStackDepth;
303 bool ModelHighAPI_Dumper::processSubs(
304 const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
308 // dump all sub-features;
309 bool isSubDumped = false;
310 int aNbSubs = theComposite->numberOfSubs();
311 for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) {
312 FeaturePtr aFeature = theComposite->subFeature(anIndex);
313 if (isDumped(aFeature))
317 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
318 if (aCompFeat) // iteratively process composite features
319 isOk = process(aCompFeat) && isOk;
321 dumpFeature(aFeature, true);
324 bool isDumpSetName = !myEntitiesStack.empty() &&
325 myEntitiesStack.top().myEntity == EntityPtr(theComposite);
326 bool isForceModelDo = isSubDumped && isDumpSetName &&
327 (myEntitiesStack.top().myUserName || !myEntitiesStack.top().myResults.empty());
328 // It is necessary for the sketch to create its result when complete (command "model.do()").
329 // This option is set by flat theDumpModelDo.
330 // However, nested sketches are rebuilt by parent feature, so, they do not need
331 // explicit call of "model.do()". This will be controlled by the depth of the stack.
332 if (isForceModelDo || (theDumpModelDo && gCompositeStackDepth <= 1))
333 *this << "model.do()" << std::endl;
335 // dump "setName" for composite feature
341 void ModelHighAPI_Dumper::dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
342 const FeaturePtr& theSubFeature)
344 name(theSubFeature, false);
345 myNames[theSubFeature] = EntityName(theSubFeatureGet, theSubFeature->name(), false);
347 // store results if they have user-defined names or colors
348 std::list<ResultPtr> aResultsWithNameOrColor;
349 const std::list<ResultPtr>& aResults = theSubFeature->results();
350 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
351 for (; aResIt != aResults.end(); ++aResIt) {
352 std::string aResName = (*aResIt)->data()->name();
353 myNames[*aResIt] = EntityName(aResName, aResName, false);
354 aResultsWithNameOrColor.push_back(*aResIt);
357 // store just dumped entity to stack
358 myEntitiesStack.push(LastDumpedEntity(theSubFeature, true, aResultsWithNameOrColor));
363 bool ModelHighAPI_Dumper::exportTo(const std::string& theFileName)
366 OSD_OpenStream(aFile, theFileName.c_str(), std::ofstream::out);
367 if (!aFile.is_open())
371 for (ModulesMap::const_iterator aModIt = myModules.begin();
372 aModIt != myModules.end(); ++aModIt) {
373 aFile << "from " << aModIt->first << " import ";
374 if (aModIt->second.empty() ||
375 aModIt->second.find(std::string()) != aModIt->second.end())
376 aFile << "*"; // import whole module
378 // import specific features
379 std::set<std::string>::const_iterator anObjIt = aModIt->second.begin();
381 for (++anObjIt; anObjIt != aModIt->second.end(); ++anObjIt)
382 aFile << ", " << *anObjIt;
386 if (!myModules.empty())
389 aFile << "from salome.shaper import model" << std::endl << std::endl;
390 aFile << "model.begin()" << std::endl;
392 // dump collected data
393 aFile << myFullDump.str();
394 aFile << myDumpBuffer.str();
397 aFile << "model.end()" << std::endl;
405 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName,
406 const std::string& theObject)
408 myModules[theModuleName].insert(theObject);
411 void ModelHighAPI_Dumper::dumpEntitySetName()
413 const LastDumpedEntity& aLastDumped = myEntitiesStack.top();
415 // dump "setName" for the entity
416 if (aLastDumped.myUserName) {
417 EntityName& anEntityNames = myNames[aLastDumped.myEntity];
418 if (!anEntityNames.myIsDefault)
419 myDumpBuffer << anEntityNames.myCurrentName << ".setName(\""
420 << anEntityNames.myUserName << "\")" << std::endl;
421 // don't dump "setName" for the entity twice
422 anEntityNames.myUserName.clear();
423 anEntityNames.myIsDefault = true;
425 // dump "setName" for results
426 std::list<ResultPtr>::const_iterator aResIt = aLastDumped.myResults.begin();
427 std::list<ResultPtr>::const_iterator aResEnd = aLastDumped.myResults.end();
428 for (; aResIt != aResEnd; ++aResIt) {
430 EntityName& anEntityNames = myNames[*aResIt];
431 if (!anEntityNames.myIsDefault) {
433 myDumpBuffer << ".setName(\"" << anEntityNames.myUserName << "\")" << std::endl;
434 // don't dump "setName" for the entity twice
435 anEntityNames.myUserName.clear();
436 anEntityNames.myIsDefault = true;
439 if (!isDefaultColor(*aResIt)) {
440 AttributeIntArrayPtr aColor = (*aResIt)->data()->intArray(ModelAPI_Result::COLOR_ID());
441 if (aColor && aColor->isInitialized()) {
443 myDumpBuffer << ".setColor(" << aColor->value(0) << ", " << aColor->value(1)
444 << ", " << aColor->value(2) << ")" << std::endl;
447 // set result deflection
448 if (!isDefaultDeflection(*aResIt)) {
449 AttributeDoublePtr aDeflectionAttr =
450 (*aResIt)->data()->real(ModelAPI_Result::DEFLECTION_ID());
451 if(aDeflectionAttr.get() && aDeflectionAttr->isInitialized()) {
453 myDumpBuffer << ".setDeflection(" << aDeflectionAttr->value() << ")" << std::endl;
456 // set result transparency
457 if (!isDefaultTransparency(*aResIt)) {
458 AttributeDoublePtr aTransparencyAttr =
459 (*aResIt)->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
460 if(aTransparencyAttr.get() && aTransparencyAttr->isInitialized()) {
462 myDumpBuffer << ".setTransparency(" << aTransparencyAttr->value() << ")" << std::endl;
467 myNames[aLastDumped.myEntity].myIsDumped = true;
468 myEntitiesStack.pop();
471 bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const
473 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
474 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
475 return aFound != myNames.end() || myFeaturesToSkip.find(aFeature) != myFeaturesToSkip.end();
478 bool ModelHighAPI_Dumper::isDefaultColor(const ResultPtr& theResult) const
480 AttributeIntArrayPtr aColor = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
481 if (!aColor || !aColor->isInitialized())
484 std::string aSection, aName, aDefault;
485 theResult->colorConfigInfo(aSection, aName, aDefault);
487 // dump current color
488 std::ostringstream aColorInfo;
489 aColorInfo << aColor->value(0) << "," << aColor->value(1) << "," << aColor->value(2);
491 return aDefault == aColorInfo.str();
494 bool ModelHighAPI_Dumper::isDefaultDeflection(const ResultPtr& theResult) const
496 AttributeDoublePtr aDeflectionAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
497 if(!aDeflectionAttr || !aDeflectionAttr->isInitialized()) {
501 double aCurrent = aDeflectionAttr->value();
502 double aDefault = -1;
504 bool isConstruction = false;
505 std::string aResultGroup = theResult->groupName();
506 if (aResultGroup == ModelAPI_ResultConstruction::group())
507 isConstruction = true;
508 else if (aResultGroup == ModelAPI_ResultBody::group()) {
509 GeomShapePtr aGeomShape = theResult->shape();
510 if (aGeomShape.get()) {
511 // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
512 // correction of deviation for them should not influence to the application performance
513 GeomAPI_ShapeExplorer anExp(aGeomShape, GeomAPI_Shape::FACE);
514 isConstruction = !anExp.more();
518 aDefault = Config_PropManager::real("Visualization", "construction_deflection");
520 aDefault = Config_PropManager::real("Visualization", "body_deflection");
522 return fabs(aCurrent - aDefault) < 1.e-12;
525 bool ModelHighAPI_Dumper::isDefaultTransparency(const ResultPtr& theResult) const
527 AttributeDoublePtr anAttribute = theResult->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
528 if(!anAttribute || !anAttribute->isInitialized()) {
531 return fabs(anAttribute->value()) < 1.e-12;
534 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
536 myDumpBuffer << theChar;
540 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
542 myDumpBuffer << theString;
546 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
548 myDumpBuffer << theString;
552 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const bool theValue)
554 myDumpBuffer << (theValue ? "True" : "False");
558 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
560 myDumpBuffer << theValue;
564 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
566 myDumpBuffer << theValue;
570 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
572 importModule("GeomAPI", "GeomAPI_Pnt");
573 myDumpBuffer << "GeomAPI_Pnt(" << thePoint->x() << ", "
574 << thePoint->y() << ", " << thePoint->z() << ")";
578 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
580 importModule("GeomAPI", "GeomAPI_Dir");
581 myDumpBuffer << "GeomAPI_Dir(" << theDir->x() << ", "
582 << theDir->y() << ", " << theDir->z() << ")";
586 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
587 const std::shared_ptr<GeomDataAPI_Dir>& theDir)
589 myDumpBuffer << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
593 static void dumpArray(std::ostringstream& theOutput, int theSize,
594 double* theValues, std::string* theTexts)
596 for (int i = 0; i < theSize; ++i) {
599 if (theTexts[i].empty())
600 theOutput << theValues[i];
602 theOutput << "\"" << theTexts[i] << "\"";
606 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
607 const std::shared_ptr<GeomDataAPI_Point>& thePoint)
609 static const int aSize = 3;
610 double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
611 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()};
612 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
616 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
617 const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
619 static const int aSize = 2;
620 double aValues[aSize] = {thePoint->x(), thePoint->y()};
621 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()};
622 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
626 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
627 const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
629 myDumpBuffer << (theAttrBool->value() ? "True" : "False");
633 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
634 const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
636 std::string aText = theAttrInt->text();
638 myDumpBuffer << theAttrInt->value();
640 myDumpBuffer << "\"" << aText << "\"";
644 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
645 const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
647 std::string aText = theAttrReal->text();
649 myDumpBuffer << theAttrReal->value();
651 myDumpBuffer << "\"" << aText << "\"";
655 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
656 const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
658 myDumpBuffer << "\"" << theAttrStr->value() << "\"";
662 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity)
664 myDumpBuffer << name(theEntity);
666 if (!myNames[theEntity].myIsDumped) {
667 bool isUserDefinedName = !myNames[theEntity].myIsDefault;
668 // store results if they have user-defined names or colors
669 std::list<ResultPtr> aResultsWithNameOrColor;
670 const std::list<ResultPtr>& aResults = theEntity->results();
671 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
672 for (; aResIt != aResults.end(); ++aResIt) {
673 if (!myNames[*aResIt].myIsDefault || !isDefaultColor(*aResIt) ||
674 !isDefaultDeflection(*aResIt) || !isDefaultTransparency(*aResIt))
675 aResultsWithNameOrColor.push_back(*aResIt);
677 ResultCompSolidPtr aCompSolid =
678 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aResIt);
680 int aNbSubs = aCompSolid->numberOfSubs();
681 for (int i = 0; i < aNbSubs; ++i) {
682 ResultPtr aCurRes = aCompSolid->subResult(i);
683 if (!myNames[aCurRes].myIsDefault || !isDefaultColor(aCurRes) ||
684 !isDefaultDeflection(aCurRes) || !isDefaultTransparency(aCurRes))
685 aResultsWithNameOrColor.push_back(aCurRes);
689 // store just dumped entity to stack
690 if (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theEntity)
691 myEntitiesStack.push(
692 LastDumpedEntity(theEntity, isUserDefinedName, aResultsWithNameOrColor));
695 // remove entity from the list of not dumped items
696 myNotDumpedEntities.erase(theEntity);
700 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ResultPtr& theResult)
702 FeaturePtr aFeature = ModelAPI_Feature::feature(theResult);
705 std::list<ResultPtr> aResults = aFeature->results();
706 for(std::list<ResultPtr>::const_iterator
707 anIt = aResults.cbegin(); anIt != aResults.cend(); ++anIt, ++anIndex) {
708 if(theResult->isSame(*anIt)) {
712 ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*anIt);
714 int aNbSubs = aCompSolid->numberOfSubs();
715 for (aSubIndex = 0; aSubIndex < aNbSubs; ++aSubIndex)
716 if (theResult->isSame(aCompSolid->subResult(aSubIndex)))
718 if (aSubIndex < aNbSubs)
724 myDumpBuffer << name(aFeature);
726 myDumpBuffer << ".result()";
728 myDumpBuffer << ".results()[" << anIndex << "]";
730 if (aSubIndex >= 0) {
731 myDumpBuffer << ".subResult(" << aSubIndex << ")";
736 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ObjectPtr& theObject)
738 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
740 myDumpBuffer << name(aFeature);
744 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
753 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const AttributePtr& theAttr)
755 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttr->owner());
757 std::string aWrapperPrefix, aWrapperSuffix;
758 // Check the attribute belongs to copied (in multi-translation or multi-rotation) feature.
759 // In this case we need to cast explicitly feature to appropriate type.
760 AttributeBooleanPtr isCopy = anOwner->boolean("Copy");
761 if (isCopy.get() && isCopy->value()) {
762 aWrapperPrefix = featureWrapper(anOwner) + "(";
763 aWrapperSuffix = ")";
764 importModule("SketchAPI");
767 myDumpBuffer << aWrapperPrefix << name(anOwner) << aWrapperSuffix
768 << "." << attributeGetter(anOwner, theAttr->id()) << "()";
772 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
773 const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
775 if (theRefAttr->isObject())
776 *this << theRefAttr->object();
778 *this << theRefAttr->attr();
782 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
783 const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList)
786 std::list<std::pair<ObjectPtr, AttributePtr> > aList = theRefAttrList->list();
787 bool isAdded = false;
788 std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aList.begin();
789 for (; anIt != aList.end(); ++anIt) {
791 myDumpBuffer << ", ";
795 *this << anIt->first;
796 else if (anIt->second)
797 * this << anIt->second;
803 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
804 const std::shared_ptr<ModelAPI_AttributeReference>& theReference)
806 *this << theReference->value();
810 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
811 const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList)
813 static const int aThreshold = 2;
814 // if number of elements in the list if greater than a threshold,
815 // dump it in a separate line with specific name
816 std::string aDumped = myDumpBuffer.str();
817 if (aDumped.empty() || theRefList->size() <= aThreshold) {
819 std::list<ObjectPtr> aList = theRefList->list();
820 bool isAdded = false;
821 std::list<ObjectPtr>::const_iterator anIt = aList.begin();
822 for (; anIt != aList.end(); ++anIt) {
824 myDumpBuffer << ", ";
832 // clear buffer and store list "as is"
833 myDumpBuffer.str("");
835 // save buffer and clear it again
836 std::string aDumpedList = myDumpBuffer.str();
837 myDumpBuffer.str("");
838 // obtain name of list
839 FeaturePtr anOwner = ModelAPI_Feature::feature(theRefList->owner());
840 std::string aListName = name(anOwner) + "_objects";
841 // store all previous data
842 myDumpBuffer << aListName << " = " << aDumpedList << std::endl
843 << aDumped << aListName;
848 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
849 const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
851 myDumpBuffer << "model.selection(";
853 if(!theAttrSelect->isInitialized()) {
858 GeomShapePtr aShape = theAttrSelect->value();
860 aShape = theAttrSelect->context()->shape();
868 myDumpBuffer << "\"" << aShape->shapeTypeStr() << "\", \"" <<
869 theAttrSelect->namingName() << "\")";
873 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
874 const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList)
879 std::string aShapeTypeStr;
881 bool isAdded = false;
883 for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) {
884 AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex);
885 aShape = anAttribute->value();
887 ResultPtr aContext = anAttribute->context();
889 aShape = aContext->shape();
897 myDumpBuffer << ", ";
901 myDumpBuffer << "model.selection(\"" <<
902 aShape->shapeTypeStr() << "\", \"" << anAttribute->namingName() << "\")";
909 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
910 const std::shared_ptr<ModelAPI_AttributeStringArray>& theArray)
913 for(int anIndex = 0; anIndex < theArray->size(); ++anIndex) {
917 myDumpBuffer<<"\""<<theArray->value(anIndex)<<"\"";
925 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
926 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
928 theDumper.myDumpBuffer << theEndl;
930 if (!theDumper.myEntitiesStack.empty()) {
932 // all copies have been stored into stack, pop them all
935 // Name for composite feature is dumped when all sub-entities are dumped
936 // (see method ModelHighAPI_Dumper::processSubs).
937 const ModelHighAPI_Dumper::LastDumpedEntity& aLastDumped = theDumper.myEntitiesStack.top();
938 CompositeFeaturePtr aComposite =
939 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aLastDumped.myEntity);
941 theDumper.dumpEntitySetName();
942 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aLastDumped.myEntity);
944 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
945 isCopy = aCopyAttr.get() && aCopyAttr->value();
951 // store all not-dumped entities first
952 std::set<EntityPtr> aNotDumped = theDumper.myNotDumpedEntities;
953 std::string aBufCopy = theDumper.myDumpBuffer.str();
954 theDumper.clear(true);
955 std::set<EntityPtr>::const_iterator anIt = aNotDumped.begin();
956 for (; anIt != aNotDumped.end(); ++anIt) {
957 // if the feature is composite, dump it with all subs
958 CompositeFeaturePtr aCompFeat =
959 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
961 theDumper.process(aCompFeat, true);
963 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
964 theDumper.dumpFeature(aFeature, true);
965 // dump all referred features for the "Copy"
966 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
967 if (aCopyAttr.get() && aCopyAttr->value())
969 const std::set<AttributePtr>& aRefs = aFeature->data()->refsToMe();
970 std::set<AttributePtr>::iterator aRefIt = aRefs.begin();
971 for (; aRefIt != aRefs.end(); ++aRefIt)
973 FeaturePtr anOwner = ModelAPI_Feature::feature((*aRefIt)->owner());
974 if (anOwner && !theDumper.isDumped(anOwner))
975 theDumper.dumpFeature(anOwner, true);
981 // avoid multiple empty lines
982 size_t anInd = std::string::npos;
983 while ((anInd = aBufCopy.find("\n\n\n")) != std::string::npos)
984 aBufCopy.erase(anInd, 1);
985 // then store currently dumped string
986 theDumper.myFullDump << aBufCopy;