+// =========== Implementation of storage of dumped data ===========
+static const int THE_DUMP_PRECISION = 16;
+
+class ModelHighAPI_Dumper::DumpStorageBuffer : public ModelHighAPI_Dumper::DumpStorage
+{
+public:
+ void addStorage(const ModelHighAPI_Dumper::DumpStoragePtr& theStorage)
+ { myStorageArray.push_back(theStorage); }
+
+ void clear() { myStorageArray.clear(); }
+
+ bool isBufferEmpty()
+ {
+ return myStorageArray.empty() || myStorageArray.front()->buffer().str().empty();
+ }
+
+ void mergeBuffer()
+ {
+ std::list<ModelHighAPI_Dumper::DumpStoragePtr>::iterator anIt = myStorageArray.begin();
+ for (; anIt != myStorageArray.end(); ++anIt) {
+ // avoid multiple empty lines
+ std::string aBuf = (*anIt)->buffer().str();
+ size_t anInd = std::string::npos;
+ while ((anInd = aBuf.find("\n\n\n")) != std::string::npos)
+ aBuf.erase(anInd, 1);
+
+ (*anIt)->fullDump() << aBuf;
+ (*anIt)->buffer().str("");
+ }
+ }
+
+ void write(const std::string& theValue)
+ {
+ if (myStorageArray.empty())
+ addStorage(DumpStoragePtr(new DumpStorage));
+
+ std::list<ModelHighAPI_Dumper::DumpStoragePtr>::iterator anIt = myStorageArray.begin();
+ for (; anIt != myStorageArray.end(); ++anIt)
+ (*anIt)->buffer() << theValue;
+ }
+
+ DumpStorageBuffer& operator<<(const char theChar)
+ {
+ std::ostringstream out;
+ out << theChar;
+ write(out.str());
+ return *this;
+ }
+
+ DumpStorageBuffer& operator<<(const char* theString)
+ {
+ write(theString);
+ return *this;
+ }
+
+ DumpStorageBuffer& operator<<(const std::string& theString)
+ {
+ write(theString);
+ return *this;
+ }
+
+ DumpStorageBuffer& operator<<(const bool theValue)
+ {
+ std::ostringstream out;
+ out << theValue;
+ write(out.str());
+ return *this;
+ }
+
+ DumpStorageBuffer& operator<<(const int theValue)
+ {
+ std::ostringstream out;
+ out << theValue;
+ write(out.str());
+ return *this;
+ }
+
+ DumpStorageBuffer& operator<<(const double theValue)
+ {
+ std::ostringstream out;
+ out << std::setprecision(THE_DUMP_PRECISION) << theValue;
+ write(out.str());
+ return *this;
+ }
+ /// Dump std::endl
+ friend
+ DumpStorageBuffer& operator<<(DumpStorageBuffer& theBuffer,
+ std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
+ {
+ theBuffer.write("\n");
+ return theBuffer;
+ }
+
+ void dumpArray(int theSize, double* theValues, std::string* theTexts)
+ {
+ std::ostringstream anOutput;
+ anOutput << std::setprecision(THE_DUMP_PRECISION);
+ for (int i = 0; i < theSize; ++i) {
+ if (i > 0)
+ anOutput << ", ";
+ if (theTexts[i].empty())
+ anOutput << theValues[i];
+ else
+ anOutput << "\"" << theTexts[i] << "\"";
+ }
+ write(anOutput.str());
+ }
+
+ virtual void write(const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
+ {
+ if (myStorageArray.empty())
+ addStorage(DumpStoragePtr(new DumpStorage));
+
+ std::list<ModelHighAPI_Dumper::DumpStoragePtr>::iterator anIt = myStorageArray.begin();
+ for (; anIt != myStorageArray.end(); ++anIt)
+ (*anIt)->write(theAttrSelect);
+ }
+
+ virtual void reserveBuffer()
+ {
+ std::list<ModelHighAPI_Dumper::DumpStoragePtr>::iterator anIt = myStorageArray.begin();
+ for (; anIt != myStorageArray.end(); ++anIt)
+ (*anIt)->reserveBuffer();
+ }
+
+ virtual void restoreReservedBuffer()
+ {
+ std::list<ModelHighAPI_Dumper::DumpStoragePtr>::iterator anIt = myStorageArray.begin();
+ for (; anIt != myStorageArray.end(); ++anIt)
+ (*anIt)->restoreReservedBuffer();
+ }
+
+ virtual bool exportTo(const std::string& theFilename, const ModulesSet& theUsedModules)
+ {
+ static const std::string THE_EXT = ".py";
+ std::string aFilenameBase = theFilename;
+ if (aFilenameBase.rfind(THE_EXT) == aFilenameBase.size() - THE_EXT.size())
+ aFilenameBase = aFilenameBase.substr(0, aFilenameBase.size() - THE_EXT.size());
+
+ bool isOk = true;
+ std::list<ModelHighAPI_Dumper::DumpStoragePtr>::iterator anIt = myStorageArray.begin();
+ for (; anIt != myStorageArray.end(); ++anIt) {
+ std::string aFilename = aFilenameBase + (*anIt)->myFilenameSuffix + THE_EXT;
+ isOk = (*anIt)->exportTo(aFilename, theUsedModules) && isOk;
+ }
+ return isOk;
+ }
+
+private:
+ std::list<ModelHighAPI_Dumper::DumpStoragePtr> myStorageArray;
+};
+
+
+ModelHighAPI_Dumper::DumpStorage::DumpStorage(const DumpStorage& theOther)
+ : myFilenameSuffix(theOther.myFilenameSuffix),
+ myDumpBufferHideout(theOther.myDumpBufferHideout)
+{
+ myFullDump.str(theOther.myFullDump.str());
+ myDumpBuffer.str(theOther.myDumpBuffer.str());
+}
+
+const ModelHighAPI_Dumper::DumpStorage&
+ModelHighAPI_Dumper::DumpStorage::operator=(const ModelHighAPI_Dumper::DumpStorage& theOther)
+{
+ myFilenameSuffix = theOther.myFilenameSuffix;
+ myFullDump.str(theOther.myFullDump.str());
+ myDumpBuffer.str(theOther.myDumpBuffer.str());
+ myDumpBufferHideout = theOther.myDumpBufferHideout;
+ return *this;
+}
+
+void ModelHighAPI_Dumper::DumpStorage::reserveBuffer()
+{
+ myDumpBufferHideout.push(myDumpBuffer.str());
+ myDumpBuffer.str("");
+}
+
+void ModelHighAPI_Dumper::DumpStorage::restoreReservedBuffer()
+{
+ myDumpBuffer << myDumpBufferHideout.top();
+ myDumpBufferHideout.pop();
+}
+
+bool ModelHighAPI_Dumper::DumpStorage::exportTo(const std::string& theFilename,
+ const ModulesSet& theUsedModules)
+{
+ std::ofstream aFile;
+ OSD_OpenStream(aFile, theFilename.c_str(), std::ofstream::out);
+ if (!aFile.is_open())
+ return false;
+
+ // standard header imported modules
+ for (ModulesSet::const_iterator aModIt = theUsedModules.begin();
+ aModIt != theUsedModules.end(); ++aModIt) {
+ aFile << "from " << *aModIt << " import *" << std::endl;
+ }
+ if (!theUsedModules.empty())
+ aFile << std::endl;
+
+ aFile << "from salome.shaper import model" << std::endl << std::endl;
+ aFile << "model.begin()" << std::endl;
+
+ // dump collected data
+ aFile << myFullDump.str();
+ aFile << myDumpBuffer.str();
+
+ // standard footer
+ aFile << "model.end()" << std::endl;
+ aFile.close();
+
+ return true;
+}
+
+static void getShapeAndContext(const AttributeSelectionPtr& theAttrSelect,
+ GeomShapePtr& theShape, ResultPtr& theContext)
+{
+ if (theAttrSelect->isInitialized()) {
+ theShape = theAttrSelect->value();
+ theContext = theAttrSelect->context();
+ if (!theShape.get())
+ theShape = theContext->shape();
+
+ if (theAttrSelect->isGeometricalSelection() &&
+ theShape.get() && theShape->shapeType() == GeomAPI_Shape::COMPOUND &&
+ theContext.get() && !theShape->isEqual(theContext->shape()) &&
+ theContext->groupName() != ModelAPI_ResultPart::group() &&
+ theContext->groupName() != ModelAPI_ResultGroup::group()) {
+ GeomAPI_ShapeIterator anIt(theShape);
+ theShape = anIt.current();
+ }
+ }
+}
+
+void ModelHighAPI_Dumper::DumpStorage::write(const AttributeSelectionPtr& theAttrSelect)
+{
+ myDumpBuffer << "model.selection(";
+
+ GeomShapePtr aShape;
+ ResultPtr aContext;
+ getShapeAndContext(theAttrSelect, aShape, aContext);
+
+ if (aShape.get()) {
+ myDumpBuffer << "\"" << aShape->shapeTypeStr() << "\", \""
+ << theAttrSelect->namingName() << "\"";
+ }
+
+ myDumpBuffer << ")";
+}
+
+static int possibleSelectionsByPoint(const GeomPointPtr& thePoint,
+ const ResultPtr& theResult,
+ const GeomShapePtr& theShape,
+ const FeaturePtr& theStartFeature,
+ const FeaturePtr& theEndFeature)
+{
+ DocumentPtr aDoc1 = theStartFeature->document();
+ DocumentPtr aDoc2 = theEndFeature->document();
+
+ std::list<FeaturePtr> aFeatures = aDoc1->allFeatures();
+ if (aDoc1 != aDoc2) {
+ // Find the position of the part, where its features should be inserted.
+ // It will avoid checking of appropriate elements in partSet after the current part.
+ std::list<FeaturePtr>::iterator aFIt = aFeatures.begin();
+ for (; aFIt != aFeatures.end(); ++aFIt) {
+ ResultPartPtr aPartRes =
+ std::dynamic_pointer_cast<ModelAPI_ResultPart>((*aFIt)->lastResult());
+ if (aPartRes && aPartRes->partDoc() == aDoc2)
+ break;
+ }
+
+ std::list<FeaturePtr> anAdditionalFeatures = aDoc2->allFeatures();
+ aFeatures.insert(aFIt, anAdditionalFeatures.begin(), anAdditionalFeatures.end());
+ }
+
+ CompositeFeaturePtr aLastCompositeFeature;
+
+ std::list<FeaturePtr>::const_iterator aFIt = aFeatures.begin();
+ while (aFIt != aFeatures.end() && *aFIt != theStartFeature) {
+ CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFIt);
+ if (aCompFeat)
+ aLastCompositeFeature = aCompFeat;
+ ++aFIt;
+ }
+
+ // collect the list of composite features, containing the last feature;
+ // these features should be excluded from searching,
+ // because the feature cannot select sub-shapes from its parent
+ std::set<FeaturePtr> aEndFeatureParents = ModelAPI_Tools::getParents(theEndFeature);
+
+ int aNbPossibleSelections = 0;
+ for (; aFIt != aFeatures.end() && *aFIt != theEndFeature; ++aFIt) {
+ bool isSkipFeature = false;
+ if (aLastCompositeFeature && aLastCompositeFeature->isSub(*aFIt))
+ isSkipFeature = true;
+ CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFIt);
+ if (aCompFeat) {
+ ResultPartPtr aPartRes =
+ std::dynamic_pointer_cast<ModelAPI_ResultPart>(aCompFeat->firstResult());
+ if (!aPartRes)
+ aLastCompositeFeature = aCompFeat;
+ if (aEndFeatureParents.find(aCompFeat) != aEndFeatureParents.end()) {
+ // do not process the parent for the last feature,
+ // because it cannot select objects from its parent
+ isSkipFeature = true;
+ }
+ }
+ if (isSkipFeature)
+ continue;
+
+ std::list<ModelGeomAlgo_Shape::SubshapeOfResult> anApproproate;
+ if (ModelGeomAlgo_Shape::findSubshapeByPoint(*aFIt, thePoint, theShape->shapeType(),
+ anApproproate)) {
+ bool isContinue = true;
+ std::list<std::pair<GeomShapePtr, int> > aCenters;
+ std::list<ModelGeomAlgo_Shape::SubshapeOfResult>::iterator anApIt = anApproproate.begin();
+ for (; anApIt != anApproproate.end() && isContinue; ++anApIt) {
+ ++aNbPossibleSelections;
+
+ // stop if the target shape and result are found
+ GeomShapePtr aCurShape = anApIt->mySubshape;
+ if (!aCurShape)
+ aCurShape = anApIt->myResult->shape();
+
+ if (anApIt->myResult->isSame(theResult)) {
+ if (anApIt->myCenterType == (int)ModelAPI_AttributeSelection::NOT_CENTER)
+ isContinue = !aCurShape->isSame(theShape);
+ else if (theShape->isVertex() && aCurShape->isEdge()) {
+ GeomEdgePtr aCurEdge = aCurShape->edge();
+ GeomVertexPtr aVertex = theShape->vertex();
+ GeomPointPtr aCenter;
+ switch (anApIt->myCenterType) {
+ case (int)ModelAPI_AttributeSelection::CIRCLE_CENTER: {
+ GeomCirclePtr aCirc = aCurEdge->circle();
+ if (aCirc)
+ aCenter = aCirc->center();
+ break;
+ }
+ case (int)ModelAPI_AttributeSelection::ELLIPSE_FIRST_FOCUS: {
+ GeomEllipsePtr anEllipse = aCurEdge->ellipse();
+ if (anEllipse)
+ aCenter = anEllipse->firstFocus();
+ break;
+ }
+ case (int)ModelAPI_AttributeSelection::ELLIPSE_SECOND_FOCUS: {
+ GeomEllipsePtr anEllipse = aCurEdge->ellipse();
+ if (anEllipse)
+ aCenter = anEllipse->secondFocus();
+ break;
+ }
+ }
+ if (aCenter && aCenter->distance(aVertex->point()) < 1.e-7)
+ aCenters.push_back(std::pair<GeomShapePtr, int>(aCurShape, aNbPossibleSelections));
+ }
+ }
+ }
+ // passed till the appropriate shape, check the center of circle
+ // or a focus of ellipse is selected
+ if (isContinue && !aCenters.empty())
+ aNbPossibleSelections = aCenters.front().second;
+ }
+ }
+ return aNbPossibleSelections;
+}
+
+void ModelHighAPI_Dumper::DumpStorageGeom::write(const AttributeSelectionPtr& theAttrSelect)
+{
+ GeomShapePtr aShape;
+ ResultPtr aContext;
+ getShapeAndContext(theAttrSelect, aShape, aContext);
+
+ // how to dump selection: construction features are dumped by name always
+ FeaturePtr aSelectedFeature;
+ FeaturePtr aFeature = theAttrSelect->contextFeature();
+ if (aShape && aContext && !aFeature)
+ aSelectedFeature = ModelAPI_Feature::feature(aContext->data()->owner());
+ bool isDumpByGeom = aSelectedFeature && aSelectedFeature->isInHistory();
+
+ if (isDumpByGeom) {
+ myDumpBuffer << "model.selection(\"" << aShape->shapeTypeStr();
+ // check the selected item is a ResultPart;
+ // in this case it is necessary to get shape with full transformation
+ // for correct calculation of the middle point
+ ResultPartPtr aResPart =
+ std::dynamic_pointer_cast<ModelAPI_ResultPart>(theAttrSelect->context());
+ if (aResPart && aShape->shapeType() == GeomAPI_Shape::COMPOUND)
+ aShape = aResPart->shape();
+ GeomPointPtr aMiddlePoint = aShape->middlePoint();
+ // calculate number of features, which could be selected by the same point
+ FeaturePtr anOwner = ModelAPI_Feature::feature(theAttrSelect->owner());
+ int aNbPossibleSelections = possibleSelectionsByPoint(aMiddlePoint,
+ theAttrSelect->context(), aShape, aSelectedFeature, anOwner);
+
+ // produce the index if the number of applicable features is greater than 1
+ std::string anIndex;
+ if (aNbPossibleSelections > 1) {
+ std::ostringstream anOutput;
+ anOutput << "_" << aNbPossibleSelections;
+ anIndex = anOutput.str();
+ }
+
+ myDumpBuffer << std::setprecision(THE_DUMP_PRECISION)
+ << anIndex << "\", ("
+ << aMiddlePoint->x() << ", "
+ << aMiddlePoint->y() << ", "
+ << aMiddlePoint->z() << ")";
+ myDumpBuffer << ")";
+ }
+ else
+ DumpStorage::write(theAttrSelect);
+}
+
+void ModelHighAPI_Dumper::DumpStorageWeak::write(const AttributeSelectionPtr& theAttrSelect)
+{
+ GeomShapePtr aShape;
+ ResultPtr aContext;
+ getShapeAndContext(theAttrSelect, aShape, aContext);
+
+ bool aStandardDump = true;
+ if (aShape.get() && aContext.get() &&
+ aShape != aContext->shape()) { // weak naming for local selection only
+ GeomAlgoAPI_NExplode aNExplode(aContext->shape(), aShape->shapeType());
+ int anIndex = aNExplode.index(aShape);
+ if (anIndex != 0) { // found a week-naming index, so, export it
+ myDumpBuffer << "model.selection(\"" << aShape->shapeTypeStr() << "\", \""
+ << theAttrSelect->contextName(aContext) << "\", " << anIndex << ")";
+ aStandardDump = false;
+ }
+ }
+ if (aStandardDump)
+ DumpStorage::write(theAttrSelect);
+}
+// ======================================================================
+
+