Salome HOME
Add an Angle option in the cylinder primitive in order to build portion of cylinders...
[modules/geom.git] / src / GEOMImpl / GEOMImpl_IInsertOperations.cxx
old mode 100644 (file)
new mode 100755 (executable)
index e48760f..86e7566
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -6,7 +6,7 @@
 // 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.
+// 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
 #include <TFunction_DriverTable.hxx>
 #include <TFunction_Driver.hxx>
 #include <TFunction_Logbook.hxx>
+#include <TDF_ChildIDIterator.hxx>
 #include <TDF_Tool.hxx>
 #include <TDataStd_Integer.hxx>
+#include <TNaming_NamedShape.hxx>
+#include <TDataStd_Comment.hxx>
+#include <TopTools_IndexedMapOfShape.hxx>
+#include <TopExp.hxx>
 
 #include <TopoDS.hxx>
 #include <TopoDS_Vertex.hxx>
 static TCollection_AsciiString GetImportFormatName
         (const TCollection_AsciiString& theFormatName)
 {
-  Standard_Integer aLastInd = 4;
-
-  if (theFormatName.Search("STL") == 1) {
-    aLastInd = 3;
-  }
-
-  return theFormatName.SubString(1, aLastInd);
+  return theFormatName.Token("_");
 }
 
 //=============================================================================
@@ -244,7 +243,7 @@ void GEOMImpl_IInsertOperations::Export
  *  Import
  */
 //=============================================================================
-Handle(GEOM_Object) GEOMImpl_IInsertOperations::Import
+Handle(TColStd_HSequenceOfTransient) GEOMImpl_IInsertOperations::Import
                                  (const TCollection_AsciiString& theFileName,
                                   const TCollection_AsciiString& theFormatName)
 {
@@ -253,19 +252,21 @@ Handle(GEOM_Object) GEOMImpl_IInsertOperations::Import
   if (theFileName.IsEmpty() || theFormatName.IsEmpty()) return NULL;
 
   //Add a new result object
-  Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
+  Handle(GEOM_Object) anImported = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
 
   //Add an Import function
-  Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_ImportDriver::GetID(), IMPORT_SHAPE);
-  if (aFunction.IsNull()) return result;
+  Handle(GEOM_Function) aFunction =
+    anImported->AddFunction(GEOMImpl_ImportDriver::GetID(), IMPORT_SHAPE);
+
+  if (aFunction.IsNull()) return NULL;
 
   //Check if the function is set correctly
-  if (aFunction->GetDriverGUID() != GEOMImpl_ImportDriver::GetID()) return result;
+  if (aFunction->GetDriverGUID() != GEOMImpl_ImportDriver::GetID()) return NULL;
 
   Handle(TCollection_HAsciiString) aHLibName;
   if (!IsSupported
           (Standard_True, GetImportFormatName(theFormatName), aHLibName)) {
-    return result;
+    return NULL;
   }
   TCollection_AsciiString aLibName = aHLibName->String();
 
@@ -276,6 +277,8 @@ Handle(GEOM_Object) GEOMImpl_IInsertOperations::Import
   aCI.SetPluginName(aLibName);
 
   //Perform the Import
+  Handle(TColStd_HSequenceOfTransient) aSeq = new TColStd_HSequenceOfTransient;
+
   try {
 #if OCC_VERSION_LARGE > 0x06010000
     OCC_CATCH_SIGNALS;
@@ -284,6 +287,11 @@ Handle(GEOM_Object) GEOMImpl_IInsertOperations::Import
       SetErrorCode("Import driver failed");
       return NULL;
     }
