Salome HOME
Check if aUnitName is NULL before testing unit value.
[modules/geom.git] / src / IGESImport / IGESImport.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  File:        IGESImport.cxx
23 //  Created:     Wed May 19 14:36:35 2004
24 //  Author:      Pavel TELKOV
25
26 #include "utilities.h"
27
28 #include <Basics_Utils.hxx>
29
30 #include <IFSelect_ReturnStatus.hxx>
31 #include <IGESControl_Reader.hxx>
32 #include <IGESData_IGESModel.hxx>
33 #include <IGESData_IGESEntity.hxx>
34
35 #include <Interface_Static.hxx>
36 #include <Interface_InterfaceModel.hxx>
37 #include <XSControl_TransferReader.hxx>
38 #include <XSControl_WorkSession.hxx>
39 #include <Transfer_TransientProcess.hxx>
40 #include <Transfer_Binder.hxx>
41 #include <TransferBRep.hxx>
42
43 #include <TNaming_Builder.hxx>
44 #include <TDF_TagSource.hxx>
45 #include <TDataStd_Name.hxx>
46 #include <TDF_Label.hxx>
47
48 #include <TCollection_HAsciiString.hxx>
49 #include <TopoDS_Shape.hxx>
50 #include <TopoDS_Vertex.hxx>
51 #include <BRep_Builder.hxx>
52 #include <gp_Pnt.hxx>
53
54 #ifdef WNT
55  #if defined IGESIMPORT_EXPORTS || defined IGESImport_EXPORTS
56   #if defined WIN32
57    #define IGESIMPORT_EXPORT __declspec( dllexport )
58   #else
59    #define IGESIMPORT_EXPORT
60   #endif
61  #else
62   #if defined WIN32
63    #define IGESIMPORT_EXPORT __declspec( dllimport )
64   #else
65    #define IGESIMPORT_EXPORT
66   #endif
67  #endif
68 #else
69  #define IGESIMPORT_EXPORT
70 #endif
71
72 //=============================================================================
73 /*!
74  *
75  */
76 //=============================================================================
77
78 extern "C"
79 {
80 IGESIMPORT_EXPORT
81   TopoDS_Shape Import (const TCollection_AsciiString& theFileName,
82                        const TCollection_AsciiString& theFormatName,
83                        TCollection_AsciiString&       theError,
84                        const TDF_Label&               theShapeLabel)
85   {
86     // Set "C" numeric locale to save numbers correctly
87     Kernel_Utils::Localizer loc;
88
89     IGESControl_Reader aReader;
90     TopoDS_Shape aResShape;
91     Interface_Static::SetCVal("xstep.cascade.unit","M");
92     try {
93       IFSelect_ReturnStatus status = aReader.ReadFile(theFileName.ToCString());
94
95       if (status == IFSelect_RetDone) {
96
97         if (theFormatName == "IGES_UNIT") {
98           Handle(IGESData_IGESModel) aModel =
99             Handle(IGESData_IGESModel)::DownCast(aReader.Model());
100           gp_Pnt P(1.0,0.0,0.0);
101           if (!aModel.IsNull()) {
102             Handle(TCollection_HAsciiString) aUnitName =
103               aModel->GlobalSection().UnitName();
104             if (!aUnitName.IsNull()) {
105               //cout<<"aUnitName = "<<aUnitName->ToCString()<<endl;
106               //cout<<"aUnitFlag = "<<aModel->GlobalSection().UnitFlag()<<endl;
107               if (aUnitName->String()=="MM") {
108                 P = gp_Pnt(0.001,0.0,0.0);
109               }
110               else if (aUnitName->String()=="CM") {
111                 P = gp_Pnt(0.01,0.0,0.0);
112               }
113             }
114             //else
115             //  cout << "aUnitName is NULL !!" << endl;
116           }
117           BRep_Builder B;
118           TopoDS_Vertex V;
119           B.MakeVertex(V,P,1.e-7);
120           aResShape = V;
121           return aResShape;
122         }
123         if (theFormatName == "IGES_SCALE") {
124           //cout<<"need re-scale a model"<<endl;
125           // set UnitFlag to 'meter'
126           Handle(IGESData_IGESModel) aModel =
127             Handle(IGESData_IGESModel)::DownCast(aReader.Model());
128           if (!aModel.IsNull()) {
129             IGESData_GlobalSection aGS = aModel->GlobalSection();
130             aGS.SetUnitFlag(6);
131             aModel->SetGlobalSection(aGS);
132           }
133         }
134
135         MESSAGE("ImportIGES : all Geometry Transfer");
136         //OCC 5.1.2 porting
137         //     aReader.Clear();
138         //     aReader.TransferRoots(false);
139         aReader.ClearShapes();
140         aReader.TransferRoots();
141
142         MESSAGE("ImportIGES : count of shapes produced = " << aReader.NbShapes());
143         aResShape = aReader.OneShape();
144
145         // BEGIN: Store names of sub-shapes from file
146         Handle(Interface_InterfaceModel) Model = aReader.WS()->Model();
147         Handle(XSControl_TransferReader) TR = aReader.WS()->TransferReader();
148         if (!TR.IsNull()) {
149           Handle(Transfer_TransientProcess) TP = /*TransientProcess();*/TR->TransientProcess();
150           Standard_Integer nb = Model->NbEntities();
151           for (Standard_Integer i = 1; i <= nb; i++) {
152             Handle(IGESData_IGESEntity) ent = Handle(IGESData_IGESEntity)::DownCast(Model->Value(i));
153             if (ent.IsNull() || ! ent->HasName()) continue;
154
155             // find target shape
156             Handle(Transfer_Binder) binder = TP->Find(ent);
157             if (binder.IsNull()) continue;
158             TopoDS_Shape S = TransferBRep::ShapeResult(binder);
159             if (S.IsNull()) continue;
160
161             // create label and set shape
162             TDF_Label L;
163             TDF_TagSource aTag;
164             L = aTag.NewChild(theShapeLabel);
165             TNaming_Builder tnBuild (L);
166             tnBuild.Generated(S);
167
168             // set a name
169             TCollection_AsciiString string = ent->NameValue()->String();
170             string.LeftAdjust();
171             string.RightAdjust();
172             TCollection_ExtendedString str (string);
173             TDataStd_Name::Set(L, str);
174           }
175         }
176         // END: Store names
177       } else {
178 //        switch (status) {
179 //        case IFSelect_RetVoid:
180 //          theError = "Nothing created or No data to process";
181 //          break;
182 //        case IFSelect_RetError:
183 //          theError = "Error in command or input data";
184 //          break;
185 //        case IFSelect_RetFail:
186 //          theError = "Execution was run, but has failed";
187 //          break;
188 //        case IFSelect_RetStop:
189 //          theError = "Execution has been stopped. Quite possible, an exception was raised";
190 //          break;
191 //        default:
192 //          break;
193 //        }
194         theError = "Wrong format of the imported file. Can't import file.";
195         aResShape.Nullify();
196       }
197     }
198     catch(Standard_Failure) {
199       Handle(Standard_Failure) aFail = Standard_Failure::Caught();
200       theError = aFail->GetMessageString();
201       aResShape.Nullify();
202     }
203     return aResShape;
204   }
205 }