Salome HOME
Issue #2998: Add help description for automatic creation of constraints
[modules/shaper.git] / src / ExchangePlugin / ExchangePlugin_ExportFeature.cpp
index 688ffc03b289b9903822431dba9871bcc3c1ea33..bf677c743730ed28aecade2876c3acdfd2e9c675 100644 (file)
@@ -38,6 +38,7 @@
 #include <GeomAlgoAPI_XAOExport.h>
 
 #include <GeomAPI_Shape.h>
+#include <GeomAPI_ShapeExplorer.h>
 
 #include <ModelAPI_AttributeSelectionList.h>
 #include <ModelAPI_AttributeString.h>
@@ -141,8 +142,6 @@ void 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;
     }
@@ -167,12 +166,8 @@ void ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
   }
 
   // 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);
-  }
+  std::shared_ptr<GeomAPI_Shape> aShape =
+    aShapes.size() == 1 ? aShapes.front() : GeomAlgoAPI_CompoundBuilder::compound(aShapes);
 
   // Perform the export
   std::string anError;
@@ -261,8 +256,42 @@ static bool isInResults(AttributeSelectionListPtr theSelection,
   // if context is in results, return true
   for(int a = 0; a < theSelection->size(); a++) {
     AttributeSelectionPtr anAttr = theSelection->value(a);
-    ResultBodyPtr aSelected= std::dynamic_pointer_cast<ModelAPI_ResultBody>(anAttr->context());
-    if (aSelected.get() && theCashedResults.count(aSelected))
+    ResultPtr aContext = anAttr->context();
+    // check is it group selected for groups BOP
+    if (aContext.get() && aContext->groupName() == ModelAPI_ResultGroup::group()) {
+      // it is impossible by used results check which result is used in this group result,
+      // so check the results shapes is it in results of this document or not
+      FeaturePtr aSelFeature =
+        std::dynamic_pointer_cast<ModelAPI_Feature>(theSelection->owner());
+      if (!aSelFeature.get() || aSelFeature->results().empty())
+        continue;
+      GeomShapePtr aGroupResShape = aSelFeature->firstResult()->shape();
+
+      std::set<ResultPtr>::iterator allResultsIter = theCashedResults.begin();
+      for(; allResultsIter != theCashedResults.end(); allResultsIter++) {
+        GeomShapePtr aResultShape = (*allResultsIter)->shape();
+
+        GeomAPI_Shape::ShapeType aType =
+          GeomAPI_Shape::shapeTypeByStr(theSelection->selectionType());
+        GeomAPI_ShapeExplorer aGroupResExp(aGroupResShape, aType);
+        for(; aGroupResExp.more(); aGroupResExp.next()) {
+          if (aResultShape->isSubShape(aGroupResExp.current(), false))
+            return true; // at least one shape of the group is in the used results
+        }
+      }
+    }
+    ResultBodyPtr aSelected = std::dynamic_pointer_cast<ModelAPI_ResultBody>(anAttr->context());
+    if (!aSelected.get()) { // try to get selected feature and all its results
+      FeaturePtr aContextFeature = anAttr->contextFeature();
+      if (aContextFeature.get() && !aContextFeature->results().empty()) {
+        const std::list<ResultPtr>& allResluts = aContextFeature->results();
+        std::list<ResultPtr>::const_iterator aResIter = allResluts.cbegin();
+        for(; aResIter != allResluts.cend(); aResIter++) {
+          if (aResIter->get() && theCashedResults.count(*aResIter))
+            return true;
+        }
+      }
+    } else if (aSelected.get() && theCashedResults.count(aSelected))
       return true;
   }
   return false;
@@ -362,6 +391,8 @@ void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName)
     for (int aGroupIndex = 0; aGroupIndex < aGroupCount; ++aGroupIndex) {
       ResultGroupPtr aResultGroup = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(
           (*aDoc)->object(ModelAPI_ResultGroup::group(), aGroupIndex));
+      if (!aResultGroup.get() || !aResultGroup->shape().get())
+        continue;
 
       FeaturePtr aGroupFeature = (*aDoc)->feature(aResultGroup);
 
@@ -372,6 +403,7 @@ void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName)
 
       // conversion of dimension
       std::string aSelectionType = aSelectionList->selectionType();
+      GeomAPI_Shape::ShapeType aSelType = GeomAPI_Shape::shapeTypeByStr(aSelectionType);
       std::string aDimensionString =
         ExchangePlugin_Tools::selectionType2xaoDimension(aSelectionType);
       XAO::Dimension aGroupDimension = XAO::XaoUtils::stringToDimension(aDimensionString);
@@ -380,23 +412,14 @@ void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName)
                                             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());
-
+        GeomAPI_ShapeExplorer aGroupResExplorer(aResultGroup->shape(), aSelType);
+        for(; aGroupResExplorer.more(); aGroupResExplorer.next()) {
+          int aReferenceID = GeomAlgoAPI_CompoundBuilder::id(aShape, aGroupResExplorer.current());
           if (aReferenceID == 0) // selected value does not found in the exported shape
             continue;
-
           std::string aReferenceString = XAO::XaoUtils::intToString(aReferenceID);
           int anElementID =
             aXao.getGeometry()->getElementIndexByReference(aGroupDimension, aReferenceString);
-
           aXaoGroup->add(anElementID);
         }
       } catch (XAO::XAO_Exception& e) {
@@ -452,8 +475,8 @@ void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName)
 
         AttributeIntArrayPtr aStamps = aFieldFeature->intArray("stamps");
         for (int aStepIndex = 0; aStepIndex < aTables->tables(); aStepIndex++) {
-          XAO::Step* aStep = aXaoField->addNewStep(aStepIndex);
-          aStep->setStep(aStepIndex);
+          XAO::Step* aStep = aXaoField->addNewStep(aStepIndex + 1);
+          aStep->setStep(aStepIndex + 1);
           int aStampIndex = aStamps->value(aStepIndex);
           aStep->setStamp(aStampIndex);
           int aNumElements = isWholePart ? aXaoField->countElements() : aTables->rows();
@@ -535,18 +558,14 @@ bool ExchangePlugin_ExportFeature::isMacro() const
     return false;
   ExchangePlugin_ExportFeature* aThis = ((ExchangePlugin_ExportFeature*)(this));
   AttributeStringPtr aFormatAttr = aThis->string(FILE_FORMAT_ID());
-  if (!aFormatAttr.get())
-    return false;
-  std::string aFormat = aFormatAttr->value();
+  std::string aFormat(aFormatAttr.get() ? aFormatAttr->value() : "");
 
   if (aFormat.empty()) { // get default format for the extension
     AttributeStringPtr aFilePathAttr = aThis->string(FILE_PATH_ID());
     std::string aFilePath = aFilePathAttr->value();
     if (!aFilePath.empty()) {
       std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(aFilePath);
-      if (anExtension == "XAO") {
-        aFormat = "XAO";
-      }
+      aFormat = anExtension;
     }
   }