Salome HOME
Merge remote-tracking branch 'origin/Toolbars_Management'
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_STEPImport.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <GeomAlgoAPI_STEPImport.h>
22
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>
29
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>
50
51 #include <BRep_Builder.hxx>
52
53 #include <TopExp.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>
59
60
61 #include <TColStd_SequenceOfAsciiString.hxx>
62
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)
68 {
69   TopoDS_Shape aResShape;
70
71   // Set "C" numeric locale to save numbers correctly
72   // Kernel_Utils::Localizer loc;
73
74   STEPControl_Reader aReader;
75
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);
80
81   BRep_Builder B;
82   TopoDS_Compound compound;
83   B.MakeCompound(compound);
84
85   try {
86     OCC_CATCH_SIGNALS;
87
88     IFSelect_ReturnStatus status = aReader.ReadFile(theFileName.c_str());
89
90     if (status == IFSelect_RetDone) {
91
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");
109           else {
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));
113             return aGeomShape;
114           }
115           // TODO (for other units than mm, cm, m or inch)
116           //else if (aLenUnits == "")
117           //  Interface_Static::SetCVal("xstep.cascade.unit", "???");
118         }
119       }
120       else {
121         //cout<<"need re-scale a model"<<endl;
122         // set UnitFlag to 'meter'
123         Interface_Static::SetCVal("xstep.cascade.unit","M");
124       }
125
126       Standard_Boolean failsonly = Standard_False;
127       aReader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity);
128
129       /* Root transfers */
130       Standard_Integer nbr = aReader.NbRootsForTransfer();
131       aReader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity);
132
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();
137         if (!ok || nbs == 0)
138         {
139           continue; // skip empty root
140         }
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) {
148             int nbSub1 = 0;
149             TopoDS_Shape currShape;
150             TopoDS_Iterator It (aResShape, Standard_True, Standard_True);
151             for (; It.More(); It.Next()) {
152               nbSub1++;
153               currShape = It.Value();
154             }
155             if (nbSub1 == 1)
156               aResShape = currShape;
157           }
158           // END workaround
159           break;
160         }
161
162         for (Standard_Integer i = 1; i <= nbs; i++) {
163           TopoDS_Shape aShape = aReader.Shape(i);
164           if (aShape.IsNull()) {
165             //return aResShape;
166             continue;
167           }
168           else {
169             B.Add(compound, aShape);
170           }
171         }
172       }
173       if (aResShape.IsNull())
174         aResShape = compound;
175
176       // Check if any BRep entity has been read, there must be at least a vertex
177       if ( !TopExp_Explorer( aResShape, TopAbs_VERTEX ).More() )
178       {
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());
182         return aGeomShape;
183       }
184     } else {
185       theError = "Wrong format of the imported file. Can't import file.";
186       aResShape.Nullify();
187     }
188   }
189   catch (Standard_Failure const& anException) {
190     theError = anException.GetMessageString();
191     aResShape.Nullify();
192   }
193   // Return previous locale
194   std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
195   aGeomShape->setImpl(new TopoDS_Shape(aResShape));
196   return aGeomShape;
197 }