summaryrefslogtreecommitdiff
path: root/src/IGESToBRep/IGESToBRep.cxx
blob: e6d41c2c6b86acc44391624876d3715ee7bf81fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// File:	IGESToBRep.cxx
// Created:	Tue Mar 22 16:47:32 1994
// Author:	GUYOT and UNTEREINER
//		<fun@ecolox>
//-Copyright:	 Matra Datavision 1994
// 21.12.98 rln, gka S4054
// 06.01.99 pdn  transmission from ShapeTool_MakeWire back to BRepAPI_MakeWire
// 19.03.99 abv  //:q5: code improvement, unnecessary includes removed
//S4181 pdn 15.04.99 Recognition of elementary surfaces as basic.
#include <IGESToBRep.ixx>
#include <Standard_Stream.hxx>
#include <Standard_ErrorHandler.hxx>
#include <Standard_Failure.hxx>

#include <IGESData_IGESEntity.hxx>

#include <IGESGeom_RuledSurface.hxx>
#include <IGESGeom_SurfaceOfRevolution.hxx>
#include <IGESGeom_TabulatedCylinder.hxx>
#include <IGESGeom_OffsetSurface.hxx>
#include <IGESGeom_TrimmedSurface.hxx>

#include <IGESGeom_Boundary.hxx>
#include <IGESGeom_BoundedSurface.hxx>
#include <IGESGeom_BSplineCurve.hxx>
#include <IGESGeom_BSplineSurface.hxx>
#include <IGESGeom_CircularArc.hxx>
#include <IGESGeom_ConicArc.hxx>
#include <IGESGeom_Line.hxx>
#include <IGESGeom_CompositeCurve.hxx>
#include <IGESGeom_CopiousData.hxx>
#include <IGESGeom_CurveOnSurface.hxx>
#include <IGESGeom_OffsetCurve.hxx>
#include <IGESGeom_Plane.hxx>
#include <IGESGeom_Point.hxx>
#include <IGESGeom_SplineCurve.hxx>
#include <IGESGeom_SplineSurface.hxx>

#include <IGESSolid_VertexList.hxx>
#include <IGESSolid_EdgeList.hxx>
#include <IGESSolid_Loop.hxx>
#include <IGESSolid_Face.hxx>
#include <IGESSolid_Shell.hxx>
#include <IGESSolid_ManifoldSolid.hxx>
#include <IGESSolid_PlaneSurface.hxx>

#include <IGESBasic_SingleParent.hxx>

#include <Geom2d_Curve.hxx>
#include <Geom_Curve.hxx>
#include <Geom_Surface.hxx>

#include <Precision.hxx>

#include <BRep_Tool.hxx>

#include <TopoDS.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS_Iterator.hxx>
#include <TopoDS_Wire.hxx>

#include <TopLoc_Location.hxx>
#include <Interface_Macros.hxx>
  
/* Just used for WriteShape */

#include <stdio.h>
#include <TCollection_HAsciiString.hxx>
#include <BRepTools.hxx>

//:21 
#include <gp_Pnt.hxx>
#include <gp_Pnt2d.hxx>
#include <BRep_Builder.hxx>
#include <GeomLib.hxx>
#include <IGESSolid_CylindricalSurface.hxx>
#include <IGESSolid_ConicalSurface.hxx>
#include <IGESSolid_SphericalSurface.hxx>
#include <IGESSolid_ToroidalSurface.hxx>

#include <XSAlgo.hxx>

//                     =========================
//                     ==  Selection Members  ==
//                     =========================

static Handle(IGESToBRep_AlgoContainer) theContainer; 
 
//=======================================================================                                                
//function : Init                                                                                                        
//purpose  :                                                                                                             
//=======================================================================                                                
                                                                                                                         
 void IGESToBRep::Init()                                                                                                   
{                                                                                                                        
  static Standard_Boolean init = Standard_False;                                                                         
  if (init) return;                                                                                                      
  init = Standard_True;                                                                                                  
  XSAlgo::Init();                                                                                                        
  theContainer = new IGESToBRep_AlgoContainer;                                                                             
}     
   
