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