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);
284 // dump folders if any
289 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
292 // increase composite features stack
293 ++gCompositeStackDepth;
294 // dump composite itself
295 if (!isDumped(EntityPtr(theComposite)) || isForce)
296 dumpFeature(FeaturePtr(theComposite), isForce);
298 // sub-part is processed independently, because it provides separate document
299 if (theComposite->getKind() == PartSetPlugin_Part::ID()) {
300 // dump name of the part if it is different from default
301 if (!myEntitiesStack.empty())
304 // decrease composite features stack because we run into separate document
305 --gCompositeStackDepth;
307 ResultPartPtr aPartResult =
308 std::dynamic_pointer_cast<ModelAPI_ResultPart>(theComposite->lastResult());
311 DocumentPtr aSubDoc = aPartResult->partDoc();
314 // set name of document
315 const std::string& aPartName = myNames[theComposite].myCurrentName;
316 std::string aDocName = aPartName + "_doc";
317 myNames[aSubDoc] = EntityName(aDocName, std::string(), true);
319 // dump document in a separate line
320 *this << aDocName << " = " << aPartName << ".document()" << std::endl;
321 // dump features in the document
322 bool aRes = process(aSubDoc);
323 *this << "model.do()" << std::endl;
328 bool isOk = processSubs(theComposite);
329 // decrease composite features stack
330 --gCompositeStackDepth;
335 bool ModelHighAPI_Dumper::processSubs(
336 const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
340 // dump all sub-features;
341 bool isSubDumped = false;
342 int aNbSubs = theComposite->numberOfSubs();
343 for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) {
344 FeaturePtr aFeature = theComposite->subFeature(anIndex);
345 if (isDumped(EntityPtr(aFeature)))
349 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
350 if (aCompFeat) // iteratively process composite features
351 isOk = process(aCompFeat) && isOk;
353 dumpFeature(aFeature, true);
356 bool isDumpSetName = !myEntitiesStack.empty() &&
357 myEntitiesStack.top().myEntity == EntityPtr(theComposite);
358 bool isForceModelDo = isSubDumped && isDumpSetName &&
359 (myEntitiesStack.top().myUserName || !myEntitiesStack.top().myResults.empty());
360 // It is necessary for the sketch to create its result when complete (command "model.do()").
361 // This option is set by flat theDumpModelDo.
362 // However, nested sketches are rebuilt by parent feature, so, they do not need
363 // explicit call of "model.do()". This will be controlled by the depth of the stack.
364 if (isForceModelDo || (theDumpModelDo && gCompositeStackDepth <= 1))
365 *this << "model.do()" << std::endl;
367 // dump "setName" for composite feature
373 void ModelHighAPI_Dumper::postpone(const EntityPtr& theEntity)
376 name(theEntity, false);
377 myPostponed.push_back(theEntity);
380 void ModelHighAPI_Dumper::dumpPostponed(bool theDumpFolders)
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);
399 myPostponed.push_back(*anIt);
402 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
404 dumpFeature(aFeature);
407 myDumpPostponedInProgress = false;
410 void ModelHighAPI_Dumper::dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
411 const FeaturePtr& theSubFeature)
413 name(theSubFeature, false);
414 myNames[theSubFeature] = EntityName(theSubFeatureGet, theSubFeature->name(), false);
416 // store results if they have user-defined names or colors
417 std::list<ResultPtr> aResultsWithNameOrColor;
418 const std::list<ResultPtr>& aResults = theSubFeature->results();
419 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
420 for (; aResIt != aResults.end(); ++aResIt) {
421 std::string aResName = (*aResIt)->data()->name();
422 myNames[*aResIt] = EntityName(aResName, aResName, false);
423 aResultsWithNameOrColor.push_back(*aResIt);
426 // store just dumped entity to stack
427 myEntitiesStack.push(LastDumpedEntity(theSubFeature, true, aResultsWithNameOrColor));
432 bool ModelHighAPI_Dumper::exportTo(const std::string& theFileName)
435 OSD_OpenStream(aFile, theFileName.c_str(), std::ofstream::out);
436 if (!aFile.is_open())
440 for (ModulesMap::const_iterator aModIt = myModules.begin();
441 aModIt != myModules.end(); ++aModIt) {
442 aFile << "from " << aModIt->first << " import ";
443 if (aModIt->second.empty() ||
444 aModIt->second.find(std::string()) != aModIt->second.end())
445 aFile << "*"; // import whole module
447 // import specific features
448 std::set<std::string>::const_iterator anObjIt = aModIt->second.begin();
450 for (++anObjIt; anObjIt != aModIt->second.end(); ++anObjIt)
451 aFile << ", " << *anObjIt;
455 if (!myModules.empty())
458 aFile << "from salome.shaper import model" << std::endl << std::endl;
459 aFile << "model.begin()" << std::endl;
461 // dump collected data
462 aFile << myFullDump.str();
463 aFile << myDumpBuffer.str();
466 aFile << "model.end()" << std::endl;
474 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName,
475 const std::string& theObject)
477 myModules[theModuleName].insert(theObject);
480 void ModelHighAPI_Dumper::dumpEntitySetName()
482 const LastDumpedEntity& aLastDumped = myEntitiesStack.top();
483 bool isBufferEmpty = myDumpBuffer.str().empty();
485 // dump "setName" for the entity
486 if (aLastDumped.myUserName) {
487 EntityName& anEntityNames = myNames[aLastDumped.myEntity];
488 if (!anEntityNames.myIsDefault)
489 myDumpBuffer << anEntityNames.myCurrentName << ".setName(\""
490 << anEntityNames.myUserName << "\")" << std::endl;
491 // don't dump "setName" for the entity twice
492 anEntityNames.myUserName.clear();
493 anEntityNames.myIsDefault = true;
495 // dump "setName" for results
496 std::list<ResultPtr>::const_iterator aResIt = aLastDumped.myResults.begin();
497 std::list<ResultPtr>::const_iterator aResEnd = aLastDumped.myResults.end();
498 for (; aResIt != aResEnd; ++aResIt) {
500 EntityName& anEntityNames = myNames[*aResIt];
501 if (!anEntityNames.myIsDefault) {
503 myDumpBuffer << ".setName(\"" << anEntityNames.myUserName << "\")" << std::endl;
504 // don't dump "setName" for the entity twice
505 anEntityNames.myUserName.clear();
506 anEntityNames.myIsDefault = true;
509 if (!isDefaultColor(*aResIt)) {
510 AttributeIntArrayPtr aColor = (*aResIt)->data()->intArray(ModelAPI_Result::COLOR_ID());
511 if (aColor && aColor->isInitialized()) {
513 myDumpBuffer << ".setColor(" << aColor->value(0) << ", " << aColor->value(1)
514 << ", " << aColor->value(2) << ")" << std::endl;
517 // set result deflection
518 if (!isDefaultDeflection(*aResIt)) {
519 AttributeDoublePtr aDeflectionAttr =
520 (*aResIt)->data()->real(ModelAPI_Result::DEFLECTION_ID());
521 if(aDeflectionAttr.get() && aDeflectionAttr->isInitialized()) {
523 myDumpBuffer << ".setDeflection(" << aDeflectionAttr->value() << ")" << std::endl;
526 // set result transparency
527 if (!isDefaultTransparency(*aResIt)) {
528 AttributeDoublePtr aTransparencyAttr =
529 (*aResIt)->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
530 if(aTransparencyAttr.get() && aTransparencyAttr->isInitialized()) {
532 myDumpBuffer << ".setTransparency(" << aTransparencyAttr->value() << ")" << std::endl;
537 myNames[aLastDumped.myEntity].myIsDumped = true;
538 myEntitiesStack.pop();
540 // clean buffer if it was clear before
542 myFullDump << myDumpBuffer.str();
543 myDumpBuffer.str("");
547 bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const
549 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
550 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
551 return (aFound != myNames.end() && aFound->second.myIsDumped) ||
552 myFeaturesToSkip.find(aFeature) != myFeaturesToSkip.end();
555 bool ModelHighAPI_Dumper::isDumped(const AttributeRefAttrPtr& theRefAttr) const
558 if (theRefAttr->isObject())
559 aFeature = ModelAPI_Feature::feature(theRefAttr->object());
561 aFeature = ModelAPI_Feature::feature(theRefAttr->attr()->owner());
562 return aFeature && isDumped(EntityPtr(aFeature));
565 bool ModelHighAPI_Dumper::isDumped(const AttributeRefListPtr& theRefList) const
567 std::list<ObjectPtr> aRefs = theRefList->list();
568 std::list<ObjectPtr>::iterator anIt = aRefs.begin();
569 for (; anIt != aRefs.end(); ++anIt) {
570 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
571 if (aFeature && !isDumped(EntityPtr(aFeature)))
577 bool ModelHighAPI_Dumper::isDefaultColor(const ResultPtr& theResult) const
579 AttributeIntArrayPtr aColor = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
580 if (!aColor || !aColor->isInitialized())
583 std::string aSection, aName, aDefault;
584 theResult->colorConfigInfo(aSection, aName, aDefault);
586 // dump current color
587 std::ostringstream aColorInfo;
588 aColorInfo << aColor->value(0) << "," << aColor->value(1) << "," << aColor->value(2);
590 return aDefault == aColorInfo.str();
593 bool ModelHighAPI_Dumper::isDefaultDeflection(const ResultPtr& theResult) const
595 AttributeDoublePtr aDeflectionAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
596 if(!aDeflectionAttr || !aDeflectionAttr->isInitialized()) {
600 double aCurrent = aDeflectionAttr->value();
601 double aDefault = -1;
603 bool isConstruction = false;
604 std::string aResultGroup = theResult->groupName();
605 if (aResultGroup == ModelAPI_ResultConstruction::group())
606 isConstruction = true;
607 else if (aResultGroup == ModelAPI_ResultBody::group()) {
608 GeomShapePtr aGeomShape = theResult->shape();
609 if (aGeomShape.get()) {
610 // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
611 // correction of deviation for them should not influence to the application performance
612 GeomAPI_ShapeExplorer anExp(aGeomShape, GeomAPI_Shape::FACE);
613 isConstruction = !anExp.more();
617 aDefault = Config_PropManager::real("Visualization", "construction_deflection");
619 aDefault = Config_PropManager::real("Visualization", "body_deflection");
621 return fabs(aCurrent - aDefault) < 1.e-12;
624 bool ModelHighAPI_Dumper::isDefaultTransparency(const ResultPtr& theResult) const
626 AttributeDoublePtr anAttribute = theResult->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
627 if(!anAttribute || !anAttribute->isInitialized()) {
630 return fabs(anAttribute->value()) < 1.e-12;
633 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
635 myDumpBuffer << theChar;
639 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
641 myDumpBuffer << theString;
645 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
647 myDumpBuffer << theString;
651 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const bool theValue)
653 myDumpBuffer << (theValue ? "True" : "False");
657 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
659 myDumpBuffer << theValue;
663 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
665 myDumpBuffer << theValue;
669 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
671 importModule("GeomAPI", "GeomAPI_Pnt");
672 myDumpBuffer << "GeomAPI_Pnt(" << thePoint->x() << ", "
673 << thePoint->y() << ", " << thePoint->z() << ")";
677 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
679 importModule("GeomAPI", "GeomAPI_Dir");
680 myDumpBuffer << "GeomAPI_Dir(" << theDir->x() << ", "
681 << theDir->y() << ", " << theDir->z() << ")";
685 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
686 const std::shared_ptr<GeomDataAPI_Dir>& theDir)
688 myDumpBuffer << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
692 static void dumpArray(std::ostringstream& theOutput, int theSize,
693 double* theValues, std::string* theTexts)
695 for (int i = 0; i < theSize; ++i) {
698 if (theTexts[i].empty())
699 theOutput << theValues[i];
701 theOutput << "\"" << theTexts[i] << "\"";
705 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
706 const std::shared_ptr<GeomDataAPI_Point>& thePoint)
708 static const int aSize = 3;
709 double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
710 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()};
711 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
715 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
716 const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
718 static const int aSize = 2;
719 double aValues[aSize] = {thePoint->x(), thePoint->y()};
720 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()};
721 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
725 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
726 const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
728 myDumpBuffer << (theAttrBool->value() ? "True" : "False");
732 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
733 const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
735 std::string aText = theAttrInt->text();
737 myDumpBuffer << theAttrInt->value();
739 myDumpBuffer << "\"" << aText << "\"";
743 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
744 const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
746 std::string aText = theAttrReal->text();
748 myDumpBuffer << theAttrReal->value();
750 myDumpBuffer << "\"" << aText << "\"";
754 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
755 const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
757 myDumpBuffer << "\"" << theAttrStr->value() << "\"";
761 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FolderPtr& theFolder)
763 myDumpBuffer << name(theFolder);
765 // add dumped folder to a stack
766 if (!myNames[theFolder].myIsDumped &&
767 (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theFolder))
768 myEntitiesStack.push(LastDumpedEntity(theFolder, !myNames[theFolder].myIsDefault));
773 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity)
775 myDumpBuffer << name(theEntity);
777 if (!myNames[theEntity].myIsDumped) {
778 bool isUserDefinedName = !myNames[theEntity].myIsDefault;
779 // store results if they have user-defined names or colors
780 std::list<ResultPtr> aResultsWithNameOrColor;
781 const std::list<ResultPtr>& aResults = theEntity->results();
782 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
783 for (; aResIt != aResults.end(); ++aResIt) {
784 if (!myNames[*aResIt].myIsDefault || !isDefaultColor(*aResIt) ||
785 !isDefaultDeflection(*aResIt) || !isDefaultTransparency(*aResIt))
786 aResultsWithNameOrColor.push_back(*aResIt);
788 ResultCompSolidPtr aCompSolid =
789 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aResIt);
791 int aNbSubs = aCompSolid->numberOfSubs();
792 for (int i = 0; i < aNbSubs; ++i) {
793 ResultPtr aCurRes = aCompSolid->subResult(i);
794 if (!myNames[aCurRes].myIsDefault || !isDefaultColor(aCurRes) ||
795 !isDefaultDeflection(aCurRes) || !isDefaultTransparency(aCurRes))
796 aResultsWithNameOrColor.push_back(aCurRes);
800 // store just dumped entity to stack
801 if (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theEntity)
802 myEntitiesStack.push(
803 LastDumpedEntity(theEntity, isUserDefinedName, aResultsWithNameOrColor));
806 // remove entity from the list of not dumped items
807 myNotDumpedEntities.erase(theEntity);
811 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ResultPtr& theResult)
813 FeaturePtr aFeature = ModelAPI_Feature::feature(theResult);
816 std::list<ResultPtr> aResults = aFeature->results();
817 for(std::list<ResultPtr>::const_iterator
818 anIt = aResults.cbegin(); anIt != aResults.cend(); ++anIt, ++anIndex) {
819 if(theResult->isSame(*anIt)) {
823 ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*anIt);
825 int aNbSubs = aCompSolid->numberOfSubs();
826 for (aSubIndex = 0; aSubIndex < aNbSubs; ++aSubIndex)
827 if (theResult->isSame(aCompSolid->subResult(aSubIndex)))
829 if (aSubIndex < aNbSubs)
835 myDumpBuffer << name(aFeature);
837 myDumpBuffer << ".result()";
839 myDumpBuffer << ".results()[" << anIndex << "]";
841 if (aSubIndex >= 0) {
842 myDumpBuffer << ".subResult(" << aSubIndex << ")";
847 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ObjectPtr& theObject)
849 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
851 myDumpBuffer << name(aFeature);
855 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
864 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const AttributePtr& theAttr)
866 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttr->owner());
868 std::string aWrapperPrefix, aWrapperSuffix;
869 // Check the attribute belongs to copied (in multi-translation or multi-rotation) feature.
870 // In this case we need to cast explicitly feature to appropriate type.
871 AttributeBooleanPtr isCopy = anOwner->boolean("Copy");
872 if (isCopy.get() && isCopy->value()) {
873 aWrapperPrefix = featureWrapper(anOwner) + "(";
874 aWrapperSuffix = ")";
875 importModule("SketchAPI");
878 myDumpBuffer << aWrapperPrefix << name(anOwner) << aWrapperSuffix
879 << "." << attributeGetter(anOwner, theAttr->id()) << "()";
883 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
884 const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
886 if (theRefAttr->isObject())
887 *this << theRefAttr->object();
889 *this << theRefAttr->attr();
893 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
894 const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList)
897 std::list<std::pair<ObjectPtr, AttributePtr> > aList = theRefAttrList->list();
898 bool isAdded = false;
899 std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aList.begin();
900 for (; anIt != aList.end(); ++anIt) {
902 myDumpBuffer << ", ";
906 *this << anIt->first;
907 else if (anIt->second)
908 * this << anIt->second;
914 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
915 const std::shared_ptr<ModelAPI_AttributeReference>& theReference)
917 *this << theReference->value();
921 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
922 const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList)
924 static const int aThreshold = 2;
925 // if number of elements in the list if greater than a threshold,
926 // dump it in a separate line with specific name
927 std::string aDumped = myDumpBuffer.str();
928 if (aDumped.empty() || theRefList->size() <= aThreshold) {
930 std::list<ObjectPtr> aList = theRefList->list();
931 bool isAdded = false;
932 std::list<ObjectPtr>::const_iterator anIt = aList.begin();
933 for (; anIt != aList.end(); ++anIt) {
935 myDumpBuffer << ", ";
943 // clear buffer and store list "as is"
944 myDumpBuffer.str("");
946 // save buffer and clear it again
947 std::string aDumpedList = myDumpBuffer.str();
948 myDumpBuffer.str("");
949 // obtain name of list
950 FeaturePtr anOwner = ModelAPI_Feature::feature(theRefList->owner());
951 std::string aListName = name(anOwner) + "_objects";
952 // store all previous data
953 myDumpBuffer << aListName << " = " << aDumpedList << std::endl
954 << aDumped << aListName;
959 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
960 const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
962 myDumpBuffer << "model.selection(";
964 if(!theAttrSelect->isInitialized()) {
969 GeomShapePtr aShape = theAttrSelect->value();
971 aShape = theAttrSelect->context()->shape();
979 myDumpBuffer << "\"" << aShape->shapeTypeStr() << "\", \"" <<
980 theAttrSelect->namingName() << "\")";
984 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
985 const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList)
987 static const int aThreshold = 2;
988 // if number of elements in the list if greater than a threshold,
989 // dump it in a separate line with specific name
990 std::string aDumped = myDumpBuffer.str();
992 if (aDumped.empty() || theAttrSelList->size() <= aThreshold) {
996 std::string aShapeTypeStr;
998 bool isAdded = false;
1000 for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) {
1001 AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex);
1002 aShape = anAttribute->value();
1004 ResultPtr aContext = anAttribute->context();
1006 aShape = aContext->shape();
1014 myDumpBuffer << ", ";
1018 myDumpBuffer << "model.selection(\"" <<
1019 aShape->shapeTypeStr() << "\", \"" << anAttribute->namingName() << "\")";
1022 myDumpBuffer << "]";
1024 // clear buffer and store list "as is"
1025 myDumpBuffer.str("");
1026 *this << theAttrSelList;
1027 // save buffer and clear it again
1028 std::string aDumpedList = myDumpBuffer.str();
1029 myDumpBuffer.str("");
1030 // obtain name of list (the feature may contain several selection lists)
1031 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttrSelList->owner());
1032 std::string aListName = name(anOwner) + "_objects";
1033 std::list<AttributePtr> aSelLists =
1034 anOwner->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
1035 if (aSelLists.size() > 1) {
1037 for (std::list<AttributePtr>::iterator aSIt = aSelLists.begin();
1038 aSIt != aSelLists.end(); ++aSIt, ++anIndex)
1039 if ((*aSIt).get() == theAttrSelList.get())
1041 std::ostringstream aSStream;
1042 aSStream << aListName << "_" << anIndex;
1043 aListName = aSStream.str();
1045 // store all previous data
1046 myDumpBuffer << aListName << " = " << aDumpedList << std::endl
1047 << aDumped << aListName;
1052 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1053 const std::shared_ptr<ModelAPI_AttributeStringArray>& theArray)
1056 for(int anIndex = 0; anIndex < theArray->size(); ++anIndex) {
1060 myDumpBuffer<<"\""<<theArray->value(anIndex)<<"\"";
1068 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
1069 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
1071 theDumper.myDumpBuffer << theEndl;
1073 if (!theDumper.myEntitiesStack.empty()) {
1075 // all copies have been stored into stack, pop them all
1078 // Name for composite feature is dumped when all sub-entities are dumped
1079 // (see method ModelHighAPI_Dumper::processSubs).
1080 const ModelHighAPI_Dumper::LastDumpedEntity& aLastDumped = theDumper.myEntitiesStack.top();
1081 CompositeFeaturePtr aComposite =
1082 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aLastDumped.myEntity);
1084 theDumper.dumpEntitySetName();
1085 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aLastDumped.myEntity);
1087 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
1088 isCopy = aCopyAttr.get() && aCopyAttr->value();
1091 } while (isCopy && !theDumper.myEntitiesStack.empty());
1094 // store all not-dumped entities first
1095 std::set<EntityPtr> aNotDumped = theDumper.myNotDumpedEntities;
1096 std::string aBufCopy = theDumper.myDumpBuffer.str();
1097 theDumper.clear(true);
1098 std::set<EntityPtr>::const_iterator anIt = aNotDumped.begin();
1099 for (; anIt != aNotDumped.end(); ++anIt) {
1100 // if the feature is composite, dump it with all subs
1101 CompositeFeaturePtr aCompFeat =
1102 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
1104 theDumper.process(aCompFeat, true);
1106 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
1107 theDumper.dumpFeature(aFeature, true);
1108 // dump the Projection feature which produces this "Copy" entity
1109 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
1110 if (aCopyAttr.get() && aCopyAttr->value())
1112 const std::set<AttributePtr>& aRefs = aFeature->data()->refsToMe();
1113 std::set<AttributePtr>::iterator aRefIt = aRefs.begin();
1114 for (; aRefIt != aRefs.end(); ++aRefIt)
1115 if ((*aRefIt)->id() == "ProjectedFeature")
1116 { // process projection only
1117 FeaturePtr anOwner = ModelAPI_Feature::feature((*aRefIt)->owner());
1118 if (anOwner && !theDumper.isDumped(EntityPtr(anOwner)))
1119 theDumper.dumpFeature(anOwner, true);
1125 // avoid multiple empty lines
1126 size_t anInd = std::string::npos;
1127 while ((anInd = aBufCopy.find("\n\n\n")) != std::string::npos)
1128 aBufCopy.erase(anInd, 1);
1129 // then store currently dumped string
1130 theDumper.myFullDump << aBufCopy;
1132 // now, store all postponed features
1133 theDumper.dumpPostponed();