From: azv Date: Thu, 7 Nov 2019 07:59:22 +0000 (+0300) Subject: Task 5.1.7: To be able to export a part to a file and import it into an existing... X-Git-Tag: V9_5_0a1~166^2~22 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=f41052a2c5a9440c85bb126a0f72df546e8d2f1c;p=modules%2Fshaper.git Task 5.1.7: To be able to export a part to a file and import it into an existing part (issue #3065) * Python API for export/import of features. * Fix the problem when exporting to unavailable directory. --- diff --git a/src/ExchangeAPI/ExchangeAPI_Export.cpp b/src/ExchangeAPI/ExchangeAPI_Export.cpp index fa1ab009e..e9863969c 100644 --- a/src/ExchangeAPI/ExchangeAPI_Export.cpp +++ b/src/ExchangeAPI/ExchangeAPI_Export.cpp @@ -19,6 +19,8 @@ #include "ExchangeAPI_Export.h" //-------------------------------------------------------------------------------------- +#include +//-------------------------------------------------------------------------------------- #include #include #include @@ -188,4 +190,17 @@ ExportPtr exportToXAO(const std::shared_ptr & thePart, return ExportPtr(new ExchangeAPI_Export(aFeature, theFilePath, theSelectedShape, "XAO")); } +void exportPart(const std::shared_ptr & thePart, + const std::string & theFilePath, + const std::list & theSelected) +{ + FeaturePtr aFeature = thePart->addFeature(ExchangePlugin_ExportPart::ID()); + aFeature->string(ExchangePlugin_ExportPart::FILE_PATH_ID())->setValue(theFilePath); + if (!theSelected.empty()) { + fillAttribute(theSelected, + aFeature->selectionList(ExchangePlugin_ExportPart::SELECTION_LIST_ID())); + } + // restart transaction to execute and delete the marcro-feature + apply(); +} //-------------------------------------------------------------------------------------- diff --git a/src/ExchangeAPI/ExchangeAPI_Export.h b/src/ExchangeAPI/ExchangeAPI_Export.h index 6cfddfd8f..c976d43a3 100644 --- a/src/ExchangeAPI/ExchangeAPI_Export.h +++ b/src/ExchangeAPI/ExchangeAPI_Export.h @@ -123,6 +123,15 @@ ExportPtr exportToXAO(const std::shared_ptr & thePart, const std::string & theAuthor = std::string(), const std::string & theGeometryName = std::string()); + +/** \ingroup CPPHighAPI + * \brief Export selected features or the whole part to the binary file. + */ +EXCHANGEAPI_EXPORT void exportPart( + const std::shared_ptr & thePart, + const std::string & theFilePath, + const std::list & theSelected = std::list()); + //-------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------- #endif /* SRC_EXCHANGEAPI_EXCHANGEAPI_EXPORT_H_ */ diff --git a/src/ExchangeAPI/ExchangeAPI_Import.cpp b/src/ExchangeAPI/ExchangeAPI_Import.cpp index d360c95a7..81b34ac03 100644 --- a/src/ExchangeAPI/ExchangeAPI_Import.cpp +++ b/src/ExchangeAPI/ExchangeAPI_Import.cpp @@ -19,7 +19,10 @@ #include "ExchangeAPI_Import.h" //-------------------------------------------------------------------------------------- +#include +//-------------------------------------------------------------------------------------- #include +#include #include //-------------------------------------------------------------------------------------- #include @@ -94,3 +97,24 @@ ImportPtr addImport( std::shared_ptr aFeature = thePart->addFeature(ExchangeAPI_Import::ID()); return ImportPtr(new ExchangeAPI_Import(aFeature, theFilePath)); } + +void importPart(const std::shared_ptr & thePart, + const std::string & theFilePath, + const ModelHighAPI_Reference & theAfterThis) +{ + static const bool THE_VISIBLE_FEATURE = false; + FeaturePtr aCurrentFeature; + if (theAfterThis.feature()) { + aCurrentFeature = thePart->currentFeature(THE_VISIBLE_FEATURE); + thePart->setCurrentFeature(theAfterThis.feature(), THE_VISIBLE_FEATURE); + } + + FeaturePtr aFeature = thePart->addFeature(ExchangePlugin_ImportPart::ID()); + aFeature->string(ExchangePlugin_ImportPart::FILE_PATH_ID())->setValue(theFilePath); + // restart transaction to execute and delete the marcro-feature + apply(); + + // restore current feature + if (aCurrentFeature) + thePart->setCurrentFeature(aCurrentFeature, THE_VISIBLE_FEATURE); +} diff --git a/src/ExchangeAPI/ExchangeAPI_Import.h b/src/ExchangeAPI/ExchangeAPI_Import.h index 1713c9271..57793c725 100644 --- a/src/ExchangeAPI/ExchangeAPI_Import.h +++ b/src/ExchangeAPI/ExchangeAPI_Import.h @@ -29,6 +29,8 @@ #include #include +#include +#include //-------------------------------------------------------------------------------------- /**\class ExchangeAPI_Import * \ingroup CPPHighAPI @@ -72,6 +74,14 @@ EXCHANGEAPI_EXPORT ImportPtr addImport(const std::shared_ptr & thePart, const std::string & theFilePath); +/** \ingroup CPPHighAPI + * \brief Import features from the file to the document after the current feature (or to the end). + */ +EXCHANGEAPI_EXPORT void importPart( + const std::shared_ptr & thePart, + const std::string & theFilePath, + const ModelHighAPI_Reference & theAfterThis = ModelHighAPI_Reference()); + //-------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------- #endif /* SRC_EXCHANGEAPI_EXCHANGEAPI_IMPORT_H_ */ diff --git a/src/ExchangePlugin/ExchangePlugin_ExportPart.cpp b/src/ExchangePlugin/ExchangePlugin_ExportPart.cpp index dbf476c2f..79de399f8 100644 --- a/src/ExchangePlugin/ExchangePlugin_ExportPart.cpp +++ b/src/ExchangePlugin/ExchangePlugin_ExportPart.cpp @@ -40,6 +40,7 @@ void ExchangePlugin_ExportPart::initAttributes() { data()->addAttribute(FILE_PATH_ID(), ModelAPI_AttributeString::typeId()); data()->addAttribute(FILE_FORMAT_ID(), ModelAPI_AttributeString::typeId()); + ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FILE_FORMAT_ID()); data()->addAttribute(SELECTION_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()); ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SELECTION_LIST_ID()); } diff --git a/src/Model/Model_Document.cpp b/src/Model/Model_Document.cpp index 8773abc2a..c69e0c5cb 100644 --- a/src/Model/Model_Document.cpp +++ b/src/Model/Model_Document.cpp @@ -67,10 +67,13 @@ #include #include +#include #include #include +#include #include #include +#include #include #ifndef WIN32 @@ -402,6 +405,14 @@ static bool saveDocument(Handle(Model_Application) theApp, { PCDM_StoreStatus aStatus; try { + // create the directory to save the document + OSD_Path aPathToFile = UTL::Path(theFilename); + aPathToFile.SetName(""); + aPathToFile.SetExtension(""); + OSD_Directory aBaseDir(aPathToFile); + if (!aBaseDir.Exists()) + aBaseDir.Build(OSD_Protection()); + // save the document aStatus = theApp->SaveAs(theDoc, theFilename); } catch (Standard_Failure const& anException) { diff --git a/src/PythonAPI/model/exchange/__init__.py b/src/PythonAPI/model/exchange/__init__.py index 308e1e6f3..5bd0d6c0b 100644 --- a/src/PythonAPI/model/exchange/__init__.py +++ b/src/PythonAPI/model/exchange/__init__.py @@ -19,4 +19,7 @@ """Package for Exchange plugin for the Parametric Geometry API of the Modeler. """ -from ExchangeAPI import addImport, exportToFile, exportToXAO \ No newline at end of file +from ExchangeAPI import addImport, exportToFile, exportToXAO +from ExchangeAPI import exportPart, importPart + +from .tools import * diff --git a/src/PythonAPI/model/exchange/tools.py b/src/PythonAPI/model/exchange/tools.py new file mode 100644 index 000000000..f6b9c6a71 --- /dev/null +++ b/src/PythonAPI/model/exchange/tools.py @@ -0,0 +1,31 @@ +# Copyright (C) 2019 CEA/DEN, EDF R&D +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +# + +import os +import tempfile + +# Generate temporary file name +def tempFileName(): + tempDir = tempfile.TemporaryDirectory() + return os.path.join(tempDir.name, "temp.shaperpart") + +def removeFile(theFilename): + try: os.remove(theFilename) + except OSError: pass + assert not os.path.exists(theFilename), "Cannot remove file {}".format(theFilename)