Salome HOME
0022468: [CEA 1048] IMP GEOM: creating groups on materials at STEP import
authorskv <skv@opencascade.com>
Tue, 25 Feb 2014 12:21:09 +0000 (16:21 +0400)
committerskv <skv@opencascade.com>
Tue, 25 Feb 2014 12:21:09 +0000 (16:21 +0400)
doc/salome/gui/GEOM/input/import_export.doc
idl/GEOM_Gen.idl
src/GEOMImpl/GEOMImpl_IInsertOperations.cxx
src/GEOMImpl/GEOMImpl_IInsertOperations.hxx
src/GEOMToolsGUI/GEOMToolsGUI.cxx
src/GEOM_I/GEOM_Gen_i.cc
src/GEOM_I/GEOM_IInsertOperations_i.cc
src/GEOM_I/GEOM_IInsertOperations_i.hh
src/GEOM_I_Superv/GEOM_Superv_i.cc
src/GEOM_SWIG/geomBuilder.py
src/STEPImport/STEPImport.cxx

index ccc5c6a7e4900aa60434bd3d19ee31189f1ca7a4..50b79203064ef34c7ac81faabc61c7d22e630ddb 100644 (file)
@@ -10,6 +10,10 @@ example, CATIA 5).
 
 The \subpage xao_format_page "import and export of shapes in XAO format" is implemented differently.
 
+\note If a plugin supports import of materials associated with shapes, these
+shapes are grouped corresponding to the imported materials. For the moment STEP
+import is the only plugin that supports this feature.
+
 <em>To import geometrical objects from a BREP, IGES, STEP, ACIS or STL file:</em>
 
 \par
index 62a3e36fb2b94be2e3c73bee60fcc9830c58d274..630b6606724ad7601d62a9300ba36bb5a1519915 100644 (file)
@@ -3682,9 +3682,9 @@ module GEOM
      *         If format 'IGES_SCALE' is used instead of 'IGES' or
      *            format 'STEP_SCALE' is used instead of 'STEP',
      *            file length unit will be ignored (set to 'meter') and result model will be scaled.
-     *  \return New GEOM_Object, containing the imported shape.
+     *  \return List of GEOM_Object, containing the created shape and groups of materials.
      */
