Salome HOME
updated copyright message
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_STEPImport.cpp
index 48f57b50beb28b53a9605a0af0aa89a3ebb93f9d..331332086ca0d06f2582fffc4ac4f681152cbc6e 100644 (file)
@@ -1,10 +1,24 @@
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
-
-// File:    GEOMALGOAPI_STEPImport.cpp
-// Created: Dec 24, 2014
-// Author:  Sergey BELASH
+// Copyright (C) 2014-2023  CEA/DEN, EDF R&D
+//
+// 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, 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
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
 
 #include <GeomAlgoAPI_STEPImport.h>
+#include <GeomAlgoAPI_STEPImportXCAF.h>
 
 #include <TDF_ChildIDIterator.hxx>
 #include <TDF_Label.hxx>
@@ -18,6 +32,8 @@
 #include <Interface_Graph.hxx>
 #include <Interface_InterfaceModel.hxx>
 #include <Interface_Static.hxx>
+
+#include <STEPCAFControl_Reader.hxx>
 #include <STEPControl_Reader.hxx>
 #include <StepBasic_Product.hxx>
 #include <StepBasic_ProductDefinition.hxx>
 #include <TopTools_IndexedMapOfShape.hxx>
 #include <TopoDS_Compound.hxx>
 #include <TopoDS_Iterator.hxx>
-#include <TopoDS_Shape.hxx>
 
+#include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
+#include <OSD_Exception.hxx>
+
+//=================================================================================================
+bool readUnits(STEPControl_Reader& aReader,
+               const bool theScalInterUnits,
+               std::string& theError)
+{
+  // Regard or not the model units
+  if (!theScalInterUnits) {
+    // set UnitFlag to units from file
+    TColStd_SequenceOfAsciiString anUnitLengthNames;
+    TColStd_SequenceOfAsciiString anUnitAngleNames;
+    TColStd_SequenceOfAsciiString anUnitSolidAngleNames;
+    aReader.FileUnits(anUnitLengthNames, anUnitAngleNames, anUnitSolidAngleNames);
+    if (anUnitLengthNames.Length() > 0) {
+      TCollection_AsciiString aLenUnits = anUnitLengthNames.First();
+      if (aLenUnits == "millimetre")
+        Interface_Static::SetCVal("xstep.cascade.unit", "MM");
+      else if (aLenUnits == "centimetre")
+        Interface_Static::SetCVal("xstep.cascade.unit", "CM");
+      else if (aLenUnits == "metre" || aLenUnits.IsEmpty())
+        Interface_Static::SetCVal("xstep.cascade.unit", "M");
+      else if (aLenUnits == "INCH")
+        Interface_Static::SetCVal("xstep.cascade.unit", "INCH");
+      else {
+        theError = "The file contains not supported units.";
+        return false;
+      }
+      // TODO (for other units than mm, cm, m or inch)
+      //else if (aLenUnits == "")
+      //  Interface_Static::SetCVal("xstep.cascade.unit", "???");
+    }
+  }
+  else {
+    //cout<<"need re-scale a model"<<endl;
+    // set UnitFlag to 'meter'
+    Interface_Static::SetCVal("xstep.cascade.unit", "M");
+  }
+  return true;
+}
 
-#include <TColStd_SequenceOfAsciiString.hxx>
 
