0031668: Visualization - WebGL sample doesn't work on Emscripten 1.39
[occt.git] / src / TDataXtd / TDataXtd_Geometry.cxx
1 // Created on: 2009-04-06
2 // Copyright (c) 2009-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15
16 #include <BRep_Tool.hxx>
17 #include <Geom_CartesianPoint.hxx>
18 #include <Geom_Circle.hxx>
19 #include <Geom_Curve.hxx>
20 #include <Geom_CylindricalSurface.hxx>
21 #include <Geom_Ellipse.hxx>
22 #include <Geom_Line.hxx>
23 #include <Geom_Plane.hxx>
24 #include <Geom_RectangularTrimmedSurface.hxx>
25 #include <Geom_TrimmedCurve.hxx>
26 #include <gp_Ax1.hxx>
27 #include <gp_Circ.hxx>
28 #include <gp_Cylinder.hxx>
29 #include <gp_Elips.hxx>
30 #include <gp_Lin.hxx>
31 #include <gp_Pln.hxx>
32 #include <gp_Pnt.hxx>
33 #include <Standard_Failure.hxx>
34 #include <Standard_GUID.hxx>
35 #include <Standard_Type.hxx>
36 #include <TDataStd.hxx>
37 #include <TDataXtd.hxx>
38 #include <TDataXtd_Geometry.hxx>
39 #include <TDF_Attribute.hxx>
40 #include <TDF_Label.hxx>
41 #include <TDF_RelocationTable.hxx>
42 #include <TNaming_NamedShape.hxx>
43 #include <TNaming_Tool.hxx>
44 #include <TopAbs.hxx>
45 #include <TopoDS.hxx>
46 #include <TopoDS_Edge.hxx>
47 #include <TopoDS_Face.hxx>
48 #include <TopoDS_Vertex.hxx>
49
50 IMPLEMENT_STANDARD_RTTIEXT(TDataXtd_Geometry,TDF_Attribute)
51
52 //=======================================================================
53 //function : GetID
54 //purpose  : 
55 //=======================================================================
56 const Standard_GUID& TDataXtd_Geometry::GetID () 
57 {
58   static Standard_GUID TDataXtd_GeometryID ("2a96b604-ec8b-11d0-bee7-080009dc3333");
59   return TDataXtd_GeometryID;
60 }
61
62
63
64 //=======================================================================
65 //function : Set
66 //purpose  : 
67 //=======================================================================
68
69 Handle(TDataXtd_Geometry) TDataXtd_Geometry::Set (const TDF_Label& L)
70
71   Handle(TDataXtd_Geometry) A;  
72   if (!L.FindAttribute(TDataXtd_Geometry::GetID(),A)) {
73     A = new TDataXtd_Geometry (); 
74 //    A->SetType(TDataXtd_ANY_GEOM);
75     L.AddAttribute(A);
76   }  
77   return A;
78 }
79
80
81 //=======================================================================
82 //function : Point
83 //purpose  : 
84 //=======================================================================
85
86 Standard_Boolean TDataXtd_Geometry::Point(const TDF_Label& L,gp_Pnt& G) 
87 {
88   Handle(TNaming_NamedShape) NS;
89   if (L.FindAttribute(TNaming_NamedShape::GetID(),NS)) { 
90     return Point(NS,G);
91   }
92   return Standard_False;
93 }
94
95 //=======================================================================
96 //function : Point
97 //purpose  : 
98 //=======================================================================
99
100 Standard_Boolean TDataXtd_Geometry::Point(const Handle(TNaming_NamedShape)& NS, gp_Pnt& G) 
101 {
102   const TopoDS_Shape& shape = TNaming_Tool::GetShape(NS);
103   if (shape.IsNull()) return Standard_False;
104   if (shape.ShapeType() == TopAbs_VERTEX) {
105     const TopoDS_Vertex& vertex = TopoDS::Vertex(shape);
106     G = BRep_Tool::Pnt (TopoDS::Vertex (vertex));
107     return Standard_True;
108   }
109   return Standard_False;
110 }
111
112 //=======================================================================
113 //function : Axis
114 //purpose  : 
115 //=======================================================================
116
117 Standard_Boolean TDataXtd_Geometry::Axis(const TDF_Label& L,gp_Ax1& G) 
118 {    
119   Handle(TNaming_NamedShape) NS;
120   if (L.FindAttribute(TNaming_NamedShape::GetID(),NS)) { 
121     return Axis(NS,G);
122   }
123   return Standard_False;
124 }
125
126 //=======================================================================
127 //function : Axis
128 //purpose  : 
129 //=======================================================================
130
131 Standard_Boolean TDataXtd_Geometry::Axis(const Handle(TNaming_NamedShape)& NS, gp_Ax1& G) 
132 {
133   gp_Lin lin;
134   if (Line(NS, lin)) {
135     G = lin.Position();
136     return Standard_True;
137   }
138   return Standard_False;
139 }
140
141
142 //=======================================================================
143 //function : Line
144 //purpose  : 
145 //=======================================================================
146
147 Standard_Boolean TDataXtd_Geometry::Line(const TDF_Label& L,gp_Lin& G) 
148 {
149   Handle(TNaming_NamedShape) NS;
150   if (L.FindAttribute(TNaming_NamedShape::GetID(),NS)) { 
151     return Line(NS,G);
152   }
153   return Standard_False;
154 }
155
156 //=======================================================================
157 //function : Line
158 //purpose  : 
159 //=======================================================================
160
161 Standard_Boolean TDataXtd_Geometry::Line(const Handle(TNaming_NamedShape)& NS,gp_Lin& G) 
162 {
163   const TopoDS_Shape& shape = TNaming_Tool::GetShape(NS);
164   if (shape.IsNull()) return Standard_False;
165   if (shape.ShapeType() == TopAbs_EDGE) {
166     const TopoDS_Edge& edge = TopoDS::Edge(shape);
167     Standard_Real first,last;
168     // TopLoc_Location loc;
169     Handle(Geom_Curve) curve = BRep_Tool::Curve (edge,first,last);
170     if (!curve.IsNull()) {
171       if (curve->IsInstance (STANDARD_TYPE (Geom_TrimmedCurve)))
172         curve =  (Handle(Geom_TrimmedCurve)::DownCast (curve))->BasisCurve ();
173       Handle(Geom_Line) C  = Handle(Geom_Line)::DownCast(curve);
174       if (!C.IsNull()) {
175         G = C->Lin();
176         return Standard_True;
177       }
178     }
179   }
180   return Standard_False;
181 }
182
183 //=======================================================================
184 //function : Circle
185 //purpose  : 
186 //=======================================================================
187
188 Standard_Boolean TDataXtd_Geometry::Circle(const TDF_Label& L,gp_Circ& G) 
189 {  
190   Handle(TNaming_NamedShape) NS;
191   if (L.FindAttribute(TNaming_NamedShape::GetID(),NS)) { 
192     return Circle(NS,G);
193   }
194   return Standard_False; 
195 }
196
197 //=======================================================================
198 //function : Circle
199 //purpose  : 
200 //=======================================================================
201
202 Standard_Boolean TDataXtd_Geometry::Circle(const Handle(TNaming_NamedShape)& NS,gp_Circ& G) 
203 {  
204   const TopoDS_Shape& shape = TNaming_Tool::GetShape(NS);
205   if (shape.IsNull()) return Standard_False;
206   if (shape.ShapeType() == TopAbs_EDGE) {
207     const TopoDS_Edge& edge = TopoDS::Edge(shape);
208     Standard_Real first,last;
209     // TopLoc_Location loc;
210     Handle(Geom_Curve) curve = BRep_Tool::Curve (edge,first,last);
211     if (!curve.IsNull()) {
212       if (curve->IsInstance (STANDARD_TYPE (Geom_TrimmedCurve)))
213         curve =  (Handle(Geom_TrimmedCurve)::DownCast (curve))->BasisCurve ();
214       Handle(Geom_Circle) C = Handle(Geom_Circle)::DownCast(curve);
215       if (!C.IsNull()) {
216         G = C->Circ();
217         return Standard_True;
218       }
219     }
220   }
221   return Standard_False;
222 }
223
224
225 //=======================================================================
226 //function : Ellipse
227 //purpose  : 
228 //=======================================================================
229
230 Standard_Boolean TDataXtd_Geometry::Ellipse(const TDF_Label& L,gp_Elips& G) 
231 {
232   Handle(TNaming_NamedShape) NS;
233   if (L.FindAttribute(TNaming_NamedShape::GetID(),NS)) { 
234     return Ellipse(NS,G);
235   }
236   return Standard_False;
237 }
238
239
240 //=======================================================================
241 //function : Ellipse
242 //purpose  : 
243 //=======================================================================
244
245 Standard_Boolean TDataXtd_Geometry::Ellipse(const Handle(TNaming_NamedShape)& NS, gp_Elips& G) 
246 {  
247   const TopoDS_Shape& shape = TNaming_Tool::GetShape(NS);
248   if (shape.IsNull()) return Standard_False;
249   if (shape.ShapeType() == TopAbs_EDGE) {
250     const TopoDS_Edge& edge = TopoDS::Edge(shape);
251     Standard_Real first,last;
252     Handle(Geom_Curve) curve = BRep_Tool::Curve (edge,first,last);
253     if (!curve.IsNull()) {
254       if (curve->IsInstance (STANDARD_TYPE (Geom_TrimmedCurve)))
255         curve =  (Handle(Geom_TrimmedCurve)::DownCast (curve))->BasisCurve ();
256       Handle(Geom_Ellipse) C = Handle(Geom_Ellipse)::DownCast(curve);
257       if (!C.IsNull()) {
258         G = C->Elips();
259         return Standard_True;
260       }
261     }
262   }
263   return Standard_False;
264 }
265
266
267 //=======================================================================
268 //function : Plane
269 //purpose  : 
270 //=======================================================================
271
272 Standard_Boolean TDataXtd_Geometry::Plane(const TDF_Label& L, gp_Pln& G) 
273 {  
274   Handle(TNaming_NamedShape) NS;
275   if (L.FindAttribute(TNaming_NamedShape::GetID(),NS)) { 
276     return Plane(NS,G);
277   }
278   return Standard_False;
279 }
280
281
282 //=======================================================================
283 //function : Plane
284 //purpose  : 
285 //=======================================================================
286
287 Standard_Boolean TDataXtd_Geometry::Plane(const Handle(TNaming_NamedShape)& NS, gp_Pln& G) 
288 {
289   const TopoDS_Shape& shape = TNaming_Tool::GetShape(NS);
290   if (shape.IsNull()) return Standard_False;
291   if (shape.ShapeType() == TopAbs_FACE) {
292     const TopoDS_Face& face = TopoDS::Face(shape);
293     Handle(Geom_Surface) surface = BRep_Tool::Surface (face);
294     if (!surface.IsNull())  {
295        if (surface->IsInstance(STANDARD_TYPE(Geom_RectangularTrimmedSurface))) 
296          surface = Handle(Geom_RectangularTrimmedSurface)::DownCast (surface)->BasisSurface();
297        Handle(Geom_Plane) S = Handle(Geom_Plane)::DownCast(surface);  
298        if (!S.IsNull()) {
299          G = S->Pln();
300          return Standard_True;
301        }
302      } 
303   }
304   return Standard_False;
305 }
306
307
308 //=======================================================================
309 //function : Cylinder
310 //purpose  : 
311 //=======================================================================
312
313 Standard_Boolean TDataXtd_Geometry::Cylinder(const TDF_Label& L, gp_Cylinder& G) 
314
315   Handle(TNaming_NamedShape) NS;
316   if (L.FindAttribute(TNaming_NamedShape::GetID(),NS)) { 
317     return Cylinder (NS,G);
318   }
319   return Standard_False;
320 }
321
322
323 //=======================================================================
324 //function : Cylinder
325 //purpose  : 
326 //=======================================================================
327
328 Standard_Boolean TDataXtd_Geometry::Cylinder(const Handle(TNaming_NamedShape)& NS, gp_Cylinder& G) 
329 {  
330   const TopoDS_Shape& shape = TNaming_Tool::GetShape(NS);
331   if (shape.IsNull()) return Standard_False;
332   if (shape.ShapeType() == TopAbs_FACE) {
333     const TopoDS_Face& face = TopoDS::Face(shape);
334     Handle(Geom_Surface) surface = BRep_Tool::Surface (face);
335     if (!surface.IsNull())  {
336       if (surface->IsInstance(STANDARD_TYPE(Geom_RectangularTrimmedSurface))) 
337         surface = Handle(Geom_RectangularTrimmedSurface)::DownCast (surface)->BasisSurface();
338       Handle(Geom_CylindricalSurface) S = Handle(Geom_CylindricalSurface)::DownCast(surface);
339       if (!S.IsNull()) {
340         G = S->Cylinder();
341         return Standard_True;
342       }
343       
344     } 
345   }
346   return Standard_False;
347 }
348
349
350 //=======================================================================
351 //function : Type
352 //purpose  : 
353 //=======================================================================
354
355 TDataXtd_GeometryEnum  TDataXtd_Geometry::Type (const TDF_Label& L)
356 {  
357   Handle(TNaming_NamedShape) NS;
358   if (L.FindAttribute(TNaming_NamedShape::GetID(),NS)) { 
359     return Type (NS);
360   }
361   return TDataXtd_ANY_GEOM;
362 }
363
364 //=======================================================================
365 //function : Type
366 //purpose  : 
367 //=======================================================================
368
369 TDataXtd_GeometryEnum  TDataXtd_Geometry::Type (const Handle(TNaming_NamedShape)& NS)
370 {
371   TDataXtd_GeometryEnum type (TDataXtd_ANY_GEOM);
372   const TopoDS_Shape& shape = TNaming_Tool::GetShape(NS);
373   switch (shape.ShapeType()) {
374   case TopAbs_VERTEX : 
375     { 
376       type = TDataXtd_POINT;
377       break;
378     }
379   case TopAbs_EDGE : 
380     {    
381       const TopoDS_Edge& edge = TopoDS::Edge(shape);    
382       Standard_Real first,last;
383       // TopLoc_Location loc;
384       Handle(Geom_Curve) curve = BRep_Tool::Curve (edge,first,last);
385       if (!curve.IsNull()) {
386         if (curve->IsInstance (STANDARD_TYPE (Geom_TrimmedCurve))) {
387           curve =  (Handle(Geom_TrimmedCurve)::DownCast (curve))->BasisCurve ();
388         }
389         if (curve->IsInstance(STANDARD_TYPE(Geom_Line))) {
390           type = TDataXtd_LINE;
391         }
392         else if (curve->IsInstance(STANDARD_TYPE(Geom_Circle))) {
393           type = TDataXtd_CIRCLE;
394         }
395         else if (curve->IsInstance(STANDARD_TYPE(Geom_Ellipse))) {
396           type = TDataXtd_ELLIPSE;
397         }
398       }
399 #ifdef OCCT_DEBUG
400       else {
401         throw Standard_Failure("curve Null dans TDataXtd_Geometry");
402       }
403 #endif
404       break;
405     }
406   case TopAbs_FACE : 
407     {
408       const TopoDS_Face& face = TopoDS::Face(shape);
409       Handle(Geom_Surface) surface = BRep_Tool::Surface (face);
410       if (!surface.IsNull()) {
411         if (surface->IsInstance(STANDARD_TYPE(Geom_RectangularTrimmedSurface))) { 
412           surface = Handle(Geom_RectangularTrimmedSurface)::DownCast (surface)->BasisSurface();
413         }
414         if (surface->IsInstance(STANDARD_TYPE(Geom_CylindricalSurface))) {
415           type = TDataXtd_CYLINDER;
416         }
417         else if (surface->IsInstance(STANDARD_TYPE(Geom_Plane))) {
418           type = TDataXtd_PLANE;
419         }
420       } 
421 #ifdef OCCT_DEBUG
422       else {
423         throw Standard_Failure("surface Null dans TDataXtd_Geometry");
424       }
425 #endif
426       break;
427     }
428     default :
429       break;
430   }
431   return type;
432 }
433
434 //=======================================================================
435 //function : TDataXtd_Geometry
436 //purpose  : 
437 //=======================================================================
438
439 TDataXtd_Geometry::TDataXtd_Geometry ()
440      : myType (TDataXtd_ANY_GEOM)
441  { }
442
443
444 //=======================================================================
445 //function : GetType
446 //purpose  : 
447 //=======================================================================
448
449 TDataXtd_GeometryEnum  TDataXtd_Geometry::GetType () const
450 {
451   return myType;
452 }
453
454
455 //=======================================================================
456 //function : SetType
457 //purpose  : 
458 //=======================================================================
459
460 void TDataXtd_Geometry::SetType (const TDataXtd_GeometryEnum G)
461 {
462   // OCC2932 correction
463   if(myType == G) return;
464
465   Backup();
466   myType = G;
467 }
468
469
470 //=======================================================================
471 //function : ID
472 //purpose  : 
473 //=======================================================================
474
475 const Standard_GUID& TDataXtd_Geometry::ID() const {  return GetID(); }
476
477
478 //=======================================================================
479 //function : NewEmpty
480 //purpose  : 
481 //=======================================================================
482
483 Handle(TDF_Attribute) TDataXtd_Geometry::NewEmpty () const
484 {  
485   return new TDataXtd_Geometry(); 
486 }
487
488 //=======================================================================
489 //function : Restore
490 //purpose  : 
491 //=======================================================================
492
493 void TDataXtd_Geometry::Restore (const Handle(TDF_Attribute)& With) 
494 {
495   myType =  Handle(TDataXtd_Geometry)::DownCast(With)->GetType();
496 }
497
498 //=======================================================================
499 //function : Paste
500 //purpose  : 
501 //=======================================================================
502
503 void TDataXtd_Geometry::Paste (const Handle(TDF_Attribute)& Into, const Handle(TDF_RelocationTable)&) const { 
504   Handle(TDataXtd_Geometry)::DownCast(Into)->SetType(myType);
505 }
506
507
508 //=======================================================================
509 //function : Dump
510 //purpose  : 
511 //=======================================================================
512
513 Standard_OStream& TDataXtd_Geometry::Dump (Standard_OStream& anOS) const
514 {  
515   anOS << "Geometry ";  
516   TDataXtd::Print(GetType(),anOS);
517   return anOS;
518 }