Back to index

salome-geom  6.5.0
IGESImport.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 //  File:        IGESImport.cxx
00023 //  Created:     Wed May 19 14:36:35 2004
00024 //  Author:      Pavel TELKOV
00025 
00026 #include "utilities.h"
00027 
00028 #include <Basics_Utils.hxx>
00029 
00030 #include <IFSelect_ReturnStatus.hxx>
00031 #include <IGESControl_Reader.hxx>
00032 #include <IGESData_IGESModel.hxx>
00033 #include <IGESData_IGESEntity.hxx>
00034 
00035 #include <Interface_Static.hxx>
00036 #include <Interface_InterfaceModel.hxx>
00037 #include <XSControl_TransferReader.hxx>
00038 #include <XSControl_WorkSession.hxx>
00039 #include <Transfer_TransientProcess.hxx>
00040 #include <Transfer_Binder.hxx>
00041 #include <TransferBRep.hxx>
00042 
00043 #include <TNaming_Builder.hxx>
00044 #include <TDF_TagSource.hxx>
00045 #include <TDataStd_Name.hxx>
00046 #include <TDF_Label.hxx>
00047 
00048 #include <TCollection_HAsciiString.hxx>
00049 #include <TopoDS_Shape.hxx>
00050 #include <TopoDS_Vertex.hxx>
00051 #include <BRep_Builder.hxx>
00052 #include <gp_Pnt.hxx>
00053 
00054 #ifdef WNT
00055  #if defined IGESIMPORT_EXPORTS || defined IGESImport_EXPORTS
00056   #if defined WIN32
00057    #define IGESIMPORT_EXPORT __declspec( dllexport )
00058   #else
00059    #define IGESIMPORT_EXPORT
00060   #endif
00061  #else
00062   #if defined WIN32
00063    #define IGESIMPORT_EXPORT __declspec( dllimport )
00064   #else
00065    #define IGESIMPORT_EXPORT
00066   #endif
00067  #endif
00068 #else
00069  #define IGESIMPORT_EXPORT
00070 #endif
00071 
00072 //=============================================================================
00076 //=============================================================================
00077 
00078 extern "C"
00079 {
00080 IGESIMPORT_EXPORT
00081   TopoDS_Shape Import (const TCollection_AsciiString& theFileName,
00082                        const TCollection_AsciiString& theFormatName,
00083                        TCollection_AsciiString&       theError,
00084                        const TDF_Label&               theShapeLabel)
00085   {
00086     // Set "C" numeric locale to save numbers correctly
00087     Kernel_Utils::Localizer loc;
00088 
00089     IGESControl_Reader aReader;
00090     TopoDS_Shape aResShape;
00091     Interface_Static::SetCVal("xstep.cascade.unit","M");
00092     try {
00093       IFSelect_ReturnStatus status = aReader.ReadFile(theFileName.ToCString());
00094 
00095       if (status == IFSelect_RetDone) {
00096 
00097         if (theFormatName == "IGES_UNIT") {
00098           Handle(IGESData_IGESModel) aModel =
00099             Handle(IGESData_IGESModel)::DownCast(aReader.Model());
00100           gp_Pnt P(1.0,0.0,0.0);
00101           if (!aModel.IsNull()) {
00102             Handle(TCollection_HAsciiString) aUnitName =
00103               aModel->GlobalSection().UnitName();
00104             //cout<<"aUnitName = "<<aUnitName->ToCString()<<endl;
00105             //cout<<"aUnitFlag = "<<aModel->GlobalSection().UnitFlag()<<endl;
00106             if (aUnitName->String()=="MM") {
00107               P = gp_Pnt(0.001,0.0,0.0);
00108             }
00109             else if (aUnitName->String()=="CM") {
00110               P = gp_Pnt(0.01,0.0,0.0);
00111             }
00112           }
00113           BRep_Builder B;
00114           TopoDS_Vertex V;
00115           B.MakeVertex(V,P,1.e-7);
00116           aResShape = V;
00117           return aResShape;
00118         }
00119         if (theFormatName == "IGES_SCALE") {
00120           //cout<<"need re-scale a model"<<endl;
00121           // set UnitFlag to 'meter'
00122           Handle(IGESData_IGESModel) aModel =
00123             Handle(IGESData_IGESModel)::DownCast(aReader.Model());
00124           if (!aModel.IsNull()) {
00125             IGESData_GlobalSection aGS = aModel->GlobalSection();
00126             aGS.SetUnitFlag(6);
00127             aModel->SetGlobalSection(aGS);
00128           }
00129         }
00130 
00131         MESSAGE("ImportIGES : all Geometry Transfer");
00132         //OCC 5.1.2 porting
00133         //     aReader.Clear();
00134         //     aReader.TransferRoots(false);
00135         aReader.ClearShapes();
00136         aReader.TransferRoots();
00137 
00138         MESSAGE("ImportIGES : count of shapes produced = " << aReader.NbShapes());
00139         aResShape = aReader.OneShape();
00140 
00141         // BEGIN: Store names of sub-shapes from file
00142         Handle(Interface_InterfaceModel) Model = aReader.WS()->Model();
00143         Handle(XSControl_TransferReader) TR = aReader.WS()->TransferReader();
00144         if (!TR.IsNull()) {
00145           Handle(Transfer_TransientProcess) TP = /*TransientProcess();*/TR->TransientProcess();
00146           Standard_Integer nb = Model->NbEntities();
00147           for (Standard_Integer i = 1; i <= nb; i++) {
00148             Handle(IGESData_IGESEntity) ent = Handle(IGESData_IGESEntity)::DownCast(Model->Value(i));
00149             if (ent.IsNull() || ! ent->HasName()) continue;
00150 
00151             // find target shape
00152             Handle(Transfer_Binder) binder = TP->Find(ent);
00153             if (binder.IsNull()) continue;
00154             TopoDS_Shape S = TransferBRep::ShapeResult(binder);
00155             if (S.IsNull()) continue;
00156 
00157             // create label and set shape
00158             TDF_Label L;
00159             TDF_TagSource aTag;
00160             L = aTag.NewChild(theShapeLabel);
00161             TNaming_Builder tnBuild (L);
00162             tnBuild.Generated(S);
00163 
00164             // set a name
00165             TCollection_AsciiString string = ent->NameValue()->String();
00166             string.LeftAdjust();
00167             string.RightAdjust();
00168             TCollection_ExtendedString str (string);
00169             TDataStd_Name::Set(L, str);
00170           }
00171         }
00172         // END: Store names
00173       } else {
00174 //        switch (status) {
00175 //        case IFSelect_RetVoid:
00176 //          theError = "Nothing created or No data to process";
00177 //          break;
00178 //        case IFSelect_RetError:
00179 //          theError = "Error in command or input data";
00180 //          break;
00181 //        case IFSelect_RetFail:
00182 //          theError = "Execution was run, but has failed";
00183 //          break;
00184 //        case IFSelect_RetStop:
00185 //          theError = "Execution has been stopped. Quite possible, an exception was raised";
00186 //          break;
00187 //        default:
00188 //          break;
00189 //        }
00190         theError = "Wrong format of the imported file. Can't import file.";
00191         aResShape.Nullify();
00192       }
00193     }
00194     catch(Standard_Failure) {
00195       Handle(Standard_Failure) aFail = Standard_Failure::Caught();
00196       theError = aFail->GetMessageString();
00197       aResShape.Nullify();
00198     }
00199     return aResShape;
00200   }
00201 }