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