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_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();
100 myDumpPostponedInProgress = false;
104 void ModelHighAPI_Dumper::clearNotDumped()
106 myNotDumpedEntities.clear();
109 // Convert string to integer. If the string is not a number, return -1
110 static int toInt(const std::string& theString)
112 std::string::const_iterator aChar = theString.begin();
113 for (; aChar != theString.end(); ++aChar)
114 if (!std::isdigit(*aChar))
116 if (aChar != theString.end())
117 return -1; // not a number
118 return std::stoi(theString);
121 const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity,
122 bool theSaveNotDumped,
123 bool theUseEntityName)
125 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
126 if (aFound != myNames.end())
127 return aFound->second.myCurrentName;
129 // entity is not found, store it
130 std::string aName, aKind;
131 bool isDefaultName = false;
132 bool isSaveNotDumped = theSaveNotDumped;
133 std::ostringstream aDefaultName;
134 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
136 aName = aFeature->name();
137 aKind = aFeature->getKind();
139 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(theEntity);
141 aName = aFolder->data()->name();
142 aKind = ModelAPI_Folder::ID();
143 isSaveNotDumped = false;
147 ObjectPtr anObject = std::dynamic_pointer_cast<ModelAPI_Object>(theEntity);
149 DocumentPtr aDoc = anObject->document();
150 std::pair<int, int>& aNbFeatures = myFeatureCount[aDoc][aKind];
151 aNbFeatures.first += 1;
153 size_t anIndex = aName.find(aKind);
154 if (anIndex == 0 && aName[aKind.length()] == '_') { // name starts with "FeatureKind_"
155 std::string anIdStr = aName.substr(aKind.length() + 1);
156 int anId = toInt(anIdStr);
158 // Check number of already registered objects of such kind. Index of current object
159 // should be the same to identify feature's name as automatically generated.
160 if (aNbFeatures.first == anId && aNbFeatures.second < anId) {
161 // name is not user-defined
162 isDefaultName = true;
165 if (anId > aNbFeatures.second)
166 aNbFeatures.second = anId;
169 // obtain default name for the feature
170 if (theUseEntityName)
171 aDefaultName << aName;
174 NbFeaturesMap::const_iterator aFIt = myFeatureCount.begin();
175 for (; aFIt != myFeatureCount.end(); ++aFIt) {
176 std::map<std::string, std::pair<int, int> >::const_iterator aFound =
177 aFIt->second.find(aKind);
178 if (aFound != aFIt->second.end())
179 aFullIndex += aFound->second.first;
181 aDefaultName << aKind << "_" << aFullIndex;
185 myNames[theEntity] = EntityName(aDefaultName.str(), aName, isDefaultName);
187 myNotDumpedEntities.insert(theEntity);
189 // store names of results
191 saveResultNames(aFeature);
193 return myNames[theEntity].myCurrentName;
196 const std::string& ModelHighAPI_Dumper::parentName(const FeaturePtr& theEntity)
198 const std::set<AttributePtr>& aRefs = theEntity->data()->refsToMe();
199 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
200 for (; aRefIt != aRefs.end(); ++aRefIt) {
201 CompositeFeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(
202 ModelAPI_Feature::feature((*aRefIt)->owner()));
204 return name(anOwner);
207 static const std::string DUMMY;
211 void ModelHighAPI_Dumper::saveResultNames(const FeaturePtr& theFeature)
213 // Default name of the feature
214 bool isFeatureDefaultName = myNames[theFeature].myIsDefault;
216 // Save only names of results which is not correspond to default feature name
217 const std::list<ResultPtr>& aResults = theFeature->results();
218 std::list<ResultPtr> allRes;
219 ModelAPI_Tools::allResults(theFeature, allRes);
220 for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
221 std::pair<std::string, bool> aName = ModelAPI_Tools::getDefaultName(*aRes);
222 std::string aDefaultName = aName.first;
223 std::string aResName = (*aRes)->data()->name();
224 bool isUserDefined = !(isFeatureDefaultName && aDefaultName == aResName);
226 EntityName(aResName, (isUserDefined ? aResName : std::string()), !isUserDefined);
230 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc,
231 const std::string& theFileName)
233 // dump top level document feature
234 static const std::string aDocName("partSet");
235 myNames[theDoc] = EntityName(aDocName, std::string(), true);
236 *this << aDocName << " = model.moduleDocument()" << std::endl;
238 // dump subfeatures and store result to file
239 return process(theDoc) && exportTo(theFileName);
242 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc)
245 std::list<ObjectPtr> anObjects = theDoc->allObjects();
246 std::list<ObjectPtr>::const_iterator anObjIt = anObjects.begin();
247 // firstly, dump all parameters
248 for (; anObjIt != anObjects.end(); ++ anObjIt) {
249 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIt);
251 dumpParameter(aFeature);
253 // dump all other features
254 for (anObjIt = anObjects.begin(); anObjIt != anObjects.end(); ++anObjIt) {
255 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anObjIt);
256 if (aCompFeat) // iteratively process composite features
257 isOk = process(aCompFeat) && isOk;
258 else if (!isDumped(EntityPtr(*anObjIt))) {
260 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(*anObjIt);
264 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIt);
265 if (aFeature) // dump common feature
266 dumpFeature(aFeature);
270 // dump folders if any
275 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
278 // increase composite features stack
279 ++gCompositeStackDepth;
280 // dump composite itself
281 if (!isDumped(EntityPtr(theComposite)) || isForce)
282 dumpFeature(FeaturePtr(theComposite), isForce);
284 // sub-part is processed independently, because it provides separate document
285 if (theComposite->getKind() == PartSetPlugin_Part::ID()) {
286 // dump name of the part if it is different from default
287 if (!myEntitiesStack.empty())
290 // decrease composite features stack because we run into separate document
291 --gCompositeStackDepth;
293 ResultPartPtr aPartResult =
294 std::dynamic_pointer_cast<ModelAPI_ResultPart>(theComposite->lastResult());
297 DocumentPtr aSubDoc = aPartResult->partDoc();
300 // set name of document
301 const std::string& aPartName = myNames[theComposite].myCurrentName;
302 std::string aDocName = aPartName + "_doc";
303 myNames[aSubDoc] = EntityName(aDocName, std::string(), true);
305 // dump document in a separate line
306 *this << aDocName << " = " << aPartName << ".document()" << std::endl;
307 // dump features in the document
308 bool aRes = process(aSubDoc);
309 *this << "model.do()" << std::endl;
314 bool isOk = processSubs(theComposite);
315 // decrease composite features stack
316 --gCompositeStackDepth;
321 bool ModelHighAPI_Dumper::processSubs(
322 const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
326 // dump all sub-features;
327 bool isSubDumped = false;
328 int aNbSubs = theComposite->numberOfSubs();
329 for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) {
330 FeaturePtr aFeature = theComposite->subFeature(anIndex);
331 if (isDumped(EntityPtr(aFeature)))
335 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
336 if (aCompFeat) // iteratively process composite features
337 isOk = process(aCompFeat) && isOk;
339 dumpFeature(aFeature, true);
342 bool isDumpSetName = !myEntitiesStack.empty() &&
343 myEntitiesStack.top().myEntity == EntityPtr(theComposite);
344 bool isForceModelDo = isSubDumped && isDumpSetName &&
345 (myEntitiesStack.top().myUserName || !myEntitiesStack.top().myResults.empty());
346 // It is necessary for the sketch to create its result when complete (command "model.do()").
347 // This option is set by flat theDumpModelDo.
348 // However, nested sketches are rebuilt by parent feature, so, they do not need
349 // explicit call of "model.do()". This will be controlled by the depth of the stack.
350 if (isForceModelDo || (theDumpModelDo && gCompositeStackDepth <= 1))
351 *this << "model.do()" << std::endl;
353 // dump "setName" for composite feature
359 void ModelHighAPI_Dumper::postpone(const EntityPtr& theEntity)
362 name(theEntity, false);
363 myPostponed.push_back(theEntity);
366 void ModelHighAPI_Dumper::dumpPostponed(bool theDumpFolders)
368 if (myDumpPostponedInProgress)
371 myDumpPostponedInProgress = true;
372 // make a copy of postponed entities, because the list will be updated
373 // if some features are not able to be dumped
374 std::list<EntityPtr> aPostponedCopy = myPostponed;
377 // iterate over postponed entities and try to dump them
378 std::list<EntityPtr>::const_iterator anIt = aPostponedCopy.begin();
379 for (; anIt != aPostponedCopy.end(); ++anIt) {
380 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(*anIt);
385 myPostponed.push_back(*anIt);
388 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
390 dumpFeature(aFeature, true);
393 myDumpPostponedInProgress = false;
396 void ModelHighAPI_Dumper::dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
397 const FeaturePtr& theSubFeature)
399 name(theSubFeature, false);
400 myNames[theSubFeature] = EntityName(theSubFeatureGet, theSubFeature->name(), false);
402 // store results if they have user-defined names or colors
403 std::list<ResultPtr> aResultsWithNameOrColor;
404 const std::list<ResultPtr>& aResults = theSubFeature->results();
405 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
406 for (; aResIt != aResults.end(); ++aResIt) {
407 std::string aResName = (*aResIt)->data()->name();
408 myNames[*aResIt] = EntityName(aResName, aResName, false);
409 aResultsWithNameOrColor.push_back(*aResIt);
412 // store just dumped entity to stack
413 myEntitiesStack.push(LastDumpedEntity(theSubFeature, true, aResultsWithNameOrColor));
418 bool ModelHighAPI_Dumper::exportTo(const std::string& theFileName)
421 OSD_OpenStream(aFile, theFileName.c_str(), std::ofstream::out);
422 if (!aFile.is_open())
425 // standard header (encoding + imported modules)
426 aFile << "# -*- coding: utf-8 -*-" << std::endl << std::endl;
427 for (ModulesMap::const_iterator aModIt = myModules.begin();
428 aModIt != myModules.end(); ++aModIt) {
429 aFile << "from " << aModIt->first << " import ";
430 if (aModIt->second.empty() ||
431 aModIt->second.find(std::string()) != aModIt->second.end())
432 aFile << "*"; // import whole module
434 // import specific features
435 std::set<std::string>::const_iterator anObjIt = aModIt->second.begin();
437 for (++anObjIt; anObjIt != aModIt->second.end(); ++anObjIt)
438 aFile << ", " << *anObjIt;
442 if (!myModules.empty())
445 aFile << "from salome.shaper import model" << std::endl << std::endl;
446 aFile << "model.begin()" << std::endl;
448 // dump collected data
449 aFile << myFullDump.str();
450 aFile << myDumpBuffer.str();
453 aFile << "model.end()" << std::endl;
461 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName,
462 const std::string& theObject)
464 myModules[theModuleName].insert(theObject);
467 void ModelHighAPI_Dumper::dumpEntitySetName()
469 const LastDumpedEntity& aLastDumped = myEntitiesStack.top();
470 bool isBufferEmpty = myDumpBuffer.str().empty();
472 // dump "setName" for the entity
473 if (aLastDumped.myUserName) {
474 EntityName& anEntityNames = myNames[aLastDumped.myEntity];
475 if (!anEntityNames.myIsDefault)
476 myDumpBuffer << anEntityNames.myCurrentName << ".setName(\""
477 << anEntityNames.myUserName << "\")" << std::endl;
478 // don't dump "setName" for the entity twice
479 anEntityNames.myUserName.clear();
480 anEntityNames.myIsDefault = true;
482 // dump "setName" for results
483 std::list<ResultPtr>::const_iterator aResIt = aLastDumped.myResults.begin();
484 std::list<ResultPtr>::const_iterator aResEnd = aLastDumped.myResults.end();
485 for (; aResIt != aResEnd; ++aResIt) {
487 EntityName& anEntityNames = myNames[*aResIt];
488 if (!anEntityNames.myIsDefault) {
490 myDumpBuffer << ".setName(\"" << anEntityNames.myUserName << "\")" << std::endl;
491 // don't dump "setName" for the entity twice
492 anEntityNames.myUserName.clear();
493 anEntityNames.myIsDefault = true;
496 if (!isDefaultColor(*aResIt)) {
497 AttributeIntArrayPtr aColor = (*aResIt)->data()->intArray(ModelAPI_Result::COLOR_ID());
498 if (aColor && aColor->isInitialized()) {
500 myDumpBuffer << ".setColor(" << aColor->value(0) << ", " << aColor->value(1)
501 << ", " << aColor->value(2) << ")" << std::endl;
504 // set result deflection
505 if (!isDefaultDeflection(*aResIt)) {
506 AttributeDoublePtr aDeflectionAttr =
507 (*aResIt)->data()->real(ModelAPI_Result::DEFLECTION_ID());
508 if(aDeflectionAttr.get() && aDeflectionAttr->isInitialized()) {
510 myDumpBuffer << ".setDeflection(" << aDeflectionAttr->value() << ")" << std::endl;
513 // set result transparency
514 if (!isDefaultTransparency(*aResIt)) {
515 AttributeDoublePtr aTransparencyAttr =
516 (*aResIt)->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
517 if(aTransparencyAttr.get() && aTransparencyAttr->isInitialized()) {
519 myDumpBuffer << ".setTransparency(" << aTransparencyAttr->value() << ")" << std::endl;
524 myNames[aLastDumped.myEntity].myIsDumped = true;
525 myEntitiesStack.pop();
527 // clean buffer if it was clear before
529 myFullDump << myDumpBuffer.str();
530 myDumpBuffer.str("");
534 bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const
536 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
537 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
538 return (aFound != myNames.end() && aFound->second.myIsDumped) ||
539 myFeaturesToSkip.find(aFeature) != myFeaturesToSkip.end();
542 bool ModelHighAPI_Dumper::isDumped(const AttributeRefAttrPtr& theRefAttr) const
545 if (theRefAttr->isObject())
546 aFeature = ModelAPI_Feature::feature(theRefAttr->object());
548 aFeature = ModelAPI_Feature::feature(theRefAttr->attr()->owner());
549 return aFeature && isDumped(EntityPtr(aFeature));
552 bool ModelHighAPI_Dumper::isDumped(const AttributeRefListPtr& theRefList) const
554 std::list<ObjectPtr> aRefs = theRefList->list();
555 std::list<ObjectPtr>::iterator anIt = aRefs.begin();
556 for (; anIt != aRefs.end(); ++anIt) {
557 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
558 if (aFeature && !isDumped(EntityPtr(aFeature)))
564 bool ModelHighAPI_Dumper::isDefaultColor(const ResultPtr& theResult) const
566 AttributeIntArrayPtr aColor = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
567 if (!aColor || !aColor->isInitialized())
570 std::string aSection, aName, aDefault;
571 theResult->colorConfigInfo(aSection, aName, aDefault);
573 // dump current color
574 std::ostringstream aColorInfo;
575 aColorInfo << aColor->value(0) << "," << aColor->value(1) << "," << aColor->value(2);
577 return aDefault == aColorInfo.str();
580 bool ModelHighAPI_Dumper::isDefaultDeflection(const ResultPtr& theResult) const
582 AttributeDoublePtr aDeflectionAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
583 if(!aDeflectionAttr || !aDeflectionAttr->isInitialized()) {
587 double aCurrent = aDeflectionAttr->value();
588 double aDefault = -1;
590 bool isConstruction = false;
591 std::string aResultGroup = theResult->groupName();
592 if (aResultGroup == ModelAPI_ResultConstruction::group())
593 isConstruction = true;
594 else if (aResultGroup == ModelAPI_ResultBody::group()) {
595 GeomShapePtr aGeomShape = theResult->shape();
596 if (aGeomShape.get()) {
597 // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
598 // correction of deviation for them should not influence to the application performance
599 GeomAPI_ShapeExplorer anExp(aGeomShape, GeomAPI_Shape::FACE);
600 isConstruction = !anExp.more();
604 aDefault = Config_PropManager::real("Visualization", "construction_deflection");
606 aDefault = Config_PropManager::real("Visualization", "body_deflection");
608 return fabs(aCurrent - aDefault) < 1.e-12;
611 bool ModelHighAPI_Dumper::isDefaultTransparency(const ResultPtr& theResult) const
613 AttributeDoublePtr anAttribute = theResult->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
614 if(!anAttribute || !anAttribute->isInitialized()) {
617 return fabs(anAttribute->value()) < 1.e-12;
620 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
622 myDumpBuffer << theChar;
626 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
628 myDumpBuffer << theString;
632 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
634 myDumpBuffer << theString;
638 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const bool theValue)
640 myDumpBuffer << (theValue ? "True" : "False");
644 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
646 myDumpBuffer << theValue;
650 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
652 myDumpBuffer << theValue;
656 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
658 importModule("GeomAPI", "GeomAPI_Pnt");
659 myDumpBuffer << "GeomAPI_Pnt(" << thePoint->x() << ", "
660 << thePoint->y() << ", " << thePoint->z() << ")";
664 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
666 importModule("GeomAPI", "GeomAPI_Dir");
667 myDumpBuffer << "GeomAPI_Dir(" << theDir->x() << ", "
668 << theDir->y() << ", " << theDir->z() << ")";
672 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
673 const std::shared_ptr<GeomDataAPI_Dir>& theDir)
675 myDumpBuffer << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
679 static void dumpArray(std::ostringstream& theOutput, int theSize,
680 double* theValues, std::string* theTexts)
682 for (int i = 0; i < theSize; ++i) {
685 if (theTexts[i].empty())
686 theOutput << theValues[i];
688 theOutput << "\"" << theTexts[i] << "\"";
692 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
693 const std::shared_ptr<GeomDataAPI_Point>& thePoint)
695 static const int aSize = 3;
696 double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
697 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()};
698 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
702 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
703 const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
705 static const int aSize = 2;
706 double aValues[aSize] = {thePoint->x(), thePoint->y()};
707 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()};
708 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
712 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
713 const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
715 myDumpBuffer << (theAttrBool->value() ? "True" : "False");
719 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
720 const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
722 std::string aText = theAttrInt->text();
724 myDumpBuffer << theAttrInt->value();
726 myDumpBuffer << "\"" << aText << "\"";
730 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
731 const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
733 std::string aText = theAttrReal->text();
735 myDumpBuffer << theAttrReal->value();
737 myDumpBuffer << "\"" << aText << "\"";
741 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
742 const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
744 myDumpBuffer << "\"" << theAttrStr->value() << "\"";
748 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FolderPtr& theFolder)
750 myDumpBuffer << name(theFolder);
752 // add dumped folder to a stack
753 if (!myNames[theFolder].myIsDumped &&
754 (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theFolder))
755 myEntitiesStack.push(LastDumpedEntity(theFolder, !myNames[theFolder].myIsDefault));
760 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity)
762 myDumpBuffer << name(theEntity);
764 if (!myNames[theEntity].myIsDumped) {
765 bool isUserDefinedName = !myNames[theEntity].myIsDefault;
766 // store results if they have user-defined names or colors
767 std::list<ResultPtr> aResultsWithNameOrColor;
768 std::list<ResultPtr> allRes;
769 ModelAPI_Tools::allResults(theEntity, allRes);
770 for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
771 if(!myNames[*aRes].myIsDefault || !isDefaultColor(*aRes) ||
772 !isDefaultDeflection(*aRes) || !isDefaultTransparency(*aRes))
773 aResultsWithNameOrColor.push_back(*aRes);
775 // store just dumped entity to stack
776 if (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theEntity)
777 myEntitiesStack.push(
778 LastDumpedEntity(theEntity, isUserDefinedName, aResultsWithNameOrColor));
781 // remove entity from the list of not dumped items
782 myNotDumpedEntities.erase(theEntity);
786 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ResultPtr& theResult)
788 // iterate in the structure of sub-results to the parent
789 ResultPtr aCurRes = theResult;
790 std::list<int> anIndices; // indexes of results in the parent result, starting from topmost
791 while(aCurRes.get()) {
792 ResultBodyPtr aParent = ModelAPI_Tools::bodyOwner(aCurRes);
794 anIndices.push_front(ModelAPI_Tools::bodyIndex(aCurRes));
799 FeaturePtr aFeature = ModelAPI_Feature::feature(theResult);
800 myDumpBuffer << name(aFeature);
801 for (std::list<int>::iterator anI = anIndices.begin(); anI != anIndices.end(); anI++) {
802 if (anI == anIndices.begin()) {
804 myDumpBuffer << ".result()";
807 myDumpBuffer << ".results()[" << *anI << "]";
810 myDumpBuffer << ".subResult(" << *anI << ")";
817 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ObjectPtr& theObject)
819 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
821 myDumpBuffer << name(aFeature);
825 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
834 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const AttributePtr& theAttr)
836 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttr->owner());
838 std::string aWrapperPrefix, aWrapperSuffix;
839 // Check the attribute belongs to copied (in multi-translation or multi-rotation) feature.
840 // In this case we need to cast explicitly feature to appropriate type.
841 AttributeBooleanPtr isCopy = anOwner->boolean("Copy");
842 if (isCopy.get() && isCopy->value()) {
843 aWrapperPrefix = featureWrapper(anOwner) + "(";
844 aWrapperSuffix = ")";
845 importModule("SketchAPI");
848 myDumpBuffer << aWrapperPrefix << name(anOwner) << aWrapperSuffix
849 << "." << attributeGetter(anOwner, theAttr->id()) << "()";
853 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
854 const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
856 if (theRefAttr->isObject())
857 *this << theRefAttr->object();
859 *this << theRefAttr->attr();
863 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
864 const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList)
867 std::list<std::pair<ObjectPtr, AttributePtr> > aList = theRefAttrList->list();
868 bool isAdded = false;
869 std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aList.begin();
870 for (; anIt != aList.end(); ++anIt) {
872 myDumpBuffer << ", ";
876 *this << anIt->first;
877 else if (anIt->second)
878 * this << anIt->second;
884 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
885 const std::shared_ptr<ModelAPI_AttributeReference>& theReference)
887 *this << theReference->value();
891 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
892 const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList)
894 static const int aThreshold = 2;
895 // if number of elements in the list if greater than a threshold,
896 // dump it in a separate line with specific name
897 std::string aDumped = myDumpBuffer.str();
898 if (aDumped.empty() || theRefList->size() <= aThreshold) {
900 std::list<ObjectPtr> aList = theRefList->list();
901 bool isAdded = false;
902 std::list<ObjectPtr>::const_iterator anIt = aList.begin();
903 for (; anIt != aList.end(); ++anIt) {
905 myDumpBuffer << ", ";
913 // clear buffer and store list "as is"
914 myDumpBuffer.str("");
916 // save buffer and clear it again
917 std::string aDumpedList = myDumpBuffer.str();
918 myDumpBuffer.str("");
919 // obtain name of list
920 FeaturePtr anOwner = ModelAPI_Feature::feature(theRefList->owner());
921 std::string aListName = name(anOwner) + "_objects";
922 // store all previous data
923 myDumpBuffer << aListName << " = " << aDumpedList << std::endl
924 << aDumped << aListName;
929 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
930 const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
932 myDumpBuffer << "model.selection(";
934 if(!theAttrSelect->isInitialized()) {
939 GeomShapePtr aShape = theAttrSelect->value();
941 aShape = theAttrSelect->context()->shape();
949 myDumpBuffer << "\"" << aShape->shapeTypeStr() << "\", \"" <<
950 theAttrSelect->namingName() << "\")";
954 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
955 const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList)
957 static const int aThreshold = 2;
958 // if number of elements in the list if greater than a threshold,
959 // dump it in a separate line with specific name
960 std::string aDumped = myDumpBuffer.str();
962 if (aDumped.empty() || theAttrSelList->size() <= aThreshold) {
966 std::string aShapeTypeStr;
968 bool isAdded = false;
970 for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) {
971 AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex);
972 aShape = anAttribute->value();
974 ResultPtr aContext = anAttribute->context();
976 aShape = aContext->shape();
984 myDumpBuffer << ", ";
988 myDumpBuffer << "model.selection(\"" <<
989 aShape->shapeTypeStr() << "\", \"" << anAttribute->namingName() << "\")";
994 // clear buffer and store list "as is"
995 myDumpBuffer.str("");
996 *this << theAttrSelList;
997 // save buffer and clear it again
998 std::string aDumpedList = myDumpBuffer.str();
999 myDumpBuffer.str("");
1000 // obtain name of list (the feature may contain several selection lists)
1001 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttrSelList->owner());
1002 std::string aListName = name(anOwner) + "_objects";
1003 std::list<AttributePtr> aSelLists =
1004 anOwner->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
1005 if (aSelLists.size() > 1) {
1007 for (std::list<AttributePtr>::iterator aSIt = aSelLists.begin();
1008 aSIt != aSelLists.end(); ++aSIt, ++anIndex)
1009 if ((*aSIt).get() == theAttrSelList.get())
1011 std::ostringstream aSStream;
1012 aSStream << aListName << "_" << anIndex;
1013 aListName = aSStream.str();
1015 // store all previous data
1016 myDumpBuffer << aListName << " = " << aDumpedList << std::endl
1017 << aDumped << aListName;
1022 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1023 const std::shared_ptr<ModelAPI_AttributeStringArray>& theArray)
1026 for(int anIndex = 0; anIndex < theArray->size(); ++anIndex) {
1030 myDumpBuffer<<"\""<<theArray->value(anIndex)<<"\"";
1038 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
1039 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
1041 theDumper.myDumpBuffer << theEndl;
1043 if (!theDumper.myEntitiesStack.empty()) {
1045 // all copies have been stored into stack, pop them all
1048 // Name for composite feature is dumped when all sub-entities are dumped
1049 // (see method ModelHighAPI_Dumper::processSubs).
1050 const ModelHighAPI_Dumper::LastDumpedEntity& aLastDumped = theDumper.myEntitiesStack.top();
1051 CompositeFeaturePtr aComposite =
1052 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aLastDumped.myEntity);
1054 theDumper.dumpEntitySetName();
1055 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aLastDumped.myEntity);
1057 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
1058 isCopy = aCopyAttr.get() && aCopyAttr->value();
1061 } while (isCopy && !theDumper.myEntitiesStack.empty());
1064 // store all not-dumped entities first
1065 std::set<EntityPtr> aNotDumped = theDumper.myNotDumpedEntities;
1066 std::string aBufCopy = theDumper.myDumpBuffer.str();
1067 theDumper.clear(true);
1068 std::set<EntityPtr>::const_iterator anIt = aNotDumped.begin();
1069 for (; anIt != aNotDumped.end(); ++anIt) {
1070 // if the feature is composite, dump it with all subs
1071 CompositeFeaturePtr aCompFeat =
1072 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
1074 theDumper.process(aCompFeat, true);
1076 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
1077 theDumper.dumpFeature(aFeature, true);
1078 // dump the Projection feature which produces this "Copy" entity
1079 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
1080 if (aCopyAttr.get() && aCopyAttr->value())
1082 const std::set<AttributePtr>& aRefs = aFeature->data()->refsToMe();
1083 std::set<AttributePtr>::iterator aRefIt = aRefs.begin();
1084 for (; aRefIt != aRefs.end(); ++aRefIt)
1085 if ((*aRefIt)->id() == "ProjectedFeature")
1086 { // process projection only
1087 FeaturePtr anOwner = ModelAPI_Feature::feature((*aRefIt)->owner());
1088 if (anOwner && !theDumper.isDumped(EntityPtr(anOwner)))
1089 theDumper.dumpFeature(anOwner, true);
1095 // avoid multiple empty lines
1096 size_t anInd = std::string::npos;
1097 while ((anInd = aBufCopy.find("\n\n\n")) != std::string::npos)
1098 aBufCopy.erase(anInd, 1);
1099 // then store currently dumped string
1100 theDumper.myFullDump << aBufCopy;
1102 // now, store all postponed features
1103 theDumper.dumpPostponed();