Salome HOME
Make exportToXAO work even if a group or a field is not valid.
[modules/shaper.git] / src / ExchangePlugin / ExchangePlugin_ExportFeature.cpp
index f4e87b76ef0c6378491cc4d1b31e9a68162190c2..02b31988bc8193e0de6245ee6ef8bbbb1cc6519b 100644 (file)
@@ -1,40 +1,65 @@
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
-
-// File:    ExchangePlugin_ExportFeature.cpp
-// Created: May 14, 2015
-// Author:  Sergey POKHODENKO
+// Copyright (C) 2014-2017  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<mailto:webmaster.salome@opencascade.com>
+//
 
 #include <ExchangePlugin_ExportFeature.h>
 
-#include <ExchangePlugin_Tools.h>
+#include <algorithm>
+#include <iterator>
+#include <string>
+#ifdef _DEBUG
+#include <iostream>
+#include <ostream>
+#endif
+
+#include <Config_Common.h>
+#include <Config_PropManager.h>
 
 #include <GeomAlgoAPI_BREPExport.h>
 #include <GeomAlgoAPI_CompoundBuilder.h>
 #include <GeomAlgoAPI_IGESExport.h>
 #include <GeomAlgoAPI_STEPExport.h>
 #include <GeomAlgoAPI_Tools.h>
-
-#include <Config_Common.h>
-#include <Config_PropManager.h>
+#include <GeomAlgoAPI_XAOExport.h>
 
 #include <GeomAPI_Shape.h>
 
 #include <ModelAPI_AttributeSelectionList.h>
 #include <ModelAPI_AttributeString.h>
+#include <ModelAPI_AttributeStringArray.h>
+#include <ModelAPI_AttributeIntArray.h>
+#include <ModelAPI_AttributeTables.h>
 #include <ModelAPI_Data.h>
 #include <ModelAPI_Document.h>
 #include <ModelAPI_Object.h>
 #include <ModelAPI_ResultBody.h>
+#include <ModelAPI_ResultGroup.h>
+#include <ModelAPI_ResultField.h>
+#include <ModelAPI_Session.h>
+#include <ModelAPI_Validator.h>
 
-#include <TopoDS_Shape.hxx>
+#include <XAO_Group.hxx>
+#include <XAO_Field.hxx>
+#include <XAO_Xao.hxx>
+#include <XAO_Geometry.hxx>
 
-#include <algorithm>
-#include <iterator>
-#include <string>
-#ifdef _DEBUG
-#include <iostream>
-#include <ostream>
-#endif
+#include <ExchangePlugin_Tools.h>
 
 ExchangePlugin_ExportFeature::ExchangePlugin_ExportFeature()
 {
@@ -46,21 +71,39 @@ ExchangePlugin_ExportFeature::~ExchangePlugin_ExportFeature()
 }
 
 /*
- * Returns the unique kind of a feature
+ * Request for initialization of data model of the feature: adding all attributes
  */
-const std::string& ExchangePlugin_ExportFeature::getKind()
+void ExchangePlugin_ExportFeature::initAttributes()
 {
-  return ExchangePlugin_ExportFeature::ID();
+  data()->addAttribute(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID(),
+    ModelAPI_AttributeString::typeId());
+  data()->addAttribute(ExchangePlugin_ExportFeature::FILE_PATH_ID(),
+    ModelAPI_AttributeString::typeId());
+  data()->addAttribute(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID(),
+    ModelAPI_AttributeString::typeId());
+  data()->addAttribute(ExchangePlugin_ExportFeature::FILE_FORMAT_ID(),
+    ModelAPI_AttributeString::typeId());
+  data()->addAttribute(ExchangePlugin_ExportFeature::SELECTION_LIST_ID(),
+    ModelAPI_AttributeSelectionList::typeId());
+  data()->addAttribute(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID(),
+    ModelAPI_AttributeString::typeId());
+  data()->addAttribute(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID(),
+    ModelAPI_AttributeString::typeId());
+
+  ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
+    ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID());
+  ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
+    ExchangePlugin_ExportFeature::XAO_AUTHOR_ID());
+  ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
+    ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID());
 }
 
