X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FModelHighAPI%2FModelHighAPI_Dumper.cpp;h=ef6f1ee621e4e62e6e1d8153220b6f7791a9e2a9;hb=11c03dec886fc348614782ffbbf2be3210efaca4;hp=1176de251e001ef09757c5da6134a53f99c1ee79;hpb=2895c0fa0d69afc25ef86a300ef00d850a3df832;p=modules%2Fshaper.git diff --git a/src/ModelHighAPI/ModelHighAPI_Dumper.cpp b/src/ModelHighAPI/ModelHighAPI_Dumper.cpp index 1176de251..ef6f1ee62 100644 --- a/src/ModelHighAPI/ModelHighAPI_Dumper.cpp +++ b/src/ModelHighAPI/ModelHighAPI_Dumper.cpp @@ -1,10 +1,22 @@ -// Copyright (C) 2016-20xx CEA/DEN, EDF R&D --> +// Copyright (C) 2014-2019 CEA/DEN, EDF R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// -// File: ModelHighAPI_Dumper.cpp -// Created: 1 August 2016 -// Author: Artem ZHIDKOV - -//-------------------------------------------------------------------------------------- #include "ModelHighAPI_Dumper.h" #include @@ -12,6 +24,8 @@ #include #include #include +#include +#include #include #include @@ -33,10 +47,17 @@ #include #include #include +#include +#include #include #include #include +#include #include +#include +#include + +#include #include @@ -44,13 +65,408 @@ #include +// =========== Implementation of storage of dumped data =========== +static const int THE_DUMP_PRECISION = 16; + +class ModelHighAPI_Dumper::DumpStorageBuffer : public ModelHighAPI_Dumper::DumpStorage +{ +public: + void addStorage(const ModelHighAPI_Dumper::DumpStoragePtr& theStorage) + { myStorageArray.push_back(theStorage); } + + void clear() { myStorageArray.clear(); } + + bool isBufferEmpty() + { + return myStorageArray.empty() || myStorageArray.front()->buffer().str().empty(); + } + + void mergeBuffer() + { + std::list::iterator anIt = myStorageArray.begin(); + for (; anIt != myStorageArray.end(); ++anIt) { + // avoid multiple empty lines + std::string aBuf = (*anIt)->buffer().str(); + size_t anInd = std::string::npos; + while ((anInd = aBuf.find("\n\n\n")) != std::string::npos) + aBuf.erase(anInd, 1); + + (*anIt)->fullDump() << aBuf; + (*anIt)->buffer().str(""); + } + } + + void write(const std::string& theValue) + { + if (myStorageArray.empty()) + addStorage(DumpStoragePtr(new DumpStorage)); + + std::list::iterator anIt = myStorageArray.begin(); + for (; anIt != myStorageArray.end(); ++anIt) + (*anIt)->buffer() << theValue; + } + + DumpStorageBuffer& operator<<(const char theChar) + { + std::ostringstream out; + out << theChar; + write(out.str()); + return *this; + } + + DumpStorageBuffer& operator<<(const char* theString) + { + write(theString); + return *this; + } + + DumpStorageBuffer& operator<<(const std::string& theString) + { + write(theString); + return *this; + } + + DumpStorageBuffer& operator<<(const bool theValue) + { + std::ostringstream out; + out << theValue; + write(out.str()); + return *this; + } + + DumpStorageBuffer& operator<<(const int theValue) + { + std::ostringstream out; + out << theValue; + write(out.str()); + return *this; + } + + DumpStorageBuffer& operator<<(const double theValue) + { + std::ostringstream out; + out << std::setprecision(THE_DUMP_PRECISION) << theValue; + write(out.str()); + return *this; + } + /// Dump std::endl + friend + DumpStorageBuffer& operator<<(DumpStorageBuffer& theBuffer, + std::basic_ostream& (*theEndl)(std::basic_ostream&)) + { + theBuffer.write("\n"); + return theBuffer; + } + + void dumpArray(int theSize, double* theValues, std::string* theTexts) + { + std::ostringstream anOutput; + anOutput << std::setprecision(THE_DUMP_PRECISION); + for (int i = 0; i < theSize; ++i) { + if (i > 0) + anOutput << ", "; + if (theTexts[i].empty()) + anOutput << theValues[i]; + else + anOutput << "\"" << theTexts[i] << "\""; + } + write(anOutput.str()); + } + + virtual void write(const std::shared_ptr& theAttrSelect) + { + if (myStorageArray.empty()) + addStorage(DumpStoragePtr(new DumpStorage)); + + std::list::iterator anIt = myStorageArray.begin(); + for (; anIt != myStorageArray.end(); ++anIt) + (*anIt)->write(theAttrSelect); + } + + virtual void reserveBuffer() + { + std::list::iterator anIt = myStorageArray.begin(); + for (; anIt != myStorageArray.end(); ++anIt) + (*anIt)->reserveBuffer(); + } + + virtual void restoreReservedBuffer() + { + std::list::iterator anIt = myStorageArray.begin(); + for (; anIt != myStorageArray.end(); ++anIt) + (*anIt)->restoreReservedBuffer(); + } + + virtual bool exportTo(const std::string& theFilename, const ModulesSet& theUsedModules) + { + static const std::string THE_EXT = ".py"; + std::string aFilenameBase = theFilename; + if (aFilenameBase.rfind(THE_EXT) == aFilenameBase.size() - THE_EXT.size()) + aFilenameBase = aFilenameBase.substr(0, aFilenameBase.size() - THE_EXT.size()); + + bool isOk = true; + std::list::iterator anIt = myStorageArray.begin(); + for (; anIt != myStorageArray.end(); ++anIt) { + std::string aFilename = aFilenameBase + (*anIt)->myFilenameSuffix + THE_EXT; + isOk = (*anIt)->exportTo(aFilename, theUsedModules) && isOk; + } + return isOk; + } + +private: + std::list myStorageArray; +}; + + +ModelHighAPI_Dumper::DumpStorage::DumpStorage(const DumpStorage& theOther) + : myFilenameSuffix(theOther.myFilenameSuffix), + myDumpBufferHideout(theOther.myDumpBufferHideout) +{ + myFullDump.str(theOther.myFullDump.str()); + myDumpBuffer.str(theOther.myDumpBuffer.str()); +} + +const ModelHighAPI_Dumper::DumpStorage& +ModelHighAPI_Dumper::DumpStorage::operator=(const ModelHighAPI_Dumper::DumpStorage& theOther) +{ + myFilenameSuffix = theOther.myFilenameSuffix; + myFullDump.str(theOther.myFullDump.str()); + myDumpBuffer.str(theOther.myDumpBuffer.str()); + myDumpBufferHideout = theOther.myDumpBufferHideout; + return *this; +} + +void ModelHighAPI_Dumper::DumpStorage::reserveBuffer() +{ + myDumpBufferHideout.push(myDumpBuffer.str()); + myDumpBuffer.str(""); +} + +void ModelHighAPI_Dumper::DumpStorage::restoreReservedBuffer() +{ + myDumpBuffer << myDumpBufferHideout.top(); + myDumpBufferHideout.pop(); +} + +bool ModelHighAPI_Dumper::DumpStorage::exportTo(const std::string& theFilename, + const ModulesSet& theUsedModules) +{ + std::ofstream aFile; + OSD_OpenStream(aFile, theFilename.c_str(), std::ofstream::out); + if (!aFile.is_open()) + return false; + + // standard header imported modules + for (ModulesSet::const_iterator aModIt = theUsedModules.begin(); + aModIt != theUsedModules.end(); ++aModIt) { + aFile << "from " << *aModIt << " import *" << std::endl; + } + if (!theUsedModules.empty()) + aFile << std::endl; + + aFile << "from salome.shaper import model" << std::endl << std::endl; + aFile << "model.begin()" << std::endl; + + // dump collected data + aFile << myFullDump.str(); + aFile << myDumpBuffer.str(); + + // standard footer + aFile << "model.end()" << std::endl; + aFile.close(); + + return true; +} + +static void getShapeAndContext(const AttributeSelectionPtr& theAttrSelect, + GeomShapePtr& theShape, ResultPtr& theContext) +{ + if (theAttrSelect->isInitialized()) { + theShape = theAttrSelect->value(); + theContext = theAttrSelect->context(); + if (!theShape.get()) + theShape = theContext->shape(); + + if (theAttrSelect->isGeometricalSelection() && + theShape.get() && theShape->shapeType() == GeomAPI_Shape::COMPOUND && + theContext.get() && !theShape->isEqual(theContext->shape()) && + theContext->groupName() != ModelAPI_ResultPart::group() && + theContext->groupName() != ModelAPI_ResultGroup::group()) { + GeomAPI_ShapeIterator anIt(theShape); + theShape = anIt.current(); + } + } +} + +void ModelHighAPI_Dumper::DumpStorage::write(const AttributeSelectionPtr& theAttrSelect) +{ + myDumpBuffer << "model.selection("; + + GeomShapePtr aShape; + ResultPtr aContext; + getShapeAndContext(theAttrSelect, aShape, aContext); + + if (aShape.get()) { + myDumpBuffer << "\"" << aShape->shapeTypeStr() << "\", \"" + << theAttrSelect->namingName() << "\""; + } + + myDumpBuffer << ")"; +} + +static int possibleSelectionsByPoint(const GeomPointPtr& thePoint, + const ResultPtr& theResult, + const GeomShapePtr& theShape, + const FeaturePtr& theStartFeature, + const FeaturePtr& theEndFeature) +{ + DocumentPtr aDoc1 = theStartFeature->document(); + DocumentPtr aDoc2 = theEndFeature->document(); + + std::list aFeatures = aDoc1->allFeatures(); + if (aDoc1 != aDoc2) { + std::list anAdditionalFeatures = aDoc2->allFeatures(); + aFeatures.insert(aFeatures.end(), anAdditionalFeatures.begin(), anAdditionalFeatures.end()); + } + + CompositeFeaturePtr aLastCompositeFeature; + + std::list::const_iterator aFIt = aFeatures.begin(); + while (aFIt != aFeatures.end() && *aFIt != theStartFeature) { + CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast(*aFIt); + if (aCompFeat) + aLastCompositeFeature = aCompFeat; + ++aFIt; + } + + // collect the list of composite features, containing the last feature; + // these features should be excluded from searching, + // because the feature cannot select sub-shapes from its parent + std::set aEndFeatureParents = ModelAPI_Tools::getParents(theEndFeature); + + int aNbPossibleSelections = 0; + for (; aFIt != aFeatures.end() && *aFIt != theEndFeature; ++aFIt) { + bool isSkipFeature = false; + if (aLastCompositeFeature && aLastCompositeFeature->isSub(*aFIt)) + isSkipFeature = true; + CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast(*aFIt); + if (aCompFeat) { + ResultPartPtr aPartRes = + std::dynamic_pointer_cast(aCompFeat->firstResult()); + if (!aPartRes) + aLastCompositeFeature = aCompFeat; + if (aEndFeatureParents.find(aCompFeat) != aEndFeatureParents.end()) { + // do not process the parent for the last feature, + // because it cannot select objects from its parent + isSkipFeature = true; + } + } + if (isSkipFeature) + continue; + + std::list anApproproate; + if (ModelGeomAlgo_Shape::findSubshapeByPoint(*aFIt, thePoint, theShape->shapeType(), + anApproproate)) { + std::list::iterator anApIt = anApproproate.begin(); + for (; anApIt != anApproproate.end(); ++anApIt) { + ++aNbPossibleSelections; + + // stop if the target shape and result are found + GeomShapePtr aCurShape = anApIt->mySubshape; + if (!aCurShape) + aCurShape = anApIt->myResult->shape(); + + if (anApIt->myResult->isSame(theResult) && aCurShape->isSame(theShape)) + break; + } + } + } + return aNbPossibleSelections; +} + +void ModelHighAPI_Dumper::DumpStorageGeom::write(const AttributeSelectionPtr& theAttrSelect) +{ + GeomShapePtr aShape; + ResultPtr aContext; + getShapeAndContext(theAttrSelect, aShape, aContext); + + // how to dump selection: construction features are dumped by name always + FeaturePtr aSelectedFeature; + FeaturePtr aFeature = theAttrSelect->contextFeature(); + if (aShape && aContext && !aFeature) + aSelectedFeature = ModelAPI_Feature::feature(aContext->data()->owner()); + bool isDumpByGeom = aSelectedFeature && aSelectedFeature->isInHistory(); + + if (isDumpByGeom) { + myDumpBuffer << "model.selection(\"" << aShape->shapeTypeStr(); + // check the selected item is a ResultPart; + // in this case it is necessary to get shape with full transformation + // for correct calculation of the middle point + ResultPartPtr aResPart = + std::dynamic_pointer_cast(theAttrSelect->context()); + if (aResPart && aShape->shapeType() == GeomAPI_Shape::COMPOUND) + aShape = aResPart->shape(); + GeomPointPtr aMiddlePoint = aShape->middlePoint(); + // calculate number of features, which could be selected by the same point + FeaturePtr anOwner = ModelAPI_Feature::feature(theAttrSelect->owner()); + int aNbPossibleSelections = possibleSelectionsByPoint(aMiddlePoint, + theAttrSelect->context(), aShape, aSelectedFeature, anOwner); + + // produce the index if the number of applicable features is greater than 1 + std::string anIndex; + if (aNbPossibleSelections > 1) { + std::ostringstream anOutput; + anOutput << "_" << aNbPossibleSelections; + anIndex = anOutput.str(); + } + + myDumpBuffer << std::setprecision(THE_DUMP_PRECISION) + << anIndex << "\", (" + << aMiddlePoint->x() << ", " + << aMiddlePoint->y() << ", " + << aMiddlePoint->z() << ")"; + myDumpBuffer << ")"; + } + else + DumpStorage::write(theAttrSelect); +} + +void ModelHighAPI_Dumper::DumpStorageWeak::write(const AttributeSelectionPtr& theAttrSelect) +{ + GeomShapePtr aShape; + ResultPtr aContext; + getShapeAndContext(theAttrSelect, aShape, aContext); + + bool aStandardDump = true; + if (aShape.get() && aContext.get() && + aShape != aContext->shape()) { // weak naming for local selection only + GeomAlgoAPI_NExplode aNExplode(aContext->shape(), aShape->shapeType()); + int anIndex = aNExplode.index(aShape); + if (anIndex != 0) { // found a week-naming index, so, export it + myDumpBuffer << "model.selection(\"" << aShape->shapeTypeStr() << "\", \"" + << theAttrSelect->contextName(aContext) << "\", " << anIndex << ")"; + aStandardDump = false; + } + } + if (aStandardDump) + DumpStorage::write(theAttrSelect); +} +// ====================================================================== + + static int gCompositeStackDepth = 0; ModelHighAPI_Dumper* ModelHighAPI_Dumper::mySelf = 0; ModelHighAPI_Dumper::ModelHighAPI_Dumper() + : myDumpStorage(new DumpStorageBuffer), + myDumpPostponedInProgress(false) { - clear(); +} + +ModelHighAPI_Dumper::~ModelHighAPI_Dumper() +{ + delete myDumpStorage; } void ModelHighAPI_Dumper::setInstance(ModelHighAPI_Dumper* theDumper) @@ -64,23 +480,22 @@ ModelHighAPI_Dumper* ModelHighAPI_Dumper::getInstance() return mySelf; } -void ModelHighAPI_Dumper::clear(bool bufferOnly) +void ModelHighAPI_Dumper::addCustomStorage(const ModelHighAPI_Dumper::DumpStoragePtr& theStorage) { - myDumpBuffer.str(""); - myDumpBuffer << std::setprecision(16); + myDumpStorage->addStorage(theStorage); +} +void ModelHighAPI_Dumper::clearCustomStorage() +{ + myDumpStorage->clear(); + + myNames.clear(); + myModules.clear(); + myFeatureCount.clear(); + myPostponed.clear(); + while (!myEntitiesStack.empty()) + myEntitiesStack.pop(); clearNotDumped(); - - if (!bufferOnly) { - myFullDump.str(""); - myFullDump << std::setprecision(16); - - myNames.clear(); - myModules.clear(); - myFeatureCount.clear(); - while (!myEntitiesStack.empty()) - myEntitiesStack.pop(); - } } void ModelHighAPI_Dumper::clearNotDumped() @@ -109,16 +524,28 @@ const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity, return aFound->second.myCurrentName; // entity is not found, store it - std::string aName; + std::string aName, aKind; bool isDefaultName = false; + bool isSaveNotDumped = theSaveNotDumped; std::ostringstream aDefaultName; FeaturePtr aFeature = std::dynamic_pointer_cast(theEntity); if (aFeature) { aName = aFeature->name(); - const std::string& aKind = aFeature->getKind(); - DocumentPtr aDoc = aFeature->document(); - int& aNbFeatures = myFeatureCount[aDoc][aKind]; - aNbFeatures += 1; + aKind = aFeature->getKind(); + } else { + FolderPtr aFolder = std::dynamic_pointer_cast(theEntity); + if (aFolder) { + aName = aFolder->data()->name(); + aKind = ModelAPI_Folder::ID(); + isSaveNotDumped = false; + } + } + + ObjectPtr anObject = std::dynamic_pointer_cast(theEntity); + if (anObject) { + DocumentPtr aDoc = anObject->document(); + std::pair& aNbFeatures = myFeatureCount[aDoc][aKind]; + aNbFeatures.first += 1; size_t anIndex = aName.find(aKind); if (anIndex == 0 && aName[aKind.length()] == '_') { // name starts with "FeatureKind_" @@ -127,10 +554,24 @@ const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity, // Check number of already registered objects of such kind. Index of current object // should be the same to identify feature's name as automatically generated. - if (aNbFeatures == anId) { + if (aNbFeatures.first == anId && aNbFeatures.second < anId) { // name is not user-defined isDefaultName = true; + + // check there are postponed features of this kind, + // dump their names, because the sequence of features may be changed + for (std::list::const_iterator aPpIt = myPostponed.begin(); + aPpIt != myPostponed.end(); ++aPpIt) { + FeaturePtr aCurFeature = std::dynamic_pointer_cast(*aPpIt); + if (aCurFeature && aCurFeature->getKind() == aKind) { + myNames[*aPpIt].myIsDefault = false; + isDefaultName = false; + } + } } + + if (anId > aNbFeatures.second) + aNbFeatures.second = anId; } // obtain default name for the feature @@ -140,16 +581,17 @@ const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity, int aFullIndex = 0; NbFeaturesMap::const_iterator aFIt = myFeatureCount.begin(); for (; aFIt != myFeatureCount.end(); ++aFIt) { - std::map::const_iterator aFound = aFIt->second.find(aKind); + std::map >::const_iterator aFound = + aFIt->second.find(aKind); if (aFound != aFIt->second.end()) - aFullIndex += aFound->second; + aFullIndex += aFound->second.first; } aDefaultName << aKind << "_" << aFullIndex; } } myNames[theEntity] = EntityName(aDefaultName.str(), aName, isDefaultName); - if (theSaveNotDumped) + if (isSaveNotDumped) myNotDumpedEntities.insert(theEntity); // store names of results @@ -177,32 +619,19 @@ const std::string& ModelHighAPI_Dumper::parentName(const FeaturePtr& theEntity) void ModelHighAPI_Dumper::saveResultNames(const FeaturePtr& theFeature) { // Default name of the feature - const std::string& aKind = theFeature->getKind(); - DocumentPtr aDoc = theFeature->document(); - int aNbFeatures = myFeatureCount[aDoc][aKind]; - std::ostringstream aNameStream; - aNameStream << aKind << "_" << aNbFeatures; - std::string aFeatureName = aNameStream.str(); + bool isFeatureDefaultName = myNames[theFeature].myIsDefault; // Save only names of results which is not correspond to default feature name const std::list& aResults = theFeature->results(); - std::list::const_iterator aResIt = aResults.begin(); - for (int i = 1; aResIt != aResults.end(); ++aResIt, ++i) { - bool isUserDefined = true; - std::string aResName = (*aResIt)->data()->name(); - size_t anIndex = aResName.find(aFeatureName); - if (anIndex == 0) { - std::string aSuffix = aResName.substr(aFeatureName.length()); - if (aSuffix.empty() && i == 1) // first result may not constain index in the name - isUserDefined = false; - else { - if (aSuffix[0] == '_' && std::stoi(aSuffix.substr(1)) == i) - isUserDefined = false; - } - } - - myNames[*aResIt] = EntityName(aResName, - (isUserDefined ? aResName : std::string()), !isUserDefined); + std::list allRes; + ModelAPI_Tools::allResults(theFeature, allRes); + for(std::list::iterator aRes = allRes.begin(); aRes != allRes.end(); aRes++) { + std::pair aName = ModelAPI_Tools::getDefaultName(*aRes); + std::string aDefaultName = aName.first; + std::string aResName = (*aRes)->data()->name(); + bool isUserDefined = !(isFeatureDefaultName && aDefaultName == aResName); + myNames[*aRes] = + EntityName(aResName, (isUserDefined ? aResName : std::string()), !isUserDefined); } } @@ -215,25 +644,41 @@ bool ModelHighAPI_Dumper::process(const std::shared_ptr& theD *this << aDocName << " = model.moduleDocument()" << std::endl; // dump subfeatures and store result to file - return process(theDoc) && exportTo(theFileName); + bool isOk = process(theDoc) && myDumpStorage->exportTo(theFileName, myModules); + clearCustomStorage(); + return isOk; } bool ModelHighAPI_Dumper::process(const std::shared_ptr& theDoc) { bool isOk = true; - std::list aFeatures = theDoc->allFeatures(); - std::list::const_iterator aFeatIt = aFeatures.begin(); + std::list anObjects = theDoc->allObjects(); + std::list::const_iterator anObjIt = anObjects.begin(); // firstly, dump all parameters - for (; aFeatIt != aFeatures.end(); ++ aFeatIt) - dumpParameter(*aFeatIt); + for (; anObjIt != anObjects.end(); ++ anObjIt) { + FeaturePtr aFeature = std::dynamic_pointer_cast(*anObjIt); + if (aFeature) + dumpParameter(aFeature); + } // dump all other features - for (aFeatIt = aFeatures.begin(); aFeatIt != aFeatures.end(); ++aFeatIt) { - CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast(*aFeatIt); + for (anObjIt = anObjects.begin(); anObjIt != anObjects.end(); ++anObjIt) { + CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast(*anObjIt); if (aCompFeat) // iteratively process composite features isOk = process(aCompFeat) && isOk; - else if (!isDumped(*aFeatIt)) // dump common feature - dumpFeature(*aFeatIt); + else if (!isDumped(EntityPtr(*anObjIt))) { + // dump folder + FolderPtr aFolder = std::dynamic_pointer_cast(*anObjIt); + if (aFolder) + dumpFolder(aFolder); + else { + FeaturePtr aFeature = std::dynamic_pointer_cast(*anObjIt); + if (aFeature) // dump common feature + dumpFeature(aFeature); + } + } } + // dump folders if any + dumpPostponed(true); return isOk; } @@ -243,7 +688,7 @@ bool ModelHighAPI_Dumper::process(const std::shared_ptrnumberOfSubs(); for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) { FeaturePtr aFeature = theComposite->subFeature(anIndex); - if (isDumped(aFeature)) + if (isDumped(EntityPtr(aFeature))) continue; isSubDumped = true; @@ -319,6 +766,43 @@ bool ModelHighAPI_Dumper::processSubs( return isOk; } +void ModelHighAPI_Dumper::postpone(const EntityPtr& theEntity) +{ + // keep the name + name(theEntity, false); + myPostponed.push_back(theEntity); +} + +void ModelHighAPI_Dumper::dumpPostponed(bool theDumpFolders) +{ + if (myDumpPostponedInProgress) + return; + + myDumpPostponedInProgress = true; + // make a copy of postponed entities, because the list will be updated + // if some features are not able to be dumped + std::list aPostponedCopy = myPostponed; + myPostponed.clear(); + + // iterate over postponed entities and try to dump them + std::list::const_iterator anIt = aPostponedCopy.begin(); + for (; anIt != aPostponedCopy.end(); ++anIt) { + FolderPtr aFolder = std::dynamic_pointer_cast(*anIt); + if (aFolder) { + if (theDumpFolders) + dumpFolder(aFolder); + else + myPostponed.push_back(*anIt); + } + else { + FeaturePtr aFeature = std::dynamic_pointer_cast(*anIt); + if (aFeature) + dumpFeature(aFeature, true); + } + } + myDumpPostponedInProgress = false; +} + void ModelHighAPI_Dumper::dumpSubFeatureNameAndColor(const std::string theSubFeatureGet, const FeaturePtr& theSubFeature) { @@ -341,64 +825,22 @@ void ModelHighAPI_Dumper::dumpSubFeatureNameAndColor(const std::string theSubFea dumpEntitySetName(); } -bool ModelHighAPI_Dumper::exportTo(const std::string& theFileName) -{ - std::ofstream aFile; - OSD_OpenStream(aFile, theFileName.c_str(), std::ofstream::out); - if (!aFile.is_open()) - return false; - - // standard header - for (ModulesMap::const_iterator aModIt = myModules.begin(); - aModIt != myModules.end(); ++aModIt) { - aFile << "from " << aModIt->first << " import "; - if (aModIt->second.empty() || - aModIt->second.find(std::string()) != aModIt->second.end()) - aFile << "*"; // import whole module - else { - // import specific features - std::set::const_iterator anObjIt = aModIt->second.begin(); - aFile << *anObjIt; - for (++anObjIt; anObjIt != aModIt->second.end(); ++anObjIt) - aFile << ", " << *anObjIt; - } - aFile << std::endl; - } - if (!myModules.empty()) - aFile << std::endl; - - aFile << "import model" << std::endl << std::endl; - aFile << "model.begin()" << std::endl; - - // dump collected data - aFile << myFullDump.str(); - aFile << myDumpBuffer.str(); - - // standard footer - aFile << "model.end()" << std::endl; - - aFile.close(); - clear(); - - return true; -} - -void ModelHighAPI_Dumper::importModule(const std::string& theModuleName, - const std::string& theObject) +void ModelHighAPI_Dumper::importModule(const std::string& theModuleName) { - myModules[theModuleName].insert(theObject); + myModules.insert(theModuleName); } void ModelHighAPI_Dumper::dumpEntitySetName() { const LastDumpedEntity& aLastDumped = myEntitiesStack.top(); + bool isBufferEmpty = myDumpStorage->isBufferEmpty(); // dump "setName" for the entity if (aLastDumped.myUserName) { EntityName& anEntityNames = myNames[aLastDumped.myEntity]; if (!anEntityNames.myIsDefault) - myDumpBuffer << anEntityNames.myCurrentName << ".setName(\"" - << anEntityNames.myUserName << "\")" << std::endl; + *myDumpStorage << anEntityNames.myCurrentName << ".setName(\"" + << anEntityNames.myUserName << "\")\n"; // don't dump "setName" for the entity twice anEntityNames.myUserName.clear(); anEntityNames.myIsDefault = true; @@ -411,7 +853,7 @@ void ModelHighAPI_Dumper::dumpEntitySetName() EntityName& anEntityNames = myNames[*aResIt]; if (!anEntityNames.myIsDefault) { *this << *aResIt; - myDumpBuffer << ".setName(\"" << anEntityNames.myUserName << "\")" << std::endl; + *myDumpStorage << ".setName(\"" << anEntityNames.myUserName << "\")\n"; // don't dump "setName" for the entity twice anEntityNames.myUserName.clear(); anEntityNames.myIsDefault = true; @@ -421,8 +863,8 @@ void ModelHighAPI_Dumper::dumpEntitySetName() AttributeIntArrayPtr aColor = (*aResIt)->data()->intArray(ModelAPI_Result::COLOR_ID()); if (aColor && aColor->isInitialized()) { *this << *aResIt; - myDumpBuffer << ".setColor(" << aColor->value(0) << ", " << aColor->value(1) - << ", " << aColor->value(2) << ")" << std::endl; + *myDumpStorage << ".setColor(" << aColor->value(0) << ", " << aColor->value(1) + << ", " << aColor->value(2) << ")\n"; } } // set result deflection @@ -431,18 +873,63 @@ void ModelHighAPI_Dumper::dumpEntitySetName() (*aResIt)->data()->real(ModelAPI_Result::DEFLECTION_ID()); if(aDeflectionAttr.get() && aDeflectionAttr->isInitialized()) { *this << *aResIt; - myDumpBuffer << ".setDeflection(" << aDeflectionAttr->value() << ")" << std::endl; + *myDumpStorage << ".setDeflection(" << aDeflectionAttr->value() << ")\n"; + } + } + // set result transparency + if (!isDefaultTransparency(*aResIt)) { + AttributeDoublePtr aTransparencyAttr = + (*aResIt)->data()->real(ModelAPI_Result::TRANSPARENCY_ID()); + if(aTransparencyAttr.get() && aTransparencyAttr->isInitialized()) { + *this << *aResIt; + *myDumpStorage << ".setTransparency(" << aTransparencyAttr->value() << ")\n"; } } } + myNames[aLastDumped.myEntity].myIsDumped = true; myEntitiesStack.pop(); + + // clean buffer if it was clear before + if (isBufferEmpty) + myDumpStorage->mergeBuffer(); } bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const { EntityNameMap::const_iterator aFound = myNames.find(theEntity); - return aFound != myNames.end(); + FeaturePtr aFeature = std::dynamic_pointer_cast(theEntity); + return (aFound != myNames.end() && aFound->second.myIsDumped) || + myFeaturesToSkip.find(aFeature) != myFeaturesToSkip.end(); +} + +bool ModelHighAPI_Dumper::isDumped(const AttributeRefAttrPtr& theRefAttr) const +{ + FeaturePtr aFeature; + if (theRefAttr->isObject()) + aFeature = ModelAPI_Feature::feature(theRefAttr->object()); + else + aFeature = ModelAPI_Feature::feature(theRefAttr->attr()->owner()); + return aFeature && isDumped(EntityPtr(aFeature)); +} + +bool ModelHighAPI_Dumper::isDumped(const AttributeRefListPtr& theRefList) const +{ + std::list aRefs = theRefList->list(); + std::list::iterator anIt = aRefs.begin(); + for (; anIt != aRefs.end(); ++anIt) { + FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt); + if (aFeature && !isDumped(EntityPtr(aFeature))) + return false; + } + return true; +} + +static bool isSketchSub(const FeaturePtr& theFeature) +{ + static const std::string SKETCH("Sketch"); + CompositeFeaturePtr anOwner = ModelAPI_Tools::compositeOwner(theFeature); + return anOwner && anOwner->getKind() == SKETCH; } bool ModelHighAPI_Dumper::isDefaultColor(const ResultPtr& theResult) const @@ -451,6 +938,15 @@ bool ModelHighAPI_Dumper::isDefaultColor(const ResultPtr& theResult) const if (!aColor || !aColor->isInitialized()) return true; + // check the result belongs to sketch entity, do not dump color in this way + ResultConstructionPtr aResConstr = + std::dynamic_pointer_cast(theResult); + if (aResConstr) { + FeaturePtr aFeature = ModelAPI_Feature::feature(theResult->data()->owner()); + if (isSketchSub(aFeature)) + return true; + } + std::string aSection, aName, aDefault; theResult->colorConfigInfo(aSection, aName, aDefault); @@ -485,94 +981,88 @@ bool ModelHighAPI_Dumper::isDefaultDeflection(const ResultPtr& theResult) const } } if (isConstruction) - aDefault = Config_PropManager::real("Visualization", "construction_deflection", - ModelAPI_ResultConstruction::DEFAULT_DEFLECTION()); + aDefault = Config_PropManager::real("Visualization", "construction_deflection"); else - aDefault = Config_PropManager::real("Visualization", "body_deflection", - ModelAPI_ResultBody::DEFAULT_DEFLECTION()); + aDefault = Config_PropManager::real("Visualization", "body_deflection"); return fabs(aCurrent - aDefault) < 1.e-12; } +bool ModelHighAPI_Dumper::isDefaultTransparency(const ResultPtr& theResult) const +{ + AttributeDoublePtr anAttribute = theResult->data()->real(ModelAPI_Result::TRANSPARENCY_ID()); + if(!anAttribute || !anAttribute->isInitialized()) { + return true; + } + return fabs(anAttribute->value()) < 1.e-12; +} + ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar) { - myDumpBuffer << theChar; + *myDumpStorage << theChar; return *this; } ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString) { - myDumpBuffer << theString; + *myDumpStorage << theString; return *this; } ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString) { - myDumpBuffer << theString; + *myDumpStorage << theString; return *this; } ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const bool theValue) { - myDumpBuffer << (theValue ? "True" : "False"); + *myDumpStorage << (theValue ? "True" : "False"); return *this; } ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue) { - myDumpBuffer << theValue; + *myDumpStorage << theValue; return *this; } ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue) { - myDumpBuffer << theValue; + *myDumpStorage << theValue; return *this; } ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr& thePoint) { - importModule("GeomAPI", "GeomAPI_Pnt"); - myDumpBuffer << "GeomAPI_Pnt(" << thePoint->x() << ", " - << thePoint->y() << ", " << thePoint->z() << ")"; + importModule("GeomAPI"); + *myDumpStorage << "GeomAPI_Pnt(" << thePoint->x() << ", " + << thePoint->y() << ", " << thePoint->z() << ")"; return *this; } ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr& theDir) { - importModule("GeomAPI", "GeomAPI_Dir"); - myDumpBuffer << "GeomAPI_Dir(" << theDir->x() << ", " - << theDir->y() << ", " << theDir->z() << ")"; + importModule("GeomAPI"); + *myDumpStorage << "GeomAPI_Dir(" << theDir->x() << ", " + << theDir->y() << ", " << theDir->z() << ")"; return *this; } ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<( const std::shared_ptr& theDir) { - myDumpBuffer << theDir->x() << ", " << theDir->y() << ", " << theDir->z(); + *myDumpStorage << theDir->x() << ", " << theDir->y() << ", " << theDir->z(); return *this; } -static void dumpArray(std::ostringstream& theOutput, int theSize, - double* theValues, std::string* theTexts) -{ - for (int i = 0; i < theSize; ++i) { - if (i > 0) - theOutput << ", "; - if (theTexts[i].empty()) - theOutput << theValues[i]; - else - theOutput << "\"" << theTexts[i] << "\""; - } -} - ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<( const std::shared_ptr& thePoint) { static const int aSize = 3; double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()}; std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()}; - dumpArray(myDumpBuffer, aSize, aValues, aTexts); + myDumpStorage->dumpArray(aSize, aValues, aTexts); return *this; } @@ -582,14 +1072,14 @@ ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<( static const int aSize = 2; double aValues[aSize] = {thePoint->x(), thePoint->y()}; std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()}; - dumpArray(myDumpBuffer, aSize, aValues, aTexts); + myDumpStorage->dumpArray(aSize, aValues, aTexts); return *this; } ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<( const std::shared_ptr& theAttrBool) { - myDumpBuffer << (theAttrBool->value() ? "True" : "False"); + *myDumpStorage << (theAttrBool->value() ? "True" : "False"); return *this; } @@ -598,9 +1088,9 @@ ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<( { std::string aText = theAttrInt->text(); if (aText.empty()) - myDumpBuffer << theAttrInt->value(); + *myDumpStorage << theAttrInt->value(); else - myDumpBuffer << "\"" << aText << "\""; + *myDumpStorage << "\"" << aText << "\""; return *this; } @@ -609,33 +1099,51 @@ ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<( { std::string aText = theAttrReal->text(); if (aText.empty()) - myDumpBuffer << theAttrReal->value(); + *myDumpStorage << theAttrReal->value(); else - myDumpBuffer << "\"" << aText << "\""; + *myDumpStorage << "\"" << aText << "\""; return *this; } ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<( const std::shared_ptr& theAttrStr) { - myDumpBuffer << "\"" << theAttrStr->value() << "\""; + *myDumpStorage << "\"" << theAttrStr->value() << "\""; return *this; } -ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity) +ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FolderPtr& theFolder) { - myDumpBuffer << name(theEntity); + *myDumpStorage << name(theFolder); - bool isUserDefinedName = !myNames[theEntity].myIsDefault; - // store results if they have user-defined names or colors - std::list aResultsWithNameOrColor; - const std::list& aResults = theEntity->results(); - std::list::const_iterator aResIt = aResults.begin(); - for (; aResIt != aResults.end(); ++aResIt) - if (!myNames[*aResIt].myIsDefault || !isDefaultColor(*aResIt) || !isDefaultDeflection(*aResIt)) - aResultsWithNameOrColor.push_back(*aResIt); - // store just dumped entity to stack - myEntitiesStack.push(LastDumpedEntity(theEntity, isUserDefinedName, aResultsWithNameOrColor)); + // add dumped folder to a stack + if (!myNames[theFolder].myIsDumped && + (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theFolder)) + myEntitiesStack.push(LastDumpedEntity(theFolder, !myNames[theFolder].myIsDefault)); + + return *this; +} + +ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity) +{ + *myDumpStorage << name(theEntity); + + if (!myNames[theEntity].myIsDumped) { + bool isUserDefinedName = !myNames[theEntity].myIsDefault; + // store results if they have user-defined names or colors + std::list aResultsWithNameOrColor; + std::list allRes; + ModelAPI_Tools::allResults(theEntity, allRes); + for(std::list::iterator aRes = allRes.begin(); aRes != allRes.end(); aRes++) { + if(!myNames[*aRes].myIsDefault || !isDefaultColor(*aRes) || + !isDefaultDeflection(*aRes) || !isDefaultTransparency(*aRes)) + aResultsWithNameOrColor.push_back(*aRes); + } + // store just dumped entity to stack + if (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theEntity) + myEntitiesStack.push( + LastDumpedEntity(theEntity, isUserDefinedName, aResultsWithNameOrColor)); + } // remove entity from the list of not dumped items myNotDumpedEntities.erase(theEntity); @@ -644,22 +1152,40 @@ ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ResultPtr& theResult) { + // iterate in the structure of sub-results to the parent + ResultPtr aCurRes = theResult; FeaturePtr aFeature = ModelAPI_Feature::feature(theResult); - int anIndex = 0; - std::list aResults = aFeature->results(); - for(std::list::const_iterator - anIt = aResults.cbegin(); anIt != aResults.cend(); ++anIt, ++anIndex) { - if(theResult->isSame(*anIt)) { - break; + std::list anIndices; // indexes of results in the parent result, starting from topmost + while(aCurRes.get()) { + ResultBodyPtr aParent = ModelAPI_Tools::bodyOwner(aCurRes); + if (aParent) { + anIndices.push_front(ModelAPI_Tools::bodyIndex(aCurRes)); + } else { // index of the result in the feature + std::list::const_iterator aRes = aFeature->results().cbegin(); + for(int anIndex = 0; aRes != aFeature->results().cend(); aRes++, anIndex++) { + if (*aRes == aCurRes) { + anIndices.push_front(anIndex); + break; + } + } } + aCurRes = aParent; } - myDumpBuffer << name(aFeature); - if(anIndex == 0) { - myDumpBuffer << ".result()"; - } else { - myDumpBuffer << ".results()[" << anIndex << "]"; + *myDumpStorage << name(aFeature); + for (std::list::iterator anI = anIndices.begin(); anI != anIndices.end(); anI++) { + if (anI == anIndices.begin()) { + if(*anI == 0) { + *myDumpStorage << ".result()"; + } + else { + *myDumpStorage << ".results()[" << *anI << "]"; + } + } else { + *myDumpStorage << ".subResult(" << *anI << ")"; + } } + return *this; } @@ -667,7 +1193,7 @@ ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ObjectPtr& theObject) { FeaturePtr aFeature = std::dynamic_pointer_cast(theObject); if(aFeature.get()) { - myDumpBuffer << name(aFeature); + *myDumpStorage << name(aFeature); return *this; } @@ -694,8 +1220,8 @@ ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const AttributePtr& theAttr importModule("SketchAPI"); } - myDumpBuffer << aWrapperPrefix << name(anOwner) << aWrapperSuffix - << "." << attributeGetter(anOwner, theAttr->id()) << "()"; + *myDumpStorage << aWrapperPrefix << name(anOwner) << aWrapperSuffix + << "." << attributeGetter(anOwner, theAttr->id()) << "()"; return *this; } @@ -712,13 +1238,13 @@ ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<( ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<( const std::shared_ptr& theRefAttrList) { - myDumpBuffer << "["; + *myDumpStorage << "["; std::list > aList = theRefAttrList->list(); bool isAdded = false; std::list >::const_iterator anIt = aList.begin(); for (; anIt != aList.end(); ++anIt) { if (isAdded) - myDumpBuffer << ", "; + *myDumpStorage << ", "; else isAdded = true; if (anIt->first) @@ -726,7 +1252,7 @@ ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<( else if (anIt->second) * this << anIt->second; } - myDumpBuffer << "]"; + *myDumpStorage << "]"; return *this; } @@ -741,36 +1267,35 @@ ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<( const std::shared_ptr& theRefList) { static const int aThreshold = 2; + static bool aDumpAsIs = false; // if number of elements in the list if greater than a threshold, // dump it in a separate line with specific name - std::string aDumped = myDumpBuffer.str(); - if (aDumped.empty() || theRefList->size() <= aThreshold) { - myDumpBuffer << "["; + if (aDumpAsIs || theRefList->size() <= aThreshold) { + *myDumpStorage << "["; std::list aList = theRefList->list(); bool isAdded = false; std::list::const_iterator anIt = aList.begin(); for (; anIt != aList.end(); ++anIt) { if (isAdded) - myDumpBuffer << ", "; + *myDumpStorage << ", "; else isAdded = true; *this << *anIt; } - myDumpBuffer << "]"; + *myDumpStorage << "]"; } else { - // clear buffer and store list "as is" - myDumpBuffer.str(""); - *this << theRefList; - // save buffer and clear it again - std::string aDumpedList = myDumpBuffer.str(); - myDumpBuffer.str(""); - // obtain name of list + // name of list FeaturePtr anOwner = ModelAPI_Feature::feature(theRefList->owner()); std::string aListName = name(anOwner) + "_objects"; - // store all previous data - myDumpBuffer << aListName << " = " << aDumpedList << std::endl - << aDumped << aListName; + // reserve dumped buffer and store list "as is" + myDumpStorage->reserveBuffer(); + aDumpAsIs = true; + *this << aListName << " = " << theRefList << "\n"; + aDumpAsIs = false; + // append reserved data to the end of the current buffer + myDumpStorage->restoreReservedBuffer(); + *myDumpStorage << aListName; } return *this; } @@ -778,74 +1303,97 @@ ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<( ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<( const std::shared_ptr& theAttrSelect) { - myDumpBuffer << "model.selection("; - - if(!theAttrSelect->isInitialized()) { - myDumpBuffer << ")"; - return *this; - } - - GeomShapePtr aShape = theAttrSelect->value(); - if(!aShape.get()) { - aShape = theAttrSelect->context()->shape(); - } - - if(!aShape.get()) { - myDumpBuffer << ")"; - return *this; - } - - myDumpBuffer << "\"" << aShape->shapeTypeStr() << "\", \"" << - theAttrSelect->namingName() << "\")"; + myDumpStorage->write(theAttrSelect); return *this; } ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<( const std::shared_ptr& theAttrSelList) { - myDumpBuffer << "["; + static const int aThreshold = 2; + static bool aDumpAsIs = false; + // if number of elements in the list if greater than a threshold, + // dump it in a separate line with specific name + if (aDumpAsIs || theAttrSelList->size() <= aThreshold) { + *myDumpStorage << "["; - GeomShapePtr aShape; - std::string aShapeTypeStr; + GeomShapePtr aShape; + std::string aShapeTypeStr; - bool isAdded = false; + bool isAdded = false; - for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) { - AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex); - aShape = anAttribute->value(); - if(!aShape.get()) { - aShape = anAttribute->context()->shape(); + for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) { + AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex); + aShape = anAttribute->value(); + if(!aShape.get()) { + ResultPtr aContext = anAttribute->context(); + if (aContext.get()) + aShape = aContext->shape(); + } + + if(!aShape.get()) { + continue; + } + + if(isAdded) { + *myDumpStorage << ", "; + } else { + isAdded = true; + } + *this << anAttribute; } - if(!aShape.get()) { - continue; + // check selection list is obtained by filters + FiltersFeaturePtr aFilters = theAttrSelList->filters(); + if (aFilters) { + if (theAttrSelList->size() > 0) + *myDumpStorage << ", "; + dumpFeature(aFilters, true); } - if(isAdded) { - myDumpBuffer << ", "; - } else { - isAdded = true; + *myDumpStorage << "]"; + } else { + // obtain name of list (the feature may contain several selection lists) + FeaturePtr anOwner = ModelAPI_Feature::feature(theAttrSelList->owner()); + std::string aListName = name(anOwner) + "_objects"; + std::list aSelLists = + anOwner->data()->attributes(ModelAPI_AttributeSelectionList::typeId()); + if (aSelLists.size() > 1) { + int anIndex = 1; + for (std::list::iterator aSIt = aSelLists.begin(); + aSIt != aSelLists.end(); ++aSIt, ++anIndex) + if ((*aSIt).get() == theAttrSelList.get()) + break; + std::ostringstream aSStream; + aSStream << aListName << "_" << anIndex; + aListName = aSStream.str(); } - myDumpBuffer << "model.selection(\"" << - aShape->shapeTypeStr() << "\", \"" << anAttribute->namingName() << "\")"; + // reserve dumped buffer and store list "as is" + myDumpStorage->reserveBuffer(); + aDumpAsIs = true; + *this << aListName << " = " << theAttrSelList << "\n"; + aDumpAsIs = false; + // append reserved data to the end of the current buffer + myDumpStorage->restoreReservedBuffer(); + *myDumpStorage << aListName; } - - myDumpBuffer << "]"; return *this; } ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<( const std::shared_ptr& theArray) { - myDumpBuffer<<"["; + std::ostringstream aBuffer; + aBuffer << "["; for(int anIndex = 0; anIndex < theArray->size(); ++anIndex) { - - myDumpBuffer<<"\""<value(anIndex)<<"\""; if (anIndex != 0) - myDumpBuffer<<", "; + aBuffer << ", "; + + aBuffer << "\"" << theArray->value(anIndex) << "\""; } + aBuffer << "]"; - myDumpBuffer<<"]"; + myDumpStorage->write(aBuffer.str()); return *this; } @@ -853,22 +1401,32 @@ ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<( ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper, std::basic_ostream& (*theEndl)(std::basic_ostream&)) { - theDumper.myDumpBuffer << theEndl; + *theDumper.myDumpStorage << theEndl; if (!theDumper.myEntitiesStack.empty()) { - // Name for composite feature is dumped when all sub-entities are dumped - // (see method ModelHighAPI_Dumper::processSubs). - const ModelHighAPI_Dumper::LastDumpedEntity& aLastDumped = theDumper.myEntitiesStack.top(); - CompositeFeaturePtr aComposite = - std::dynamic_pointer_cast(aLastDumped.myEntity); - if (!aComposite) - theDumper.dumpEntitySetName(); + bool isCopy; + // all copies have been stored into stack, pop them all + do { + isCopy = false; + // Name for composite feature is dumped when all sub-entities are dumped + // (see method ModelHighAPI_Dumper::processSubs). + const ModelHighAPI_Dumper::LastDumpedEntity& aLastDumped = theDumper.myEntitiesStack.top(); + CompositeFeaturePtr aComposite = + std::dynamic_pointer_cast(aLastDumped.myEntity); + if (!aComposite) { + theDumper.dumpEntitySetName(); + FeaturePtr aFeature = std::dynamic_pointer_cast(aLastDumped.myEntity); + if (aFeature) { + AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy"); + isCopy = aCopyAttr.get() && aCopyAttr->value(); + } + } + } while (isCopy && !theDumper.myEntitiesStack.empty()); } // store all not-dumped entities first std::set aNotDumped = theDumper.myNotDumpedEntities; - std::string aBufCopy = theDumper.myDumpBuffer.str(); - theDumper.clear(true); + theDumper.myDumpStorage->reserveBuffer(); std::set::const_iterator anIt = aNotDumped.begin(); for (; anIt != aNotDumped.end(); ++anIt) { // if the feature is composite, dump it with all subs @@ -879,15 +1437,29 @@ ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper, else { FeaturePtr aFeature = std::dynamic_pointer_cast(*anIt); theDumper.dumpFeature(aFeature, true); + // dump the Projection feature which produces this "Copy" entity + AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy"); + if (aCopyAttr.get() && aCopyAttr->value()) + { + const std::set& aRefs = aFeature->data()->refsToMe(); + std::set::iterator aRefIt = aRefs.begin(); + for (; aRefIt != aRefs.end(); ++aRefIt) + if ((*aRefIt)->id() == "ProjectedFeature") + { // process projection only + FeaturePtr anOwner = ModelAPI_Feature::feature((*aRefIt)->owner()); + if (anOwner && !theDumper.isDumped(EntityPtr(anOwner))) + theDumper.dumpFeature(anOwner, true); + } + } } } - // avoid multiple empty lines - size_t anInd = std::string::npos; - while ((anInd = aBufCopy.find("\n\n\n")) != std::string::npos) - aBufCopy.erase(anInd, 1); - // then store currently dumped string - theDumper.myFullDump << aBufCopy; + // then store the reserved data + theDumper.myDumpStorage->restoreReservedBuffer(); + theDumper.myDumpStorage->mergeBuffer(); + + // now, store all postponed features + theDumper.dumpPostponed(); return theDumper; }