]> SALOME platform Git repositories - modules/shaper.git/commitdiff
Salome HOME
Make export XAO with groups
authorspo <sergey.pokhodenko@opencascade.com>
Mon, 30 Nov 2015 13:10:03 +0000 (16:10 +0300)
committerspo <sergey.pokhodenko@opencascade.com>
Mon, 20 Jun 2016 12:07:39 +0000 (15:07 +0300)
src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp
src/ExchangePlugin/ExchangePlugin_ExportFeature.h
src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp
src/ExchangePlugin/ExchangePlugin_Tools.cpp
src/ExchangePlugin/ExchangePlugin_Tools.h
src/ExchangePlugin/export_widget.xml
src/GeomAlgoAPI/GeomAlgoAPI_XAOExport.cpp
src/GeomAlgoAPI/GeomAlgoAPI_XAOExport.h

index a341a001558d9372717de629895419307edff34c..437bae8088533ed679350f69638f98ce71211e25 100644 (file)
 #include <ModelAPI_Document.h>
 #include <ModelAPI_Object.h>
 #include <ModelAPI_ResultBody.h>
+#include <ModelAPI_ResultGroup.h>
 #include <ModelAPI_Session.h>
 #include <ModelAPI_Validator.h>
 
+#include <XAO_Group.hxx>
 #include <XAO_Xao.hxx>
 
 #include <ExchangePlugin_Tools.h>
@@ -59,6 +61,7 @@ void ExchangePlugin_ExportFeature::initAttributes()
   data()->addAttribute(ExchangePlugin_ExportFeature::SELECTION_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
   data()->addAttribute(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID(), ModelAPI_AttributeString::typeId());
 
+  ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ExchangePlugin_ExportFeature::SELECTION_LIST_ID());
   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ExchangePlugin_ExportFeature::XAO_AUTHOR_ID());
 }
 
@@ -70,9 +73,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());
@@ -80,32 +80,11 @@ 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) {
-    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);
-  }
-
-  exportFile(aFilePath, aFormat, aShape);
+  exportFile(aFilePath, aFormat);
 }
 
 void ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
-                                              const std::string& theFormat,
-                                              std::shared_ptr<GeomAPI_Shape> theShape)
+                                              const std::string& theFormat)
 {
   std::string aFormatName = theFormat;
 
@@ -126,19 +105,40 @@ void ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
   }
 
   if (aFormatName == "XAO") {
-    exportXAO(theFileName, theShape);
+    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;
   bool aResult = false;
   if (aFormatName == "BREP") {
-    aResult = BREPExport(theFileName, aFormatName, theShape, anError);
+    aResult = BREPExport(theFileName, aFormatName, aShape, anError);
   } else if (aFormatName == "STEP") {
-    aResult = STEPExport(theFileName, aFormatName, theShape, anError);
+    aResult = STEPExport(theFileName, aFormatName, aShape, anError);
   } else if (aFormatName.substr(0, 4) == "IGES") {
-    aResult = IGESExport(theFileName, aFormatName, theShape, anError);
+    aResult = IGESExport(theFileName, aFormatName, aShape, anError);
   } else {
     anError = "Unsupported format: " + aFormatName;
   }
@@ -149,19 +149,85 @@ void ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
   }
 }
 
-void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName,
-                                             std::shared_ptr<GeomAPI_Shape> theShape)
+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;
+  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());
+  }
+  GeomShapePtr aShape = (aShapes.size() == 1)
+      ? *aShapes.begin()
+      : GeomAlgoAPI_CompoundBuilder::compound(aShapes);
 
-  XAO::Xao aXao(anAuthor, "1.0");
+  SetShapeToXAO(aShape, &aXao, anError);
 