-/*
- * Request for initialization of data model of the feature: adding all attributes
- */
-void ExchangePlugin_ExportFeature::initAttributes()
+void ExchangePlugin_ExportFeature::attributeChanged(const std::string& theID)
 {
-  data()->addAttribute(ExchangePlugin_ExportFeature::FILE_FORMAT_ID(), ModelAPI_AttributeString::typeId());
-  data()->addAttribute(ExchangePlugin_ExportFeature::FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
-  data()->addAttribute(ExchangePlugin_ExportFeature::SELECTION_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
+  if (theID == XAO_FILE_PATH_ID()) {
+    string(ExchangePlugin_ExportFeature::FILE_PATH_ID())->setValue(
+      string(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID())->value());
+  }
 }
 
 /*
@@ -71,9 +114,6 @@ void ExchangePlugin_ExportFeature::execute()
   AttributeStringPtr aFormatAttr =
       this->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID());
   std::string aFormat = aFormatAttr->value();
-  // Format may be empty. In this case look at extension.
-//  if (aFormat.empty())
-//    return;
 
   AttributeStringPtr aFilePathAttr =
       this->string(ExchangePlugin_ExportFeature::FILE_PATH_ID());
@@ -81,26 +121,15 @@ void ExchangePlugin_ExportFeature::execute()
   if (aFilePath.empty())
     return;
 
-  AttributeSelectionListPtr aSelectionListAttr =
-      this->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID());
-  std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
-  for (int i = 0, aSize = aSelectionListAttr->size(); i < aSize; ++i) {
-    aShapes.push_back(aSelectionListAttr->value(i)->value());
-  }
-  std::shared_ptr<GeomAPI_Shape> aShape =
-      GeomAlgoAPI_CompoundBuilder::compound(aShapes);
-
-  exportFile(aFilePath, aFormat, aShape);
+  exportFile(aFilePath, aFormat);
 }
 
-bool ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
-                                              const std::string& theFormat,
-                                              std::shared_ptr<GeomAPI_Shape> theShape)
+void ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
+                                              const std::string& theFormat)
 {
-  // retrieve the file and plugin library names
   std::string aFormatName = theFormat;
 
-  if (theFormat.empty()) { // look at extension
+  if (aFormatName.empty()) { // get default format for the extension
     // ".brep" -> "BREP"
     std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
     if (anExtension == "BREP" || anExtension == "BRP") {
@@ -109,15 +138,41 @@ bool ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
       aFormatName = "STEP";
     } else if (anExtension == "IGES" || anExtension == "IGS") {
       aFormatName = "IGES-5.1";
+    } else if (anExtension == "XAO") {
+      aFormatName = "XAO";
     } else {
       aFormatName = anExtension;
     }
   }
 
+  if (aFormatName == "XAO") {
+    exportXAO(theFileName);
+    return;
+  }
+
+  // make shape for export from selected shapes
+  AttributeSelectionListPtr aSelectionListAttr =
+      this->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID());
+  std::list<GeomShapePtr> aShapes;
+  for (int i = 0, aSize = aSelectionListAttr->size(); i < aSize; ++i) {
+    AttributeSelectionPtr anAttrSelection = aSelectionListAttr->value(i);
+    std::shared_ptr<GeomAPI_Shape> aCurShape = anAttrSelection->value();
+    if (aCurShape.get() == NULL)
+      aCurShape = anAttrSelection->context()->shape();
+    if (aCurShape.get() != NULL)
+      aShapes.push_back(aCurShape);
+  }
+
+  // Store compound if we have more than one shape.
+  std::shared_ptr<GeomAPI_Shape> aShape;
+  if(aShapes.size() == 1) {
+    aShape = aShapes.front();
+  } else {
+    aShape = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
+  }
+
   // Perform the export
   std::string anError;
-
-  TopoDS_Shape aShape(theShape->impl<TopoDS_Shape>());
   bool aResult = false;
   if (aFormatName == "BREP") {
     aResult = BREPExport(theFileName, aFormatName, aShape, anError);
@@ -126,15 +181,237 @@ bool ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
   } else if (aFormatName.substr(0, 4) == "IGES") {
     aResult = IGESExport(theFileName, aFormatName, aShape, anError);
   } else {
-    anError = "Unsupported format " + aFormatName;
+    anError = "Unsupported format: " + aFormatName;
+  }
+
+  if (!anError.empty()) {
+    setError("An error occurred while exporting " + theFileName + ": " + anError);
+    return;
+  }
+}
+
+/// Returns XAO string by the value from the table
+static std::string valToString(const ModelAPI_AttributeTables::Value& theVal,
+  const ModelAPI_AttributeTables::ValueType& theType) {
+  std::ostringstream aStr; // the resulting string value
+  switch(theType) {
+  case ModelAPI_AttributeTables::BOOLEAN:
+    aStr<<(theVal.myBool ? "true" : "false");
+    break;
+  case ModelAPI_AttributeTables::INTEGER:
+    aStr<<theVal.myInt;
+    break;
+  case ModelAPI_AttributeTables::DOUBLE:
+    aStr<<theVal.myDouble;
+    break;
+  case ModelAPI_AttributeTables::STRING:
+    aStr<<theVal.myStr;
+    break;
+  }
+  return aStr.str();
+}
+
+void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName)
+{
+  try {
+
+  std::string anError;
+  XAO::Xao aXao;
+
+  // author
+
+  std::string anAuthor = string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID())->value();
+  aXao.setAuthor(anAuthor);
+
+  // make shape for export from all results
+  std::list<GeomShapePtr> aShapes;
+  std::list<ResultBodyPtr> aResults;
+  int aBodyCount = document()->size(ModelAPI_ResultBody::group());
+  for (int aBodyIndex = 0; aBodyIndex < aBodyCount; ++aBodyIndex) {
+    ResultBodyPtr aResultBody =
+        std::dynamic_pointer_cast<ModelAPI_ResultBody>(
+            document()->object(ModelAPI_ResultBody::group(), aBodyIndex));
+    if (!aResultBody.get())
+      continue;
+    aShapes.push_back(aResultBody->shape());
+    aResults.push_back(aResultBody);
+  }
+  GeomShapePtr aShape = (aShapes.size() == 1)
+      ? *aShapes.begin()
+      : GeomAlgoAPI_CompoundBuilder::compound(aShapes);
+
+  SetShapeToXAO(aShape, &aXao, anError);
+
+  if (!anError.empty()) {
+    setError("An error occurred while exporting " + theFileName + ": " + anError);
+    return;
+  }
+
+  // geometry name
+  std::string aGeometryName = string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID())->value();
+  if (aGeometryName.empty() && aBodyCount == 1) {
+    // get the name from the first result
+    ResultBodyPtr aResultBody = *aResults.begin();
+    aGeometryName = aResultBody->data()->name();
+  }
+
+  aXao.getGeometry()->setName(aGeometryName);
+
+  // groups
+  int aGroupCount = document()->size(ModelAPI_ResultGroup::group());
+  for (int aGroupIndex = 0; aGroupIndex < aGroupCount; ++aGroupIndex) {
+    ResultGroupPtr aResultGroup =
+        std::dynamic_pointer_cast<ModelAPI_ResultGroup>(
+            document()->object(ModelAPI_ResultGroup::group(), aGroupIndex));
+
+    FeaturePtr aGroupFeature = document()->feature(aResultGroup);
+
+    AttributeSelectionListPtr aSelectionList =
+        aGroupFeature->selectionList("group_list");
+
+    // conversion of dimension
+    std::string aSelectionType = aSelectionList->selectionType();
+    std::string aDimensionString =
+      ExchangePlugin_Tools::selectionType2xaoDimension(aSelectionType);
+    XAO::Dimension aGroupDimension = XAO::XaoUtils::stringToDimension(aDimensionString);
+
+    XAO::Group* aXaoGroup = aXao.addGroup(aGroupDimension,
+                                          aResultGroup->data()->name());
+
+    try {
+      for (int aSelectionIndex = 0; aSelectionIndex < aSelectionList->size(); ++aSelectionIndex) {
+        AttributeSelectionPtr aSelection = aSelectionList->value(aSelectionIndex);
+
+        // complex conversion of reference id to element index
+        // gives bad id in case the selection is done from python script
+        // => using GeomAlgoAPI_CompoundBuilder::id instead
+        // int aReferenceID_old = aSelection->Id();
+
+        int aReferenceID = GeomAlgoAPI_CompoundBuilder::id(aShape, aSelection->value());
+
+        std::string aReferenceString = XAO::XaoUtils::intToString(aReferenceID);
+        int anElementID =
+          aXao.getGeometry()->getElementIndexByReference(aGroupDimension, aReferenceString);
+
+        aXaoGroup->add(anElementID);
+      }
+    } catch (XAO::XAO_Exception& e) {
+      std::string msg = "An error occurred while exporting group " + aResultGroup->data()->name();
+      msg += ".\n";
+      msg += e.what();
+      msg += "\n";
+      msg += "=> skipping this group from XAO export.";
+      setError(msg);
+      aXao.removeGroup(aXaoGroup);
+    }
   }
 
-  if (!aResult) {
-    std::string aShapeError =
-        "An error occurred while exporting " + theFileName + ": " + anError;
-    setError(aShapeError);
-    return false;
+  // fields
+  int aFieldCount = document()->size(ModelAPI_ResultField::group());
+  for (int aFieldIndex = 0; aFieldIndex < aFieldCount; ++aFieldIndex) {
+    ResultFieldPtr aResultField =
+        std::dynamic_pointer_cast<ModelAPI_ResultField>(
+            document()->object(ModelAPI_ResultField::group(), aFieldIndex));
+
+    FeaturePtr aFieldFeature = document()->feature(aResultField);
+
+    AttributeSelectionListPtr aSelectionList =
+        aFieldFeature->selectionList("selected");
+
+    // conversion of dimension
+    std::string aSelectionType = aSelectionList->selectionType();
+    std::string aDimensionString =
+      ExchangePlugin_Tools::selectionType2xaoDimension(aSelectionType);
+    XAO::Dimension aFieldDimension = XAO::XaoUtils::stringToDimension(aDimensionString);
+    bool isWholePart = aSelectionType == "part";
+    // get tables and their type
+    std::shared_ptr<ModelAPI_AttributeTables> aTables = aFieldFeature->tables("values");
+    std::string aTypeString = ExchangePlugin_Tools::valuesType2xaoType(aTables->type());
+    XAO::Type aFieldType = XAO::XaoUtils::stringToFieldType(aTypeString);
+
+    XAO::Field* aXaoField = aXao.addField(aFieldType, aFieldDimension, aTables->columns(),
+                                          aResultField->data()->name());
+
+
+    try {
+      // set components names
+      AttributeStringArrayPtr aComponents = aFieldFeature->stringArray("components_names");
+      for(int aComp = 0; aComp < aComponents->size(); aComp++) {
+        std::string aName = aComponents->value(aComp);
+        aXaoField->setComponentName(aComp, aName);
+      }
+
+      AttributeIntArrayPtr aStamps = aFieldFeature->intArray("stamps");
+      for (int aStepIndex = 0; aStepIndex < aTables->tables(); aStepIndex++) {
+        XAO::Step* aStep = aXaoField->addNewStep(aStepIndex);
+        aStep->setStep(aStepIndex);
+        int aStampIndex = aStamps->value(aStepIndex);
+        aStep->setStamp(aStampIndex);
+        int aNumElements = isWholePart ? aXaoField->countElements() : aTables->rows();
+        int aNumComps = aTables->columns();
+        std::set<int> aFilledIDs; // to fill the rest by defaults
+        // omit default values first row
+        for(int aRow = isWholePart ? 0 : 1; aRow < aNumElements; aRow++) {
+          for(int aCol = 0; aCol < aNumComps; aCol++) {
+            int anElementID = 0;
+            if (!isWholePart) {
+              // element index actually is the ID of the selection
+              AttributeSelectionPtr aSelection = aSelectionList->value(aRow - 1);
+
+              // complex conversion of reference id to element index
+              // gives bad id in case the selection is done from python script
+              // => using GeomAlgoAPI_CompoundBuilder::id instead
+              //int aReferenceID_old = aSelection->Id();
+
+              int aReferenceID = GeomAlgoAPI_CompoundBuilder::id(aShape, aSelection->value());
+
+              std::string aReferenceString = XAO::XaoUtils::intToString(aReferenceID);
+              anElementID =
+                aXao.getGeometry()->getElementIndexByReference(aFieldDimension, aReferenceString);
+            }
+
+            ModelAPI_AttributeTables::Value aVal = aTables->value(
+              isWholePart ? 0 : aRow, aCol, aStepIndex);
+            std::string aStrVal = valToString(aVal, aTables->type());
+            aStep->setStringValue(isWholePart ? aRow : anElementID, aCol, aStrVal);
+            aFilledIDs.insert(anElementID);
+          }
+        }
+        if (!isWholePart) { // fill the rest values by default ones
+          XAO::GeometricElementList::iterator allElem = aXao.getGeometry()->begin(aFieldDimension);
+          for(; allElem != aXao.getGeometry()->end(aFieldDimension); allElem++) {
+            if (aFilledIDs.find(allElem->first) != aFilledIDs.end())
+              continue;
+            for(int aCol = 0; aCol < aNumComps; aCol++) {
+              ModelAPI_AttributeTables::Value aVal = aTables->value(0, aCol, aStepIndex); // default
+              std::string aStrVal = valToString(aVal, aTables->type());
+              aStep->setStringValue(allElem->first, aCol, aStrVal);
+            }
+          }
+        }
+      }
+    } catch (XAO::XAO_Exception& e) {
+      std::string msg = "An error occurred while exporting field " + aResultField->data()->name();
+      msg += ".\n";
+      msg += e.what();
+      msg += "\n";
+      msg += "=> skipping this field from XAO export.";
+      setError(msg);
+      aXao.removeField(aXaoField);
+    }
   }
 
-  return true;
+  // exporting
+  XAOExport(theFileName, &aXao, anError);
+
+  if (!anError.empty()) {
+    setError("An error occurred while exporting " + theFileName + ": " + anError);
+    return;
+  }
+
+  } catch (XAO::XAO_Exception& e) {
+    std::string anError = e.what();
+    setError("An error occurred while exporting " + theFileName + ": " + anError);
+    return;
+  }
 }