Salome HOME
Task 5.1.7: To be able to export a part to a file and import it into an existing...
authorazv <azv@opencascade.com>
Thu, 7 Nov 2019 07:59:22 +0000 (10:59 +0300)
committerazv <azv@opencascade.com>
Thu, 7 Nov 2019 07:59:22 +0000 (10:59 +0300)
* Python API for export/import of features.
* Fix the problem when exporting to unavailable directory.

src/ExchangeAPI/ExchangeAPI_Export.cpp
src/ExchangeAPI/ExchangeAPI_Export.h
src/ExchangeAPI/ExchangeAPI_Import.cpp
src/ExchangeAPI/ExchangeAPI_Import.h
src/ExchangePlugin/ExchangePlugin_ExportPart.cpp
src/Model/Model_Document.cpp
src/PythonAPI/model/exchange/__init__.py
src/PythonAPI/model/exchange/tools.py [new file with mode: 0644]

index fa1ab009e885ea54f52438e9754466c91d7dc332..e9863969ca7195ea9a940f1819c23d1a1155bb01 100644 (file)
@@ -19,6 +19,8 @@
 
 #include "ExchangeAPI_Export.h"
 //--------------------------------------------------------------------------------------
+#include <ExchangePlugin_ExportPart.h>
+//--------------------------------------------------------------------------------------
 #include <ModelAPI_Document.h>
 #include <ModelAPI_Feature.h>
 #include <ModelHighAPI_Tools.h>
@@ -188,4 +190,17 @@ ExportPtr exportToXAO(const std::shared_ptr<ModelAPI_Document> & thePart,
   return ExportPtr(new ExchangeAPI_Export(aFeature, theFilePath, theSelectedShape, "XAO"));
 }
 
+void exportPart(const std::shared_ptr<ModelAPI_Document> & thePart,
+                const std::string & theFilePath,
+                const std::list<ModelHighAPI_Selection> & 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();
+}
 //--------------------------------------------------------------------------------------
index 6cfddfd8f65d13500e2b984bb59d85dc39a4dff5..c976d43a31ecbed9b9c10ea39de875ea72e1e04f 100644 (file)
@@ -123,6 +123,15 @@ ExportPtr exportToXAO(const std::shared_ptr<ModelAPI_Document> & 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<ModelAPI_Document> & thePart,
+    const std::string & theFilePath,
+    const std::list<ModelHighAPI_Selection> & theSelected = std::list<ModelHighAPI_Selection>());
+
 //--------------------------------------------------------------------------------------
 //--------------------------------------------------------------------------------------
 #endif /* SRC_EXCHANGEAPI_EXCHANGEAPI_EXPORT_H_ */
index d360c95a76ca54f2bc23866b2f33c8db82a49d2e..81b34ac03e66d53ddfbf1a6f921d0c6833c51327 100644 (file)
 
 #include "ExchangeAPI_Import.h"
 //--------------------------------------------------------------------------------------
+#include <ExchangePlugin_ImportPart.h>
+//--------------------------------------------------------------------------------------
 #include <ModelHighAPI_Dumper.h>
+#include <ModelHighAPI_Services.h>
 #include <ModelHighAPI_Tools.h>
 //--------------------------------------------------------------------------------------
 #include <algorithm>
@@ -94,3 +97,24 @@ ImportPtr addImport(
   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ExchangeAPI_Import::ID());
   return ImportPtr(new ExchangeAPI_Import(aFeature, theFilePath));
 }
+
+void importPart(const std::shared_ptr<ModelAPI_Document> & 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);
+}
index 1713c9271bfc4eba6b5ae063e06e4cd45829c825..57793c725c106343ab44a5ac76a2877248be9b93 100644 (file)
@@ -29,6 +29,8 @@
 
 #include <ModelHighAPI_Interface.h>
 #include <ModelHighAPI_Macro.h>
+#include <ModelHighAPI_Reference.h>
+#include <ModelHighAPI_Selection.h>
 //--------------------------------------------------------------------------------------
 /**\class ExchangeAPI_Import
  * \ingroup CPPHighAPI
@@ -72,6 +74,14 @@ EXCHANGEAPI_EXPORT
 ImportPtr addImport(const std::shared_ptr<ModelAPI_Document> & 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<ModelAPI_Document> & thePart,
+    const std::string & theFilePath,
+    const ModelHighAPI_Reference & theAfterThis = ModelHighAPI_Reference());
+
 //--------------------------------------------------------------------------------------
 //--------------------------------------------------------------------------------------
 #endif /* SRC_EXCHANGEAPI_EXCHANGEAPI_IMPORT_H_ */
index dbf476c2fe9b2eca08ea2c59042588855e5cdfd8..79de399f8e69c93eb2d4b93be3c5ce25733c72c0 100644 (file)
@@ -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());
 }
index 8773abc2ae44de88d0d4ee6aea2b643581a98334..c69e0c5cb0abe63fab1ab559f5353c295d74eb28 100644 (file)
 #include <TopExp_Explorer.hxx>
 #include <TopoDS_Shape.hxx>
 
+#include <OSD_Directory.hxx>
 #include <OSD_File.hxx>
 #include <OSD_Path.hxx>
+#include <OSD_Protection.hxx>
 #include <CDF_Session.hxx>
 #include <CDF_Directory.hxx>
+#include <UTL.hxx>
 
 #include <climits>
 #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) {
index 308e1e6f3929ba4b695a77e6065b8d0cd8ffa40d..5bd0d6c0b6cc60348c27786a3803a169112d49f3 100644 (file)
@@ -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 (file)
index 0000000..f6b9c6a
--- /dev/null
@@ -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)