1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <GeomAlgoAPI_STEPImport.h>
23 #include <TDF_ChildIDIterator.hxx>
24 #include <TDF_Label.hxx>
25 #include <TDataStd_Name.hxx>
26 #include <TDataStd_Comment.hxx>
27 #include <TNaming_Builder.hxx>
28 #include <TNaming_NamedShape.hxx>
30 #include <IFSelect_ReturnStatus.hxx>
31 #include <Interface_EntityIterator.hxx>
32 #include <Interface_Graph.hxx>
33 #include <Interface_InterfaceModel.hxx>
34 #include <Interface_Static.hxx>
35 #include <STEPControl_Reader.hxx>
36 #include <StepBasic_Product.hxx>
37 #include <StepBasic_ProductDefinition.hxx>
38 #include <StepBasic_ProductDefinitionFormation.hxx>
39 #include <StepGeom_GeometricRepresentationItem.hxx>
40 #include <StepShape_TopologicalRepresentationItem.hxx>
41 #include <StepRepr_DescriptiveRepresentationItem.hxx>
42 #include <StepRepr_ProductDefinitionShape.hxx>
43 #include <StepRepr_PropertyDefinitionRepresentation.hxx>
44 #include <StepRepr_Representation.hxx>
45 #include <TransferBRep.hxx>
46 #include <Transfer_Binder.hxx>
47 #include <Transfer_TransientProcess.hxx>
48 #include <XSControl_TransferReader.hxx>
49 #include <XSControl_WorkSession.hxx>
51 #include <BRep_Builder.hxx>
54 #include <TopExp_Explorer.hxx>
55 #include <TopTools_IndexedMapOfShape.hxx>
56 #include <TopoDS_Compound.hxx>
57 #include <TopoDS_Iterator.hxx>
58 #include <TopoDS_Shape.hxx>
61 #include <TColStd_SequenceOfAsciiString.hxx>
63 #include <Standard_Failure.hxx>
64 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
65 std::shared_ptr<GeomAPI_Shape> STEPImport(const std::string& theFileName,
66 const std::string& theFormatName,
67 std::string& theError)
69 TopoDS_Shape aResShape;
71 // Set "C" numeric locale to save numbers correctly
72 // Kernel_Utils::Localizer loc;
74 STEPControl_Reader aReader;
76 //VSR: 16/09/09: Convert to METERS
77 Interface_Static::SetCVal("xstep.cascade.unit","M");
78 Interface_Static::SetIVal("read.step.ideas", 1);
79 Interface_Static::SetIVal("read.step.nonmanifold", 1);
82 TopoDS_Compound compound;
83 B.MakeCompound(compound);
88 IFSelect_ReturnStatus status = aReader.ReadFile(theFileName.c_str());
90 if (status == IFSelect_RetDone) {
92 // Regard or not the model units
93 if (theFormatName == "STEP_SCALE") {
94 // set UnitFlag to units from file
95 TColStd_SequenceOfAsciiString anUnitLengthNames;
96 TColStd_SequenceOfAsciiString anUnitAngleNames;
97 TColStd_SequenceOfAsciiString anUnitSolidAngleNames;
98 aReader.FileUnits(anUnitLengthNames, anUnitAngleNames, anUnitSolidAngleNames);
99 if (anUnitLengthNames.Length() > 0) {
100 TCollection_AsciiString aLenUnits = anUnitLengthNames.First();
101 if (aLenUnits == "millimetre")
102 Interface_Static::SetCVal("xstep.cascade.unit", "MM");
103 else if (aLenUnits == "centimetre")
104 Interface_Static::SetCVal("xstep.cascade.unit", "CM");
105 else if (aLenUnits == "metre" || aLenUnits.IsEmpty())
106 Interface_Static::SetCVal("xstep.cascade.unit", "M");
107 else if (aLenUnits == "INCH")
108 Interface_Static::SetCVal("xstep.cascade.unit", "INCH");
110 theError = "The file contains not supported units.";
111 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
112 aGeomShape->setImpl(new TopoDS_Shape(aResShape));
115 // TODO (for other units than mm, cm, m or inch)
116 //else if (aLenUnits == "")
117 // Interface_Static::SetCVal("xstep.cascade.unit", "???");
121 //cout<<"need re-scale a model"<<endl;
122 // set UnitFlag to 'meter'
123 Interface_Static::SetCVal("xstep.cascade.unit","M");
126 Standard_Boolean failsonly = Standard_False;
127 aReader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity);
130 Standard_Integer nbr = aReader.NbRootsForTransfer();
131 aReader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity);
133 for (Standard_Integer n = 1; n <= nbr; n++) {
134 Standard_Boolean ok = aReader.TransferRoot(n);
135 /* Collecting resulting entities */
136 Standard_Integer nbs = aReader.NbShapes();
139 continue; // skip empty root
141 /* For a single entity */
142 else if (nbr == 1 && nbs == 1) {
143 aResShape = aReader.Shape(1);
144 // ATTENTION: this is a workaround for mantis issue 0020442 remark 0010776
145 // It should be removed after patching OCCT for bug OCC22436
146 // (fix for OCCT is expected in service pack next to OCCT6.3sp12)
147 if (aResShape.ShapeType() == TopAbs_COMPOUND) {
149 TopoDS_Shape currShape;
150 TopoDS_Iterator It (aResShape, Standard_True, Standard_True);
151 for (; It.More(); It.Next()) {
153 currShape = It.Value();
156 aResShape = currShape;
162 for (Standard_Integer i = 1; i <= nbs; i++) {
163 TopoDS_Shape aShape = aReader.Shape(i);
164 if (aShape.IsNull()) {
169 B.Add(compound, aShape);
173 if (aResShape.IsNull())
174 aResShape = compound;
176 // Check if any BRep entity has been read, there must be at least a vertex
177 if ( !TopExp_Explorer( aResShape, TopAbs_VERTEX ).More() )
179 theError = "No geometrical data in the imported file.";
180 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
181 aGeomShape->setImpl(new TopoDS_Shape());
185 theError = "Wrong format of the imported file. Can't import file.";
189 catch (Standard_Failure) {
190 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
191 theError = aFail->GetMessageString();
194 // Return previous locale
195 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
196 aGeomShape->setImpl(new TopoDS_Shape(aResShape));