]> SALOME platform Git repositories - modules/geom.git/blob - src/STEPImport/STEPImport.cxx
Salome HOME
Porting to OCCT6.5.1
[modules/geom.git] / src / STEPImport / STEPImport.cxx
1 // Copyright (C) 2007-2011  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:        STEPImport.cxx
23 //  Created:     Wed May 19 14:41:10 2004
24 //  Author:      Pavel TELKOV
25
26 #include "utilities.h"
27
28 #include <Basics_Utils.hxx>
29
30 #include <CASCatch_OCCTVersion.hxx>
31
32 #include <BRep_Builder.hxx>
33
34 #include <IFSelect_ReturnStatus.hxx>
35
36 #include <STEPControl_Reader.hxx>
37 #include <StepBasic_ProductDefinition.hxx>
38 #include <StepBasic_ProductDefinitionFormation.hxx>
39 #include <StepBasic_Product.hxx>
40 #include <Interface_InterfaceModel.hxx>
41 #include <XSControl_TransferReader.hxx>
42 #include <XSControl_WorkSession.hxx>
43
44 #include <Transfer_Binder.hxx>
45 #include <TNaming_Builder.hxx>
46 #include <TDataStd_Name.hxx>
47 #include <Transfer_TransientProcess.hxx>
48 #include <TransferBRep.hxx>
49
50 #include <TCollection_AsciiString.hxx>
51 #include <TopoDS_Compound.hxx>
52 #include <TopoDS_Shape.hxx>
53 #include <TDF_Label.hxx>
54 #include <TDF_Tool.hxx>
55 #include <Interface_Static.hxx>
56
57 #include <TopTools_IndexedMapOfShape.hxx>
58 #include <TopExp.hxx>
59 #include <TopoDS_Iterator.hxx>
60 #include <BRepTools.hxx>
61
62 #include <Standard_Failure.hxx>
63 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
64
65 #ifdef WNT
66  #if defined STEPIMPORT_EXPORTS || defined STEPImport_EXPORTS
67   #if defined WIN32
68    #define STEPIMPORT_EXPORT __declspec( dllexport )
69   #else
70    #define STEPIMPORT_EXPORT
71   #endif
72  #else
73   #if defined WIN32
74    #define STEPIMPORT_EXPORT __declspec( dllimport )
75   #else
76    #define STEPIMPORT_EXPORT
77   #endif
78  #endif
79 #else
80  #define STEPIMPORT_EXPORT
81 #endif
82
83 //=============================================================================
84 /*!
85  *  Import()
86  */
87 //=============================================================================
88
89 extern "C"
90 {
91   STEPIMPORT_EXPORT
92   TopoDS_Shape Import (const TCollection_AsciiString& theFileName,
93                        const TCollection_AsciiString& /*theFormatName*/,
94                        TCollection_AsciiString&       theError,
95                        const TDF_Label&               theShapeLabel)
96   {
97     MESSAGE("Import STEP model from file " << theFileName.ToCString());
98     // Set "C" numeric locale to save numbers correctly
99     Kernel_Utils::Localizer loc;
100     TopoDS_Shape aResShape;
101     //VRV: OCC 4.0 migration
102     STEPControl_Reader aReader;
103     //VSR: 16/09/09: Convert to METERS
104     Interface_Static::SetCVal("xstep.cascade.unit","M");
105     Interface_Static::SetIVal("read.step.ideas", 1);
106     Interface_Static::SetIVal("read.step.nonmanifold", 1);
107     //VRV: OCC 4.0 migration
108     TopoDS_Compound compound;
109     BRep_Builder B;
110     B.MakeCompound(compound);
111     try {
112 #if OCC_VERSION_LARGE > 0x06010000
113       OCC_CATCH_SIGNALS;
114 #endif
115       IFSelect_ReturnStatus status = aReader.ReadFile(theFileName.ToCString());
116
117       if (status == IFSelect_RetDone) {
118         Standard_Boolean failsonly = Standard_False;
119         aReader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity);
120         /* Root transfers */
121         Standard_Integer nbr = aReader.NbRootsForTransfer();
122         aReader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity);
123
124         for (Standard_Integer n = 1; n <= nbr; n++) {
125           Standard_Boolean ok = aReader.TransferRoot(n);
126           /* Collecting resulting entities */
127           Standard_Integer nbs = aReader.NbShapes();
128           if (!ok || nbs == 0)
129           {
130             // THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::ImportStep", SALOME::BAD_PARAM);
131             continue; // skip empty root
132           }
133           /* For a single entity */
134           else if (nbr == 1 && nbs == 1) {
135             aResShape = aReader.Shape(1);
136             // ATTENTION: this is a workaround for mantis issue 0020442 remark 0010776
137             // It should be removed after patching OCCT for bug OCC22436
138             // (fix for OCCT is expected in service pack next to OCCT6.3sp12)
139             if (aResShape.ShapeType() == TopAbs_COMPOUND) {
140               int nbSub1 = 0;
141               TopoDS_Shape currShape;
142               TopoDS_Iterator It (aResShape, Standard_True, Standard_True);
143               for (; It.More(); It.Next()) {
144                 nbSub1++;
145                 currShape = It.Value();
146               }
147               if (nbSub1 == 1)
148                 aResShape = currShape;
149             }
150             // END workaround
151             break;
152           }
153
154           for (Standard_Integer i = 1; i <= nbs; i++) {
155             TopoDS_Shape aShape = aReader.Shape(i);
156             if (aShape.IsNull()) {
157               // THROW_SALOME_CORBA_EXCEPTION("Null shape in GEOM_Gen_i::ImportStep", SALOME::BAD_PARAM) ;
158               //return aResShape;
159               continue;
160             }
161             else {
162               B.Add(compound, aShape);
163             }
164           }
165         }
166         if (aResShape.IsNull())
167           aResShape = compound;
168
169         // BEGIN: Store names of sub-shapes from file
170         TopTools_IndexedMapOfShape anIndices;
171         TopExp::MapShapes(aResShape, anIndices);
172
173         Handle(Interface_InterfaceModel) Model = aReader.WS()->Model();
174         Handle(XSControl_TransferReader) TR = aReader.WS()->TransferReader();
175         if (!TR.IsNull()) {
176           Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
177           Handle(Standard_Type) tPD  = STANDARD_TYPE(StepBasic_ProductDefinition);
178
179           Standard_Integer nb = Model->NbEntities();
180           for (Standard_Integer ie = 1; ie <= nb; ie++) {
181             Handle(Standard_Transient) enti = Model->Value(ie);
182             if (enti->DynamicType() != tPD) continue;
183
184             Handle(StepBasic_ProductDefinition) PD =
185               Handle(StepBasic_ProductDefinition)::DownCast(enti);
186             if (PD.IsNull()) continue;
187
188             Handle(StepBasic_Product) Prod = PD->Formation()->OfProduct();
189             if (Prod->Name()->UsefullLength() <= 0) continue;
190
191             Handle(TCollection_HAsciiString) aName = Prod->Name();
192             TCollection_ExtendedString aNameExt (aName->ToCString());
193
194             // special check to pass names like "Open CASCADE STEP translator 6.3 1"
195             TCollection_AsciiString aSkipName ("Open CASCADE STEP translator");
196             if (aName->Length() >= aSkipName.Length()) {
197               if (aName->String().SubString(1, aSkipName.Length()).IsEqual(aSkipName))
198                 continue;
199             }
200
201             // find target shape
202             Handle(Transfer_Binder) binder = TP->Find(enti);
203             if (binder.IsNull()) continue;
204             TopoDS_Shape S = TransferBRep::ShapeResult(binder);
205             if (S.IsNull()) continue;
206
207             // as PRODUCT can be included in the main shape
208             // several times, we look here for all iclusions.
209             Standard_Integer isub, nbSubs = anIndices.Extent();
210             for (isub = 1; isub <= nbSubs; isub++)
211             {
212               TopoDS_Shape aSub = anIndices.FindKey(isub);
213               if (aSub.IsPartner(S)) {
214                 // create label and set shape
215                 TDF_Label L;
216                 TDF_TagSource aTag;
217                 L = aTag.NewChild(theShapeLabel);
218                 TNaming_Builder tnBuild (L);
219                 //tnBuild.Generated(S);
220                 tnBuild.Generated(aSub);
221
222                 // set a name
223                 TDataStd_Name::Set(L, aNameExt);
224               }
225             }
226           }
227         }
228         // END: Store names
229       }
230       else {
231 //        switch (status) {
232 //        case IFSelect_RetVoid:
233 //          theError = "Nothing created or No data to process";
234 //          break;
235 //        case IFSelect_RetError:
236 //          theError = "Error in command or input data";
237 //          break;
238 //        case IFSelect_RetFail:
239 //          theError = "Execution was run, but has failed";
240 //          break;
241 //        case IFSelect_RetStop:
242 //          theError = "Execution has been stopped. Quite possible, an exception was raised";
243 //          break;
244 //        default:
245 //          break;
246 //        }
247         theError = "Wrong format of the imported file. Can't import file.";
248         aResShape.Nullify();
249       }
250     }
251     catch (Standard_Failure) {
252       Handle(Standard_Failure) aFail = Standard_Failure::Caught();
253       theError = aFail->GetMessageString();
254       aResShape.Nullify();
255     }
256     // Return previous locale
257     return aResShape;
258   }
259 }