1 // Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File: STEPImport.cxx
23 // Created: Wed May 19 14:41:10 2004
24 // Author: Pavel TELKOV
26 #include "utilities.h"
28 #include <Basics_Utils.hxx>
30 #include <Basics_OCCTVersion.hxx>
32 #include <BRep_Builder.hxx>
34 #include <IFSelect_ReturnStatus.hxx>
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>
44 #include <Transfer_Binder.hxx>
45 #include <TNaming_Builder.hxx>
46 #include <TDataStd_Name.hxx>
47 #include <Transfer_TransientProcess.hxx>
48 #include <TransferBRep.hxx>
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>
57 #include <TopTools_IndexedMapOfShape.hxx>
59 #include <TopoDS_Iterator.hxx>
60 #include <BRepTools.hxx>
62 #include <Standard_Failure.hxx>
63 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
66 #if defined STEPIMPORT_EXPORTS || defined STEPImport_EXPORTS
68 #define STEPIMPORT_EXPORT __declspec( dllexport )
70 #define STEPIMPORT_EXPORT
74 #define STEPIMPORT_EXPORT __declspec( dllimport )
76 #define STEPIMPORT_EXPORT
80 #define STEPIMPORT_EXPORT
83 //=============================================================================
87 //=============================================================================
92 TopoDS_Shape Import (const TCollection_AsciiString& theFileName,
93 const TCollection_AsciiString& /*theFormatName*/,
94 TCollection_AsciiString& theError,
95 const TDF_Label& theShapeLabel)
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;
110 B.MakeCompound(compound);
112 #if OCC_VERSION_LARGE > 0x06010000
115 IFSelect_ReturnStatus status = aReader.ReadFile(theFileName.ToCString());
117 if (status == IFSelect_RetDone) {
118 Standard_Boolean failsonly = Standard_False;
119 aReader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity);
121 Standard_Integer nbr = aReader.NbRootsForTransfer();
122 aReader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity);
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();
130 // THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::ImportStep", SALOME::BAD_PARAM);
131 continue; // skip empty root
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) {
141 TopoDS_Shape currShape;
142 TopoDS_Iterator It (aResShape, Standard_True, Standard_True);
143 for (; It.More(); It.Next()) {
145 currShape = It.Value();
148 aResShape = currShape;
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) ;
162 B.Add(compound, aShape);
166 if (aResShape.IsNull())
167 aResShape = compound;
169 // BEGIN: Store names of sub-shapes from file
170 TopTools_IndexedMapOfShape anIndices;
171 TopExp::MapShapes(aResShape, anIndices);
173 Handle(Interface_InterfaceModel) Model = aReader.WS()->Model();
174 Handle(XSControl_TransferReader) TR = aReader.WS()->TransferReader();
176 Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
177 Handle(Standard_Type) tPD = STANDARD_TYPE(StepBasic_ProductDefinition);
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;
184 Handle(StepBasic_ProductDefinition) PD =
185 Handle(StepBasic_ProductDefinition)::DownCast(enti);
186 if (PD.IsNull()) continue;
188 Handle(StepBasic_Product) Prod = PD->Formation()->OfProduct();
189 if (Prod->Name()->UsefullLength() <= 0) continue;
191 Handle(TCollection_HAsciiString) aName = Prod->Name();
192 TCollection_ExtendedString aNameExt (aName->ToCString());
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))
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;
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++)
212 TopoDS_Shape aSub = anIndices.FindKey(isub);
213 if (aSub.IsPartner(S)) {
214 // create label and set shape
217 L = aTag.NewChild(theShapeLabel);
218 TNaming_Builder tnBuild (L);
219 //tnBuild.Generated(S);
220 tnBuild.Generated(aSub);
223 TDataStd_Name::Set(L, aNameExt);
232 // case IFSelect_RetVoid:
233 // theError = "Nothing created or No data to process";
235 // case IFSelect_RetError:
236 // theError = "Error in command or input data";
238 // case IFSelect_RetFail:
239 // theError = "Execution was run, but has failed";
241 // case IFSelect_RetStop:
242 // theError = "Execution has been stopped. Quite possible, an exception was raised";
247 theError = "Wrong format of the imported file. Can't import file.";
251 catch (Standard_Failure) {
252 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
253 theError = aFail->GetMessageString();
256 // Return previous locale