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();
101 myDumpPostponedInProgress = false;
105 void ModelHighAPI_Dumper::clearNotDumped()
107 myNotDumpedEntities.clear();
110 // Convert string to integer. If the string is not a number, return -1
111 static int toInt(const std::string& theString)
113 std::string::const_iterator aChar = theString.begin();
114 for (; aChar != theString.end(); ++aChar)
115 if (!std::isdigit(*aChar))
117 if (aChar != theString.end())
118 return -1; // not a number
119 return std::stoi(theString);
122 const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity,
123 bool theSaveNotDumped,
124 bool theUseEntityName)
126 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
127 if (aFound != myNames.end())
128 return aFound->second.myCurrentName;
130 // entity is not found, store it
131 std::string aName, aKind;
132 bool isDefaultName = false;
133 bool isSaveNotDumped = theSaveNotDumped;
134 std::ostringstream aDefaultName;
135 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
137 aName = aFeature->name();
138 aKind = aFeature->getKind();
140 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(theEntity);
142 aName = aFolder->data()->name();
143 aKind = ModelAPI_Folder::ID();
144 isSaveNotDumped = false;
148 ObjectPtr anObject = std::dynamic_pointer_cast<ModelAPI_Object>(theEntity);
150 DocumentPtr aDoc = anObject->document();
151 int& aNbFeatures = myFeatureCount[aDoc][aKind];
154 size_t anIndex = aName.find(aKind);
155 if (anIndex == 0 && aName[aKind.length()] == '_') { // name starts with "FeatureKind_"
156 std::string anIdStr = aName.substr(aKind.length() + 1);
157 int anId = toInt(anIdStr);
159 // Check number of already registered objects of such kind. Index of current object
160 // should be the same to identify feature's name as automatically generated.
161 if (aNbFeatures == anId) {
162 // name is not user-defined
163 isDefaultName = true;
167 // obtain default name for the feature
168 if (theUseEntityName)
169 aDefaultName << aName;
172 NbFeaturesMap::const_iterator aFIt = myFeatureCount.begin();
173 for (; aFIt != myFeatureCount.end(); ++aFIt) {
174 std::map<std::string, int>::const_iterator aFound = aFIt->second.find(aKind);
175 if (aFound != aFIt->second.end())
176 aFullIndex += aFound->second;
178 aDefaultName << aKind << "_" << aFullIndex;
182 myNames[theEntity] = EntityName(aDefaultName.str(), aName, isDefaultName);
184 myNotDumpedEntities.insert(theEntity);
186 // store names of results
188 saveResultNames(aFeature);
190 return myNames[theEntity].myCurrentName;
193 const std::string& ModelHighAPI_Dumper::parentName(const FeaturePtr& theEntity)
195 const std::set<AttributePtr>& aRefs = theEntity->data()->refsToMe();
196 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
197 for (; aRefIt != aRefs.end(); ++aRefIt) {
198 CompositeFeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(
199 ModelAPI_Feature::feature((*aRefIt)->owner()));
201 return name(anOwner);
204 static const std::string DUMMY;
208 void ModelHighAPI_Dumper::saveResultNames(const FeaturePtr& theFeature)
210 // Default name of the feature
211 bool isFeatureDefaultName = myNames[theFeature].myIsDefault;
213 // Save only names of results which is not correspond to default feature name
214 const std::list<ResultPtr>& aResults = theFeature->results();
215 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
216 for (int i = 0; aResIt != aResults.end(); ++aResIt, ++i) {
217 std::pair<std::string, bool> aName = ModelAPI_Tools::getDefaultName(*aResIt, i);
218 std::string aDefaultName = aName.first;
219 std::string aResName = (*aResIt)->data()->name();
221 bool isUserDefined = !(isFeatureDefaultName && aDefaultName == aResName);
223 myNames[*aResIt] = EntityName(aResName,
224 (isUserDefined ? aResName : std::string()), !isUserDefined);
226 // check names of sub-results for CompSolid
227 ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aResIt);
229 int aNbSubs = aCompSolid->numberOfSubs();
230 for (int j = 0; j < aNbSubs; ++j) {
231 ResultPtr aSub = aCompSolid->subResult(j);
232 std::string aSubName = aSub->data()->name();
233 aName = ModelAPI_Tools::getDefaultName(aSub, j);
234 aDefaultName = aName.first;
236 bool isUserDefinedSubName = isUserDefined || aDefaultName != aSubName;
237 myNames[aSub] = EntityName(aSubName,
238 (isUserDefinedSubName ? aSubName : std::string()), !isUserDefinedSubName);
244 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc,
245 const std::string& theFileName)
247 // dump top level document feature
248 static const std::string aDocName("partSet");
249 myNames[theDoc] = EntityName(aDocName, std::string(), true);
250 *this << aDocName << " = model.moduleDocument()" << std::endl;
252 // dump subfeatures and store result to file
253 return process(theDoc) && exportTo(theFileName);
256 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc)
259 std::list<ObjectPtr> anObjects = theDoc->allObjects();
260 std::list<ObjectPtr>::const_iterator anObjIt = anObjects.begin();
261 // firstly, dump all parameters
262 for (; anObjIt != anObjects.end(); ++ anObjIt) {
263 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIt);
265 dumpParameter(aFeature);
267 // dump all other features
268 for (anObjIt = anObjects.begin(); anObjIt != anObjects.end(); ++anObjIt) {
269 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anObjIt);
270 if (aCompFeat) // iteratively process composite features
271 isOk = process(aCompFeat) && isOk;
272 else if (!isDumped(EntityPtr(*anObjIt))) {
274 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(*anObjIt);
278 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIt);
279 if (aFeature) // dump common feature
280 dumpFeature(aFeature);
287 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
290 // increase composite features stack
291 ++gCompositeStackDepth;
292 // dump composite itself
293 if (!isDumped(EntityPtr(theComposite)) || isForce)
294 dumpFeature(FeaturePtr(theComposite), isForce);
296 // sub-part is processed independently, because it provides separate document
297 if (theComposite->getKind() == PartSetPlugin_Part::ID()) {
298 // dump name of the part if it is different from default
299 if (!myEntitiesStack.empty())
302 // decrease composite features stack because we run into separate document
303 --gCompositeStackDepth;
305 ResultPartPtr aPartResult =
306 std::dynamic_pointer_cast<ModelAPI_ResultPart>(theComposite->lastResult());
309 DocumentPtr aSubDoc = aPartResult->partDoc();
312 // set name of document
313 const std::string& aPartName = myNames[theComposite].myCurrentName;
314 std::string aDocName = aPartName + "_doc";
315 myNames[aSubDoc] = EntityName(aDocName, std::string(), true);
317 // dump document in a separate line
318 *this << aDocName << " = " << aPartName << ".document()" << std::endl;
319 // dump features in the document
320 bool aRes = process(aSubDoc);
321 *this << "model.do()" << std::endl;
326 bool isOk = processSubs(theComposite);
327 // decrease composite features stack
328 --gCompositeStackDepth;
333 bool ModelHighAPI_Dumper::processSubs(
334 const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
338 // dump all sub-features;
339 bool isSubDumped = false;
340 int aNbSubs = theComposite->numberOfSubs();
341 for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) {
342 FeaturePtr aFeature = theComposite->subFeature(anIndex);
343 if (isDumped(EntityPtr(aFeature)))
347 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
348 if (aCompFeat) // iteratively process composite features
349 isOk = process(aCompFeat) && isOk;
351 dumpFeature(aFeature, true);
354 bool isDumpSetName = !myEntitiesStack.empty() &&
355 myEntitiesStack.top().myEntity == EntityPtr(theComposite);
356 bool isForceModelDo = isSubDumped && isDumpSetName &&
357 (myEntitiesStack.top().myUserName || !myEntitiesStack.top().myResults.empty());
358 // It is necessary for the sketch to create its result when complete (command "model.do()").
359 // This option is set by flat theDumpModelDo.
360 // However, nested sketches are rebuilt by parent feature, so, they do not need
361 // explicit call of "model.do()". This will be controlled by the depth of the stack.
362 if (isForceModelDo || (theDumpModelDo && gCompositeStackDepth <= 1))
363 *this << "model.do()" << std::endl;
365 // dump "setName" for composite feature
368 // dump folders if any
373 void ModelHighAPI_Dumper::postpone(const EntityPtr& theEntity)
376 name(theEntity, false);
377 myPostponed.push_back(theEntity);
380 void ModelHighAPI_Dumper::dumpPostponed()
382 if (myDumpPostponedInProgress)
385 myDumpPostponedInProgress = true;
386 // make a copy of postponed entities, because the list will be updated
387 // if some features are not able to be dumped
388 std::list<EntityPtr> aPostponedCopy = myPostponed;
391 // iterate over postponed entities and try to dump them
392 std::list<EntityPtr>::const_iterator anIt = aPostponedCopy.begin();
393 for (; anIt != aPostponedCopy.end(); ++anIt) {
394 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(*anIt);
398 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
400 dumpFeature(aFeature);
403 myDumpPostponedInProgress = false;
406 void ModelHighAPI_Dumper::dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
407 const FeaturePtr& theSubFeature)
409 name(theSubFeature, false);
410 myNames[theSubFeature] = EntityName(theSubFeatureGet, theSubFeature->name(), false);
412 // store results if they have user-defined names or colors
413 std::list<ResultPtr> aResultsWithNameOrColor;
414 const std::list<ResultPtr>& aResults = theSubFeature->results();
415 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
416 for (; aResIt != aResults.end(); ++aResIt) {
417 std::string aResName = (*aResIt)->data()->name();
418 myNames[*aResIt] = EntityName(aResName, aResName, false);
419 aResultsWithNameOrColor.push_back(*aResIt);
422 // store just dumped entity to stack
423 myEntitiesStack.push(LastDumpedEntity(theSubFeature, true, aResultsWithNameOrColor));
428 bool ModelHighAPI_Dumper::exportTo(const std::string& theFileName)
431 OSD_OpenStream(aFile, theFileName.c_str(), std::ofstream::out);
432 if (!aFile.is_open())
436 for (ModulesMap::const_iterator aModIt = myModules.begin();
437 aModIt != myModules.end(); ++aModIt) {
438 aFile << "from " << aModIt->first << " import ";
439 if (aModIt->second.empty() ||
440 aModIt->second.find(std::string()) != aModIt->second.end())
441 aFile << "*"; // import whole module
443 // import specific features
444 std::set<std::string>::const_iterator anObjIt = aModIt->second.begin();
446 for (++anObjIt; anObjIt != aModIt->second.end(); ++anObjIt)
447 aFile << ", " << *anObjIt;
451 if (!myModules.empty())
454 aFile << "from salome.shaper import model" << std::endl << std::endl;
455 aFile << "model.begin()" << std::endl;
457 // dump collected data
458 aFile << myFullDump.str();
459 aFile << myDumpBuffer.str();
462 aFile << "model.end()" << std::endl;
470 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName,
471 const std::string& theObject)
473 myModules[theModuleName].insert(theObject);
476 void ModelHighAPI_Dumper::dumpEntitySetName()
478 const LastDumpedEntity& aLastDumped = myEntitiesStack.top();
480 // dump "setName" for the entity
481 if (aLastDumped.myUserName) {
482 EntityName& anEntityNames = myNames[aLastDumped.myEntity];
483 if (!anEntityNames.myIsDefault)
484 myDumpBuffer << anEntityNames.myCurrentName << ".setName(\""
485 << anEntityNames.myUserName << "\")" << std::endl;
486 // don't dump "setName" for the entity twice
487 anEntityNames.myUserName.clear();
488 anEntityNames.myIsDefault = true;
490 // dump "setName" for results
491 std::list<ResultPtr>::const_iterator aResIt = aLastDumped.myResults.begin();
492 std::list<ResultPtr>::const_iterator aResEnd = aLastDumped.myResults.end();
493 for (; aResIt != aResEnd; ++aResIt) {
495 EntityName& anEntityNames = myNames[*aResIt];
496 if (!anEntityNames.myIsDefault) {
498 myDumpBuffer << ".setName(\"" << anEntityNames.myUserName << "\")" << std::endl;
499 // don't dump "setName" for the entity twice
500 anEntityNames.myUserName.clear();
501 anEntityNames.myIsDefault = true;
504 if (!isDefaultColor(*aResIt)) {
505 AttributeIntArrayPtr aColor = (*aResIt)->data()->intArray(ModelAPI_Result::COLOR_ID());
506 if (aColor && aColor->isInitialized()) {
508 myDumpBuffer << ".setColor(" << aColor->value(0) << ", " << aColor->value(1)
509 << ", " << aColor->value(2) << ")" << std::endl;
512 // set result deflection
513 if (!isDefaultDeflection(*aResIt)) {
514 AttributeDoublePtr aDeflectionAttr =
515 (*aResIt)->data()->real(ModelAPI_Result::DEFLECTION_ID());
516 if(aDeflectionAttr.get() && aDeflectionAttr->isInitialized()) {
518 myDumpBuffer << ".setDeflection(" << aDeflectionAttr->value() << ")" << std::endl;
521 // set result transparency
522 if (!isDefaultTransparency(*aResIt)) {
523 AttributeDoublePtr aTransparencyAttr =
524 (*aResIt)->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
525 if(aTransparencyAttr.get() && aTransparencyAttr->isInitialized()) {
527 myDumpBuffer << ".setTransparency(" << aTransparencyAttr->value() << ")" << std::endl;
532 myNames[aLastDumped.myEntity].myIsDumped = true;
533 myEntitiesStack.pop();
536 bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const
538 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
539 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
540 return (aFound != myNames.end() && aFound->second.myIsDumped) ||
541 myFeaturesToSkip.find(aFeature) != myFeaturesToSkip.end();
544 bool ModelHighAPI_Dumper::isDumped(const AttributeRefAttrPtr& theRefAttr) const
547 if (theRefAttr->isObject())
548 aFeature = ModelAPI_Feature::feature(theRefAttr->object());
550 aFeature = ModelAPI_Feature::feature(theRefAttr->attr()->owner());
551 return aFeature && isDumped(EntityPtr(aFeature));
554 bool ModelHighAPI_Dumper::isDumped(const AttributeRefListPtr& theRefList) const
556 std::list<ObjectPtr> aRefs = theRefList->list();
557 std::list<ObjectPtr>::iterator anIt = aRefs.begin();
558 for (; anIt != aRefs.end(); ++anIt) {
559 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
560 if (aFeature && !isDumped(EntityPtr(aFeature)))
566 bool ModelHighAPI_Dumper::isDefaultColor(const ResultPtr& theResult) const
568 AttributeIntArrayPtr aColor = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
569 if (!aColor || !aColor->isInitialized())
572 std::string aSection, aName, aDefault;
573 theResult->colorConfigInfo(aSection, aName, aDefault);
575 // dump current color
576 std::ostringstream aColorInfo;
577 aColorInfo << aColor->value(0) << "," << aColor->value(1) << "," << aColor->value(2);
579 return aDefault == aColorInfo.str();
582 bool ModelHighAPI_Dumper::isDefaultDeflection(const ResultPtr& theResult) const
584 AttributeDoublePtr aDeflectionAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
585 if(!aDeflectionAttr || !aDeflectionAttr->isInitialized()) {
589 double aCurrent = aDeflectionAttr->value();
590 double aDefault = -1;
592 bool isConstruction = false;
593 std::string aResultGroup = theResult->groupName();
594 if (aResultGroup == ModelAPI_ResultConstruction::group())
595 isConstruction = true;
596 else if (aResultGroup == ModelAPI_ResultBody::group()) {
597 GeomShapePtr aGeomShape = theResult->shape();
598 if (aGeomShape.get()) {
599 // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
600 // correction of deviation for them should not influence to the application performance
601 GeomAPI_ShapeExplorer anExp(aGeomShape, GeomAPI_Shape::FACE);
602 isConstruction = !anExp.more();
606 aDefault = Config_PropManager::real("Visualization", "construction_deflection");
608 aDefault = Config_PropManager::real("Visualization", "body_deflection");
610 return fabs(aCurrent - aDefault) < 1.e-12;
613 bool ModelHighAPI_Dumper::isDefaultTransparency(const ResultPtr& theResult) const
615 AttributeDoublePtr anAttribute = theResult->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
616 if(!anAttribute || !anAttribute->isInitialized()) {
619 return fabs(anAttribute->value()) < 1.e-12;
622 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
624 myDumpBuffer << theChar;
628 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
630 myDumpBuffer << theString;
634 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
636 myDumpBuffer << theString;
640 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const bool theValue)
642 myDumpBuffer << (theValue ? "True" : "False");
646 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
648 myDumpBuffer << theValue;
652 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
654 myDumpBuffer << theValue;
658 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
660 importModule("GeomAPI", "GeomAPI_Pnt");
661 myDumpBuffer << "GeomAPI_Pnt(" << thePoint->x() << ", "
662 << thePoint->y() << ", " << thePoint->z() << ")";
666 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
668 importModule("GeomAPI", "GeomAPI_Dir");
669 myDumpBuffer << "GeomAPI_Dir(" << theDir->x() << ", "
670 << theDir->y() << ", " << theDir->z() << ")";
674 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
675 const std::shared_ptr<GeomDataAPI_Dir>& theDir)
677 myDumpBuffer << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
681 static void dumpArray(std::ostringstream& theOutput, int theSize,
682 double* theValues, std::string* theTexts)
684 for (int i = 0; i < theSize; ++i) {
687 if (theTexts[i].empty())
688 theOutput << theValues[i];
690 theOutput << "\"" << theTexts[i] << "\"";
694 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
695 const std::shared_ptr<GeomDataAPI_Point>& thePoint)
697 static const int aSize = 3;
698 double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
699 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()};
700 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
704 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
705 const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
707 static const int aSize = 2;
708 double aValues[aSize] = {thePoint->x(), thePoint->y()};
709 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()};
710 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
714 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
715 const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
717 myDumpBuffer << (theAttrBool->value() ? "True" : "False");
721 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
722 const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
724 std::string aText = theAttrInt->text();
726 myDumpBuffer << theAttrInt->value();
728 myDumpBuffer << "\"" << aText << "\"";
732 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
733 const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
735 std::string aText = theAttrReal->text();
737 myDumpBuffer << theAttrReal->value();
739 myDumpBuffer << "\"" << aText << "\"";
743 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
744 const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
746 myDumpBuffer << "\"" << theAttrStr->value() << "\"";
750 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FolderPtr& theFolder)
752 myDumpBuffer << name(theFolder);
756 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity)
758 myDumpBuffer << name(theEntity);
760 if (!myNames[theEntity].myIsDumped) {
761 bool isUserDefinedName = !myNames[theEntity].myIsDefault;
762 // store results if they have user-defined names or colors
763 std::list<ResultPtr> aResultsWithNameOrColor;
764 const std::list<ResultPtr>& aResults = theEntity->results();
765 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
766 for (; aResIt != aResults.end(); ++aResIt) {
767 if (!myNames[*aResIt].myIsDefault || !isDefaultColor(*aResIt) ||
768 !isDefaultDeflection(*aResIt) || !isDefaultTransparency(*aResIt))
769 aResultsWithNameOrColor.push_back(*aResIt);
771 ResultCompSolidPtr aCompSolid =
772 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aResIt);
774 int aNbSubs = aCompSolid->numberOfSubs();
775 for (int i = 0; i < aNbSubs; ++i) {
776 ResultPtr aCurRes = aCompSolid->subResult(i);
777 if (!myNames[aCurRes].myIsDefault || !isDefaultColor(aCurRes) ||
778 !isDefaultDeflection(aCurRes) || !isDefaultTransparency(aCurRes))
779 aResultsWithNameOrColor.push_back(aCurRes);
783 // store just dumped entity to stack
784 if (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theEntity)
785 myEntitiesStack.push(
786 LastDumpedEntity(theEntity, isUserDefinedName, aResultsWithNameOrColor));
789 // remove entity from the list of not dumped items
790 myNotDumpedEntities.erase(theEntity);
794 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ResultPtr& theResult)
796 FeaturePtr aFeature = ModelAPI_Feature::feature(theResult);
799 std::list<ResultPtr> aResults = aFeature->results();
800 for(std::list<ResultPtr>::const_iterator
801 anIt = aResults.cbegin(); anIt != aResults.cend(); ++anIt, ++anIndex) {
802 if(theResult->isSame(*anIt)) {
806 ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*anIt);
808 int aNbSubs = aCompSolid->numberOfSubs();
809 for (aSubIndex = 0; aSubIndex < aNbSubs; ++aSubIndex)
810 if (theResult->isSame(aCompSolid->subResult(aSubIndex)))
812 if (aSubIndex < aNbSubs)
818 myDumpBuffer << name(aFeature);
820 myDumpBuffer << ".result()";
822 myDumpBuffer << ".results()[" << anIndex << "]";
824 if (aSubIndex >= 0) {
825 myDumpBuffer << ".subResult(" << aSubIndex << ")";
830 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ObjectPtr& theObject)
832 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
834 myDumpBuffer << name(aFeature);
838 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
847 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const AttributePtr& theAttr)
849 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttr->owner());
851 std::string aWrapperPrefix, aWrapperSuffix;
852 // Check the attribute belongs to copied (in multi-translation or multi-rotation) feature.
853 // In this case we need to cast explicitly feature to appropriate type.
854 AttributeBooleanPtr isCopy = anOwner->boolean("Copy");
855 if (isCopy.get() && isCopy->value()) {
856 aWrapperPrefix = featureWrapper(anOwner) + "(";
857 aWrapperSuffix = ")";
858 importModule("SketchAPI");
861 myDumpBuffer << aWrapperPrefix << name(anOwner) << aWrapperSuffix
862 << "." << attributeGetter(anOwner, theAttr->id()) << "()";
866 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
867 const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
869 if (theRefAttr->isObject())
870 *this << theRefAttr->object();
872 *this << theRefAttr->attr();
876 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
877 const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList)
880 std::list<std::pair<ObjectPtr, AttributePtr> > aList = theRefAttrList->list();
881 bool isAdded = false;
882 std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aList.begin();
883 for (; anIt != aList.end(); ++anIt) {
885 myDumpBuffer << ", ";
889 *this << anIt->first;
890 else if (anIt->second)
891 * this << anIt->second;
897 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
898 const std::shared_ptr<ModelAPI_AttributeReference>& theReference)
900 *this << theReference->value();
904 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
905 const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList)
907 static const int aThreshold = 2;
908 // if number of elements in the list if greater than a threshold,
909 // dump it in a separate line with specific name
910 std::string aDumped = myDumpBuffer.str();
911 if (aDumped.empty() || theRefList->size() <= aThreshold) {
913 std::list<ObjectPtr> aList = theRefList->list();
914 bool isAdded = false;
915 std::list<ObjectPtr>::const_iterator anIt = aList.begin();
916 for (; anIt != aList.end(); ++anIt) {
918 myDumpBuffer << ", ";
926 // clear buffer and store list "as is"
927 myDumpBuffer.str("");
929 // save buffer and clear it again
930 std::string aDumpedList = myDumpBuffer.str();
931 myDumpBuffer.str("");
932 // obtain name of list
933 FeaturePtr anOwner = ModelAPI_Feature::feature(theRefList->owner());
934 std::string aListName = name(anOwner) + "_objects";
935 // store all previous data
936 myDumpBuffer << aListName << " = " << aDumpedList << std::endl
937 << aDumped << aListName;
942 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
943 const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
945 myDumpBuffer << "model.selection(";
947 if(!theAttrSelect->isInitialized()) {
952 GeomShapePtr aShape = theAttrSelect->value();
954 aShape = theAttrSelect->context()->shape();
962 myDumpBuffer << "\"" << aShape->shapeTypeStr() << "\", \"" <<
963 theAttrSelect->namingName() << "\")";
967 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
968 const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList)
973 std::string aShapeTypeStr;
975 bool isAdded = false;
977 for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) {
978 AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex);
979 aShape = anAttribute->value();
981 ResultPtr aContext = anAttribute->context();
983 aShape = aContext->shape();
991 myDumpBuffer << ", ";
995 myDumpBuffer << "model.selection(\"" <<
996 aShape->shapeTypeStr() << "\", \"" << anAttribute->namingName() << "\")";
1003 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1004 const std::shared_ptr<ModelAPI_AttributeStringArray>& theArray)
1007 for(int anIndex = 0; anIndex < theArray->size(); ++anIndex) {
1011 myDumpBuffer<<"\""<<theArray->value(anIndex)<<"\"";
1019 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
1020 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
1022 theDumper.myDumpBuffer << theEndl;
1024 if (!theDumper.myEntitiesStack.empty()) {
1026 // all copies have been stored into stack, pop them all
1029 // Name for composite feature is dumped when all sub-entities are dumped
1030 // (see method ModelHighAPI_Dumper::processSubs).
1031 const ModelHighAPI_Dumper::LastDumpedEntity& aLastDumped = theDumper.myEntitiesStack.top();
1032 CompositeFeaturePtr aComposite =
1033 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aLastDumped.myEntity);
1035 theDumper.dumpEntitySetName();
1036 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aLastDumped.myEntity);
1038 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
1039 isCopy = aCopyAttr.get() && aCopyAttr->value();
1042 } while (isCopy && !theDumper.myEntitiesStack.empty());
1045 // store all not-dumped entities first
1046 std::set<EntityPtr> aNotDumped = theDumper.myNotDumpedEntities;
1047 std::string aBufCopy = theDumper.myDumpBuffer.str();
1048 theDumper.clear(true);
1049 std::set<EntityPtr>::const_iterator anIt = aNotDumped.begin();
1050 for (; anIt != aNotDumped.end(); ++anIt) {
1051 // if the feature is composite, dump it with all subs
1052 CompositeFeaturePtr aCompFeat =
1053 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
1055 theDumper.process(aCompFeat, true);
1057 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
1058 theDumper.dumpFeature(aFeature, true);
1059 // dump the Projection feature which produces this "Copy" entity
1060 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
1061 if (aCopyAttr.get() && aCopyAttr->value())
1063 const std::set<AttributePtr>& aRefs = aFeature->data()->refsToMe();
1064 std::set<AttributePtr>::iterator aRefIt = aRefs.begin();
1065 for (; aRefIt != aRefs.end(); ++aRefIt)
1066 if ((*aRefIt)->id() == "ProjectedFeature")
1067 { // process projection only
1068 FeaturePtr anOwner = ModelAPI_Feature::feature((*aRefIt)->owner());
1069 if (anOwner && !theDumper.isDumped(EntityPtr(anOwner)))
1070 theDumper.dumpFeature(anOwner, true);
1076 // avoid multiple empty lines
1077 size_t anInd = std::string::npos;
1078 while ((anInd = aBufCopy.find("\n\n\n")) != std::string::npos)
1079 aBufCopy.erase(anInd, 1);
1080 // then store currently dumped string
1081 theDumper.myFullDump << aBufCopy;
1083 // now, store all postponed features
1084 theDumper.dumpPostponed();