Salome HOME
Issue #1865 : implementation of import/export fields in XAO format
authormpv <mpv@opencascade.com>
Fri, 25 Nov 2016 12:31:47 +0000 (15:31 +0300)
committermpv <mpv@opencascade.com>
Fri, 25 Nov 2016 12:31:47 +0000 (15:31 +0300)
src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp
src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp
src/ExchangePlugin/ExchangePlugin_Tools.cpp
src/ExchangePlugin/ExchangePlugin_Tools.h
src/XAO/XAO_XaoUtils.cxx

index ddc8e1431adead0bb409d60693077ed4b5f16a17..e2bbfa0e5147dacd3e0f9f55fc6ed7d288b0556e 100644 (file)
 
 #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 <XAO_Group.hxx>
+#include <XAO_Field.hxx>
 #include <XAO_Xao.hxx>
 
 #include <ExchangePlugin_Tools.h>
@@ -224,7 +229,8 @@ void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName)
 
     // conversion of dimension
     std::string aSelectionType = aSelectionList->selectionType();
-    std::string aDimensionString = ExchangePlugin_Tools::selectionType2xaoDimension(aSelectionType);
+    std::string aDimensionString = 
+      ExchangePlugin_Tools::selectionType2xaoDimension(aSelectionType);
     XAO::Dimension aGroupDimension = XAO::XaoUtils::stringToDimension(aDimensionString);
 
     XAO::Group* aXaoGroup = aXao.addGroup(aGroupDimension,
@@ -243,6 +249,86 @@ void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName)
     }
   }
 
+  // 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());
+    // 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();
+      // 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
+            int aReferenceID = aSelection->Id();
+            std::string aReferenceString = XAO::XaoUtils::intToString(aReferenceID);
+            int anElementID =
+              aXao.getGeometry()->getElementIndexByReference(aFieldDimension, aReferenceString);
+          }
+
+          ModelAPI_AttributeTables::Value aVal = aTables->value(
+            isWholePart ? 0 : aRow, aCol, aStepIndex);
+          std::ostringstream aStr; // string value
+          switch(aTables->type()) {
+          case ModelAPI_AttributeTables::BOOLEAN:
+            aStr<<(aVal.myBool ? "True" : "False");
+            break;
+          case ModelAPI_AttributeTables::INTEGER:
+            aStr<<aVal.myInt;
+            break;
+          case ModelAPI_AttributeTables::DOUBLE:
+            aStr<<aVal.myDouble;
+            break;
+          case ModelAPI_AttributeTables::STRING:
+            aStr<<aVal.myStr;
+            break;
+          }
+          std::string aStrVal = aStr.str();
+          aStep->setStringValue(isWholePart ? aRow : anElementID, aCol, aStrVal);
+        }
+      }
+    }
+  }
+
+
   // exporting
 
   XAOExport(theFileName, &aXao, anError);
@@ -254,7 +340,7 @@ void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName)
 
   } catch (XAO::XAO_Exception& e) {
     std::string anError = e.what();
-    setError("An error occurred while importing " + theFileName + ": " + anError);
+    setError("An error occurred while exporting " + theFileName + ": " + anError);
     return;
   }
 }
index e55c4efd9e2eeb0d86c2783e898c950cc28d76cf..2d8c81bb229a9b30aa2ef31fc677f46331de0d90 100644 (file)
@@ -23,6 +23,9 @@
 #include <ModelAPI_AttributeRefList.h>
 #include <ModelAPI_AttributeSelectionList.h>
 #include <ModelAPI_AttributeString.h>
+#include <ModelAPI_AttributeStringArray.h>
+#include <ModelAPI_AttributeIntArray.h>
+#include <ModelAPI_AttributeTables.h>
 #include <ModelAPI_BodyBuilder.h>
 #include <ModelAPI_Data.h>
 #include <ModelAPI_Document.h>
@@ -35,6 +38,8 @@
 
 #include <XAO_Xao.hxx>
 #include <XAO_Group.hxx>
+#include <XAO_Field.hxx>
+#include <XAO_Step.hxx>
 
 #include <ExchangePlugin_Tools.h>
 