-  std::string anError;
-  XAOExport(theFileName, theShape, &aXao, anError);
+  if (!anError.empty()) {
+    setError("An error occurred while exporting " + theFileName + ": " + anError);
+    return;
+  }
+
+  // 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());
+
+    for (int aSelectionIndex = 0; aSelectionIndex < aSelectionList->size(); ++aSelectionIndex) {
+      AttributeSelectionPtr aSelection = aSelectionList->value(aSelectionIndex);
+
+      // complex conversion of reference id to element index
+      int aReferenceID = aSelection->Id();
+      std::string aReferenceString = XAO::XaoUtils::intToString(aReferenceID);
+      int anElementID = aXao.getGeometry()->getElementIndexByReference(aGroupDimension, aReferenceString);
+
+      aXaoGroup->add(anElementID);
+    }
+  }
+
+  // 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 importing " + theFileName + ": " + anError);
+    return;
+  }
+}
index 9a71b05618ae5106b7e886a78b94b10a995c7618..8b57ed612ee6720d0486a35a0bcd494fd7690959 100644 (file)
@@ -85,12 +85,10 @@ public:
 protected:
   /// Performs export of the file
   EXCHANGEPLUGIN_EXPORT void exportFile(const std::string& theFileName,
-                                        const std::string& theFormat,
-                                        std::shared_ptr<GeomAPI_Shape> theShape);
+                                        const std::string& theFormat);
 
   /// Performs export to XAO file
-  EXCHANGEPLUGIN_EXPORT void exportXAO(const std::string& theFileName,
-                                       std::shared_ptr<GeomAPI_Shape> theShape);
+  EXCHANGEPLUGIN_EXPORT void exportXAO(const std::string& theFileName);
 };
 
 #endif /* EXPORT_EXPORTFEATURE_H_ */
index 8e50157e647fc1966aac1aaa37f2bf75f579c951..cecca95cf75e00d09adee6d2d1f86cd9ca9e6eed 100644 (file)
@@ -40,6 +40,8 @@
 #include <XAO_Xao.hxx>
 #include <XAO_Group.hxx>
 
+#include <ExchangePlugin_Tools.h>
+
 ExchangePlugin_ImportFeature::ExchangePlugin_ImportFeature()
 {
 }
@@ -122,6 +124,7 @@ void ExchangePlugin_ImportFeature::importFile(const std::string& theFileName)
 
 void ExchangePlugin_ImportFeature::importXAO(const std::string& theFileName)
 {
+  try {
   std::string anError;
 
   XAO::Xao aXao;
@@ -168,7 +171,13 @@ void ExchangePlugin_ImportFeature::importXAO(const std::string& theFileName)
 
     // fill selection
     AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
-    aSelectionList->setSelectionType(XAO::XaoUtils::dimensionToString(aXaoGroup->getDimension()));
+
+    // conversion of dimension
+    XAO::Dimension aGroupDimension = aXaoGroup->getDimension();
+    std::string aDimensionString = XAO::XaoUtils::dimensionToString(aXaoGroup->getDimension());
+    std::string aSelectionType = ExchangePlugin_Tools::xaoDimension2selectionType(aDimensionString);
+
+    aSelectionList->setSelectionType(aSelectionType);
     for (int anElementIndex = 0; anElementIndex < aXaoGroup->count(); ++anElementIndex) {
       aSelectionList->append(aResultBody, GeomShapePtr());
       // complex conversion of element index to reference id
@@ -184,6 +193,12 @@ void ExchangePlugin_ImportFeature::importXAO(const std::string& theFileName)
 
     document()->setCurrentFeature(aGroupFeature, true);
   }
+
+  } catch (XAO::XAO_Exception& e) {
+    std::string anError = e.what();
+    setError("An error occurred while importing " + theFileName + ": " + anError);
+    return;
+  }
 }
 
 //============================================================================
index 26391487d7886ed4d7d074f78a4d9d2946b26f8a..51f22b82bb7792b56cfc6bf3a0bdbfe7d849d86f 100644 (file)
@@ -17,3 +17,32 @@ std::list<std::string> ExchangePlugin_Tools::split(const std::string& theString,
     theResult.push_back(aSection);
   return theResult;
 }
+
+std::string ExchangePlugin_Tools::selectionType2xaoDimension(const std::string& theType)
+{
+  if (theType == "Vertices")
+    return "vertex";
+  else if (theType == "Edges")
+    return "edge";
+  else if (theType == "Faces")
+    return "face";
+  else if (theType == "Solids")
+    return "solid";
+
+  return std::string();
+}
+
+std::string ExchangePlugin_Tools::xaoDimension2selectionType(const std::string& theDimension)
+{
+  if (theDimension == "vertex")
+    return "Vertices";
+  else if (theDimension == "edge")
+    return "Edges";
+  else if (theDimension == "face")
+    return "Faces";
+  else if (theDimension == "solid")
+    return "Solids";
+
+  return std::string();
+}
+
index 58d0dd19fd26b47815aa0eee85511a8c1679563c..c481d8ddc2588b6eab4d687f0a49ef5d37334c4f 100644 (file)
@@ -22,6 +22,12 @@ public:
   static std::list<std::string> split(const std::string& theString,
                                       char theDelimiter);
 
