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