0028002: Invalid result of Boolean Fuse operation
[occt.git] / src / AIS / AIS_AngleDimension.cxx
CommitLineData
b311480e 1// Created on: 1996-12-05
2// Created by: Arnaud BOUZY/Odile Olivier
3// Copyright (c) 1996-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 8// This library is free software; you can redistribute it and/or modify it under
9// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 10// by the Free Software Foundation, with special exception defined in the file
11// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12// distribution for complete text of the license and disclaimer of any warranty.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
b311480e 16
a6eb515f 17#include <AIS_AngleDimension.hxx>
7fd59977 18
19#include <AIS.hxx>
7fd59977 20#include <BRepBuilderAPI_MakeFace.hxx>
21#include <BRepAdaptor_Curve.hxx>
22#include <BRepAdaptor_Surface.hxx>
60bf98ae 23#include <BRepLib_MakeVertex.hxx>
24#include <BRep_Tool.hxx>
7fd59977 25#include <ElCLib.hxx>
60bf98ae 26#include <GCPnts_UniformAbscissa.hxx>
27#include <GC_MakeArcOfCircle.hxx>
a6eb515f 28#include <gce_MakeLin2d.hxx>
60bf98ae 29#include <gce_MakeLin.hxx>
30#include <gce_MakeCirc.hxx>
a6eb515f 31#include <gce_MakeCone.hxx>
fe83e1ea 32#include <gce_MakePln.hxx>
fe83e1ea 33#include <gce_MakeDir.hxx>
7fd59977 34#include <Geom_Circle.hxx>
60bf98ae 35#include <Geom_TrimmedCurve.hxx>
7fd59977 36#include <Geom_ConicalSurface.hxx>
37#include <Geom_SurfaceOfRevolution.hxx>
7fd59977 38#include <Geom_OffsetSurface.hxx>
a6eb515f 39#include <Graphic3d_ArrayOfSegments.hxx>
a6eb515f 40#include <Graphic3d_Group.hxx>
a6eb515f 41#include <Graphic3d_ArrayOfPolylines.hxx>
7fd59977 42#include <IntAna2d_AnaIntersection.hxx>
7fd59977 43#include <ProjLib.hxx>
a6eb515f 44#include <Prs3d_Root.hxx>
d7bffd44 45#include <Prs3d_ShadingAspect.hxx>
a6eb515f 46#include <PrsMgr_PresentationManager3d.hxx>
62b6361a 47#include <Select3D_SensitiveGroup.hxx>
7fd59977 48#include <Select3D_SensitiveSegment.hxx>
a6eb515f 49#include <SelectMgr_Selection.hxx>
af203d54 50#include <Standard_ProgramError.hxx>
7fd59977 51#include <UnitsAPI.hxx>
ec357c5c 52#include <Geom_Line.hxx>
53#include <Geom_Plane.hxx>
7fd59977 54
7fd59977 55
92efcf78 56IMPLEMENT_STANDARD_RTTIEXT(AIS_AngleDimension,AIS_Dimension)
57
d7bffd44 58namespace
59{
fe83e1ea 60 static const TCollection_ExtendedString THE_EMPTY_LABEL_STRING;
61 static const Standard_Real THE_EMPTY_LABEL_WIDTH = 0.0;
62 static const Standard_ExtCharacter THE_DEGREE_SYMBOL (0x00B0);
63 static const Standard_Real THE_3D_TEXT_MARGIN = 0.1;
a3f6f591 64}
d7bffd44 65
7fd59977 66//=======================================================================
7fd59977 67//function : Constructor
60bf98ae 68//purpose :
7fd59977 69//=======================================================================
a6eb515f 70AIS_AngleDimension::AIS_AngleDimension (const TopoDS_Edge& theFirstEdge,
71 const TopoDS_Edge& theSecondEdge)
60bf98ae 72: AIS_Dimension (AIS_KOD_PLANEANGLE)
7fd59977 73{
60bf98ae 74 Init();
75 SetMeasuredGeometry (theFirstEdge, theSecondEdge);
7fd59977 76}
77
7fd59977 78//=======================================================================
79//function : Constructor
60bf98ae 80//purpose :
7fd59977 81//=======================================================================
60bf98ae 82AIS_AngleDimension::AIS_AngleDimension (const gp_Pnt& theFirstPoint,
83 const gp_Pnt& theSecondPoint,
84 const gp_Pnt& theThirdPoint)
85: AIS_Dimension (AIS_KOD_PLANEANGLE)
7fd59977 86{
60bf98ae 87 Init();
88 SetMeasuredGeometry (theFirstPoint, theSecondPoint, theThirdPoint);
7fd59977 89}
90
91//=======================================================================
92//function : Constructor
60bf98ae 93//purpose :
7fd59977 94//=======================================================================
60bf98ae 95AIS_AngleDimension::AIS_AngleDimension (const TopoDS_Vertex& theFirstVertex,
96 const TopoDS_Vertex& theSecondVertex,
97 const TopoDS_Vertex& theThirdVertex)
98: AIS_Dimension (AIS_KOD_PLANEANGLE)
7fd59977 99{
60bf98ae 100 Init();
101 SetMeasuredGeometry (theFirstVertex, theSecondVertex, theThirdVertex);
7fd59977 102}
103
7fd59977 104//=======================================================================
a6eb515f 105//function : Constructor
60bf98ae 106//purpose :
7fd59977 107//=======================================================================
a6eb515f 108AIS_AngleDimension::AIS_AngleDimension (const TopoDS_Face& theCone)
60bf98ae 109: AIS_Dimension (AIS_KOD_PLANEANGLE)
7fd59977 110{
60bf98ae 111 Init();
112 SetMeasuredGeometry (theCone);
7fd59977 113}
114
115//=======================================================================
a6eb515f 116//function : Constructor
60bf98ae 117//purpose :
7fd59977 118//=======================================================================
a6eb515f 119AIS_AngleDimension::AIS_AngleDimension (const TopoDS_Face& theFirstFace,
60bf98ae 120 const TopoDS_Face& theSecondFace)
121: AIS_Dimension (AIS_KOD_PLANEANGLE)
7fd59977 122{
60bf98ae 123 Init();
124 SetMeasuredGeometry (theFirstFace, theSecondFace);
7fd59977 125}
126
7fd59977 127//=======================================================================
60bf98ae 128//function : Constructor
7fd59977 129//purpose :
130//=======================================================================
60bf98ae 131AIS_AngleDimension::AIS_AngleDimension (const TopoDS_Face& theFirstFace,
132 const TopoDS_Face& theSecondFace,
133 const gp_Pnt& thePoint)
134: AIS_Dimension (AIS_KOD_PLANEANGLE)
7fd59977 135{
60bf98ae 136 Init();
137 SetMeasuredGeometry (theFirstFace, theSecondFace, thePoint);
7fd59977 138}
139
7fd59977 140//=======================================================================
60bf98ae 141//function : SetMeasuredGeometry
142//purpose :
7fd59977 143//=======================================================================
60bf98ae 144void AIS_AngleDimension::SetMeasuredGeometry (const TopoDS_Edge& theFirstEdge,
145 const TopoDS_Edge& theSecondEdge)
7fd59977 146{
60bf98ae 147 gp_Pln aComputedPlane;
148
91b16a64 149 myFirstShape = theFirstEdge;
150 mySecondShape = theSecondEdge;
151 myThirdShape = TopoDS_Shape();
152 myGeometryType = GeometryType_Edges;
153 myIsGeometryValid = InitTwoEdgesAngle (aComputedPlane);
60bf98ae 154
91b16a64 155 if (myIsGeometryValid && !myIsPlaneCustom)
60bf98ae 156 {
4d147bf2 157 myPlane = aComputedPlane;
60bf98ae 158 }
159
60bf98ae 160 SetToUpdate();
7fd59977 161}
162
163//=======================================================================
60bf98ae 164//function : SetMeasuredGeometry
165//purpose :
7fd59977 166//=======================================================================
60bf98ae 167void AIS_AngleDimension::SetMeasuredGeometry (const gp_Pnt& theFirstPoint,
168 const gp_Pnt& theSecondPoint,
169 const gp_Pnt& theThirdPoint)
7fd59977 170{
60bf98ae 171 myFirstPoint = theFirstPoint;
172 myCenterPoint = theSecondPoint;
173 mySecondPoint = theThirdPoint;
174 myFirstShape = BRepLib_MakeVertex (myFirstPoint);
175 mySecondShape = BRepLib_MakeVertex (myCenterPoint);
176 myThirdShape = BRepLib_MakeVertex (mySecondPoint);
177 myGeometryType = GeometryType_Points;
91b16a64 178 myIsGeometryValid = IsValidPoints (myFirstPoint, myCenterPoint, mySecondPoint);
7fd59977 179
91b16a64 180 if (myIsGeometryValid && !myIsPlaneCustom)
a6eb515f 181 {
60bf98ae 182 ComputePlane();
183 }
a6eb515f 184
60bf98ae 185 SetToUpdate();
186}
a6eb515f 187
60bf98ae 188//=======================================================================
189//function : SetMeasuredGeometry
190//purpose :
191//=======================================================================
192void AIS_AngleDimension::SetMeasuredGeometry (const TopoDS_Vertex& theFirstVertex,
193 const TopoDS_Vertex& theSecondVertex,
194 const TopoDS_Vertex& theThirdVertex)
195{
91b16a64 196 myFirstShape = theFirstVertex;
197 mySecondShape = theSecondVertex;
198 myThirdShape = theThirdVertex;
199 myFirstPoint = BRep_Tool::Pnt (theFirstVertex);
200 myCenterPoint = BRep_Tool::Pnt (theSecondVertex);
201 mySecondPoint = BRep_Tool::Pnt (theThirdVertex);
202 myGeometryType = GeometryType_Points;
203 myIsGeometryValid = IsValidPoints (myFirstPoint, myCenterPoint, mySecondPoint);
204
205 if (myIsGeometryValid && !myIsPlaneCustom)
a6eb515f 206 {
60bf98ae 207 ComputePlane();
7fd59977 208 }
7fd59977 209
60bf98ae 210 SetToUpdate();
211}
7fd59977 212
60bf98ae 213//=======================================================================
214//function : SetMeasuredGeometry
215//purpose :
216//=======================================================================
217void AIS_AngleDimension::SetMeasuredGeometry (const TopoDS_Face& theCone)
218{
91b16a64 219 myFirstShape = theCone;
220 mySecondShape = TopoDS_Shape();
221 myThirdShape = TopoDS_Shape();
222 myGeometryType = GeometryType_Face;
223 myIsGeometryValid = InitConeAngle();
7fd59977 224
91b16a64 225 if (myIsGeometryValid && !myIsPlaneCustom)
a6eb515f 226 {
60bf98ae 227 ComputePlane();
a6eb515f 228 }
7fd59977 229
60bf98ae 230 SetToUpdate();
7fd59977 231}
232
233//=======================================================================
60bf98ae 234//function : SetMeasuredGeometry
235//purpose :
7fd59977 236//=======================================================================
60bf98ae 237void AIS_AngleDimension::SetMeasuredGeometry (const TopoDS_Face& theFirstFace,
238 const TopoDS_Face& theSecondFace)
7fd59977 239{
91b16a64 240 myFirstShape = theFirstFace;
241 mySecondShape = theSecondFace;
242 myThirdShape = TopoDS_Shape();
243 myGeometryType = GeometryType_Faces;
244 myIsGeometryValid = InitTwoFacesAngle();
a6eb515f 245
91b16a64 246 if (myIsGeometryValid && !myIsPlaneCustom)
a6eb515f 247 {
60bf98ae 248 ComputePlane();
7fd59977 249 }
60bf98ae 250
60bf98ae 251 SetToUpdate();
7fd59977 252}
253
7fd59977 254//=======================================================================
60bf98ae 255//function : SetMeasuredGeometry
7fd59977 256//purpose :
257//=======================================================================
60bf98ae 258void AIS_AngleDimension::SetMeasuredGeometry (const TopoDS_Face& theFirstFace,
259 const TopoDS_Face& theSecondFace,
260 const gp_Pnt& thePoint)
7fd59977 261{
91b16a64 262 myFirstShape = theFirstFace;
263 mySecondShape = theSecondFace;
264 myThirdShape = TopoDS_Shape();
265 myGeometryType = GeometryType_Faces;
266 myIsGeometryValid = InitTwoFacesAngle (thePoint);
60bf98ae 267
91b16a64 268 if (myIsGeometryValid && !myIsPlaneCustom)
60bf98ae 269 {
270 ComputePlane();
271 }
272
60bf98ae 273 SetToUpdate();
7fd59977 274}
275
276//=======================================================================
60bf98ae 277//function : Init
7fd59977 278//purpose :
279//=======================================================================
60bf98ae 280void AIS_AngleDimension::Init()
7fd59977 281{
60bf98ae 282 SetSpecialSymbol (THE_DEGREE_SYMBOL);
283 SetDisplaySpecialSymbol (AIS_DSS_After);
284 SetFlyout (15.0);
7fd59977 285}
286
7fd59977 287//=======================================================================
60bf98ae 288//function: GetCenterOnArc
289//purpose :
7fd59977 290//=======================================================================
60bf98ae 291gp_Pnt AIS_AngleDimension::GetCenterOnArc (const gp_Pnt& theFirstAttach,
292 const gp_Pnt& theSecondAttach,
af203d54 293 const gp_Pnt& theCenter) const
7fd59977 294{
60bf98ae 295 // construct plane where the circle and the arc are located
296 gce_MakePln aConstructPlane (theFirstAttach, theSecondAttach, theCenter);
297 if (!aConstructPlane.IsDone())
a6eb515f 298 {
60bf98ae 299 return gp::Origin();
7fd59977 300 }
60bf98ae 301
302 gp_Pln aPlane = aConstructPlane.Value();
7fd59977 303
60bf98ae 304 Standard_Real aRadius = theFirstAttach.Distance (theCenter);
7fd59977 305
60bf98ae 306 // construct circle forming the arc
307 gce_MakeCirc aConstructCircle (theCenter, aPlane, aRadius);
308 if (!aConstructCircle.IsDone())
a6eb515f 309 {
60bf98ae 310 return gp::Origin();
7fd59977 311 }
312
60bf98ae 313 gp_Circ aCircle = aConstructCircle.Value();
314
315 // compute angle parameters of arc end-points on circle
316 Standard_Real aParamBeg = ElCLib::Parameter (aCircle, theFirstAttach);
317 Standard_Real aParamEnd = ElCLib::Parameter (aCircle, theSecondAttach);
318 ElCLib::AdjustPeriodic (0.0, M_PI * 2, Precision::PConfusion(), aParamBeg, aParamEnd);
7fd59977 319
60bf98ae 320 return ElCLib::Value ((aParamBeg + aParamEnd) * 0.5, aCircle);
321}
322
323//=======================================================================
4d147bf2 324//function : GetNormalForMinAngle
325//purpose :
326//=======================================================================
327gp_Dir AIS_AngleDimension::GetNormalForMinAngle() const
328{
329 const gp_Dir& aNormal = myPlane.Axis().Direction();
330 gp_Dir aFirst (gp_Vec (myCenterPoint, myFirstPoint) );
331 gp_Dir aSecond (gp_Vec (myCenterPoint, mySecondPoint) );
332
333 return aFirst.AngleWithRef (aSecond, aNormal) < 0.0
334 ? aNormal.Reversed()
335 : aNormal;
336}
337
338//=======================================================================
60bf98ae 339//function : DrawArc
340//purpose : draws the arc between two attach points
341//=======================================================================
342void AIS_AngleDimension::DrawArc (const Handle(Prs3d_Presentation)& thePresentation,
343 const gp_Pnt& theFirstAttach,
344 const gp_Pnt& theSecondAttach,
345 const gp_Pnt& theCenter,
346 const Standard_Real theRadius,
347 const Standard_Integer theMode)
348{
4d147bf2 349 gp_Pln aPlane (myCenterPoint, GetNormalForMinAngle());
60bf98ae 350
351 // construct circle forming the arc
352 gce_MakeCirc aConstructCircle (theCenter, aPlane, theRadius);
353 if (!aConstructCircle.IsDone())
a6eb515f 354 {
60bf98ae 355 return;
7fd59977 356 }
357
60bf98ae 358 gp_Circ aCircle = aConstructCircle.Value();
359
360 // construct the arc
361 GC_MakeArcOfCircle aConstructArc (aCircle, theFirstAttach, theSecondAttach, Standard_True);
362 if (!aConstructArc.IsDone())
a6eb515f 363 {
60bf98ae 364 return;
7fd59977 365 }
366
60bf98ae 367 // generate points with specified deflection
368 const Handle(Geom_TrimmedCurve)& anArcCurve = aConstructArc.Value();
369
370 GeomAdaptor_Curve anArcAdaptor (anArcCurve, anArcCurve->FirstParameter(), anArcCurve->LastParameter());
7fd59977 371
60bf98ae 372 // compute number of discretization elements in old-fanshioned way
373 gp_Vec aCenterToFirstVec (theCenter, theFirstAttach);
374 gp_Vec aCenterToSecondVec (theCenter, theSecondAttach);
375 const Standard_Real anAngle = aCenterToFirstVec.Angle (aCenterToSecondVec);
376 const Standard_Integer aNbPoints = Max (4, Standard_Integer (50.0 * anAngle / M_PI));
a6eb515f 377
60bf98ae 378 GCPnts_UniformAbscissa aMakePnts (anArcAdaptor, aNbPoints);
379 if (!aMakePnts.IsDone())
380 {
381 return;
7fd59977 382 }
383
60bf98ae 384 // init data arrays for graphical and selection primitives
385 Handle(Graphic3d_ArrayOfPolylines) aPrimSegments = new Graphic3d_ArrayOfPolylines (aNbPoints);
7fd59977 386
60bf98ae 387 SelectionGeometry::Curve& aSensitiveCurve = mySelectionGeom.NewCurve();
a6eb515f 388
60bf98ae 389 // load data into arrays
390 for (Standard_Integer aPntIt = 1; aPntIt <= aMakePnts.NbPoints(); ++aPntIt)
a6eb515f 391 {
60bf98ae 392 gp_Pnt aPnt = anArcAdaptor.Value (aMakePnts.Parameter (aPntIt));
7fd59977 393
60bf98ae 394 aPrimSegments->AddVertex (aPnt);
7fd59977 395
60bf98ae 396 aSensitiveCurve.Append (aPnt);
a6eb515f 397 }
7fd59977 398
60bf98ae 399 // add display presentation
400 if (!myDrawer->DimensionAspect()->IsText3d() && theMode == ComputeMode_All)
401 {
402 Prs3d_Root::CurrentGroup (thePresentation)->SetStencilTestOptions (Standard_True);
403 }
404 Handle(Graphic3d_AspectLine3d) aDimensionLineStyle = myDrawer->DimensionAspect()->LineAspect()->Aspect();
405 Prs3d_Root::CurrentGroup (thePresentation)->SetPrimitivesAspect (aDimensionLineStyle);
406 Prs3d_Root::CurrentGroup (thePresentation)->AddPrimitiveArray (aPrimSegments);
407 if (!myDrawer->DimensionAspect()->IsText3d() && theMode == ComputeMode_All)
408 {
409 Prs3d_Root::CurrentGroup (thePresentation)->SetStencilTestOptions (Standard_False);
410 }
7fd59977 411}
a6eb515f 412
7fd59977 413//=======================================================================
60bf98ae 414//function: DrawArcWithText
a6eb515f 415//purpose :
7fd59977 416//=======================================================================
60bf98ae 417void AIS_AngleDimension::DrawArcWithText (const Handle(Prs3d_Presentation)& thePresentation,
a6eb515f 418 const gp_Pnt& theFirstAttach,
419 const gp_Pnt& theSecondAttach,
60bf98ae 420 const gp_Pnt& theCenter,
a6eb515f 421 const TCollection_ExtendedString& theText,
fe83e1ea 422 const Standard_Real theTextWidth,
423 const Standard_Integer theMode,
d7bffd44 424 const Standard_Integer theLabelPosition)
7fd59977 425{
4d147bf2 426 gp_Pln aPlane (myCenterPoint, GetNormalForMinAngle());
427
60bf98ae 428 Standard_Real aRadius = theFirstAttach.Distance (myCenterPoint);
a6eb515f 429
fe83e1ea 430 // construct circle forming the arc
60bf98ae 431 gce_MakeCirc aConstructCircle (theCenter, aPlane, aRadius);
fe83e1ea 432 if (!aConstructCircle.IsDone())
433 {
434 return;
435 }
a6eb515f 436
fe83e1ea 437 gp_Circ aCircle = aConstructCircle.Value();
a6eb515f 438
fe83e1ea 439 // compute angle parameters of arc end-points on circle
440 Standard_Real aParamBeg = ElCLib::Parameter (aCircle, theFirstAttach);
441 Standard_Real aParamEnd = ElCLib::Parameter (aCircle, theSecondAttach);
60bf98ae 442 ElCLib::AdjustPeriodic (0.0, M_PI * 2, Precision::PConfusion(), aParamBeg, aParamEnd);
a6eb515f 443
fe83e1ea 444 // middle point of arc parameter on circle
445 Standard_Real aParamMid = (aParamBeg + aParamEnd) * 0.5;
a6eb515f 446
fe83e1ea 447 // add text graphical primitives
448 if (theMode == ComputeMode_All || theMode == ComputeMode_Text)
449 {
450 gp_Pnt aTextPos = ElCLib::Value (aParamMid, aCircle);
60bf98ae 451 gp_Dir aTextDir = gce_MakeDir (theFirstAttach, theSecondAttach);
fe83e1ea 452
453 // Drawing text
7c65581d 454 drawText (thePresentation,
fe83e1ea 455 aTextPos,
456 aTextDir,
457 theText,
458 theLabelPosition);
459 }
a6eb515f 460
fe83e1ea 461 if (theMode != ComputeMode_All && theMode != ComputeMode_Line)
462 {
463 return;
464 }
a6eb515f 465
fe83e1ea 466 Handle(Prs3d_DimensionAspect) aDimensionAspect = myDrawer->DimensionAspect();
a6eb515f 467
fe83e1ea 468 Standard_Boolean isLineBreak = aDimensionAspect->TextVerticalPosition() == Prs3d_DTVP_Center
469 && aDimensionAspect->IsText3d();
a6eb515f 470
fe83e1ea 471 if (isLineBreak)
a6eb515f 472 {
fe83e1ea 473 // compute gap for label as parameteric size of sector on circle segment
4d147bf2 474 Standard_Real aSectorOfText = theTextWidth / aRadius;
475 Standard_Real aTextBegin = aParamMid - aSectorOfText * 0.5;
476 Standard_Real aTextEnd = aParamMid + aSectorOfText * 0.5;
477 gp_Pnt aTextPntBeg = ElCLib::Value (aTextBegin, aCircle);
478 gp_Pnt aTextPntEnd = ElCLib::Value (aTextEnd, aCircle);
fe83e1ea 479
480 // Drawing arcs
4d147bf2 481 if (aTextBegin > aParamBeg)
482 {
483 DrawArc (thePresentation, theFirstAttach, aTextPntBeg, theCenter, aRadius, theMode);
484 }
485 if (aTextEnd < aParamEnd)
486 {
487 DrawArc (thePresentation, aTextPntEnd, theSecondAttach, theCenter, aRadius, theMode);
488 }
fe83e1ea 489 }
490 else
491 {
60bf98ae 492 DrawArc (thePresentation, theFirstAttach, theSecondAttach, theCenter, aRadius, theMode);
7fd59977 493 }
a6eb515f 494}
7fd59977 495
a6eb515f 496//=======================================================================
60bf98ae 497//function : CheckPlane
498//purpose :
a6eb515f 499//=======================================================================
60bf98ae 500Standard_Boolean AIS_AngleDimension::CheckPlane (const gp_Pln& thePlane)const
a6eb515f 501{
60bf98ae 502 if (!thePlane.Contains (myFirstPoint, Precision::Confusion()) &&
503 !thePlane.Contains (mySecondPoint, Precision::Confusion()) &&
504 !thePlane.Contains (myCenterPoint, Precision::Confusion()))
fe83e1ea 505 {
60bf98ae 506 return Standard_False;
fe83e1ea 507 }
508
60bf98ae 509 return Standard_True;
510}
a6eb515f 511
60bf98ae 512//=======================================================================
513//function : ComputePlane
514//purpose :
515//=======================================================================
516void AIS_AngleDimension::ComputePlane()
517{
91b16a64 518 if (!myIsGeometryValid)
a6eb515f 519 {
fe83e1ea 520 return;
7fd59977 521 }
7fd59977 522
4d147bf2 523 // Compute working plane so that Y axis is codirectional
524 // with Y axis of text coordinate system (necessary for text alignment)
525 gp_Vec aFirstVec = gp_Vec (myCenterPoint, myFirstPoint);
526 gp_Vec aSecondVec = gp_Vec (myCenterPoint, mySecondPoint);
527 gp_Vec aDirectionN = aSecondVec ^ aFirstVec;
528 gp_Vec aDirectionY = aFirstVec + aSecondVec;
529 gp_Vec aDirectionX = aDirectionY ^ aDirectionN;
fe83e1ea 530
60bf98ae 531 myPlane = gp_Pln (gp_Ax3 (myCenterPoint, gp_Dir (aDirectionN), gp_Dir (aDirectionX)));
532}
7fd59977 533
60bf98ae 534//=======================================================================
535//function : GetModelUnits
536//purpose :
537//=======================================================================
538const TCollection_AsciiString& AIS_AngleDimension::GetModelUnits() const
539{
540 return myDrawer->DimAngleModelUnits();
541}
fe83e1ea 542
60bf98ae 543//=======================================================================
544//function : GetDisplayUnits
545//purpose :
546//=======================================================================
547const TCollection_AsciiString& AIS_AngleDimension::GetDisplayUnits() const
548{
549 return myDrawer->DimAngleDisplayUnits();
550}
fe83e1ea 551
60bf98ae 552//=======================================================================
553//function : SetModelUnits
554//purpose :
555//=======================================================================
556void AIS_AngleDimension::SetModelUnits (const TCollection_AsciiString& theUnits)
557{
558 myDrawer->SetDimAngleModelUnits (theUnits);
559}
fe83e1ea 560
60bf98ae 561//=======================================================================
562//function : SetDisplayUnits
563//purpose :
564//=======================================================================
565void AIS_AngleDimension::SetDisplayUnits (const TCollection_AsciiString& theUnits)
566{
567 myDrawer->SetDimAngleDisplayUnits (theUnits);
568}
569
570//=======================================================================
571//function : ComputeValue
572//purpose :
573//=======================================================================
574Standard_Real AIS_AngleDimension::ComputeValue() const
575{
576 if (!IsValid())
a6eb515f 577 {
60bf98ae 578 return 0.0;
7fd59977 579 }
60bf98ae 580
581 gp_Vec aVec1 (myCenterPoint, myFirstPoint);
582 gp_Vec aVec2 (myCenterPoint, mySecondPoint);
583
4d147bf2 584 Standard_Real anAngle = aVec1.AngleWithRef (aVec2, GetNormalForMinAngle());
60bf98ae 585
af203d54 586 return anAngle > 0.0 ? anAngle : (2.0 * M_PI + anAngle);
7fd59977 587}
588
7fd59977 589//=======================================================================
a6eb515f 590//function : Compute
591//purpose : Having three gp_Pnt points compute presentation
7fd59977 592//=======================================================================
a6eb515f 593void AIS_AngleDimension::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePM*/,
594 const Handle(Prs3d_Presentation)& thePresentation,
595 const Standard_Integer theMode)
596{
fe83e1ea 597 mySelectionGeom.Clear (theMode);
7fd59977 598
60bf98ae 599 if (!IsValid())
a6eb515f 600 {
a6eb515f 601 return;
60bf98ae 602 }
7fd59977 603
a6eb515f 604 // Parameters for presentation
605 Handle(Prs3d_DimensionAspect) aDimensionAspect = myDrawer->DimensionAspect();
d7bffd44 606
60bf98ae 607 Prs3d_Root::CurrentGroup(thePresentation)->SetPrimitivesAspect (aDimensionAspect->LineAspect()->Aspect());
d7bffd44 608
a6eb515f 609 Quantity_Length anArrowLength = aDimensionAspect->ArrowAspect()->Length();
d7bffd44 610
60bf98ae 611 // prepare label string and compute its geometrical width
612 Standard_Real aLabelWidth;
613 TCollection_ExtendedString aLabelString = GetValueString (aLabelWidth);
d7bffd44 614
fe83e1ea 615 // add margins to label width
616 if (aDimensionAspect->IsText3d())
617 {
60bf98ae 618 aLabelWidth += aDimensionAspect->TextAspect()->Height() * THE_3D_TEXT_MARGIN * 2.0;
d7bffd44 619 }
620
af203d54 621 // Get parameters from aspect or adjust it according with custom text position
622 Standard_Real anExtensionSize = aDimensionAspect->ExtensionSize();
623 Prs3d_DimensionTextHorizontalPosition aHorisontalTextPos = aDimensionAspect->TextHorizontalPosition();
7fd59977 624
af203d54 625 if (IsTextPositionCustom())
d7bffd44 626 {
91b16a64 627 AdjustParameters (myFixedTextPosition,anExtensionSize, aHorisontalTextPos, myFlyout);
af203d54 628 }
fe83e1ea 629
af203d54 630 // Handle user-defined and automatic arrow placement
631 Standard_Boolean isArrowsExternal = Standard_False;
632 Standard_Integer aLabelPosition = LabelPosition_None;
fe83e1ea 633
af203d54 634 FitTextAlignment (aHorisontalTextPos, aLabelPosition, isArrowsExternal);
d7bffd44 635
af203d54 636 gp_Pnt aFirstAttach = myCenterPoint.Translated (gp_Vec(myCenterPoint, myFirstPoint).Normalized() * GetFlyout());
637 gp_Pnt aSecondAttach = myCenterPoint.Translated (gp_Vec(myCenterPoint, mySecondPoint).Normalized() * GetFlyout());
7fd59977 638
a6eb515f 639 //Arrows positions and directions
4d147bf2 640 gp_Vec aWorkingPlaneDir (GetNormalForMinAngle());
d7bffd44 641
4d147bf2 642 gp_Dir aFirstExtensionDir = aWorkingPlaneDir.Reversed() ^ gp_Vec (myCenterPoint, aFirstAttach);
643 gp_Dir aSecondExtensionDir = aWorkingPlaneDir ^ gp_Vec (myCenterPoint, aSecondAttach);
7fd59977 644
d7bffd44 645 gp_Vec aFirstArrowVec = gp_Vec (aFirstExtensionDir) * anArrowLength;
646 gp_Vec aSecondArrowVec = gp_Vec (aSecondExtensionDir) * anArrowLength;
7fd59977 647
d7bffd44 648 if (isArrowsExternal)
a6eb515f 649 {
650 aFirstArrowVec.Reverse();
651 aSecondArrowVec.Reverse();
d7bffd44 652 }
653
4d147bf2 654 gp_Pnt aFirstArrowBegin (0.0, 0.0, 0.0);
655 gp_Pnt aFirstArrowEnd (0.0, 0.0, 0.0);
656 gp_Pnt aSecondArrowBegin (0.0, 0.0, 0.0);
657 gp_Pnt aSecondArrowEnd (0.0, 0.0, 0.0);
658
d7bffd44 659 aFirstArrowBegin = aFirstAttach;
660 aSecondArrowBegin = aSecondAttach;
661 aFirstArrowEnd = aFirstAttach.Translated (-aFirstArrowVec);
662 aSecondArrowEnd = aSecondAttach.Translated (-aSecondArrowVec);
663
d7bffd44 664 // Group1: stenciling text and the angle dimension arc
665 Prs3d_Root::NewGroup (thePresentation);
666
d7bffd44 667 Standard_Integer aHPosition = aLabelPosition & LabelPosition_HMask;
668
669 // draw text label
670 switch (aHPosition)
b8ddfc2f 671 {
d7bffd44 672 case LabelPosition_HCenter :
a6eb515f 673 {
d7bffd44 674 Standard_Boolean isLineBreak = aDimensionAspect->TextVerticalPosition() == Prs3d_DTVP_Center
675 && aDimensionAspect->IsText3d();
676
677 if (isLineBreak)
678 {
60bf98ae 679 DrawArcWithText (thePresentation,
d7bffd44 680 aFirstAttach,
681 aSecondAttach,
60bf98ae 682 myCenterPoint,
683 aLabelString,
684 aLabelWidth,
fe83e1ea 685 theMode,
d7bffd44 686 aLabelPosition);
687 break;
688 }
689
fe83e1ea 690 // compute text primitives
691 if (theMode == ComputeMode_All || theMode == ComputeMode_Text)
d7bffd44 692 {
60bf98ae 693 gp_Vec aDimensionDir (aFirstAttach, aSecondAttach);
af203d54 694 gp_Pnt aTextPos = IsTextPositionCustom() ? myFixedTextPosition
695 : GetCenterOnArc (aFirstAttach, aSecondAttach, myCenterPoint);
60bf98ae 696 gp_Dir aTextDir = aDimensionDir;
fe83e1ea 697
7c65581d 698 drawText (thePresentation,
fe83e1ea 699 aTextPos,
700 aTextDir,
60bf98ae 701 aLabelString,
fe83e1ea 702 aLabelPosition);
d7bffd44 703 }
704
fe83e1ea 705 if (theMode == ComputeMode_All || theMode == ComputeMode_Line)
706 {
60bf98ae 707 DrawArc (thePresentation,
fe83e1ea 708 isArrowsExternal ? aFirstAttach : aFirstArrowEnd,
709 isArrowsExternal ? aSecondAttach : aSecondArrowEnd,
60bf98ae 710 myCenterPoint,
fe83e1ea 711 Abs (GetFlyout()),
712 theMode);
713 }
a6eb515f 714 }
d7bffd44 715 break;
716
717 case LabelPosition_Left :
a6eb515f 718 {
60bf98ae 719 DrawExtension (thePresentation,
d7bffd44 720 anExtensionSize,
721 isArrowsExternal ? aFirstArrowEnd : aFirstAttach,
722 aFirstExtensionDir,
60bf98ae 723 aLabelString,
724 aLabelWidth,
fe83e1ea 725 theMode,
d7bffd44 726 aLabelPosition);
a6eb515f 727 }
d7bffd44 728 break;
729
730 case LabelPosition_Right :
a6eb515f 731 {
60bf98ae 732 DrawExtension (thePresentation,
d7bffd44 733 anExtensionSize,
734 isArrowsExternal ? aSecondArrowEnd : aSecondAttach,
735 aSecondExtensionDir,
60bf98ae 736 aLabelString,
737 aLabelWidth,
fe83e1ea 738 theMode,
d7bffd44 739 aLabelPosition);
a6eb515f 740 }
d7bffd44 741 break;
742 }
743
744 // dimension arc without text
fe83e1ea 745 if ((theMode == ComputeMode_All || theMode == ComputeMode_Line) && aHPosition != LabelPosition_HCenter)
d7bffd44 746 {
747 Prs3d_Root::NewGroup (thePresentation);
748
60bf98ae 749 DrawArc (thePresentation,
d7bffd44 750 isArrowsExternal ? aFirstAttach : aFirstArrowEnd,
751 isArrowsExternal ? aSecondAttach : aSecondArrowEnd,
60bf98ae 752 myCenterPoint,
d7bffd44 753 Abs(GetFlyout ()),
fe83e1ea 754 theMode);
d7bffd44 755 }
7fd59977 756
d7bffd44 757 // arrows and arrow extensions
fe83e1ea 758 if (theMode == ComputeMode_All || theMode == ComputeMode_Line)
d7bffd44 759 {
760 Prs3d_Root::NewGroup (thePresentation);
761
60bf98ae 762 DrawArrow (thePresentation, aFirstArrowBegin, gp_Dir (aFirstArrowVec));
763 DrawArrow (thePresentation, aSecondArrowBegin, gp_Dir (aSecondArrowVec));
d7bffd44 764 }
765
fe83e1ea 766 if ((theMode == ComputeMode_All || theMode == ComputeMode_Line) && isArrowsExternal)
d7bffd44 767 {
768 Prs3d_Root::NewGroup (thePresentation);
769
770 if (aHPosition != LabelPosition_Left)
771 {
60bf98ae 772 DrawExtension (thePresentation,
af203d54 773 aDimensionAspect->ArrowTailSize(),
d7bffd44 774 aFirstArrowEnd,
775 aFirstExtensionDir,
fe83e1ea 776 THE_EMPTY_LABEL_STRING,
777 THE_EMPTY_LABEL_WIDTH,
778 theMode,
d7bffd44 779 LabelPosition_None);
780 }
781
782 if (aHPosition != LabelPosition_Right)
a6eb515f 783 {
60bf98ae 784 DrawExtension (thePresentation,
af203d54 785 aDimensionAspect->ArrowTailSize(),
d7bffd44 786 aSecondArrowEnd,
787 aSecondExtensionDir,
fe83e1ea 788 THE_EMPTY_LABEL_STRING,
789 THE_EMPTY_LABEL_WIDTH,
790 theMode,
d7bffd44 791 LabelPosition_None);
a6eb515f 792 }
793 }
7fd59977 794
d7bffd44 795 // flyouts
60bf98ae 796 if (theMode == ComputeMode_All)
a6eb515f 797 {
d7bffd44 798 Prs3d_Root::NewGroup (thePresentation);
799
800 Handle(Graphic3d_ArrayOfSegments) aPrimSegments = new Graphic3d_ArrayOfSegments (4);
60bf98ae 801 aPrimSegments->AddVertex (myCenterPoint);
a6eb515f 802 aPrimSegments->AddVertex (aFirstAttach);
60bf98ae 803 aPrimSegments->AddVertex (myCenterPoint);
a6eb515f 804 aPrimSegments->AddVertex (aSecondAttach);
d7bffd44 805
806 Handle(Graphic3d_AspectLine3d) aFlyoutStyle = myDrawer->DimensionAspect()->LineAspect()->Aspect();
807 Prs3d_Root::CurrentGroup (thePresentation)->SetPrimitivesAspect (aFlyoutStyle);
a6eb515f 808 Prs3d_Root::CurrentGroup (thePresentation)->AddPrimitiveArray (aPrimSegments);
7fd59977 809 }
810
91b16a64 811 mySelectionGeom.IsComputed = Standard_True;
7fd59977 812}
62b6361a 813
814//=======================================================================
60bf98ae 815//function : ComputeFlyoutSelection
62b6361a 816//purpose : computes selection for flyouts
817//=======================================================================
60bf98ae 818void AIS_AngleDimension::ComputeFlyoutSelection (const Handle(SelectMgr_Selection)& theSelection,
fe83e1ea 819 const Handle(SelectMgr_EntityOwner)& theOwner)
62b6361a 820{
60bf98ae 821 gp_Pnt aFirstAttach = myCenterPoint.Translated (gp_Vec (myCenterPoint, myFirstPoint).Normalized() * GetFlyout());
822 gp_Pnt aSecondAttach = myCenterPoint.Translated (gp_Vec (myCenterPoint, mySecondPoint).Normalized() * GetFlyout());
823
824 Handle(Select3D_SensitiveGroup) aSensitiveEntity = new Select3D_SensitiveGroup (theOwner);
825 aSensitiveEntity->Add (new Select3D_SensitiveSegment (theOwner, myCenterPoint, aFirstAttach));
826 aSensitiveEntity->Add (new Select3D_SensitiveSegment (theOwner, myCenterPoint, aSecondAttach));
827
828 theSelection->Add (aSensitiveEntity);
829}
830
831//=======================================================================
832//function : InitTwoEdgesAngle
833//purpose :
834//=======================================================================
835Standard_Boolean AIS_AngleDimension::InitTwoEdgesAngle (gp_Pln& theComputedPlane)
836{
837 TopoDS_Edge aFirstEdge = TopoDS::Edge (myFirstShape);
838 TopoDS_Edge aSecondEdge = TopoDS::Edge (mySecondShape);
839
840 BRepAdaptor_Curve aMakeFirstLine (aFirstEdge);
841 BRepAdaptor_Curve aMakeSecondLine (aSecondEdge);
842
843 if (aMakeFirstLine.GetType() != GeomAbs_Line || aMakeSecondLine.GetType() != GeomAbs_Line)
fe83e1ea 844 {
60bf98ae 845 return Standard_False;
fe83e1ea 846 }
847
60bf98ae 848 Handle(Geom_Line) aFirstLine = new Geom_Line (aMakeFirstLine.Line());
849 Handle(Geom_Line) aSecondLine = new Geom_Line (aMakeSecondLine.Line());
fe83e1ea 850
60bf98ae 851 gp_Lin aFirstLin = aFirstLine->Lin();
852 gp_Lin aSecondLin = aSecondLine->Lin();
fe83e1ea 853
4d147bf2 854 Standard_Boolean isParallelLines = aFirstLin.Direction().IsParallel (aSecondLin.Direction(), Precision::Angular());
60bf98ae 855
4d147bf2 856 theComputedPlane = isParallelLines ? gp_Pln(gp::XOY())
857 : gp_Pln (aSecondLin.Location(), gp_Vec (aFirstLin.Direction()) ^ gp_Vec (aSecondLin.Direction()));
60bf98ae 858
4d147bf2 859 // Compute geometry for this plane and edges
60bf98ae 860 Standard_Boolean isInfinite1,isInfinite2;
861 gp_Pnt aFirstPoint1, aLastPoint1, aFirstPoint2, aLastPoint2;
aa00364d 862 Handle(Geom_Curve) aFirstCurve = aFirstLine, aSecondCurve = aSecondLine;
60bf98ae 863 if (!AIS::ComputeGeometry (aFirstEdge, aSecondEdge,
aa00364d 864 aFirstCurve, aSecondCurve,
60bf98ae 865 aFirstPoint1, aLastPoint1,
866 aFirstPoint2, aLastPoint2,
867 isInfinite1, isInfinite2))
868 {
869 return Standard_False;
870 }
871
4d147bf2 872 Standard_Boolean isSameLines = aFirstLin.Direction().IsEqual (aSecondLin.Direction(), Precision::Angular())
873 && aFirstLin.Location().IsEqual (aSecondLin.Location(),Precision::Confusion());
874
875 // It can be the same gp_Lin geometry but the different begin and end parameters
876 Standard_Boolean isSameEdges =
877 (aFirstPoint1.IsEqual (aFirstPoint2, Precision::Confusion()) && aLastPoint1.IsEqual (aLastPoint2, Precision::Confusion()))
878 || (aFirstPoint1.IsEqual (aLastPoint2, Precision::Confusion()) && aLastPoint1.IsEqual (aFirstPoint2, Precision::Confusion()));
60bf98ae 879
4d147bf2 880 if (isParallelLines)
881 {
882 // Zero angle, it could not handle this geometry
883 if (isSameLines && isSameEdges)
60bf98ae 884 {
4d147bf2 885 return Standard_False;
60bf98ae 886 }
887
4d147bf2 888 // Handle the case of Pi angle
889 const Standard_Real aParam11 = ElCLib::Parameter (aFirstLin, aFirstPoint1);
890 const Standard_Real aParam12 = ElCLib::Parameter (aFirstLin, aLastPoint1);
891 const Standard_Real aParam21 = ElCLib::Parameter (aFirstLin, aFirstPoint2);
892 const Standard_Real aParam22 = ElCLib::Parameter (aFirstLin, aLastPoint2);
893 myCenterPoint = ElCLib::Value ( (Min (aParam11, aParam12) + Max (aParam21, aParam22)) * 0.5, aFirstLin);
894 myFirstPoint = myCenterPoint.Translated (gp_Vec (aFirstLin.Direction()) * Abs (GetFlyout()));
895 mySecondPoint = myCenterPoint.XYZ() + (aFirstLin.Direction().IsEqual (aSecondLin.Direction(), Precision::Angular())
896 ? aFirstLin.Direction().Reversed().XYZ() * Abs (GetFlyout())
897 : aSecondLin.Direction().XYZ() * Abs (GetFlyout()));
60bf98ae 898 }
899 else
900 {
901 // Find intersection
902 gp_Lin2d aFirstLin2d = ProjLib::Project (theComputedPlane, aFirstLin);
903 gp_Lin2d aSecondLin2d = ProjLib::Project (theComputedPlane, aSecondLin);
904
905 IntAna2d_AnaIntersection anInt2d (aFirstLin2d, aSecondLin2d);
906 gp_Pnt2d anIntersectPoint;
907 if (!anInt2d.IsDone() || anInt2d.IsEmpty())
908 {
909 return Standard_False;
910 }
911
912 anIntersectPoint = gp_Pnt2d (anInt2d.Point(1).Value());
913 myCenterPoint = ElCLib::To3d (theComputedPlane.Position().Ax2(), anIntersectPoint);
914
915 if (isInfinite1 || isInfinite2)
916 {
917 myFirstPoint = myCenterPoint.Translated (gp_Vec (aFirstLin.Direction()) * Abs (GetFlyout()));
918 mySecondPoint = myCenterPoint.Translated (gp_Vec (aSecondLin.Direction()) * Abs (GetFlyout()));
919
920 return IsValidPoints (myFirstPoint, myCenterPoint, mySecondPoint);
921 }
922
923 // |
924 // | <- dimension should be here
925 // *----
926 myFirstPoint = myCenterPoint.Distance (aFirstPoint1) > myCenterPoint.Distance (aLastPoint1)
927 ? aFirstPoint1
928 : aLastPoint1;
929
930 mySecondPoint = myCenterPoint.Distance (aFirstPoint2) > myCenterPoint.Distance (aLastPoint2)
931 ? aFirstPoint2
932 : aLastPoint2;
933 }
934
935 return IsValidPoints (myFirstPoint, myCenterPoint, mySecondPoint);
936}
937
938//=======================================================================
939//function : InitTwoFacesAngle
940//purpose : initialization of angle dimension between two faces
941//=======================================================================
942Standard_Boolean AIS_AngleDimension::InitTwoFacesAngle()
943{
944 TopoDS_Face aFirstFace = TopoDS::Face (myFirstShape);
945 TopoDS_Face aSecondFace = TopoDS::Face (mySecondShape);
946
947 gp_Dir aFirstDir, aSecondDir;
51740958 948 gp_Pln aFirstPln, aSecondPln;
60bf98ae 949 Handle(Geom_Surface) aFirstBasisSurf, aSecondBasisSurf;
950 AIS_KindOfSurface aFirstSurfType, aSecondSurfType;
951 Standard_Real aFirstOffset, aSecondOffset;
952
51740958 953 AIS::GetPlaneFromFace (aFirstFace, aFirstPln,
60bf98ae 954 aFirstBasisSurf,aFirstSurfType,aFirstOffset);
955
51740958 956 AIS::GetPlaneFromFace (aSecondFace, aSecondPln,
60bf98ae 957 aSecondBasisSurf, aSecondSurfType, aSecondOffset);
958
959 if (aFirstSurfType == AIS_KOS_Plane && aSecondSurfType == AIS_KOS_Plane)
960 {
961 //Planar faces angle
962 Handle(Geom_Plane) aFirstPlane = Handle(Geom_Plane)::DownCast (aFirstBasisSurf);
963 Handle(Geom_Plane) aSecondPlane = Handle(Geom_Plane)::DownCast (aSecondBasisSurf);
964 return AIS::InitAngleBetweenPlanarFaces (aFirstFace,
965 aSecondFace,
966 myCenterPoint,
967 myFirstPoint,
968 mySecondPoint)
969 && IsValidPoints (myFirstPoint,
970 myCenterPoint,
971 mySecondPoint);
972 }
973 else
974 {
975 // Curvilinear faces angle
976 return AIS::InitAngleBetweenCurvilinearFaces (aFirstFace,
977 aSecondFace,
978 aFirstSurfType,
979 aSecondSurfType,
980 myCenterPoint,
981 myFirstPoint,
982 mySecondPoint)
983 && IsValidPoints (myFirstPoint,
984 myCenterPoint,
985 mySecondPoint);
986 }
987}
988
989//=======================================================================
990//function : InitTwoFacesAngle
991//purpose : initialization of angle dimension between two faces
992//=======================================================================
993Standard_Boolean AIS_AngleDimension::InitTwoFacesAngle (const gp_Pnt thePointOnFirstFace)
994{
995 TopoDS_Face aFirstFace = TopoDS::Face (myFirstShape);
996 TopoDS_Face aSecondFace = TopoDS::Face (mySecondShape);
997
998 gp_Dir aFirstDir, aSecondDir;
51740958 999 gp_Pln aFirstPln, aSecondPln;
60bf98ae 1000 Handle(Geom_Surface) aFirstBasisSurf, aSecondBasisSurf;
1001 AIS_KindOfSurface aFirstSurfType, aSecondSurfType;
1002 Standard_Real aFirstOffset, aSecondOffset;
1003
51740958 1004 AIS::GetPlaneFromFace (aFirstFace, aFirstPln,
60bf98ae 1005 aFirstBasisSurf,aFirstSurfType,aFirstOffset);
1006
51740958 1007 AIS::GetPlaneFromFace (aSecondFace, aSecondPln,
60bf98ae 1008 aSecondBasisSurf, aSecondSurfType, aSecondOffset);
1009
1010 myFirstPoint = thePointOnFirstFace;
1011 if (aFirstSurfType == AIS_KOS_Plane && aSecondSurfType == AIS_KOS_Plane)
1012 {
1013 //Planar faces angle
1014 Handle(Geom_Plane) aFirstPlane = Handle(Geom_Plane)::DownCast (aFirstBasisSurf);
1015 Handle(Geom_Plane) aSecondPlane = Handle(Geom_Plane)::DownCast (aSecondBasisSurf);
1016 return AIS::InitAngleBetweenPlanarFaces (aFirstFace,
1017 aSecondFace,
1018 myCenterPoint,
1019 myFirstPoint,
1020 mySecondPoint,
1021 Standard_True)
1022 && IsValidPoints (myFirstPoint,
1023 myCenterPoint,
1024 mySecondPoint);
1025 }
1026 else
1027 {
1028 // Curvilinear faces angle
1029 return AIS::InitAngleBetweenCurvilinearFaces (aFirstFace,
1030 aSecondFace,
1031 aFirstSurfType,
1032 aSecondSurfType,
1033 myCenterPoint,
1034 myFirstPoint,
1035 mySecondPoint,
1036 Standard_True)
1037 && IsValidPoints (myFirstPoint,
1038 myCenterPoint,
1039 mySecondPoint);
1040 }
1041}
1042
1043//=======================================================================
1044//function : InitConeAngle
1045//purpose : initialization of the cone angle
1046//=======================================================================
1047Standard_Boolean AIS_AngleDimension::InitConeAngle()
1048{
1049 if (myFirstShape.IsNull())
1050 {
1051 return Standard_False;
1052 }
1053
1054 TopoDS_Face aConeShape = TopoDS::Face (myFirstShape);
1055 gp_Pln aPln;
1056 gp_Cone aCone;
1057 gp_Circ aCircle;
1058 // A surface from the Face
1059 Handle(Geom_Surface) aSurf;
1060 Handle(Geom_OffsetSurface) aOffsetSurf;
1061 Handle(Geom_ConicalSurface) aConicalSurf;
1062 Handle(Geom_SurfaceOfRevolution) aRevSurf;
1063 Handle(Geom_Line) aLine;
1064 BRepAdaptor_Surface aConeAdaptor (aConeShape);
1065 TopoDS_Face aFace;
1066 AIS_KindOfSurface aSurfType;
1067 Standard_Real anOffset = 0.;
1068 Handle(Standard_Type) aType;
1069
1070 Standard_Real aMaxV = aConeAdaptor.FirstVParameter();
1071 Standard_Real aMinV = aConeAdaptor.LastVParameter();
1072
1073 AIS::GetPlaneFromFace (aConeShape, aPln, aSurf, aSurfType, anOffset);
1074
1075 if (aSurfType == AIS_KOS_Revolution)
1076 {
1077 // Surface of revolution
1078 aRevSurf = Handle(Geom_SurfaceOfRevolution)::DownCast(aSurf);
1079 gp_Lin aLin (aRevSurf->Axis());
1080 Handle(Geom_Curve) aBasisCurve = aRevSurf->BasisCurve();
1081 //Must be a part of line (basis curve should be linear)
1082 if (aBasisCurve ->DynamicType() != STANDARD_TYPE(Geom_Line))
1083 return Standard_False;
1084
1085 gp_Pnt aFirst1 = aConeAdaptor.Value (0., aMinV);
1086 gp_Pnt aLast1 = aConeAdaptor.Value (0., aMaxV);
1087 gp_Vec aVec1 (aFirst1, aLast1);
1088
1089 //Projection <aFirst> on <aLin>
1090 gp_Pnt aFirst2 = ElCLib::Value (ElCLib::Parameter (aLin, aFirst1), aLin);
1091 // Projection <aLast> on <aLin>
1092 gp_Pnt aLast2 = ElCLib::Value (ElCLib::Parameter (aLin, aLast1), aLin);
1093
1094 gp_Vec aVec2 (aFirst2, aLast2);
1095
1096 // Check if two parts of revolution are parallel (it's a cylinder) or normal (it's a circle).
1097 if (aVec1.IsParallel (aVec2, Precision::Angular())
1098 || aVec1.IsNormal (aVec2,Precision::Angular()))
1099 return Standard_False;
1100
1101 gce_MakeCone aMkCone (aRevSurf->Axis(), aFirst1, aLast1);
1102 aCone = aMkCone.Value();
1103 myCenterPoint = aCone.Apex();
1104 }
1105 else
1106 {
1107 aType = aSurf->DynamicType();
1108 if (aType == STANDARD_TYPE(Geom_OffsetSurface) || anOffset > 0.01)
1109 {
1110 // Offset surface
1111 aOffsetSurf = new Geom_OffsetSurface (aSurf, anOffset);
1112 aSurf = aOffsetSurf->Surface();
1113 BRepBuilderAPI_MakeFace aMkFace(aSurf, Precision::Confusion());
1114 aMkFace.Build();
1115 if (!aMkFace.IsDone())
1116 return Standard_False;
1117 aConeAdaptor.Initialize (aMkFace.Face());
1118 }
1119 aCone = aConeAdaptor.Cone();
1120 aConicalSurf = Handle(Geom_ConicalSurface)::DownCast (aSurf);
1121 myCenterPoint = aConicalSurf->Apex();
1122 }
1123
1124 // A circle where the angle is drawn
1125 Handle(Geom_Curve) aCurve;
1126 Standard_Real aMidV = ( aMinV + aMaxV ) / 2.5;
1127 aCurve = aSurf->VIso (aMidV);
1128 aCircle = Handle(Geom_Circle)::DownCast (aCurve)->Circ();
1129
1130 aCurve = aSurf->VIso(aMaxV);
1131 gp_Circ aCircVmax = Handle(Geom_Circle)::DownCast(aCurve)->Circ();
1132 aCurve = aSurf->VIso(aMinV);
1133 gp_Circ aCircVmin = Handle(Geom_Circle)::DownCast(aCurve)->Circ();
1134
1135 if (aCircVmax.Radius() < aCircVmin.Radius())
1136 {
1137 gp_Circ aTmpCirc = aCircVmax;
1138 aCircVmax = aCircVmin;
1139 aCircVmin = aTmpCirc;
1140 }
1141
1142 myFirstPoint = ElCLib::Value (0, aCircle);
1143 mySecondPoint = ElCLib::Value (M_PI, aCircle);
1144 return Standard_True;
1145}
1146
1147//=======================================================================
1148//function : IsValidPoints
1149//purpose :
1150//=======================================================================
1151Standard_Boolean AIS_AngleDimension::IsValidPoints (const gp_Pnt& theFirstPoint,
1152 const gp_Pnt& theCenterPoint,
1153 const gp_Pnt& theSecondPoint) const
1154{
1155 return theFirstPoint.Distance (theCenterPoint) > Precision::Confusion()
1156 && theSecondPoint.Distance (theCenterPoint) > Precision::Confusion()
1157 && gp_Vec (theCenterPoint, theFirstPoint).Angle (
1158 gp_Vec (theCenterPoint, theSecondPoint)) > Precision::Angular();
d7bffd44 1159}
af203d54 1160
1161//=======================================================================
1162//function : GetTextPosition
1163//purpose :
1164//=======================================================================
1165const gp_Pnt AIS_AngleDimension::GetTextPosition() const
1166{
1167 if (!IsValid())
1168 {
1169 return gp::Origin();
1170 }
1171
1172 if (IsTextPositionCustom())
1173 {
1174 return myFixedTextPosition;
1175 }
1176
1177 // Counts text position according to the dimension parameters
1178 gp_Pnt aTextPosition (gp::Origin());
1179
1180 Handle(Prs3d_DimensionAspect) aDimensionAspect = myDrawer->DimensionAspect();
1181
1182 // Prepare label string and compute its geometrical width
1183 Standard_Real aLabelWidth;
1184 TCollection_ExtendedString aLabelString = GetValueString (aLabelWidth);
1185
1186 gp_Pnt aFirstAttach = myCenterPoint.Translated (gp_Vec(myCenterPoint, myFirstPoint).Normalized() * GetFlyout());
1187 gp_Pnt aSecondAttach = myCenterPoint.Translated (gp_Vec(myCenterPoint, mySecondPoint).Normalized() * GetFlyout());
1188
1189 // Handle user-defined and automatic arrow placement
1190 Standard_Boolean isArrowsExternal = Standard_False;
1191 Standard_Integer aLabelPosition = LabelPosition_None;
1192 FitTextAlignment (aDimensionAspect->TextHorizontalPosition(),
1193 aLabelPosition, isArrowsExternal);
1194
1195 // Get text position
1196 switch (aLabelPosition & LabelPosition_HMask)
1197 {
1198 case LabelPosition_HCenter:
1199 {
1200 aTextPosition = GetCenterOnArc (aFirstAttach, aSecondAttach, myCenterPoint);
1201 }
1202 break;
1203 case LabelPosition_Left:
1204 {
1205 gp_Dir aPlaneNormal = gp_Vec (aFirstAttach, aSecondAttach) ^ gp_Vec (myCenterPoint, aFirstAttach);
1206 gp_Dir anExtensionDir = aPlaneNormal ^ gp_Vec (myCenterPoint, aFirstAttach);
1207 Standard_Real anExtensionSize = aDimensionAspect->ExtensionSize();
1208 Standard_Real anOffset = isArrowsExternal
1209 ? anExtensionSize + aDimensionAspect->ArrowAspect()->Length()
1210 : anExtensionSize;
1211 gp_Vec anExtensionVec = gp_Vec (anExtensionDir) * -anOffset;
1212 aTextPosition = aFirstAttach.Translated (anExtensionVec);
1213 }
1214 break;
1215 case LabelPosition_Right:
1216 {
1217 gp_Dir aPlaneNormal = gp_Vec (aFirstAttach, aSecondAttach) ^ gp_Vec (myCenterPoint, aFirstAttach);
1218 gp_Dir anExtensionDir = aPlaneNormal ^ gp_Vec (myCenterPoint, aSecondAttach);
1219 Standard_Real anExtensionSize = aDimensionAspect->ExtensionSize();
1220 Standard_Real anOffset = isArrowsExternal
1221 ? anExtensionSize + aDimensionAspect->ArrowAspect()->Length()
1222 : anExtensionSize;
1223 gp_Vec anExtensionVec = gp_Vec (anExtensionDir) * anOffset;
1224 aTextPosition = aSecondAttach.Translated (anExtensionVec);
1225 }
1226 break;
1227 }
1228
1229 return aTextPosition;
1230}
1231
1232//=======================================================================
1233//function : SetTextPosition
1234//purpose :
1235//=======================================================================
1236void AIS_AngleDimension::SetTextPosition (const gp_Pnt& theTextPos)
1237{
1238 if (!IsValid())
1239 {
1240 return;
1241 }
1242
1243 // The text position point for angle dimension should belong to the working plane.
1244 if (!GetPlane().Contains (theTextPos, Precision::Confusion()))
1245 {
1246 Standard_ProgramError::Raise ("The text position point for angle dimension doesn't belong to the working plane.");
1247 }
1248
1249 myIsTextPositionFixed = Standard_True;
1250 myFixedTextPosition = theTextPos;
1251}
1252
1253//=======================================================================
91b16a64 1254//function : AdjustParameters
af203d54 1255//purpose :
1256//=======================================================================
1257void AIS_AngleDimension::AdjustParameters (const gp_Pnt& theTextPos,
1258 Standard_Real& theExtensionSize,
91b16a64 1259 Prs3d_DimensionTextHorizontalPosition& theAlignment,
1260 Standard_Real& theFlyout) const
af203d54 1261{
1262 Handle(Prs3d_DimensionAspect) aDimensionAspect = myDrawer->DimensionAspect();
1263 Standard_Real anArrowLength = aDimensionAspect->ArrowAspect()->Length();
1264
af203d54 1265 // Build circle with radius that is equal to distance from text position to the center point.
1266 Standard_Real aRadius = gp_Vec (myCenterPoint, theTextPos).Magnitude();
1267
1268 // Set attach points in positive direction of the flyout.
1269 gp_Pnt aFirstAttach = myCenterPoint.Translated (gp_Vec (myCenterPoint, myFirstPoint).Normalized() * aRadius);
1270 gp_Pnt aSecondAttach = myCenterPoint.Translated (gp_Vec (myCenterPoint, mySecondPoint).Normalized() * aRadius);
1271
1272 gce_MakeCirc aConstructCircle (myCenterPoint, GetPlane(), aRadius);
1273 if (!aConstructCircle.IsDone())
1274 {
1275 return;
1276 }
1277 gp_Circ aCircle = aConstructCircle.Value();
1278
1279 // Default values
1280 theExtensionSize = aDimensionAspect->ArrowAspect()->Length();
1281 theAlignment = Prs3d_DTHP_Center;
1282
1283 Standard_Real aParamBeg = ElCLib::Parameter (aCircle, aFirstAttach);
1284 Standard_Real aParamEnd = ElCLib::Parameter (aCircle, aSecondAttach);
1285 if (aParamEnd < aParamBeg)
1286 {
1287 Standard_Real aParam = aParamEnd;
1288 aParamEnd = aParamBeg;
1289 aParamBeg = aParam;
1290 }
1291
1292 ElCLib::AdjustPeriodic (0.0, M_PI * 2, Precision::PConfusion(), aParamBeg, aParamEnd);
1293 Standard_Real aTextPar = ElCLib::Parameter (aCircle , theTextPos);
1294
1295 // Horizontal center
1296 if (aTextPar > aParamBeg && aTextPar < aParamEnd)
1297 {
91b16a64 1298 theFlyout = aRadius;
af203d54 1299 return;
1300 }
1301
1302 aParamBeg += M_PI;
1303 aParamEnd += M_PI;
1304 ElCLib::AdjustPeriodic (0.0, M_PI * 2, Precision::PConfusion(), aParamBeg, aParamEnd);
1305
1306 if (aTextPar > aParamBeg && aTextPar < aParamEnd)
1307 {
91b16a64 1308 theFlyout = -aRadius;
af203d54 1309 return;
1310 }
1311
1312 // Text on the extensions
1313 gp_Lin aFirstLine = gce_MakeLin (myCenterPoint, myFirstPoint);
1314 gp_Lin aSecondLine = gce_MakeLin (myCenterPoint, mySecondPoint);
1315 gp_Pnt aFirstTextProj = AIS::Nearest (aFirstLine, theTextPos);
1316 gp_Pnt aSecondTextProj = AIS::Nearest (aSecondLine, theTextPos);
1317 Standard_Real aFirstDist = aFirstTextProj.Distance (theTextPos);
1318 Standard_Real aSecondDist = aSecondTextProj.Distance (theTextPos);
1319
1320 if (aFirstDist <= aSecondDist)
1321 {
1322 aRadius = myCenterPoint.Distance (aFirstTextProj);
1323 Standard_Real aNewExtensionSize = aFirstDist - anArrowLength;
1324 theExtensionSize = aNewExtensionSize < 0.0 ? 0.0 : aNewExtensionSize;
1325
1326 theAlignment = Prs3d_DTHP_Left;
1327
1328 gp_Vec aPosFlyoutDir = gp_Vec (myCenterPoint, myFirstPoint).Normalized().Scaled (aRadius);
1329
91b16a64 1330 theFlyout = aFirstTextProj.Distance (myCenterPoint.Translated (aPosFlyoutDir)) > Precision::Confusion()
af203d54 1331 ? -aRadius : aRadius;
1332 }
1333 else
1334 {
1335 aRadius = myCenterPoint.Distance (aSecondTextProj);
1336
1337 Standard_Real aNewExtensionSize = aSecondDist - anArrowLength;
1338
1339 theExtensionSize = aNewExtensionSize < 0.0 ? 0.0 : aNewExtensionSize;
1340
1341 theAlignment = Prs3d_DTHP_Right;
1342
1343 gp_Vec aPosFlyoutDir = gp_Vec (myCenterPoint, mySecondPoint).Normalized().Scaled (aRadius);
1344
91b16a64 1345 theFlyout = aSecondTextProj.Distance (myCenterPoint.Translated (aPosFlyoutDir)) > Precision::Confusion()
af203d54 1346 ? -aRadius : aRadius;
1347 }
1348}
1349
1350//=======================================================================
1351//function : FitTextAlignment
1352//purpose :
1353//=======================================================================
1354void AIS_AngleDimension::FitTextAlignment (const Prs3d_DimensionTextHorizontalPosition& theHorizontalTextPos,
1355 Standard_Integer& theLabelPosition,
1356 Standard_Boolean& theIsArrowsExternal) const
1357{
1358 Handle(Prs3d_DimensionAspect) aDimensionAspect = myDrawer->DimensionAspect();
1359
1360 Quantity_Length anArrowLength = aDimensionAspect->ArrowAspect()->Length();
1361
1362 // Prepare label string and compute its geometrical width
1363 Standard_Real aLabelWidth;
1364 TCollection_ExtendedString aLabelString = GetValueString (aLabelWidth);
1365
1366 // add margins to label width
1367 if (aDimensionAspect->IsText3d())
1368 {
1369 aLabelWidth += aDimensionAspect->TextAspect()->Height() * THE_3D_TEXT_MARGIN * 2.0;
1370 }
1371
1372 gp_Pnt aFirstAttach = myCenterPoint.Translated (gp_Vec (myCenterPoint, myFirstPoint).Normalized() * GetFlyout());
1373 gp_Pnt aSecondAttach = myCenterPoint.Translated (gp_Vec (myCenterPoint, mySecondPoint).Normalized() * GetFlyout());
1374
1375 // Handle user-defined and automatic arrow placement
1376 switch (aDimensionAspect->ArrowOrientation())
1377 {
1378 case Prs3d_DAO_External: theIsArrowsExternal = true; break;
1379 case Prs3d_DAO_Internal: theIsArrowsExternal = false; break;
1380 case Prs3d_DAO_Fit:
1381 {
1382 gp_Vec anAttachVector (aFirstAttach, aSecondAttach);
1383 Standard_Real aDimensionWidth = anAttachVector.Magnitude();
1384
1385 // Add margin to ensure a small tail between text and arrow
1386 Standard_Real anArrowMargin = aDimensionAspect->IsText3d()
1387 ? aDimensionAspect->TextAspect()->Height() * THE_3D_TEXT_MARGIN
1388 : 0.0;
1389
1390 Standard_Real anArrowsWidth = (anArrowLength + anArrowMargin) * 2.0;
1391
1392 theIsArrowsExternal = aDimensionWidth < aLabelWidth + anArrowsWidth;
1393 break;
1394 }
1395 }
1396
1397 // Handle user-defined and automatic text placement
1398 switch (theHorizontalTextPos)
1399 {
1400 case Prs3d_DTHP_Left : theLabelPosition |= LabelPosition_Left; break;
1401 case Prs3d_DTHP_Right : theLabelPosition |= LabelPosition_Right; break;
1402 case Prs3d_DTHP_Center: theLabelPosition |= LabelPosition_HCenter; break;
1403 case Prs3d_DTHP_Fit:
1404 {
1405 gp_Vec anAttachVector (aFirstAttach, aSecondAttach);
1406 Standard_Real aDimensionWidth = anAttachVector.Magnitude();
1407 Standard_Real anArrowsWidth = anArrowLength * 2.0;
1408 Standard_Real aContentWidth = theIsArrowsExternal ? aLabelWidth : aLabelWidth + anArrowsWidth;
1409
1410 theLabelPosition |= aDimensionWidth < aContentWidth ? LabelPosition_Left : LabelPosition_HCenter;
1411 break;
1412 }
1413 }
1414
1415 switch (aDimensionAspect->TextVerticalPosition())
1416 {
1417 case Prs3d_DTVP_Above : theLabelPosition |= LabelPosition_Above; break;
1418 case Prs3d_DTVP_Below : theLabelPosition |= LabelPosition_Below; break;
1419 case Prs3d_DTVP_Center : theLabelPosition |= LabelPosition_VCenter; break;
1420 }
1421}