| 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 | #include <TDataXtd_Geometry.ixx> |
| 16 | #include <TDataStd.hxx> |
| 17 | #include <TDataXtd.hxx> |
| 18 | #include <TNaming_NamedShape.hxx> |
| 19 | #include <TNaming_Tool.hxx> |
| 20 | |
| 21 | #include <TopoDS.hxx> |
| 22 | #include <TopoDS_Vertex.hxx> |
| 23 | #include <TopoDS_Edge.hxx> |
| 24 | #include <TopoDS_Face.hxx> |
| 25 | #include <TopAbs.hxx> |
| 26 | #include <BRep_Tool.hxx> |
| 27 | |
| 28 | #include <Geom_CartesianPoint.hxx> |
| 29 | |
| 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> |
| 36 | |
| 37 | #include <Geom_RectangularTrimmedSurface.hxx> |
| 38 | #include <Geom_Plane.hxx> |
| 39 | #include <Geom_CylindricalSurface.hxx> |
| 40 | #include <Standard_Failure.hxx> |
| 41 | |
| 42 | |
| 43 | //======================================================================= |
| 44 | //function : GetID |
| 45 | //purpose : |
| 46 | //======================================================================= |
| 47 | |
| 48 | const Standard_GUID& TDataXtd_Geometry::GetID () |
| 49 | { |
| 50 | static Standard_GUID TDataXtd_GeometryID ("2a96b604-ec8b-11d0-bee7-080009dc3333"); |
| 51 | return TDataXtd_GeometryID; |
| 52 | } |
| 53 | |
| 54 | |
| 55 | |
| 56 | //======================================================================= |
| 57 | //function : Set |
| 58 | //purpose : |
| 59 | //======================================================================= |
| 60 | |
| 61 | Handle(TDataXtd_Geometry) TDataXtd_Geometry::Set (const TDF_Label& L) |
| 62 | { |
| 63 | Handle(TDataXtd_Geometry) A; |
| 64 | if (!L.FindAttribute(TDataXtd_Geometry::GetID(),A)) { |
| 65 | A = new TDataXtd_Geometry (); |
| 66 | // A->SetType(TDataXtd_ANY_GEOM); |
| 67 | L.AddAttribute(A); |
| 68 | } |
| 69 | return A; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | //======================================================================= |
| 74 | //function : Point |
| 75 | //purpose : |
| 76 | //======================================================================= |
| 77 | |
| 78 | Standard_Boolean TDataXtd_Geometry::Point(const TDF_Label& L,gp_Pnt& G) |
| 79 | { |
| 80 | Handle(TNaming_NamedShape) NS; |
| 81 | if (L.FindAttribute(TNaming_NamedShape::GetID(),NS)) { |
| 82 | return Point(NS,G); |
| 83 | } |
| 84 | return Standard_False; |
| 85 | } |
| 86 | |
| 87 | //======================================================================= |
| 88 | //function : Point |
| 89 | //purpose : |
| 90 | //======================================================================= |
| 91 | |
| 92 | Standard_Boolean TDataXtd_Geometry::Point(const Handle(TNaming_NamedShape)& NS, gp_Pnt& G) |
| 93 | { |
| 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)); |
| 99 | return Standard_True; |
| 100 | } |
| 101 | return Standard_False; |
| 102 | } |
| 103 | |
| 104 | //======================================================================= |
| 105 | //function : Axis |
| 106 | //purpose : |
| 107 | //======================================================================= |
| 108 | |
| 109 | Standard_Boolean TDataXtd_Geometry::Axis(const TDF_Label& L,gp_Ax1& G) |
| 110 | { |
| 111 | Handle(TNaming_NamedShape) NS; |
| 112 | if (L.FindAttribute(TNaming_NamedShape::GetID(),NS)) { |
| 113 | return Axis(NS,G); |
| 114 | } |
| 115 | return Standard_False; |
| 116 | } |
| 117 | |
| 118 | //======================================================================= |
| 119 | //function : Axis |
| 120 | //purpose : |
| 121 | //======================================================================= |
| 122 | |
| 123 | Standard_Boolean TDataXtd_Geometry::Axis(const Handle(TNaming_NamedShape)& NS, gp_Ax1& G) |
| 124 | { |
| 125 | gp_Lin lin; |
| 126 | if (Line(NS, lin)) { |
| 127 | G = lin.Position(); |
| 128 | return Standard_True; |
| 129 | } |
| 130 | return Standard_False; |
| 131 | } |
| 132 | |
| 133 | |
| 134 | //======================================================================= |
| 135 | //function : Line |
| 136 | //purpose : |
| 137 | //======================================================================= |
| 138 | |
| 139 | Standard_Boolean TDataXtd_Geometry::Line(const TDF_Label& L,gp_Lin& G) |
| 140 | { |
| 141 | Handle(TNaming_NamedShape) NS; |
| 142 | if (L.FindAttribute(TNaming_NamedShape::GetID(),NS)) { |
| 143 | return Line(NS,G); |
| 144 | } |
| 145 | return Standard_False; |
| 146 | } |
| 147 | |
| 148 | //======================================================================= |
| 149 | //function : Line |
| 150 | //purpose : |
| 151 | //======================================================================= |
| 152 | |
| 153 | Standard_Boolean TDataXtd_Geometry::Line(const Handle(TNaming_NamedShape)& NS,gp_Lin& G) |
| 154 | { |
| 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); |
| 166 | if (!C.IsNull()) { |
| 167 | G = C->Lin(); |
| 168 | return Standard_True; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | return Standard_False; |
| 173 | } |
| 174 | |
| 175 | //======================================================================= |
| 176 | //function : Circle |
| 177 | //purpose : |
| 178 | //======================================================================= |
| 179 | |
| 180 | Standard_Boolean TDataXtd_Geometry::Circle(const TDF_Label& L,gp_Circ& G) |
| 181 | { |
| 182 | Handle(TNaming_NamedShape) NS; |
| 183 | if (L.FindAttribute(TNaming_NamedShape::GetID(),NS)) { |
| 184 | return Circle(NS,G); |
| 185 | } |
| 186 | return Standard_False; |
| 187 | } |
| 188 | |
| 189 | //======================================================================= |
| 190 | //function : Circle |
| 191 | //purpose : |
| 192 | //======================================================================= |
| 193 | |
| 194 | Standard_Boolean TDataXtd_Geometry::Circle(const Handle(TNaming_NamedShape)& NS,gp_Circ& G) |
| 195 | { |
| 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); |
| 207 | if (!C.IsNull()) { |
| 208 | G = C->Circ(); |
| 209 | return Standard_True; |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | return Standard_False; |
| 214 | } |
| 215 | |
| 216 | |
| 217 | //======================================================================= |
| 218 | //function : Ellipse |
| 219 | //purpose : |
| 220 | //======================================================================= |
| 221 | |
| 222 | Standard_Boolean TDataXtd_Geometry::Ellipse(const TDF_Label& L,gp_Elips& G) |
| 223 | { |
| 224 | Handle(TNaming_NamedShape) NS; |
| 225 | if (L.FindAttribute(TNaming_NamedShape::GetID(),NS)) { |
| 226 | return Ellipse(NS,G); |
| 227 | } |
| 228 | return Standard_False; |
| 229 | } |
| 230 | |
| 231 | |
| 232 | //======================================================================= |
| 233 | //function : Ellipse |
| 234 | //purpose : |
| 235 | //======================================================================= |
| 236 | |
| 237 | Standard_Boolean TDataXtd_Geometry::Ellipse(const Handle(TNaming_NamedShape)& NS, gp_Elips& G) |
| 238 | { |
| 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); |
| 249 | if (!C.IsNull()) { |
| 250 | G = C->Elips(); |
| 251 | return Standard_True; |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | return Standard_False; |
| 256 | } |
| 257 | |
| 258 | |
| 259 | //======================================================================= |
| 260 | //function : Plane |
| 261 | //purpose : |
| 262 | //======================================================================= |
| 263 | |
| 264 | Standard_Boolean TDataXtd_Geometry::Plane(const TDF_Label& L, gp_Pln& G) |
| 265 | { |
| 266 | Handle(TNaming_NamedShape) NS; |
| 267 | if (L.FindAttribute(TNaming_NamedShape::GetID(),NS)) { |
| 268 | return Plane(NS,G); |
| 269 | } |
| 270 | return Standard_False; |
| 271 | } |
| 272 | |
| 273 | |
| 274 | //======================================================================= |
| 275 | //function : Plane |
| 276 | //purpose : |
| 277 | //======================================================================= |
| 278 | |
| 279 | Standard_Boolean TDataXtd_Geometry::Plane(const Handle(TNaming_NamedShape)& NS, gp_Pln& G) |
| 280 | { |
| 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); |
| 290 | if (!S.IsNull()) { |
| 291 | G = S->Pln(); |
| 292 | return Standard_True; |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | return Standard_False; |
| 297 | } |
| 298 | |
| 299 | |
| 300 | //======================================================================= |
| 301 | //function : Cylinder |
| 302 | //purpose : |
| 303 | //======================================================================= |
| 304 | |
| 305 | Standard_Boolean TDataXtd_Geometry::Cylinder(const TDF_Label& L, gp_Cylinder& G) |
| 306 | { |
| 307 | Handle(TNaming_NamedShape) NS; |
| 308 | if (L.FindAttribute(TNaming_NamedShape::GetID(),NS)) { |
| 309 | return Cylinder (NS,G); |
| 310 | } |
| 311 | return Standard_False; |
| 312 | } |
| 313 | |
| 314 | |
| 315 | //======================================================================= |
| 316 | //function : Cylinder |
| 317 | //purpose : |
| 318 | //======================================================================= |
| 319 | |
| 320 | Standard_Boolean TDataXtd_Geometry::Cylinder(const Handle(TNaming_NamedShape)& NS, gp_Cylinder& G) |
| 321 | { |
| 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); |
| 331 | if (!S.IsNull()) { |
| 332 | G = S->Cylinder(); |
| 333 | return Standard_True; |
| 334 | } |
| 335 | |
| 336 | } |
| 337 | } |
| 338 | return Standard_False; |
| 339 | } |
| 340 | |
| 341 | |
| 342 | //======================================================================= |
| 343 | //function : Type |
| 344 | //purpose : |
| 345 | //======================================================================= |
| 346 | |
| 347 | TDataXtd_GeometryEnum TDataXtd_Geometry::Type (const TDF_Label& L) |
| 348 | { |
| 349 | Handle(TNaming_NamedShape) NS; |
| 350 | if (L.FindAttribute(TNaming_NamedShape::GetID(),NS)) { |
| 351 | return Type (NS); |
| 352 | } |
| 353 | return TDataXtd_ANY_GEOM; |
| 354 | } |
| 355 | |
| 356 | //======================================================================= |
| 357 | //function : Type |
| 358 | //purpose : |
| 359 | //======================================================================= |
| 360 | |
| 361 | TDataXtd_GeometryEnum TDataXtd_Geometry::Type (const Handle(TNaming_NamedShape)& NS) |
| 362 | { |
| 363 | TDataXtd_GeometryEnum type (TDataXtd_ANY_GEOM); |
| 364 | const TopoDS_Shape& shape = TNaming_Tool::GetShape(NS); |
| 365 | switch (shape.ShapeType()) { |
| 366 | case TopAbs_VERTEX : |
| 367 | { |
| 368 | type = TDataXtd_POINT; |
| 369 | break; |
| 370 | } |
| 371 | case TopAbs_EDGE : |
| 372 | { |
| 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 (); |
| 380 | } |
| 381 | if (curve->IsInstance(STANDARD_TYPE(Geom_Line))) { |
| 382 | type = TDataXtd_LINE; |
| 383 | } |
| 384 | else if (curve->IsInstance(STANDARD_TYPE(Geom_Circle))) { |
| 385 | type = TDataXtd_CIRCLE; |
| 386 | } |
| 387 | else if (curve->IsInstance(STANDARD_TYPE(Geom_Ellipse))) { |
| 388 | type = TDataXtd_ELLIPSE; |
| 389 | } |
| 390 | break; |
| 391 | } |
| 392 | #ifdef OCCT_DEBUG |
| 393 | else { |
| 394 | Standard_Failure::Raise("curve Null dans TDataXtd_Geometry"); |
| 395 | } |
| 396 | #endif |
| 397 | } |
| 398 | case TopAbs_FACE : |
| 399 | { |
| 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(); |
| 405 | } |
| 406 | if (surface->IsInstance(STANDARD_TYPE(Geom_CylindricalSurface))) { |
| 407 | type = TDataXtd_CYLINDER; |
| 408 | } |
| 409 | else if (surface->IsInstance(STANDARD_TYPE(Geom_Plane))) { |
| 410 | type = TDataXtd_PLANE; |
| 411 | } |
| 412 | } |
| 413 | #ifdef OCCT_DEBUG |
| 414 | else { |
| 415 | Standard_Failure::Raise("surface Null dans TDataXtd_Geometry"); |
| 416 | } |
| 417 | #endif |
| 418 | break; |
| 419 | } |
| 420 | default : |
| 421 | { |
| 422 | break; |
| 423 | } |
| 424 | } |
| 425 | return type; |
| 426 | } |
| 427 | |
| 428 | //======================================================================= |
| 429 | //function : TDataXtd_Geometry |
| 430 | //purpose : |
| 431 | //======================================================================= |
| 432 | |
| 433 | TDataXtd_Geometry::TDataXtd_Geometry () |
| 434 | : myType (TDataXtd_ANY_GEOM) |
| 435 | { } |
| 436 | |
| 437 | |
| 438 | //======================================================================= |
| 439 | //function : GetType |
| 440 | //purpose : |
| 441 | //======================================================================= |
| 442 | |
| 443 | TDataXtd_GeometryEnum TDataXtd_Geometry::GetType () const |
| 444 | { |
| 445 | return myType; |
| 446 | } |
| 447 | |
| 448 | |
| 449 | //======================================================================= |
| 450 | //function : SetType |
| 451 | //purpose : |
| 452 | //======================================================================= |
| 453 | |
| 454 | void TDataXtd_Geometry::SetType (const TDataXtd_GeometryEnum G) |
| 455 | { |
| 456 | // OCC2932 correction |
| 457 | if(myType == G) return; |
| 458 | |
| 459 | Backup(); |
| 460 | myType = G; |
| 461 | } |
| 462 | |
| 463 | |
| 464 | //======================================================================= |
| 465 | //function : ID |
| 466 | //purpose : |
| 467 | //======================================================================= |
| 468 | |
| 469 | const Standard_GUID& TDataXtd_Geometry::ID() const { return GetID(); } |
| 470 | |
| 471 | |
| 472 | //======================================================================= |
| 473 | //function : NewEmpty |
| 474 | //purpose : |
| 475 | //======================================================================= |
| 476 | |
| 477 | Handle(TDF_Attribute) TDataXtd_Geometry::NewEmpty () const |
| 478 | { |
| 479 | return new TDataXtd_Geometry(); |
| 480 | } |
| 481 | |
| 482 | //======================================================================= |
| 483 | //function : Restore |
| 484 | //purpose : |
| 485 | //======================================================================= |
| 486 | |
| 487 | void TDataXtd_Geometry::Restore (const Handle(TDF_Attribute)& With) |
| 488 | { |
| 489 | myType = Handle(TDataXtd_Geometry)::DownCast(With)->GetType(); |
| 490 | } |
| 491 | |
| 492 | //======================================================================= |
| 493 | //function : Paste |
| 494 | //purpose : |
| 495 | //======================================================================= |
| 496 | |
| 497 | void TDataXtd_Geometry::Paste (const Handle(TDF_Attribute)& Into, const Handle(TDF_RelocationTable)&) const { |
| 498 | Handle(TDataXtd_Geometry)::DownCast(Into)->SetType(myType); |
| 499 | } |
| 500 | |
| 501 | |
| 502 | //======================================================================= |
| 503 | //function : Dump |
| 504 | //purpose : |
| 505 | //======================================================================= |
| 506 | |
| 507 | Standard_OStream& TDataXtd_Geometry::Dump (Standard_OStream& anOS) const |
| 508 | { |
| 509 | anOS << "Geometry "; |
| 510 | TDataXtd::Print(GetType(),anOS); |
| 511 | return anOS; |
| 512 | } |