Back to index

salome-geom  6.5.0
STEPImport.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  File:        STEPImport.cxx
00024 //  Created:     Wed May 19 14:41:10 2004
00025 //  Author:      Pavel TELKOV
00026 
00027 #include "utilities.h"
00028 
00029 #include <Basics_Utils.hxx>
00030 
00031 #include <Basics_OCCTVersion.hxx>
00032 
00033 #include <BRep_Builder.hxx>
00034 
00035 #include <IFSelect_ReturnStatus.hxx>
00036 
00037 #include <STEPControl_Reader.hxx>
00038 #include <StepBasic_ProductDefinition.hxx>
00039 #include <StepBasic_ProductDefinitionFormation.hxx>
00040 #include <StepBasic_Product.hxx>
00041 #include <Interface_InterfaceModel.hxx>
00042 #include <XSControl_TransferReader.hxx>
00043 #include <XSControl_WorkSession.hxx>
00044 #include <StepShape_TopologicalRepresentationItem.hxx>
00045 #include <StepGeom_GeometricRepresentationItem.hxx>
00046 
00047 #include <Transfer_Binder.hxx>
00048 #include <TNaming_Builder.hxx>
00049 #include <TDataStd_Name.hxx>
00050 #include <Transfer_TransientProcess.hxx>
00051 #include <TransferBRep.hxx>
00052 
00053 #include <TCollection_AsciiString.hxx>
00054 #include <TopoDS_Compound.hxx>
00055 #include <TopoDS_Shape.hxx>
00056 #include <TDF_Label.hxx>
00057 #include <TDF_ChildIDIterator.hxx>
00058 #include <TNaming_NamedShape.hxx>
00059 #include <TDF_Tool.hxx>
00060 #include <Interface_Static.hxx>
00061 
00062 #include <TopTools_IndexedMapOfShape.hxx>
00063 #include <TopExp.hxx>
00064 #include <TopoDS_Iterator.hxx>
00065 #include <BRepTools.hxx>
00066 
00067 #include <Standard_Failure.hxx>
00068 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
00069 
00070 #ifdef WNT
00071  #if defined STEPIMPORT_EXPORTS || defined STEPImport_EXPORTS
00072   #if defined WIN32
00073    #define STEPIMPORT_EXPORT __declspec( dllexport )
00074   #else
00075    #define STEPIMPORT_EXPORT
00076   #endif
00077  #else
00078   #if defined WIN32
00079    #define STEPIMPORT_EXPORT __declspec( dllimport )
00080   #else
00081    #define STEPIMPORT_EXPORT
00082   #endif
00083  #endif
00084 #else
00085  #define STEPIMPORT_EXPORT
00086 #endif
00087 
00088 //=============================================================================
00092 //=============================================================================
00093 
00094 extern "C"
00095 {
00096   STEPIMPORT_EXPORT
00097   TopoDS_Shape Import (const TCollection_AsciiString& theFileName,
00098                        const TCollection_AsciiString& /*theFormatName*/,
00099                        TCollection_AsciiString&       theError,
00100                        const TDF_Label&               theShapeLabel)
00101   {
00102     MESSAGE("Import STEP model from file " << theFileName.ToCString());
00103     // Set "C" numeric locale to save numbers correctly
00104     Kernel_Utils::Localizer loc;
00105     TopoDS_Shape aResShape;
00106     //VRV: OCC 4.0 migration
00107     STEPControl_Reader aReader;
00108     //VSR: 16/09/09: Convert to METERS
00109     Interface_Static::SetCVal("xstep.cascade.unit","M");
00110     Interface_Static::SetIVal("read.step.ideas", 1);
00111     Interface_Static::SetIVal("read.step.nonmanifold", 1);
00112     //VRV: OCC 4.0 migration
00113     TopoDS_Compound compound;
00114     BRep_Builder B;
00115     B.MakeCompound(compound);
00116     try {
00117 #if OCC_VERSION_LARGE > 0x06010000
00118       OCC_CATCH_SIGNALS;
00119 #endif
00120       IFSelect_ReturnStatus status = aReader.ReadFile(theFileName.ToCString());
00121 
00122       if (status == IFSelect_RetDone) {
00123         Standard_Boolean failsonly = Standard_False;
00124         aReader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity);
00125         /* Root transfers */
00126         Standard_Integer nbr = aReader.NbRootsForTransfer();
00127         aReader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity);
00128 
00129         for (Standard_Integer n = 1; n <= nbr; n++) {
00130           Standard_Boolean ok = aReader.TransferRoot(n);
00131           /* Collecting resulting entities */
00132           Standard_Integer nbs = aReader.NbShapes();
00133           if (!ok || nbs == 0)
00134           {
00135             // THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::ImportStep", SALOME::BAD_PARAM);
00136             continue; // skip empty root
00137           }
00138           /* For a single entity */
00139           else if (nbr == 1 && nbs == 1) {
00140             aResShape = aReader.Shape(1);
00141             // ATTENTION: this is a workaround for mantis issue 0020442 remark 0010776
00142             // It should be removed after patching OCCT for bug OCC22436
00143             // (fix for OCCT is expected in service pack next to OCCT6.3sp12)
00144             if (aResShape.ShapeType() == TopAbs_COMPOUND) {
00145               int nbSub1 = 0;
00146               TopoDS_Shape currShape;
00147               TopoDS_Iterator It (aResShape, Standard_True, Standard_True);
00148               for (; It.More(); It.Next()) {
00149                 nbSub1++;
00150                 currShape = It.Value();
00151               }
00152               if (nbSub1 == 1)
00153                 aResShape = currShape;
00154             }
00155             // END workaround
00156             break;
00157           }
00158 
00159           for (Standard_Integer i = 1; i <= nbs; i++) {
00160             TopoDS_Shape aShape = aReader.Shape(i);
00161             if (aShape.IsNull()) {
00162               // THROW_SALOME_CORBA_EXCEPTION("Null shape in GEOM_Gen_i::ImportStep", SALOME::BAD_PARAM) ;
00163               //return aResShape;
00164               continue;
00165             }
00166             else {
00167               B.Add(compound, aShape);
00168             }
00169           }
00170         }
00171         if (aResShape.IsNull())
00172           aResShape = compound;
00173 
00174         // BEGIN: Store names of sub-shapes from file
00175         TopTools_IndexedMapOfShape anIndices;
00176         TopExp::MapShapes(aResShape, anIndices);
00177 
00178         Handle(Interface_InterfaceModel) Model = aReader.WS()->Model();
00179         Handle(XSControl_TransferReader) TR = aReader.WS()->TransferReader();
00180         if (!TR.IsNull()) {
00181           Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
00182           Handle(Standard_Type) tPD  = STANDARD_TYPE(StepBasic_ProductDefinition);
00183           Handle(Standard_Type) tShape  = STANDARD_TYPE(StepShape_TopologicalRepresentationItem);
00184           Handle(Standard_Type) tGeom  = STANDARD_TYPE(StepGeom_GeometricRepresentationItem);
00185 
00186           Standard_Integer nb = Model->NbEntities();
00187           for (Standard_Integer ie = 1; ie <= nb; ie++) {
00188             Handle(Standard_Transient) enti = Model->Value(ie);
00189             Handle(TCollection_HAsciiString) aName;
00190             if ( enti->IsKind( tShape ) || enti->IsKind(tGeom))
00191             {
00192               aName = Handle(StepRepr_RepresentationItem)::DownCast(enti)->Name();
00193             }
00194             else if (enti->DynamicType() == tPD)
00195             {
00196               Handle(StepBasic_ProductDefinition) PD =
00197                 Handle(StepBasic_ProductDefinition)::DownCast(enti);
00198               if (PD.IsNull()) continue;
00199 
00200               Handle(StepBasic_Product) Prod = PD->Formation()->OfProduct();
00201               aName = Prod->Name();
00202             }
00203             else
00204             {
00205               continue;
00206             }
00207             if ( aName->UsefullLength() < 1 )
00208               continue;
00209             // skip 'N0NE' name
00210             if ( aName->UsefullLength() == 4 &&
00211                  toupper (aName->Value(1)) == 'N' &&
00212                  toupper (aName->Value(2)) == 'O' &&
00213                  toupper (aName->Value(3)) == 'N' &&
00214                  toupper (aName->Value(4)) == 'E')
00215               continue;
00216 
00217             // special check to pass names like "Open CASCADE STEP translator 6.3 1"
00218             TCollection_AsciiString aSkipName ("Open CASCADE STEP translator");
00219             if (aName->Length() >= aSkipName.Length()) {
00220               if (aName->String().SubString(1, aSkipName.Length()).IsEqual(aSkipName))
00221                 continue;
00222             }
00223             TCollection_ExtendedString aNameExt (aName->ToCString());
00224 
00225             // find target shape
00226             Handle(Transfer_Binder) binder = TP->Find(enti);
00227             if (binder.IsNull()) continue;
00228             TopoDS_Shape S = TransferBRep::ShapeResult(binder);
00229             if (S.IsNull()) continue;
00230 
00231             // as PRODUCT can be included in the main shape
00232             // several times, we look here for all iclusions.
00233             Standard_Integer isub, nbSubs = anIndices.Extent();
00234             for (isub = 1; isub <= nbSubs; isub++)
00235             {
00236               TopoDS_Shape aSub = anIndices.FindKey(isub);
00237               if (aSub.IsPartner(S)) {
00238                 TDF_Label L;
00239                 if (enti->IsKind(tGeom)) {
00240                   // check all named shapes using iterator
00241                   TDF_ChildIDIterator anIt (theShapeLabel, TDataStd_Name::GetID(), Standard_True);
00242                   for (; anIt.More(); anIt.Next()) {
00243                     Handle(TDataStd_Name) nameAttr =
00244                       Handle(TDataStd_Name)::DownCast(anIt.Value());
00245                     if (nameAttr.IsNull()) continue;
00246                     TDF_Label Lab = nameAttr->Label();
00247                     Handle(TNaming_NamedShape) shAttr; 
00248                     if (Lab.FindAttribute(TNaming_NamedShape::GetID(), shAttr) && shAttr->Get().IsEqual(aSub))
00249                       L = Lab;
00250                   }
00251                 }
00252                 // create label and set shape
00253                 if (L.IsNull())
00254                 {
00255                   TDF_TagSource aTag;
00256                   L = aTag.NewChild(theShapeLabel);
00257                   TNaming_Builder tnBuild (L);
00258                   //tnBuild.Generated(S);
00259                   tnBuild.Generated(aSub);
00260                 }
00261                 // set a name
00262                 TDataStd_Name::Set(L, aNameExt);
00263               }
00264             }
00265           }
00266         }
00267         // END: Store names
00268       }
00269       else {
00270 //        switch (status) {
00271 //        case IFSelect_RetVoid:
00272 //          theError = "Nothing created or No data to process";
00273 //          break;
00274 //        case IFSelect_RetError:
00275 //          theError = "Error in command or input data";
00276 //          break;
00277 //        case IFSelect_RetFail:
00278 //          theError = "Execution was run, but has failed";
00279 //          break;
00280 //        case IFSelect_RetStop:
00281 //          theError = "Execution has been stopped. Quite possible, an exception was raised";
00282 //          break;
00283 //        default:
00284 //          break;
00285 //        }
00286         theError = "Wrong format of the imported file. Can't import file.";
00287         aResShape.Nullify();
00288       }
00289     }
00290     catch (Standard_Failure) {
00291       Handle(Standard_Failure) aFail = Standard_Failure::Caught();
00292       theError = aFail->GetMessageString();
00293       aResShape.Nullify();
00294     }
00295     // Return previous locale
00296     return aResShape;
00297   }
00298 }