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 std::pair<int, int>& aNbFeatures = myFeatureCount[aDoc][aKind];
152 aNbFeatures.first += 1;
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.first == anId && aNbFeatures.second < anId) {
162 // name is not user-defined
163 isDefaultName = true;
166 if (anId > aNbFeatures.second)
167 aNbFeatures.second = anId;
170 // obtain default name for the feature
171 if (theUseEntityName)
172 aDefaultName << aName;
175 NbFeaturesMap::const_iterator aFIt = myFeatureCount.begin();
176 for (; aFIt != myFeatureCount.end(); ++aFIt) {
177 std::map<std::string, std::pair<int, int> >::const_iterator aFound =
178 aFIt->second.find(aKind);
179 if (aFound != aFIt->second.end())
180 aFullIndex += aFound->second.first;
182 aDefaultName << aKind << "_" << aFullIndex;
186 myNames[theEntity] = EntityName(aDefaultName.str(), aName, isDefaultName);
188 myNotDumpedEntities.insert(theEntity);
190 // store names of results
192 saveResultNames(aFeature);
194 return myNames[theEntity].myCurrentName;
197 const std::string& ModelHighAPI_Dumper::parentName(const FeaturePtr& theEntity)
199 const std::set<AttributePtr>& aRefs = theEntity->data()->refsToMe();
200 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
201 for (; aRefIt != aRefs.end(); ++aRefIt) {
202 CompositeFeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(
203 ModelAPI_Feature::feature((*aRefIt)->owner()));
205 return name(anOwner);
208 static const std::string DUMMY;
212 void ModelHighAPI_Dumper::saveResultNames(const FeaturePtr& theFeature)
214 // Default name of the feature
215 bool isFeatureDefaultName = myNames[theFeature].myIsDefault;
217 // Save only names of results which is not correspond to default feature name
218 const std::list<ResultPtr>& aResults = theFeature->results();
219 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
220 for (int i = 0; aResIt != aResults.end(); ++aResIt, ++i) {
221 std::pair<std::string, bool> aName = ModelAPI_Tools::getDefaultName(*aResIt, i);
222 std::string aDefaultName = aName.first;
223 std::string aResName = (*aResIt)->data()->name();
225 bool isUserDefined = !(isFeatureDefaultName && aDefaultName == aResName);
227 myNames[*aResIt] = EntityName(aResName,
228 (isUserDefined ? aResName : std::string()), !isUserDefined);
230 // check names of sub-results for CompSolid
231 ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aResIt);
233 int aNbSubs = aCompSolid->numberOfSubs();
234 for (int j = 0; j < aNbSubs; ++j) {
235 ResultPtr aSub = aCompSolid->subResult(j);
236 std::string aSubName = aSub->data()->name();
237 aName = ModelAPI_Tools::getDefaultName(aSub, j);
238 aDefaultName = aName.first;
240 bool isUserDefinedSubName = isUserDefined || aDefaultName != aSubName;
241 myNames[aSub] = EntityName(aSubName,
242 (isUserDefinedSubName ? aSubName : std::string()), !isUserDefinedSubName);
248 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc,
249 const std::string& theFileName)
251 // dump top level document feature
252 static const std::string aDocName("partSet");
253 myNames[theDoc] = EntityName(aDocName, std::string(), true);
254 *this << aDocName << " = model.moduleDocument()" << std::endl;
256 // dump subfeatures and store result to file
257 return process(theDoc) && exportTo(theFileName);
260 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc)
263 std::list<ObjectPtr> anObjects = theDoc->allObjects();
264 std::list<ObjectPtr>::const_iterator anObjIt = anObjects.begin();
265 // firstly, dump all parameters
266 for (; anObjIt != anObjects.end(); ++ anObjIt) {
267 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIt);
269 dumpParameter(aFeature);
271 // dump all other features
272 for (anObjIt = anObjects.begin(); anObjIt != anObjects.end(); ++anObjIt) {
273 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anObjIt);
274 if (aCompFeat) // iteratively process composite features
275 isOk = process(aCompFeat) && isOk;
276 else if (!isDumped(EntityPtr(*anObjIt))) {
278 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(*anObjIt);
282 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIt);
283 if (aFeature) // dump common feature
284 dumpFeature(aFeature);
288 // dump folders if any
293 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
296 // increase composite features stack
297 ++gCompositeStackDepth;
298 // dump composite itself
299 if (!isDumped(EntityPtr(theComposite)) || isForce)
300 dumpFeature(FeaturePtr(theComposite), isForce);
302 // sub-part is processed independently, because it provides separate document
303 if (theComposite->getKind() == PartSetPlugin_Part::ID()) {
304 // dump name of the part if it is different from default
305 if (!myEntitiesStack.empty())
308 // decrease composite features stack because we run into separate document
309 --gCompositeStackDepth;
311 ResultPartPtr aPartResult =
312 std::dynamic_pointer_cast<ModelAPI_ResultPart>(theComposite->lastResult());
315 DocumentPtr aSubDoc = aPartResult->partDoc();
318 // set name of document
319 const std::string& aPartName = myNames[theComposite].myCurrentName;
320 std::string aDocName = aPartName + "_doc";
321 myNames[aSubDoc] = EntityName(aDocName, std::string(), true);
323 // dump document in a separate line
324 *this << aDocName << " = " << aPartName << ".document()" << std::endl;
325 // dump features in the document
326 bool aRes = process(aSubDoc);
327 *this << "model.do()" << std::endl;
332 bool isOk = processSubs(theComposite);
333 // decrease composite features stack
334 --gCompositeStackDepth;
339 bool ModelHighAPI_Dumper::processSubs(
340 const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
344 // dump all sub-features;
345 bool isSubDumped = false;
346 int aNbSubs = theComposite->numberOfSubs();
347 for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) {
348 FeaturePtr aFeature = theComposite->subFeature(anIndex);
349 if (isDumped(EntityPtr(aFeature)))
353 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
354 if (aCompFeat) // iteratively process composite features
355 isOk = process(aCompFeat) && isOk;
357 dumpFeature(aFeature, true);
360 bool isDumpSetName = !myEntitiesStack.empty() &&
361 myEntitiesStack.top().myEntity == EntityPtr(theComposite);
362 bool isForceModelDo = isSubDumped && isDumpSetName &&
363 (myEntitiesStack.top().myUserName || !myEntitiesStack.top().myResults.empty());
364 // It is necessary for the sketch to create its result when complete (command "model.do()").
365 // This option is set by flat theDumpModelDo.
366 // However, nested sketches are rebuilt by parent feature, so, they do not need
367 // explicit call of "model.do()". This will be controlled by the depth of the stack.
368 if (isForceModelDo || (theDumpModelDo && gCompositeStackDepth <= 1))
369 *this << "model.do()" << std::endl;
371 // dump "setName" for composite feature
377 void ModelHighAPI_Dumper::postpone(const EntityPtr& theEntity)
380 name(theEntity, false);
381 myPostponed.push_back(theEntity);
384 void ModelHighAPI_Dumper::dumpPostponed(bool theDumpFolders)
386 if (myDumpPostponedInProgress)
389 myDumpPostponedInProgress = true;
390 // make a copy of postponed entities, because the list will be updated
391 // if some features are not able to be dumped
392 std::list<EntityPtr> aPostponedCopy = myPostponed;
395 // iterate over postponed entities and try to dump them
396 std::list<EntityPtr>::const_iterator anIt = aPostponedCopy.begin();
397 for (; anIt != aPostponedCopy.end(); ++anIt) {
398 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(*anIt);
403 myPostponed.push_back(*anIt);
406 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
408 dumpFeature(aFeature, true);
411 myDumpPostponedInProgress = false;
414 void ModelHighAPI_Dumper::dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
415 const FeaturePtr& theSubFeature)
417 name(theSubFeature, false);
418 myNames[theSubFeature] = EntityName(theSubFeatureGet, theSubFeature->name(), false);
420 // store results if they have user-defined names or colors
421 std::list<ResultPtr> aResultsWithNameOrColor;
422 const std::list<ResultPtr>& aResults = theSubFeature->results();
423 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
424 for (; aResIt != aResults.end(); ++aResIt) {
425 std::string aResName = (*aResIt)->data()->name();
426 myNames[*aResIt] = EntityName(aResName, aResName, false);
427 aResultsWithNameOrColor.push_back(*aResIt);
430 // store just dumped entity to stack
431 myEntitiesStack.push(LastDumpedEntity(theSubFeature, true, aResultsWithNameOrColor));
436 bool ModelHighAPI_Dumper::exportTo(const std::string& theFileName)
439 OSD_OpenStream(aFile, theFileName.c_str(), std::ofstream::out);
440 if (!aFile.is_open())
443 // standard header (encoding + imported modules)
444 aFile << "# -*- coding: utf-8 -*-" << std::endl << std::endl;
445 for (ModulesMap::const_iterator aModIt = myModules.begin();
446 aModIt != myModules.end(); ++aModIt) {
447 aFile << "from " << aModIt->first << " import ";
448 if (aModIt->second.empty() ||
449 aModIt->second.find(std::string()) != aModIt->second.end())
450 aFile << "*"; // import whole module
452 // import specific features
453 std::set<std::string>::const_iterator anObjIt = aModIt->second.begin();
455 for (++anObjIt; anObjIt != aModIt->second.end(); ++anObjIt)
456 aFile << ", " << *anObjIt;
460 if (!myModules.empty())
463 aFile << "from salome.shaper import model" << std::endl << std::endl;
464 aFile << "model.begin()" << std::endl;
466 // dump collected data
467 aFile << myFullDump.str();
468 aFile << myDumpBuffer.str();
471 aFile << "model.end()" << std::endl;
479 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName,
480 const std::string& theObject)
482 myModules[theModuleName].insert(theObject);
485 void ModelHighAPI_Dumper::dumpEntitySetName()
487 const LastDumpedEntity& aLastDumped = myEntitiesStack.top();
488 bool isBufferEmpty = myDumpBuffer.str().empty();
490 // dump "setName" for the entity
491 if (aLastDumped.myUserName) {
492 EntityName& anEntityNames = myNames[aLastDumped.myEntity];
493 if (!anEntityNames.myIsDefault)
494 myDumpBuffer << anEntityNames.myCurrentName << ".setName(\""
495 << anEntityNames.myUserName << "\")" << std::endl;
496 // don't dump "setName" for the entity twice
497 anEntityNames.myUserName.clear();
498 anEntityNames.myIsDefault = true;
500 // dump "setName" for results
501 std::list<ResultPtr>::const_iterator aResIt = aLastDumped.myResults.begin();
502 std::list<ResultPtr>::const_iterator aResEnd = aLastDumped.myResults.end();
503 for (; aResIt != aResEnd; ++aResIt) {
505 EntityName& anEntityNames = myNames[*aResIt];
506 if (!anEntityNames.myIsDefault) {
508 myDumpBuffer << ".setName(\"" << anEntityNames.myUserName << "\")" << std::endl;
509 // don't dump "setName" for the entity twice
510 anEntityNames.myUserName.clear();
511 anEntityNames.myIsDefault = true;
514 if (!isDefaultColor(*aResIt)) {
515 AttributeIntArrayPtr aColor = (*aResIt)->data()->intArray(ModelAPI_Result::COLOR_ID());
516 if (aColor && aColor->isInitialized()) {
518 myDumpBuffer << ".setColor(" << aColor->value(0) << ", " << aColor->value(1)
519 << ", " << aColor->value(2) << ")" << std::endl;
522 // set result deflection
523 if (!isDefaultDeflection(*aResIt)) {
524 AttributeDoublePtr aDeflectionAttr =
525 (*aResIt)->data()->real(ModelAPI_Result::DEFLECTION_ID());
526 if(aDeflectionAttr.get() && aDeflectionAttr->isInitialized()) {
528 myDumpBuffer << ".setDeflection(" << aDeflectionAttr->value() << ")" << std::endl;
531 // set result transparency
532 if (!isDefaultTransparency(*aResIt)) {
533 AttributeDoublePtr aTransparencyAttr =
534 (*aResIt)->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
535 if(aTransparencyAttr.get() && aTransparencyAttr->isInitialized()) {
537 myDumpBuffer << ".setTransparency(" << aTransparencyAttr->value() << ")" << std::endl;
542 myNames[aLastDumped.myEntity].myIsDumped = true;
543 myEntitiesStack.pop();
545 // clean buffer if it was clear before
547 myFullDump << myDumpBuffer.str();
548 myDumpBuffer.str("");
552 bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const
554 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
555 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
556 return (aFound != myNames.end() && aFound->second.myIsDumped) ||
557 myFeaturesToSkip.find(aFeature) != myFeaturesToSkip.end();
560 bool ModelHighAPI_Dumper::isDumped(const AttributeRefAttrPtr& theRefAttr) const
563 if (theRefAttr->isObject())
564 aFeature = ModelAPI_Feature::feature(theRefAttr->object());
566 aFeature = ModelAPI_Feature::feature(theRefAttr->attr()->owner());
567 return aFeature && isDumped(EntityPtr(aFeature));
570 bool ModelHighAPI_Dumper::isDumped(const AttributeRefListPtr& theRefList) const
572 std::list<ObjectPtr> aRefs = theRefList->list();
573 std::list<ObjectPtr>::iterator anIt = aRefs.begin();
574 for (; anIt != aRefs.end(); ++anIt) {
575 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
576 if (aFeature && !isDumped(EntityPtr(aFeature)))
582 bool ModelHighAPI_Dumper::isDefaultColor(const ResultPtr& theResult) const
584 AttributeIntArrayPtr aColor = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
585 if (!aColor || !aColor->isInitialized())
588 std::string aSection, aName, aDefault;
589 theResult->colorConfigInfo(aSection, aName, aDefault);
591 // dump current color
592 std::ostringstream aColorInfo;
593 aColorInfo << aColor->value(0) << "," << aColor->value(1) << "," << aColor->value(2);
595 return aDefault == aColorInfo.str();
598 bool ModelHighAPI_Dumper::isDefaultDeflection(const ResultPtr& theResult) const
600 AttributeDoublePtr aDeflectionAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
601 if(!aDeflectionAttr || !aDeflectionAttr->isInitialized()) {
605 double aCurrent = aDeflectionAttr->value();
606 double aDefault = -1;
608 bool isConstruction = false;
609 std::string aResultGroup = theResult->groupName();
610 if (aResultGroup == ModelAPI_ResultConstruction::group())
611 isConstruction = true;
612 else if (aResultGroup == ModelAPI_ResultBody::group()) {
613 GeomShapePtr aGeomShape = theResult->shape();
614 if (aGeomShape.get()) {
615 // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
616 // correction of deviation for them should not influence to the application performance
617 GeomAPI_ShapeExplorer anExp(aGeomShape, GeomAPI_Shape::FACE);
618 isConstruction = !anExp.more();
622 aDefault = Config_PropManager::real("Visualization", "construction_deflection");
624 aDefault = Config_PropManager::real("Visualization", "body_deflection");
626 return fabs(aCurrent - aDefault) < 1.e-12;
629 bool ModelHighAPI_Dumper::isDefaultTransparency(const ResultPtr& theResult) const
631 AttributeDoublePtr anAttribute = theResult->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
632 if(!anAttribute || !anAttribute->isInitialized()) {
635 return fabs(anAttribute->value()) < 1.e-12;
638 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
640 myDumpBuffer << theChar;
644 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
646 myDumpBuffer << theString;
650 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
652 myDumpBuffer << theString;
656 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const bool theValue)
658 myDumpBuffer << (theValue ? "True" : "False");
662 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
664 myDumpBuffer << theValue;
668 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
670 myDumpBuffer << theValue;
674 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
676 importModule("GeomAPI", "GeomAPI_Pnt");
677 myDumpBuffer << "GeomAPI_Pnt(" << thePoint->x() << ", "
678 << thePoint->y() << ", " << thePoint->z() << ")";
682 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
684 importModule("GeomAPI", "GeomAPI_Dir");
685 myDumpBuffer << "GeomAPI_Dir(" << theDir->x() << ", "
686 << theDir->y() << ", " << theDir->z() << ")";
690 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
691 const std::shared_ptr<GeomDataAPI_Dir>& theDir)
693 myDumpBuffer << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
697 static void dumpArray(std::ostringstream& theOutput, int theSize,
698 double* theValues, std::string* theTexts)
700 for (int i = 0; i < theSize; ++i) {
703 if (theTexts[i].empty())
704 theOutput << theValues[i];
706 theOutput << "\"" << theTexts[i] << "\"";
710 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
711 const std::shared_ptr<GeomDataAPI_Point>& thePoint)
713 static const int aSize = 3;
714 double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
715 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()};
716 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
720 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
721 const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
723 static const int aSize = 2;
724 double aValues[aSize] = {thePoint->x(), thePoint->y()};
725 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()};
726 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
730 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
731 const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
733 myDumpBuffer << (theAttrBool->value() ? "True" : "False");
737 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
738 const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
740 std::string aText = theAttrInt->text();
742 myDumpBuffer << theAttrInt->value();
744 myDumpBuffer << "\"" << aText << "\"";
748 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
749 const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
751 std::string aText = theAttrReal->text();
753 myDumpBuffer << theAttrReal->value();
755 myDumpBuffer << "\"" << aText << "\"";
759 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
760 const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
762 myDumpBuffer << "\"" << theAttrStr->value() << "\"";
766 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FolderPtr& theFolder)
768 myDumpBuffer << name(theFolder);
770 // add dumped folder to a stack
771 if (!myNames[theFolder].myIsDumped &&
772 (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theFolder))
773 myEntitiesStack.push(LastDumpedEntity(theFolder, !myNames[theFolder].myIsDefault));
778 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity)
780 myDumpBuffer << name(theEntity);
782 if (!myNames[theEntity].myIsDumped) {
783 bool isUserDefinedName = !myNames[theEntity].myIsDefault;
784 // store results if they have user-defined names or colors
785 std::list<ResultPtr> aResultsWithNameOrColor;
786 const std::list<ResultPtr>& aResults = theEntity->results();
787 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
788 for (; aResIt != aResults.end(); ++aResIt) {
789 if (!myNames[*aResIt].myIsDefault || !isDefaultColor(*aResIt) ||
790 !isDefaultDeflection(*aResIt) || !isDefaultTransparency(*aResIt))
791 aResultsWithNameOrColor.push_back(*aResIt);
793 ResultCompSolidPtr aCompSolid =
794 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aResIt);
796 int aNbSubs = aCompSolid->numberOfSubs();
797 for (int i = 0; i < aNbSubs; ++i) {
798 ResultPtr aCurRes = aCompSolid->subResult(i);
799 if (!myNames[aCurRes].myIsDefault || !isDefaultColor(aCurRes) ||
800 !isDefaultDeflection(aCurRes) || !isDefaultTransparency(aCurRes))
801 aResultsWithNameOrColor.push_back(aCurRes);
805 // store just dumped entity to stack
806 if (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theEntity)
807 myEntitiesStack.push(
808 LastDumpedEntity(theEntity, isUserDefinedName, aResultsWithNameOrColor));
811 // remove entity from the list of not dumped items
812 myNotDumpedEntities.erase(theEntity);
816 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ResultPtr& theResult)
818 FeaturePtr aFeature = ModelAPI_Feature::feature(theResult);
821 std::list<ResultPtr> aResults = aFeature->results();
822 for(std::list<ResultPtr>::const_iterator
823 anIt = aResults.cbegin(); anIt != aResults.cend(); ++anIt, ++anIndex) {
824 if(theResult->isSame(*anIt)) {
828 ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*anIt);
830 int aNbSubs = aCompSolid->numberOfSubs();
831 for (aSubIndex = 0; aSubIndex < aNbSubs; ++aSubIndex)
832 if (theResult->isSame(aCompSolid->subResult(aSubIndex)))
834 if (aSubIndex < aNbSubs)
840 myDumpBuffer << name(aFeature);
842 myDumpBuffer << ".result()";
844 myDumpBuffer << ".results()[" << anIndex << "]";
846 if (aSubIndex >= 0) {
847 myDumpBuffer << ".subResult(" << aSubIndex << ")";
852 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ObjectPtr& theObject)
854 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
856 myDumpBuffer << name(aFeature);
860 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
869 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const AttributePtr& theAttr)
871 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttr->owner());
873 std::string aWrapperPrefix, aWrapperSuffix;
874 // Check the attribute belongs to copied (in multi-translation or multi-rotation) feature.
875 // In this case we need to cast explicitly feature to appropriate type.
876 AttributeBooleanPtr isCopy = anOwner->boolean("Copy");
877 if (isCopy.get() && isCopy->value()) {
878 aWrapperPrefix = featureWrapper(anOwner) + "(";
879 aWrapperSuffix = ")";
880 importModule("SketchAPI");
883 myDumpBuffer << aWrapperPrefix << name(anOwner) << aWrapperSuffix
884 << "." << attributeGetter(anOwner, theAttr->id()) << "()";
888 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
889 const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
891 if (theRefAttr->isObject())
892 *this << theRefAttr->object();
894 *this << theRefAttr->attr();
898 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
899 const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList)
902 std::list<std::pair<ObjectPtr, AttributePtr> > aList = theRefAttrList->list();
903 bool isAdded = false;
904 std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aList.begin();
905 for (; anIt != aList.end(); ++anIt) {
907 myDumpBuffer << ", ";
911 *this << anIt->first;
912 else if (anIt->second)
913 * this << anIt->second;
919 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
920 const std::shared_ptr<ModelAPI_AttributeReference>& theReference)
922 *this << theReference->value();
926 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
927 const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList)
929 static const int aThreshold = 2;
930 // if number of elements in the list if greater than a threshold,
931 // dump it in a separate line with specific name
932 std::string aDumped = myDumpBuffer.str();
933 if (aDumped.empty() || theRefList->size() <= aThreshold) {
935 std::list<ObjectPtr> aList = theRefList->list();
936 bool isAdded = false;
937 std::list<ObjectPtr>::const_iterator anIt = aList.begin();
938 for (; anIt != aList.end(); ++anIt) {
940 myDumpBuffer << ", ";
948 // clear buffer and store list "as is"
949 myDumpBuffer.str("");
951 // save buffer and clear it again
952 std::string aDumpedList = myDumpBuffer.str();
953 myDumpBuffer.str("");
954 // obtain name of list
955 FeaturePtr anOwner = ModelAPI_Feature::feature(theRefList->owner());
956 std::string aListName = name(anOwner) + "_objects";
957 // store all previous data
958 myDumpBuffer << aListName << " = " << aDumpedList << std::endl
959 << aDumped << aListName;
964 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
965 const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
967 myDumpBuffer << "model.selection(";
969 if(!theAttrSelect->isInitialized()) {
974 GeomShapePtr aShape = theAttrSelect->value();
976 aShape = theAttrSelect->context()->shape();
984 myDumpBuffer << "\"" << aShape->shapeTypeStr() << "\", \"" <<
985 theAttrSelect->namingName() << "\")";
989 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
990 const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList)
992 static const int aThreshold = 2;
993 // if number of elements in the list if greater than a threshold,
994 // dump it in a separate line with specific name
995 std::string aDumped = myDumpBuffer.str();
997 if (aDumped.empty() || theAttrSelList->size() <= aThreshold) {
1000 GeomShapePtr aShape;
1001 std::string aShapeTypeStr;
1003 bool isAdded = false;
1005 for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) {
1006 AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex);
1007 aShape = anAttribute->value();
1009 ResultPtr aContext = anAttribute->context();
1011 aShape = aContext->shape();
1019 myDumpBuffer << ", ";
1023 myDumpBuffer << "model.selection(\"" <<
1024 aShape->shapeTypeStr() << "\", \"" << anAttribute->namingName() << "\")";
1027 myDumpBuffer << "]";
1029 // clear buffer and store list "as is"
1030 myDumpBuffer.str("");
1031 *this << theAttrSelList;
1032 // save buffer and clear it again
1033 std::string aDumpedList = myDumpBuffer.str();
1034 myDumpBuffer.str("");
1035 // obtain name of list (the feature may contain several selection lists)
1036 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttrSelList->owner());
1037 std::string aListName = name(anOwner) + "_objects";
1038 std::list<AttributePtr> aSelLists =
1039 anOwner->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
1040 if (aSelLists.size() > 1) {
1042 for (std::list<AttributePtr>::iterator aSIt = aSelLists.begin();
1043 aSIt != aSelLists.end(); ++aSIt, ++anIndex)
1044 if ((*aSIt).get() == theAttrSelList.get())
1046 std::ostringstream aSStream;
1047 aSStream << aListName << "_" << anIndex;
1048 aListName = aSStream.str();
1050 // store all previous data
1051 myDumpBuffer << aListName << " = " << aDumpedList << std::endl
1052 << aDumped << aListName;
1057 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1058 const std::shared_ptr<ModelAPI_AttributeStringArray>& theArray)
1061 for(int anIndex = 0; anIndex < theArray->size(); ++anIndex) {
1065 myDumpBuffer<<"\""<<theArray->value(anIndex)<<"\"";
1073 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
1074 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
1076 theDumper.myDumpBuffer << theEndl;
1078 if (!theDumper.myEntitiesStack.empty()) {
1080 // all copies have been stored into stack, pop them all
1083 // Name for composite feature is dumped when all sub-entities are dumped
1084 // (see method ModelHighAPI_Dumper::processSubs).
1085 const ModelHighAPI_Dumper::LastDumpedEntity& aLastDumped = theDumper.myEntitiesStack.top();
1086 CompositeFeaturePtr aComposite =
1087 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aLastDumped.myEntity);
1089 theDumper.dumpEntitySetName();
1090 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aLastDumped.myEntity);
1092 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
1093 isCopy = aCopyAttr.get() && aCopyAttr->value();
1096 } while (isCopy && !theDumper.myEntitiesStack.empty());
1099 // store all not-dumped entities first
1100 std::set<EntityPtr> aNotDumped = theDumper.myNotDumpedEntities;
1101 std::string aBufCopy = theDumper.myDumpBuffer.str();
1102 theDumper.clear(true);
1103 std::set<EntityPtr>::const_iterator anIt = aNotDumped.begin();
1104 for (; anIt != aNotDumped.end(); ++anIt) {
1105 // if the feature is composite, dump it with all subs
1106 CompositeFeaturePtr aCompFeat =
1107 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
1109 theDumper.process(aCompFeat, true);
1111 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
1112 theDumper.dumpFeature(aFeature, true);
1113 // dump the Projection feature which produces this "Copy" entity
1114 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
1115 if (aCopyAttr.get() && aCopyAttr->value())
1117 const std::set<AttributePtr>& aRefs = aFeature->data()->refsToMe();
1118 std::set<AttributePtr>::iterator aRefIt = aRefs.begin();
1119 for (; aRefIt != aRefs.end(); ++aRefIt)
1120 if ((*aRefIt)->id() == "ProjectedFeature")
1121 { // process projection only
1122 FeaturePtr anOwner = ModelAPI_Feature::feature((*aRefIt)->owner());
1123 if (anOwner && !theDumper.isDumped(EntityPtr(anOwner)))
1124 theDumper.dumpFeature(anOwner, true);
1130 // avoid multiple empty lines
1131 size_t anInd = std::string::npos;
1132 while ((anInd = aBufCopy.find("\n\n\n")) != std::string::npos)
1133 aBufCopy.erase(anInd, 1);
1134 // then store currently dumped string
1135 theDumper.myFullDump << aBufCopy;
1137 // now, store all postponed features
1138 theDumper.dumpPostponed();