Salome HOME
updated copyright message
[modules/shaper.git] / src / ExchangeAPI / ExchangeAPI_Import.cpp
index ab60deed59bec458a66484951138ff8e0dfa215f..d29f7b2d2d870d876c0c9081d069a92df59c31bb 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2023  CEA, EDF
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 //
 // 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
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-// See http://www.salome-platform.org/ or
-// email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 
 #include "ExchangeAPI_Import.h"
 //--------------------------------------------------------------------------------------
+#include <ExchangePlugin_ImportPart.h>
+//--------------------------------------------------------------------------------------
 #include <ModelHighAPI_Dumper.h>
+#include <ModelHighAPI_Services.h>
 #include <ModelHighAPI_Tools.h>
 //--------------------------------------------------------------------------------------
+#include <ModelAPI_AttributeStringArray.h>
+#include <ModelAPI_AttributeImage.h>
+#include <ModelAPI_Session.h>
+#include <ModelAPI_Tools.h>
+#include <GeomAlgoAPI_Tools.h>
+//--------------------------------------------------------------------------------------
 #include <algorithm>
+//--------------------------------------------------------------------------------------
+#include <QPixmap>
+#include <QDir>
+#include <QFile>
+#include <QFileInfo>
 
 ExchangeAPI_Import::ExchangeAPI_Import(
     const std::shared_ptr<ModelAPI_Feature> & theFeature)
@@ -41,17 +54,49 @@ ExchangeAPI_Import::ExchangeAPI_Import(
     setFilePath(theFilePath);
 }
 
+ExchangeAPI_Import::ExchangeAPI_Import(
+    const std::shared_ptr<ModelAPI_Feature> & theFeature,
+    const std::string & theFilePath,
+    const bool theScalInterUnits,
+    const bool theMaterials,
+    const bool theColor)
+: ModelHighAPI_Interface(theFeature)
+{
+  if (initialize())
+    setParameters(theFilePath, theScalInterUnits, theMaterials, theColor);
+}
+
 ExchangeAPI_Import::~ExchangeAPI_Import()
 {
 
 }
 
+//--------------------------------------------------------------------------------------
+void ExchangeAPI_Import::setParameters(const std::string & theFilePath,
+                                       const bool theScalInterUnits,
+                                       const bool theMaterials,
+                                       const bool theColor)
+{
+  fillAttribute(theFilePath, mystepFilePath);
+  fillAttribute("STEP", myimportType);
+  fillAttribute(theScalInterUnits, myscalInterUnits);
+  fillAttribute(theMaterials,mymaterials);
+  fillAttribute(theColor,mycolors);
+  execute();
+}
+
 //--------------------------------------------------------------------------------------
 void ExchangeAPI_Import::setFilePath(const std::string & theFilePath)
 {
-  fillAttribute(theFilePath, myfilePath);
 
-  execute();
+  std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFilePath);
+  if (anExtension == "STEP" || anExtension == "STP") {
+    setParameters(theFilePath,true,false,false);
+  } else {
+    fillAttribute(theFilePath, myfilePath);
+    fillAttribute(anExtension, myimportType);
+    execute();
+  }
 }
 
 //--------------------------------------------------------------------------------------
@@ -60,7 +105,17 @@ void ExchangeAPI_Import::dump(ModelHighAPI_Dumper& theDumper) const
   FeaturePtr aBase = feature();
   std::string aPartName = theDumper.name(aBase->document());
 
-  std::string aFilePath = aBase->string(ExchangePlugin_ImportFeature::FILE_PATH_ID())->value();
+  AttributeStringPtr aImportTypeAttr =
+                    aBase->string(ExchangePlugin_ImportFeature::IMPORT_TYPE_ID());
+  std::string aFormat = aImportTypeAttr->value();
+  std::string aFilePath;
+  if (aFormat == "STEP" || aFormat == "STP")
+  {
+    aFilePath = aBase->string(ExchangePlugin_ImportFeature::STEP_FILE_PATH_ID())->value();
+  } else {
+    aFilePath = aBase->string(ExchangePlugin_ImportFeature::FILE_PATH_ID())->value();
+  }
+
   std::string aFrom = "\\";
   std::string aTo = "\\\\";
   for(std::size_t aPos = aFilePath.find(aFrom);
@@ -69,15 +124,25 @@ void ExchangeAPI_Import::dump(ModelHighAPI_Dumper& theDumper) const
     aFilePath.replace(aPos, aFrom.size(), aTo);
     aPos += aTo.size();
   }
+  std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(aFilePath);
+  if (anExtension == "STP" || anExtension == "STEP"){
+      theDumper << aBase << " = model.addImportSTEP(" << aPartName << ", \""
+                << aFilePath << "\"" ;
 
-  theDumper << aBase << " = model.addImport(" << aPartName << ", \""
+      theDumper << ", " << scalInterUnits()->value()
+                << ", " << materials()->value()
+                << ", " << colors()->value() << ")"<< std::endl;
+  } else {
+      theDumper << aBase << " = model.addImport(" << aPartName << ", \""
             << aFilePath << "\")" << std::endl;
+  }
+
   // to make import have results
   theDumper << "model.do()" << std::endl;
 
   CompositeFeaturePtr aCompositeFeature =
     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aBase);