+
+    aSeq->Append(anImported);
+
+    // Greate material groups.
+    MakeMaterialGroups(anImported, aSeq);
   }
   catch (Standard_Failure) {
     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
@@ -295,17 +303,17 @@ Handle(GEOM_Object) GEOMImpl_IInsertOperations::Import
   if (theFormatName != "IGES_UNIT") {
     GEOM::TPythonDump pd (aFunction);
     if (theFormatName == "BREP")
-      pd << result << " = geompy.ImportBREP(\"" << theFileName.ToCString() << "\")";
+      pd << aSeq << " = geompy.ImportBREP(\"" << theFileName.ToCString() << "\")";
     else if (theFormatName == "IGES")
-      pd << result << " = geompy.ImportIGES(\"" << theFileName.ToCString() << "\")";
+      pd << aSeq << " = geompy.ImportIGES(\"" << theFileName.ToCString() << "\")";
     else if (theFormatName == "IGES_SCALE")
-      pd << result << " = geompy.ImportIGES(\"" << theFileName.ToCString() << "\", True)";
+      pd << aSeq << " = geompy.ImportIGES(\"" << theFileName.ToCString() << "\", True)";
     else if (theFormatName == "STEP")
-      pd << result << " = geompy.ImportSTEP(\"" << theFileName.ToCString() << "\")";
+      pd << aSeq << " = geompy.ImportSTEP(\"" << theFileName.ToCString() << "\")";
     else if (theFormatName == "STEP_SCALE")
-      pd << result << " = geompy.ImportSTEP(\"" << theFileName.ToCString() << "\", True)";
+      pd << aSeq << " = geompy.ImportSTEP(\"" << theFileName.ToCString() << "\", True)";
     else {
-      pd << result << " = geompy.ImportFile(\""
+      pd << aSeq << " = geompy.ImportFile(\""
          << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
     }
   }
@@ -328,7 +336,7 @@ Handle(GEOM_Object) GEOMImpl_IInsertOperations::Import
   }
   // OLD CODE: end
 
-  return result;
+  return aSeq;
 }
 
 //=============================================================================
@@ -384,56 +392,46 @@ Standard_Boolean GEOMImpl_IInsertOperations::ImportTranslators
 
   if (!InitResMgr()) return Standard_False;
 
