1 // Copyright (C) 2014-2021 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 <Locale_Convert.h>
42 #include <ModelAPI_AttributeBoolean.h>
43 #include <ModelAPI_AttributeDouble.h>
44 #include <ModelAPI_AttributeDoubleArray.h>
45 #include <ModelAPI_AttributeIntArray.h>
46 #include <ModelAPI_AttributeInteger.h>
47 #include <ModelAPI_AttributeRefAttr.h>
48 #include <ModelAPI_AttributeRefAttrList.h>
49 #include <ModelAPI_AttributeReference.h>
50 #include <ModelAPI_AttributeRefList.h>
51 #include <ModelAPI_AttributeSelection.h>
52 #include <ModelAPI_AttributeSelectionList.h>
53 #include <ModelAPI_AttributeString.h>
54 #include <ModelAPI_AttributeStringArray.h>
55 #include <ModelAPI_CompositeFeature.h>
56 #include <ModelAPI_Document.h>
57 #include <ModelAPI_Entity.h>
58 #include <ModelAPI_Feature.h>
59 #include <ModelAPI_FiltersFeature.h>
60 #include <ModelAPI_Folder.h>
61 #include <ModelAPI_Result.h>
62 #include <ModelAPI_ResultBody.h>
63 #include <ModelAPI_ResultConstruction.h>
64 #include <ModelAPI_ResultGroup.h>
65 #include <ModelAPI_ResultPart.h>
66 #include <ModelAPI_Session.h>
67 #include <ModelAPI_Tools.h>
69 #include <ModelGeomAlgo_Shape.h>
71 #include <PartSetPlugin_Part.h>
73 #include <OSD_OpenFile.hxx>
77 // =========== Implementation of storage of dumped data ===========
78 static const int THE_DUMP_PRECISION = 16;
80 class ModelHighAPI_Dumper::DumpStorageBuffer : public ModelHighAPI_Dumper::DumpStorage
83 void addStorage(const ModelHighAPI_Dumper::DumpStoragePtr& theStorage)
84 { myStorageArray.push_back(theStorage); }
86 void clear() { myStorageArray.clear(); }
90 return myStorageArray.empty() || myStorageArray.front()->buffer().str().empty();
95 std::list<ModelHighAPI_Dumper::DumpStoragePtr>::iterator anIt = myStorageArray.begin();
96 for (; anIt != myStorageArray.end(); ++anIt) {
97 // avoid multiple empty lines
98 std::string aBuf = (*anIt)->buffer().str();
99 size_t anInd = std::string::npos;
100 while ((anInd = aBuf.find("\n\n\n")) != std::string::npos)
101 aBuf.erase(anInd, 1);
103 (*anIt)->fullDump() << aBuf;
104 (*anIt)->buffer().str("");
108 void write(const std::string& theValue)
110 if (myStorageArray.empty())
111 addStorage(DumpStoragePtr(new DumpStorage));
113 std::list<ModelHighAPI_Dumper::DumpStoragePtr>::iterator anIt = myStorageArray.begin();
114 for (; anIt != myStorageArray.end(); ++anIt)
115 (*anIt)->buffer() << theValue;
118 DumpStorageBuffer& operator<<(const char theChar)
120 std::ostringstream out;
126 DumpStorageBuffer& operator<<(const char* theString)
132 DumpStorageBuffer& operator<<(const std::string& theString)
138 DumpStorageBuffer& operator<<(const bool theValue)
140 std::ostringstream out;
146 DumpStorageBuffer& operator<<(const int theValue)
148 std::ostringstream out;
154 DumpStorageBuffer& operator<<(const double theValue)
156 std::ostringstream out;
157 out << std::setprecision(THE_DUMP_PRECISION) << theValue;
163 DumpStorageBuffer& operator<<(DumpStorageBuffer& theBuffer,
164 std::basic_ostream<char>& (*)(std::basic_ostream<char>&))
166 theBuffer.write("\n");
170 void dumpArray(int theSize, double* theValues, std::string* theTexts)
172 std::ostringstream anOutput;
173 anOutput << std::setprecision(THE_DUMP_PRECISION);
174 for (int i = 0; i < theSize; ++i) {
177 if (theTexts[i].empty())
178 anOutput << theValues[i];
180 anOutput << "\"" << theTexts[i] << "\"";
182 write(anOutput.str());
185 virtual void write(const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
187 if (myStorageArray.empty())
188 addStorage(DumpStoragePtr(new DumpStorage));
190 std::list<ModelHighAPI_Dumper::DumpStoragePtr>::iterator anIt = myStorageArray.begin();
191 for (; anIt != myStorageArray.end(); ++anIt)
192 (*anIt)->write(theAttrSelect);
195 virtual void reserveBuffer()
197 std::list<ModelHighAPI_Dumper::DumpStoragePtr>::iterator anIt = myStorageArray.begin();
198 for (; anIt != myStorageArray.end(); ++anIt)
199 (*anIt)->reserveBuffer();
202 virtual void restoreReservedBuffer()
204 std::list<ModelHighAPI_Dumper::DumpStoragePtr>::iterator anIt = myStorageArray.begin();
205 for (; anIt != myStorageArray.end(); ++anIt)
206 (*anIt)->restoreReservedBuffer();
209 virtual bool exportTo(const std::string& theFilename, const ModulesSet& theUsedModules)
211 static const std::string THE_EXT = ".py";
212 std::string aFilenameBase = theFilename;
213 if (aFilenameBase.rfind(THE_EXT) == aFilenameBase.size() - THE_EXT.size())
214 aFilenameBase = aFilenameBase.substr(0, aFilenameBase.size() - THE_EXT.size());
217 std::list<ModelHighAPI_Dumper::DumpStoragePtr>::iterator anIt = myStorageArray.begin();
218 for (; anIt != myStorageArray.end(); ++anIt) {
219 std::string aFilename = aFilenameBase + (*anIt)->myFilenameSuffix + THE_EXT;
220 isOk = (*anIt)->exportTo(aFilename, theUsedModules) && isOk;
226 std::list<ModelHighAPI_Dumper::DumpStoragePtr> myStorageArray;
230 ModelHighAPI_Dumper::DumpStorage::DumpStorage(const DumpStorage& theOther)
231 : myFilenameSuffix(theOther.myFilenameSuffix),
232 myDumpBufferHideout(theOther.myDumpBufferHideout)
234 myFullDump.str(theOther.myFullDump.str());
235 myDumpBuffer.str(theOther.myDumpBuffer.str());
238 const ModelHighAPI_Dumper::DumpStorage&
239 ModelHighAPI_Dumper::DumpStorage::operator=(const ModelHighAPI_Dumper::DumpStorage& theOther)
241 myFilenameSuffix = theOther.myFilenameSuffix;
242 myFullDump.str(theOther.myFullDump.str());
243 myDumpBuffer.str(theOther.myDumpBuffer.str());
244 myDumpBufferHideout = theOther.myDumpBufferHideout;
248 void ModelHighAPI_Dumper::DumpStorage::reserveBuffer()
250 myDumpBufferHideout.push(myDumpBuffer.str());
251 myDumpBuffer.str("");
254 void ModelHighAPI_Dumper::DumpStorage::restoreReservedBuffer()
256 myDumpBuffer << myDumpBufferHideout.top();
257 myDumpBufferHideout.pop();
260 bool ModelHighAPI_Dumper::DumpStorage::exportTo(const std::string& theFilename,
261 const ModulesSet& theUsedModules)
264 OSD_OpenStream(aFile, theFilename.c_str(), std::ofstream::out);
265 if (!aFile.is_open())
268 // standard header imported modules
269 for (ModulesSet::const_iterator aModIt = theUsedModules.begin();
270 aModIt != theUsedModules.end(); ++aModIt) {
271 aFile << "from " << *aModIt << " import *" << std::endl;
273 if (!theUsedModules.empty())
276 aFile << "from salome.shaper import model" << std::endl << std::endl;
277 aFile << "model.begin()" << std::endl;
279 // dump collected data
280 aFile << myFullDump.str();
281 aFile << myDumpBuffer.str();
284 aFile << "model.end()" << std::endl;
290 static void getShapeAndContext(const AttributeSelectionPtr& theAttrSelect,
291 GeomShapePtr& theShape, ResultPtr& theContext)
293 if (theAttrSelect->isInitialized()) {
294 theShape = theAttrSelect->value();
295 theContext = theAttrSelect->context();
297 theShape = theContext->shape();
299 if (theAttrSelect->isGeometricalSelection() &&
300 theShape.get() && theShape->shapeType() == GeomAPI_Shape::COMPOUND &&
301 theContext.get() && !theShape->isEqual(theContext->shape()) &&
302 theContext->groupName() != ModelAPI_ResultPart::group() &&
303 theContext->groupName() != ModelAPI_ResultGroup::group()) {
304 GeomAPI_ShapeIterator anIt(theShape);
305 theShape = anIt.current();
310 void ModelHighAPI_Dumper::DumpStorage::write(const AttributeSelectionPtr& theAttrSelect)
312 myDumpBuffer << "model.selection(";
316 getShapeAndContext(theAttrSelect, aShape, aContext);
319 myDumpBuffer << "\"" << aShape->shapeTypeStr() << "\", \""
320 << Locale::Convert::toString(theAttrSelect->namingName()) << "\"";
326 static int possibleSelectionsByPoint(const GeomPointPtr& thePoint,
327 const ResultPtr& theResult,
328 const GeomShapePtr& theShape,
329 const FeaturePtr& theStartFeature,
330 const FeaturePtr& theEndFeature)
332 DocumentPtr aDoc1 = theStartFeature->document();
333 DocumentPtr aDoc2 = theEndFeature->document();
335 std::list<FeaturePtr> aFeatures = aDoc1->allFeatures();
336 if (aDoc1 != aDoc2) {
337 // Find the position of the part, where its features should be inserted.
338 // It will avoid checking of appropriate elements in partSet after the current part.
339 std::list<FeaturePtr>::iterator aFIt = aFeatures.begin();
340 for (; aFIt != aFeatures.end(); ++aFIt) {
341 ResultPartPtr aPartRes =
342 std::dynamic_pointer_cast<ModelAPI_ResultPart>((*aFIt)->lastResult());
343 if (aPartRes && aPartRes->partDoc() == aDoc2)
347 std::list<FeaturePtr> anAdditionalFeatures = aDoc2->allFeatures();
348 aFeatures.insert(aFIt, anAdditionalFeatures.begin(), anAdditionalFeatures.end());
351 CompositeFeaturePtr aLastCompositeFeature;
353 std::list<FeaturePtr>::const_iterator aFIt = aFeatures.begin();
354 while (aFIt != aFeatures.end() && *aFIt != theStartFeature) {
355 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFIt);
357 aLastCompositeFeature = aCompFeat;
361 // collect the list of composite features, containing the last feature;
362 // these features should be excluded from searching,
363 // because the feature cannot select sub-shapes from its parent
364 std::set<FeaturePtr> aEndFeatureParents = ModelAPI_Tools::getParents(theEndFeature);
366 int aNbPossibleSelections = 0;
367 for (; aFIt != aFeatures.end() && *aFIt != theEndFeature; ++aFIt) {
368 bool isSkipFeature = false;
369 if (aLastCompositeFeature && aLastCompositeFeature->isSub(*aFIt))
370 isSkipFeature = true;
371 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFIt);
373 ResultPartPtr aPartRes =
374 std::dynamic_pointer_cast<ModelAPI_ResultPart>(aCompFeat->firstResult());
376 aLastCompositeFeature = aCompFeat;
377 if (aEndFeatureParents.find(aCompFeat) != aEndFeatureParents.end()) {
378 // do not process the parent for the last feature,
379 // because it cannot select objects from its parent
380 isSkipFeature = true;
386 std::list<ModelGeomAlgo_Shape::SubshapeOfResult> anApproproate;
387 if (ModelGeomAlgo_Shape::findSubshapeByPoint(*aFIt, thePoint, theShape->shapeType(),
389 bool isContinue = true;
390 std::list<std::pair<GeomShapePtr, int> > aCenters;
391 std::list<ModelGeomAlgo_Shape::SubshapeOfResult>::iterator anApIt = anApproproate.begin();
392 for (; anApIt != anApproproate.end() && isContinue; ++anApIt) {
393 ++aNbPossibleSelections;
395 // stop if the target shape and result are found
396 GeomShapePtr aCurShape = anApIt->mySubshape;
398 aCurShape = anApIt->myResult->shape();
400 if (anApIt->myResult->isSame(theResult)) {
401 if (anApIt->myCenterType == (int)ModelAPI_AttributeSelection::NOT_CENTER)
402 isContinue = !aCurShape->isSame(theShape);
403 else if (theShape->isVertex() && aCurShape->isEdge()) {
404 GeomEdgePtr aCurEdge = aCurShape->edge();
405 GeomVertexPtr aVertex = theShape->vertex();
406 GeomPointPtr aCenter;
407 switch (anApIt->myCenterType) {
408 case (int)ModelAPI_AttributeSelection::CIRCLE_CENTER: {
409 GeomCirclePtr aCirc = aCurEdge->circle();
411 aCenter = aCirc->center();
414 case (int)ModelAPI_AttributeSelection::ELLIPSE_FIRST_FOCUS: {
415 GeomEllipsePtr anEllipse = aCurEdge->ellipse();
417 aCenter = anEllipse->firstFocus();
420 case (int)ModelAPI_AttributeSelection::ELLIPSE_SECOND_FOCUS: {
421 GeomEllipsePtr anEllipse = aCurEdge->ellipse();
423 aCenter = anEllipse->secondFocus();
427 if (aCenter && aCenter->distance(aVertex->point()) < 1.e-7)
428 aCenters.push_back(std::pair<GeomShapePtr, int>(aCurShape, aNbPossibleSelections));
432 // passed till the appropriate shape, check the center of circle
433 // or a focus of ellipse is selected
434 if (isContinue && !aCenters.empty())
435 aNbPossibleSelections = aCenters.front().second;
438 return aNbPossibleSelections;
441 void ModelHighAPI_Dumper::DumpStorageGeom::write(const AttributeSelectionPtr& theAttrSelect)
445 getShapeAndContext(theAttrSelect, aShape, aContext);
447 // how to dump selection: construction features are dumped by name always
448 FeaturePtr aSelectedFeature;
449 FeaturePtr aFeature = theAttrSelect->contextFeature();
450 if (aShape && aContext && !aFeature)
451 aSelectedFeature = ModelAPI_Feature::feature(aContext->data()->owner());
452 bool isDumpByGeom = aSelectedFeature && aSelectedFeature->isInHistory();
455 myDumpBuffer << "model.selection(\"" << aShape->shapeTypeStr();
456 // check the selected item is a ResultPart;
457 // in this case it is necessary to get shape with full transformation
458 // for correct calculation of the middle point
459 ResultPartPtr aResPart =
460 std::dynamic_pointer_cast<ModelAPI_ResultPart>(theAttrSelect->context());
461 if (aResPart && aShape->shapeType() == GeomAPI_Shape::COMPOUND)
462 aShape = aResPart->shape();
463 GeomPointPtr aMiddlePoint = aShape->middlePoint();
464 // calculate number of features, which could be selected by the same point
465 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttrSelect->owner());
466 int aNbPossibleSelections = possibleSelectionsByPoint(aMiddlePoint,
467 theAttrSelect->context(), aShape, aSelectedFeature, anOwner);
469 // produce the index if the number of applicable features is greater than 1
471 if (aNbPossibleSelections > 1) {
472 std::ostringstream anOutput;
473 anOutput << "_" << aNbPossibleSelections;
474 anIndex = anOutput.str();
477 myDumpBuffer << std::setprecision(THE_DUMP_PRECISION)
478 << anIndex << "\", ("
479 << aMiddlePoint->x() << ", "
480 << aMiddlePoint->y() << ", "
481 << aMiddlePoint->z() << ")";
485 DumpStorage::write(theAttrSelect);
488 void ModelHighAPI_Dumper::DumpStorageWeak::write(const AttributeSelectionPtr& theAttrSelect)
492 getShapeAndContext(theAttrSelect, aShape, aContext);
494 bool aStandardDump = true;
495 if (aShape.get() && aContext.get() &&
496 aShape != aContext->shape()) { // weak naming for local selection only
497 GeomAlgoAPI_NExplode aNExplode(aContext->shape(), aShape->shapeType());
498 int anIndex = aNExplode.index(aShape);
499 if (anIndex != 0) { // found a week-naming index, so, export it
500 myDumpBuffer << "model.selection(\"" << aShape->shapeTypeStr() << "\", \""
501 << Locale::Convert::toString(theAttrSelect->contextName(aContext))
502 << "\", " << anIndex << ")";
503 aStandardDump = false;
507 DumpStorage::write(theAttrSelect);
509 // ======================================================================
512 static int gCompositeStackDepth = 0;
514 ModelHighAPI_Dumper* ModelHighAPI_Dumper::mySelf = 0;
516 ModelHighAPI_Dumper::ModelHighAPI_Dumper()
517 : myDumpStorage(new DumpStorageBuffer),
518 myDumpPostponedInProgress(false)
522 ModelHighAPI_Dumper::~ModelHighAPI_Dumper()
524 delete myDumpStorage;
527 void ModelHighAPI_Dumper::setInstance(ModelHighAPI_Dumper* theDumper)
533 ModelHighAPI_Dumper* ModelHighAPI_Dumper::getInstance()
538 void ModelHighAPI_Dumper::addCustomStorage(const ModelHighAPI_Dumper::DumpStoragePtr& theStorage)
540 myDumpStorage->addStorage(theStorage);
543 void ModelHighAPI_Dumper::clearCustomStorage()
545 myDumpStorage->clear();
549 myFeatureCount.clear();
551 while (!myEntitiesStack.empty())
552 myEntitiesStack.pop();
556 void ModelHighAPI_Dumper::clearNotDumped()
558 myNotDumpedEntities.clear();
561 // Convert string to integer. If the string is not a number, return -1
562 static int toInt(const std::string& theString)
564 std::string::const_iterator aChar = theString.begin();
565 for (; aChar != theString.end(); ++aChar)
566 if (!std::isdigit(*aChar))
568 if (aChar != theString.end())
569 return -1; // not a number
570 return std::stoi(theString);
573 const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity,
574 bool theSaveNotDumped,
575 bool theUseEntityName,
578 EntityNameMap::iterator aFound = myNames.find(theEntity);
579 if (aFound != myNames.end()) {
580 // Set dumped flag for postponed constraints which are without names
581 if (!aFound->second.myIsDumped)
582 aFound->second.myIsDumped = theSetIsDumped;
583 return aFound->second.myCurrentName;
585 // entity is not found, store it
588 bool isDefaultName = false;
589 bool isSaveNotDumped = theSaveNotDumped;
590 std::ostringstream aDefaultName;
591 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
593 aName = Locale::Convert::toString(aFeature->name());
594 aKind = aFeature->getKind();
596 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(theEntity);
598 aName = Locale::Convert::toString(aFolder->data()->name());
599 aKind = ModelAPI_Folder::ID();
600 isSaveNotDumped = false;
604 ObjectPtr anObject = std::dynamic_pointer_cast<ModelAPI_Object>(theEntity);
606 DocumentPtr aDoc = anObject->document();
607 std::pair<int, int>& aNbFeatures = myFeatureCount[aDoc][aKind];
608 aNbFeatures.first += 1;
610 size_t anIndex = aName.find(aKind);
611 if (anIndex == 0 && aName[aKind.length()] == '_') { // name starts with "FeatureKind_"
612 std::string anIdStr = aName.substr(aKind.length() + 1);
613 int anId = toInt(anIdStr);
615 // Check number of already registered objects of such kind. Index of current object
616 // should be the same to identify feature's name as automatically generated.
617 if (aNbFeatures.first == anId && aNbFeatures.second < anId) {
618 // name is not user-defined
619 isDefaultName = true;
621 // check there are postponed features of this kind,
622 // dump their names, because the sequence of features may be changed
623 for (std::list<EntityPtr>::const_iterator aPpIt = myPostponed.begin();
624 aPpIt != myPostponed.end(); ++aPpIt) {
625 FeaturePtr aCurFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*aPpIt);
626 if (aCurFeature && aCurFeature->getKind() == aKind) {
627 myNames[*aPpIt].myIsDefault = false;
628 isDefaultName = false;
633 if (anId > aNbFeatures.second)
634 aNbFeatures.second = anId;
637 // obtain default name for the feature
638 if (theUseEntityName)
639 aDefaultName << aName;
642 NbFeaturesMap::const_iterator aFIt = myFeatureCount.begin();
643 for (; aFIt != myFeatureCount.end(); ++aFIt) {
644 std::map<std::string, std::pair<int, int> >::const_iterator aFoundKind =
645 aFIt->second.find(aKind);
646 if (aFoundKind != aFIt->second.end())
647 aFullIndex += aFoundKind->second.first;
649 aDefaultName << aKind << "_" << aFullIndex;
653 myNames[theEntity] = EntityName(aDefaultName.str(), aName, isDefaultName);
655 myNotDumpedEntities.insert(theEntity);
657 // store names of results
659 saveResultNames(aFeature);
661 myNames[theEntity].myIsDumped = theSetIsDumped;
663 return myNames[theEntity].myCurrentName;
666 const std::string& ModelHighAPI_Dumper::parentName(const FeaturePtr& theEntity)
668 const std::set<AttributePtr>& aRefs = theEntity->data()->refsToMe();
669 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
670 for (; aRefIt != aRefs.end(); ++aRefIt) {
671 CompositeFeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(
672 ModelAPI_Feature::feature((*aRefIt)->owner()));
674 return name(anOwner);
677 static const std::string DUMMY;
681 void ModelHighAPI_Dumper::saveResultNames(const FeaturePtr& theFeature)
683 // Default name of the feature
684 bool isFeatureDefaultName = myNames[theFeature].myIsDefault;
686 // Save only names of results which is not correspond to default feature name
687 std::list<ResultPtr> allRes;
688 ModelAPI_Tools::allResults(theFeature, allRes);
689 for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
690 std::pair<std::wstring, bool> aName = ModelAPI_Tools::getDefaultName(*aRes, true, true);
691 std::string aDefaultName = Locale::Convert::toString(aName.first);
692 std::string aResName = Locale::Convert::toString((*aRes)->data()->name());
693 bool isUserDefined = !(isFeatureDefaultName && aDefaultName == aResName);
695 EntityName(aResName, (isUserDefined ? aResName : std::string()), !isUserDefined);
699 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc,
700 const std::string& theFileName)
702 // dump top level document feature
703 static const std::string aDocName("partSet");
704 myNames[theDoc] = EntityName(aDocName, std::string(), true);
705 *this << aDocName << " = model.moduleDocument()" << std::endl;
707 // dump subfeatures and store result to file
708 bool isOk = process(theDoc) && myDumpStorage->exportTo(theFileName, myModules);
712 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc)
715 std::list<ObjectPtr> anObjects = theDoc->allObjects();
716 std::list<ObjectPtr>::const_iterator anObjIt = anObjects.begin();
717 // firstly, dump all parameters
718 for (; anObjIt != anObjects.end(); ++ anObjIt) {
719 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIt);
721 dumpParameter(aFeature);
723 // dump all other features
724 for (anObjIt = anObjects.begin(); anObjIt != anObjects.end(); ++anObjIt) {
725 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anObjIt);
727 // iteratively process composite features,
728 // if the composite feature is the last in the document, no need to dump "model.do()" action
729 std::list<ObjectPtr>::const_iterator aNext = anObjIt;
730 isOk = process(aCompFeat, false, ++aNext != anObjects.end()) && isOk;
732 else if (!isDumped(EntityPtr(*anObjIt))) {
734 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(*anObjIt);
738 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIt);
739 if (aFeature) // dump common feature
740 dumpFeature(aFeature);
744 // dump folders if any
749 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
750 bool isForce, bool isDumpModelDo)
752 // increase composite features stack
753 ++gCompositeStackDepth;
754 // dump composite itself
755 if (!isDumped(EntityPtr(theComposite)) || isForce)
756 dumpFeature(FeaturePtr(theComposite), isForce);
758 // sub-part is processed independently, because it provides separate document
759 if (theComposite->getKind() == PartSetPlugin_Part::ID()) {
760 // dump name of the part if it is different from default
761 if (!myEntitiesStack.empty())
764 // decrease composite features stack because we run into separate document
765 --gCompositeStackDepth;
767 ResultPartPtr aPartResult =
768 std::dynamic_pointer_cast<ModelAPI_ResultPart>(theComposite->lastResult());
771 DocumentPtr aSubDoc = aPartResult->partDoc();
774 // set name of document
775 const std::string& aPartName = myNames[theComposite].myCurrentName;
776 std::string aDocName = aPartName + "_doc";
777 myNames[aSubDoc] = EntityName(aDocName, std::string(), true);
779 // dump document in a separate line
780 *this << aDocName << " = " << aPartName << ".document()" << std::endl;
781 // dump features in the document
782 bool aRes = process(aSubDoc);
784 *this << "\nmodel.do()\n";
790 bool isOk = processSubs(theComposite);
791 // decrease composite features stack
792 --gCompositeStackDepth;
797 bool ModelHighAPI_Dumper::processSubs(
798 const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
802 // dump all sub-features;
803 bool isSubDumped = false;
804 int aNbSubs = theComposite->numberOfSubs();
805 for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) {
806 FeaturePtr aFeature = theComposite->subFeature(anIndex);
807 if (isDumped(EntityPtr(aFeature)))
811 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
812 if (aCompFeat) // iteratively process composite features
813 isOk = process(aCompFeat) && isOk;
815 dumpFeature(aFeature, true);
818 bool isDumpSetName = !myEntitiesStack.empty() &&
819 myEntitiesStack.top().myEntity == EntityPtr(theComposite);
820 bool isForceModelDo = isSubDumped && isDumpSetName &&
821 (myEntitiesStack.top().myUserName || !myEntitiesStack.top().myResults.empty());
822 // It is necessary for the sketch to create its result when complete (command "model.do()").
823 // This option is set by flat theDumpModelDo.
824 // However, nested sketches are rebuilt by parent feature, so, they do not need
825 // explicit call of "model.do()". This will be controlled by the depth of the stack.
826 if (isForceModelDo || (theDumpModelDo && gCompositeStackDepth <= 1))
827 *this << "model.do()" << std::endl;
829 // dump "setName" for composite feature
835 void ModelHighAPI_Dumper::postpone(const EntityPtr& theEntity)
838 name(theEntity, false);
839 myPostponed.push_back(theEntity);
842 void ModelHighAPI_Dumper::dumpPostponed(bool theDumpFolders)
844 if (myDumpPostponedInProgress)
847 myDumpPostponedInProgress = true;
848 // make a copy of postponed entities, because the list will be updated
849 // if some features are not able to be dumped
850 std::list<EntityPtr> aPostponedCopy = myPostponed;
853 // iterate over postponed entities and try to dump them
854 std::list<EntityPtr>::const_iterator anIt = aPostponedCopy.begin();
855 for (; anIt != aPostponedCopy.end(); ++anIt) {
856 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(*anIt);
861 myPostponed.push_back(*anIt);
864 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
866 dumpFeature(aFeature, true);
869 myDumpPostponedInProgress = false;
872 void ModelHighAPI_Dumper::dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
873 const FeaturePtr& theSubFeature)
875 name(theSubFeature, false);
876 myNames[theSubFeature] =
877 EntityName(theSubFeatureGet, Locale::Convert::toString(theSubFeature->name()), false);
879 // store results if they have user-defined names or colors
880 std::list<ResultPtr> aResultsWithNameOrColor;
881 const std::list<ResultPtr>& aResults = theSubFeature->results();
882 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
883 for (; aResIt != aResults.end(); ++aResIt) {
884 std::string aResName = Locale::Convert::toString((*aResIt)->data()->name());
885 myNames[*aResIt] = EntityName(aResName, aResName, false);
886 aResultsWithNameOrColor.push_back(*aResIt);
889 // store just dumped entity to stack
890 myEntitiesStack.push(LastDumpedEntity(theSubFeature, true, aResultsWithNameOrColor));
895 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName)
897 myModules.insert(theModuleName);
900 void ModelHighAPI_Dumper::dumpEntitySetName()
902 const LastDumpedEntity& aLastDumped = myEntitiesStack.top();
903 bool isBufferEmpty = myDumpStorage->isBufferEmpty();
905 // dump "setName" for the entity
906 if (aLastDumped.myUserName) {
907 EntityName& anEntityNames = myNames[aLastDumped.myEntity];
908 if (!anEntityNames.myIsDefault)
909 *myDumpStorage << anEntityNames.myCurrentName << ".setName(\""
910 << anEntityNames.myUserName << "\")\n";
911 // don't dump "setName" for the entity twice
912 anEntityNames.myUserName.clear();
913 anEntityNames.myIsDefault = true;
915 // dump "setName" for results
916 std::list<ResultPtr>::const_iterator aResIt = aLastDumped.myResults.begin();
917 std::list<ResultPtr>::const_iterator aResEnd = aLastDumped.myResults.end();
918 for (; aResIt != aResEnd; ++aResIt) {
920 EntityName& anEntityNames = myNames[*aResIt];
921 if (!anEntityNames.myIsDefault) {
923 *myDumpStorage << ".setName(\"" << anEntityNames.myUserName << "\")\n";
924 // don't dump "setName" for the entity twice
925 anEntityNames.myUserName.clear();
926 anEntityNames.myIsDefault = true;
929 if (!isDefaultColor(*aResIt)) {
930 AttributeIntArrayPtr aColor = (*aResIt)->data()->intArray(ModelAPI_Result::COLOR_ID());
931 if (aColor && aColor->isInitialized()) {
933 *myDumpStorage << ".setColor(" << aColor->value(0) << ", " << aColor->value(1)
934 << ", " << aColor->value(2) << ")\n";
937 // set result deflection
938 if (!isDefaultDeflection(*aResIt)) {
939 AttributeDoublePtr aDeflectionAttr =
940 (*aResIt)->data()->real(ModelAPI_Result::DEFLECTION_ID());
941 if(aDeflectionAttr.get() && aDeflectionAttr->isInitialized()) {
943 *myDumpStorage << ".setDeflection(" << aDeflectionAttr->value() << ")\n";
946 // set result transparency
947 if (!isDefaultTransparency(*aResIt)) {
948 AttributeDoublePtr aTransparencyAttr =
949 (*aResIt)->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
950 if(aTransparencyAttr.get() && aTransparencyAttr->isInitialized()) {
952 *myDumpStorage << ".setTransparency(" << aTransparencyAttr->value() << ")\n";
957 myNames[aLastDumped.myEntity].myIsDumped = true;
958 myEntitiesStack.pop();
960 // clean buffer if it was clear before
962 myDumpStorage->mergeBuffer();
965 bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const
967 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
968 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
969 return (aFound != myNames.end() && aFound->second.myIsDumped) ||
970 myFeaturesToSkip.find(aFeature) != myFeaturesToSkip.end();
973 bool ModelHighAPI_Dumper::isDumped(const AttributeRefAttrPtr& theRefAttr) const
976 if (theRefAttr->isObject())
977 aFeature = ModelAPI_Feature::feature(theRefAttr->object());
979 aFeature = ModelAPI_Feature::feature(theRefAttr->attr()->owner());
980 return aFeature && isDumped(EntityPtr(aFeature));
983 bool ModelHighAPI_Dumper::isDumped(const AttributeRefListPtr& theRefList) const
985 std::list<ObjectPtr> aRefs = theRefList->list();
986 std::list<ObjectPtr>::iterator anIt = aRefs.begin();
987 for (; anIt != aRefs.end(); ++anIt) {
988 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
989 if (aFeature && !isDumped(EntityPtr(aFeature)))
995 size_t ModelHighAPI_Dumper::indexOfFirstNotDumped(
996 const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList) const
999 std::list<ObjectPtr> anObjects = theRefList->list();
1000 for (std::list<ObjectPtr>::const_iterator anIt = anObjects.begin();
1001 anIt != anObjects.end(); ++anIt, ++anIndex)
1002 if (!isDumped(ModelAPI_Feature::feature(*anIt)))
1007 static bool isSketchSub(const FeaturePtr& theFeature)
1009 static const std::string SKETCH("Sketch");
1010 CompositeFeaturePtr anOwner = ModelAPI_Tools::compositeOwner(theFeature);
1011 return anOwner && anOwner->getKind() == SKETCH;
1014 bool ModelHighAPI_Dumper::isDefaultColor(const ResultPtr& theResult) const
1016 AttributeIntArrayPtr aColor = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
1017 if (!aColor || !aColor->isInitialized())
1020 // check the result belongs to sketch entity, do not dump color in this way
1021 ResultConstructionPtr aResConstr =
1022 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult);
1024 FeaturePtr aFeature = ModelAPI_Feature::feature(theResult->data()->owner());
1025 if (isSketchSub(aFeature))
1029 std::string aSection, aName, aDefault;
1030 theResult->colorConfigInfo(aSection, aName, aDefault);
1032 // dump current color
1033 std::ostringstream aColorInfo;
1034 aColorInfo << aColor->value(0) << "," << aColor->value(1) << "," << aColor->value(2);
1036 return aDefault == aColorInfo.str();
1039 bool ModelHighAPI_Dumper::isDefaultDeflection(const ResultPtr& theResult) const
1041 AttributeDoublePtr aDeflectionAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
1042 if(!aDeflectionAttr || !aDeflectionAttr->isInitialized()) {
1046 double aCurrent = aDeflectionAttr->value();
1047 double aDefault = -1;
1049 bool isConstruction = false;
1050 std::string aResultGroup = theResult->groupName();
1051 if (aResultGroup == ModelAPI_ResultConstruction::group())
1052 isConstruction = true;
1053 else if (aResultGroup == ModelAPI_ResultBody::group()) {
1054 GeomShapePtr aGeomShape = theResult->shape();
1055 if (aGeomShape.get()) {
1056 // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
1057 // correction of deviation for them should not influence to the application performance
1058 GeomAPI_ShapeExplorer anExp(aGeomShape, GeomAPI_Shape::FACE);
1059 isConstruction = !anExp.more();
1063 aDefault = Config_PropManager::real("Visualization", "construction_deflection");
1065 aDefault = Config_PropManager::real("Visualization", "body_deflection");
1067 return fabs(aCurrent - aDefault) < 1.e-12;
1070 bool ModelHighAPI_Dumper::isDefaultTransparency(const ResultPtr& theResult) const
1072 AttributeDoublePtr anAttribute = theResult->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
1073 if(!anAttribute || !anAttribute->isInitialized()) {
1076 return fabs(anAttribute->value()) < 1.e-12;
1079 bool ModelHighAPI_Dumper::dumpCommentBeforeFeature(const FeaturePtr& theFeature) const
1081 // currently, the comment should not be dumped only before the filters
1082 FiltersFeaturePtr aFilters = std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(theFeature);
1085 // all other features should be commented before the dump
1086 return !isDumped(theFeature);
1089 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
1091 *myDumpStorage << theChar;
1095 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
1097 *myDumpStorage << theString;
1101 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
1103 *myDumpStorage << theString;
1107 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::wstring& theString)
1109 *myDumpStorage << Locale::Convert::toString(theString);
1113 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const bool theValue)
1115 *myDumpStorage << (theValue ? "True" : "False");
1119 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
1121 *myDumpStorage << theValue;
1125 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
1127 *myDumpStorage << theValue;
1131 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
1133 importModule("GeomAPI");
1134 *myDumpStorage << "GeomAPI_Pnt(" << thePoint->x() << ", "
1135 << thePoint->y() << ", " << thePoint->z() << ")";
1139 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
1141 importModule("GeomAPI");
1142 *myDumpStorage << "GeomAPI_Dir(" << theDir->x() << ", "
1143 << theDir->y() << ", " << theDir->z() << ")";
1147 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1148 const std::shared_ptr<GeomDataAPI_Dir>& theDir)
1150 *myDumpStorage << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
1154 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1155 const std::shared_ptr<GeomDataAPI_Point>& thePoint)
1157 static const int aSize = 3;
1158 double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
1159 std::string aTexts[aSize] = {
1160 Locale::Convert::toString(thePoint->textX()),
1161 Locale::Convert::toString(thePoint->textY()),
1162 Locale::Convert::toString(thePoint->textZ())
1164 myDumpStorage->dumpArray(aSize, aValues, aTexts);
1168 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1169 const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
1171 static const int aSize = 2;
1172 double aValues[aSize] = {thePoint->x(), thePoint->y()};
1173 std::string aTexts[aSize] = {
1174 Locale::Convert::toString(thePoint->textX()),
1175 Locale::Convert::toString(thePoint->textY())
1177 myDumpStorage->dumpArray(aSize, aValues, aTexts);
1181 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1182 const std::shared_ptr<GeomDataAPI_Point2DArray>& thePointArray)
1184 static const int aThreshold = 4;
1185 static bool aDumpAsIs = false;
1186 static std::string aSeparator = "";
1187 // if number of elements in the list if greater than a threshold,
1188 // dump it in a separate line with specific name
1189 int aSize = thePointArray->size();
1190 if (aDumpAsIs || aSize <= aThreshold) {
1191 *myDumpStorage << "[";
1192 GeomPnt2dPtr aPoint = thePointArray->pnt(0);
1193 *myDumpStorage << "(" << aPoint->x() << ", " << aPoint->y() << ")";
1194 for (int anIndex = 1; anIndex < aSize; ++anIndex) {
1195 aPoint = thePointArray->pnt(anIndex);
1196 *myDumpStorage << "," << aSeparator << " (" << aPoint->x() << ", " << aPoint->y() << ")";
1198 *myDumpStorage << aSeparator << "]";
1202 FeaturePtr anOwner = ModelAPI_Feature::feature(thePointArray->owner());
1203 std::string aListName = name(anOwner) + "_" + thePointArray->id();
1204 // reserve dumped buffer and store list "as is"
1205 myDumpStorage->reserveBuffer();
1207 aSeparator = std::string("\n") + std::string(aListName.size() + 3, ' ');
1208 *this << aListName << " = " << thePointArray << "\n";
1211 // append reserved data to the end of the current buffer
1212 myDumpStorage->restoreReservedBuffer();
1213 *myDumpStorage << aListName;
1218 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1219 const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
1221 *myDumpStorage << (theAttrBool->value() ? "True" : "False");
1225 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1226 const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
1228 std::string aText = Locale::Convert::toString(theAttrInt->text());
1230 *myDumpStorage << theAttrInt->value();
1232 *myDumpStorage << "\"" << aText << "\"";
1236 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1237 const std::shared_ptr<ModelAPI_AttributeIntArray>& theArray)
1239 *myDumpStorage << "[";
1240 int aSize = theArray->size();
1242 *myDumpStorage << theArray->value(0);
1243 for (int anIndex = 1; anIndex < aSize; ++anIndex)
1244 *myDumpStorage << ", " << theArray->value(anIndex);
1246 *myDumpStorage << "]";
1250 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1251 const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
1253 std::string aText = Locale::Convert::toString(theAttrReal->text());
1255 *myDumpStorage << theAttrReal->value();
1257 *myDumpStorage << "\"" << aText << "\"";
1261 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1262 const std::shared_ptr<ModelAPI_AttributeDoubleArray>& theArray)
1264 *myDumpStorage << "[";
1265 int aSize = theArray->size();
1267 *myDumpStorage << theArray->value(0);
1268 for (int anIndex = 1; anIndex < aSize; ++anIndex)
1269 *myDumpStorage << ", " << theArray->value(anIndex);
1271 *myDumpStorage << "]";
1275 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1276 const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
1278 // escaping the quote sign in the string under dumping
1279 std::string aStr = theAttrStr->value();
1280 size_t aPos = aStr.find("\"");
1281 while (aPos != std::string::npos) {
1282 aStr.insert(aPos, "\\");
1283 aPos = aStr.find("\"", aPos + 2);
1285 *myDumpStorage << "'" << aStr << "'";
1289 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FolderPtr& theFolder)
1291 *myDumpStorage << name(theFolder);
1293 // add dumped folder to a stack
1294 if (!myNames[theFolder].myIsDumped &&
1295 (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theFolder))
1296 myEntitiesStack.push(LastDumpedEntity(theFolder, !myNames[theFolder].myIsDefault));
1301 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity)
1303 *myDumpStorage << name(theEntity);
1305 if (!myNames[theEntity].myIsDumped) {
1306 bool isUserDefinedName = !myNames[theEntity].myIsDefault;
1307 // store results if they have user-defined names or colors
1308 std::list<ResultPtr> aResultsWithNameOrColor;
1309 std::list<ResultPtr> allRes;
1310 ModelAPI_Tools::allResults(theEntity, allRes);
1311 for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
1312 if(!myNames[*aRes].myIsDefault || !isDefaultColor(*aRes) ||
1313 !isDefaultDeflection(*aRes) || !isDefaultTransparency(*aRes))
1314 aResultsWithNameOrColor.push_back(*aRes);
1316 // store just dumped entity to stack
1317 if (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theEntity)
1318 myEntitiesStack.push(
1319 LastDumpedEntity(theEntity, isUserDefinedName, aResultsWithNameOrColor));
1322 // remove entity from the list of not dumped items
1323 myNotDumpedEntities.erase(theEntity);
1327 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ResultPtr& theResult)
1329 // iterate in the structure of sub-results to the parent
1330 ResultPtr aCurRes = theResult;
1331 FeaturePtr aFeature = ModelAPI_Feature::feature(theResult);
1332 std::list<int> anIndices; // indexes of results in the parent result, starting from topmost
1333 while(aCurRes.get()) {
1334 ResultBodyPtr aParent = ModelAPI_Tools::bodyOwner(aCurRes);
1336 anIndices.push_front(ModelAPI_Tools::bodyIndex(aCurRes));
1337 } else { // index of the result in the feature
1338 std::list<ResultPtr>::const_iterator aRes = aFeature->results().cbegin();
1339 for(int anIndex = 0; aRes != aFeature->results().cend(); aRes++, anIndex++) {
1340 if (*aRes == aCurRes) {
1341 anIndices.push_front(anIndex);
1349 *myDumpStorage << name(aFeature);
1350 for (std::list<int>::iterator anI = anIndices.begin(); anI != anIndices.end(); anI++) {
1351 if (anI == anIndices.begin()) {
1353 *myDumpStorage << ".result()";
1356 *myDumpStorage << ".results()[" << *anI << "]";
1359 *myDumpStorage << ".subResult(" << *anI << ")";
1366 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::list<ResultPtr>& theResults)
1369 for (std::list<ResultPtr>::const_iterator anIt = theResults.begin();
1370 anIt != theResults.end(); ++anIt) {
1371 if (anIt != theResults.begin())
1379 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ObjectPtr& theObject)
1381 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
1382 if(aFeature.get()) {
1383 *myDumpStorage << name(aFeature);
1387 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1396 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const AttributePtr& theAttr)
1398 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttr->owner());
1400 std::string aWrapperPrefix, aWrapperSuffix;
1401 // Check the attribute belongs to copied (in multi-translation or multi-rotation) feature.
1402 // In this case we need to cast explicitly feature to appropriate type.
1403 AttributeBooleanPtr isCopy = anOwner->boolean("Copy");
1404 AttributeReferencePtr hasParent = anOwner->reference("ParentFeature");
1405 if ((isCopy.get() && isCopy->value()) || (hasParent && hasParent->value())) {
1406 aWrapperPrefix = featureWrapper(anOwner) + "(";
1407 aWrapperSuffix = ")";
1408 importModule("SketchAPI");
1411 *myDumpStorage << aWrapperPrefix << name(anOwner) << aWrapperSuffix
1412 << "." << attributeGetter(anOwner, theAttr->id()) << "()";
1416 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1417 const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
1419 if (theRefAttr->isObject())
1420 *this << theRefAttr->object();
1422 *this << theRefAttr->attr();
1426 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1427 const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList)
1429 *myDumpStorage << "[";
1430 std::list<std::pair<ObjectPtr, AttributePtr> > aList = theRefAttrList->list();
1431 bool isAdded = false;
1432 std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aList.begin();
1433 for (; anIt != aList.end(); ++anIt) {
1435 *myDumpStorage << ", ";
1439 *this << anIt->first;
1440 else if (anIt->second)
1441 * this << anIt->second;
1443 *myDumpStorage << "]";
1447 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1448 const std::shared_ptr<ModelAPI_AttributeReference>& theReference)
1450 *this << theReference->value();
1454 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1455 const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList)
1457 static const int aThreshold = 2;
1458 static bool aDumpAsIs = false;
1459 // if number of elements in the list if greater than a threshold,
1460 // dump it in a separate line with specific name
1461 if (aDumpAsIs || theRefList->size() <= aThreshold) {
1462 *myDumpStorage << "[";
1463 std::list<ObjectPtr> aList = theRefList->list();
1464 bool isAdded = false;
1465 std::list<ObjectPtr>::const_iterator anIt = aList.begin();
1466 for (; anIt != aList.end(); ++anIt) {
1469 if (!isDumped(ModelAPI_Feature::feature(*anIt)))
1470 break; // stop if the object is not dumped yet (parent feature should be postponed)
1473 *myDumpStorage << ", ";
1479 *myDumpStorage << "]";
1482 FeaturePtr anOwner = ModelAPI_Feature::feature(theRefList->owner());
1483 std::string aListName = name(anOwner) + "_objects";
1484 // reserve dumped buffer and store list "as is"
1485 myDumpStorage->reserveBuffer();
1487 *this << aListName << " = " << theRefList << "\n";
1489 // append reserved data to the end of the current buffer
1490 myDumpStorage->restoreReservedBuffer();
1491 *myDumpStorage << aListName;
1496 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1497 const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
1499 myDumpStorage->write(theAttrSelect);
1503 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1504 const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList)
1506 static const int aThreshold = 2;
1507 static int aNbSpaces = 0;
1508 // if number of elements in the list if greater than a threshold,
1509 // dump it in a separate line with specific name
1510 if (aNbSpaces > 0 || theAttrSelList->size() <= aThreshold) {
1511 *myDumpStorage << "[";
1513 GeomShapePtr aShape;
1514 std::string aShapeTypeStr;
1516 bool isAdded = false;
1518 for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) {
1519 AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex);
1520 aShape = anAttribute->value();
1522 ResultPtr aContext = anAttribute->context();
1524 aShape = aContext->shape();
1532 // print each attribute on separate line with the appropriate shift
1533 if (aNbSpaces > 0) {
1534 std::string aSpaces(aNbSpaces + 1, ' ');
1535 *myDumpStorage << ",\n" << aSpaces;
1537 *myDumpStorage << ", ";
1541 *this << anAttribute;
1544 // check selection list is obtained by filters
1545 FiltersFeaturePtr aFilters = theAttrSelList->filters();
1547 if (theAttrSelList->size() > 0)
1548 *myDumpStorage << ", ";
1549 dumpFeature(aFilters, true);
1552 *myDumpStorage << "]";
1554 // obtain name of list (the feature may contain several selection lists)
1555 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttrSelList->owner());
1556 std::string aListName = name(anOwner) + "_objects";
1557 std::list<AttributePtr> aSelLists =
1558 anOwner->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
1559 if (aSelLists.size() > 1) {
1561 for (std::list<AttributePtr>::iterator aSIt = aSelLists.begin();
1562 aSIt != aSelLists.end(); ++aSIt, ++anIndex)
1563 if ((*aSIt).get() == theAttrSelList.get())
1565 std::ostringstream aSStream;
1566 aSStream << aListName << "_" << anIndex;
1567 aListName = aSStream.str();
1569 // reserve dumped buffer and store list "as is"
1570 myDumpStorage->reserveBuffer();
1571 aNbSpaces = (int)aListName.size() + 3;
1572 *this << aListName << " = " << theAttrSelList << "\n";
1574 // append reserved data to the end of the current buffer
1575 myDumpStorage->restoreReservedBuffer();
1576 *myDumpStorage << aListName;
1581 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1582 const std::shared_ptr<ModelAPI_AttributeStringArray>& theArray)
1584 std::ostringstream aBuffer;
1586 for(int anIndex = 0; anIndex < theArray->size(); ++anIndex) {
1590 aBuffer << "\"" << theArray->value(anIndex) << "\"";
1594 myDumpStorage->write(aBuffer.str());
1598 void ModelHighAPI_Dumper::newline()
1604 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
1605 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
1607 *theDumper.myDumpStorage << theEndl;
1609 if (!theDumper.myEntitiesStack.empty()) {
1611 // all copies have been stored into stack, pop them all
1614 // Name for composite feature is dumped when all sub-entities are dumped
1615 // (see method ModelHighAPI_Dumper::processSubs).
1616 const ModelHighAPI_Dumper::LastDumpedEntity& aLastDumped = theDumper.myEntitiesStack.top();
1617 CompositeFeaturePtr aComposite =
1618 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aLastDumped.myEntity);
1620 theDumper.dumpEntitySetName();
1621 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aLastDumped.myEntity);
1623 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
1624 isCopy = aCopyAttr.get() && aCopyAttr->value();
1627 } while (isCopy && !theDumper.myEntitiesStack.empty());
1630 // store all not-dumped entities first
1631 std::set<EntityPtr> aNotDumped = theDumper.myNotDumpedEntities;
1632 theDumper.clearNotDumped();
1633 theDumper.myDumpStorage->reserveBuffer();
1634 std::set<EntityPtr>::const_iterator anIt = aNotDumped.begin();
1635 for (; anIt != aNotDumped.end(); ++anIt) {
1636 // if the feature is composite, dump it with all subs
1637 CompositeFeaturePtr aCompFeat =
1638 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
1640 theDumper.process(aCompFeat, true);
1642 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
1643 theDumper.dumpFeature(aFeature, true);
1644 // dump the Projection feature which produces this "Copy" entity
1645 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
1646 if (aCopyAttr.get() && aCopyAttr->value())
1648 const std::set<AttributePtr>& aRefs = aFeature->data()->refsToMe();
1649 std::set<AttributePtr>::iterator aRefIt = aRefs.begin();
1650 for (; aRefIt != aRefs.end(); ++aRefIt)
1651 if ((*aRefIt)->id() == "ProjectedFeature")
1652 { // process projection only
1653 FeaturePtr anOwner = ModelAPI_Feature::feature((*aRefIt)->owner());
1654 if (anOwner && !theDumper.isDumped(EntityPtr(anOwner)))
1655 theDumper.dumpFeature(anOwner, true);
1661 // then store the reserved data
1662 theDumper.myDumpStorage->restoreReservedBuffer();
1663 theDumper.myDumpStorage->mergeBuffer();
1665 // now, store all postponed features
1666 theDumper.dumpPostponed();
1672 void ModelHighAPI_Dumper::exportVariables() const
1674 DocumentPtr aRoot = ModelAPI_Session::get()->moduleDocument();
1675 EntityNameMap::const_iterator aNameIter = myNames.cbegin();
1676 for(; aNameIter != myNames.end(); aNameIter++) {
1677 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aNameIter->first);
1678 if (aFeature.get() && aFeature->document() != aRoot) {
1679 FeaturePtr aPartFeat = ModelAPI_Tools::findPartFeature(aRoot, aFeature->document());
1680 if (aPartFeat.get()) {
1681 int aFeatureId = aFeature->data()->featureId();
1682 int aPartId = aPartFeat->data()->featureId();
1683 std::ostringstream anEntryStr;
1684 anEntryStr<<aPartId<<":"<<aFeatureId;
1685 std::string anEntry = anEntryStr.str();
1686 exportVariable(anEntry, aNameIter->second.myCurrentName);
1687 size_t aSize = aFeature->results().size();
1688 if (aSize > 1) { // additional entries for features with more than one result
1689 for(size_t a = 1; a < aSize; a++) {
1690 std::ostringstream aResEntryStr;
1691 aResEntryStr<<anEntry<<":"<<a;
1692 std::string aResEntry = aResEntryStr.str();
1693 exportVariable(aResEntry, aNameIter->second.myCurrentName);