//=======================================================================                                                
//function : SetAlgoContainer                                                                                            
//purpose  :                                                                                                             
//=======================================================================                                                
                                                                                                                         
 void IGESToBRep::SetAlgoContainer(const Handle(IGESToBRep_AlgoContainer)& aContainer)                                       
{
  theContainer = aContainer;                                                                                             
}                                                                                                                        
                                                                                                                         
//=======================================================================                                                
//function : AlgoContainer                                                                                               
//purpose  :                                                                                                             
//=======================================================================                                                
                                                                                                                         
 Handle(IGESToBRep_AlgoContainer) IGESToBRep::AlgoContainer()                                                                
{                                                                                                                        
  return theContainer;                                                                                                   
}    

//=======================================================================
//function : IsCurveAndSurface
//purpose  : Return True if the IgesEntity can be transfered
//           by TransferCurveAndSurface
//=======================================================================
Standard_Boolean IGESToBRep::IsCurveAndSurface(const Handle(IGESData_IGESEntity)& start)
{
  //S4054
  if (start.IsNull())              return Standard_False;
  if (IsTopoCurve(start))          return Standard_True;
  if (IsTopoSurface(start))        return Standard_True;
  if (IsBRepEntity(start))         return Standard_True;
  return Standard_False;
}


//=======================================================================
//function : IsBasicCurve
//purpose  : Return True if the IgesEntity can be transfered
//           by TransferBasicCurve
//=======================================================================
Standard_Boolean IGESToBRep::IsBasicCurve(const Handle(IGESData_IGESEntity)& start)
{
  //S4054
  if (start.IsNull())                                      return Standard_False;
  if (start->IsKind(STANDARD_TYPE(IGESGeom_BSplineCurve))) return Standard_True;
  if (start->IsKind(STANDARD_TYPE(IGESGeom_Line)))         return Standard_True;
  if (start->IsKind(STANDARD_TYPE(IGESGeom_CircularArc)))  return Standard_True;
  if (start->IsKind(STANDARD_TYPE(IGESGeom_ConicArc)))     return Standard_True;
  if (start->IsKind(STANDARD_TYPE(IGESGeom_CopiousData)))  return Standard_True;
  if (start->IsKind(STANDARD_TYPE(IGESGeom_SplineCurve)))  return Standard_True;
  return Standard_False;
}


//=======================================================================
//function : IsBasicSurface
//purpose  : Return True if the IgesEntity can be transfered
//           by TransferBasicSurface
//=======================================================================
Standard_Boolean IGESToBRep::IsBasicSurface(const Handle(IGESData_IGESEntity)& start)
{
  //S4054
  if (start.IsNull())                                        return Standard_False;
  if (start->IsKind(STANDARD_TYPE(IGESGeom_BSplineSurface))) return Standard_True;
  if (start->IsKind(STANDARD_TYPE(IGESGeom_SplineSurface)))  return Standard_True;
  //S4181 pdn 15.04.99 added to basic surfaces
  if (start->IsKind(STANDARD_TYPE(IGESSolid_PlaneSurface)))       return Standard_True;
  if (start->IsKind(STANDARD_TYPE(IGESSolid_CylindricalSurface))) return Standard_True;
  if (start->IsKind(STANDARD_TYPE(IGESSolid_ConicalSurface)))     return Standard_True;
  if (start->IsKind(STANDARD_TYPE(IGESSolid_SphericalSurface)))   return Standard_True;
  if (start->IsKind(STANDARD_TYPE(IGESSolid_ToroidalSurface)))   return Standard_True;
  
  return Standard_False;
}


//=======================================================================
//function : IsTopoCurve
//purpose  : Return True if the IgesEntity can be transfered
//           by TransferTopoCurve
//=======================================================================
Standard_Boolean IGESToBRep::IsTopoCurve(const Handle(IGESData_IGESEntity)& start)
{
  //S4054
  if (start.IsNull())                                        return Standard_False;
  if (IsBasicCurve(start))                                   return Standard_True;
  if (start->IsKind(STANDARD_TYPE(IGESGeom_CompositeCurve))) return Standard_True;
  if (start->IsKind(STANDARD_TYPE(IGESGeom_CurveOnSurface))) return Standard_True;
  if (start->IsKind(STANDARD_TYPE(IGESGeom_Boundary)))       return Standard_True;
  if (start->IsKind(STANDARD_TYPE(IGESGeom_Point)))          return Standard_True;
  if (start->IsKind(STANDARD_TYPE(IGESGeom_OffsetCurve)))    return Standard_True;
  return Standard_False;
}


