Salome HOME
updated copyright message
[modules/geom.git] / src / XAOPlugin / XAOPlugin_IOperations.cxx
index f77a29aae7869facb1ee759ab2627232aae642c7..865ead17a22f433a435d01b3a00585349c59b33d 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2016  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2014-2023  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -23,6 +23,7 @@
 #include "XAOPlugin_IImportExport.hxx"
 
 // KERNEL includes
+#include <Basics_DirUtils.hxx>
 #include <utilities.h>
 #include <Utils_SALOME_Exception.hxx>
 
@@ -55,6 +56,7 @@
 #include <TColStd_HArray1OfReal.hxx>
 #include <TDataStd_Integer.hxx>
 
+#include <TopExp.hxx>
 
 XAO::Dimension shapeEnumToDimension(const TopAbs_ShapeEnum& shape)
 {
@@ -134,6 +136,9 @@ bool XAOPlugin_IOperations::exportGroups( std::list<Handle(GEOM_Object)> groupLi
     XAO::Dimension dim = shapeEnumToDimension(shapeGroup);
     XAO::Group* group = xaoObject->addGroup(dim, currGroup->GetName().ToCString());
 
+    // Group can be empty
+    if (groupIds.IsNull()) continue;
+
     switch (shapeGroup)
     {
     case TopAbs_VERTEX:
@@ -168,6 +173,8 @@ bool XAOPlugin_IOperations::exportGroups( std::list<Handle(GEOM_Object)> groupLi
         group->add(index);
       }
       break;
+    default:
+      ;
     }
   }
   return true;
@@ -175,7 +182,7 @@ bool XAOPlugin_IOperations::exportGroups( std::list<Handle(GEOM_Object)> groupLi
 
 void XAOPlugin_IOperations::exportFields( std::list<Handle(GEOM_Field)> fieldList,
                                           XAO::Xao* xaoObject,
-                                          XAO::BrepGeometry* geometry )
+                                          XAO::BrepGeometry* /*geometry*/ )
 {
   std::list<Handle(GEOM_Field)>::iterator fieldIterator = fieldList.begin();
   while (fieldIterator != fieldList.end())
@@ -267,6 +274,13 @@ void XAOPlugin_IOperations::exportSubshapes( const Handle(GEOM_Object)& shape, X
 {
   Handle(TColStd_HSequenceOfTransient) subObjects = myShapesOperations->GetExistingSubObjects( shape, GEOMImpl_IShapesOperations::SubShapes );
   int nbSubObjects = subObjects->Length();
+  if (!nbSubObjects) return;
+
+  TopoDS_Shape aMainShape = shape->GetValue();
+  if (aMainShape.IsNull()) return;
+  TopTools_IndexedMapOfShape anIndices;
+  TopExp::MapShapes(aMainShape, anIndices);
+
   // set the names of the sub shapes
   for (int i = 1; i <= nbSubObjects; i++)
   {
@@ -277,8 +291,16 @@ void XAOPlugin_IOperations::exportSubshapes( const Handle(GEOM_Object)& shape, X
     Handle(GEOM_Object) subObject = Handle(GEOM_Object)::DownCast( transientSubObject );
     if (subObject->GetType() != GEOM_GROUP)
     {
-      int subIndex = myShapesOperations->GetSubShapeIndex( shape, subObject );
-      switch (subObject->GetValue().ShapeType())
+      TopoDS_Shape aSubShape = subObject->GetValue();
+      if (aSubShape.IsNull()) continue;
+
+      // Do not call GEOMImpl_IShapesOperations::GetSubShapeIndex() here
+      // for time optimization reason (it invokes TopExp::MapShapes())
+      //int subIndex = myShapesOperations->GetSubShapeIndex( shape, subObject );
+      int subIndex = anIndices.FindIndex(aSubShape);
+      if (!subIndex) continue;
+
+      switch (aSubShape.ShapeType())
       {
       case TopAbs_VERTEX:
         geometry->changeVertexName(subIndex, subObject->GetName().ToCString());
@@ -292,6 +314,8 @@ void XAOPlugin_IOperations::exportSubshapes( const Handle(GEOM_Object)& shape, X
       case TopAbs_SOLID:
         geometry->changeSolidName(subIndex, subObject->GetName().ToCString());
         break;
+      default:
+        ;
       }
     }
   }
@@ -353,6 +377,7 @@ bool XAOPlugin_IOperations::ExportXAO( Handle(GEOM_Object) shape,
 
   // make a Python command
   GEOM::TPythonDump pd(exportFunction);
+  std::string convFileName = Kernel_Utils::BackSlashToSlash(fileName);
   pd << "exported = geompy.ExportXAO(" << shape;
 
   // list of groups
@@ -379,7 +404,7 @@ bool XAOPlugin_IOperations::ExportXAO( Handle(GEOM_Object) shape,
     }
   }
   pd << "], ";
-  pd << "\"" << author << "\", \"" << fileName << "\", \"" << shapeFileName << "\")";
+  pd << "\"" << author << "\", \"" << convFileName.c_str() << "\", \"" << shapeFileName << "\")";
 
   SetErrorCode(OK);
   delete xaoObject;
@@ -509,13 +534,19 @@ bool XAOPlugin_IOperations::ImportXAO( const char* fileName,
 
     // build an array with the indexes of the sub shapes
     int nbElt = xaoGroup->count();
-    Handle(TColStd_HArray1OfInteger) array = new TColStd_HArray1OfInteger(1, nbElt);
-    int j = 0;
-    for (std::set<int>::iterator it = xaoGroup->begin(); it != xaoGroup->end(); ++it)
-    {
-      int index = (*it);
-      std::string ref = xaoGeometry->getElementReference(xaoGroup->getDimension(), index);
-      array->SetValue(++j, XAO::XaoUtils::stringToInt(ref));
+    Handle(TColStd_HArray1OfInteger) array;
+    if (nbElt > 0) {
+      array = new TColStd_HArray1OfInteger(1, nbElt);
+      int j = 0;
+      for (std::set<int>::iterator it = xaoGroup->begin(); it != xaoGroup->end(); ++it) {
+        int index = (*it);
+        std::string ref = xaoGeometry->getElementReference(xaoGroup->getDimension(), index);
+        array->SetValue(++j, XAO::XaoUtils::stringToInt(ref));
+      }
+    }
+    else { // empty group
+      array = new TColStd_HArray1OfInteger(1, 1);
+      array->SetValue(1, -1);
     }
 
     // create the group with the array of sub shapes indexes
@@ -527,8 +558,6 @@ bool XAOPlugin_IOperations::ImportXAO( const char* fileName,
     TDF_Label freeLabel = group->GetFreeLabel();
     TDataStd_Integer::Set(freeLabel, (Standard_Integer) getGroupDimension(xaoGroup));
     groups->Append(group);
-
-    function = group->GetLastFunction();
   }
 
   // create the fields
@@ -666,8 +695,9 @@ bool XAOPlugin_IOperations::ImportXAO( const char* fileName,
       pd << obj << ((i < nbFields) ? ", " : "");
     }
   }
+  std::string convFileName =  Kernel_Utils::BackSlashToSlash( fileName );
   pd << "]";
-  pd << ") = geompy.ImportXAO(\"" << fileName << "\")";
+  pd << ") = geompy.ImportXAO(\"" << convFileName.c_str() << "\")";
 
   delete xaoObject;
   SetErrorCode(OK);