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 // Find the position of the part, where its features should be inserted.
336 // It will avoid checking of appropriate elements in partSet after the current part.
337 std::list<FeaturePtr>::iterator aFIt = aFeatures.begin();
338 for (; aFIt != aFeatures.end(); ++aFIt) {
339 ResultPartPtr aPartRes =
340 std::dynamic_pointer_cast<ModelAPI_ResultPart>((*aFIt)->lastResult());
341 if (aPartRes && aPartRes->partDoc() == aDoc2)
345 std::list<FeaturePtr> anAdditionalFeatures = aDoc2->allFeatures();
346 aFeatures.insert(aFIt, anAdditionalFeatures.begin(), anAdditionalFeatures.end());
349 CompositeFeaturePtr aLastCompositeFeature;
351 std::list<FeaturePtr>::const_iterator aFIt = aFeatures.begin();
352 while (aFIt != aFeatures.end() && *aFIt != theStartFeature) {
353 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFIt);
355 aLastCompositeFeature = aCompFeat;
359 // collect the list of composite features, containing the last feature;
360 // these features should be excluded from searching,
361 // because the feature cannot select sub-shapes from its parent
362 std::set<FeaturePtr> aEndFeatureParents = ModelAPI_Tools::getParents(theEndFeature);
364 int aNbPossibleSelections = 0;
365 for (; aFIt != aFeatures.end() && *aFIt != theEndFeature; ++aFIt) {
366 bool isSkipFeature = false;
367 if (aLastCompositeFeature && aLastCompositeFeature->isSub(*aFIt))
368 isSkipFeature = true;
369 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFIt);
371 ResultPartPtr aPartRes =
372 std::dynamic_pointer_cast<ModelAPI_ResultPart>(aCompFeat->firstResult());
374 aLastCompositeFeature = aCompFeat;
375 if (aEndFeatureParents.find(aCompFeat) != aEndFeatureParents.end()) {
376 // do not process the parent for the last feature,
377 // because it cannot select objects from its parent
378 isSkipFeature = true;
384 std::list<ModelGeomAlgo_Shape::SubshapeOfResult> anApproproate;
385 if (ModelGeomAlgo_Shape::findSubshapeByPoint(*aFIt, thePoint, theShape->shapeType(),
387 bool isContinue = true;
388 std::list<std::pair<GeomShapePtr, int> > aCenters;
389 std::list<ModelGeomAlgo_Shape::SubshapeOfResult>::iterator anApIt = anApproproate.begin();
390 for (; anApIt != anApproproate.end() && isContinue; ++anApIt) {
391 ++aNbPossibleSelections;
393 // stop if the target shape and result are found
394 GeomShapePtr aCurShape = anApIt->mySubshape;
396 aCurShape = anApIt->myResult->shape();
398 if (anApIt->myResult->isSame(theResult)) {
399 if (anApIt->myCenterType == (int)ModelAPI_AttributeSelection::NOT_CENTER)
400 isContinue = !aCurShape->isSame(theShape);
401 else if (theShape->isVertex() && aCurShape->isEdge()) {
402 GeomEdgePtr aCurEdge = aCurShape->edge();
403 GeomVertexPtr aVertex = theShape->vertex();
404 GeomPointPtr aCenter;
405 switch (anApIt->myCenterType) {
406 case (int)ModelAPI_AttributeSelection::CIRCLE_CENTER: {
407 GeomCirclePtr aCirc = aCurEdge->circle();
409 aCenter = aCirc->center();
412 case (int)ModelAPI_AttributeSelection::ELLIPSE_FIRST_FOCUS: {
413 GeomEllipsePtr anEllipse = aCurEdge->ellipse();
415 aCenter = anEllipse->firstFocus();
418 case (int)ModelAPI_AttributeSelection::ELLIPSE_SECOND_FOCUS: {
419 GeomEllipsePtr anEllipse = aCurEdge->ellipse();
421 aCenter = anEllipse->secondFocus();
425 if (aCenter && aCenter->distance(aVertex->point()) < 1.e-7)
426 aCenters.push_back(std::pair<GeomShapePtr, int>(aCurShape, aNbPossibleSelections));
430 // passed till the appropriate shape, check the center of circle
431 // or a focus of ellipse is selected
432 if (isContinue && !aCenters.empty())
433 aNbPossibleSelections = aCenters.front().second;
436 return aNbPossibleSelections;
439 void ModelHighAPI_Dumper::DumpStorageGeom::write(const AttributeSelectionPtr& theAttrSelect)
443 getShapeAndContext(theAttrSelect, aShape, aContext);
445 // how to dump selection: construction features are dumped by name always
446 FeaturePtr aSelectedFeature;
447 FeaturePtr aFeature = theAttrSelect->contextFeature();
448 if (aShape && aContext && !aFeature)
449 aSelectedFeature = ModelAPI_Feature::feature(aContext->data()->owner());
450 bool isDumpByGeom = aSelectedFeature && aSelectedFeature->isInHistory();
453 myDumpBuffer << "model.selection(\"" << aShape->shapeTypeStr();
454 // check the selected item is a ResultPart;
455 // in this case it is necessary to get shape with full transformation
456 // for correct calculation of the middle point
457 ResultPartPtr aResPart =
458 std::dynamic_pointer_cast<ModelAPI_ResultPart>(theAttrSelect->context());
459 if (aResPart && aShape->shapeType() == GeomAPI_Shape::COMPOUND)
460 aShape = aResPart->shape();
461 GeomPointPtr aMiddlePoint = aShape->middlePoint();
462 // calculate number of features, which could be selected by the same point
463 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttrSelect->owner());
464 int aNbPossibleSelections = possibleSelectionsByPoint(aMiddlePoint,
465 theAttrSelect->context(), aShape, aSelectedFeature, anOwner);
467 // produce the index if the number of applicable features is greater than 1
469 if (aNbPossibleSelections > 1) {
470 std::ostringstream anOutput;
471 anOutput << "_" << aNbPossibleSelections;
472 anIndex = anOutput.str();
475 myDumpBuffer << std::setprecision(THE_DUMP_PRECISION)
476 << anIndex << "\", ("
477 << aMiddlePoint->x() << ", "
478 << aMiddlePoint->y() << ", "
479 << aMiddlePoint->z() << ")";
483 DumpStorage::write(theAttrSelect);
486 void ModelHighAPI_Dumper::DumpStorageWeak::write(const AttributeSelectionPtr& theAttrSelect)
490 getShapeAndContext(theAttrSelect, aShape, aContext);
492 bool aStandardDump = true;
493 if (aShape.get() && aContext.get() &&
494 aShape != aContext->shape()) { // weak naming for local selection only
495 GeomAlgoAPI_NExplode aNExplode(aContext->shape(), aShape->shapeType());
496 int anIndex = aNExplode.index(aShape);
497 if (anIndex != 0) { // found a week-naming index, so, export it
498 myDumpBuffer << "model.selection(\"" << aShape->shapeTypeStr() << "\", \""
499 << theAttrSelect->contextName(aContext) << "\", " << anIndex << ")";
500 aStandardDump = false;
504 DumpStorage::write(theAttrSelect);
506 // ======================================================================
509 static int gCompositeStackDepth = 0;
511 ModelHighAPI_Dumper* ModelHighAPI_Dumper::mySelf = 0;
513 ModelHighAPI_Dumper::ModelHighAPI_Dumper()
514 : myDumpStorage(new DumpStorageBuffer),
515 myDumpPostponedInProgress(false)
519 ModelHighAPI_Dumper::~ModelHighAPI_Dumper()
521 delete myDumpStorage;
524 void ModelHighAPI_Dumper::setInstance(ModelHighAPI_Dumper* theDumper)
530 ModelHighAPI_Dumper* ModelHighAPI_Dumper::getInstance()
535 void ModelHighAPI_Dumper::addCustomStorage(const ModelHighAPI_Dumper::DumpStoragePtr& theStorage)
537 myDumpStorage->addStorage(theStorage);
540 void ModelHighAPI_Dumper::clearCustomStorage()
542 myDumpStorage->clear();
546 myFeatureCount.clear();
548 while (!myEntitiesStack.empty())
549 myEntitiesStack.pop();
553 void ModelHighAPI_Dumper::clearNotDumped()
555 myNotDumpedEntities.clear();
558 // Convert string to integer. If the string is not a number, return -1
559 static int toInt(const std::string& theString)
561 std::string::const_iterator aChar = theString.begin();
562 for (; aChar != theString.end(); ++aChar)
563 if (!std::isdigit(*aChar))
565 if (aChar != theString.end())
566 return -1; // not a number
567 return std::stoi(theString);
570 const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity,
571 bool theSaveNotDumped,
572 bool theUseEntityName,
575 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
576 if (aFound != myNames.end())
577 return aFound->second.myCurrentName;
579 // entity is not found, store it
580 std::string aName, aKind;
581 bool isDefaultName = false;
582 bool isSaveNotDumped = theSaveNotDumped;
583 std::ostringstream aDefaultName;
584 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
586 aName = aFeature->name();
587 aKind = aFeature->getKind();
589 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(theEntity);
591 aName = aFolder->data()->name();
592 aKind = ModelAPI_Folder::ID();
593 isSaveNotDumped = false;
597 ObjectPtr anObject = std::dynamic_pointer_cast<ModelAPI_Object>(theEntity);
599 DocumentPtr aDoc = anObject->document();
600 std::pair<int, int>& aNbFeatures = myFeatureCount[aDoc][aKind];
601 aNbFeatures.first += 1;
603 size_t anIndex = aName.find(aKind);
604 if (anIndex == 0 && aName[aKind.length()] == '_') { // name starts with "FeatureKind_"
605 std::string anIdStr = aName.substr(aKind.length() + 1);
606 int anId = toInt(anIdStr);
608 // Check number of already registered objects of such kind. Index of current object
609 // should be the same to identify feature's name as automatically generated.
610 if (aNbFeatures.first == anId && aNbFeatures.second < anId) {
611 // name is not user-defined
612 isDefaultName = true;
614 // check there are postponed features of this kind,
615 // dump their names, because the sequence of features may be changed
616 for (std::list<EntityPtr>::const_iterator aPpIt = myPostponed.begin();
617 aPpIt != myPostponed.end(); ++aPpIt) {
618 FeaturePtr aCurFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*aPpIt);
619 if (aCurFeature && aCurFeature->getKind() == aKind) {
620 myNames[*aPpIt].myIsDefault = false;
621 isDefaultName = false;
626 if (anId > aNbFeatures.second)
627 aNbFeatures.second = anId;
630 // obtain default name for the feature
631 if (theUseEntityName)
632 aDefaultName << aName;
635 NbFeaturesMap::const_iterator aFIt = myFeatureCount.begin();
636 for (; aFIt != myFeatureCount.end(); ++aFIt) {
637 std::map<std::string, std::pair<int, int> >::const_iterator aFound =
638 aFIt->second.find(aKind);
639 if (aFound != aFIt->second.end())
640 aFullIndex += aFound->second.first;
642 aDefaultName << aKind << "_" << aFullIndex;
646 myNames[theEntity] = EntityName(aDefaultName.str(), aName, isDefaultName);
648 myNotDumpedEntities.insert(theEntity);
650 // store names of results
652 saveResultNames(aFeature);
654 myNames[theEntity].myIsDumped = theSetIsDumped;
656 return myNames[theEntity].myCurrentName;
659 const std::string& ModelHighAPI_Dumper::parentName(const FeaturePtr& theEntity)
661 const std::set<AttributePtr>& aRefs = theEntity->data()->refsToMe();
662 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
663 for (; aRefIt != aRefs.end(); ++aRefIt) {
664 CompositeFeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(
665 ModelAPI_Feature::feature((*aRefIt)->owner()));
667 return name(anOwner);
670 static const std::string DUMMY;
674 void ModelHighAPI_Dumper::saveResultNames(const FeaturePtr& theFeature)
676 // Default name of the feature
677 bool isFeatureDefaultName = myNames[theFeature].myIsDefault;
679 // Save only names of results which is not correspond to default feature name
680 const std::list<ResultPtr>& aResults = theFeature->results();
681 std::list<ResultPtr> allRes;
682 ModelAPI_Tools::allResults(theFeature, allRes);
683 for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
684 std::pair<std::string, bool> aName = ModelAPI_Tools::getDefaultName(*aRes);
685 std::string aDefaultName = aName.first;
686 std::string aResName = (*aRes)->data()->name();
687 bool isUserDefined = !(isFeatureDefaultName && aDefaultName == aResName);
689 EntityName(aResName, (isUserDefined ? aResName : std::string()), !isUserDefined);
693 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc,
694 const std::string& theFileName)
696 // dump top level document feature
697 static const std::string aDocName("partSet");
698 myNames[theDoc] = EntityName(aDocName, std::string(), true);
699 *this << aDocName << " = model.moduleDocument()" << std::endl;
701 // dump subfeatures and store result to file
702 bool isOk = process(theDoc) && myDumpStorage->exportTo(theFileName, myModules);
706 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc)
709 std::list<ObjectPtr> anObjects = theDoc->allObjects();
710 std::list<ObjectPtr>::const_iterator anObjIt = anObjects.begin();
711 // firstly, dump all parameters
712 for (; anObjIt != anObjects.end(); ++ anObjIt) {
713 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIt);
715 dumpParameter(aFeature);
717 // dump all other features
718 for (anObjIt = anObjects.begin(); anObjIt != anObjects.end(); ++anObjIt) {
719 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anObjIt);
721 // iteratively process composite features,
722 // if the composite feature is the last in the document, no need to dump "model.do()" action
723 std::list<ObjectPtr>::const_iterator aNext = anObjIt;
724 isOk = process(aCompFeat, false, ++aNext != anObjects.end()) && isOk;
726 else if (!isDumped(EntityPtr(*anObjIt))) {
728 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(*anObjIt);
732 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIt);
733 if (aFeature) // dump common feature
734 dumpFeature(aFeature);
738 // dump folders if any
743 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
744 bool isForce, bool isDumpModelDo)
746 // increase composite features stack
747 ++gCompositeStackDepth;
748 // dump composite itself
749 if (!isDumped(EntityPtr(theComposite)) || isForce)
750 dumpFeature(FeaturePtr(theComposite), isForce);
752 // sub-part is processed independently, because it provides separate document
753 if (theComposite->getKind() == PartSetPlugin_Part::ID()) {
754 // dump name of the part if it is different from default
755 if (!myEntitiesStack.empty())
758 // decrease composite features stack because we run into separate document
759 --gCompositeStackDepth;
761 ResultPartPtr aPartResult =
762 std::dynamic_pointer_cast<ModelAPI_ResultPart>(theComposite->lastResult());
765 DocumentPtr aSubDoc = aPartResult->partDoc();
768 // set name of document
769 const std::string& aPartName = myNames[theComposite].myCurrentName;
770 std::string aDocName = aPartName + "_doc";
771 myNames[aSubDoc] = EntityName(aDocName, std::string(), true);
773 // dump document in a separate line
774 *this << aDocName << " = " << aPartName << ".document()" << std::endl;
775 // dump features in the document
776 bool aRes = process(aSubDoc);
778 *this << "\nmodel.do()\n";
784 bool isOk = processSubs(theComposite);
785 // decrease composite features stack
786 --gCompositeStackDepth;
791 bool ModelHighAPI_Dumper::processSubs(
792 const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
796 // dump all sub-features;
797 bool isSubDumped = false;
798 int aNbSubs = theComposite->numberOfSubs();
799 for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) {
800 FeaturePtr aFeature = theComposite->subFeature(anIndex);
801 if (isDumped(EntityPtr(aFeature)))
805 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
806 if (aCompFeat) // iteratively process composite features
807 isOk = process(aCompFeat) && isOk;
809 dumpFeature(aFeature, true);
812 bool isDumpSetName = !myEntitiesStack.empty() &&
813 myEntitiesStack.top().myEntity == EntityPtr(theComposite);
814 bool isForceModelDo = isSubDumped && isDumpSetName &&
815 (myEntitiesStack.top().myUserName || !myEntitiesStack.top().myResults.empty());
816 // It is necessary for the sketch to create its result when complete (command "model.do()").
817 // This option is set by flat theDumpModelDo.
818 // However, nested sketches are rebuilt by parent feature, so, they do not need
819 // explicit call of "model.do()". This will be controlled by the depth of the stack.
820 if (isForceModelDo || (theDumpModelDo && gCompositeStackDepth <= 1))
821 *this << "model.do()" << std::endl;
823 // dump "setName" for composite feature
829 void ModelHighAPI_Dumper::postpone(const EntityPtr& theEntity)
832 name(theEntity, false);
833 myPostponed.push_back(theEntity);
836 void ModelHighAPI_Dumper::dumpPostponed(bool theDumpFolders)
838 if (myDumpPostponedInProgress)
841 myDumpPostponedInProgress = true;
842 // make a copy of postponed entities, because the list will be updated
843 // if some features are not able to be dumped
844 std::list<EntityPtr> aPostponedCopy = myPostponed;
847 // iterate over postponed entities and try to dump them
848 std::list<EntityPtr>::const_iterator anIt = aPostponedCopy.begin();
849 for (; anIt != aPostponedCopy.end(); ++anIt) {
850 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(*anIt);
855 myPostponed.push_back(*anIt);
858 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
860 dumpFeature(aFeature, true);
863 myDumpPostponedInProgress = false;
866 void ModelHighAPI_Dumper::dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
867 const FeaturePtr& theSubFeature)
869 name(theSubFeature, false);
870 myNames[theSubFeature] = EntityName(theSubFeatureGet, theSubFeature->name(), false);
872 // store results if they have user-defined names or colors
873 std::list<ResultPtr> aResultsWithNameOrColor;
874 const std::list<ResultPtr>& aResults = theSubFeature->results();
875 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
876 for (; aResIt != aResults.end(); ++aResIt) {
877 std::string aResName = (*aResIt)->data()->name();
878 myNames[*aResIt] = EntityName(aResName, aResName, false);
879 aResultsWithNameOrColor.push_back(*aResIt);
882 // store just dumped entity to stack
883 myEntitiesStack.push(LastDumpedEntity(theSubFeature, true, aResultsWithNameOrColor));
888 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName)
890 myModules.insert(theModuleName);
893 void ModelHighAPI_Dumper::dumpEntitySetName()
895 const LastDumpedEntity& aLastDumped = myEntitiesStack.top();
896 bool isBufferEmpty = myDumpStorage->isBufferEmpty();
898 // dump "setName" for the entity
899 if (aLastDumped.myUserName) {
900 EntityName& anEntityNames = myNames[aLastDumped.myEntity];
901 if (!anEntityNames.myIsDefault)
902 *myDumpStorage << anEntityNames.myCurrentName << ".setName(\""
903 << anEntityNames.myUserName << "\")\n";
904 // don't dump "setName" for the entity twice
905 anEntityNames.myUserName.clear();
906 anEntityNames.myIsDefault = true;
908 // dump "setName" for results
909 std::list<ResultPtr>::const_iterator aResIt = aLastDumped.myResults.begin();
910 std::list<ResultPtr>::const_iterator aResEnd = aLastDumped.myResults.end();
911 for (; aResIt != aResEnd; ++aResIt) {
913 EntityName& anEntityNames = myNames[*aResIt];
914 if (!anEntityNames.myIsDefault) {
916 *myDumpStorage << ".setName(\"" << anEntityNames.myUserName << "\")\n";
917 // don't dump "setName" for the entity twice
918 anEntityNames.myUserName.clear();
919 anEntityNames.myIsDefault = true;
922 if (!isDefaultColor(*aResIt)) {
923 AttributeIntArrayPtr aColor = (*aResIt)->data()->intArray(ModelAPI_Result::COLOR_ID());
924 if (aColor && aColor->isInitialized()) {
926 *myDumpStorage << ".setColor(" << aColor->value(0) << ", " << aColor->value(1)
927 << ", " << aColor->value(2) << ")\n";
930 // set result deflection
931 if (!isDefaultDeflection(*aResIt)) {
932 AttributeDoublePtr aDeflectionAttr =
933 (*aResIt)->data()->real(ModelAPI_Result::DEFLECTION_ID());
934 if(aDeflectionAttr.get() && aDeflectionAttr->isInitialized()) {
936 *myDumpStorage << ".setDeflection(" << aDeflectionAttr->value() << ")\n";
939 // set result transparency
940 if (!isDefaultTransparency(*aResIt)) {
941 AttributeDoublePtr aTransparencyAttr =
942 (*aResIt)->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
943 if(aTransparencyAttr.get() && aTransparencyAttr->isInitialized()) {
945 *myDumpStorage << ".setTransparency(" << aTransparencyAttr->value() << ")\n";
950 myNames[aLastDumped.myEntity].myIsDumped = true;
951 myEntitiesStack.pop();
953 // clean buffer if it was clear before
955 myDumpStorage->mergeBuffer();
958 bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const
960 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
961 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
962 return (aFound != myNames.end() && aFound->second.myIsDumped) ||
963 myFeaturesToSkip.find(aFeature) != myFeaturesToSkip.end();
966 bool ModelHighAPI_Dumper::isDumped(const AttributeRefAttrPtr& theRefAttr) const
969 if (theRefAttr->isObject())
970 aFeature = ModelAPI_Feature::feature(theRefAttr->object());
972 aFeature = ModelAPI_Feature::feature(theRefAttr->attr()->owner());
973 return aFeature && isDumped(EntityPtr(aFeature));
976 bool ModelHighAPI_Dumper::isDumped(const AttributeRefListPtr& theRefList) const
978 std::list<ObjectPtr> aRefs = theRefList->list();
979 std::list<ObjectPtr>::iterator anIt = aRefs.begin();
980 for (; anIt != aRefs.end(); ++anIt) {
981 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
982 if (aFeature && !isDumped(EntityPtr(aFeature)))
988 static bool isSketchSub(const FeaturePtr& theFeature)
990 static const std::string SKETCH("Sketch");
991 CompositeFeaturePtr anOwner = ModelAPI_Tools::compositeOwner(theFeature);
992 return anOwner && anOwner->getKind() == SKETCH;
995 bool ModelHighAPI_Dumper::isDefaultColor(const ResultPtr& theResult) const
997 AttributeIntArrayPtr aColor = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
998 if (!aColor || !aColor->isInitialized())
1001 // check the result belongs to sketch entity, do not dump color in this way
1002 ResultConstructionPtr aResConstr =
1003 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult);
1005 FeaturePtr aFeature = ModelAPI_Feature::feature(theResult->data()->owner());
1006 if (isSketchSub(aFeature))
1010 std::string aSection, aName, aDefault;
1011 theResult->colorConfigInfo(aSection, aName, aDefault);
1013 // dump current color
1014 std::ostringstream aColorInfo;
1015 aColorInfo << aColor->value(0) << "," << aColor->value(1) << "," << aColor->value(2);
1017 return aDefault == aColorInfo.str();
1020 bool ModelHighAPI_Dumper::isDefaultDeflection(const ResultPtr& theResult) const
1022 AttributeDoublePtr aDeflectionAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
1023 if(!aDeflectionAttr || !aDeflectionAttr->isInitialized()) {
1027 double aCurrent = aDeflectionAttr->value();
1028 double aDefault = -1;
1030 bool isConstruction = false;
1031 std::string aResultGroup = theResult->groupName();
1032 if (aResultGroup == ModelAPI_ResultConstruction::group())
1033 isConstruction = true;
1034 else if (aResultGroup == ModelAPI_ResultBody::group()) {
1035 GeomShapePtr aGeomShape = theResult->shape();
1036 if (aGeomShape.get()) {
1037 // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
1038 // correction of deviation for them should not influence to the application performance
1039 GeomAPI_ShapeExplorer anExp(aGeomShape, GeomAPI_Shape::FACE);
1040 isConstruction = !anExp.more();
1044 aDefault = Config_PropManager::real("Visualization", "construction_deflection");
1046 aDefault = Config_PropManager::real("Visualization", "body_deflection");
1048 return fabs(aCurrent - aDefault) < 1.e-12;
1051 bool ModelHighAPI_Dumper::isDefaultTransparency(const ResultPtr& theResult) const
1053 AttributeDoublePtr anAttribute = theResult->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
1054 if(!anAttribute || !anAttribute->isInitialized()) {
1057 return fabs(anAttribute->value()) < 1.e-12;
1060 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
1062 *myDumpStorage << theChar;
1066 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
1068 *myDumpStorage << theString;
1072 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
1074 *myDumpStorage << theString;
1078 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const bool theValue)
1080 *myDumpStorage << (theValue ? "True" : "False");
1084 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
1086 *myDumpStorage << theValue;
1090 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
1092 *myDumpStorage << theValue;
1096 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
1098 importModule("GeomAPI");
1099 *myDumpStorage << "GeomAPI_Pnt(" << thePoint->x() << ", "
1100 << thePoint->y() << ", " << thePoint->z() << ")";
1104 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
1106 importModule("GeomAPI");
1107 *myDumpStorage << "GeomAPI_Dir(" << theDir->x() << ", "
1108 << theDir->y() << ", " << theDir->z() << ")";
1112 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1113 const std::shared_ptr<GeomDataAPI_Dir>& theDir)
1115 *myDumpStorage << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
1119 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1120 const std::shared_ptr<GeomDataAPI_Point>& thePoint)
1122 static const int aSize = 3;
1123 double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
1124 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()};
1125 myDumpStorage->dumpArray(aSize, aValues, aTexts);
1129 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1130 const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
1132 static const int aSize = 2;
1133 double aValues[aSize] = {thePoint->x(), thePoint->y()};
1134 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()};
1135 myDumpStorage->dumpArray(aSize, aValues, aTexts);
1139 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1140 const std::shared_ptr<GeomDataAPI_Point2DArray>& thePointArray)
1142 static const int aThreshold = 4;
1143 static bool aDumpAsIs = false;
1144 static std::string aSeparator = "";
1145 // if number of elements in the list if greater than a threshold,
1146 // dump it in a separate line with specific name
1147 int aSize = thePointArray->size();
1148 if (aDumpAsIs || aSize <= aThreshold) {
1149 *myDumpStorage << "[";
1150 GeomPnt2dPtr aPoint = thePointArray->pnt(0);
1151 *myDumpStorage << "(" << aPoint->x() << ", " << aPoint->y() << ")";
1152 for (int anIndex = 1; anIndex < aSize; ++anIndex) {
1153 aPoint = thePointArray->pnt(anIndex);
1154 *myDumpStorage << "," << aSeparator << " (" << aPoint->x() << ", " << aPoint->y() << ")";
1156 *myDumpStorage << aSeparator << "]";
1160 FeaturePtr anOwner = ModelAPI_Feature::feature(thePointArray->owner());
1161 std::string aListName = name(anOwner) + "_" + thePointArray->id();
1162 // reserve dumped buffer and store list "as is"
1163 myDumpStorage->reserveBuffer();
1165 aSeparator = std::string("\n") + std::string(aListName.size() + 3, ' ');
1166 *this << aListName << " = " << thePointArray << "\n";
1169 // append reserved data to the end of the current buffer
1170 myDumpStorage->restoreReservedBuffer();
1171 *myDumpStorage << aListName;
1176 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1177 const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
1179 *myDumpStorage << (theAttrBool->value() ? "True" : "False");
1183 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1184 const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
1186 std::string aText = theAttrInt->text();
1188 *myDumpStorage << theAttrInt->value();
1190 *myDumpStorage << "\"" << aText << "\"";
1194 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1195 const std::shared_ptr<ModelAPI_AttributeIntArray>& theArray)
1197 *myDumpStorage << "[";
1198 int aSize = theArray->size();
1200 *myDumpStorage << theArray->value(0);
1201 for (int anIndex = 1; anIndex < aSize; ++anIndex)
1202 *myDumpStorage << ", " << theArray->value(anIndex);
1204 *myDumpStorage << "]";
1208 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1209 const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
1211 std::string aText = theAttrReal->text();
1213 *myDumpStorage << theAttrReal->value();
1215 *myDumpStorage << "\"" << aText << "\"";
1219 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1220 const std::shared_ptr<ModelAPI_AttributeDoubleArray>& theArray)
1222 *myDumpStorage << "[";
1223 int aSize = theArray->size();
1225 *myDumpStorage << theArray->value(0);
1226 for (int anIndex = 1; anIndex < aSize; ++anIndex)
1227 *myDumpStorage << ", " << theArray->value(anIndex);
1229 *myDumpStorage << "]";
1233 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1234 const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
1236 *myDumpStorage << "\"" << theAttrStr->value() << "\"";
1240 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FolderPtr& theFolder)
1242 *myDumpStorage << name(theFolder);
1244 // add dumped folder to a stack
1245 if (!myNames[theFolder].myIsDumped &&
1246 (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theFolder))
1247 myEntitiesStack.push(LastDumpedEntity(theFolder, !myNames[theFolder].myIsDefault));
1252 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity)
1254 *myDumpStorage << "\n### Create "<< theEntity->getKind() << "\n";
1255 *myDumpStorage << name(theEntity);
1257 if (!myNames[theEntity].myIsDumped) {
1258 bool isUserDefinedName = !myNames[theEntity].myIsDefault;
1259 // store results if they have user-defined names or colors
1260 std::list<ResultPtr> aResultsWithNameOrColor;
1261 std::list<ResultPtr> allRes;
1262 ModelAPI_Tools::allResults(theEntity, allRes);
1263 for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
1264 if(!myNames[*aRes].myIsDefault || !isDefaultColor(*aRes) ||
1265 !isDefaultDeflection(*aRes) || !isDefaultTransparency(*aRes))
1266 aResultsWithNameOrColor.push_back(*aRes);
1268 // store just dumped entity to stack
1269 if (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theEntity)
1270 myEntitiesStack.push(
1271 LastDumpedEntity(theEntity, isUserDefinedName, aResultsWithNameOrColor));
1274 // remove entity from the list of not dumped items
1275 myNotDumpedEntities.erase(theEntity);
1279 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ResultPtr& theResult)
1281 // iterate in the structure of sub-results to the parent
1282 ResultPtr aCurRes = theResult;
1283 FeaturePtr aFeature = ModelAPI_Feature::feature(theResult);
1284 std::list<int> anIndices; // indexes of results in the parent result, starting from topmost
1285 while(aCurRes.get()) {
1286 ResultBodyPtr aParent = ModelAPI_Tools::bodyOwner(aCurRes);
1288 anIndices.push_front(ModelAPI_Tools::bodyIndex(aCurRes));
1289 } else { // index of the result in the feature
1290 std::list<ResultPtr>::const_iterator aRes = aFeature->results().cbegin();
1291 for(int anIndex = 0; aRes != aFeature->results().cend(); aRes++, anIndex++) {
1292 if (*aRes == aCurRes) {
1293 anIndices.push_front(anIndex);
1301 *myDumpStorage << name(aFeature);
1302 for (std::list<int>::iterator anI = anIndices.begin(); anI != anIndices.end(); anI++) {
1303 if (anI == anIndices.begin()) {
1305 *myDumpStorage << ".result()";
1308 *myDumpStorage << ".results()[" << *anI << "]";
1311 *myDumpStorage << ".subResult(" << *anI << ")";
1318 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::list<ResultPtr>& theResults)
1321 for (std::list<ResultPtr>::const_iterator anIt = theResults.begin();
1322 anIt != theResults.end(); ++anIt) {
1323 if (anIt != theResults.begin())
1331 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ObjectPtr& theObject)
1333 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
1334 if(aFeature.get()) {
1335 *myDumpStorage << name(aFeature);
1339 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1348 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const AttributePtr& theAttr)
1350 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttr->owner());
1352 std::string aWrapperPrefix, aWrapperSuffix;
1353 // Check the attribute belongs to copied (in multi-translation or multi-rotation) feature.
1354 // In this case we need to cast explicitly feature to appropriate type.
1355 AttributeBooleanPtr isCopy = anOwner->boolean("Copy");
1356 AttributeReferencePtr hasParent = anOwner->reference("ParentFeature");
1357 if ((isCopy.get() && isCopy->value()) || (hasParent && hasParent->value())) {
1358 aWrapperPrefix = featureWrapper(anOwner) + "(";
1359 aWrapperSuffix = ")";
1360 importModule("SketchAPI");
1363 *myDumpStorage << aWrapperPrefix << name(anOwner) << aWrapperSuffix
1364 << "." << attributeGetter(anOwner, theAttr->id()) << "()";
1368 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1369 const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
1371 if (theRefAttr->isObject())
1372 *this << theRefAttr->object();
1374 *this << theRefAttr->attr();
1378 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1379 const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList)
1381 *myDumpStorage << "[";
1382 std::list<std::pair<ObjectPtr, AttributePtr> > aList = theRefAttrList->list();
1383 bool isAdded = false;
1384 std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aList.begin();
1385 for (; anIt != aList.end(); ++anIt) {
1387 *myDumpStorage << ", ";
1391 *this << anIt->first;
1392 else if (anIt->second)
1393 * this << anIt->second;
1395 *myDumpStorage << "]";
1399 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1400 const std::shared_ptr<ModelAPI_AttributeReference>& theReference)
1402 *this << theReference->value();
1406 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1407 const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList)
1409 static const int aThreshold = 2;
1410 static bool aDumpAsIs = false;
1411 // if number of elements in the list if greater than a threshold,
1412 // dump it in a separate line with specific name
1413 if (aDumpAsIs || theRefList->size() <= aThreshold) {
1414 *myDumpStorage << "[";
1415 std::list<ObjectPtr> aList = theRefList->list();
1416 bool isAdded = false;
1417 std::list<ObjectPtr>::const_iterator anIt = aList.begin();
1418 for (; anIt != aList.end(); ++anIt) {
1420 *myDumpStorage << ", ";
1426 *myDumpStorage << "]";
1429 FeaturePtr anOwner = ModelAPI_Feature::feature(theRefList->owner());
1430 std::string aListName = name(anOwner) + "_objects";
1431 // reserve dumped buffer and store list "as is"
1432 myDumpStorage->reserveBuffer();
1434 *this << aListName << " = " << theRefList << "\n";
1436 // append reserved data to the end of the current buffer
1437 myDumpStorage->restoreReservedBuffer();
1438 *myDumpStorage << aListName;
1443 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1444 const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
1446 myDumpStorage->write(theAttrSelect);
1450 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1451 const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList)
1453 static const int aThreshold = 2;
1454 static bool aDumpAsIs = false;
1455 // if number of elements in the list if greater than a threshold,
1456 // dump it in a separate line with specific name
1457 if (aDumpAsIs || theAttrSelList->size() <= aThreshold) {
1458 *myDumpStorage << "[";
1460 GeomShapePtr aShape;
1461 std::string aShapeTypeStr;
1463 bool isAdded = false;
1465 for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) {
1466 AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex);
1467 aShape = anAttribute->value();
1469 ResultPtr aContext = anAttribute->context();
1471 aShape = aContext->shape();
1479 *myDumpStorage << ", ";
1483 *this << anAttribute;
1486 // check selection list is obtained by filters
1487 FiltersFeaturePtr aFilters = theAttrSelList->filters();
1489 if (theAttrSelList->size() > 0)
1490 *myDumpStorage << ", ";
1491 dumpFeature(aFilters, true);
1494 *myDumpStorage << "]";
1496 // obtain name of list (the feature may contain several selection lists)
1497 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttrSelList->owner());
1498 std::string aListName = name(anOwner) + "_objects";
1499 std::list<AttributePtr> aSelLists =
1500 anOwner->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
1501 if (aSelLists.size() > 1) {
1503 for (std::list<AttributePtr>::iterator aSIt = aSelLists.begin();
1504 aSIt != aSelLists.end(); ++aSIt, ++anIndex)
1505 if ((*aSIt).get() == theAttrSelList.get())
1507 std::ostringstream aSStream;
1508 aSStream << aListName << "_" << anIndex;
1509 aListName = aSStream.str();
1511 // reserve dumped buffer and store list "as is"
1512 myDumpStorage->reserveBuffer();
1514 *this << aListName << " = " << theAttrSelList << "\n";
1516 // append reserved data to the end of the current buffer
1517 myDumpStorage->restoreReservedBuffer();
1518 *myDumpStorage << aListName;
1523 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1524 const std::shared_ptr<ModelAPI_AttributeStringArray>& theArray)
1526 std::ostringstream aBuffer;
1528 for(int anIndex = 0; anIndex < theArray->size(); ++anIndex) {
1532 aBuffer << "\"" << theArray->value(anIndex) << "\"";
1536 myDumpStorage->write(aBuffer.str());
1541 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
1542 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
1544 *theDumper.myDumpStorage << theEndl;
1546 if (!theDumper.myEntitiesStack.empty()) {
1548 // all copies have been stored into stack, pop them all
1551 // Name for composite feature is dumped when all sub-entities are dumped
1552 // (see method ModelHighAPI_Dumper::processSubs).
1553 const ModelHighAPI_Dumper::LastDumpedEntity& aLastDumped = theDumper.myEntitiesStack.top();
1554 CompositeFeaturePtr aComposite =
1555 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aLastDumped.myEntity);
1557 theDumper.dumpEntitySetName();
1558 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aLastDumped.myEntity);
1560 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
1561 isCopy = aCopyAttr.get() && aCopyAttr->value();
1564 } while (isCopy && !theDumper.myEntitiesStack.empty());
1567 // store all not-dumped entities first
1568 std::set<EntityPtr> aNotDumped = theDumper.myNotDumpedEntities;
1569 theDumper.myDumpStorage->reserveBuffer();
1570 std::set<EntityPtr>::const_iterator anIt = aNotDumped.begin();
1571 for (; anIt != aNotDumped.end(); ++anIt) {
1572 // if the feature is composite, dump it with all subs
1573 CompositeFeaturePtr aCompFeat =
1574 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
1576 theDumper.process(aCompFeat, true);
1578 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
1579 theDumper.dumpFeature(aFeature, true);
1580 // dump the Projection feature which produces this "Copy" entity
1581 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
1582 if (aCopyAttr.get() && aCopyAttr->value())
1584 const std::set<AttributePtr>& aRefs = aFeature->data()->refsToMe();
1585 std::set<AttributePtr>::iterator aRefIt = aRefs.begin();
1586 for (; aRefIt != aRefs.end(); ++aRefIt)
1587 if ((*aRefIt)->id() == "ProjectedFeature")
1588 { // process projection only
1589 FeaturePtr anOwner = ModelAPI_Feature::feature((*aRefIt)->owner());
1590 if (anOwner && !theDumper.isDumped(EntityPtr(anOwner)))
1591 theDumper.dumpFeature(anOwner, true);
1597 // then store the reserved data
1598 theDumper.myDumpStorage->restoreReservedBuffer();
1599 theDumper.myDumpStorage->mergeBuffer();
1601 // now, store all postponed features
1602 theDumper.dumpPostponed();
1608 void ModelHighAPI_Dumper::exportVariables() const
1610 DocumentPtr aRoot = ModelAPI_Session::get()->moduleDocument();
1611 EntityNameMap::const_iterator aNameIter = myNames.cbegin();
1612 for(; aNameIter != myNames.end(); aNameIter++) {
1613 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aNameIter->first);
1614 if (aFeature.get() && aFeature->document() != aRoot) {
1615 FeaturePtr aPartFeat = ModelAPI_Tools::findPartFeature(aRoot, aFeature->document());
1616 if (aPartFeat.get()) {
1617 int aFeatureId = aFeature->data()->featureId();
1618 int aPartId = aPartFeat->data()->featureId();
1619 std::ostringstream anEntryStr;
1620 anEntryStr<<aPartId<<":"<<aFeatureId;
1621 std::string anEntry = anEntryStr.str();
1622 exportVariable(anEntry, aNameIter->second.myCurrentName);
1623 size_t aSize = aFeature->results().size();
1624 if (aSize > 1) { // additional entries for features with more than one result
1625 for(int a = 1; a < aSize; a++) {
1626 std::ostringstream aResEntryStr;
1627 aResEntryStr<<anEntry<<":"<<a;
1628 std::string aResEntry = aResEntryStr.str();
1629 exportVariable(aResEntry, aNameIter->second.myCurrentName);