1 // Created on: 2009-04-06
2 // Copyright (c) 2009-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
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.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
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>
27 #include <gp_Circ.hxx>
28 #include <gp_Cylinder.hxx>
29 #include <gp_Elips.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>
46 #include <TopoDS_Edge.hxx>
47 #include <TopoDS_Face.hxx>
48 #include <TopoDS_Vertex.hxx>
50 IMPLEMENT_STANDARD_RTTIEXT(TDataXtd_Geometry,TDF_Attribute)
52 //=======================================================================
55 //=======================================================================
56 const Standard_GUID& TDataXtd_Geometry::GetID ()
58 static Standard_GUID TDataXtd_GeometryID ("2a96b604-ec8b-11d0-bee7-080009dc3333");
59 return TDataXtd_GeometryID;
64 //=======================================================================
67 //=======================================================================
69 Handle(TDataXtd_Geometry) TDataXtd_Geometry::Set (const TDF_Label& L)
71 Handle(TDataXtd_Geometry) A;
72 if (!L.FindAttribute(TDataXtd_Geometry::GetID(),A)) {
73 A = new TDataXtd_Geometry ();
74 // A->SetType(TDataXtd_ANY_GEOM);
81 //=======================================================================
84 //=======================================================================
86 Standard_Boolean TDataXtd_Geometry::Point(const TDF_Label& L,gp_Pnt& G)
88 Handle(TNaming_NamedShape) NS;
89 if (L.FindAttribute(TNaming_NamedShape::GetID(),NS)) {
92 return Standard_False;
95 //=======================================================================
98 //=======================================================================
100 Standard_Boolean TDataXtd_Geometry::Point(const Handle(TNaming_NamedShape)& NS, gp_Pnt& G)
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;
109 return Standard_False;
112 //=======================================================================
115 //=======================================================================
117 Standard_Boolean TDataXtd_Geometry::Axis(const TDF_Label& L,gp_Ax1& G)
119 Handle(TNaming_NamedShape) NS;
120 if (L.FindAttribute(TNaming_NamedShape::GetID(),NS)) {
123 return Standard_False;
126 //=======================================================================
129 //=======================================================================
131 Standard_Boolean TDataXtd_Geometry::Axis(const Handle(TNaming_NamedShape)& NS, gp_Ax1& G)
136 return Standard_True;
138 return Standard_False;
142 //=======================================================================
145 //=======================================================================
147 Standard_Boolean TDataXtd_Geometry::Line(const TDF_Label& L,gp_Lin& G)
149 Handle(TNaming_NamedShape) NS;
150 if (L.FindAttribute(TNaming_NamedShape::GetID(),NS)) {
153 return Standard_False;
156 //=======================================================================
159 //=======================================================================
161 Standard_Boolean TDataXtd_Geometry::Line(const Handle(TNaming_NamedShape)& NS,gp_Lin& G)
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);
176 return Standard_True;
180 return Standard_False;
183 //=======================================================================
186 //=======================================================================
188 Standard_Boolean TDataXtd_Geometry::Circle(const TDF_Label& L,gp_Circ& G)
190 Handle(TNaming_NamedShape) NS;
191 if (L.FindAttribute(TNaming_NamedShape::GetID(),NS)) {
194 return Standard_False;
197 //=======================================================================
200 //=======================================================================
202 Standard_Boolean TDataXtd_Geometry::Circle(const Handle(TNaming_NamedShape)& NS,gp_Circ& G)
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);
217 return Standard_True;
221 return Standard_False;
225 //=======================================================================
228 //=======================================================================
230 Standard_Boolean TDataXtd_Geometry::Ellipse(const TDF_Label& L,gp_Elips& G)
232 Handle(TNaming_NamedShape) NS;
233 if (L.FindAttribute(TNaming_NamedShape::GetID(),NS)) {
234 return Ellipse(NS,G);
236 return Standard_False;
240 //=======================================================================
243 //=======================================================================
245 Standard_Boolean TDataXtd_Geometry::Ellipse(const Handle(TNaming_NamedShape)& NS, gp_Elips& G)
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);
259 return Standard_True;
263 return Standard_False;
267 //=======================================================================
270 //=======================================================================
272 Standard_Boolean TDataXtd_Geometry::Plane(const TDF_Label& L, gp_Pln& G)
274 Handle(TNaming_NamedShape) NS;
275 if (L.FindAttribute(TNaming_NamedShape::GetID(),NS)) {
278 return Standard_False;
282 //=======================================================================
285 //=======================================================================
287 Standard_Boolean TDataXtd_Geometry::Plane(const Handle(TNaming_NamedShape)& NS, gp_Pln& G)
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);
300 return Standard_True;
304 return Standard_False;
308 //=======================================================================
309 //function : Cylinder
311 //=======================================================================
313 Standard_Boolean TDataXtd_Geometry::Cylinder(const TDF_Label& L, gp_Cylinder& G)
315 Handle(TNaming_NamedShape) NS;
316 if (L.FindAttribute(TNaming_NamedShape::GetID(),NS)) {
317 return Cylinder (NS,G);
319 return Standard_False;
323 //=======================================================================
324 //function : Cylinder
326 //=======================================================================
328 Standard_Boolean TDataXtd_Geometry::Cylinder(const Handle(TNaming_NamedShape)& NS, gp_Cylinder& G)
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);
341 return Standard_True;
346 return Standard_False;
350 //=======================================================================
353 //=======================================================================
355 TDataXtd_GeometryEnum TDataXtd_Geometry::Type (const TDF_Label& L)
357 Handle(TNaming_NamedShape) NS;
358 if (L.FindAttribute(TNaming_NamedShape::GetID(),NS)) {
361 return TDataXtd_ANY_GEOM;
364 //=======================================================================
367 //=======================================================================
369 TDataXtd_GeometryEnum TDataXtd_Geometry::Type (const Handle(TNaming_NamedShape)& NS)
371 TDataXtd_GeometryEnum type (TDataXtd_ANY_GEOM);
372 const TopoDS_Shape& shape = TNaming_Tool::GetShape(NS);
373 switch (shape.ShapeType()) {
376 type = TDataXtd_POINT;
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 ();
389 if (curve->IsInstance(STANDARD_TYPE(Geom_Line))) {
390 type = TDataXtd_LINE;
392 else if (curve->IsInstance(STANDARD_TYPE(Geom_Circle))) {
393 type = TDataXtd_CIRCLE;
395 else if (curve->IsInstance(STANDARD_TYPE(Geom_Ellipse))) {
396 type = TDataXtd_ELLIPSE;
402 throw Standard_Failure("curve Null dans TDataXtd_Geometry");
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();
414 if (surface->IsInstance(STANDARD_TYPE(Geom_CylindricalSurface))) {
415 type = TDataXtd_CYLINDER;
417 else if (surface->IsInstance(STANDARD_TYPE(Geom_Plane))) {
418 type = TDataXtd_PLANE;
423 throw Standard_Failure("surface Null dans TDataXtd_Geometry");
436 //=======================================================================
437 //function : TDataXtd_Geometry
439 //=======================================================================
441 TDataXtd_Geometry::TDataXtd_Geometry ()
442 : myType (TDataXtd_ANY_GEOM)
446 //=======================================================================
449 //=======================================================================
451 TDataXtd_GeometryEnum TDataXtd_Geometry::GetType () const
457 //=======================================================================
460 //=======================================================================
462 void TDataXtd_Geometry::SetType (const TDataXtd_GeometryEnum G)
464 // OCC2932 correction
465 if(myType == G) return;
472 //=======================================================================
475 //=======================================================================
477 const Standard_GUID& TDataXtd_Geometry::ID() const { return GetID(); }
480 //=======================================================================
481 //function : NewEmpty
483 //=======================================================================
485 Handle(TDF_Attribute) TDataXtd_Geometry::NewEmpty () const
487 return new TDataXtd_Geometry();
490 //=======================================================================
493 //=======================================================================
495 void TDataXtd_Geometry::Restore (const Handle(TDF_Attribute)& With)
497 myType = Handle(TDataXtd_Geometry)::DownCast(With)->GetType();
500 //=======================================================================
503 //=======================================================================
505 void TDataXtd_Geometry::Paste (const Handle(TDF_Attribute)& Into, const Handle(TDF_RelocationTable)&) const {
506 Handle(TDataXtd_Geometry)::DownCast(Into)->SetType(myType);
510 //=======================================================================
513 //=======================================================================
515 Standard_OStream& TDataXtd_Geometry::Dump (Standard_OStream& anOS) const
518 TDataXtd::Print(GetType(),anOS);