-#include <Standard_Failure.hxx>
-#include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
+//==================================================================================================
 std::shared_ptr<GeomAPI_Shape> STEPImport(const std::string& theFileName,
-                                          const std::string& theFormatName,
+                                          const std::string& /*theFormatName*/,
+                                          const bool theScalInterUnits,
                                           std::string& theError)
 {
+
   TopoDS_Shape aResShape;
 
   // Set "C" numeric locale to save numbers correctly
@@ -60,7 +116,7 @@ std::shared_ptr<GeomAPI_Shape> STEPImport(const std::string& theFileName,
   STEPControl_Reader aReader;
 
   //VSR: 16/09/09: Convert to METERS
-  Interface_Static::SetCVal("xstep.cascade.unit","M");
+  Interface_Static::SetCVal("xstep.cascade.unit", "M");
   Interface_Static::SetIVal("read.step.ideas", 1);
   Interface_Static::SetIVal("read.step.nonmanifold", 1);
 
@@ -74,39 +130,11 @@ std::shared_ptr<GeomAPI_Shape> STEPImport(const std::string& theFileName,
     IFSelect_ReturnStatus status = aReader.ReadFile(theFileName.c_str());
 
     if (status == IFSelect_RetDone) {
-
       // Regard or not the model units
-      if (theFormatName == "STEP_SCALE") {
-        // set UnitFlag to units from file
-        TColStd_SequenceOfAsciiString anUnitLengthNames;
-        TColStd_SequenceOfAsciiString anUnitAngleNames;
-        TColStd_SequenceOfAsciiString anUnitSolidAngleNames;
-        aReader.FileUnits(anUnitLengthNames, anUnitAngleNames, anUnitSolidAngleNames);
-        if (anUnitLengthNames.Length() > 0) {
-          TCollection_AsciiString aLenUnits = anUnitLengthNames.First();
-          if (aLenUnits == "millimetre")
-            Interface_Static::SetCVal("xstep.cascade.unit", "MM");
-          else if (aLenUnits == "centimetre")
-            Interface_Static::SetCVal("xstep.cascade.unit", "CM");
-          else if (aLenUnits == "metre" || aLenUnits.IsEmpty())
-            Interface_Static::SetCVal("xstep.cascade.unit", "M");
-          else if (aLenUnits == "INCH")
-            Interface_Static::SetCVal("xstep.cascade.unit", "INCH");
-          else {
-            theError = "The file contains not supported units.";
-            std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
-            aGeomShape->setImpl(new TopoDS_Shape(aResShape));
-            return aGeomShape;
-          }
-          // TODO (for other units than mm, cm, m or inch)
-          //else if (aLenUnits == "")
-          //  Interface_Static::SetCVal("xstep.cascade.unit", "???");
-        }
-      }
-      else {
-        //cout<<"need re-scale a model"<<endl;
-        // set UnitFlag to 'meter'
-        Interface_Static::SetCVal("xstep.cascade.unit","M");
+      if( !readUnits(aReader, theScalInterUnits, theError)) {
+        std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
+        aGeomShape->setImpl(new TopoDS_Shape());
+        return aGeomShape;
       }
 
       Standard_Boolean failsonly = Standard_False;
@@ -122,7 +150,6 @@ std::shared_ptr<GeomAPI_Shape> STEPImport(const std::string& theFileName,
         Standard_Integer nbs = aReader.NbShapes();
         if (!ok || nbs == 0)
         {
-          // THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::ImportStep", SALOME::BAD_PARAM);
           continue; // skip empty root
         }
         /* For a single entity */
@@ -149,7 +176,6 @@ std::shared_ptr<GeomAPI_Shape> STEPImport(const std::string& theFileName,
         for (Standard_Integer i = 1; i <= nbs; i++) {
           TopoDS_Shape aShape = aReader.Shape(i);
           if (aShape.IsNull()) {
-            // THROW_SALOME_CORBA_EXCEPTION("Null shape in GEOM_Gen_i::ImportStep", SALOME::BAD_PARAM) ;
             //return aResShape;
             continue;
           }
@@ -162,7 +188,7 @@ std::shared_ptr<GeomAPI_Shape> STEPImport(const std::string& theFileName,
         aResShape = compound;
 
       // Check if any BRep entity has been read, there must be at least a vertex
-      if ( !TopExp_Explorer( aResShape, TopAbs_VERTEX ).More() )
+      if (!TopExp_Explorer(aResShape, TopAbs_VERTEX).More())
       {
         theError = "No geometrical data in the imported file.";
         std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
@@ -174,9 +200,8 @@ std::shared_ptr<GeomAPI_Shape> STEPImport(const std::string& theFileName,
       aResShape.Nullify();
     }
   }
-  catch (Standard_Failure) {
-    Handle(Standard_Failure) aFail = Standard_Failure::Caught();
-    theError = aFail->GetMessageString();
+  catch (Standard_Failure const& anException) {
+    theError = anException.GetMessageString();
     aResShape.Nullify();
   }
   // Return previous locale
@@ -184,3 +209,62 @@ std::shared_ptr<GeomAPI_Shape> STEPImport(const std::string& theFileName,
   aGeomShape->setImpl(new TopoDS_Shape(aResShape));
   return aGeomShape;
 }
+
+//==================================================================================================
+GeomShapePtr STEPImportAttributs(const std::string& theFileName,
+                                 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
+                                 const bool theScalInterUnits,
+                                 const bool theMaterials,
+                                 const bool /*theColor*/,
+                                 std::map< std::wstring,
+                                 std::list<std::wstring>>& theMaterialShape,
+                                 std::string& theError)
+{
+
+  try {
+    STEPControl_Reader aReader;
+    std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
+
+    Interface_Static::SetCVal("xstep.cascade.unit", "M");
+    Interface_Static::SetIVal("read.step.ideas", 1);
+    Interface_Static::SetIVal("read.step.nonmanifold", 1);
+
+    try {
+      OCC_CATCH_SIGNALS;
+
+      IFSelect_ReturnStatus status = aReader.ReadFile(theFileName.c_str());
+
+      if (status == IFSelect_RetDone) {
+        // Regard or not the model units
+        if( !readUnits(aReader, theScalInterUnits, theError)) {
+          aGeomShape->setImpl(new TopoDS_Shape());
+          return aGeomShape;
+        }
+      }
+    } catch (Standard_Failure const& anException) {
+      theError = anException.GetMessageString();
+      aGeomShape->setImpl(new TopoDS_Shape());
+      return aGeomShape;
+    }
+
+    STEPCAFControl_Reader aCafreader;
+    aCafreader.SetColorMode(true);
+    aCafreader.SetNameMode(true);
+    aCafreader.SetMatMode(true);
+
+    if (aCafreader.ReadFile(theFileName.c_str()) != IFSelect_RetDone) {
+      theError = "Wrong format of the imported file. Can't import file.";
+      aGeomShape->setImpl(new TopoDS_Shape());
+      return aGeomShape;
+    }
+
+    return readAttributes(aCafreader,
+                          theResultBody,
+                          theMaterials,
+                          theMaterialShape,
+                          theError);
+  } catch (OSD_Exception&) {
+    //Try to load STEP file without colors...
+    return STEPImport(theFileName, "", theScalInterUnits, theError);
+  }
+}