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_Folder.h>
50 #include <ModelAPI_Result.h>
51 #include <ModelAPI_ResultBody.h>
52 #include <ModelAPI_ResultCompSolid.h>
53 #include <ModelAPI_ResultConstruction.h>
54 #include <ModelAPI_ResultPart.h>
55 #include <ModelAPI_Tools.h>
57 #include <PartSetPlugin_Part.h>
59 #include <OSD_OpenFile.hxx>
63 static int gCompositeStackDepth = 0;
65 ModelHighAPI_Dumper* ModelHighAPI_Dumper::mySelf = 0;
67 ModelHighAPI_Dumper::ModelHighAPI_Dumper()
72 void ModelHighAPI_Dumper::setInstance(ModelHighAPI_Dumper* theDumper)
78 ModelHighAPI_Dumper* ModelHighAPI_Dumper::getInstance()
83 void ModelHighAPI_Dumper::clear(bool bufferOnly)
86 myDumpBuffer << std::setprecision(16);
92 myFullDump << std::setprecision(16);
96 myFeatureCount.clear();
97 while (!myEntitiesStack.empty())
98 myEntitiesStack.pop();
102 void ModelHighAPI_Dumper::clearNotDumped()
104 myNotDumpedEntities.clear();
107 // Convert string to integer. If the string is not a number, return -1
108 static int toInt(const std::string& theString)
110 std::string::const_iterator aChar = theString.begin();
111 for (; aChar != theString.end(); ++aChar)
112 if (!std::isdigit(*aChar))
114 if (aChar != theString.end())
115 return -1; // not a number
116 return std::stoi(theString);
119 const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity,
120 bool theSaveNotDumped,
121 bool theUseEntityName)
123 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
124 if (aFound != myNames.end())
125 return aFound->second.myCurrentName;
127 // entity is not found, store it
128 std::string aName, aKind;
129 bool isDefaultName = false;
130 bool isSaveNotDumped = theSaveNotDumped;
131 std::ostringstream aDefaultName;
132 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
134 aName = aFeature->name();
135 aKind = aFeature->getKind();
137 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(theEntity);
139 aName = aFolder->data()->name();
140 aKind = ModelAPI_Folder::ID();
141 isSaveNotDumped = false;
142 myNotDumpedFolders.insert(aFolder);
146 ObjectPtr anObject = std::dynamic_pointer_cast<ModelAPI_Object>(theEntity);
148 DocumentPtr aDoc = anObject->document();
149 int& aNbFeatures = myFeatureCount[aDoc][aKind];
152 size_t anIndex = aName.find(aKind);
153 if (anIndex == 0 && aName[aKind.length()] == '_') { // name starts with "FeatureKind_"
154 std::string anIdStr = aName.substr(aKind.length() + 1);
155 int anId = toInt(anIdStr);
157 // Check number of already registered objects of such kind. Index of current object
158 // should be the same to identify feature's name as automatically generated.
159 if (aNbFeatures == anId) {
160 // name is not user-defined
161 isDefaultName = true;
165 // obtain default name for the feature
166 if (theUseEntityName)
167 aDefaultName << aName;
170 NbFeaturesMap::const_iterator aFIt = myFeatureCount.begin();
171 for (; aFIt != myFeatureCount.end(); ++aFIt) {
172 std::map<std::string, int>::const_iterator aFound = aFIt->second.find(aKind);
173 if (aFound != aFIt->second.end())
174 aFullIndex += aFound->second;
176 aDefaultName << aKind << "_" << aFullIndex;
180 myNames[theEntity] = EntityName(aDefaultName.str(), aName, isDefaultName);
182 myNotDumpedEntities.insert(theEntity);
184 // store names of results
186 saveResultNames(aFeature);
188 return myNames[theEntity].myCurrentName;
191 const std::string& ModelHighAPI_Dumper::parentName(const FeaturePtr& theEntity)
193 const std::set<AttributePtr>& aRefs = theEntity->data()->refsToMe();
194 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
195 for (; aRefIt != aRefs.end(); ++aRefIt) {
196 CompositeFeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(
197 ModelAPI_Feature::feature((*aRefIt)->owner()));
199 return name(anOwner);
202 static const std::string DUMMY;
206 void ModelHighAPI_Dumper::saveResultNames(const FeaturePtr& theFeature)
208 // Default name of the feature
209 bool isFeatureDefaultName = myNames[theFeature].myIsDefault;
211 // Save only names of results which is not correspond to default feature name
212 const std::list<ResultPtr>& aResults = theFeature->results();
213 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
214 for (int i = 0; aResIt != aResults.end(); ++aResIt, ++i) {
215 std::string aDefaultName = ModelAPI_Tools::getDefaultName(*aResIt, i);
216 std::string aResName = (*aResIt)->data()->name();
218 bool isUserDefined = !(isFeatureDefaultName && aDefaultName == aResName);
220 myNames[*aResIt] = EntityName(aResName,
221 (isUserDefined ? aResName : std::string()), !isUserDefined);
223 // check names of sub-results for CompSolid
224 ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aResIt);
226 int aNbSubs = aCompSolid->numberOfSubs();
227 for (int j = 0; j < aNbSubs; ++j) {
228 ResultPtr aSub = aCompSolid->subResult(j);
229 std::string aSubName = aSub->data()->name();
230 aDefaultName = ModelAPI_Tools::getDefaultName(aSub, j);
232 bool isUserDefinedSubName = isUserDefined || aDefaultName != aSubName;
233 myNames[aSub] = EntityName(aSubName,
234 (isUserDefinedSubName ? aSubName : std::string()), !isUserDefinedSubName);
240 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc,
241 const std::string& theFileName)
243 // dump top level document feature
244 static const std::string aDocName("partSet");
245 myNames[theDoc] = EntityName(aDocName, std::string(), true);
246 *this << aDocName << " = model.moduleDocument()" << std::endl;
248 // dump subfeatures and store result to file
249 return process(theDoc) && exportTo(theFileName);
252 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc)
255 std::list<ObjectPtr> anObjects = theDoc->allObjects();
256 std::list<ObjectPtr>::const_iterator anObjIt = anObjects.begin();
257 // firstly, dump all parameters
258 for (; anObjIt != anObjects.end(); ++ anObjIt) {
259 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIt);
261 dumpParameter(aFeature);
263 // dump all other features
264 for (anObjIt = anObjects.begin(); anObjIt != anObjects.end(); ++anObjIt) {
265 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anObjIt);
266 if (aCompFeat) // iteratively process composite features
267 isOk = process(aCompFeat) && isOk;
268 else if (!isDumped(*anObjIt)) {
270 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(*anObjIt);
274 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIt);
275 if (aFeature) // dump common feature
276 dumpFeature(aFeature);
283 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
286 // increase composite features stack
287 ++gCompositeStackDepth;
288 // dump composite itself
289 if (!isDumped(theComposite) || isForce)
290 dumpFeature(FeaturePtr(theComposite), isForce);
292 // sub-part is processed independently, because it provides separate document
293 if (theComposite->getKind() == PartSetPlugin_Part::ID()) {
294 // dump name of the part if it is different from default
295 if (!myEntitiesStack.empty())
298 // decrease composite features stack because we run into separate document
299 --gCompositeStackDepth;
301 ResultPartPtr aPartResult =
302 std::dynamic_pointer_cast<ModelAPI_ResultPart>(theComposite->lastResult());
305 DocumentPtr aSubDoc = aPartResult->partDoc();
308 // set name of document
309 const std::string& aPartName = myNames[theComposite].myCurrentName;
310 std::string aDocName = aPartName + "_doc";
311 myNames[aSubDoc] = EntityName(aDocName, std::string(), true);
313 // dump document in a separate line
314 *this << aDocName << " = " << aPartName << ".document()" << std::endl;
315 // dump features in the document
316 bool aRes = process(aSubDoc);
317 *this << "model.do()" << std::endl;
322 bool isOk = processSubs(theComposite);
323 // decrease composite features stack
324 --gCompositeStackDepth;
329 bool ModelHighAPI_Dumper::processSubs(
330 const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
334 // dump all sub-features;
335 bool isSubDumped = false;
336 int aNbSubs = theComposite->numberOfSubs();
337 for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) {
338 FeaturePtr aFeature = theComposite->subFeature(anIndex);
339 if (isDumped(aFeature))
343 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
344 if (aCompFeat) // iteratively process composite features
345 isOk = process(aCompFeat) && isOk;
347 dumpFeature(aFeature, true);
350 bool isDumpSetName = !myEntitiesStack.empty() &&
351 myEntitiesStack.top().myEntity == EntityPtr(theComposite);
352 bool isForceModelDo = isSubDumped && isDumpSetName &&
353 (myEntitiesStack.top().myUserName || !myEntitiesStack.top().myResults.empty());
354 // It is necessary for the sketch to create its result when complete (command "model.do()").
355 // This option is set by flat theDumpModelDo.
356 // However, nested sketches are rebuilt by parent feature, so, they do not need
357 // explicit call of "model.do()". This will be controlled by the depth of the stack.
358 if (isForceModelDo || (theDumpModelDo && gCompositeStackDepth <= 1))
359 *this << "model.do()" << std::endl;
361 // dump "setName" for composite feature
367 void ModelHighAPI_Dumper::dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
368 const FeaturePtr& theSubFeature)
370 name(theSubFeature, false);
371 myNames[theSubFeature] = EntityName(theSubFeatureGet, theSubFeature->name(), false);
373 // store results if they have user-defined names or colors
374 std::list<ResultPtr> aResultsWithNameOrColor;
375 const std::list<ResultPtr>& aResults = theSubFeature->results();
376 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
377 for (; aResIt != aResults.end(); ++aResIt) {
378 std::string aResName = (*aResIt)->data()->name();
379 myNames[*aResIt] = EntityName(aResName, aResName, false);
380 aResultsWithNameOrColor.push_back(*aResIt);
383 // store just dumped entity to stack
384 myEntitiesStack.push(LastDumpedEntity(theSubFeature, true, aResultsWithNameOrColor));
389 bool ModelHighAPI_Dumper::exportTo(const std::string& theFileName)
392 OSD_OpenStream(aFile, theFileName.c_str(), std::ofstream::out);
393 if (!aFile.is_open())
397 for (ModulesMap::const_iterator aModIt = myModules.begin();
398 aModIt != myModules.end(); ++aModIt) {
399 aFile << "from " << aModIt->first << " import ";
400 if (aModIt->second.empty() ||
401 aModIt->second.find(std::string()) != aModIt->second.end())
402 aFile << "*"; // import whole module
404 // import specific features
405 std::set<std::string>::const_iterator anObjIt = aModIt->second.begin();
407 for (++anObjIt; anObjIt != aModIt->second.end(); ++anObjIt)
408 aFile << ", " << *anObjIt;
412 if (!myModules.empty())
415 aFile << "from salome.shaper import model" << std::endl << std::endl;
416 aFile << "model.begin()" << std::endl;
418 // dump collected data
419 aFile << myFullDump.str();
420 aFile << myDumpBuffer.str();
423 aFile << "model.end()" << std::endl;
431 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName,
432 const std::string& theObject)
434 myModules[theModuleName].insert(theObject);
437 void ModelHighAPI_Dumper::dumpEntitySetName()
439 const LastDumpedEntity& aLastDumped = myEntitiesStack.top();
441 // dump "setName" for the entity
442 if (aLastDumped.myUserName) {
443 EntityName& anEntityNames = myNames[aLastDumped.myEntity];
444 if (!anEntityNames.myIsDefault)
445 myDumpBuffer << anEntityNames.myCurrentName << ".setName(\""
446 << anEntityNames.myUserName << "\")" << std::endl;
447 // don't dump "setName" for the entity twice
448 anEntityNames.myUserName.clear();
449 anEntityNames.myIsDefault = true;
451 // dump "setName" for results
452 std::list<ResultPtr>::const_iterator aResIt = aLastDumped.myResults.begin();
453 std::list<ResultPtr>::const_iterator aResEnd = aLastDumped.myResults.end();
454 for (; aResIt != aResEnd; ++aResIt) {
456 EntityName& anEntityNames = myNames[*aResIt];
457 if (!anEntityNames.myIsDefault) {
459 myDumpBuffer << ".setName(\"" << anEntityNames.myUserName << "\")" << std::endl;
460 // don't dump "setName" for the entity twice
461 anEntityNames.myUserName.clear();
462 anEntityNames.myIsDefault = true;
465 if (!isDefaultColor(*aResIt)) {
466 AttributeIntArrayPtr aColor = (*aResIt)->data()->intArray(ModelAPI_Result::COLOR_ID());
467 if (aColor && aColor->isInitialized()) {
469 myDumpBuffer << ".setColor(" << aColor->value(0) << ", " << aColor->value(1)
470 << ", " << aColor->value(2) << ")" << std::endl;
473 // set result deflection
474 if (!isDefaultDeflection(*aResIt)) {
475 AttributeDoublePtr aDeflectionAttr =
476 (*aResIt)->data()->real(ModelAPI_Result::DEFLECTION_ID());
477 if(aDeflectionAttr.get() && aDeflectionAttr->isInitialized()) {
479 myDumpBuffer << ".setDeflection(" << aDeflectionAttr->value() << ")" << std::endl;
482 // set result transparency
483 if (!isDefaultTransparency(*aResIt)) {
484 AttributeDoublePtr aTransparencyAttr =
485 (*aResIt)->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
486 if(aTransparencyAttr.get() && aTransparencyAttr->isInitialized()) {
488 myDumpBuffer << ".setTransparency(" << aTransparencyAttr->value() << ")" << std::endl;
493 myNames[aLastDumped.myEntity].myIsDumped = true;
494 myEntitiesStack.pop();
497 bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const
499 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
500 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
501 return aFound != myNames.end() || myFeaturesToSkip.find(aFeature) != myFeaturesToSkip.end();
504 bool ModelHighAPI_Dumper::isDefaultColor(const ResultPtr& theResult) const
506 AttributeIntArrayPtr aColor = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
507 if (!aColor || !aColor->isInitialized())
510 std::string aSection, aName, aDefault;
511 theResult->colorConfigInfo(aSection, aName, aDefault);
513 // dump current color
514 std::ostringstream aColorInfo;
515 aColorInfo << aColor->value(0) << "," << aColor->value(1) << "," << aColor->value(2);
517 return aDefault == aColorInfo.str();
520 bool ModelHighAPI_Dumper::isDefaultDeflection(const ResultPtr& theResult) const
522 AttributeDoublePtr aDeflectionAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
523 if(!aDeflectionAttr || !aDeflectionAttr->isInitialized()) {
527 double aCurrent = aDeflectionAttr->value();
528 double aDefault = -1;
530 bool isConstruction = false;
531 std::string aResultGroup = theResult->groupName();
532 if (aResultGroup == ModelAPI_ResultConstruction::group())
533 isConstruction = true;
534 else if (aResultGroup == ModelAPI_ResultBody::group()) {
535 GeomShapePtr aGeomShape = theResult->shape();
536 if (aGeomShape.get()) {
537 // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
538 // correction of deviation for them should not influence to the application performance
539 GeomAPI_ShapeExplorer anExp(aGeomShape, GeomAPI_Shape::FACE);
540 isConstruction = !anExp.more();
544 aDefault = Config_PropManager::real("Visualization", "construction_deflection");
546 aDefault = Config_PropManager::real("Visualization", "body_deflection");
548 return fabs(aCurrent - aDefault) < 1.e-12;
551 bool ModelHighAPI_Dumper::isDefaultTransparency(const ResultPtr& theResult) const
553 AttributeDoublePtr anAttribute = theResult->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
554 if(!anAttribute || !anAttribute->isInitialized()) {
557 return fabs(anAttribute->value()) < 1.e-12;
560 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
562 myDumpBuffer << theChar;
566 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
568 myDumpBuffer << theString;
572 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
574 myDumpBuffer << theString;
578 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const bool theValue)
580 myDumpBuffer << (theValue ? "True" : "False");
584 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
586 myDumpBuffer << theValue;
590 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
592 myDumpBuffer << theValue;
596 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
598 importModule("GeomAPI", "GeomAPI_Pnt");
599 myDumpBuffer << "GeomAPI_Pnt(" << thePoint->x() << ", "
600 << thePoint->y() << ", " << thePoint->z() << ")";
604 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
606 importModule("GeomAPI", "GeomAPI_Dir");
607 myDumpBuffer << "GeomAPI_Dir(" << theDir->x() << ", "
608 << theDir->y() << ", " << theDir->z() << ")";
612 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
613 const std::shared_ptr<GeomDataAPI_Dir>& theDir)
615 myDumpBuffer << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
619 static void dumpArray(std::ostringstream& theOutput, int theSize,
620 double* theValues, std::string* theTexts)
622 for (int i = 0; i < theSize; ++i) {
625 if (theTexts[i].empty())
626 theOutput << theValues[i];
628 theOutput << "\"" << theTexts[i] << "\"";
632 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
633 const std::shared_ptr<GeomDataAPI_Point>& thePoint)
635 static const int aSize = 3;
636 double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
637 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()};
638 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
642 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
643 const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
645 static const int aSize = 2;
646 double aValues[aSize] = {thePoint->x(), thePoint->y()};
647 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()};
648 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
652 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
653 const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
655 myDumpBuffer << (theAttrBool->value() ? "True" : "False");
659 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
660 const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
662 std::string aText = theAttrInt->text();
664 myDumpBuffer << theAttrInt->value();
666 myDumpBuffer << "\"" << aText << "\"";
670 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
671 const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
673 std::string aText = theAttrReal->text();
675 myDumpBuffer << theAttrReal->value();
677 myDumpBuffer << "\"" << aText << "\"";
681 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
682 const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
684 myDumpBuffer << "\"" << theAttrStr->value() << "\"";
688 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FolderPtr& theFolder)
690 myDumpBuffer << name(theFolder);
691 myNotDumpedFolders.erase(theFolder);
695 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity)
697 myDumpBuffer << name(theEntity);
699 if (!myNames[theEntity].myIsDumped) {
700 bool isUserDefinedName = !myNames[theEntity].myIsDefault;
701 // store results if they have user-defined names or colors
702 std::list<ResultPtr> aResultsWithNameOrColor;
703 const std::list<ResultPtr>& aResults = theEntity->results();
704 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
705 for (; aResIt != aResults.end(); ++aResIt) {
706 if (!myNames[*aResIt].myIsDefault || !isDefaultColor(*aResIt) ||
707 !isDefaultDeflection(*aResIt) || !isDefaultTransparency(*aResIt))
708 aResultsWithNameOrColor.push_back(*aResIt);
710 ResultCompSolidPtr aCompSolid =
711 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aResIt);
713 int aNbSubs = aCompSolid->numberOfSubs();
714 for (int i = 0; i < aNbSubs; ++i) {
715 ResultPtr aCurRes = aCompSolid->subResult(i);
716 if (!myNames[aCurRes].myIsDefault || !isDefaultColor(aCurRes) ||
717 !isDefaultDeflection(aCurRes) || !isDefaultTransparency(aCurRes))
718 aResultsWithNameOrColor.push_back(aCurRes);
722 // store just dumped entity to stack
723 if (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theEntity)
724 myEntitiesStack.push(
725 LastDumpedEntity(theEntity, isUserDefinedName, aResultsWithNameOrColor));
728 // remove entity from the list of not dumped items
729 myNotDumpedEntities.erase(theEntity);
733 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ResultPtr& theResult)
735 FeaturePtr aFeature = ModelAPI_Feature::feature(theResult);
738 std::list<ResultPtr> aResults = aFeature->results();
739 for(std::list<ResultPtr>::const_iterator
740 anIt = aResults.cbegin(); anIt != aResults.cend(); ++anIt, ++anIndex) {
741 if(theResult->isSame(*anIt)) {
745 ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*anIt);
747 int aNbSubs = aCompSolid->numberOfSubs();
748 for (aSubIndex = 0; aSubIndex < aNbSubs; ++aSubIndex)
749 if (theResult->isSame(aCompSolid->subResult(aSubIndex)))
751 if (aSubIndex < aNbSubs)
757 myDumpBuffer << name(aFeature);
759 myDumpBuffer << ".result()";
761 myDumpBuffer << ".results()[" << anIndex << "]";
763 if (aSubIndex >= 0) {
764 myDumpBuffer << ".subResult(" << aSubIndex << ")";
769 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ObjectPtr& theObject)
771 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
773 myDumpBuffer << name(aFeature);
777 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
786 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const AttributePtr& theAttr)
788 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttr->owner());
790 std::string aWrapperPrefix, aWrapperSuffix;
791 // Check the attribute belongs to copied (in multi-translation or multi-rotation) feature.
792 // In this case we need to cast explicitly feature to appropriate type.
793 AttributeBooleanPtr isCopy = anOwner->boolean("Copy");
794 if (isCopy.get() && isCopy->value()) {
795 aWrapperPrefix = featureWrapper(anOwner) + "(";
796 aWrapperSuffix = ")";
797 importModule("SketchAPI");
800 myDumpBuffer << aWrapperPrefix << name(anOwner) << aWrapperSuffix
801 << "." << attributeGetter(anOwner, theAttr->id()) << "()";
805 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
806 const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
808 if (theRefAttr->isObject())
809 *this << theRefAttr->object();
811 *this << theRefAttr->attr();
815 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
816 const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList)
819 std::list<std::pair<ObjectPtr, AttributePtr> > aList = theRefAttrList->list();
820 bool isAdded = false;
821 std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aList.begin();
822 for (; anIt != aList.end(); ++anIt) {
824 myDumpBuffer << ", ";
828 *this << anIt->first;
829 else if (anIt->second)
830 * this << anIt->second;
836 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
837 const std::shared_ptr<ModelAPI_AttributeReference>& theReference)
839 *this << theReference->value();
843 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
844 const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList)
846 static const int aThreshold = 2;
847 // if number of elements in the list if greater than a threshold,
848 // dump it in a separate line with specific name
849 std::string aDumped = myDumpBuffer.str();
850 if (aDumped.empty() || theRefList->size() <= aThreshold) {
852 std::list<ObjectPtr> aList = theRefList->list();
853 bool isAdded = false;
854 std::list<ObjectPtr>::const_iterator anIt = aList.begin();
855 for (; anIt != aList.end(); ++anIt) {
857 myDumpBuffer << ", ";
865 // clear buffer and store list "as is"
866 myDumpBuffer.str("");
868 // save buffer and clear it again
869 std::string aDumpedList = myDumpBuffer.str();
870 myDumpBuffer.str("");
871 // obtain name of list
872 FeaturePtr anOwner = ModelAPI_Feature::feature(theRefList->owner());
873 std::string aListName = name(anOwner) + "_objects";
874 // store all previous data
875 myDumpBuffer << aListName << " = " << aDumpedList << std::endl
876 << aDumped << aListName;
881 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
882 const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
884 myDumpBuffer << "model.selection(";
886 if(!theAttrSelect->isInitialized()) {
891 GeomShapePtr aShape = theAttrSelect->value();
893 aShape = theAttrSelect->context()->shape();
901 myDumpBuffer << "\"" << aShape->shapeTypeStr() << "\", \"" <<
902 theAttrSelect->namingName() << "\")";
906 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
907 const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList)
912 std::string aShapeTypeStr;
914 bool isAdded = false;
916 for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) {
917 AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex);
918 aShape = anAttribute->value();
920 ResultPtr aContext = anAttribute->context();
922 aShape = aContext->shape();
930 myDumpBuffer << ", ";
934 myDumpBuffer << "model.selection(\"" <<
935 aShape->shapeTypeStr() << "\", \"" << anAttribute->namingName() << "\")";
942 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
943 const std::shared_ptr<ModelAPI_AttributeStringArray>& theArray)
946 for(int anIndex = 0; anIndex < theArray->size(); ++anIndex) {
950 myDumpBuffer<<"\""<<theArray->value(anIndex)<<"\"";
958 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
959 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
961 theDumper.myDumpBuffer << theEndl;
963 if (!theDumper.myEntitiesStack.empty()) {
965 // all copies have been stored into stack, pop them all
968 // Name for composite feature is dumped when all sub-entities are dumped
969 // (see method ModelHighAPI_Dumper::processSubs).
970 const ModelHighAPI_Dumper::LastDumpedEntity& aLastDumped = theDumper.myEntitiesStack.top();
971 CompositeFeaturePtr aComposite =
972 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aLastDumped.myEntity);
974 theDumper.dumpEntitySetName();
975 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aLastDumped.myEntity);
977 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
978 isCopy = aCopyAttr.get() && aCopyAttr->value();
984 // store all not-dumped entities first
985 std::set<EntityPtr> aNotDumped = theDumper.myNotDumpedEntities;
986 std::string aBufCopy = theDumper.myDumpBuffer.str();
987 theDumper.clear(true);
988 std::set<EntityPtr>::const_iterator anIt = aNotDumped.begin();
989 for (; anIt != aNotDumped.end(); ++anIt) {
990 // if the feature is composite, dump it with all subs
991 CompositeFeaturePtr aCompFeat =
992 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
994 theDumper.process(aCompFeat, true);
996 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
997 theDumper.dumpFeature(aFeature, true);
998 // dump all referred features for the "Copy"
999 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
1000 if (aCopyAttr.get() && aCopyAttr->value())
1002 const std::set<AttributePtr>& aRefs = aFeature->data()->refsToMe();
1003 std::set<AttributePtr>::iterator aRefIt = aRefs.begin();
1004 for (; aRefIt != aRefs.end(); ++aRefIt)
1006 FeaturePtr anOwner = ModelAPI_Feature::feature((*aRefIt)->owner());
1007 if (anOwner && !theDumper.isDumped(anOwner))
1008 theDumper.dumpFeature(anOwner, true);
1014 // avoid multiple empty lines
1015 size_t anInd = std::string::npos;
1016 while ((anInd = aBufCopy.find("\n\n\n")) != std::string::npos)
1017 aBufCopy.erase(anInd, 1);
1018 // then store currently dumped string
1019 theDumper.myFullDump << aBufCopy;
1021 // now, store all not dumped folders
1022 std::set<FolderPtr>::const_iterator aFolderIt = theDumper.myNotDumpedFolders.begin();
1023 while (aFolderIt != theDumper.myNotDumpedFolders.end()) {
1024 FolderPtr aFolder = *aFolderIt++;
1025 theDumper.dumpFolder(aFolder);