1 // Copyright (C) 2014-2019 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 email : webmaster.salome@opencascade.com
20 #include "ModelHighAPI_Dumper.h"
22 #include <Config_PropManager.h>
24 #include <GeomAPI_Circ.h>
25 #include <GeomAPI_Edge.h>
26 #include <GeomAPI_Ellipse.h>
27 #include <GeomAPI_Dir.h>
28 #include <GeomAPI_Pnt.h>
29 #include <GeomAPI_Pnt2d.h>
30 #include <GeomAPI_ShapeExplorer.h>
31 #include <GeomAPI_ShapeIterator.h>
32 #include <GeomAPI_Vertex.h>
33 #include <GeomAlgoAPI_NExplode.h>
35 #include <GeomDataAPI_Dir.h>
36 #include <GeomDataAPI_Point.h>
37 #include <GeomDataAPI_Point2D.h>
38 #include <GeomDataAPI_Point2DArray.h>
40 #include <ModelAPI_AttributeBoolean.h>
41 #include <ModelAPI_AttributeDouble.h>
42 #include <ModelAPI_AttributeDoubleArray.h>
43 #include <ModelAPI_AttributeIntArray.h>
44 #include <ModelAPI_AttributeInteger.h>
45 #include <ModelAPI_AttributeRefAttr.h>
46 #include <ModelAPI_AttributeRefAttrList.h>
47 #include <ModelAPI_AttributeReference.h>
48 #include <ModelAPI_AttributeRefList.h>
49 #include <ModelAPI_AttributeSelection.h>
50 #include <ModelAPI_AttributeSelectionList.h>
51 #include <ModelAPI_AttributeString.h>
52 #include <ModelAPI_AttributeStringArray.h>
53 #include <ModelAPI_CompositeFeature.h>
54 #include <ModelAPI_Document.h>
55 #include <ModelAPI_Entity.h>
56 #include <ModelAPI_Feature.h>
57 #include <ModelAPI_FiltersFeature.h>
58 #include <ModelAPI_Folder.h>
59 #include <ModelAPI_Result.h>
60 #include <ModelAPI_ResultBody.h>
61 #include <ModelAPI_ResultConstruction.h>
62 #include <ModelAPI_ResultGroup.h>
63 #include <ModelAPI_ResultPart.h>
64 #include <ModelAPI_Session.h>
65 #include <ModelAPI_Tools.h>
67 #include <ModelGeomAlgo_Shape.h>
69 #include <PartSetPlugin_Part.h>
71 #include <OSD_OpenFile.hxx>
75 // =========== Implementation of storage of dumped data ===========
76 static const int THE_DUMP_PRECISION = 16;
78 class ModelHighAPI_Dumper::DumpStorageBuffer : public ModelHighAPI_Dumper::DumpStorage
81 void addStorage(const ModelHighAPI_Dumper::DumpStoragePtr& theStorage)
82 { myStorageArray.push_back(theStorage); }
84 void clear() { myStorageArray.clear(); }
88 return myStorageArray.empty() || myStorageArray.front()->buffer().str().empty();
93 std::list<ModelHighAPI_Dumper::DumpStoragePtr>::iterator anIt = myStorageArray.begin();
94 for (; anIt != myStorageArray.end(); ++anIt) {
95 // avoid multiple empty lines
96 std::string aBuf = (*anIt)->buffer().str();
97 size_t anInd = std::string::npos;
98 while ((anInd = aBuf.find("\n\n\n")) != std::string::npos)
101 (*anIt)->fullDump() << aBuf;
102 (*anIt)->buffer().str("");
106 void write(const std::string& theValue)
108 if (myStorageArray.empty())
109 addStorage(DumpStoragePtr(new DumpStorage));
111 std::list<ModelHighAPI_Dumper::DumpStoragePtr>::iterator anIt = myStorageArray.begin();
112 for (; anIt != myStorageArray.end(); ++anIt)
113 (*anIt)->buffer() << theValue;
116 DumpStorageBuffer& operator<<(const char theChar)
118 std::ostringstream out;
124 DumpStorageBuffer& operator<<(const char* theString)
130 DumpStorageBuffer& operator<<(const std::string& theString)
136 DumpStorageBuffer& operator<<(const bool theValue)
138 std::ostringstream out;
144 DumpStorageBuffer& operator<<(const int theValue)
146 std::ostringstream out;
152 DumpStorageBuffer& operator<<(const double theValue)
154 std::ostringstream out;
155 out << std::setprecision(THE_DUMP_PRECISION) << theValue;
161 DumpStorageBuffer& operator<<(DumpStorageBuffer& theBuffer,
162 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
164 theBuffer.write("\n");
168 void dumpArray(int theSize, double* theValues, std::string* theTexts)
170 std::ostringstream anOutput;
171 anOutput << std::setprecision(THE_DUMP_PRECISION);
172 for (int i = 0; i < theSize; ++i) {
175 if (theTexts[i].empty())
176 anOutput << theValues[i];
178 anOutput << "\"" << theTexts[i] << "\"";
180 write(anOutput.str());
183 virtual void write(const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
185 if (myStorageArray.empty())
186 addStorage(DumpStoragePtr(new DumpStorage));
188 std::list<ModelHighAPI_Dumper::DumpStoragePtr>::iterator anIt = myStorageArray.begin();
189 for (; anIt != myStorageArray.end(); ++anIt)
190 (*anIt)->write(theAttrSelect);
193 virtual void reserveBuffer()
195 std::list<ModelHighAPI_Dumper::DumpStoragePtr>::iterator anIt = myStorageArray.begin();
196 for (; anIt != myStorageArray.end(); ++anIt)
197 (*anIt)->reserveBuffer();
200 virtual void restoreReservedBuffer()
202 std::list<ModelHighAPI_Dumper::DumpStoragePtr>::iterator anIt = myStorageArray.begin();
203 for (; anIt != myStorageArray.end(); ++anIt)
204 (*anIt)->restoreReservedBuffer();
207 virtual bool exportTo(const std::string& theFilename, const ModulesSet& theUsedModules)
209 static const std::string THE_EXT = ".py";
210 std::string aFilenameBase = theFilename;
211 if (aFilenameBase.rfind(THE_EXT) == aFilenameBase.size() - THE_EXT.size())
212 aFilenameBase = aFilenameBase.substr(0, aFilenameBase.size() - THE_EXT.size());
215 std::list<ModelHighAPI_Dumper::DumpStoragePtr>::iterator anIt = myStorageArray.begin();
216 for (; anIt != myStorageArray.end(); ++anIt) {
217 std::string aFilename = aFilenameBase + (*anIt)->myFilenameSuffix + THE_EXT;
218 isOk = (*anIt)->exportTo(aFilename, theUsedModules) && isOk;
224 std::list<ModelHighAPI_Dumper::DumpStoragePtr> myStorageArray;
228 ModelHighAPI_Dumper::DumpStorage::DumpStorage(const DumpStorage& theOther)
229 : myFilenameSuffix(theOther.myFilenameSuffix),
230 myDumpBufferHideout(theOther.myDumpBufferHideout)
232 myFullDump.str(theOther.myFullDump.str());
233 myDumpBuffer.str(theOther.myDumpBuffer.str());
236 const ModelHighAPI_Dumper::DumpStorage&
237 ModelHighAPI_Dumper::DumpStorage::operator=(const ModelHighAPI_Dumper::DumpStorage& theOther)
239 myFilenameSuffix = theOther.myFilenameSuffix;
240 myFullDump.str(theOther.myFullDump.str());
241 myDumpBuffer.str(theOther.myDumpBuffer.str());
242 myDumpBufferHideout = theOther.myDumpBufferHideout;
246 void ModelHighAPI_Dumper::DumpStorage::reserveBuffer()
248 myDumpBufferHideout.push(myDumpBuffer.str());
249 myDumpBuffer.str("");
252 void ModelHighAPI_Dumper::DumpStorage::restoreReservedBuffer()
254 myDumpBuffer << myDumpBufferHideout.top();
255 myDumpBufferHideout.pop();
258 bool ModelHighAPI_Dumper::DumpStorage::exportTo(const std::string& theFilename,
259 const ModulesSet& theUsedModules)
262 OSD_OpenStream(aFile, theFilename.c_str(), std::ofstream::out);
263 if (!aFile.is_open())
266 // standard header imported modules
267 for (ModulesSet::const_iterator aModIt = theUsedModules.begin();
268 aModIt != theUsedModules.end(); ++aModIt) {
269 aFile << "from " << *aModIt << " import *" << std::endl;
271 if (!theUsedModules.empty())
274 aFile << "from salome.shaper import model" << std::endl << std::endl;
275 aFile << "model.begin()" << std::endl;
277 // dump collected data
278 aFile << myFullDump.str();
279 aFile << myDumpBuffer.str();
282 aFile << "model.end()" << std::endl;
288 static void getShapeAndContext(const AttributeSelectionPtr& theAttrSelect,
289 GeomShapePtr& theShape, ResultPtr& theContext)
291 if (theAttrSelect->isInitialized()) {
292 theShape = theAttrSelect->value();
293 theContext = theAttrSelect->context();
295 theShape = theContext->shape();
297 if (theAttrSelect->isGeometricalSelection() &&
298 theShape.get() && theShape->shapeType() == GeomAPI_Shape::COMPOUND &&
299 theContext.get() && !theShape->isEqual(theContext->shape()) &&
300 theContext->groupName() != ModelAPI_ResultPart::group() &&
301 theContext->groupName() != ModelAPI_ResultGroup::group()) {
302 GeomAPI_ShapeIterator anIt(theShape);
303 theShape = anIt.current();
308 void ModelHighAPI_Dumper::DumpStorage::write(const AttributeSelectionPtr& theAttrSelect)
310 myDumpBuffer << "model.selection(";
314 getShapeAndContext(theAttrSelect, aShape, aContext);
317 myDumpBuffer << "\"" << aShape->shapeTypeStr() << "\", \""
318 << theAttrSelect->namingName() << "\"";
324 static int possibleSelectionsByPoint(const GeomPointPtr& thePoint,
325 const ResultPtr& theResult,
326 const GeomShapePtr& theShape,
327 const FeaturePtr& theStartFeature,
328 const FeaturePtr& theEndFeature)
330 DocumentPtr aDoc1 = theStartFeature->document();
331 DocumentPtr aDoc2 = theEndFeature->document();
333 std::list<FeaturePtr> aFeatures = aDoc1->allFeatures();
334 if (aDoc1 != aDoc2) {
335 std::list<FeaturePtr> anAdditionalFeatures = aDoc2->allFeatures();
336 aFeatures.insert(aFeatures.end(), anAdditionalFeatures.begin(), anAdditionalFeatures.end());
339 CompositeFeaturePtr aLastCompositeFeature;
341 std::list<FeaturePtr>::const_iterator aFIt = aFeatures.begin();
342 while (aFIt != aFeatures.end() && *aFIt != theStartFeature) {
343 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFIt);
345 aLastCompositeFeature = aCompFeat;
349 // collect the list of composite features, containing the last feature;
350 // these features should be excluded from searching,
351 // because the feature cannot select sub-shapes from its parent
352 std::set<FeaturePtr> aEndFeatureParents = ModelAPI_Tools::getParents(theEndFeature);
354 int aNbPossibleSelections = 0;
355 for (; aFIt != aFeatures.end() && *aFIt != theEndFeature; ++aFIt) {
356 bool isSkipFeature = false;
357 if (aLastCompositeFeature && aLastCompositeFeature->isSub(*aFIt))
358 isSkipFeature = true;
359 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFIt);
361 ResultPartPtr aPartRes =
362 std::dynamic_pointer_cast<ModelAPI_ResultPart>(aCompFeat->firstResult());
364 aLastCompositeFeature = aCompFeat;
365 if (aEndFeatureParents.find(aCompFeat) != aEndFeatureParents.end()) {
366 // do not process the parent for the last feature,
367 // because it cannot select objects from its parent
368 isSkipFeature = true;
374 std::list<ModelGeomAlgo_Shape::SubshapeOfResult> anApproproate;
375 if (ModelGeomAlgo_Shape::findSubshapeByPoint(*aFIt, thePoint, theShape->shapeType(),
377 bool isContinue = true;
378 std::list<std::pair<GeomShapePtr, int> > aCenters;
379 std::list<ModelGeomAlgo_Shape::SubshapeOfResult>::iterator anApIt = anApproproate.begin();
380 for (; anApIt != anApproproate.end() && isContinue; ++anApIt) {
381 ++aNbPossibleSelections;
383 // stop if the target shape and result are found
384 GeomShapePtr aCurShape = anApIt->mySubshape;
386 aCurShape = anApIt->myResult->shape();
388 if (anApIt->myResult->isSame(theResult)) {
389 if (anApIt->myCenterType == (int)ModelAPI_AttributeSelection::NOT_CENTER)
390 isContinue = !aCurShape->isSame(theShape);
391 else if (theShape->isVertex() && aCurShape->isEdge()) {
392 GeomEdgePtr aCurEdge = aCurShape->edge();
393 GeomVertexPtr aVertex = theShape->vertex();
394 GeomPointPtr aCenter;
395 switch (anApIt->myCenterType) {
396 case (int)ModelAPI_AttributeSelection::CIRCLE_CENTER: {
397 GeomCirclePtr aCirc = aCurEdge->circle();
399 aCenter = aCirc->center();
402 case (int)ModelAPI_AttributeSelection::ELLIPSE_FIRST_FOCUS: {
403 GeomEllipsePtr anEllipse = aCurEdge->ellipse();
405 aCenter = anEllipse->firstFocus();
408 case (int)ModelAPI_AttributeSelection::ELLIPSE_SECOND_FOCUS: {
409 GeomEllipsePtr anEllipse = aCurEdge->ellipse();
411 aCenter = anEllipse->secondFocus();
415 if (aCenter && aCenter->distance(aVertex->point()) < 1.e-7)
416 aCenters.push_back(std::pair<GeomShapePtr, int>(aCurShape, aNbPossibleSelections));
420 // passed till the appropriate shape, check the center of circle or a focus of ellipse is selected
421 if (isContinue && !aCenters.empty())
422 aNbPossibleSelections = aCenters.front().second;
425 return aNbPossibleSelections;
428 void ModelHighAPI_Dumper::DumpStorageGeom::write(const AttributeSelectionPtr& theAttrSelect)
432 getShapeAndContext(theAttrSelect, aShape, aContext);
434 // how to dump selection: construction features are dumped by name always
435 FeaturePtr aSelectedFeature;
436 FeaturePtr aFeature = theAttrSelect->contextFeature();
437 if (aShape && aContext && !aFeature)
438 aSelectedFeature = ModelAPI_Feature::feature(aContext->data()->owner());
439 bool isDumpByGeom = aSelectedFeature && aSelectedFeature->isInHistory();
442 myDumpBuffer << "model.selection(\"" << aShape->shapeTypeStr();
443 // check the selected item is a ResultPart;
444 // in this case it is necessary to get shape with full transformation
445 // for correct calculation of the middle point
446 ResultPartPtr aResPart =
447 std::dynamic_pointer_cast<ModelAPI_ResultPart>(theAttrSelect->context());
448 if (aResPart && aShape->shapeType() == GeomAPI_Shape::COMPOUND)
449 aShape = aResPart->shape();
450 GeomPointPtr aMiddlePoint = aShape->middlePoint();
451 // calculate number of features, which could be selected by the same point
452 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttrSelect->owner());
453 int aNbPossibleSelections = possibleSelectionsByPoint(aMiddlePoint,
454 theAttrSelect->context(), aShape, aSelectedFeature, anOwner);
456 // produce the index if the number of applicable features is greater than 1
458 if (aNbPossibleSelections > 1) {
459 std::ostringstream anOutput;
460 anOutput << "_" << aNbPossibleSelections;
461 anIndex = anOutput.str();
464 myDumpBuffer << std::setprecision(THE_DUMP_PRECISION)
465 << anIndex << "\", ("
466 << aMiddlePoint->x() << ", "
467 << aMiddlePoint->y() << ", "
468 << aMiddlePoint->z() << ")";
472 DumpStorage::write(theAttrSelect);
475 void ModelHighAPI_Dumper::DumpStorageWeak::write(const AttributeSelectionPtr& theAttrSelect)
479 getShapeAndContext(theAttrSelect, aShape, aContext);
481 bool aStandardDump = true;
482 if (aShape.get() && aContext.get() &&
483 aShape != aContext->shape()) { // weak naming for local selection only
484 GeomAlgoAPI_NExplode aNExplode(aContext->shape(), aShape->shapeType());
485 int anIndex = aNExplode.index(aShape);
486 if (anIndex != 0) { // found a week-naming index, so, export it
487 myDumpBuffer << "model.selection(\"" << aShape->shapeTypeStr() << "\", \""
488 << theAttrSelect->contextName(aContext) << "\", " << anIndex << ")";
489 aStandardDump = false;
493 DumpStorage::write(theAttrSelect);
495 // ======================================================================
498 static int gCompositeStackDepth = 0;
500 ModelHighAPI_Dumper* ModelHighAPI_Dumper::mySelf = 0;
502 ModelHighAPI_Dumper::ModelHighAPI_Dumper()
503 : myDumpStorage(new DumpStorageBuffer),
504 myDumpPostponedInProgress(false)
508 ModelHighAPI_Dumper::~ModelHighAPI_Dumper()
510 delete myDumpStorage;
513 void ModelHighAPI_Dumper::setInstance(ModelHighAPI_Dumper* theDumper)
519 ModelHighAPI_Dumper* ModelHighAPI_Dumper::getInstance()
524 void ModelHighAPI_Dumper::addCustomStorage(const ModelHighAPI_Dumper::DumpStoragePtr& theStorage)
526 myDumpStorage->addStorage(theStorage);
529 void ModelHighAPI_Dumper::clearCustomStorage()
531 myDumpStorage->clear();
535 myFeatureCount.clear();
537 while (!myEntitiesStack.empty())
538 myEntitiesStack.pop();
542 void ModelHighAPI_Dumper::clearNotDumped()
544 myNotDumpedEntities.clear();
547 // Convert string to integer. If the string is not a number, return -1
548 static int toInt(const std::string& theString)
550 std::string::const_iterator aChar = theString.begin();
551 for (; aChar != theString.end(); ++aChar)
552 if (!std::isdigit(*aChar))
554 if (aChar != theString.end())
555 return -1; // not a number
556 return std::stoi(theString);
559 const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity,
560 bool theSaveNotDumped,
561 bool theUseEntityName)
563 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
564 if (aFound != myNames.end())
565 return aFound->second.myCurrentName;
567 // entity is not found, store it
568 std::string aName, aKind;
569 bool isDefaultName = false;
570 bool isSaveNotDumped = theSaveNotDumped;
571 std::ostringstream aDefaultName;
572 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
574 aName = aFeature->name();
575 aKind = aFeature->getKind();
577 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(theEntity);
579 aName = aFolder->data()->name();
580 aKind = ModelAPI_Folder::ID();
581 isSaveNotDumped = false;
585 ObjectPtr anObject = std::dynamic_pointer_cast<ModelAPI_Object>(theEntity);
587 DocumentPtr aDoc = anObject->document();
588 std::pair<int, int>& aNbFeatures = myFeatureCount[aDoc][aKind];
589 aNbFeatures.first += 1;
591 size_t anIndex = aName.find(aKind);
592 if (anIndex == 0 && aName[aKind.length()] == '_') { // name starts with "FeatureKind_"
593 std::string anIdStr = aName.substr(aKind.length() + 1);
594 int anId = toInt(anIdStr);
596 // Check number of already registered objects of such kind. Index of current object
597 // should be the same to identify feature's name as automatically generated.
598 if (aNbFeatures.first == anId && aNbFeatures.second < anId) {
599 // name is not user-defined
600 isDefaultName = true;
602 // check there are postponed features of this kind,
603 // dump their names, because the sequence of features may be changed
604 for (std::list<EntityPtr>::const_iterator aPpIt = myPostponed.begin();
605 aPpIt != myPostponed.end(); ++aPpIt) {
606 FeaturePtr aCurFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*aPpIt);
607 if (aCurFeature && aCurFeature->getKind() == aKind) {
608 myNames[*aPpIt].myIsDefault = false;
609 isDefaultName = false;
614 if (anId > aNbFeatures.second)
615 aNbFeatures.second = anId;
618 // obtain default name for the feature
619 if (theUseEntityName)
620 aDefaultName << aName;
623 NbFeaturesMap::const_iterator aFIt = myFeatureCount.begin();
624 for (; aFIt != myFeatureCount.end(); ++aFIt) {
625 std::map<std::string, std::pair<int, int> >::const_iterator aFound =
626 aFIt->second.find(aKind);
627 if (aFound != aFIt->second.end())
628 aFullIndex += aFound->second.first;
630 aDefaultName << aKind << "_" << aFullIndex;
634 myNames[theEntity] = EntityName(aDefaultName.str(), aName, isDefaultName);
636 myNotDumpedEntities.insert(theEntity);
638 // store names of results
640 saveResultNames(aFeature);
642 return myNames[theEntity].myCurrentName;
645 const std::string& ModelHighAPI_Dumper::parentName(const FeaturePtr& theEntity)
647 const std::set<AttributePtr>& aRefs = theEntity->data()->refsToMe();
648 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
649 for (; aRefIt != aRefs.end(); ++aRefIt) {
650 CompositeFeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(
651 ModelAPI_Feature::feature((*aRefIt)->owner()));
653 return name(anOwner);
656 static const std::string DUMMY;
660 void ModelHighAPI_Dumper::saveResultNames(const FeaturePtr& theFeature)
662 // Default name of the feature
663 bool isFeatureDefaultName = myNames[theFeature].myIsDefault;
665 // Save only names of results which is not correspond to default feature name
666 const std::list<ResultPtr>& aResults = theFeature->results();
667 std::list<ResultPtr> allRes;
668 ModelAPI_Tools::allResults(theFeature, allRes);
669 for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
670 std::pair<std::string, bool> aName = ModelAPI_Tools::getDefaultName(*aRes);
671 std::string aDefaultName = aName.first;
672 std::string aResName = (*aRes)->data()->name();
673 bool isUserDefined = !(isFeatureDefaultName && aDefaultName == aResName);
675 EntityName(aResName, (isUserDefined ? aResName : std::string()), !isUserDefined);
679 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc,
680 const std::string& theFileName)
682 // dump top level document feature
683 static const std::string aDocName("partSet");
684 myNames[theDoc] = EntityName(aDocName, std::string(), true);
685 *this << aDocName << " = model.moduleDocument()" << std::endl;
687 // dump subfeatures and store result to file
688 bool isOk = process(theDoc) && myDumpStorage->exportTo(theFileName, myModules);
689 clearCustomStorage();
693 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc)
696 std::list<ObjectPtr> anObjects = theDoc->allObjects();
697 std::list<ObjectPtr>::const_iterator anObjIt = anObjects.begin();
698 // firstly, dump all parameters
699 for (; anObjIt != anObjects.end(); ++ anObjIt) {
700 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIt);
702 dumpParameter(aFeature);
704 // dump all other features
705 for (anObjIt = anObjects.begin(); anObjIt != anObjects.end(); ++anObjIt) {
706 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anObjIt);
708 // iteratively process composite features,
709 // if the composite feature is the last in the document, no need to dump "model.do()" action
710 std::list<ObjectPtr>::const_iterator aNext = anObjIt;
711 isOk = process(aCompFeat, false, ++aNext != anObjects.end()) && isOk;
713 else if (!isDumped(EntityPtr(*anObjIt))) {
715 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(*anObjIt);
719 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIt);
720 if (aFeature) // dump common feature
721 dumpFeature(aFeature);
725 // dump folders if any
730 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
731 bool isForce, bool isDumpModelDo)
733 // increase composite features stack
734 ++gCompositeStackDepth;
735 // dump composite itself
736 if (!isDumped(EntityPtr(theComposite)) || isForce)
737 dumpFeature(FeaturePtr(theComposite), isForce);
739 // sub-part is processed independently, because it provides separate document
740 if (theComposite->getKind() == PartSetPlugin_Part::ID()) {
741 // dump name of the part if it is different from default
742 if (!myEntitiesStack.empty())
745 // decrease composite features stack because we run into separate document
746 --gCompositeStackDepth;
748 ResultPartPtr aPartResult =
749 std::dynamic_pointer_cast<ModelAPI_ResultPart>(theComposite->lastResult());
752 DocumentPtr aSubDoc = aPartResult->partDoc();
755 // set name of document
756 const std::string& aPartName = myNames[theComposite].myCurrentName;
757 std::string aDocName = aPartName + "_doc";
758 myNames[aSubDoc] = EntityName(aDocName, std::string(), true);
760 // dump document in a separate line
761 *this << aDocName << " = " << aPartName << ".document()" << std::endl;
762 // dump features in the document
763 bool aRes = process(aSubDoc);
765 *this << "model.do()\n";
771 bool isOk = processSubs(theComposite);
772 // decrease composite features stack
773 --gCompositeStackDepth;
778 bool ModelHighAPI_Dumper::processSubs(
779 const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
783 // dump all sub-features;
784 bool isSubDumped = false;
785 int aNbSubs = theComposite->numberOfSubs();
786 for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) {
787 FeaturePtr aFeature = theComposite->subFeature(anIndex);
788 if (isDumped(EntityPtr(aFeature)))
792 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
793 if (aCompFeat) // iteratively process composite features
794 isOk = process(aCompFeat) && isOk;
796 dumpFeature(aFeature, true);
799 bool isDumpSetName = !myEntitiesStack.empty() &&
800 myEntitiesStack.top().myEntity == EntityPtr(theComposite);
801 bool isForceModelDo = isSubDumped && isDumpSetName &&
802 (myEntitiesStack.top().myUserName || !myEntitiesStack.top().myResults.empty());
803 // It is necessary for the sketch to create its result when complete (command "model.do()").
804 // This option is set by flat theDumpModelDo.
805 // However, nested sketches are rebuilt by parent feature, so, they do not need
806 // explicit call of "model.do()". This will be controlled by the depth of the stack.
807 if (isForceModelDo || (theDumpModelDo && gCompositeStackDepth <= 1))
808 *this << "model.do()" << std::endl;
810 // dump "setName" for composite feature
816 void ModelHighAPI_Dumper::postpone(const EntityPtr& theEntity)
819 name(theEntity, false);
820 myPostponed.push_back(theEntity);
823 void ModelHighAPI_Dumper::dumpPostponed(bool theDumpFolders)
825 if (myDumpPostponedInProgress)
828 myDumpPostponedInProgress = true;
829 // make a copy of postponed entities, because the list will be updated
830 // if some features are not able to be dumped
831 std::list<EntityPtr> aPostponedCopy = myPostponed;
834 // iterate over postponed entities and try to dump them
835 std::list<EntityPtr>::const_iterator anIt = aPostponedCopy.begin();
836 for (; anIt != aPostponedCopy.end(); ++anIt) {
837 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(*anIt);
842 myPostponed.push_back(*anIt);
845 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
847 dumpFeature(aFeature, true);
850 myDumpPostponedInProgress = false;
853 void ModelHighAPI_Dumper::dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
854 const FeaturePtr& theSubFeature)
856 name(theSubFeature, false);
857 myNames[theSubFeature] = EntityName(theSubFeatureGet, theSubFeature->name(), false);
859 // store results if they have user-defined names or colors
860 std::list<ResultPtr> aResultsWithNameOrColor;
861 const std::list<ResultPtr>& aResults = theSubFeature->results();
862 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
863 for (; aResIt != aResults.end(); ++aResIt) {
864 std::string aResName = (*aResIt)->data()->name();
865 myNames[*aResIt] = EntityName(aResName, aResName, false);
866 aResultsWithNameOrColor.push_back(*aResIt);
869 // store just dumped entity to stack
870 myEntitiesStack.push(LastDumpedEntity(theSubFeature, true, aResultsWithNameOrColor));
875 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName)
877 myModules.insert(theModuleName);
880 void ModelHighAPI_Dumper::dumpEntitySetName()
882 const LastDumpedEntity& aLastDumped = myEntitiesStack.top();
883 bool isBufferEmpty = myDumpStorage->isBufferEmpty();
885 // dump "setName" for the entity
886 if (aLastDumped.myUserName) {
887 EntityName& anEntityNames = myNames[aLastDumped.myEntity];
888 if (!anEntityNames.myIsDefault)
889 *myDumpStorage << anEntityNames.myCurrentName << ".setName(\""
890 << anEntityNames.myUserName << "\")\n";
891 // don't dump "setName" for the entity twice
892 anEntityNames.myUserName.clear();
893 anEntityNames.myIsDefault = true;
895 // dump "setName" for results
896 std::list<ResultPtr>::const_iterator aResIt = aLastDumped.myResults.begin();
897 std::list<ResultPtr>::const_iterator aResEnd = aLastDumped.myResults.end();
898 for (; aResIt != aResEnd; ++aResIt) {
900 EntityName& anEntityNames = myNames[*aResIt];
901 if (!anEntityNames.myIsDefault) {
903 *myDumpStorage << ".setName(\"" << anEntityNames.myUserName << "\")\n";
904 // don't dump "setName" for the entity twice
905 anEntityNames.myUserName.clear();
906 anEntityNames.myIsDefault = true;
909 if (!isDefaultColor(*aResIt)) {
910 AttributeIntArrayPtr aColor = (*aResIt)->data()->intArray(ModelAPI_Result::COLOR_ID());
911 if (aColor && aColor->isInitialized()) {
913 *myDumpStorage << ".setColor(" << aColor->value(0) << ", " << aColor->value(1)
914 << ", " << aColor->value(2) << ")\n";
917 // set result deflection
918 if (!isDefaultDeflection(*aResIt)) {
919 AttributeDoublePtr aDeflectionAttr =
920 (*aResIt)->data()->real(ModelAPI_Result::DEFLECTION_ID());
921 if(aDeflectionAttr.get() && aDeflectionAttr->isInitialized()) {
923 *myDumpStorage << ".setDeflection(" << aDeflectionAttr->value() << ")\n";
926 // set result transparency
927 if (!isDefaultTransparency(*aResIt)) {
928 AttributeDoublePtr aTransparencyAttr =
929 (*aResIt)->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
930 if(aTransparencyAttr.get() && aTransparencyAttr->isInitialized()) {
932 *myDumpStorage << ".setTransparency(" << aTransparencyAttr->value() << ")\n";
937 myNames[aLastDumped.myEntity].myIsDumped = true;
938 myEntitiesStack.pop();
940 // clean buffer if it was clear before
942 myDumpStorage->mergeBuffer();
945 bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const
947 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
948 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
949 return (aFound != myNames.end() && aFound->second.myIsDumped) ||
950 myFeaturesToSkip.find(aFeature) != myFeaturesToSkip.end();
953 bool ModelHighAPI_Dumper::isDumped(const AttributeRefAttrPtr& theRefAttr) const
956 if (theRefAttr->isObject())
957 aFeature = ModelAPI_Feature::feature(theRefAttr->object());
959 aFeature = ModelAPI_Feature::feature(theRefAttr->attr()->owner());
960 return aFeature && isDumped(EntityPtr(aFeature));
963 bool ModelHighAPI_Dumper::isDumped(const AttributeRefListPtr& theRefList) const
965 std::list<ObjectPtr> aRefs = theRefList->list();
966 std::list<ObjectPtr>::iterator anIt = aRefs.begin();
967 for (; anIt != aRefs.end(); ++anIt) {
968 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
969 if (aFeature && !isDumped(EntityPtr(aFeature)))
975 static bool isSketchSub(const FeaturePtr& theFeature)
977 static const std::string SKETCH("Sketch");
978 CompositeFeaturePtr anOwner = ModelAPI_Tools::compositeOwner(theFeature);
979 return anOwner && anOwner->getKind() == SKETCH;
982 bool ModelHighAPI_Dumper::isDefaultColor(const ResultPtr& theResult) const
984 AttributeIntArrayPtr aColor = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
985 if (!aColor || !aColor->isInitialized())
988 // check the result belongs to sketch entity, do not dump color in this way
989 ResultConstructionPtr aResConstr =
990 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult);
992 FeaturePtr aFeature = ModelAPI_Feature::feature(theResult->data()->owner());
993 if (isSketchSub(aFeature))
997 std::string aSection, aName, aDefault;
998 theResult->colorConfigInfo(aSection, aName, aDefault);
1000 // dump current color
1001 std::ostringstream aColorInfo;
1002 aColorInfo << aColor->value(0) << "," << aColor->value(1) << "," << aColor->value(2);
1004 return aDefault == aColorInfo.str();
1007 bool ModelHighAPI_Dumper::isDefaultDeflection(const ResultPtr& theResult) const
1009 AttributeDoublePtr aDeflectionAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
1010 if(!aDeflectionAttr || !aDeflectionAttr->isInitialized()) {
1014 double aCurrent = aDeflectionAttr->value();
1015 double aDefault = -1;
1017 bool isConstruction = false;
1018 std::string aResultGroup = theResult->groupName();
1019 if (aResultGroup == ModelAPI_ResultConstruction::group())
1020 isConstruction = true;
1021 else if (aResultGroup == ModelAPI_ResultBody::group()) {
1022 GeomShapePtr aGeomShape = theResult->shape();
1023 if (aGeomShape.get()) {
1024 // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
1025 // correction of deviation for them should not influence to the application performance
1026 GeomAPI_ShapeExplorer anExp(aGeomShape, GeomAPI_Shape::FACE);
1027 isConstruction = !anExp.more();
1031 aDefault = Config_PropManager::real("Visualization", "construction_deflection");
1033 aDefault = Config_PropManager::real("Visualization", "body_deflection");
1035 return fabs(aCurrent - aDefault) < 1.e-12;
1038 bool ModelHighAPI_Dumper::isDefaultTransparency(const ResultPtr& theResult) const
1040 AttributeDoublePtr anAttribute = theResult->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
1041 if(!anAttribute || !anAttribute->isInitialized()) {
1044 return fabs(anAttribute->value()) < 1.e-12;
1047 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
1049 *myDumpStorage << theChar;
1053 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
1055 *myDumpStorage << theString;
1059 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
1061 *myDumpStorage << theString;
1065 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const bool theValue)
1067 *myDumpStorage << (theValue ? "True" : "False");
1071 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
1073 *myDumpStorage << theValue;
1077 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
1079 *myDumpStorage << theValue;
1083 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
1085 importModule("GeomAPI");
1086 *myDumpStorage << "GeomAPI_Pnt(" << thePoint->x() << ", "
1087 << thePoint->y() << ", " << thePoint->z() << ")";
1091 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
1093 importModule("GeomAPI");
1094 *myDumpStorage << "GeomAPI_Dir(" << theDir->x() << ", "
1095 << theDir->y() << ", " << theDir->z() << ")";
1099 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1100 const std::shared_ptr<GeomDataAPI_Dir>& theDir)
1102 *myDumpStorage << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
1106 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1107 const std::shared_ptr<GeomDataAPI_Point>& thePoint)
1109 static const int aSize = 3;
1110 double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
1111 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()};
1112 myDumpStorage->dumpArray(aSize, aValues, aTexts);
1116 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1117 const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
1119 static const int aSize = 2;
1120 double aValues[aSize] = {thePoint->x(), thePoint->y()};
1121 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()};
1122 myDumpStorage->dumpArray(aSize, aValues, aTexts);
1126 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1127 const std::shared_ptr<GeomDataAPI_Point2DArray>& thePointArray)
1129 static const int aThreshold = 4;
1130 static bool aDumpAsIs = false;
1131 static std::string aSeparator = "";
1132 // if number of elements in the list if greater than a threshold,
1133 // dump it in a separate line with specific name
1134 int aSize = thePointArray->size();
1135 if (aDumpAsIs || aSize <= aThreshold) {
1136 *myDumpStorage << "[";
1137 GeomPnt2dPtr aPoint = thePointArray->pnt(0);
1138 *myDumpStorage << "(" << aPoint->x() << ", " << aPoint->y() << ")";
1139 for (int anIndex = 1; anIndex < aSize; ++anIndex) {
1140 aPoint = thePointArray->pnt(anIndex);
1141 *myDumpStorage << "," << aSeparator << " (" << aPoint->x() << ", " << aPoint->y() << ")";
1143 *myDumpStorage << aSeparator << "]";
1147 FeaturePtr anOwner = ModelAPI_Feature::feature(thePointArray->owner());
1148 std::string aListName = name(anOwner) + "_" + thePointArray->id();
1149 // reserve dumped buffer and store list "as is"
1150 myDumpStorage->reserveBuffer();
1152 aSeparator = std::string("\n") + std::string(aListName.size() + 3, ' ');
1153 *this << aListName << " = " << thePointArray << "\n";
1156 // append reserved data to the end of the current buffer
1157 myDumpStorage->restoreReservedBuffer();
1158 *myDumpStorage << aListName;
1163 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1164 const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
1166 *myDumpStorage << (theAttrBool->value() ? "True" : "False");
1170 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1171 const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
1173 std::string aText = theAttrInt->text();
1175 *myDumpStorage << theAttrInt->value();
1177 *myDumpStorage << "\"" << aText << "\"";
1181 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1182 const std::shared_ptr<ModelAPI_AttributeIntArray>& theArray)
1184 *myDumpStorage << "[";
1185 int aSize = theArray->size();
1187 *myDumpStorage << theArray->value(0);
1188 for (int anIndex = 1; anIndex < aSize; ++anIndex)
1189 *myDumpStorage << ", " << theArray->value(anIndex);
1191 *myDumpStorage << "]";
1195 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1196 const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
1198 std::string aText = theAttrReal->text();
1200 *myDumpStorage << theAttrReal->value();
1202 *myDumpStorage << "\"" << aText << "\"";
1206 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1207 const std::shared_ptr<ModelAPI_AttributeDoubleArray>& theArray)
1209 *myDumpStorage << "[";
1210 int aSize = theArray->size();
1212 *myDumpStorage << theArray->value(0);
1213 for (int anIndex = 1; anIndex < aSize; ++anIndex)
1214 *myDumpStorage << ", " << theArray->value(anIndex);
1216 *myDumpStorage << "]";
1220 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1221 const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
1223 *myDumpStorage << "\"" << theAttrStr->value() << "\"";
1227 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FolderPtr& theFolder)
1229 *myDumpStorage << name(theFolder);
1231 // add dumped folder to a stack
1232 if (!myNames[theFolder].myIsDumped &&
1233 (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theFolder))
1234 myEntitiesStack.push(LastDumpedEntity(theFolder, !myNames[theFolder].myIsDefault));
1239 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity)
1241 *myDumpStorage << name(theEntity);
1243 if (!myNames[theEntity].myIsDumped) {
1244 bool isUserDefinedName = !myNames[theEntity].myIsDefault;
1245 // store results if they have user-defined names or colors
1246 std::list<ResultPtr> aResultsWithNameOrColor;
1247 std::list<ResultPtr> allRes;
1248 ModelAPI_Tools::allResults(theEntity, allRes);
1249 for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
1250 if(!myNames[*aRes].myIsDefault || !isDefaultColor(*aRes) ||
1251 !isDefaultDeflection(*aRes) || !isDefaultTransparency(*aRes))
1252 aResultsWithNameOrColor.push_back(*aRes);
1254 // store just dumped entity to stack
1255 if (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theEntity)
1256 myEntitiesStack.push(
1257 LastDumpedEntity(theEntity, isUserDefinedName, aResultsWithNameOrColor));
1260 // remove entity from the list of not dumped items
1261 myNotDumpedEntities.erase(theEntity);
1265 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ResultPtr& theResult)
1267 // iterate in the structure of sub-results to the parent
1268 ResultPtr aCurRes = theResult;
1269 FeaturePtr aFeature = ModelAPI_Feature::feature(theResult);
1270 std::list<int> anIndices; // indexes of results in the parent result, starting from topmost
1271 while(aCurRes.get()) {
1272 ResultBodyPtr aParent = ModelAPI_Tools::bodyOwner(aCurRes);
1274 anIndices.push_front(ModelAPI_Tools::bodyIndex(aCurRes));
1275 } else { // index of the result in the feature
1276 std::list<ResultPtr>::const_iterator aRes = aFeature->results().cbegin();
1277 for(int anIndex = 0; aRes != aFeature->results().cend(); aRes++, anIndex++) {
1278 if (*aRes == aCurRes) {
1279 anIndices.push_front(anIndex);
1287 *myDumpStorage << name(aFeature);
1288 for (std::list<int>::iterator anI = anIndices.begin(); anI != anIndices.end(); anI++) {
1289 if (anI == anIndices.begin()) {
1291 *myDumpStorage << ".result()";
1294 *myDumpStorage << ".results()[" << *anI << "]";
1297 *myDumpStorage << ".subResult(" << *anI << ")";
1304 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::list<ResultPtr>& theResults)
1307 for (std::list<ResultPtr>::const_iterator anIt = theResults.begin();
1308 anIt != theResults.end(); ++anIt) {
1309 if (anIt != theResults.begin())
1317 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ObjectPtr& theObject)
1319 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
1320 if(aFeature.get()) {
1321 *myDumpStorage << name(aFeature);
1325 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1334 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const AttributePtr& theAttr)
1336 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttr->owner());
1338 std::string aWrapperPrefix, aWrapperSuffix;
1339 // Check the attribute belongs to copied (in multi-translation or multi-rotation) feature.
1340 // In this case we need to cast explicitly feature to appropriate type.
1341 AttributeBooleanPtr isCopy = anOwner->boolean("Copy");
1342 AttributeReferencePtr hasParent = anOwner->reference("ParentFeature");
1343 if ((isCopy.get() && isCopy->value()) || (hasParent && hasParent->value())) {
1344 aWrapperPrefix = featureWrapper(anOwner) + "(";
1345 aWrapperSuffix = ")";
1346 importModule("SketchAPI");
1349 *myDumpStorage << aWrapperPrefix << name(anOwner) << aWrapperSuffix
1350 << "." << attributeGetter(anOwner, theAttr->id()) << "()";
1354 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1355 const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
1357 if (theRefAttr->isObject())
1358 *this << theRefAttr->object();
1360 *this << theRefAttr->attr();
1364 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1365 const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList)
1367 *myDumpStorage << "[";
1368 std::list<std::pair<ObjectPtr, AttributePtr> > aList = theRefAttrList->list();
1369 bool isAdded = false;
1370 std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aList.begin();
1371 for (; anIt != aList.end(); ++anIt) {
1373 *myDumpStorage << ", ";
1377 *this << anIt->first;
1378 else if (anIt->second)
1379 * this << anIt->second;
1381 *myDumpStorage << "]";
1385 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1386 const std::shared_ptr<ModelAPI_AttributeReference>& theReference)
1388 *this << theReference->value();
1392 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1393 const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList)
1395 static const int aThreshold = 2;
1396 static bool aDumpAsIs = false;
1397 // if number of elements in the list if greater than a threshold,
1398 // dump it in a separate line with specific name
1399 if (aDumpAsIs || theRefList->size() <= aThreshold) {
1400 *myDumpStorage << "[";
1401 std::list<ObjectPtr> aList = theRefList->list();
1402 bool isAdded = false;
1403 std::list<ObjectPtr>::const_iterator anIt = aList.begin();
1404 for (; anIt != aList.end(); ++anIt) {
1406 *myDumpStorage << ", ";
1412 *myDumpStorage << "]";
1415 FeaturePtr anOwner = ModelAPI_Feature::feature(theRefList->owner());
1416 std::string aListName = name(anOwner) + "_objects";
1417 // reserve dumped buffer and store list "as is"
1418 myDumpStorage->reserveBuffer();
1420 *this << aListName << " = " << theRefList << "\n";
1422 // append reserved data to the end of the current buffer
1423 myDumpStorage->restoreReservedBuffer();
1424 *myDumpStorage << aListName;
1429 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1430 const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
1432 myDumpStorage->write(theAttrSelect);
1436 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1437 const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList)
1439 static const int aThreshold = 2;
1440 static bool aDumpAsIs = false;
1441 // if number of elements in the list if greater than a threshold,
1442 // dump it in a separate line with specific name
1443 if (aDumpAsIs || theAttrSelList->size() <= aThreshold) {
1444 *myDumpStorage << "[";
1446 GeomShapePtr aShape;
1447 std::string aShapeTypeStr;
1449 bool isAdded = false;
1451 for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) {
1452 AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex);
1453 aShape = anAttribute->value();
1455 ResultPtr aContext = anAttribute->context();
1457 aShape = aContext->shape();
1465 *myDumpStorage << ", ";
1469 *this << anAttribute;
1472 // check selection list is obtained by filters
1473 FiltersFeaturePtr aFilters = theAttrSelList->filters();
1475 if (theAttrSelList->size() > 0)
1476 *myDumpStorage << ", ";
1477 dumpFeature(aFilters, true);
1480 *myDumpStorage << "]";
1482 // obtain name of list (the feature may contain several selection lists)
1483 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttrSelList->owner());
1484 std::string aListName = name(anOwner) + "_objects";
1485 std::list<AttributePtr> aSelLists =
1486 anOwner->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
1487 if (aSelLists.size() > 1) {
1489 for (std::list<AttributePtr>::iterator aSIt = aSelLists.begin();
1490 aSIt != aSelLists.end(); ++aSIt, ++anIndex)
1491 if ((*aSIt).get() == theAttrSelList.get())
1493 std::ostringstream aSStream;
1494 aSStream << aListName << "_" << anIndex;
1495 aListName = aSStream.str();
1497 // reserve dumped buffer and store list "as is"
1498 myDumpStorage->reserveBuffer();
1500 *this << aListName << " = " << theAttrSelList << "\n";
1502 // append reserved data to the end of the current buffer
1503 myDumpStorage->restoreReservedBuffer();
1504 *myDumpStorage << aListName;
1509 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1510 const std::shared_ptr<ModelAPI_AttributeStringArray>& theArray)
1512 std::ostringstream aBuffer;
1514 for(int anIndex = 0; anIndex < theArray->size(); ++anIndex) {
1518 aBuffer << "\"" << theArray->value(anIndex) << "\"";
1522 myDumpStorage->write(aBuffer.str());
1527 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
1528 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
1530 *theDumper.myDumpStorage << theEndl;
1532 if (!theDumper.myEntitiesStack.empty()) {
1534 // all copies have been stored into stack, pop them all
1537 // Name for composite feature is dumped when all sub-entities are dumped
1538 // (see method ModelHighAPI_Dumper::processSubs).
1539 const ModelHighAPI_Dumper::LastDumpedEntity& aLastDumped = theDumper.myEntitiesStack.top();
1540 CompositeFeaturePtr aComposite =
1541 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aLastDumped.myEntity);
1543 theDumper.dumpEntitySetName();
1544 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aLastDumped.myEntity);
1546 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
1547 isCopy = aCopyAttr.get() && aCopyAttr->value();
1550 } while (isCopy && !theDumper.myEntitiesStack.empty());
1553 // store all not-dumped entities first
1554 std::set<EntityPtr> aNotDumped = theDumper.myNotDumpedEntities;
1555 theDumper.myDumpStorage->reserveBuffer();
1556 std::set<EntityPtr>::const_iterator anIt = aNotDumped.begin();
1557 for (; anIt != aNotDumped.end(); ++anIt) {
1558 // if the feature is composite, dump it with all subs
1559 CompositeFeaturePtr aCompFeat =
1560 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
1562 theDumper.process(aCompFeat, true);
1564 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
1565 theDumper.dumpFeature(aFeature, true);
1566 // dump the Projection feature which produces this "Copy" entity
1567 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
1568 if (aCopyAttr.get() && aCopyAttr->value())
1570 const std::set<AttributePtr>& aRefs = aFeature->data()->refsToMe();
1571 std::set<AttributePtr>::iterator aRefIt = aRefs.begin();
1572 for (; aRefIt != aRefs.end(); ++aRefIt)
1573 if ((*aRefIt)->id() == "ProjectedFeature")
1574 { // process projection only
1575 FeaturePtr anOwner = ModelAPI_Feature::feature((*aRefIt)->owner());
1576 if (anOwner && !theDumper.isDumped(EntityPtr(anOwner)))
1577 theDumper.dumpFeature(anOwner, true);
1583 // then store the reserved data
1584 theDumper.myDumpStorage->restoreReservedBuffer();
1585 theDumper.myDumpStorage->mergeBuffer();
1587 // now, store all postponed features
1588 theDumper.dumpPostponed();