-  // Read Import formats list from install directory
-  if (myResMgr->Find("Import")) {
-    TCollection_AsciiString aFormats (myResMgr->Value("Import"));
-    TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
-    int i = 1;
-    for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
-      theFormats->Append(aToken);
-    }
-  }
-
-  // Read Import formats from user directory
-  if (myResMgrUser->Find("Import")) {
-    TCollection_AsciiString aFormats (myResMgrUser->Value("Import"));
-    TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
-    int i = 1;
-    for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
-      int aLenFormats = theFormats->Length();
-      bool isFound = false;
-      for(int aInd=1;aInd<=aLenFormats;aInd++){
-        if( theFormats->Value(aInd) == aToken){
-          isFound = true;
-          break;
+  // Read Import formats from directories
+  Handle(Resource_Manager) aResMgr;
+  Handle(TColStd_HSequenceOfAsciiString) aFormatsToAdd;
+  for(int index = 0; index < myResMgrList.size(); index++) {
+    int anOldLen = theFormats->Length();
+    aResMgr = myResMgrList.at(index);
+    if (aResMgr->Find("Import")) {
+      TCollection_AsciiString aFormats (aResMgr->Value("Import"));
+      TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
+      for (int i = 1; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
+        int aLenFormats = theFormats->Length();
+        bool isFound = false;
+        for(int aInd=1;aInd<=aLenFormats;aInd++){
+          if( theFormats->Value(aInd) == aToken ){
+            isFound = true;
+            break;
+          }
         }
+        if(!isFound)
+          theFormats->Append(aToken);
       }
-      if(!isFound)
-        theFormats->Append(aToken);
     }
-  }
 
-  // Read Patterns for each supported format
-  int j = 1, len = theFormats->Length();
-  for (; j <= len; j++) {
-    TCollection_AsciiString aKey, aPattern;
-    aKey = theFormats->Value(j) + ".ImportPattern";
-    if (myResMgr->Find(aKey.ToCString()))
-      aPattern = myResMgr->Value(aKey.ToCString());
-    else if(myResMgrUser->Find(aKey.ToCString()))
-      aPattern = myResMgrUser->Value(aKey.ToCString());
-    else {
-      aKey = theFormats->Value(j) + ".Pattern";
-      if (myResMgr->Find(aKey.ToCString()))
-        aPattern = myResMgr->Value(aKey.ToCString());
-      else if(myResMgrUser->Find(aKey.ToCString()))
-        aPattern = myResMgrUser->Value(aKey.ToCString());
+    // Read Patterns for each supported format
+    for (int j = anOldLen+1; j <= theFormats->Length(); j++) {
+      TCollection_AsciiString aKey, aPattern;
+      aKey = theFormats->Value(j) + ".ImportPattern";
+      if (aResMgr->Find(aKey.ToCString()))
+        aPattern = aResMgr->Value(aKey.ToCString());
       else {
-        aPattern = theFormats->Value(j);
-        aPattern += " Files ( *.* )";
+        aKey = theFormats->Value(j) + ".Pattern";
+        if (aResMgr->Find(aKey.ToCString()))
+          aPattern = aResMgr->Value(aKey.ToCString());
+        else {
+          aPattern = theFormats->Value(j);
+          aPattern += " Files ( *.* )";
+        }
       }
+      thePatterns->Append(aPattern);
     }
-    thePatterns->Append(aPattern);
   }
 
   return (!theFormats->IsEmpty());
@@ -460,56 +458,45 @@ Standard_Boolean GEOMImpl_IInsertOperations::ExportTranslators
 
   if (!InitResMgr()) return Standard_False;
 
-  // Read Export formats list from install directory
-  if (myResMgr->Find("Export")) {
-    TCollection_AsciiString aFormats (myResMgr->Value("Export"));
-    TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
-    int i = 1;
-    for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
-      theFormats->Append(aToken);
-    }
-  }
-
-  // Read Export formats list from user directory
-  if (myResMgrUser->Find("Export")) {
-    TCollection_AsciiString aFormats (myResMgrUser->Value("Export"));
-    TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
-    int i = 1;
-    for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
-      int aLenFormats = theFormats->Length();
-      bool isFound = false;
-      for(int aInd=1;aInd<=aLenFormats;aInd++){
-        if( theFormats->Value(aInd) == aToken){
-          isFound = true;
-          break;
+  // Read Export formats list from directories
+  Handle(Resource_Manager) aResMgr;
+  for(int index=0; index < myResMgrList.size(); index++) {
+    int anOldLen = theFormats->Length();
+    aResMgr = myResMgrList.at(index);
+    if (aResMgr->Find("Export")) {
+      TCollection_AsciiString aFormats (aResMgr->Value("Export"));
+      TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
+      for (int i = 1; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
+        int aLenFormats = theFormats->Length();
+        bool isFound = false;
+        for(int aInd=1;aInd<=aLenFormats;aInd++){
+          if( theFormats->Value(aInd) == aToken){
+            isFound = true;
+            break;
+          }
         }
+        if(!isFound)
+          theFormats->Append(aToken);
       }
-      if(!isFound)
-        theFormats->Append(aToken);
     }
-  }
 
-  // Read Patterns for each supported format
-  int j = 1, len = theFormats->Length();
-  for (; j <= len; j++) {
-    TCollection_AsciiString aKey, aPattern;
-    aKey = theFormats->Value(j) + ".ExportPattern";
-    if (myResMgr->Find(aKey.ToCString()))
-      aPattern = myResMgr->Value(aKey.ToCString());
-    else if (myResMgrUser->Find(aKey.ToCString()))
-      aPattern = myResMgrUser->Value(aKey.ToCString());
-    else {
-      aKey = theFormats->Value(j) + ".Pattern";
-      if (myResMgr->Find(aKey.ToCString()))
-        aPattern = myResMgr->Value(aKey.ToCString());
-      else if (myResMgrUser->Find(aKey.ToCString()))
-        aPattern = myResMgrUser->Value(aKey.ToCString());
+    // Read Patterns for each supported format
+    for (int j = anOldLen+1; j <= theFormats->Length(); j++) {
+      TCollection_AsciiString aKey, aPattern;
+      aKey = theFormats->Value(j) + ".ExportPattern";
+      if (aResMgr->Find(aKey.ToCString()))
+        aPattern = aResMgr->Value(aKey.ToCString());
       else {
-        aPattern = theFormats->Value(j);
-        aPattern += " Files ( *.* )";
+        aKey = theFormats->Value(j) + ".Pattern";
+        if (aResMgr->Find(aKey.ToCString()))
+          aPattern = aResMgr->Value(aKey.ToCString());
+        else {
+          aPattern = theFormats->Value(j);
+          aPattern += " Files ( *.* )";
+        }
       }
+      thePatterns->Append(aPattern);
     }
-    thePatterns->Append(aPattern);
   }
 
   return (!theFormats->IsEmpty());
@@ -533,48 +520,29 @@ Standard_Boolean GEOMImpl_IInsertOperations::IsSupported
   if (isImport) aMode = "Import";
   else aMode = "Export";
 
-  // Read supported formats for the certain mode from install directory
-  if (myResMgr->Find(aMode.ToCString())) {
-    TCollection_AsciiString aFormats (myResMgr->Value(aMode.ToCString()));
-    if (aFormats.Search(theFormat) > -1) {
-      // Read library name for the supported format
-      TCollection_AsciiString aKey (theFormat);
-      aKey += ".";
-      aKey += aMode;
-      if (myResMgr->Find(aKey.ToCString())) {
-        TCollection_AsciiString aLibName (myResMgr->Value(aKey.ToCString()));        
-#ifndef WIN32
-       if ( aLibName.Length() > 3 && aLibName.SubString(1,3) != "lib" )
-         aLibName.Prepend("lib");
-        aLibName += ".so";
-#else
-        aLibName += ".dll";
-#endif
-        theLibName = new TCollection_HAsciiString (aLibName);
-        return Standard_True;
-      }
-    }
-  }
-  
   // Read supported formats for the certain mode from user directory
-  if (myResMgrUser->Find(aMode.ToCString())) {
-    TCollection_AsciiString aFormats (myResMgrUser->Value(aMode.ToCString()));
-    if (aFormats.Search(theFormat) > -1) {
-      // Read library name for the supported format
-      TCollection_AsciiString aKey (theFormat);
-      aKey += ".";
-      aKey += aMode;
-      if (myResMgrUser->Find(aKey.ToCString())) {
-        TCollection_AsciiString aLibName (myResMgrUser->Value(aKey.ToCString()));
-#ifndef WIN32
-       if ( aLibName.Length() > 3 && aLibName.SubString(1,3) != "lib" )
-         aLibName.Prepend("lib");
-        aLibName += ".so";
-#else
-        aLibName += ".dll";
-#endif
-        theLibName = new TCollection_HAsciiString (aLibName);
-        return Standard_True;
+  Handle(Resource_Manager) aResMgr;
+  for(int index=0; index < myResMgrList.size(); index++) {
+    aResMgr = myResMgrList.at(index);
+    if (aResMgr->Find(aMode.ToCString())) {
+      TCollection_AsciiString aFormats (aResMgr->Value(aMode.ToCString()));
+      if (aFormats.Search(theFormat) > -1) {
+        // Read library name for the supported format
+        TCollection_AsciiString aKey (theFormat);
+        aKey += ".";
+        aKey += aMode;
+        if (aResMgr->Find(aKey.ToCString())) {
+          TCollection_AsciiString aLibName (aResMgr->Value(aKey.ToCString()));
+  #ifndef WIN32
+         if ( aLibName.Length() > 3 && aLibName.SubString(1,3) != "lib" )
+           aLibName.Prepend("lib");
+          aLibName += ".so";
+  #else
+          aLibName += ".dll";
+  #endif
+          theLibName = new TCollection_HAsciiString (aLibName);
+          return Standard_True;
+        }
       }
     }
   }
@@ -589,67 +557,58 @@ Standard_Boolean GEOMImpl_IInsertOperations::IsSupported
 //=============================================================================
 Standard_Boolean GEOMImpl_IInsertOperations::InitResMgr()
 {
-  bool isResourceFound     = false;
-  bool isResourceFoundUser = false;
-  TCollection_AsciiString aUserResDir,aResDir;
+  bool isResourceFound = false;
+  TCollection_AsciiString aNull;
   
-  if (myResMgr.IsNull()) {
-    // Initialize the Resource Manager
-    TCollection_AsciiString aNull;
-    aResDir = TCollection_AsciiString(getenv("GEOM_ROOT_DIR"));
+  myResMgrList.clear();
+
+  // Initialize the GEOM Resource Manager
+  TCollection_AsciiString aResDir;
+  aResDir = getenv("GEOM_ROOT_DIR");
 #ifdef WIN32
-    aResDir += "\\share\\salome\\resources\\geom";
+  aResDir += "\\share\\salome\\resources\\geom";
 #else
-    aResDir += "/share/salome/resources/geom";
+  aResDir += "/share/salome/resources/geom";
 #endif
-    
-    myResMgr = new Resource_Manager ("ImportExport", aResDir, aNull, Standard_False);
-
-    isResourceFound = true;
-    if (!myResMgr->Find("Import") && !myResMgr->Find("Export")) {
-      // instead of complains in Resource_Manager
-      isResourceFound = false;
-      INFOS("No valid file \"ImportExport\" found in " << aResDir.ToCString());
-    }
-  } else
+  Handle(Resource_Manager) aGeomResMgr = new Resource_Manager ("ImportExport", aResDir, aNull, Standard_False);
+  if ( aGeomResMgr->Find("Import") || aGeomResMgr->Find("Export") ) {
+    myResMgrList.push_back( aGeomResMgr );
     isResourceFound = true;
+  }
 
-  if (myResMgrUser.IsNull()) {
-    char * dir = getenv("GEOM_ENGINE_RESOURCES_DIR");
-    TCollection_AsciiString aNull;
-    if ( dir )
-    {
-      aUserResDir = dir;
+  // Initialize the user's Resource Manager
+  TCollection_AsciiString aResDirsStr;
+  aResDirsStr = getenv("GEOM_ENGINE_RESOURCES_DIR");
+  if ( !aResDirsStr.IsEmpty() )
+  {
+    std::string aSep = ":";
+#ifdef WIN32
+    aSep = ";";
+#endif
+    aResDir = aResDirsStr.Token(aSep.c_str(), 1);
+    for (int i = 1; !aResDir.IsEmpty(); aResDir = aResDirsStr.Token(aSep.c_str(), ++i)) {
+      Handle(Resource_Manager) anUserResMgr = new Resource_Manager ("ImportExport", aNull, aResDir, Standard_False);
+      if (anUserResMgr->Find("Import") || anUserResMgr->Find("Export")) {
+        myResMgrList.push_back( anUserResMgr );
+        isResourceFound = true;
+      }
     }
-    else
-    {
-      aUserResDir = getenv("HOME");
+  }
+  else
+  {
+    aResDir = getenv("HOME");
 #ifdef WIN32
-      aUserResDir += "\\.salome\\resources";
+    aResDir += "\\.config\\salome";
 #else
-      aUserResDir += "/.salome/resources";
+    aResDir += "/.config/salome";
 #endif
+    Handle(Resource_Manager) anUserResMgr = new Resource_Manager ("ImportExport", aNull, aResDir, Standard_False);
+    if (anUserResMgr->Find("Import") || anUserResMgr->Find("Export")) {
+      myResMgrList.push_back( anUserResMgr );
+      isResourceFound = true;
     }
-
-    myResMgrUser = new Resource_Manager ("ImportExport", aNull, aUserResDir, Standard_False);
-
-    isResourceFoundUser = true;
-    
-    if (!myResMgrUser->Find("Import") && !myResMgrUser->Find("Export")) {
-      // instead of complains in Resource_Manager
-      isResourceFoundUser = false;
-    }
-      
-  } else
-    isResourceFoundUser = true;
-    
-  if(!isResourceFound && !isResourceFoundUser){
-    INFOS("No valid file \"ImportExport\" found in " << aResDir.ToCString());
-    INFOS("No valid file \"ImportExport\" found in " << aUserResDir.ToCString() );
   }
-
-  return ( myResMgr->Find("Import") || myResMgr->Find("Export") ||
-           myResMgrUser->Find("Import") || myResMgrUser->Find("Export"));
+  return isResourceFound;
 }
 
 //=============================================================================
@@ -1400,3 +1359,173 @@ bool GEOMImpl_IInsertOperations::ImportXAO(const char* fileName,
   
   return true;
 }
+
+//=============================================================================
+/*!
+ *  This method creates material groups for an imported object.
+ *  \param theObject the imported object.
+ */
+//=============================================================================
+void GEOMImpl_IInsertOperations::MakeMaterialGroups
+                        (const Handle(GEOM_Object) &theObject,
+                         const Handle(TColStd_HSequenceOfTransient) &theSeq)
+{
+  TopoDS_Shape aResShape = theObject->GetValue();
+
+  if (aResShape.IsNull() == Standard_False) {
+    // Group shapes by material names.
+    Handle(GEOM_Function)      aFunction = theObject->GetLastFunction();
+    DataMapOfStringListOfShape aMapMaterialShapes;
+
+    // check all named shapes using iterator
+    TDF_ChildIDIterator anIt (aFunction->GetNamingEntry(),
+        TNaming_NamedShape::GetID(), Standard_True);
+
+    for (; anIt.More(); anIt.Next()) {
+      Handle(TNaming_NamedShape) anAttr =
+          Handle(TNaming_NamedShape)::DownCast(anIt.Value());
+
+      if (anAttr.IsNull() == Standard_False) {
+        TDF_Label                aLabel = anAttr->Label();
+        Handle(TDataStd_Comment) aComment;
+
+        if (aLabel.FindAttribute(TDataStd_Comment::GetID(), aComment)) {
+          TCollection_ExtendedString aMatName = aComment->Get();
+          TopoDS_Shape               aShape   = anAttr->Get();
+
+          if (aMapMaterialShapes.IsBound(aMatName) == Standard_False) {
+            NCollection_List<TopoDS_Shape> anEmptyList;
+
+            aMapMaterialShapes.Bind(aMatName, anEmptyList);
+          }
+
+          aMapMaterialShapes(aMatName).Append(aShape);
+        }
+      }
+    }
+
+    if (aMapMaterialShapes.IsEmpty() == Standard_False) {
+      // Construct groups.
+      TopAbs_ShapeEnum aType = aResShape.ShapeType();
+      Standard_Integer i;
+      DataMapOfStringListOfShape::Iterator aMapIter;
+
+      // Check each shape type.
+      for(i = aType; i <= TopAbs_VERTEX; i++) {
+        DataMapOfStringListOfShape::Iterator aMapIter(aMapMaterialShapes);
+
+        for (; aMapIter.More(); aMapIter.Next()) {
+          NCollection_List<TopoDS_Shape> &aShList = aMapIter.ChangeValue();
+          NCollection_List<TopoDS_Shape>::Iterator aShIter(aShList);
+          NCollection_List<TopoDS_Shape>  aShListSameType;
+
+          while (aShIter.More()) {
+            const TopoDS_Shape &aShape = aShIter.Value();
+
+            if (i == aShape.ShapeType()) {
+              // Treat this element.
+              aShListSameType.Append(aShape);
+              aShList.Remove(aShIter);
+            } else {
+              // Go to the next element.
+              aShIter.Next();
+            }
+          }
+
+          if (aShListSameType.IsEmpty() == Standard_False) {
+            // Construct a group.
+            Handle(GEOM_Object) aGroup =
+              MakeGroup(theObject, aMapIter.Key(), aShListSameType);
+
+            if (aGroup.IsNull() == Standard_False) {
+              theSeq->Append(aGroup);
+            }
+          }
+        }
+      }
+    }
+  }
+}
+
+
+//=============================================================================
+/*!
+ *  This method creates a group of shapes of certain type.
+ *  \param theObject the imported object.
+ *  \param theName the material name.
+ *  \param theShapes the list of shapes to be added to this group.
+ *  \return the created group.
+ */
+//=============================================================================
+Handle(GEOM_Object) GEOMImpl_IInsertOperations::MakeGroup
+                  (const Handle(GEOM_Object)            &theObject,
+                   const TCollection_ExtendedString     &theName,
+                   const NCollection_List<TopoDS_Shape> &theShapes)
+{
+  Handle(GEOM_Object)                aGroup;
+  TopTools_IndexedMapOfShape         anIndices;
+  Handle(TColStd_HSequenceOfInteger) aSeqIDs = new TColStd_HSequenceOfInteger;
+  NCollection_List<TopoDS_Shape>::Iterator anIter(theShapes);
+
+  TopExp::MapShapes(theObject->GetValue(), anIndices);
+
+  // Compose shape IDs.
+  for (; anIter.More(); anIter.Next()) {
+    const TopoDS_Shape &aShape = anIter.Value();
+    const Standard_Integer anIndex = anIndices.FindIndex(aShape);
+
+    if (anIndex > 0) {
+      aSeqIDs->Append(anIndex);
+    }
+  }
+
+  if (aSeqIDs->IsEmpty() == Standard_False) {
+    // Create a group.
+    const TopAbs_ShapeEnum aType  = theShapes.First().ShapeType();
+
+    aGroup = myGroupOperations->CreateGroup(theObject, aType);
+
+    if (aGroup.IsNull() == Standard_False) {
+      aGroup->GetLastFunction()->SetDescription("");
+      myGroupOperations->UnionIDs(aGroup, aSeqIDs);
+      aGroup->GetLastFunction()->SetDescription("");
+
+      // Compose the group name.
+      TCollection_AsciiString aGroupName(theName);
+
+      switch(aType) {
+        case TopAbs_VERTEX:
+          aGroupName += "_VERTEX";
+          break;
+        case TopAbs_EDGE:
+          aGroupName += "_EDGE";
+          break;
+        case TopAbs_WIRE:
+          aGroupName += "_WIRE";
+          break;
+        case TopAbs_FACE:
+          aGroupName += "_FACE";
+          break;
+        case TopAbs_SHELL:
+          aGroupName += "_SHELL";
+          break;
+        case TopAbs_SOLID:
+          aGroupName += "_SOLID";
+          break;
+        case TopAbs_COMPSOLID:
+          aGroupName += "_COMPSOLID";
+          break;
+        case TopAbs_COMPOUND:
+          aGroupName += "_COMPOUND";
+          break;
+        default:
+          aGroupName += "_SHAPE";
+          break;
+      }
+
+      aGroup->SetName(aGroupName.ToCString());
+    }
+  }
+
+  return aGroup;
+}