@@ -147,11 +152,11 @@ void ExchangePlugin_ImportFeature::importXAO(const std::string& theFileName)
   ResultBodyPtr aResultBody = createResultBody(aGeomShape);
   setResult(aResultBody);
 
-  // Process groups
+  // Process groups/fields
   std::shared_ptr<ModelAPI_AttributeRefList> aRefListOfGroups =
       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(data()->attribute(FEATURES_ID()));
 
-  // Remove previous groups stored in RefList
+  // Remove previous groups/fields stored in RefList
   std::list<ObjectPtr> anGroupList = aRefListOfGroups->list();
   std::list<ObjectPtr>::iterator anGroupIt = anGroupList.begin();
   for (; anGroupIt != anGroupList.end(); ++anGroupIt) {
@@ -174,9 +179,9 @@ void ExchangePlugin_ImportFeature::importXAO(const std::string& theFileName)
     AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
 
     // conversion of dimension
-    XAO::Dimension aGroupDimension = aXaoGroup->getDimension();
     std::string aDimensionString = XAO::XaoUtils::dimensionToString(aXaoGroup->getDimension());
-    std::string aSelectionType = ExchangePlugin_Tools::xaoDimension2selectionType(aDimensionString);
+    std::string aSelectionType =
+      ExchangePlugin_Tools::xaoDimension2selectionType(aDimensionString);
 
     aSelectionList->setSelectionType(aSelectionType);
     for (int anElementIndex = 0; anElementIndex < aXaoGroup->count(); ++anElementIndex) {
@@ -190,6 +195,69 @@ void ExchangePlugin_ImportFeature::importXAO(const std::string& theFileName)
       aSelectionList->value(anElementIndex)->setId(aReferenceID);
     }
   }
+  // Create new fields
+  for (int aFieldIndex = 0; aFieldIndex < aXao.countFields(); ++aFieldIndex) {
+    XAO::Field* aXaoField = aXao.getField(aFieldIndex);
+
+    std::shared_ptr<ModelAPI_Feature> aFieldFeature = addFeature("Field");
+
+    // group name
+    if (!aXaoField->getName().empty())
+      aFieldFeature->data()->setName(aXaoField->getName());
+
+    // fill selection
+    AttributeSelectionListPtr aSelectionList = aFieldFeature->selectionList("selected");
+
+    // conversion of dimension
+    std::string aDimensionString = XAO::XaoUtils::dimensionToString(aXaoField->getDimension());
+    std::string aSelectionType =
+      ExchangePlugin_Tools::xaoDimension2selectionType(aDimensionString);
+    aSelectionList->setSelectionType(aSelectionType);
+    // conversion of type
+    XAO::Type aFieldType = aXaoField->getType();
+    std::string aTypeString = XAO::XaoUtils::fieldTypeToString(aFieldType);
+    ModelAPI_AttributeTables::ValueType aType =
+      ExchangePlugin_Tools::xaoType2valuesType(aTypeString);
+    // set components names
+    AttributeStringArrayPtr aComponents = aFieldFeature->stringArray("components_names");
+    aComponents->setSize(aXaoField->countComponents());
+    for(int aComp = 0; aComp < aXaoField->countComponents(); aComp++) {
+      aComponents->setValue(aComp, aXaoField->getComponentName(aComp));
+    }
+
+    AttributeIntArrayPtr aStamps = aFieldFeature->intArray("stamps");
+    aStamps->setSize(aXaoField->countSteps());
+    std::shared_ptr<ModelAPI_AttributeTables> aTables = aFieldFeature->tables("values");
+    aTables->setSize(
+      aXaoField->countElements() + 1, aXaoField->countComponents(), aXaoField->countSteps());
+    aTables->setType(aType);
+    // iterate steps
+    XAO::stepIterator aStepIter = aXaoField->begin();
+    for(int aStepIndex = 0; aStepIter != aXaoField->end(); aStepIter++, aStepIndex++) {
+      aStamps->setValue(aStepIndex, (*aStepIter)->getStamp());
+      for(int aRow = 1; aRow <= aXaoField->countElements(); aRow++) {
+        for(int aCol = 0; aCol < aXaoField->countComponents(); aCol++) {
+          ModelAPI_AttributeTables::Value aVal;
+          std::string aValStr = (*aStepIter)->getStringValue(aRow - 1, aCol);
+          switch(aType) {
+          case ModelAPI_AttributeTables::BOOLEAN:
+            aVal.myBool = aValStr == "True";
+            break;
+          case ModelAPI_AttributeTables::INTEGER:
+            aVal.myInt = atoi(aValStr.c_str());
+            break;
+          case ModelAPI_AttributeTables::DOUBLE:
+            aVal.myDouble = atof(aValStr.c_str());
+            break;
+          case ModelAPI_AttributeTables::STRING:
+            aVal.myStr = aValStr;
+            break;
+          }
+          aTables->setValue(aVal, aRow, aCol, aStepIndex);
+        }
+      }
+    }
+  }
   // Top avoid problems in Object Browser update: issue #1647.
   ModelAPI_EventCreator::get()->sendReordered(
     std::dynamic_pointer_cast<ModelAPI_Feature>(aRefListOfGroups->owner()));