-    GEOM_Object ImportFile (in string theFileName, in string theFormatName);
+    ListOfGO ImportFile (in string theFileName, in string theFormatName);
 
     /*!
      *  \brief Read a value of parameter from a file, containing a shape.
index d1c694af4a19cca3e8a73a6d23b2ba401f6a4f4d..561e92fd0a0b116d1aba189048fce0f14fe6db88 100644 (file)
 #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>
@@ -238,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)
 {
@@ -247,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();
 
@@ -270,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;
@@ -278,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();
@@ -289,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 << anImported << " = geompy.ImportBREP(\"" << theFileName.ToCString() << "\")";
     else if (theFormatName == "IGES")
-      pd << result << " = geompy.ImportIGES(\"" << theFileName.ToCString() << "\")";
+      pd << anImported << " = geompy.ImportIGES(\"" << theFileName.ToCString() << "\")";
     else if (theFormatName == "IGES_SCALE")
-      pd << result << " = geompy.ImportIGES(\"" << theFileName.ToCString() << "\", True)";
+      pd << anImported << " = geompy.ImportIGES(\"" << theFileName.ToCString() << "\", True)";
     else if (theFormatName == "STEP")
-      pd << result << " = geompy.ImportSTEP(\"" << theFileName.ToCString() << "\")";
+      pd << anImported << " = geompy.ImportSTEP(\"" << theFileName.ToCString() << "\")";
     else if (theFormatName == "STEP_SCALE")
-      pd << result << " = geompy.ImportSTEP(\"" << theFileName.ToCString() << "\", True)";
+      pd << anImported << " = geompy.ImportSTEP(\"" << theFileName.ToCString() << "\", True)";
     else {
-      pd << result << " = geompy.ImportFile(\""
+      pd << anImported << " = geompy.ImportFile(\""
          << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
     }
   }
@@ -322,7 +336,7 @@ Handle(GEOM_Object) GEOMImpl_IInsertOperations::Import
   }
   // OLD CODE: end
 
-  return result;
+  return aSeq;
 }
 
 //=============================================================================
@@ -1394,3 +1408,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;
+}
index ab574e0b31fd746b11f6450090d57b92bdb0cdd6..0aa30a51979a395ff17217d02f70bd39d46900e8 100644 (file)
@@ -35,6 +35,8 @@
 #include <TDocStd_Document.hxx>
 #include <TColStd_HSequenceOfAsciiString.hxx>
 #include <TCollection_HAsciiString.hxx>
+#include <NCollection_DataMap.hxx>
+#include <NCollection_List.hxx>
 #include <Resource_Manager.hxx>
 
 #include <list>
@@ -55,6 +57,9 @@ namespace XAO {
   class Xao;
 }
 
+typedef NCollection_DataMap<TCollection_ExtendedString, NCollection_List<TopoDS_Shape> >
+        DataMapOfStringListOfShape;
+
 class GEOMImpl_IInsertOperations : public GEOM_IOperations {
  public:
   Standard_EXPORT GEOMImpl_IInsertOperations(GEOM_Engine* theEngine, int theDocID);
@@ -63,8 +68,9 @@ class GEOMImpl_IInsertOperations : public GEOM_IOperations {
 
   Standard_EXPORT Handle(GEOM_Object) MakeCopy (Handle(GEOM_Object) theOriginal);
   
-  Standard_EXPORT Handle(GEOM_Object) Import (const TCollection_AsciiString& theFileName,
-                                              const TCollection_AsciiString& theFormatType);
+  Standard_EXPORT Handle(TColStd_HSequenceOfTransient) Import
+                         (const TCollection_AsciiString& theFileName,
+                          const TCollection_AsciiString& theFormatType);
   
   Standard_EXPORT TCollection_AsciiString ReadValue (const TCollection_AsciiString& theFileName,
                                                      const TCollection_AsciiString& theFormatType,
@@ -128,6 +134,14 @@ class GEOMImpl_IInsertOperations : public GEOM_IOperations {
   void exportGroups(std::list<Handle(GEOM_Object)> groupList, XAO::Xao* xaoObject,
                     XAO::BrepGeometry* geometry);
 
+  void MakeMaterialGroups(const Handle(GEOM_Object)                  &theObject,
+                          const Handle(TColStd_HSequenceOfTransient) &theSeq);
+
+  Handle(GEOM_Object) MakeGroup
+                         (const Handle(GEOM_Object)            &theObject,
+                          const TCollection_ExtendedString     &theName,
+                          const NCollection_List<TopoDS_Shape> &theShapes);
+
  private:
   Handle(Resource_Manager) myResMgr;
   Handle(Resource_Manager) myResMgrUser;
index c02b6d6880163d0cb0e62d5f43f3b60576e2b4ff..daf888e2d6ff30947310255c2a7ed7ca5e9df7a2 100644 (file)
@@ -818,9 +818,10 @@ bool GEOMToolsGUI::Import()
       } // else if ( aCurrentType == "ACIS" )
 
       // IMPORT
-      GEOM::GEOM_Object_var anObj = aInsOp->ImportFile( fileN, fileT );
+      GEOM::ListOfGO_var anObj = aInsOp->ImportFile( fileN, fileT );
 
-      if ( !anObj->_is_nil() && aInsOp->IsDone() ) {
+      if ( anObj->length() > 0 && aInsOp->IsDone() ) {
+        GEOM::GEOM_Object_ptr aFather = anObj[0]._retn();
         QString aPublishObjName =
           GEOMBase::GetDefaultName( SUIT_Tools::file( fileName, /*withExten=*/true ) );
 
@@ -828,19 +829,26 @@ bool GEOMToolsGUI::Import()
         SALOMEDS::SObject_var aSO =
           GeometryGUI::GetGeomGen()->PublishInStudy( aDSStudy,
                                                      SALOMEDS::SObject::_nil(),
-                                                     anObj,
+                                                     aFather,
                                                      aPublishObjName.toLatin1().constData() );
         if ( ( !aSO->_is_nil() ) )
           anEntryList.append( aSO->GetID() );
 
