1 // Copyright (C) 2007-2012 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
23 // File: STEPImport.cxx
24 // Created: Wed May 19 14:41:10 2004
25 // Author: Pavel TELKOV
27 #include "utilities.h"
29 #include <Basics_Utils.hxx>
31 #include <Basics_OCCTVersion.hxx>
33 #include <BRep_Builder.hxx>
35 #include <IFSelect_ReturnStatus.hxx>
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>
47 #include <Transfer_Binder.hxx>
48 #include <TNaming_Builder.hxx>
49 #include <TDataStd_Name.hxx>
50 #include <Transfer_TransientProcess.hxx>
51 #include <TransferBRep.hxx>
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>
62 #include <TopTools_IndexedMapOfShape.hxx>
64 #include <TopoDS_Iterator.hxx>
65 #include <BRepTools.hxx>
67 #include <Standard_Failure.hxx>
68 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
71 #if defined STEPIMPORT_EXPORTS || defined STEPImport_EXPORTS
73 #define STEPIMPORT_EXPORT __declspec( dllexport )
75 #define STEPIMPORT_EXPORT
79 #define STEPIMPORT_EXPORT __declspec( dllimport )
81 #define STEPIMPORT_EXPORT
85 #define STEPIMPORT_EXPORT
88 //=============================================================================
92 //=============================================================================
97 TopoDS_Shape Import (const TCollection_AsciiString& theFileName,
98 const TCollection_AsciiString& /*theFormatName*/,
99 TCollection_AsciiString& theError,
100 const TDF_Label& theShapeLabel)
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;
115 B.MakeCompound(compound);
117 #if OCC_VERSION_LARGE > 0x06010000
120 IFSelect_ReturnStatus status = aReader.ReadFile(theFileName.ToCString());
122 if (status == IFSelect_RetDone) {
123 Standard_Boolean failsonly = Standard_False;
124 aReader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity);
126 Standard_Integer nbr = aReader.NbRootsForTransfer();
127 aReader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity);
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();
135 // THROW_SALOME_CORBA_EXCEPTION("Exception catched in GEOM_Gen_i::ImportStep", SALOME::BAD_PARAM);
136 continue; // skip empty root
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) {
146 TopoDS_Shape currShape;
147 TopoDS_Iterator It (aResShape, Standard_True, Standard_True);
148 for (; It.More(); It.Next()) {
150 currShape = It.Value();
153 aResShape = currShape;
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) ;
167 B.Add(compound, aShape);
171 if (aResShape.IsNull())
172 aResShape = compound;
174 // BEGIN: Store names of sub-shapes from file
175 TopTools_IndexedMapOfShape anIndices;
176 TopExp::MapShapes(aResShape, anIndices);
178 Handle(Interface_InterfaceModel) Model = aReader.WS()->Model();
179 Handle(XSControl_TransferReader) TR = aReader.WS()->TransferReader();
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);
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))
192 aName = Handle(StepRepr_RepresentationItem)::DownCast(enti)->Name();
194 else if (enti->DynamicType() == tPD)
196 Handle(StepBasic_ProductDefinition) PD =
197 Handle(StepBasic_ProductDefinition)::DownCast(enti);
198 if (PD.IsNull()) continue;
200 Handle(StepBasic_Product) Prod = PD->Formation()->OfProduct();
201 aName = Prod->Name();
207 if ( aName->UsefullLength() < 1 )
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')
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))
223 TCollection_ExtendedString aNameExt (aName->ToCString());
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;
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++)
236 TopoDS_Shape aSub = anIndices.FindKey(isub);
237 if (aSub.IsPartner(S)) {
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))
252 // create label and set shape
256 L = aTag.NewChild(theShapeLabel);
257 TNaming_Builder tnBuild (L);
258 //tnBuild.Generated(S);
259 tnBuild.Generated(aSub);
262 TDataStd_Name::Set(L, aNameExt);
271 // case IFSelect_RetVoid:
272 // theError = "Nothing created or No data to process";
274 // case IFSelect_RetError:
275 // theError = "Error in command or input data";
277 // case IFSelect_RetFail:
278 // theError = "Execution was run, but has failed";
280 // case IFSelect_RetStop:
281 // theError = "Execution has been stopped. Quite possible, an exception was raised";
286 theError = "Wrong format of the imported file. Can't import file.";
290 catch (Standard_Failure) {
291 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
292 theError = aFail->GetMessageString();
295 // Return previous locale