index a7ec0d9bf1a92efa0fb0981e9ce795b5cee241bd..c4299c30e248f0bed5cc410e54853d33501addfe 100644 (file)
@@ -28,6 +28,8 @@ std::string ExchangePlugin_Tools::selectionType2xaoDimension(const std::string&
     return "face";
   else if (theType == "Solids" || theType == "solid")
     return "solid";
+  else if (theType == "Part" || theType == "part")
+    return "part";
 
   return std::string();
 }
@@ -46,3 +48,32 @@ std::string ExchangePlugin_Tools::xaoDimension2selectionType(const std::string&
   return std::string();
 }
 
+
+std::string ExchangePlugin_Tools::valuesType2xaoType(
+  const ModelAPI_AttributeTables::ValueType& theType)
+{
+  switch(theType) {
+  case ModelAPI_AttributeTables::BOOLEAN:
+    return "boolean";
+  case ModelAPI_AttributeTables::INTEGER:
+    return "integer";
+  case ModelAPI_AttributeTables::DOUBLE:
+    return "double";
+  case ModelAPI_AttributeTables::STRING:
+    return "string";
+  }
+  return "";
+}
+
+ModelAPI_AttributeTables::ValueType ExchangePlugin_Tools::xaoType2valuesType(std::string theType)
+{
+  if (theType == "boolean")
+    return ModelAPI_AttributeTables::BOOLEAN;
+  if (theType == "integer")
+    return ModelAPI_AttributeTables::INTEGER;
+  if (theType == "double")
+    return ModelAPI_AttributeTables::DOUBLE;
+  if (theType == "string")
+    return ModelAPI_AttributeTables::STRING;
+  return ModelAPI_AttributeTables::DOUBLE;
+}
index c481d8ddc2588b6eab4d687f0a49ef5d37334c4f..2ff2507dc289748b1420fe5f6b03a0c25b5bd3d4 100644 (file)
@@ -8,6 +8,7 @@
 #define EXCHANGEPLUGIN_TOOLS_H_
 
 #include <ExchangePlugin.h>
+#include <ModelAPI_AttributeTables.h>
 
 #include <list>
 #include <string>
@@ -28,6 +29,10 @@ public:
   /// Converts string representation of XAO dimension to selection type.
   static std::string xaoDimension2selectionType(const std::string& theDimension);
 
+  /// Converts representation of values type to XAO type.
+  static std::string valuesType2xaoType(const ModelAPI_AttributeTables::ValueType& theType);
+  /// Converts representation of values type to XAO type.
+  static ModelAPI_AttributeTables::ValueType xaoType2valuesType(std::string theType);
 };
 
 #endif /* EXCHANGEPLUGIN_TOOLS_H_ */
index 8d08cea84d5fd44fe76581113459631f915bd859..5d63f1748b3acb827d0263ddafb1e11aa2c2dc45 100644 (file)
@@ -107,7 +107,7 @@ throw(XAO_Exception)
         return XAO::FACE;
     if (dimension == "solid")
         return XAO::SOLID;
-    if (dimension == "whole")
+    if (dimension == "part")
         return XAO::WHOLE;
 
     throw XAO_Exception(MsgBuilder() << "Bad dimension: " << dimension);