0026937: Eliminate NO_CXX_EXCEPTION macro support
[occt.git] / src / IGESToBRep / IGESToBRep.cxx
1 // Created on: 1994-03-22
2 // Created by: GUYOT and UNTEREINER
3 // Copyright (c) 1994-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 // 21.12.98 rln, gka S4054
18 // 06.01.99 pdn  transmission from ShapeTool_MakeWire back to BRepAPI_MakeWire
19 // 19.03.99 abv  //:q5: code improvement, unnecessary includes removed
20 //S4181 pdn 15.04.99 Recognition of elementary surfaces as basic.
21
22 #include <BRep_Builder.hxx>
23 #include <BRep_Tool.hxx>
24 #include <Geom2d_Curve.hxx>
25 #include <Geom_Curve.hxx>
26 #include <Geom_Surface.hxx>
27 #include <GeomLib.hxx>
28 #include <gp_Pnt.hxx>
29 #include <gp_Pnt2d.hxx>
30 #include <IGESBasic_SingleParent.hxx>
31 #include <IGESData_IGESEntity.hxx>
32 #include <IGESGeom_Boundary.hxx>
33 #include <IGESGeom_BoundedSurface.hxx>
34 #include <IGESGeom_BSplineCurve.hxx>
35 #include <IGESGeom_BSplineSurface.hxx>
36 #include <IGESGeom_CircularArc.hxx>
37 #include <IGESGeom_CompositeCurve.hxx>
38 #include <IGESGeom_ConicArc.hxx>
39 #include <IGESGeom_CopiousData.hxx>
40 #include <IGESGeom_CurveOnSurface.hxx>
41 #include <IGESGeom_Line.hxx>
42 #include <IGESGeom_OffsetCurve.hxx>
43 #include <IGESGeom_OffsetSurface.hxx>
44 #include <IGESGeom_Plane.hxx>
45 #include <IGESGeom_Point.hxx>
46 #include <IGESGeom_RuledSurface.hxx>
47 #include <IGESGeom_SplineCurve.hxx>
48 #include <IGESGeom_SplineSurface.hxx>
49 #include <IGESGeom_SurfaceOfRevolution.hxx>
50 #include <IGESGeom_TabulatedCylinder.hxx>
51 #include <IGESGeom_TrimmedSurface.hxx>
52 #include <IGESSolid_ConicalSurface.hxx>
53 #include <IGESSolid_CylindricalSurface.hxx>
54 #include <IGESSolid_EdgeList.hxx>
55 #include <IGESSolid_Face.hxx>
56 #include <IGESSolid_Loop.hxx>
57 #include <IGESSolid_ManifoldSolid.hxx>
58 #include <IGESSolid_PlaneSurface.hxx>
59 #include <IGESSolid_Shell.hxx>
60 #include <IGESSolid_SphericalSurface.hxx>
61 #include <IGESSolid_ToroidalSurface.hxx>
62 #include <IGESSolid_VertexList.hxx>
63 #include <IGESToBRep.hxx>
64 #include <IGESToBRep_AlgoContainer.hxx>
65 #include <Interface_Macros.hxx>
66 #include <Precision.hxx>
67 #include <Standard_ErrorHandler.hxx>
68 #include <Standard_Failure.hxx>
69 #include <Standard_Stream.hxx>
70 #include <TCollection_HAsciiString.hxx>
71 #include <TopLoc_Location.hxx>
72 #include <TopoDS.hxx>
73 #include <TopoDS_Edge.hxx>
74 #include <TopoDS_Face.hxx>
75 #include <TopoDS_Iterator.hxx>
76 #include <TopoDS_Shape.hxx>
77 #include <TopoDS_Wire.hxx>
78 #include <XSAlgo.hxx>
79
80 static Handle(IGESToBRep_AlgoContainer) theContainer; 
81  
82 //=======================================================================                                                
83 //function : Init                                                                                                        
84 //purpose  :                                                                                                             
85 //=======================================================================                                                
86                                                                                                                          
87  void IGESToBRep::Init()                                                                                                   
88 {                                                                                                                        
89   static Standard_Boolean init = Standard_False;                                                                         
90   if (init) return;                                                                                                      
91   init = Standard_True;                                                                                                  
92   XSAlgo::Init();                                                                                                        
93   theContainer = new IGESToBRep_AlgoContainer;                                                                             
94 }     
95    
96 //=======================================================================                                                
97 //function : SetAlgoContainer                                                                                            
98 //purpose  :                                                                                                             
99 //=======================================================================                                                
100                                                                                                                          
101  void IGESToBRep::SetAlgoContainer(const Handle(IGESToBRep_AlgoContainer)& aContainer)                                       
102 {
103   theContainer = aContainer;                                                                                             
104 }                                                                                                                        
105                                                                                                                          
106 //=======================================================================                                                
107 //function : AlgoContainer                                                                                               
108 //purpose  :                                                                                                             
109 //=======================================================================                                                
110                                                                                                                          
111  Handle(IGESToBRep_AlgoContainer) IGESToBRep::AlgoContainer()                                                                
112 {                                                                                                                        
113   return theContainer;                                                                                                   
114 }    
115
116 //=======================================================================
117 //function : IsCurveAndSurface
118 //purpose  : Return True if the IgesEntity can be transfered
119 //           by TransferCurveAndSurface
120 //=======================================================================
121 Standard_Boolean IGESToBRep::IsCurveAndSurface(const Handle(IGESData_IGESEntity)& start)
122 {
123   //S4054
124   if (start.IsNull())              return Standard_False;
125   if (IsTopoCurve(start))          return Standard_True;
126   if (IsTopoSurface(start))        return Standard_True;
127   if (IsBRepEntity(start))         return Standard_True;
128   return Standard_False;
129 }
130
131
132 //=======================================================================
133 //function : IsBasicCurve
134 //purpose  : Return True if the IgesEntity can be transfered
135 //           by TransferBasicCurve
136 //=======================================================================
137 Standard_Boolean IGESToBRep::IsBasicCurve(const Handle(IGESData_IGESEntity)& start)
138 {
139   //S4054
140   if (start.IsNull())                                      return Standard_False;
141   if (start->IsKind(STANDARD_TYPE(IGESGeom_BSplineCurve))) return Standard_True;
142   if (start->IsKind(STANDARD_TYPE(IGESGeom_Line)))         return Standard_True;
143   if (start->IsKind(STANDARD_TYPE(IGESGeom_CircularArc)))  return Standard_True;
144   if (start->IsKind(STANDARD_TYPE(IGESGeom_ConicArc)))     return Standard_True;
145   if (start->IsKind(STANDARD_TYPE(IGESGeom_CopiousData)))  return Standard_True;
146   if (start->IsKind(STANDARD_TYPE(IGESGeom_SplineCurve)))  return Standard_True;
147   return Standard_False;
148 }
149
150
151 //=======================================================================
152 //function : IsBasicSurface
153 //purpose  : Return True if the IgesEntity can be transfered
154 //           by TransferBasicSurface
155 //=======================================================================
156 Standard_Boolean IGESToBRep::IsBasicSurface(const Handle(IGESData_IGESEntity)& start)
157 {
158   //S4054
159   if (start.IsNull())                                        return Standard_False;
160   if (start->IsKind(STANDARD_TYPE(IGESGeom_BSplineSurface))) return Standard_True;
161   if (start->IsKind(STANDARD_TYPE(IGESGeom_SplineSurface)))  return Standard_True;
162   //S4181 pdn 15.04.99 added to basic surfaces
163   if (start->IsKind(STANDARD_TYPE(IGESSolid_PlaneSurface)))       return Standard_True;
164   if (start->IsKind(STANDARD_TYPE(IGESSolid_CylindricalSurface))) return Standard_True;
165   if (start->IsKind(STANDARD_TYPE(IGESSolid_ConicalSurface)))     return Standard_True;
166   if (start->IsKind(STANDARD_TYPE(IGESSolid_SphericalSurface)))   return Standard_True;
167   if (start->IsKind(STANDARD_TYPE(IGESSolid_ToroidalSurface)))   return Standard_True;
168   
169   return Standard_False;
170 }
171
172
173 //=======================================================================
174 //function : IsTopoCurve
175 //purpose  : Return True if the IgesEntity can be transfered
176 //           by TransferTopoCurve
177 //=======================================================================
178 Standard_Boolean IGESToBRep::IsTopoCurve(const Handle(IGESData_IGESEntity)& start)
179 {
180   //S4054
181   if (start.IsNull())                                        return Standard_False;
182   if (IsBasicCurve(start))                                   return Standard_True;
183   if (start->IsKind(STANDARD_TYPE(IGESGeom_CompositeCurve))) return Standard_True;
184   if (start->IsKind(STANDARD_TYPE(IGESGeom_CurveOnSurface))) return Standard_True;
185   if (start->IsKind(STANDARD_TYPE(IGESGeom_Boundary)))       return Standard_True;
186   if (start->IsKind(STANDARD_TYPE(IGESGeom_Point)))          return Standard_True;
187   if (start->IsKind(STANDARD_TYPE(IGESGeom_OffsetCurve)))    return Standard_True;
188   return Standard_False;
189 }
190
191
192 //=======================================================================
193 //function : IsTopoSurface
194 //purpose  : Return True if the IgesEntity can be transfered
195 //           by TransferTopoSurface
196 //=======================================================================
197 Standard_Boolean IGESToBRep::IsTopoSurface(const Handle(IGESData_IGESEntity)& start)
198 {
199   //S4054
200   if (start.IsNull())                                             return Standard_False;
201   if (IsBasicSurface(start))                                      return Standard_True;
202   if (start->IsKind(STANDARD_TYPE(IGESGeom_TrimmedSurface)))      return Standard_True;
203   if (start->IsKind(STANDARD_TYPE(IGESGeom_SurfaceOfRevolution))) return Standard_True;
204   if (start->IsKind(STANDARD_TYPE(IGESGeom_TabulatedCylinder)))   return Standard_True;
205   if (start->IsKind(STANDARD_TYPE(IGESGeom_RuledSurface)))        return Standard_True;
206   if (start->IsKind(STANDARD_TYPE(IGESGeom_Plane)))               return Standard_True;
207   if (start->IsKind(STANDARD_TYPE(IGESGeom_BoundedSurface)))      return Standard_True;
208   if (start->IsKind(STANDARD_TYPE(IGESGeom_OffsetSurface)))       return Standard_True;
209   //S4181 pdn 15.04.99 removing to basic surface
210   //if (start->IsKind(STANDARD_TYPE(IGESSolid_PlaneSurface)))       return Standard_True;
211 //  SingleParent, cas particulier (Face Trouee : ne contient que des PLANE)
212   if (start->IsKind(STANDARD_TYPE(IGESBasic_SingleParent))) {
213     DeclareAndCast(IGESBasic_SingleParent,sp,start);
214     if (!sp->SingleParent()->IsKind(STANDARD_TYPE(IGESGeom_Plane))) return Standard_False;
215     Standard_Integer nb = sp->NbChildren();
216     for (Standard_Integer i = 1; i <= nb; i ++) {
217       if (!sp->Child(i)->IsKind(STANDARD_TYPE(IGESGeom_Plane))) return Standard_False;
218     }
219     return Standard_True;
220   }
221   return Standard_False;
222 }
223
224
225 //=======================================================================
226 //function : IsBRepEntity
227 //purpose  : Return True if the IgesEntity can be transfered
228 //           by TransferBRepEntity
229 //=======================================================================
230 Standard_Boolean IGESToBRep::IsBRepEntity(const Handle(IGESData_IGESEntity)& start)
231 {
232   //S4054
233   if (start.IsNull())                                         return Standard_False;
234   if (start->IsKind(STANDARD_TYPE(IGESSolid_Face)))           return Standard_True;
235   if (start->IsKind(STANDARD_TYPE(IGESSolid_Shell)))          return Standard_True;
236   if (start->IsKind(STANDARD_TYPE(IGESSolid_ManifoldSolid)))  return Standard_True;
237   if (start->IsKind(STANDARD_TYPE(IGESSolid_VertexList)))     return Standard_True;
238   if (start->IsKind(STANDARD_TYPE(IGESSolid_EdgeList)))       return Standard_True;
239   if (start->IsKind(STANDARD_TYPE(IGESSolid_Loop)))           return Standard_True;
240   return Standard_False;
241 }
242
243 //=======================================================================
244 //function : IGESCurveToSequenceOfIGESCurve
245 //purpose  : Creates a sequence of IGES curves from IGES curve:
246 //           - if curve is CompositeCurve its components are recursively added,
247 //           - if curve is ordinary IGES curve it is simply added
248 //           - otherwise (Null or not curve) it is ignored
249 //remark   : if sequence is Null it is created, otherwise it is appended
250 //returns  : number of curves in sequence
251 //example  : (A B (C (D ( E F) G) H)) -> (A B C D E F G H)
252 //=======================================================================
253
254  Standard_Integer IGESToBRep::IGESCurveToSequenceOfIGESCurve(const Handle(IGESData_IGESEntity)& curve,
255                                                              Handle(TColStd_HSequenceOfTransient)& sequence) 
256 {
257   if (sequence.IsNull()) sequence = new TColStd_HSequenceOfTransient;
258   if (!curve.IsNull()) {
259     if (curve->IsKind (STANDARD_TYPE (IGESGeom_CompositeCurve))) {
260       Handle(IGESGeom_CompositeCurve) comp = Handle(IGESGeom_CompositeCurve)::DownCast (curve);
261       for (Standard_Integer i = 1; i <= comp->NbCurves(); i++) {
262         Handle(TColStd_HSequenceOfTransient) tmpsequence;
263         IGESCurveToSequenceOfIGESCurve (comp->Curve (i), tmpsequence);
264         sequence->Append (tmpsequence);
265       }
266     }
267     else if (IGESToBRep::IsTopoCurve (curve) &&
268              ! curve->IsKind (STANDARD_TYPE (IGESGeom_Point)))
269       sequence->Append (curve);
270   }
271   return sequence->Length();
272 }
273
274 //=======================================================================
275 //function : TransferPCurve
276 //purpose  : Copies pcurve on face <face> from <fromedge> to <toedge>
277 //           If <toedge> already has pcurve on that <face>, <toedge> becomes
278 //           a seam-edge; if both pcurves are not SameRange, the SameRange is
279 //           called. Returns False if pcurves are not made SameRange
280 //           Making <toedge> SameParameter should be done outside the method (???)
281 //=======================================================================
282
283  Standard_Boolean IGESToBRep::TransferPCurve (const TopoDS_Edge& fromedge, const TopoDS_Edge& toedge, const TopoDS_Face& face)
284 {
285   Standard_Boolean result = Standard_True;
286   Standard_Real olda, oldb, a, b;
287   Handle(Geom2d_Curve) oldpcurve = BRep_Tool::CurveOnSurface (toedge,   face, olda, oldb),
288                           pcurve = BRep_Tool::CurveOnSurface (fromedge, face, a,    b   );
289   BRep_Builder B;
290   if (!oldpcurve.IsNull()) {
291     if (olda != a || oldb != b) {
292       try {
293         OCC_CATCH_SIGNALS
294         Handle(Geom2d_Curve) newpcurve;
295         GeomLib::SameRange (Precision::PConfusion(), oldpcurve, olda, oldb, a, b, newpcurve);
296         if (!newpcurve.IsNull()) {
297           olda = a; oldb = b; oldpcurve = newpcurve;
298         }
299         else {
300 #ifdef OCCT_DEBUG
301           cout << "Warning: IGESToBRep::TransferPCurve: pcurves are not SameRange" << endl;
302 #endif
303           result = Standard_False;
304         }
305       }
306       catch(Standard_Failure const& anException) {
307 #ifdef OCCT_DEBUG
308         cout << "\n**IGESToBRep::TransferPCurve: Exception in SameRange : "; 
309         anException.Print(cout);
310 #endif
311         (void)anException;
312         result = Standard_False;
313       }
314     }
315     if (toedge.Orientation() == TopAbs_FORWARD)
316       B.UpdateEdge (toedge,
317                     Handle(Geom2d_Curve)::DownCast (pcurve->Copy()), 
318                     Handle(Geom2d_Curve)::DownCast (oldpcurve->Copy()), face, 0);
319     else
320       B.UpdateEdge (toedge,
321                     Handle(Geom2d_Curve)::DownCast (oldpcurve->Copy()), 
322                     Handle(Geom2d_Curve)::DownCast (pcurve->Copy()), face, 0);
323   }
324   else {
325     olda = a; oldb = b;
326     B.UpdateEdge (toedge, Handle(Geom2d_Curve)::DownCast (pcurve->Copy()), face, 0);
327   }
328   B.Range (toedge, face, a, b);
329   Standard_Real first, last;
330   if (!BRep_Tool::Curve (toedge, first, last).IsNull() && (first != a || last != b))
331     B.SameRange (toedge, Standard_False);
332   else
333     B.SameRange (toedge, Standard_True);
334   return result;
335 }