1 // Copyright (C) 2007-2010 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 <BRep_Builder.hxx>
32 #include <IFSelect_ReturnStatus.hxx>
34 #include <STEPControl_Reader.hxx>
35 #include <StepBasic_ProductDefinition.hxx>
36 #include <StepBasic_ProductDefinitionFormation.hxx>
37 #include <StepBasic_Product.hxx>
38 #include <Interface_InterfaceModel.hxx>
39 #include <XSControl_TransferReader.hxx>
40 #include <XSControl_WorkSession.hxx>
42 #include <Transfer_Binder.hxx>
43 #include <TNaming_Builder.hxx>
44 #include <TDataStd_Name.hxx>
45 #include <Transfer_TransientProcess.hxx>
46 #include <TransferBRep.hxx>
48 #include <TCollection_AsciiString.hxx>
49 #include <TopoDS_Compound.hxx>
50 #include <TopoDS_Shape.hxx>
51 #include <TDF_Label.hxx>
52 #include <TDF_Tool.hxx>
53 #include <Interface_Static.hxx>
55 #include <TopTools_IndexedMapOfShape.hxx>
57 #include <TopoDS_Iterator.hxx>
58 #include <BRepTools.hxx>
60 #include <Standard_Failure.hxx>
61 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
64 #if defined STEPIMPORT_EXPORTS || defined STEPImport_EXPORTS
66 #define STEPIMPORT_EXPORT __declspec( dllexport )
68 #define STEPIMPORT_EXPORT
72 #define STEPIMPORT_EXPORT __declspec( dllimport )
74 #define STEPIMPORT_EXPORT
78 #define STEPIMPORT_EXPORT
81 //=============================================================================
85 //=============================================================================
90 TopoDS_Shape Import (const TCollection_AsciiString& theFileName,
91 const TCollection_AsciiString& /*theFormatName*/,
92 TCollection_AsciiString& theError,
93 const TDF_Label& theShapeLabel)
95 MESSAGE("Import STEP model from file " << theFileName.ToCString());
96 // Set "C" numeric locale to save numbers correctly
97 Kernel_Utils::Localizer loc;
98 TopoDS_Shape aResShape;
99 //VRV: OCC 4.0 migration
100 STEPControl_Reader aReader;
101 //VSR: 16/09/09: Convert to METERS
102 Interface_Static::SetCVal("xstep.cascade.unit","M");
103 Interface_Static::SetIVal("read.step.ideas", 1);
104 Interface_Static::SetIVal("read.step.nonmanifold", 1);
105 //VRV: OCC 4.0 migration
106 TopoDS_Compound compound;
108 B.MakeCompound(compound);
110 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
113 IFSelect_ReturnStatus status = aReader.ReadFile(theFileName.ToCString());
115 if (status == IFSelect_RetDone) {
116 Standard_Boolean failsonly = Standard_False;
117 aReader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity);
119 Standard_Integer nbr = aReader.NbRootsForTransfer();
120 aReader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity);
122 for (Standard_Integer n = 1; n <= nbr; n++) {
123 Standard_Boolean ok = aReader.TransferRoot(n);
124 /* Collecting resulting entities */
125 Standard_Integer nbs = aReader.NbShapes();
128 // THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::ImportStep", SALOME::BAD_PARAM);
129 continue; // skip empty root
131 /* For a single entity */
132 else if (nbr == 1 && nbs == 1) {
133 aResShape = aReader.Shape(1);
137 for (Standard_Integer i = 1; i <= nbs; i++) {
138 TopoDS_Shape aShape = aReader.Shape(i);
139 if (aShape.IsNull()) {
140 // THROW_SALOME_CORBA_EXCEPTION("Null shape in GEOM_Gen_i::ImportStep", SALOME::BAD_PARAM) ;
145 B.Add(compound, aShape);
149 if (aResShape.IsNull())
150 aResShape = compound;
152 // BEGIN: Store names of sub-shapes from file
153 TopTools_IndexedMapOfShape anIndices;
154 TopExp::MapShapes(aResShape, anIndices);
156 Handle(Interface_InterfaceModel) Model = aReader.WS()->Model();
157 Handle(XSControl_TransferReader) TR = aReader.WS()->TransferReader();
159 Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
160 Handle(Standard_Type) tPD = STANDARD_TYPE(StepBasic_ProductDefinition);
162 Standard_Integer nb = Model->NbEntities();
163 for (Standard_Integer ie = 1; ie <= nb; ie++) {
164 Handle(Standard_Transient) enti = Model->Value(ie);
165 if (enti->DynamicType() != tPD) continue;
167 Handle(StepBasic_ProductDefinition) PD =
168 Handle(StepBasic_ProductDefinition)::DownCast(enti);
169 if (PD.IsNull()) continue;
171 Handle(StepBasic_Product) Prod = PD->Formation()->OfProduct();
172 if (Prod->Name()->UsefullLength() <= 0) continue;
174 Handle(TCollection_HAsciiString) aName = Prod->Name();
175 TCollection_ExtendedString aNameExt (aName->ToCString());
177 // special check to pass names like "Open CASCADE STEP translator 6.3 1"
178 TCollection_AsciiString aSkipName ("Open CASCADE STEP translator");
179 if (aName->Length() >= aSkipName.Length()) {
180 if (aName->String().SubString(1, aSkipName.Length()).IsEqual(aSkipName))
185 Handle(Transfer_Binder) binder = TP->Find(enti);
186 if (binder.IsNull()) continue;
187 TopoDS_Shape S = TransferBRep::ShapeResult(binder);
188 if (S.IsNull()) continue;
190 // as PRODUCT can be included in the main shape
191 // several times, we look here for all iclusions.
192 Standard_Integer isub, nbSubs = anIndices.Extent();
193 for (isub = 1; isub <= nbSubs; isub++)
195 TopoDS_Shape aSub = anIndices.FindKey(isub);
196 if (aSub.IsPartner(S)) {
197 // create label and set shape
200 L = aTag.NewChild(theShapeLabel);
201 TNaming_Builder tnBuild (L);
202 //tnBuild.Generated(S);
203 tnBuild.Generated(aSub);
206 TDataStd_Name::Set(L, aNameExt);
215 // case IFSelect_RetVoid:
216 // theError = "Nothing created or No data to process";
218 // case IFSelect_RetError:
219 // theError = "Error in command or input data";
221 // case IFSelect_RetFail:
222 // theError = "Execution was run, but has failed";
224 // case IFSelect_RetStop:
225 // theError = "Execution has been stopped. Quite possible, an exception was raised";
230 theError = "Wrong format of the imported file. Can't import file.";
234 catch (Standard_Failure) {
235 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
236 theError = aFail->GetMessageString();
239 // Return previous locale