+  /// Converts string representation of selection type to XAO dimension.
+  static std::string selectionType2xaoDimension(const std::string& theString);
+
+  /// Converts string representation of XAO dimension to selection type.
+  static std::string xaoDimension2selectionType(const std::string& theDimension);
+
 };
 
 #endif /* EXCHANGEPLUGIN_TOOLS_H_ */
index 2709a7264e62ca5899270c2b6596634d1ac3922b..d00baf07b66282151363a1e99e82e0cf08a5715d 100644 (file)
         <validator id="ExchangePlugin_ExportFormat"
                    parameters="BREP|BRP:BREP,STEP|STP:STEP,IGES|IGS:IGES-5.1,IGES|IGS:IGES-5.3" />
       </export_file_selector>
+      <multi_selector id="selection_list" 
+                      tooltip="Select a set of objects" 
+                      type_choice="Vertices Edges Faces Solids Objects">
+        <validator id="GeomValidators_Finite"/>
+      </multi_selector>
     </case>
     <case id="XAO" title="XAO">
       <export_file_selector id="file_path"
       </stringvalue>
     </case>
   </switch>
-  <multi_selector id="selection_list" 
-                  tooltip="Select a set of objects" 
-                  type_choice="Vertices Edges Faces Solids Objects"
-                  use_choice="true">
-    <validator id="GeomValidators_Finite"/>
-  </multi_selector>
 </source>
index 6bd8127920b90a48a834500a15a630c1eaabe1a0..8bf37d5383197eb73faeb52e1a9f286b49a81418 100644 (file)
 #include <XAO_XaoExporter.hxx>
 #include <XAO_BrepGeometry.hxx>
 
+//=============================================================================
+bool SetShapeToXAO(const std::shared_ptr<GeomAPI_Shape>& theShape,
+                   XAO::Xao* theXao,
+                   std::string& theError)
+{
+  if (!theShape.get() || !theXao) {
+    theError = "An invalid argument.";
+    return false;
+  }
+
+  TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
+  try {
+    XAO::BrepGeometry* aGeometry = new XAO::BrepGeometry;
+    theXao->setGeometry(aGeometry);
+    aGeometry->setTopoDS_Shape(aShape);
+  } catch (XAO::XAO_Exception& e) {
+    theError = e.what();
+    return false;
+  }
+  return true;
+}
+
 //=============================================================================
 /*!
  *
  */
 //=============================================================================
 bool XAOExport(const std::string& theFileName,
-               const std::shared_ptr<GeomAPI_Shape>& theShape,
                XAO::Xao* theXao,
                std::string& theError)
 {
@@ -27,16 +48,12 @@ bool XAOExport(const std::string& theFileName,
   std::cout << "Export XAO into file " << theFileName << std::endl;
   #endif
 
-  if (theFileName.empty() || !theShape.get() || !theXao) {
+  if (theFileName.empty() || !theXao) {
     theError = "An invalid argument.";
     return false;
   }
 
-  TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
   try {
-    XAO::BrepGeometry* aGeometry = new XAO::BrepGeometry;
-    theXao->setGeometry(aGeometry);
-    aGeometry->setTopoDS_Shape(aShape);
     XAO::XaoExporter::saveToFile(theXao, theFileName);
   } catch (XAO::XAO_Exception& e) {
     theError = e.what();
index 5f535cfec8110e0f6b9157b8f3f256069a40b704..4ad789272d8b29195d80273716309d3ee59b69ba 100644 (file)
@@ -17,10 +17,15 @@ namespace XAO {
 class Xao;
 } // namespace XAO
 
+/// Defines shape for the XAO object
+GEOMALGOAPI_EXPORT
+bool SetShapeToXAO(const std::shared_ptr<GeomAPI_Shape>& theShape,
+                   XAO::Xao* theXao,
+                   std::string& theError);
+
 /// Implementation of the export XAO files algorithms
 GEOMALGOAPI_EXPORT
 bool XAOExport(const std::string& theFileName,
-               const std::shared_ptr<GeomAPI_Shape>& theShape,
                XAO::Xao* theXao,
                std::string& theError);