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.
15 #include <TDataXtd_Geometry.ixx>
16 #include <TDataStd.hxx>
17 #include <TDataXtd.hxx>
18 #include <TNaming_NamedShape.hxx>
19 #include <TNaming_Tool.hxx>
22 #include <TopoDS_Vertex.hxx>
23 #include <TopoDS_Edge.hxx>
24 #include <TopoDS_Face.hxx>
26 #include <BRep_Tool.hxx>
28 #include <Geom_CartesianPoint.hxx>
30 #include <Geom_TrimmedCurve.hxx>
31 #include <Geom_Curve.hxx>
32 #include <Geom_Line.hxx>
33 #include <Geom_Ellipse.hxx>
34 #include <Geom_Circle.hxx>
35 #include <Geom_Curve.hxx>
37 #include <Geom_RectangularTrimmedSurface.hxx>
38 #include <Geom_Plane.hxx>
39 #include <Geom_CylindricalSurface.hxx>
40 #include <Standard_Failure.hxx>
43 //=======================================================================
46 //=======================================================================
48 const Standard_GUID& TDataXtd_Geometry::GetID ()
50 static Standard_GUID TDataXtd_GeometryID ("2a96b604-ec8b-11d0-bee7-080009dc3333");
51 return TDataXtd_GeometryID;
56 //=======================================================================
59 //=======================================================================
61 Handle(TDataXtd_Geometry) TDataXtd_Geometry::Set (const TDF_Label& L)
63 Handle(TDataXtd_Geometry) A;
64 if (!L.FindAttribute(TDataXtd_Geometry::GetID(),A)) {
65 A = new TDataXtd_Geometry ();
66 // A->SetType(TDataXtd_ANY_GEOM);
73 //=======================================================================
76 //=======================================================================
78 Standard_Boolean TDataXtd_Geometry::Point(const TDF_Label& L,gp_Pnt& G)
80 Handle(TNaming_NamedShape) NS;
81 if (L.FindAttribute(TNaming_NamedShape::GetID(),NS)) {
84 return Standard_False;
87 //=======================================================================
90 //=======================================================================
92 Standard_Boolean TDataXtd_Geometry::Point(const Handle(TNaming_NamedShape)& NS, gp_Pnt& G)
94 const TopoDS_Shape& shape = TNaming_Tool::GetShape(NS);
95 if (shape.IsNull()) return Standard_False;
96 if (shape.ShapeType() == TopAbs_VERTEX) {
97 const TopoDS_Vertex& vertex = TopoDS::Vertex(shape);
98 G = BRep_Tool::Pnt (TopoDS::Vertex (vertex));
101 return Standard_False;
104 //=======================================================================
107 //=======================================================================
109 Standard_Boolean TDataXtd_Geometry::Axis(const TDF_Label& L,gp_Ax1& G)
111 Handle(TNaming_NamedShape) NS;
112 if (L.FindAttribute(TNaming_NamedShape::GetID(),NS)) {
115 return Standard_False;
118 //=======================================================================
121 //=======================================================================
123 Standard_Boolean TDataXtd_Geometry::Axis(const Handle(TNaming_NamedShape)& NS, gp_Ax1& G)
128 return Standard_True;
130 return Standard_False;
134 //=======================================================================
137 //=======================================================================
139 Standard_Boolean TDataXtd_Geometry::Line(const TDF_Label& L,gp_Lin& G)
141 Handle(TNaming_NamedShape) NS;
142 if (L.FindAttribute(TNaming_NamedShape::GetID(),NS)) {
145 return Standard_False;
148 //=======================================================================
151 //=======================================================================
153 Standard_Boolean TDataXtd_Geometry::Line(const Handle(TNaming_NamedShape)& NS,gp_Lin& G)
155 const TopoDS_Shape& shape = TNaming_Tool::GetShape(NS);
156 if (shape.IsNull()) return Standard_False;
157 if (shape.ShapeType() == TopAbs_EDGE) {
158 const TopoDS_Edge& edge = TopoDS::Edge(shape);
159 Standard_Real first,last;
160 // TopLoc_Location loc;
161 Handle(Geom_Curve) curve = BRep_Tool::Curve (edge,first,last);
162 if (!curve.IsNull()) {
163 if (curve->IsInstance (STANDARD_TYPE (Geom_TrimmedCurve)))
164 curve = (Handle(Geom_TrimmedCurve)::DownCast (curve))->BasisCurve ();
165 Handle(Geom_Line) C = Handle(Geom_Line)::DownCast(curve);
168 return Standard_True;
172 return Standard_False;
175 //=======================================================================
178 //=======================================================================
180 Standard_Boolean TDataXtd_Geometry::Circle(const TDF_Label& L,gp_Circ& G)
182 Handle(TNaming_NamedShape) NS;
183 if (L.FindAttribute(TNaming_NamedShape::GetID(),NS)) {
186 return Standard_False;
189 //=======================================================================
192 //=======================================================================
194 Standard_Boolean TDataXtd_Geometry::Circle(const Handle(TNaming_NamedShape)& NS,gp_Circ& G)
196 const TopoDS_Shape& shape = TNaming_Tool::GetShape(NS);
197 if (shape.IsNull()) return Standard_False;
198 if (shape.ShapeType() == TopAbs_EDGE) {
199 const TopoDS_Edge& edge = TopoDS::Edge(shape);
200 Standard_Real first,last;
201 // TopLoc_Location loc;
202 Handle(Geom_Curve) curve = BRep_Tool::Curve (edge,first,last);
203 if (!curve.IsNull()) {
204 if (curve->IsInstance (STANDARD_TYPE (Geom_TrimmedCurve)))
205 curve = (Handle(Geom_TrimmedCurve)::DownCast (curve))->BasisCurve ();
206 Handle(Geom_Circle) C = Handle(Geom_Circle)::DownCast(curve);
209 return Standard_True;
213 return Standard_False;
217 //=======================================================================
220 //=======================================================================
222 Standard_Boolean TDataXtd_Geometry::Ellipse(const TDF_Label& L,gp_Elips& G)
224 Handle(TNaming_NamedShape) NS;
225 if (L.FindAttribute(TNaming_NamedShape::GetID(),NS)) {
226 return Ellipse(NS,G);
228 return Standard_False;
232 //=======================================================================
235 //=======================================================================
237 Standard_Boolean TDataXtd_Geometry::Ellipse(const Handle(TNaming_NamedShape)& NS, gp_Elips& G)
239 const TopoDS_Shape& shape = TNaming_Tool::GetShape(NS);
240 if (shape.IsNull()) return Standard_False;
241 if (shape.ShapeType() == TopAbs_EDGE) {
242 const TopoDS_Edge& edge = TopoDS::Edge(shape);
243 Standard_Real first,last;
244 Handle(Geom_Curve) curve = BRep_Tool::Curve (edge,first,last);
245 if (!curve.IsNull()) {
246 if (curve->IsInstance (STANDARD_TYPE (Geom_TrimmedCurve)))
247 curve = (Handle(Geom_TrimmedCurve)::DownCast (curve))->BasisCurve ();
248 Handle(Geom_Ellipse) C = Handle(Geom_Ellipse)::DownCast(curve);
251 return Standard_True;
255 return Standard_False;
259 //=======================================================================
262 //=======================================================================
264 Standard_Boolean TDataXtd_Geometry::Plane(const TDF_Label& L, gp_Pln& G)
266 Handle(TNaming_NamedShape) NS;
267 if (L.FindAttribute(TNaming_NamedShape::GetID(),NS)) {
270 return Standard_False;
274 //=======================================================================
277 //=======================================================================
279 Standard_Boolean TDataXtd_Geometry::Plane(const Handle(TNaming_NamedShape)& NS, gp_Pln& G)
281 const TopoDS_Shape& shape = TNaming_Tool::GetShape(NS);
282 if (shape.IsNull()) return Standard_False;
283 if (shape.ShapeType() == TopAbs_FACE) {
284 const TopoDS_Face& face = TopoDS::Face(shape);
285 Handle(Geom_Surface) surface = BRep_Tool::Surface (face);
286 if (!surface.IsNull()) {
287 if (surface->IsInstance(STANDARD_TYPE(Geom_RectangularTrimmedSurface)))
288 surface = Handle(Geom_RectangularTrimmedSurface)::DownCast (surface)->BasisSurface();
289 Handle(Geom_Plane) S = Handle(Geom_Plane)::DownCast(surface);
292 return Standard_True;
296 return Standard_False;
300 //=======================================================================
301 //function : Cylinder
303 //=======================================================================
305 Standard_Boolean TDataXtd_Geometry::Cylinder(const TDF_Label& L, gp_Cylinder& G)
307 Handle(TNaming_NamedShape) NS;
308 if (L.FindAttribute(TNaming_NamedShape::GetID(),NS)) {
309 return Cylinder (NS,G);
311 return Standard_False;
315 //=======================================================================
316 //function : Cylinder
318 //=======================================================================
320 Standard_Boolean TDataXtd_Geometry::Cylinder(const Handle(TNaming_NamedShape)& NS, gp_Cylinder& G)
322 const TopoDS_Shape& shape = TNaming_Tool::GetShape(NS);
323 if (shape.IsNull()) return Standard_False;
324 if (shape.ShapeType() == TopAbs_FACE) {
325 const TopoDS_Face& face = TopoDS::Face(shape);
326 Handle(Geom_Surface) surface = BRep_Tool::Surface (face);
327 if (!surface.IsNull()) {
328 if (surface->IsInstance(STANDARD_TYPE(Geom_RectangularTrimmedSurface)))
329 surface = Handle(Geom_RectangularTrimmedSurface)::DownCast (surface)->BasisSurface();
330 Handle(Geom_CylindricalSurface) S = Handle(Geom_CylindricalSurface)::DownCast(surface);
333 return Standard_True;
338 return Standard_False;
342 //=======================================================================
345 //=======================================================================
347 TDataXtd_GeometryEnum TDataXtd_Geometry::Type (const TDF_Label& L)
349 Handle(TNaming_NamedShape) NS;
350 if (L.FindAttribute(TNaming_NamedShape::GetID(),NS)) {
353 return TDataXtd_ANY_GEOM;
356 //=======================================================================
359 //=======================================================================
361 TDataXtd_GeometryEnum TDataXtd_Geometry::Type (const Handle(TNaming_NamedShape)& NS)
363 TDataXtd_GeometryEnum type (TDataXtd_ANY_GEOM);
364 const TopoDS_Shape& shape = TNaming_Tool::GetShape(NS);
365 switch (shape.ShapeType()) {
368 type = TDataXtd_POINT;
373 const TopoDS_Edge& edge = TopoDS::Edge(shape);
374 Standard_Real first,last;
375 // TopLoc_Location loc;
376 Handle(Geom_Curve) curve = BRep_Tool::Curve (edge,first,last);
377 if (!curve.IsNull()) {
378 if (curve->IsInstance (STANDARD_TYPE (Geom_TrimmedCurve))) {
379 curve = (Handle(Geom_TrimmedCurve)::DownCast (curve))->BasisCurve ();
381 if (curve->IsInstance(STANDARD_TYPE(Geom_Line))) {
382 type = TDataXtd_LINE;
384 else if (curve->IsInstance(STANDARD_TYPE(Geom_Circle))) {
385 type = TDataXtd_CIRCLE;
387 else if (curve->IsInstance(STANDARD_TYPE(Geom_Ellipse))) {
388 type = TDataXtd_ELLIPSE;
394 Standard_Failure::Raise("curve Null dans TDataXtd_Geometry");
400 const TopoDS_Face& face = TopoDS::Face(shape);
401 Handle(Geom_Surface) surface = BRep_Tool::Surface (face);
402 if (!surface.IsNull()) {
403 if (surface->IsInstance(STANDARD_TYPE(Geom_RectangularTrimmedSurface))) {
404 surface = Handle(Geom_RectangularTrimmedSurface)::DownCast (surface)->BasisSurface();
406 if (surface->IsInstance(STANDARD_TYPE(Geom_CylindricalSurface))) {
407 type = TDataXtd_CYLINDER;
409 else if (surface->IsInstance(STANDARD_TYPE(Geom_Plane))) {
410 type = TDataXtd_PLANE;
415 Standard_Failure::Raise("surface Null dans TDataXtd_Geometry");
428 //=======================================================================
429 //function : TDataXtd_Geometry
431 //=======================================================================
433 TDataXtd_Geometry::TDataXtd_Geometry ()
434 : myType (TDataXtd_ANY_GEOM)
438 //=======================================================================
441 //=======================================================================
443 TDataXtd_GeometryEnum TDataXtd_Geometry::GetType () const
449 //=======================================================================
452 //=======================================================================
454 void TDataXtd_Geometry::SetType (const TDataXtd_GeometryEnum G)
456 // OCC2932 correction
457 if(myType == G) return;
464 //=======================================================================
467 //=======================================================================
469 const Standard_GUID& TDataXtd_Geometry::ID() const { return GetID(); }
472 //=======================================================================
473 //function : NewEmpty
475 //=======================================================================
477 Handle(TDF_Attribute) TDataXtd_Geometry::NewEmpty () const
479 return new TDataXtd_Geometry();
482 //=======================================================================
485 //=======================================================================
487 void TDataXtd_Geometry::Restore (const Handle(TDF_Attribute)& With)
489 myType = Handle(TDataXtd_Geometry)::DownCast(With)->GetType();
492 //=======================================================================
495 //=======================================================================
497 void TDataXtd_Geometry::Paste (const Handle(TDF_Attribute)& Into, const Handle(TDF_RelocationTable)&) const {
498 Handle(TDataXtd_Geometry)::DownCast(Into)->SetType(myType);
502 //=======================================================================
505 //=======================================================================
507 Standard_OStream& TDataXtd_Geometry::Dump (Standard_OStream& anOS) const
510 TDataXtd::Print(GetType(),anOS);