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 //TODO: vsv 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 //TODO: vsv 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::iterator aFound = myNames.find(theEntity);
576 if (aFound != myNames.end()) {
577 // Set dumped flag for postponed constraints which are without names
578 if (!aFound->second.myIsDumped)
579 aFound->second.myIsDumped = theSetIsDumped;
580 return aFound->second.myCurrentName;
582 // entity is not found, store it
585 bool isDefaultName = false;
586 bool isSaveNotDumped = theSaveNotDumped;
587 std::ostringstream aDefaultName;
588 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
590 aName = ModelAPI_Tools::toString(aFeature->name());
591 aKind = aFeature->getKind();
593 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(theEntity);
595 aName = ModelAPI_Tools::toString(aFolder->data()->name());
596 aKind = ModelAPI_Folder::ID();
597 isSaveNotDumped = false;
601 ObjectPtr anObject = std::dynamic_pointer_cast<ModelAPI_Object>(theEntity);
603 DocumentPtr aDoc = anObject->document();
604 std::pair<int, int>& aNbFeatures = myFeatureCount[aDoc][aKind];
605 aNbFeatures.first += 1;
607 size_t anIndex = aName.find(aKind);
608 if (anIndex == 0 && aName[aKind.length()] == '_') { // name starts with "FeatureKind_"
609 std::string anIdStr = aName.substr(aKind.length() + 1);
610 int anId = toInt(anIdStr);
612 // Check number of already registered objects of such kind. Index of current object
613 // should be the same to identify feature's name as automatically generated.
614 if (aNbFeatures.first == anId && aNbFeatures.second < anId) {
615 // name is not user-defined
616 isDefaultName = true;
618 // check there are postponed features of this kind,
619 // dump their names, because the sequence of features may be changed
620 for (std::list<EntityPtr>::const_iterator aPpIt = myPostponed.begin();
621 aPpIt != myPostponed.end(); ++aPpIt) {
622 FeaturePtr aCurFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*aPpIt);
623 if (aCurFeature && aCurFeature->getKind() == aKind) {
624 myNames[*aPpIt].myIsDefault = false;
625 isDefaultName = false;
630 if (anId > aNbFeatures.second)
631 aNbFeatures.second = anId;
634 // obtain default name for the feature
635 if (theUseEntityName)
636 aDefaultName << aName;
639 NbFeaturesMap::const_iterator aFIt = myFeatureCount.begin();
640 for (; aFIt != myFeatureCount.end(); ++aFIt) {
641 std::map<std::string, std::pair<int, int> >::const_iterator aFound =
642 aFIt->second.find(aKind);
643 if (aFound != aFIt->second.end())
644 aFullIndex += aFound->second.first;
646 aDefaultName << aKind << "_" << aFullIndex;
650 myNames[theEntity] = EntityName(aDefaultName.str(), aName, isDefaultName);
652 myNotDumpedEntities.insert(theEntity);
654 // store names of results
656 saveResultNames(aFeature);
658 myNames[theEntity].myIsDumped = theSetIsDumped;
660 return myNames[theEntity].myCurrentName;
663 const std::string& ModelHighAPI_Dumper::parentName(const FeaturePtr& theEntity)
665 const std::set<AttributePtr>& aRefs = theEntity->data()->refsToMe();
666 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
667 for (; aRefIt != aRefs.end(); ++aRefIt) {
668 CompositeFeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(
669 ModelAPI_Feature::feature((*aRefIt)->owner()));
671 return name(anOwner);
674 static const std::string DUMMY;
678 void ModelHighAPI_Dumper::saveResultNames(const FeaturePtr& theFeature)
680 // Default name of the feature
681 bool isFeatureDefaultName = myNames[theFeature].myIsDefault;
683 // Save only names of results which is not correspond to default feature name
684 const std::list<ResultPtr>& aResults = theFeature->results();
685 std::list<ResultPtr> allRes;
686 ModelAPI_Tools::allResults(theFeature, allRes);
687 for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
688 std::pair<std::wstring, bool> aName = ModelAPI_Tools::getDefaultName(*aRes);
689 std::string aDefaultName = ModelAPI_Tools::toString(aName.first);
690 std::string aResName = ModelAPI_Tools::toString((*aRes)->data()->name());
691 bool isUserDefined = !(isFeatureDefaultName && aDefaultName == aResName);
693 EntityName(aResName, (isUserDefined ? aResName : std::string()), !isUserDefined);
697 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc,
698 const std::string& theFileName)
700 // dump top level document feature
701 static const std::string aDocName("partSet");
702 myNames[theDoc] = EntityName(aDocName, std::string(), true);
703 *this << aDocName << " = model.moduleDocument()" << std::endl;
705 // dump subfeatures and store result to file
706 bool isOk = process(theDoc) && myDumpStorage->exportTo(theFileName, myModules);
710 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc)
713 std::list<ObjectPtr> anObjects = theDoc->allObjects();
714 std::list<ObjectPtr>::const_iterator anObjIt = anObjects.begin();
715 // firstly, dump all parameters
716 for (; anObjIt != anObjects.end(); ++ anObjIt) {
717 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIt);
719 dumpParameter(aFeature);
721 // dump all other features
722 for (anObjIt = anObjects.begin(); anObjIt != anObjects.end(); ++anObjIt) {
723 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anObjIt);
725 // iteratively process composite features,
726 // if the composite feature is the last in the document, no need to dump "model.do()" action
727 std::list<ObjectPtr>::const_iterator aNext = anObjIt;
728 isOk = process(aCompFeat, false, ++aNext != anObjects.end()) && isOk;
730 else if (!isDumped(EntityPtr(*anObjIt))) {
732 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(*anObjIt);
736 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIt);
737 if (aFeature) // dump common feature
738 dumpFeature(aFeature);
742 // dump folders if any
747 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
748 bool isForce, bool isDumpModelDo)
750 // increase composite features stack
751 ++gCompositeStackDepth;
752 // dump composite itself
753 if (!isDumped(EntityPtr(theComposite)) || isForce)
754 dumpFeature(FeaturePtr(theComposite), isForce);
756 // sub-part is processed independently, because it provides separate document
757 if (theComposite->getKind() == PartSetPlugin_Part::ID()) {
758 // dump name of the part if it is different from default
759 if (!myEntitiesStack.empty())
762 // decrease composite features stack because we run into separate document
763 --gCompositeStackDepth;
765 ResultPartPtr aPartResult =
766 std::dynamic_pointer_cast<ModelAPI_ResultPart>(theComposite->lastResult());
769 DocumentPtr aSubDoc = aPartResult->partDoc();
772 // set name of document
773 const std::string& aPartName = myNames[theComposite].myCurrentName;
774 std::string aDocName = aPartName + "_doc";
775 myNames[aSubDoc] = EntityName(aDocName, std::string(), true);
777 // dump document in a separate line
778 *this << aDocName << " = " << aPartName << ".document()" << std::endl;
779 // dump features in the document
780 bool aRes = process(aSubDoc);
782 *this << "\nmodel.do()\n";
788 bool isOk = processSubs(theComposite);
789 // decrease composite features stack
790 --gCompositeStackDepth;
795 bool ModelHighAPI_Dumper::processSubs(
796 const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
800 // dump all sub-features;
801 bool isSubDumped = false;
802 int aNbSubs = theComposite->numberOfSubs();
803 for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) {
804 FeaturePtr aFeature = theComposite->subFeature(anIndex);
805 if (isDumped(EntityPtr(aFeature)))
809 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
810 if (aCompFeat) // iteratively process composite features
811 isOk = process(aCompFeat) && isOk;
813 dumpFeature(aFeature, true);
816 bool isDumpSetName = !myEntitiesStack.empty() &&
817 myEntitiesStack.top().myEntity == EntityPtr(theComposite);
818 bool isForceModelDo = isSubDumped && isDumpSetName &&
819 (myEntitiesStack.top().myUserName || !myEntitiesStack.top().myResults.empty());
820 // It is necessary for the sketch to create its result when complete (command "model.do()").
821 // This option is set by flat theDumpModelDo.
822 // However, nested sketches are rebuilt by parent feature, so, they do not need
823 // explicit call of "model.do()". This will be controlled by the depth of the stack.
824 if (isForceModelDo || (theDumpModelDo && gCompositeStackDepth <= 1))
825 *this << "model.do()" << std::endl;
827 // dump "setName" for composite feature
833 void ModelHighAPI_Dumper::postpone(const EntityPtr& theEntity)
836 name(theEntity, false);
837 myPostponed.push_back(theEntity);
840 void ModelHighAPI_Dumper::dumpPostponed(bool theDumpFolders)
842 if (myDumpPostponedInProgress)
845 myDumpPostponedInProgress = true;
846 // make a copy of postponed entities, because the list will be updated
847 // if some features are not able to be dumped
848 std::list<EntityPtr> aPostponedCopy = myPostponed;
851 // iterate over postponed entities and try to dump them
852 std::list<EntityPtr>::const_iterator anIt = aPostponedCopy.begin();
853 for (; anIt != aPostponedCopy.end(); ++anIt) {
854 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(*anIt);
859 myPostponed.push_back(*anIt);
862 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
864 dumpFeature(aFeature, true);
867 myDumpPostponedInProgress = false;
870 void ModelHighAPI_Dumper::dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
871 const FeaturePtr& theSubFeature)
873 name(theSubFeature, false);
874 myNames[theSubFeature] =
875 EntityName(theSubFeatureGet, ModelAPI_Tools::toString(theSubFeature->name()), false);
877 // store results if they have user-defined names or colors
878 std::list<ResultPtr> aResultsWithNameOrColor;
879 const std::list<ResultPtr>& aResults = theSubFeature->results();
880 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
881 for (; aResIt != aResults.end(); ++aResIt) {
882 std::string aResName = ModelAPI_Tools::toString((*aResIt)->data()->name());
883 myNames[*aResIt] = EntityName(aResName, aResName, false);
884 aResultsWithNameOrColor.push_back(*aResIt);
887 // store just dumped entity to stack
888 myEntitiesStack.push(LastDumpedEntity(theSubFeature, true, aResultsWithNameOrColor));
893 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName)
895 myModules.insert(theModuleName);
898 void ModelHighAPI_Dumper::dumpEntitySetName()
900 const LastDumpedEntity& aLastDumped = myEntitiesStack.top();
901 bool isBufferEmpty = myDumpStorage->isBufferEmpty();
903 // dump "setName" for the entity
904 if (aLastDumped.myUserName) {
905 EntityName& anEntityNames = myNames[aLastDumped.myEntity];
906 if (!anEntityNames.myIsDefault)
907 *myDumpStorage << anEntityNames.myCurrentName << ".setName(\""
908 << anEntityNames.myUserName << "\")\n";
909 // don't dump "setName" for the entity twice
910 anEntityNames.myUserName.clear();
911 anEntityNames.myIsDefault = true;
913 // dump "setName" for results
914 std::list<ResultPtr>::const_iterator aResIt = aLastDumped.myResults.begin();
915 std::list<ResultPtr>::const_iterator aResEnd = aLastDumped.myResults.end();
916 for (; aResIt != aResEnd; ++aResIt) {
918 EntityName& anEntityNames = myNames[*aResIt];
919 if (!anEntityNames.myIsDefault) {
921 *myDumpStorage << ".setName(\"" << anEntityNames.myUserName << "\")\n";
922 // don't dump "setName" for the entity twice
923 anEntityNames.myUserName.clear();
924 anEntityNames.myIsDefault = true;
927 if (!isDefaultColor(*aResIt)) {
928 AttributeIntArrayPtr aColor = (*aResIt)->data()->intArray(ModelAPI_Result::COLOR_ID());
929 if (aColor && aColor->isInitialized()) {
931 *myDumpStorage << ".setColor(" << aColor->value(0) << ", " << aColor->value(1)
932 << ", " << aColor->value(2) << ")\n";
935 // set result deflection
936 if (!isDefaultDeflection(*aResIt)) {
937 AttributeDoublePtr aDeflectionAttr =
938 (*aResIt)->data()->real(ModelAPI_Result::DEFLECTION_ID());
939 if(aDeflectionAttr.get() && aDeflectionAttr->isInitialized()) {
941 *myDumpStorage << ".setDeflection(" << aDeflectionAttr->value() << ")\n";
944 // set result transparency
945 if (!isDefaultTransparency(*aResIt)) {
946 AttributeDoublePtr aTransparencyAttr =
947 (*aResIt)->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
948 if(aTransparencyAttr.get() && aTransparencyAttr->isInitialized()) {
950 *myDumpStorage << ".setTransparency(" << aTransparencyAttr->value() << ")\n";
955 myNames[aLastDumped.myEntity].myIsDumped = true;
956 myEntitiesStack.pop();
958 // clean buffer if it was clear before
960 myDumpStorage->mergeBuffer();
963 bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const
965 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
966 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
967 return (aFound != myNames.end() && aFound->second.myIsDumped) ||
968 myFeaturesToSkip.find(aFeature) != myFeaturesToSkip.end();
971 bool ModelHighAPI_Dumper::isDumped(const AttributeRefAttrPtr& theRefAttr) const
974 if (theRefAttr->isObject())
975 aFeature = ModelAPI_Feature::feature(theRefAttr->object());
977 aFeature = ModelAPI_Feature::feature(theRefAttr->attr()->owner());
978 return aFeature && isDumped(EntityPtr(aFeature));
981 bool ModelHighAPI_Dumper::isDumped(const AttributeRefListPtr& theRefList) const
983 std::list<ObjectPtr> aRefs = theRefList->list();
984 std::list<ObjectPtr>::iterator anIt = aRefs.begin();
985 for (; anIt != aRefs.end(); ++anIt) {
986 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
987 if (aFeature && !isDumped(EntityPtr(aFeature)))
993 static bool isSketchSub(const FeaturePtr& theFeature)
995 static const std::string SKETCH("Sketch");
996 CompositeFeaturePtr anOwner = ModelAPI_Tools::compositeOwner(theFeature);
997 return anOwner && anOwner->getKind() == SKETCH;
1000 bool ModelHighAPI_Dumper::isDefaultColor(const ResultPtr& theResult) const
1002 AttributeIntArrayPtr aColor = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
1003 if (!aColor || !aColor->isInitialized())
1006 // check the result belongs to sketch entity, do not dump color in this way
1007 ResultConstructionPtr aResConstr =
1008 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult);
1010 FeaturePtr aFeature = ModelAPI_Feature::feature(theResult->data()->owner());
1011 if (isSketchSub(aFeature))
1015 std::string aSection, aName, aDefault;
1016 theResult->colorConfigInfo(aSection, aName, aDefault);
1018 // dump current color
1019 std::ostringstream aColorInfo;
1020 aColorInfo << aColor->value(0) << "," << aColor->value(1) << "," << aColor->value(2);
1022 return aDefault == aColorInfo.str();
1025 bool ModelHighAPI_Dumper::isDefaultDeflection(const ResultPtr& theResult) const
1027 AttributeDoublePtr aDeflectionAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
1028 if(!aDeflectionAttr || !aDeflectionAttr->isInitialized()) {
1032 double aCurrent = aDeflectionAttr->value();
1033 double aDefault = -1;
1035 bool isConstruction = false;
1036 std::string aResultGroup = theResult->groupName();
1037 if (aResultGroup == ModelAPI_ResultConstruction::group())
1038 isConstruction = true;
1039 else if (aResultGroup == ModelAPI_ResultBody::group()) {
1040 GeomShapePtr aGeomShape = theResult->shape();
1041 if (aGeomShape.get()) {
1042 // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
1043 // correction of deviation for them should not influence to the application performance
1044 GeomAPI_ShapeExplorer anExp(aGeomShape, GeomAPI_Shape::FACE);
1045 isConstruction = !anExp.more();
1049 aDefault = Config_PropManager::real("Visualization", "construction_deflection");
1051 aDefault = Config_PropManager::real("Visualization", "body_deflection");
1053 return fabs(aCurrent - aDefault) < 1.e-12;
1056 bool ModelHighAPI_Dumper::isDefaultTransparency(const ResultPtr& theResult) const
1058 AttributeDoublePtr anAttribute = theResult->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
1059 if(!anAttribute || !anAttribute->isInitialized()) {
1062 return fabs(anAttribute->value()) < 1.e-12;
1065 bool ModelHighAPI_Dumper::dumpCommentBeforeFeature(const FeaturePtr& theFeature) const
1067 // currently, the comment should not be dumped only before the filters
1068 FiltersFeaturePtr aFilters = std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(theFeature);
1071 // all other features should be commented before the dump
1075 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
1077 *myDumpStorage << theChar;
1081 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
1083 *myDumpStorage << theString;
1087 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
1089 *myDumpStorage << theString;
1093 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::wstring& theString)
1095 *myDumpStorage << ModelAPI_Tools::toString(theString);
1099 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const bool theValue)
1101 *myDumpStorage << (theValue ? "True" : "False");
1105 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
1107 *myDumpStorage << theValue;
1111 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
1113 *myDumpStorage << theValue;
1117 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
1119 importModule("GeomAPI");
1120 *myDumpStorage << "GeomAPI_Pnt(" << thePoint->x() << ", "
1121 << thePoint->y() << ", " << thePoint->z() << ")";
1125 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
1127 importModule("GeomAPI");
1128 *myDumpStorage << "GeomAPI_Dir(" << theDir->x() << ", "
1129 << theDir->y() << ", " << theDir->z() << ")";
1133 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1134 const std::shared_ptr<GeomDataAPI_Dir>& theDir)
1136 *myDumpStorage << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
1140 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1141 const std::shared_ptr<GeomDataAPI_Point>& thePoint)
1143 static const int aSize = 3;
1144 double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
1145 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()};
1146 myDumpStorage->dumpArray(aSize, aValues, aTexts);
1150 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1151 const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
1153 static const int aSize = 2;
1154 double aValues[aSize] = {thePoint->x(), thePoint->y()};
1155 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()};
1156 myDumpStorage->dumpArray(aSize, aValues, aTexts);
1160 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1161 const std::shared_ptr<GeomDataAPI_Point2DArray>& thePointArray)
1163 static const int aThreshold = 4;
1164 static bool aDumpAsIs = false;
1165 static std::string aSeparator = "";
1166 // if number of elements in the list if greater than a threshold,
1167 // dump it in a separate line with specific name
1168 int aSize = thePointArray->size();
1169 if (aDumpAsIs || aSize <= aThreshold) {
1170 *myDumpStorage << "[";
1171 GeomPnt2dPtr aPoint = thePointArray->pnt(0);
1172 *myDumpStorage << "(" << aPoint->x() << ", " << aPoint->y() << ")";
1173 for (int anIndex = 1; anIndex < aSize; ++anIndex) {
1174 aPoint = thePointArray->pnt(anIndex);
1175 *myDumpStorage << "," << aSeparator << " (" << aPoint->x() << ", " << aPoint->y() << ")";
1177 *myDumpStorage << aSeparator << "]";
1181 FeaturePtr anOwner = ModelAPI_Feature::feature(thePointArray->owner());
1182 std::string aListName = name(anOwner) + "_" + thePointArray->id();
1183 // reserve dumped buffer and store list "as is"
1184 myDumpStorage->reserveBuffer();
1186 aSeparator = std::string("\n") + std::string(aListName.size() + 3, ' ');
1187 *this << aListName << " = " << thePointArray << "\n";
1190 // append reserved data to the end of the current buffer
1191 myDumpStorage->restoreReservedBuffer();
1192 *myDumpStorage << aListName;
1197 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1198 const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
1200 *myDumpStorage << (theAttrBool->value() ? "True" : "False");
1204 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1205 const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
1207 std::string aText = theAttrInt->text();
1209 *myDumpStorage << theAttrInt->value();
1211 *myDumpStorage << "\"" << aText << "\"";
1215 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1216 const std::shared_ptr<ModelAPI_AttributeIntArray>& theArray)
1218 *myDumpStorage << "[";
1219 int aSize = theArray->size();
1221 *myDumpStorage << theArray->value(0);
1222 for (int anIndex = 1; anIndex < aSize; ++anIndex)
1223 *myDumpStorage << ", " << theArray->value(anIndex);
1225 *myDumpStorage << "]";
1229 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1230 const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
1232 std::string aText = theAttrReal->text();
1234 *myDumpStorage << theAttrReal->value();
1236 *myDumpStorage << "\"" << aText << "\"";
1240 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1241 const std::shared_ptr<ModelAPI_AttributeDoubleArray>& theArray)
1243 *myDumpStorage << "[";
1244 int aSize = theArray->size();
1246 *myDumpStorage << theArray->value(0);
1247 for (int anIndex = 1; anIndex < aSize; ++anIndex)
1248 *myDumpStorage << ", " << theArray->value(anIndex);
1250 *myDumpStorage << "]";
1254 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1255 const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
1257 *myDumpStorage << "\"" << theAttrStr->value() << "\"";
1261 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FolderPtr& theFolder)
1263 *myDumpStorage << name(theFolder);
1265 // add dumped folder to a stack
1266 if (!myNames[theFolder].myIsDumped &&
1267 (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theFolder))
1268 myEntitiesStack.push(LastDumpedEntity(theFolder, !myNames[theFolder].myIsDefault));
1273 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity)
1275 *myDumpStorage << name(theEntity);
1277 if (!myNames[theEntity].myIsDumped) {
1278 bool isUserDefinedName = !myNames[theEntity].myIsDefault;
1279 // store results if they have user-defined names or colors
1280 std::list<ResultPtr> aResultsWithNameOrColor;
1281 std::list<ResultPtr> allRes;
1282 ModelAPI_Tools::allResults(theEntity, allRes);
1283 for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
1284 if(!myNames[*aRes].myIsDefault || !isDefaultColor(*aRes) ||
1285 !isDefaultDeflection(*aRes) || !isDefaultTransparency(*aRes))
1286 aResultsWithNameOrColor.push_back(*aRes);
1288 // store just dumped entity to stack
1289 if (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theEntity)
1290 myEntitiesStack.push(
1291 LastDumpedEntity(theEntity, isUserDefinedName, aResultsWithNameOrColor));
1294 // remove entity from the list of not dumped items
1295 myNotDumpedEntities.erase(theEntity);
1299 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ResultPtr& theResult)
1301 // iterate in the structure of sub-results to the parent
1302 ResultPtr aCurRes = theResult;
1303 FeaturePtr aFeature = ModelAPI_Feature::feature(theResult);
1304 std::list<int> anIndices; // indexes of results in the parent result, starting from topmost
1305 while(aCurRes.get()) {
1306 ResultBodyPtr aParent = ModelAPI_Tools::bodyOwner(aCurRes);
1308 anIndices.push_front(ModelAPI_Tools::bodyIndex(aCurRes));
1309 } else { // index of the result in the feature
1310 std::list<ResultPtr>::const_iterator aRes = aFeature->results().cbegin();
1311 for(int anIndex = 0; aRes != aFeature->results().cend(); aRes++, anIndex++) {
1312 if (*aRes == aCurRes) {
1313 anIndices.push_front(anIndex);
1321 *myDumpStorage << name(aFeature);
1322 for (std::list<int>::iterator anI = anIndices.begin(); anI != anIndices.end(); anI++) {
1323 if (anI == anIndices.begin()) {
1325 *myDumpStorage << ".result()";
1328 *myDumpStorage << ".results()[" << *anI << "]";
1331 *myDumpStorage << ".subResult(" << *anI << ")";
1338 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::list<ResultPtr>& theResults)
1341 for (std::list<ResultPtr>::const_iterator anIt = theResults.begin();
1342 anIt != theResults.end(); ++anIt) {
1343 if (anIt != theResults.begin())
1351 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ObjectPtr& theObject)
1353 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
1354 if(aFeature.get()) {
1355 *myDumpStorage << name(aFeature);
1359 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1368 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const AttributePtr& theAttr)
1370 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttr->owner());
1372 std::string aWrapperPrefix, aWrapperSuffix;
1373 // Check the attribute belongs to copied (in multi-translation or multi-rotation) feature.
1374 // In this case we need to cast explicitly feature to appropriate type.
1375 AttributeBooleanPtr isCopy = anOwner->boolean("Copy");
1376 AttributeReferencePtr hasParent = anOwner->reference("ParentFeature");
1377 if ((isCopy.get() && isCopy->value()) || (hasParent && hasParent->value())) {
1378 aWrapperPrefix = featureWrapper(anOwner) + "(";
1379 aWrapperSuffix = ")";
1380 importModule("SketchAPI");
1383 *myDumpStorage << aWrapperPrefix << name(anOwner) << aWrapperSuffix
1384 << "." << attributeGetter(anOwner, theAttr->id()) << "()";
1388 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1389 const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
1391 if (theRefAttr->isObject())
1392 *this << theRefAttr->object();
1394 *this << theRefAttr->attr();
1398 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1399 const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList)
1401 *myDumpStorage << "[";
1402 std::list<std::pair<ObjectPtr, AttributePtr> > aList = theRefAttrList->list();
1403 bool isAdded = false;
1404 std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aList.begin();
1405 for (; anIt != aList.end(); ++anIt) {
1407 *myDumpStorage << ", ";
1411 *this << anIt->first;
1412 else if (anIt->second)
1413 * this << anIt->second;
1415 *myDumpStorage << "]";
1419 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1420 const std::shared_ptr<ModelAPI_AttributeReference>& theReference)
1422 *this << theReference->value();
1426 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1427 const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList)
1429 static const int aThreshold = 2;
1430 static bool aDumpAsIs = false;
1431 // if number of elements in the list if greater than a threshold,
1432 // dump it in a separate line with specific name
1433 if (aDumpAsIs || theRefList->size() <= aThreshold) {
1434 *myDumpStorage << "[";
1435 std::list<ObjectPtr> aList = theRefList->list();
1436 bool isAdded = false;
1437 std::list<ObjectPtr>::const_iterator anIt = aList.begin();
1438 for (; anIt != aList.end(); ++anIt) {
1440 *myDumpStorage << ", ";
1446 *myDumpStorage << "]";
1449 FeaturePtr anOwner = ModelAPI_Feature::feature(theRefList->owner());
1450 std::string aListName = name(anOwner) + "_objects";
1451 // reserve dumped buffer and store list "as is"
1452 myDumpStorage->reserveBuffer();
1454 *this << aListName << " = " << theRefList << "\n";
1456 // append reserved data to the end of the current buffer
1457 myDumpStorage->restoreReservedBuffer();
1458 *myDumpStorage << aListName;
1463 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1464 const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
1466 myDumpStorage->write(theAttrSelect);
1470 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1471 const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList)
1473 static const int aThreshold = 2;
1474 static int aNbSpaces = 0;
1475 // if number of elements in the list if greater than a threshold,
1476 // dump it in a separate line with specific name
1477 if (aNbSpaces > 0 || theAttrSelList->size() <= aThreshold) {
1478 *myDumpStorage << "[";
1480 GeomShapePtr aShape;
1481 std::string aShapeTypeStr;
1483 bool isAdded = false;
1485 for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) {
1486 AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex);
1487 aShape = anAttribute->value();
1489 ResultPtr aContext = anAttribute->context();
1491 aShape = aContext->shape();
1499 *myDumpStorage << ", ";
1500 // print each attribute on separate line with the appropriate shift
1501 if (aNbSpaces > 0) {
1502 std::string aSpaces(aNbSpaces + 1, ' ');
1503 *myDumpStorage << "\n" << aSpaces;
1508 *this << anAttribute;
1511 // check selection list is obtained by filters
1512 FiltersFeaturePtr aFilters = theAttrSelList->filters();
1514 if (theAttrSelList->size() > 0)
1515 *myDumpStorage << ", ";
1516 dumpFeature(aFilters, true);
1519 *myDumpStorage << "]";
1521 // obtain name of list (the feature may contain several selection lists)
1522 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttrSelList->owner());
1523 std::string aListName = name(anOwner) + "_objects";
1524 std::list<AttributePtr> aSelLists =
1525 anOwner->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
1526 if (aSelLists.size() > 1) {
1528 for (std::list<AttributePtr>::iterator aSIt = aSelLists.begin();
1529 aSIt != aSelLists.end(); ++aSIt, ++anIndex)
1530 if ((*aSIt).get() == theAttrSelList.get())
1532 std::ostringstream aSStream;
1533 aSStream << aListName << "_" << anIndex;
1534 aListName = aSStream.str();
1536 // reserve dumped buffer and store list "as is"
1537 myDumpStorage->reserveBuffer();
1538 aNbSpaces = (int)aListName.size() + 3;
1539 *this << aListName << " = " << theAttrSelList << "\n";
1541 // append reserved data to the end of the current buffer
1542 myDumpStorage->restoreReservedBuffer();
1543 *myDumpStorage << aListName;
1548 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1549 const std::shared_ptr<ModelAPI_AttributeStringArray>& theArray)
1551 std::ostringstream aBuffer;
1553 for(int anIndex = 0; anIndex < theArray->size(); ++anIndex) {
1557 aBuffer << "\"" << theArray->value(anIndex) << "\"";
1561 myDumpStorage->write(aBuffer.str());
1565 void ModelHighAPI_Dumper::newline()
1571 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
1572 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
1574 *theDumper.myDumpStorage << theEndl;
1576 if (!theDumper.myEntitiesStack.empty()) {
1578 // all copies have been stored into stack, pop them all
1581 // Name for composite feature is dumped when all sub-entities are dumped
1582 // (see method ModelHighAPI_Dumper::processSubs).
1583 const ModelHighAPI_Dumper::LastDumpedEntity& aLastDumped = theDumper.myEntitiesStack.top();
1584 CompositeFeaturePtr aComposite =
1585 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aLastDumped.myEntity);
1587 theDumper.dumpEntitySetName();
1588 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aLastDumped.myEntity);
1590 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
1591 isCopy = aCopyAttr.get() && aCopyAttr->value();
1594 } while (isCopy && !theDumper.myEntitiesStack.empty());
1597 // store all not-dumped entities first
1598 std::set<EntityPtr> aNotDumped = theDumper.myNotDumpedEntities;
1599 theDumper.clearNotDumped();
1600 theDumper.myDumpStorage->reserveBuffer();
1601 std::set<EntityPtr>::const_iterator anIt = aNotDumped.begin();
1602 for (; anIt != aNotDumped.end(); ++anIt) {
1603 // if the feature is composite, dump it with all subs
1604 CompositeFeaturePtr aCompFeat =
1605 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
1607 theDumper.process(aCompFeat, true);
1609 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
1610 theDumper.dumpFeature(aFeature, true);
1611 // dump the Projection feature which produces this "Copy" entity
1612 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
1613 if (aCopyAttr.get() && aCopyAttr->value())
1615 const std::set<AttributePtr>& aRefs = aFeature->data()->refsToMe();
1616 std::set<AttributePtr>::iterator aRefIt = aRefs.begin();
1617 for (; aRefIt != aRefs.end(); ++aRefIt)
1618 if ((*aRefIt)->id() == "ProjectedFeature")
1619 { // process projection only
1620 FeaturePtr anOwner = ModelAPI_Feature::feature((*aRefIt)->owner());
1621 if (anOwner && !theDumper.isDumped(EntityPtr(anOwner)))
1622 theDumper.dumpFeature(anOwner, true);
1628 // then store the reserved data
1629 theDumper.myDumpStorage->restoreReservedBuffer();
1630 theDumper.myDumpStorage->mergeBuffer();
1632 // now, store all postponed features
1633 theDumper.dumpPostponed();
1639 void ModelHighAPI_Dumper::exportVariables() const
1641 DocumentPtr aRoot = ModelAPI_Session::get()->moduleDocument();
1642 EntityNameMap::const_iterator aNameIter = myNames.cbegin();
1643 for(; aNameIter != myNames.end(); aNameIter++) {
1644 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aNameIter->first);
1645 if (aFeature.get() && aFeature->document() != aRoot) {
1646 FeaturePtr aPartFeat = ModelAPI_Tools::findPartFeature(aRoot, aFeature->document());
1647 if (aPartFeat.get()) {
1648 int aFeatureId = aFeature->data()->featureId();
1649 int aPartId = aPartFeat->data()->featureId();
1650 std::ostringstream anEntryStr;
1651 anEntryStr<<aPartId<<":"<<aFeatureId;
1652 std::string anEntry = anEntryStr.str();
1653 exportVariable(anEntry, aNameIter->second.myCurrentName);
1654 size_t aSize = aFeature->results().size();
1655 if (aSize > 1) { // additional entries for features with more than one result
1656 for(int a = 1; a < aSize; a++) {
1657 std::ostringstream aResEntryStr;
1658 aResEntryStr<<anEntry<<":"<<a;
1659 std::string aResEntry = aResEntryStr.str();
1660 exportVariable(aResEntry, aNameIter->second.myCurrentName);