//=======================================================================
//function : IsTopoSurface
//purpose  : Return True if the IgesEntity can be transfered
//           by TransferTopoSurface
//=======================================================================
Standard_Boolean IGESToBRep::IsTopoSurface(const Handle(IGESData_IGESEntity)& start)
{
  //S4054
  if (start.IsNull())                                             return Standard_False;
  if (IsBasicSurface(start))                                      return Standard_True;
  if (start->IsKind(STANDARD_TYPE(IGESGeom_TrimmedSurface)))      return Standard_True;
  if (start->IsKind(STANDARD_TYPE(IGESGeom_SurfaceOfRevolution))) return Standard_True;
  if (start->IsKind(STANDARD_TYPE(IGESGeom_TabulatedCylinder)))   return Standard_True;
  if (start->IsKind(STANDARD_TYPE(IGESGeom_RuledSurface)))        return Standard_True;
  if (start->IsKind(STANDARD_TYPE(IGESGeom_Plane)))               return Standard_True;
  if (start->IsKind(STANDARD_TYPE(IGESGeom_BoundedSurface)))      return Standard_True;
  if (start->IsKind(STANDARD_TYPE(IGESGeom_OffsetSurface)))       return Standard_True;
  //S4181 pdn 15.04.99 removing to basic surface
  //if (start->IsKind(STANDARD_TYPE(IGESSolid_PlaneSurface)))       return Standard_True;
//  SingleParent, cas particulier (Face Trouee : ne contient que des PLANE)
  if (start->IsKind(STANDARD_TYPE(IGESBasic_SingleParent))) {
    DeclareAndCast(IGESBasic_SingleParent,sp,start);
    if (!sp->SingleParent()->IsKind(STANDARD_TYPE(IGESGeom_Plane))) return Standard_False;
    Standard_Integer nb = sp->NbChildren();
    for (Standard_Integer i = 1; i <= nb; i ++) {
      if (!sp->Child(i)->IsKind(STANDARD_TYPE(IGESGeom_Plane))) return Standard_False;
    }
    return Standard_True;
  }
  return Standard_False;
}


//=======================================================================
//function : IsBRepEntity
//purpose  : Return True if the IgesEntity can be transfered
//           by TransferBRepEntity
//=======================================================================
Standard_Boolean IGESToBRep::IsBRepEntity(const Handle(IGESData_IGESEntity)& start)
{
  //S4054
  if (start.IsNull())                                         return Standard_False;
  if (start->IsKind(STANDARD_TYPE(IGESSolid_Face)))           return Standard_True;
  if (start->IsKind(STANDARD_TYPE(IGESSolid_Shell)))          return Standard_True;
  if (start->IsKind(STANDARD_TYPE(IGESSolid_ManifoldSolid)))  return Standard_True;
  if (start->IsKind(STANDARD_TYPE(IGESSolid_VertexList)))     return Standard_True;
  if (start->IsKind(STANDARD_TYPE(IGESSolid_EdgeList)))       return Standard_True;
  if (start->IsKind(STANDARD_TYPE(IGESSolid_Loop)))           return Standard_True;
  return Standard_False;
}

//=======================================================================
//function : WriteShape
//purpose  : Creates a file Shape_'number'
//=======================================================================
void IGESToBRep::WriteShape(const TopoDS_Shape& shape,
			    const Standard_Integer number)
{
  char fname[110];
  sprintf(fname, "Shape_%d",number);
  ofstream f(fname,ios::out);
  cout << "Output file name : " << fname << endl;
  f << "DBRep_DrawableShape\n";
  
  BRepTools::Write(shape, f);
  f.close();
}

