Salome HOME
[bos #32342] EDF 26130 - Undo and auxiliarity
[modules/shaper.git] / src / ExchangeAPI / ExchangeAPI_Import.cpp
index f12ea5dc6d3059230ddd7bc9498c957b2ddb4fe9..195a1af80abe2f79d73ef0ff90cd6eb3f6fcc075 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2020  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2022  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
 #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)
@@ -205,3 +211,88 @@ void importPart(const std::shared_ptr<ModelAPI_Document> & thePart,
   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;
+}