-  if(aCompositeFeature.get()) {
+  if (aCompositeFeature.get()) {
     int aNbOfSubs = aCompositeFeature->numberOfSubs();
     for(int anIndex = 0; anIndex < aNbOfSubs; ++anIndex) {
       std::string aSubFeatureGet =
@@ -95,3 +160,139 @@ ImportPtr addImport(
   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ExchangeAPI_Import::ID());
   return ImportPtr(new ExchangeAPI_Import(aFeature, theFilePath));
 }
+
+ImportPtr addImportSTEP(
+    const std::shared_ptr<ModelAPI_Document> & thePart,
+    const std::string & theFilePath,
+    const bool theScalInterUnits,
+    const bool theMaterials,
+    const bool theColor )
+{
+  std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ExchangeAPI_Import::ID());
+  return ImportPtr(new ExchangeAPI_Import(aFeature, theFilePath,
+                                          theScalInterUnits, theMaterials, theColor));
+}
+
+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);
+
+  // specify the ID of selected document
+  int aTargetPartIndex = 0;
+  SessionPtr aSession = ModelAPI_Session::get();
+  if (aSession->moduleDocument() == thePart) {
+    // Importing to PartSet has 2 choices: import directly to PartSet (if possible)
+    // or create a new part. Because then importing to existing part the document
+    // has to be specified explicitly.
+    // As a result, parse the list of possible target documents and generate new part
+    // if the import document is not applicable on PartSet level
+    // (there is no 'PartSet' in the list of applicable documents).
+    AttributeStringArrayPtr aDocsList =
+        aFeature->stringArray(ExchangePlugin_ImportPart::TARGET_PARTS_LIST_ID());
+    if (aDocsList->size() > 1 && aDocsList->value(1) == "PartSet")
+      aTargetPartIndex = 1;
+  }
+  aFeature->integer(ExchangePlugin_ImportPart::TARGET_PART_ID())->setValue(aTargetPartIndex);
+
+  // restart transaction to execute and delete the macro-feature
+  apply();
+
+  // restore current feature
+  if (aCurrentFeature)
+    thePart->setCurrentFeature(aCurrentFeature, THE_VISIBLE_FEATURE);
+}
+
+//-------------------------------------------------------------------------------------------------
+//-------------------------------------------------------------------------------------------------
+
+ExchangeAPI_Import_Image::ExchangeAPI_Import_Image(
+    const std::shared_ptr<ModelAPI_Feature> & theFeature)
+: ModelHighAPI_Interface(theFeature)
+{
+  initialize();
+}
+
+ExchangeAPI_Import_Image::ExchangeAPI_Import_Image(
+    const std::shared_ptr<ModelAPI_Feature> & theFeature,
+    const std::string & theFilePath)
+: ModelHighAPI_Interface(theFeature)
+{
+  if (initialize())
+    setFilePath(theFilePath);
+}
+
+void ExchangeAPI_Import_Image::setFilePath(const std::string & theFilePath)
+{
+  fillAttribute(theFilePath, myfilePath);
+  execute();
+}
+
+ImportImagePtr addImportImage(
+    const std::shared_ptr<ModelAPI_Document> & thePart,
+    const std::string & theFilePath)
+{
+  std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ExchangeAPI_Import_Image::ID());
+  return ImportImagePtr(new ExchangeAPI_Import_Image(aFeature, theFilePath));
+}
+
+void ExchangeAPI_Import_Image::dump(ModelHighAPI_Dumper& theDumper) const
+{
+  FeaturePtr aBase = feature();
+  std::string aPartName = theDumper.name(aBase->document());
+
+  std::string aFilePath =
+    aBase->string(ExchangePlugin_Import_ImageFeature::FILE_PATH_ID())->value();
+
+  // store image into a new file near the dumped python script
+  ResultPtr aResult = aBase->firstResult();
+  std::string aNewImageDir = theDumper.getDumpDir();
+  if (aResult.get() && aResult->hasTexture()) {
+    // get image data
+    int aWidth, aHeight;
+    std::string aFormat;
+    std::list<unsigned char> aByteList;
+    AttributeImagePtr anImageAttr =
+      aResult->data()->image(ModelAPI_ResultBody::IMAGE_ID());
+    anImageAttr->texture(aWidth, aHeight, aByteList, aFormat);
+
+    // convert image data to QPixmap
+    uchar* arr = new uchar[aByteList.size()];
+    std::copy(aByteList.begin(), aByteList.end(), arr);
+    QImage image (arr, aWidth, aHeight, QImage::Format_ARGB32);
+    QPixmap pixmap = QPixmap::fromImage( image );
+
+    // get new file name
+    std::wstring aName = aBase->name();
+    std::string anImageName (aName.begin(), aName.end());
+    std::string aNewImageFile = anImageName + "." + aFormat;
+    QFileInfo aNewFileInfo (QDir(aNewImageDir.c_str()), aNewImageFile.c_str());
+    for (int ii = 1; QFile::exists(aNewFileInfo.absoluteFilePath()); ii++) {
+      // construct the new file name by adding the unique number
+      aNewImageFile = anImageName + "_" + std::to_string(ii) + "." + aFormat;
+      aNewFileInfo.setFile(QDir(aNewImageDir.c_str()), aNewImageFile.c_str());
+    }
+
+    // write image to a new file
+    if (pixmap.save(aNewFileInfo.absoluteFilePath())) {
+      // to dump new file name
+      aFilePath = aNewFileInfo.absoluteFilePath().toStdString();
+    }
+    delete [] arr;
+  }
+
+  theDumper << aBase << " = model.addImportImage(" << aPartName << ", \""
+            << aFilePath << "\")" << std::endl;
+
+  // to make import have results
+  theDumper << "model.do()" << std::endl;
+}