-        objsForDisplay.append( anObj );
+        objsForDisplay.append( aFather );
 
         if ( aCurrentType == "ACIS" ) {
           if ( acisAnswer == SUIT_MessageBox::Yes || acisAnswer == SUIT_MessageBox::YesToAll )
-            GeometryGUI::GetGeomGen()->PublishNamedShapesInStudy( aDSStudy, anObj );
+            GeometryGUI::GetGeomGen()->PublishNamedShapesInStudy( aDSStudy, aFather );
         }
 
         anOp->commit();
+
+        // Treat group objects.
+        for (int i = 1, n = anObj->length(); i < n; i++) {
+          GEOM::GEOM_Object_ptr anObject = anObj[i]._retn();
+          GeometryGUI::GetGeomGen()->AddInStudy(aDSStudy,
+            anObject, tr(anObject->GetName()).toStdString().c_str(), aFather);
+        }
       }
       else {
         anOp->abort();
index 38c0065c03ea0efa5eae08642eeade9f0f71dfa5..5a9685b5d8decd1429d35e807ab0e6d5675ffdd2 100644 (file)
@@ -243,9 +243,10 @@ SALOMEDS::SObject_ptr GEOM_Gen_i::PublishInStudy(SALOMEDS::Study_ptr   theStudy,
   aResultSO->SetAttrString("AttributeIOR",aGeomObjIOR);
 
   TCollection_AsciiString anObjectName, aNamePrefix("Shape_");
+  CORBA::Long mytype=aBaseObj->GetType();
 
   // BEGIN: try to find existed name for current shape
-  if ( !aShape->_is_nil() )
+  if ( !aShape->_is_nil() && mytype != GEOM_GROUP)
   {
     // recieve current TopoDS shape
     CORBA::String_var entry = aShape->GetEntry();
@@ -279,7 +280,6 @@ SALOMEDS::SObject_ptr GEOM_Gen_i::PublishInStudy(SALOMEDS::Study_ptr   theStudy,
   }
   // END: try to find existed name for current shape
 
-  CORBA::Long mytype=aBaseObj->GetType();
   if ( mytype == GEOM_GROUP ) {
     GEOM::GEOM_IGroupOperations_var anOp = GetIGroupOperations( theStudy->StudyId() );
     switch ( (TopAbs_ShapeEnum)anOp->GetType( aShape )) {
index 50eadf77bd73e18baff33eaa5ac98280f8521033..3d8f79751ebc03ddae3fff1c3319d279009c0316 100644 (file)
@@ -125,11 +125,11 @@ void GEOM_IInsertOperations_i::Export
  *  ImportFile
  */
 //=============================================================================
-GEOM::GEOM_Object_ptr GEOM_IInsertOperations_i::ImportFile
+GEOM::ListOfGO* GEOM_IInsertOperations_i::ImportFile
                    (const char* theFileName,
                     const char* theFormatName)
 {
-  GEOM::GEOM_Object_var aGEOMObject;
+  GEOM::ListOfGO_var aSeq = new GEOM::ListOfGO;
 
   //Set a not done flag
   GetOperations()->SetNotDone();
@@ -137,22 +137,31 @@ GEOM::GEOM_Object_ptr GEOM_IInsertOperations_i::ImportFile
   //Import the shape from the file
   char* aFileName   = strdup(theFileName);
   char* aFormatName = strdup(theFormatName);
-  Handle(GEOM_Object) anObject = GetOperations()->Import(aFileName, aFormatName);
+  Handle(TColStd_HSequenceOfTransient) aHSeq = GetOperations()->Import(aFileName, aFormatName);
 
-  if( strcmp(aFormatName,"IGES_UNIT")==0 && !anObject.IsNull() ) {
+  if( strcmp(aFormatName,"IGES_UNIT")==0 && !aHSeq.IsNull() ) {
     free(aFileName);
     free(aFormatName);
-    return GetObject(anObject);
+    return aSeq._retn();
   }
 
   free(aFileName);
   free(aFormatName);
 
-  if (!GetOperations()->IsDone() || anObject.IsNull()) {
-    return aGEOMObject._retn();
+  if (!GetOperations()->IsDone() || aHSeq.IsNull()) {
+    return aSeq._retn();
   }
 
-  return GetObject(anObject);
+  // Copy created objects.
+  Standard_Integer aLength = aHSeq->Length();
+
+  aSeq->length(aLength);
+
+  for (Standard_Integer i = 1; i <= aLength; i++) {
+    aSeq[i-1] = GetObject(Handle(GEOM_Object)::DownCast(aHSeq->Value(i)));
+  }
+
+  return aSeq._retn();
 }
 
 //=============================================================================
index c9088371e0469e698683a41ecfaf6bbb96fce485..d36e0446d8813ab9067e7f5541cee394cfc5f755 100644 (file)
@@ -49,8 +49,8 @@ class GEOM_I_EXPORT GEOM_IInsertOperations_i :
                                 const char*           theFileName,
                                 const char*           theFormatName);
 
-  GEOM::GEOM_Object_ptr ImportFile (const char* theFileName,
-                                    const char* theFormatName);
+  GEOM::ListOfGO* ImportFile (const char* theFileName,
+                              const char* theFormatName);
 
   char* ReadValue (const char* theFileName,
                    const char* theFormatName,
index 9e999020c6d6fa87909bc2dfeb6201e240f1ff3e..708f793dc45e6b94e2fead2131ad7241216d4c37 100644 (file)
@@ -1588,7 +1588,13 @@ GEOM::GEOM_Object_ptr GEOM_Superv_i::ImportFile (const char* theFileName,
   beginService( " GEOM_Superv_i::ImportFile" );
   MESSAGE("GEOM_Superv_i::ImportFile");
   getInsOp();
-  GEOM::GEOM_Object_ptr anObj = myInsOp->ImportFile(theFileName, theFormatName);
+  GEOM::ListOfGO* aSeq = myInsOp->ImportFile(theFileName, theFormatName);
+  GEOM::GEOM_Object_ptr anObj;
+  
+  if (aSeq->length() > 0) {
+    anObj = aSeq->operator[](0);
+  }
+
   endService( " GEOM_Superv_i::ImportFile" );
   return anObj;
 }
index 2507e2c2c45532cf7b291e67c4c13a9507c67739..9b1a12a2786eab8b620005583a3dd7c28348f495 100644 (file)
@@ -10303,6 +10303,11 @@ class geomBuilder(object, GEOM._objref_GEOM_Gen):
         #         publication is switched on, default value is used for result name.
         #
         #  @return New GEOM.GEOM_Object, containing the imported shape.
+        #          If material names are imported it returns the list of
+        #          objects. The first one is the imported object followed by
+        #          material groups.
+        #  @note Auto publishing is allowed for the shape itself. Imported
+        #        material groups are not automatically published.
         #
         #  @ref swig_Import_Export "Example"
         def ImportFile(self, theFileName, theFormatName, theName=None):
@@ -10323,12 +10328,22 @@ class geomBuilder(object, GEOM._objref_GEOM_Gen):
 
             Returns:
                 New GEOM.GEOM_Object, containing the imported shape.
+                If material names are imported it returns the list of
+                objects. The first one is the imported object followed by
+                material groups.
+            Note:
+                Auto publishing is allowed for the shape itself. Imported
+                material groups are not automatically published.
             """
             # Example: see GEOM_TestOthers.py
-            anObj = self.InsertOp.ImportFile(theFileName, theFormatName)
+            aListObj = self.InsertOp.ImportFile(theFileName, theFormatName)
             RaiseIfFailed("ImportFile", self.InsertOp)
-            self._autoPublish(anObj, theName, "imported")
-            return anObj
+            aNbObj = len(aListObj)
+            if aNbObj > 0:
+                self._autoPublish(aListObj[0], theName, "imported")
+            if aNbObj == 1:
+                return aListObj[0]
+            return aListObj
 
         ## Deprecated analog of ImportFile()
         def Import(self, theFileName, theFormatName, theName=None):
@@ -10432,6 +10447,11 @@ class geomBuilder(object, GEOM._objref_GEOM_Gen):
         #         publication is switched on, default value is used for result name.
         #
         #  @return New GEOM.GEOM_Object, containing the imported shape.
+        #          If material names are imported it returns the list of
+        #          objects. The first one is the imported object followed by
+        #          material groups.
+        #  @note Auto publishing is allowed for the shape itself. Imported
+        #        material groups are not automatically published.
         #
         #  @ref swig_Import_Export "Example"
         def ImportSTEP(self, theFileName, ignoreUnits = False, theName=None):
@@ -10449,6 +10469,12 @@ class geomBuilder(object, GEOM._objref_GEOM_Gen):
 
             Returns:
                 New GEOM.GEOM_Object, containing the imported shape.
+                If material names are imported it returns the list of
+                objects. The first one is the imported object followed by
+                material groups.
+            Note:
+                Auto publishing is allowed for the shape itself. Imported
+                material groups are not automatically published.
             """
             # Example: see GEOM_TestOthers.py
             # note: auto-publishing is done in self.ImportFile()
index 0dd1bcfd021cefcc53b7fc73fb54dfe2c23ec1ac..0dc8d70578516ea4ccd4555f391d6efeae63e675 100644 (file)
 #include <TDF_ChildIDIterator.hxx>
 #include <TDF_Label.hxx>
 #include <TDataStd_Name.hxx>
+#include <TDataStd_Comment.hxx>
 #include <TNaming_Builder.hxx>
 #include <TNaming_NamedShape.hxx>
 
 #include <IFSelect_ReturnStatus.hxx>
+#include <Interface_EntityIterator.hxx>
+#include <Interface_Graph.hxx>
 #include <Interface_InterfaceModel.hxx>
 #include <Interface_Static.hxx>
 #include <STEPControl_Reader.hxx>
 #include <StepBasic_ProductDefinitionFormation.hxx>
 #include <StepGeom_GeometricRepresentationItem.hxx>
 #include <StepShape_TopologicalRepresentationItem.hxx>
+#include <StepRepr_DescriptiveRepresentationItem.hxx>
+#include <StepRepr_ProductDefinitionShape.hxx>
+#include <StepRepr_PropertyDefinitionRepresentation.hxx>
+#include <StepRepr_Representation.hxx>
 #include <TransferBRep.hxx>
 #include <Transfer_Binder.hxx>
 #include <Transfer_TransientProcess.hxx>
   #define STEPIMPORT_EXPORT
 #endif
 
+
+//=============================================================================
+/*!
+ *  GetShape()
+ */
+//=============================================================================
+
+static TopoDS_Shape GetShape(const Handle(Standard_Transient)        &theEnti,
+                             const Handle(Transfer_TransientProcess) &theTP)
+{
+  TopoDS_Shape            aResult;
+  Handle(Transfer_Binder) aBinder = theTP->Find(theEnti);
+
+  if (aBinder.IsNull()) {
+    return aResult;
+  }
+
+  aResult = TransferBRep::ShapeResult(aBinder);
+
+  return aResult;
+}
+
+//=============================================================================
+/*!
+ *  GetLabel()
+ */
+//=============================================================================
+
+static TDF_Label GetLabel(const Handle(Standard_Transient) &theEnti,
+                          const TDF_Label                  &theShapeLabel,
+                          const TopoDS_Shape               &aShape)
+{
+  TDF_Label aResult;
+
+  if (theEnti->IsKind
+            (STANDARD_TYPE(StepGeom_GeometricRepresentationItem))) {
+    // check all named shapes using iterator
+    TDF_ChildIDIterator anIt
+      (theShapeLabel, TDataStd_Name::GetID(), Standard_True);
+
+    for (; anIt.More(); anIt.Next()) {
+      Handle(TDataStd_Name) nameAttr =
+        Handle(TDataStd_Name)::DownCast(anIt.Value());
+
+      if (nameAttr.IsNull()) {
+        continue;
+      }
+
+      TDF_Label aLab = nameAttr->Label();
+      Handle(TNaming_NamedShape) shAttr; 
+
+      if (aLab.FindAttribute(TNaming_NamedShape::GetID(), shAttr) &&
+          shAttr->Get().IsEqual(aShape)) {
+        aResult = aLab;
+      }
+    }
+  }
+
+  // create label and set shape
+  if (aResult.IsNull()) {
+    TDF_TagSource aTag;
+
+    aResult = aTag.NewChild(theShapeLabel);
+
+    TNaming_Builder tnBuild (aResult);
+
+    tnBuild.Generated(aShape);
+  }
+
+  return aResult;
+}
+
+//=============================================================================
+/*!
+ *  StoreName()
+ */
+//=============================================================================
+
+static void StoreName(const Handle(Standard_Transient)        &theEnti,
+                      const TopTools_IndexedMapOfShape        &theIndices,
+                      const Handle(Transfer_TransientProcess) &theTP,
+                      const TDF_Label                         &theShapeLabel)
+{
+  Handle(TCollection_HAsciiString) aName;
+
+  if (theEnti->IsKind(STANDARD_TYPE(StepShape_TopologicalRepresentationItem)) ||
+      theEnti->IsKind(STANDARD_TYPE(StepGeom_GeometricRepresentationItem))) {
+    aName = Handle(StepRepr_RepresentationItem)::DownCast(theEnti)->Name();
+  } else {
+    Handle(StepBasic_ProductDefinition) PD =
+      Handle(StepBasic_ProductDefinition)::DownCast(theEnti);
+
+    if (PD.IsNull() == Standard_False) {
+      Handle(StepBasic_Product) Prod = PD->Formation()->OfProduct();
+      aName = Prod->Name();
+    }
+  }
+
+  bool isValidName = false;
+
+  if (aName.IsNull() == Standard_False) {
+    isValidName = true;
+
+    if (aName->UsefullLength() < 1) {
+      isValidName = false;
+    } else if (aName->UsefullLength() == 4 &&
+               toupper (aName->Value(1)) == 'N' &&
+               toupper (aName->Value(2)) == 'O' &&
+               toupper (aName->Value(3)) == 'N' &&
+               toupper (aName->Value(4)) == 'E') {
+      // skip 'N0NE' name
+      isValidName = false;
+    } else {
+      // special check to pass names like "Open CASCADE STEP translator 6.3 1"
+      TCollection_AsciiString aSkipName ("Open CASCADE STEP translator");
+
+      if (aName->Length() >= aSkipName.Length()) {
+        if (aName->String().SubString
+                            (1, aSkipName.Length()).IsEqual(aSkipName)) {
+          isValidName = false;
+        }
+      }
+    }
+  }
+
+  if (isValidName) {
+    TCollection_ExtendedString aNameExt (aName->ToCString());
+
+    // find target shape
+    TopoDS_Shape S = GetShape(theEnti, theTP);
+
+    if (S.IsNull()) {
+      return;
+    }
+
+    // as PRODUCT can be included in the main shape
+    // several times, we look here for all iclusions.
+    Standard_Integer isub, nbSubs = theIndices.Extent();
+
+    for (isub = 1; isub <= nbSubs; isub++) {
+      TopoDS_Shape aSub = theIndices.FindKey(isub);
+
+      if (aSub.IsPartner(S)) {
+        TDF_Label L = GetLabel(theEnti, theShapeLabel, aSub);
+
+        // set a name
+        TDataStd_Name::Set(L, aNameExt);
+      }
+    }
+  }
+}
+
+//=============================================================================
+/*!
+ *  StoreMaterial()
+ */
+//=============================================================================
+
+static void StoreMaterial
+                    (const Handle(Standard_Transient)        &theEnti,
+                     const TopTools_IndexedMapOfShape        &theIndices,
+                     const Handle(Transfer_TransientProcess) &theTP,
+                     const TDF_Label                         &theShapeLabel)
+{
+  // Treat Product Definition Shape only.
+  Handle(StepRepr_ProductDefinitionShape) aPDS =
+      Handle(StepRepr_ProductDefinitionShape)::DownCast(theEnti);
+  Handle(StepBasic_ProductDefinition)     aProdDef;
+
+  if(aPDS.IsNull() == Standard_False) {
+    // Product Definition Shape ==> Product Definition
+    aProdDef = aPDS->Definition().ProductDefinition();
+  }
+
+  if (aProdDef.IsNull() == Standard_False) {
+    // Product Definition ==> Property Definition
+    const Interface_Graph    &aGraph = theTP->Graph();
+    Interface_EntityIterator  aSubs  = aGraph.Sharings(aProdDef);
+    TopoDS_Shape              aShape;
+
+    for(aSubs.Start(); aSubs.More(); aSubs.Next()) {
+      Handle(StepRepr_PropertyDefinition) aPropD =
+        Handle(StepRepr_PropertyDefinition)::DownCast(aSubs.Value());
+
+      if(aPropD.IsNull() == Standard_False) {
+        // Property Definition ==> Representation.
+        Interface_EntityIterator aSubs1 = aGraph.Sharings(aPropD);
+
+        for(aSubs1.Start(); aSubs1.More(); aSubs1.Next()) {
+          Handle(StepRepr_PropertyDefinitionRepresentation) aPDR =
+            Handle(StepRepr_PropertyDefinitionRepresentation)::
+              DownCast(aSubs1.Value());
+
+          if(aPDR.IsNull() == Standard_False) {
+            // Property Definition ==> Material Name.
+            Handle(StepRepr_Representation) aRepr = aPDR->UsedRepresentation();
+
+            if(aRepr.IsNull() == Standard_False) {
+              Standard_Integer ir;
+
+              for(ir = 1; ir <= aRepr->NbItems(); ir++) {
+                Handle(StepRepr_RepresentationItem) aRI = aRepr->ItemsValue(ir);
+                Handle(StepRepr_DescriptiveRepresentationItem) aDRI =
+                  Handle(StepRepr_DescriptiveRepresentationItem)::DownCast(aRI);
+
+                if(aDRI.IsNull() == Standard_False) {
+                  // Get shape from Product Definition
+                  Handle(TCollection_HAsciiString) aMatName = aDRI->Name();
+
+                  if(aMatName.IsNull() == Standard_False) {
+                    TCollection_ExtendedString
+                                 aMatNameExt (aMatName->ToCString());
+
+                    if (aShape.IsNull()) {
+                      // Get the shape.
+                      aShape = GetShape(aProdDef, theTP);
+
+                      if (aShape.IsNull()) {
+                        return;
+                      }
+                    }
+
+                    // as PRODUCT can be included in the main shape
+                    // several times, we look here for all iclusions.
+                    Standard_Integer isub, nbSubs = theIndices.Extent();
+
+                    for (isub = 1; isub <= nbSubs; isub++) {
+                      TopoDS_Shape aSub = theIndices.FindKey(isub);
+
+                      if (aSub.IsPartner(aShape)) {
+                        TDF_Label aLabel =
+                          GetLabel(aProdDef, theShapeLabel, aSub);
+
+                        // set a name
+                        TDataStd_Comment::Set(aLabel, aMatNameExt);
+                      }
+                    }
+                  }
+                }
+              }
+            }
+          }
+        }
+      }
+    }
+  }
+}
+
 //=============================================================================
 /*!
  *  Import()
@@ -271,7 +526,7 @@ extern "C"
           return TopoDS_Shape();
         }
 
-        // BEGIN: Store names of sub-shapes from file
+        // BEGIN: Store names and materials of sub-shapes from file
         TopTools_IndexedMapOfShape anIndices;
         TopExp::MapShapes(aResShape, anIndices);
 
@@ -279,92 +534,20 @@ extern "C"
         Handle(XSControl_TransferReader) TR = aReader.WS()->TransferReader();
         if (!TR.IsNull()) {
           Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
-          Handle(Standard_Type) tPD  = STANDARD_TYPE(StepBasic_ProductDefinition);
-          Handle(Standard_Type) tShape  = STANDARD_TYPE(StepShape_TopologicalRepresentationItem);
-          Handle(Standard_Type) tGeom  = STANDARD_TYPE(StepGeom_GeometricRepresentationItem);
 
           Standard_Integer nb = Model->NbEntities();
+
           for (Standard_Integer ie = 1; ie <= nb; ie++) {
             Handle(Standard_Transient) enti = Model->Value(ie);
-            Handle(TCollection_HAsciiString) aName;
-            if ( enti->IsKind( tShape ) || enti->IsKind(tGeom))
-            {
-              aName = Handle(StepRepr_RepresentationItem)::DownCast(enti)->Name();
-            }
-            else if (enti->DynamicType() == tPD)
-            {
-              Handle(StepBasic_ProductDefinition) PD =
-                Handle(StepBasic_ProductDefinition)::DownCast(enti);
-              if (PD.IsNull()) continue;
-
-              Handle(StepBasic_Product) Prod = PD->Formation()->OfProduct();
-              aName = Prod->Name();
-            }
-            else
-            {
-              continue;
-            }
-            if ( aName->UsefullLength() < 1 )
-              continue;
-            // skip 'N0NE' name
-            if ( aName->UsefullLength() == 4 &&
-                 toupper (aName->Value(1)) == 'N' &&
-                 toupper (aName->Value(2)) == 'O' &&
-                 toupper (aName->Value(3)) == 'N' &&
-                 toupper (aName->Value(4)) == 'E')
-              continue;
 
-            // special check to pass names like "Open CASCADE STEP translator 6.3 1"
-            TCollection_AsciiString aSkipName ("Open CASCADE STEP translator");
-            if (aName->Length() >= aSkipName.Length()) {
-              if (aName->String().SubString(1, aSkipName.Length()).IsEqual(aSkipName))
-                continue;
-            }
-            TCollection_ExtendedString aNameExt (aName->ToCString());
-
-            // find target shape
-            Handle(Transfer_Binder) binder = TP->Find(enti);
-            if (binder.IsNull()) continue;
-            TopoDS_Shape S = TransferBRep::ShapeResult(binder);
-            if (S.IsNull()) continue;
-
-            // as PRODUCT can be included in the main shape
-            // several times, we look here for all iclusions.
-            Standard_Integer isub, nbSubs = anIndices.Extent();
-            for (isub = 1; isub <= nbSubs; isub++)
-            {
-              TopoDS_Shape aSub = anIndices.FindKey(isub);
-              if (aSub.IsPartner(S)) {
-                TDF_Label L;
-                if (enti->IsKind(tGeom)) {
-                  // check all named shapes using iterator
-                  TDF_ChildIDIterator anIt (theShapeLabel, TDataStd_Name::GetID(), Standard_True);
-                  for (; anIt.More(); anIt.Next()) {
-                    Handle(TDataStd_Name) nameAttr =
-                      Handle(TDataStd_Name)::DownCast(anIt.Value());
-                    if (nameAttr.IsNull()) continue;
-                    TDF_Label Lab = nameAttr->Label();
-                    Handle(TNaming_NamedShape) shAttr; 
-                    if (Lab.FindAttribute(TNaming_NamedShape::GetID(), shAttr) && shAttr->Get().IsEqual(aSub))
-                      L = Lab;
-                  }
-                }
-                // create label and set shape
-                if (L.IsNull())
-                {
-                  TDF_TagSource aTag;
-                  L = aTag.NewChild(theShapeLabel);
-                  TNaming_Builder tnBuild (L);
-                  //tnBuild.Generated(S);
-                  tnBuild.Generated(aSub);
-                }
-                // set a name
-                TDataStd_Name::Set(L, aNameExt);
-              }
-            }
+            // Store names.
+            StoreName(enti, anIndices, TP, theShapeLabel);
+
+            // Store materials.
+            StoreMaterial(enti, anIndices, TP, theShapeLabel);
           }
         }
-        // END: Store names
+        // END: Store names and materials
       }
       else {
 //        switch (status) {