//=======================================================================
//function : IGESCurveToSequenceOfIGESCurve
//purpose  : Creates a sequence of IGES curves from IGES curve:
//           - if curve is CompositeCurve its components are recursively added,
//           - if curve is ordinary IGES curve it is simply added
//           - otherwise (Null or not curve) it is ignored
//remark   : if sequence is Null it is created, otherwise it is appended
//returns  : number of curves in sequence
//example  : (A B (C (D ( E F) G) H)) -> (A B C D E F G H)
//=======================================================================

 Standard_Integer IGESToBRep::IGESCurveToSequenceOfIGESCurve(const Handle(IGESData_IGESEntity)& curve,
							     Handle(TColStd_HSequenceOfTransient)& sequence) 
{
  if (sequence.IsNull()) sequence = new TColStd_HSequenceOfTransient;
  if (!curve.IsNull()) {
    if (curve->IsKind (STANDARD_TYPE (IGESGeom_CompositeCurve))) {
      Handle(IGESGeom_CompositeCurve) comp = Handle(IGESGeom_CompositeCurve)::DownCast (curve);
      for (Standard_Integer i = 1; i <= comp->NbCurves(); i++) {
	Handle(TColStd_HSequenceOfTransient) tmpsequence;
	IGESCurveToSequenceOfIGESCurve (comp->Curve (i), tmpsequence);
	sequence->Append (tmpsequence);
      }
    }
    else if (IGESToBRep::IsTopoCurve (curve) &&
	     ! curve->IsKind (STANDARD_TYPE (IGESGeom_Point)))
      sequence->Append (curve);
  }
  return sequence->Length();
}

//=======================================================================
//function : TransferPCurve
//purpose  : Copies pcurve on face <face> from <fromedge> to <toedge>
//           If <toedge> already has pcurve on that <face>, <toedge> becomes
//           a seam-edge; if both pcurves are not SameRange, the SameRange is
//           called. Returns False if pcurves are not made SameRange
//           Making <toedge> SameParameter should be done outside the method (???)
//=======================================================================

 Standard_Boolean IGESToBRep::TransferPCurve (const TopoDS_Edge& fromedge, const TopoDS_Edge& toedge, const TopoDS_Face& face)
{
  Standard_Boolean result = Standard_True;
  Standard_Real olda, oldb, a, b;
  Handle(Geom2d_Curve) oldpcurve = BRep_Tool::CurveOnSurface (toedge,   face, olda, oldb),
                          pcurve = BRep_Tool::CurveOnSurface (fromedge, face, a,    b   );
  BRep_Builder B;
  if (!oldpcurve.IsNull()) {
    if (olda != a || oldb != b) {
      try {
        OCC_CATCH_SIGNALS
	Handle(Geom2d_Curve) newpcurve;
	GeomLib::SameRange (Precision::PConfusion(), oldpcurve, olda, oldb, a, b, newpcurve);
	if (!newpcurve.IsNull()) {
	  olda = a; oldb = b; oldpcurve = newpcurve;
	}
	else {
#ifdef DEB
	  cout << "Warning: IGESToBRep::TransferPCurve: pcurves are not SameRange" << endl;
#endif
	  result = Standard_False;
	}
      }
      catch(Standard_Failure) {
#ifdef DEB
	cout << "\n**IGESToBRep::TransferPCurve: Exception in SameRange : "; 
	Standard_Failure::Caught()->Print(cout);
#endif
	result = Standard_False;
      }
    }
    if (toedge.Orientation() == TopAbs_FORWARD)
      B.UpdateEdge (toedge,
		    Handle(Geom2d_Curve)::DownCast (pcurve->Copy()), 
		    Handle(Geom2d_Curve)::DownCast (oldpcurve->Copy()), face, 0);
    else
      B.UpdateEdge (toedge,
		    Handle(Geom2d_Curve)::DownCast (oldpcurve->Copy()), 
		    Handle(Geom2d_Curve)::DownCast (pcurve->Copy()), face, 0);
  }
  else {
    olda = a; oldb = b;
    B.UpdateEdge (toedge, Handle(Geom2d_Curve)::DownCast (pcurve->Copy()), face, 0);
  }
  B.Range (toedge, face, a, b);
  Standard_Real first, last;
  if (!BRep_Tool::Curve (toedge, first, last).IsNull() && (first != a || last != b))
    B.SameRange (toedge, Standard_False);
  else
    B.SameRange (toedge, Standard_True);
  return result;
}