0026586: Eliminate compile warnings obtained by building occt with vc14: declaration...
[occt.git] / src / AIS / AIS_Dimension.cxx
CommitLineData
60bf98ae 1// Created on: 2013-11-11
2// Created by: Anastasia BORISOVA
d5f74e42 3// Copyright (c) 2013-2014 OPEN CASCADE SAS
a6eb515f 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
a6eb515f 6//
d5f74e42 7// This library is free software; you can redistribute it and/or modify it under
8// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 9// by the Free Software Foundation, with special exception defined in the file
10// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11// distribution for complete text of the license and disclaimer of any warranty.
a6eb515f 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
a6eb515f 15
a6eb515f 16#include <AIS_Dimension.hxx>
d7bffd44 17
18#include <AIS.hxx>
a6eb515f 19#include <AIS_DimensionOwner.hxx>
a6eb515f 20#include <Adaptor3d_HCurve.hxx>
21#include <BRepAdaptor_Curve.hxx>
22#include <BRepAdaptor_Surface.hxx>
a6eb515f 23#include <BRepBndLib.hxx>
60bf98ae 24#include <Bnd_Box.hxx>
a6eb515f 25#include <ElCLib.hxx>
26#include <Font_BRepFont.hxx>
ac84fcf6 27#include <Font_BRepTextBuilder.hxx>
a6eb515f 28#include <GC_MakeCircle.hxx>
af203d54 29#include <Geom_Line.hxx>
fe83e1ea 30#include <GeomAdaptor_Curve.hxx>
a6eb515f 31#include <Geom_Circle.hxx>
a6eb515f 32#include <Geom_TrimmedCurve.hxx>
33#include <gce_MakeDir.hxx>
34#include <gce_MakeLin.hxx>
af203d54 35#include <gce_MakePln.hxx>
a6eb515f 36#include <Graphic3d_ArrayOfSegments.hxx>
37#include <Graphic3d_ArrayOfTriangles.hxx>
38#include <Graphic3d_AspectLine3d.hxx>
39#include <Graphic3d_AspectFillArea3d.hxx>
40#include <Graphic3d_AspectText3d.hxx>
41#include <Graphic3d_Group.hxx>
42#include <PrsMgr_PresentationManager3d.hxx>
43#include <Prs3d_Arrow.hxx>
44#include <Prs3d_ArrowAspect.hxx>
45#include <Prs3d_Drawer.hxx>
46#include <Prs3d_LineAspect.hxx>
47#include <Prs3d_Presentation.hxx>
48#include <Prs3d_Root.hxx>
49#include <Prs3d_ShadingAspect.hxx>
50#include <Prs3d_Text.hxx>
51#include <SelectMgr_EntityOwner.hxx>
52#include <SelectMgr_Selection.hxx>
53#include <SelectMgr_SequenceOfOwner.hxx>
a6eb515f 54#include <Select3D_SensitiveCircle.hxx>
55#include <Select3D_SensitiveGroup.hxx>
fe83e1ea 56#include <Select3D_SensitiveCurve.hxx>
a6eb515f 57#include <Select3D_SensitiveSegment.hxx>
fe83e1ea 58#include <Select3D_SensitiveTriangle.hxx>
51f8f60c 59#include <Select3D_SensitiveTriangulation.hxx>
60#include <Poly_Array1OfTriangle.hxx>
61#include <Poly_Triangulation.hxx>
a6eb515f 62#include <Standard_CString.hxx>
af203d54 63#include <Standard_ProgramError.hxx>
a6eb515f 64#include <StdPrs_ShadedShape.hxx>
65#include <StdPrs_WFShape.hxx>
66#include <TCollection_AsciiString.hxx>
67#include <TCollection_ExtendedString.hxx>
68#include <TopExp_Explorer.hxx>
69#include <TopoDS.hxx>
0499eb06 70#include <TopoDS_Edge.hxx>
a6eb515f 71#include <TopoDS_Vertex.hxx>
72#include <Units.hxx>
73#include <Units_UnitsDictionary.hxx>
74#include <UnitsAPI.hxx>
75#include <UnitsAPI_SystemUnits.hxx>
76
a6eb515f 77
d7bffd44 78namespace
a6eb515f 79{
fe83e1ea 80 // default text strings
d7bffd44 81 static const TCollection_ExtendedString THE_EMPTY_LABEL;
60bf98ae 82 static const TCollection_AsciiString THE_UNDEFINED_UNITS;
fe83e1ea 83
84 // default text margin and resolution
85 static const Standard_Real THE_3D_TEXT_MARGIN = 0.1;
86 static const unsigned int THE_2D_TEXT_RESOLUTION = 72;
87
88 // default selection priorities
89 static const Standard_Integer THE_NEUTRAL_SEL_PRIORITY = 5;
90 static const Standard_Integer THE_LOCAL_SEL_PRIORITY = 6;
d7bffd44 91};
a6eb515f 92
93//=======================================================================
94//function : Constructor
95//purpose :
96//=======================================================================
60bf98ae 97AIS_Dimension::AIS_Dimension (const AIS_KindOfDimension theType)
cbff1e55 98: AIS_InteractiveObject (),
99 mySelToleranceForText2d(0.0),
100 myCustomValue (0.0),
101 myIsValueCustom (Standard_False),
102 myIsTextPositionFixed (Standard_False),
103 mySpecialSymbol (' '),
a6eb515f 104 myDisplaySpecialSymbol (AIS_DSS_No),
cbff1e55 105 myGeometryType (GeometryType_UndefShapes),
106 myIsPlaneCustom (Standard_False),
107 myFlyout (0.0),
108 myIsGeometryValid (Standard_False),
109 myKindOfDimension (theType)
a6eb515f 110{
a6eb515f 111}
112
113//=======================================================================
60bf98ae 114//function : SetCustomValue
115//purpose :
a6eb515f 116//=======================================================================
60bf98ae 117void AIS_Dimension::SetCustomValue (const Standard_Real theValue)
a6eb515f 118{
60bf98ae 119 if (myIsValueCustom && myCustomValue == theValue)
120 {
121 return;
122 }
a6eb515f 123
60bf98ae 124 myIsValueCustom = Standard_True;
125
126 myCustomValue = theValue;
127
128 SetToUpdate();
a6eb515f 129}
130
131//=======================================================================
60bf98ae 132//function : GetPlane
a6eb515f 133//purpose :
134//=======================================================================
60bf98ae 135const gp_Pln& AIS_Dimension::GetPlane() const
a6eb515f 136{
60bf98ae 137 return myPlane;
a6eb515f 138}
139
140//=======================================================================
60bf98ae 141//function : GetGeometryType
a6eb515f 142//purpose :
143//=======================================================================
60bf98ae 144const Standard_Integer AIS_Dimension::GetGeometryType () const
a6eb515f 145{
60bf98ae 146 return myGeometryType;
a6eb515f 147}
148
149//=======================================================================
60bf98ae 150//function : SetUserPlane
a6eb515f 151//purpose :
152//=======================================================================
60bf98ae 153void AIS_Dimension::SetCustomPlane (const gp_Pln& thePlane)
a6eb515f 154{
60bf98ae 155 myPlane = thePlane;
156 myIsPlaneCustom = Standard_True;
157
91b16a64 158 // Disable fixed (custom) text position
af203d54 159 UnsetFixedTextPosition();
160
60bf98ae 161 // Check validity if geometry has been set already.
91b16a64 162 if (IsValid())
60bf98ae 163 {
60bf98ae 164 SetToUpdate();
165 }
a6eb515f 166}
167
168//=======================================================================
60bf98ae 169//function : SetDimensionAspect
170//purpose :
a6eb515f 171//=======================================================================
60bf98ae 172void AIS_Dimension::SetDimensionAspect (const Handle(Prs3d_DimensionAspect)& theDimensionAspect)
a6eb515f 173{
60bf98ae 174 myDrawer->SetDimensionAspect (theDimensionAspect);
175
176 SetToUpdate();
a6eb515f 177}
178
179//=======================================================================
60bf98ae 180//function : SetDisplaySpecialSymbol
181//purpose :
a6eb515f 182//=======================================================================
60bf98ae 183void AIS_Dimension::SetDisplaySpecialSymbol (const AIS_DisplaySpecialSymbol theDisplaySpecSymbol)
a6eb515f 184{
60bf98ae 185 if (myDisplaySpecialSymbol == theDisplaySpecSymbol)
186 {
187 return;
188 }
189
190 myDisplaySpecialSymbol = theDisplaySpecSymbol;
191
192 SetToUpdate();
a6eb515f 193}
194
195//=======================================================================
60bf98ae 196//function : SetSpecialSymbol
a6eb515f 197//purpose :
198//=======================================================================
60bf98ae 199void AIS_Dimension::SetSpecialSymbol (const Standard_ExtCharacter theSpecialSymbol)
a6eb515f 200{
60bf98ae 201 if (mySpecialSymbol == theSpecialSymbol)
202 {
203 return;
204 }
205
206 mySpecialSymbol = theSpecialSymbol;
207
208 SetToUpdate();
a6eb515f 209}
210
211//=======================================================================
60bf98ae 212//function : SetSelToleranceForText2d
213//purpose :
a6eb515f 214//=======================================================================
60bf98ae 215void AIS_Dimension::SetSelToleranceForText2d (const Standard_Real theTol)
a6eb515f 216{
60bf98ae 217 if (mySelToleranceForText2d == theTol)
218 {
219 return;
220 }
221
222 mySelToleranceForText2d = theTol;
223
224 SetToUpdate();
a6eb515f 225}
226
227//=======================================================================
60bf98ae 228//function : SetFlyout
229//purpose :
a6eb515f 230//=======================================================================
60bf98ae 231void AIS_Dimension::SetFlyout (const Standard_Real theFlyout)
a6eb515f 232{
60bf98ae 233 if (myFlyout == theFlyout)
234 {
235 return;
236 }
a6eb515f 237
60bf98ae 238 myFlyout = theFlyout;
239
af203d54 240 // Disable fixed text position
241 UnsetFixedTextPosition();
242
60bf98ae 243 SetToUpdate();
244}
a6eb515f 245
246//=======================================================================
60bf98ae 247//function : GetDisplayUnits
248//purpose :
a6eb515f 249//=======================================================================
60bf98ae 250const TCollection_AsciiString& AIS_Dimension::GetDisplayUnits() const
a6eb515f 251{
60bf98ae 252 return THE_UNDEFINED_UNITS;
a6eb515f 253}
254
255//=======================================================================
60bf98ae 256//function : GetModelUnits
257//purpose :
a6eb515f 258//=======================================================================
60bf98ae 259const TCollection_AsciiString& AIS_Dimension::GetModelUnits() const
a6eb515f 260{
60bf98ae 261 return THE_UNDEFINED_UNITS;
a6eb515f 262}
263
264//=======================================================================
60bf98ae 265//function : ValueToDisplayUnits
266//purpose :
a6eb515f 267//=======================================================================
60bf98ae 268Standard_Real AIS_Dimension::ValueToDisplayUnits() const
a6eb515f 269{
60bf98ae 270 return UnitsAPI::AnyToAny (GetValue(),
271 GetModelUnits().ToCString(),
272 GetDisplayUnits().ToCString());
a6eb515f 273}
274
275//=======================================================================
60bf98ae 276//function : GetValueString
a6eb515f 277//purpose :
278//=======================================================================
60bf98ae 279TCollection_ExtendedString AIS_Dimension::GetValueString (Standard_Real& theWidth) const
a6eb515f 280{
60bf98ae 281 // format value string using "sprintf"
282 TCollection_AsciiString aFormatStr = myDrawer->DimensionAspect()->ValueStringFormat();
a6eb515f 283
60bf98ae 284 char aFmtBuffer[256];
285 sprintf (aFmtBuffer, aFormatStr.ToCString(), ValueToDisplayUnits());
286 TCollection_ExtendedString aValueStr = TCollection_ExtendedString (aFmtBuffer);
a6eb515f 287
60bf98ae 288 // add units to values string
289 if (myDrawer->DimensionAspect()->IsUnitsDisplayed())
a6eb515f 290 {
60bf98ae 291 aValueStr += " ";
292 aValueStr += TCollection_ExtendedString (GetDisplayUnits());
a6eb515f 293 }
60bf98ae 294
295 switch (myDisplaySpecialSymbol)
a6eb515f 296 {
60bf98ae 297 case AIS_DSS_Before : aValueStr.Insert (1, mySpecialSymbol); break;
298 case AIS_DSS_After : aValueStr.Insert (aValueStr.Length() + 1, mySpecialSymbol); break;
299 case AIS_DSS_No : break;
a6eb515f 300 }
301
d7bffd44 302 // Get text style parameters
a6eb515f 303 Quantity_Color aColor;
d7bffd44 304 Standard_CString aFontName;
a6eb515f 305 Standard_Real aFactor;
306 Standard_Real aSpace;
d7bffd44 307 myDrawer->DimensionAspect()->TextAspect()->Aspect()->Values (aColor, aFontName, aFactor, aSpace);
fe83e1ea 308 Font_FontAspect aFontAspect = myDrawer->DimensionAspect()->TextAspect()->Aspect()->GetTextFontAspect();
309 Standard_Real aFontHeight = myDrawer->DimensionAspect()->TextAspect()->Height();
310
60bf98ae 311 NCollection_Utf8String anUTFString = (Standard_Utf16Char* )aValueStr.ToExtString();
fe83e1ea 312
d7bffd44 313 theWidth = 0.0;
fe83e1ea 314
315 if (myDrawer->DimensionAspect()->IsText3d())
316 {
317 // text width produced by BRepFont
318 Font_BRepFont aFont (aFontName, aFontAspect, aFontHeight);
319
320 for (NCollection_Utf8Iter anIter = anUTFString.Iterator(); *anIter != 0; )
321 {
322 Standard_Utf32Char aCurrChar = *anIter;
323 Standard_Utf32Char aNextChar = *(++anIter);
324 theWidth += aFont.AdvanceX (aCurrChar, aNextChar);
325 }
326 }
327 else
d7bffd44 328 {
fe83e1ea 329 // Text width for 1:1 scale 2D case
60bf98ae 330 Handle(Font_FTFont) aFont = new Font_FTFont();
fe83e1ea 331 aFont->Init (aFontName, aFontAspect, (const unsigned int)aFontHeight, THE_2D_TEXT_RESOLUTION);
332
333 for (NCollection_Utf8Iter anIter = anUTFString.Iterator(); *anIter != 0; )
334 {
335 Standard_Utf32Char aCurrChar = *anIter;
336 Standard_Utf32Char aNextChar = *(++anIter);
337 theWidth += (Standard_Real) aFont->AdvanceX (aCurrChar, aNextChar);
338 }
d7bffd44 339 }
60bf98ae 340
341 return aValueStr;
a6eb515f 342}
343
344//=======================================================================
60bf98ae 345//function : DrawArrow
a6eb515f 346//purpose :
347//=======================================================================
60bf98ae 348void AIS_Dimension::DrawArrow (const Handle(Prs3d_Presentation)& thePresentation,
a6eb515f 349 const gp_Pnt& theLocation,
350 const gp_Dir& theDirection)
351{
352 Prs3d_Root::NewGroup (thePresentation);
fe83e1ea 353
354 Quantity_Length aLength = myDrawer->DimensionAspect()->ArrowAspect()->Length();
355 Standard_Real anAngle = myDrawer->DimensionAspect()->ArrowAspect()->Angle();
a6eb515f 356
357 if (myDrawer->DimensionAspect()->IsArrows3d())
358 {
359 Prs3d_Arrow::Draw (thePresentation,
360 theLocation,
d7bffd44 361 theDirection,
fe83e1ea 362 anAngle,
363 aLength);
a6eb515f 364 }
365 else
366 {
fe83e1ea 367 gp_Pnt aLeftPoint (gp::Origin());
368 gp_Pnt aRightPoint (gp::Origin());
60bf98ae 369 const gp_Dir& aPlane = GetPlane().Axis().Direction();
fe83e1ea 370
371 PointsForArrow (theLocation, theDirection, aPlane, aLength, anAngle, aLeftPoint, aRightPoint);
372
a6eb515f 373 Handle(Graphic3d_ArrayOfTriangles) anArrow = new Graphic3d_ArrayOfTriangles(3);
a6eb515f 374
375 anArrow->AddVertex (aLeftPoint);
376 anArrow->AddVertex (theLocation);
377 anArrow->AddVertex (aRightPoint);
378
379 // Set aspect for arrow triangles
380 Quantity_Color aColor;
381 Aspect_TypeOfLine aTOL;
382 Standard_Real aWidth;
383 myDrawer->DimensionAspect()->ArrowAspect()->Aspect()->Values (aColor, aTOL, aWidth);
384 Graphic3d_MaterialAspect aShadeMat (Graphic3d_NOM_DEFAULT);
385 aShadeMat.SetReflectionModeOff (Graphic3d_TOR_AMBIENT);
386 aShadeMat.SetReflectionModeOff (Graphic3d_TOR_DIFFUSE);
387 aShadeMat.SetReflectionModeOff (Graphic3d_TOR_SPECULAR);
d7bffd44 388
389 Handle(Prs3d_ShadingAspect) aShadingStyle = new Prs3d_ShadingAspect();
390 aShadingStyle->SetColor (aColor);
391 aShadingStyle->SetMaterial (aShadeMat);
392
393 Prs3d_Root::CurrentGroup (thePresentation)->SetPrimitivesAspect (aShadingStyle->Aspect());
394 Prs3d_Root::CurrentGroup (thePresentation)->AddPrimitiveArray (anArrow);
a6eb515f 395 }
fe83e1ea 396
397 SelectionGeometry::Arrow& aSensitiveArrow = mySelectionGeom.NewArrow();
398 aSensitiveArrow.Position = theLocation;
399 aSensitiveArrow.Direction = theDirection;
a6eb515f 400}
401
402//=======================================================================
60bf98ae 403//function : DrawText
a6eb515f 404//purpose :
405//=======================================================================
60bf98ae 406void AIS_Dimension::DrawText (const Handle(Prs3d_Presentation)& thePresentation,
fe83e1ea 407 const gp_Pnt& theTextPos,
408 const gp_Dir& theTextDir,
409 const TCollection_ExtendedString& theText,
410 const Standard_Integer theLabelPosition)
a6eb515f 411{
a6eb515f 412 if (myDrawer->DimensionAspect()->IsText3d())
413 {
fe83e1ea 414 // getting font parameters
a6eb515f 415 Quantity_Color aColor;
416 Standard_CString aFontName;
417 Standard_Real anExpansionFactor;
418 Standard_Real aSpace;
419 myDrawer->DimensionAspect()->TextAspect()->Aspect()->Values (aColor, aFontName, anExpansionFactor, aSpace);
420 Font_FontAspect aFontAspect = myDrawer->DimensionAspect()->TextAspect()->Aspect()->GetTextFontAspect();
fe83e1ea 421 Standard_Real aFontHeight = myDrawer->DimensionAspect()->TextAspect()->Height();
a6eb515f 422
fe83e1ea 423 // creating TopoDS_Shape for text
424 Font_BRepFont aFont (aFontName, aFontAspect, aFontHeight);
425 NCollection_Utf8String anUTFString = (Standard_Utf16Char* )theText.ToExtString();
ac84fcf6 426
427 Font_BRepTextBuilder aBuilder;
428 TopoDS_Shape aTextShape = aBuilder.Perform (aFont, anUTFString);
a6eb515f 429
fe83e1ea 430 // compute text width with kerning
431 Standard_Real aTextWidth = 0.0;
432 Standard_Real aTextHeight = aFont.Ascender() + aFont.Descender();
d7bffd44 433
fe83e1ea 434 for (NCollection_Utf8Iter anIter = anUTFString.Iterator(); *anIter != 0; )
435 {
436 Standard_Utf32Char aCurrChar = *anIter;
437 Standard_Utf32Char aNextChar = *(++anIter);
438 aTextWidth += aFont.AdvanceX (aCurrChar, aNextChar);
439 }
440
441 // formating text position in XOY plane
d7bffd44 442 Standard_Integer aHLabelPos = theLabelPosition & LabelPosition_HMask;
443 Standard_Integer aVLabelPos = theLabelPosition & LabelPosition_VMask;
444
445 gp_Dir aTextDir (aHLabelPos == LabelPosition_Left ? -theTextDir : theTextDir);
446
447 // compute label offsets
fe83e1ea 448 Standard_Real aMarginSize = aFontHeight * THE_3D_TEXT_MARGIN;
d7bffd44 449 Standard_Real aCenterHOffset = 0.0;
450 Standard_Real aCenterVOffset = 0.0;
451 switch (aHLabelPos)
a6eb515f 452 {
d7bffd44 453 case LabelPosition_HCenter : aCenterHOffset = 0.0; break;
454 case LabelPosition_Right : aCenterHOffset = aTextWidth / 2.0 + aMarginSize; break;
455 case LabelPosition_Left : aCenterHOffset = -aTextWidth / 2.0 - aMarginSize; break;
a6eb515f 456 }
d7bffd44 457 switch (aVLabelPos)
a6eb515f 458 {
d7bffd44 459 case LabelPosition_VCenter : aCenterVOffset = 0.0; break;
460 case LabelPosition_Above : aCenterVOffset = aTextHeight / 2.0 + aMarginSize; break;
461 case LabelPosition_Below : aCenterVOffset = -aTextHeight / 2.0 - aMarginSize; break;
a6eb515f 462 }
d7bffd44 463
464 // compute shape offset transformation
fe83e1ea 465 Standard_Real aShapeHOffset = aCenterHOffset - aTextWidth / 2.0;
d7bffd44 466 Standard_Real aShapeVOffset = aCenterVOffset - aTextHeight / 2.0;
467
fe83e1ea 468 // center shape in its bounding box (suppress border spacing added by FT_Font)
469 Bnd_Box aShapeBnd;
470 BRepBndLib::AddClose (aTextShape, aShapeBnd);
471
472 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
473 aShapeBnd.Get (aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
474
475 Standard_Real aXalign = aTextWidth * 0.5 - (aXmax + aXmin) * 0.5;
476 Standard_Real aYalign = aTextHeight * 0.5 - (aYmax + aYmin) * 0.5;
477 aShapeHOffset += aXalign;
478 aShapeVOffset += aYalign;
479
d7bffd44 480 gp_Trsf anOffsetTrsf;
481 anOffsetTrsf.SetTranslation (gp::Origin(), gp_Pnt (aShapeHOffset, aShapeVOffset, 0.0));
482 aTextShape.Move (anOffsetTrsf);
a6eb515f 483
fe83e1ea 484 // transform text to myWorkingPlane coordinate system
60bf98ae 485 gp_Ax3 aTextCoordSystem (theTextPos, GetPlane().Axis().Direction(), aTextDir);
d7bffd44 486 gp_Trsf aTextPlaneTrsf;
487 aTextPlaneTrsf.SetTransformation (aTextCoordSystem, gp_Ax3 (gp::XOY()));
488 aTextShape.Move (aTextPlaneTrsf);
a6eb515f 489
fe83e1ea 490 // set text flipping anchors
d7bffd44 491 gp_Trsf aCenterOffsetTrsf;
492 gp_Pnt aCenterOffset (aCenterHOffset, aCenterVOffset, 0.0);
493 aCenterOffsetTrsf.SetTranslation (gp::Origin(), aCenterOffset);
494
fe83e1ea 495 gp_Pnt aCenterOfLabel (gp::Origin());
496 aCenterOfLabel.Transform (aCenterOffsetTrsf);
497 aCenterOfLabel.Transform (aTextPlaneTrsf);
d7bffd44 498
60bf98ae 499 gp_Ax2 aFlippingAxes (aCenterOfLabel, GetPlane().Axis().Direction(), aTextDir);
d7bffd44 500 Prs3d_Root::CurrentGroup (thePresentation)->SetFlippingOptions (Standard_True, aFlippingAxes);
501
fe83e1ea 502 // draw text
a6eb515f 503 if (myDrawer->DimensionAspect()->IsTextShaded())
504 {
505 // Setting text shading and color parameters
506 Graphic3d_MaterialAspect aShadeMat (Graphic3d_NOM_DEFAULT);
507 aShadeMat.SetReflectionModeOff (Graphic3d_TOR_AMBIENT);
508 aShadeMat.SetReflectionModeOff (Graphic3d_TOR_DIFFUSE);
509 aShadeMat.SetReflectionModeOff (Graphic3d_TOR_SPECULAR);
510 myDrawer->ShadingAspect()->Aspect()->SetInteriorColor (aColor);
511 myDrawer->ShadingAspect()->Aspect()->SetBackInteriorColor (aColor);
512 myDrawer->ShadingAspect()->SetMaterial (aShadeMat);
513
fe83e1ea 514 // drawing text
a6eb515f 515 StdPrs_ShadedShape::Add (thePresentation, aTextShape, myDrawer);
516 }
517 else
518 {
fe83e1ea 519 // setting color for text
a6eb515f 520 myDrawer->FreeBoundaryAspect()->Aspect()->SetColor (aColor);
fe83e1ea 521 // drawing text
a6eb515f 522 StdPrs_WFShape::Add (thePresentation, aTextShape, myDrawer);
523 }
d7bffd44 524 Prs3d_Root::CurrentGroup (thePresentation)->SetFlippingOptions (Standard_False, gp_Ax2());
525
fe83e1ea 526 mySelectionGeom.TextPos = aCenterOfLabel;
527 mySelectionGeom.TextDir = aTextDir;
528 mySelectionGeom.TextWidth = aTextWidth + aMarginSize * 2.0;
529 mySelectionGeom.TextHeight = aTextHeight;
530
531 return;
a6eb515f 532 }
533
fe83e1ea 534 // generate primitives for 2D text
d7bffd44 535 myDrawer->DimensionAspect()->TextAspect()->Aspect()->SetDisplayType (Aspect_TODT_DIMENSION);
536
537 Prs3d_Text::Draw (thePresentation,
538 myDrawer->DimensionAspect()->TextAspect(),
539 theText,
fe83e1ea 540 theTextPos);
d7bffd44 541
fe83e1ea 542 mySelectionGeom.TextPos = theTextPos;
543 mySelectionGeom.TextDir = theTextDir;
544 mySelectionGeom.TextWidth = 0.0;
545 mySelectionGeom.TextHeight = 0.0;
a6eb515f 546}
547
d7bffd44 548//=======================================================================
60bf98ae 549//function : DrawExtension
a6eb515f 550//purpose :
551//=======================================================================
60bf98ae 552void AIS_Dimension::DrawExtension (const Handle(Prs3d_Presentation)& thePresentation,
d7bffd44 553 const Standard_Real theExtensionSize,
554 const gp_Pnt& theExtensionStart,
555 const gp_Dir& theExtensionDir,
fe83e1ea 556 const TCollection_ExtendedString& theLabelString,
557 const Standard_Real theLabelWidth,
558 const Standard_Integer theMode,
d7bffd44 559 const Standard_Integer theLabelPosition)
a6eb515f 560{
fe83e1ea 561 // reference line for extension starting at its connection point
562 gp_Lin anExtensionLine (theExtensionStart, theExtensionDir);
a6eb515f 563
fe83e1ea 564 Standard_Boolean hasLabel = theLabelString.Length() > 0;
fe83e1ea 565 if (hasLabel && (theMode == ComputeMode_All || theMode == ComputeMode_Text))
a6eb515f 566 {
d7bffd44 567 // compute text primitives; get its model width
fe83e1ea 568 gp_Pnt aTextPos = ElCLib::Value (theExtensionSize, anExtensionLine);
60bf98ae 569 gp_Dir aTextDir = theExtensionDir;
fe83e1ea 570
60bf98ae 571 DrawText (thePresentation,
fe83e1ea 572 aTextPos,
573 aTextDir,
574 theLabelString,
575 theLabelPosition);
a6eb515f 576 }
577
fe83e1ea 578 if (theMode != ComputeMode_All && theMode != ComputeMode_Line)
a6eb515f 579 {
d7bffd44 580 return;
a6eb515f 581 }
582
fe83e1ea 583 Standard_Boolean isShortLine = !myDrawer->DimensionAspect()->IsText3d()
584 || theLabelPosition & LabelPosition_VCenter;
585
d7bffd44 586 // compute graphical primitives and sensitives for extension line
587 gp_Pnt anExtStart = theExtensionStart;
fe83e1ea 588 gp_Pnt anExtEnd = !hasLabel || isShortLine
589 ? ElCLib::Value (theExtensionSize, anExtensionLine)
590 : ElCLib::Value (theExtensionSize + theLabelWidth, anExtensionLine);
d7bffd44 591
fe83e1ea 592 // add graphical primitives
d7bffd44 593 Handle(Graphic3d_ArrayOfSegments) anExtPrimitive = new Graphic3d_ArrayOfSegments (2);
594 anExtPrimitive->AddVertex (anExtStart);
595 anExtPrimitive->AddVertex (anExtEnd);
596
fe83e1ea 597 // add selection primitives
598 SelectionGeometry::Curve& aSensitiveCurve = mySelectionGeom.NewCurve();
599 aSensitiveCurve.Append (anExtStart);
600 aSensitiveCurve.Append (anExtEnd);
a6eb515f 601
fe83e1ea 602 if (!myDrawer->DimensionAspect()->IsText3d() && theMode == ComputeMode_All)
a6eb515f 603 {
d7bffd44 604 Prs3d_Root::CurrentGroup (thePresentation)->SetStencilTestOptions (Standard_True);
605 }
606 Handle(Graphic3d_AspectLine3d) aDimensionLineStyle = myDrawer->DimensionAspect()->LineAspect()->Aspect();
607 Prs3d_Root::CurrentGroup (thePresentation)->SetPrimitivesAspect (aDimensionLineStyle);
608 Prs3d_Root::CurrentGroup (thePresentation)->AddPrimitiveArray (anExtPrimitive);
fe83e1ea 609 if (!myDrawer->DimensionAspect()->IsText3d() && theMode == ComputeMode_All)
d7bffd44 610 {
611 Prs3d_Root::CurrentGroup (thePresentation)->SetStencilTestOptions (Standard_False);
a6eb515f 612 }
613}
614
615//=======================================================================
60bf98ae 616//function : DrawLinearDimension
a6eb515f 617//purpose :
618//=======================================================================
60bf98ae 619void AIS_Dimension::DrawLinearDimension (const Handle(Prs3d_Presentation)& thePresentation,
fe83e1ea 620 const Standard_Integer theMode,
60bf98ae 621 const gp_Pnt& theFirstPoint,
622 const gp_Pnt& theSecondPoint,
623 const Standard_Boolean theIsOneSide)
a6eb515f 624{
60bf98ae 625 // do not build any dimension for equal points
626 if (theFirstPoint.IsEqual (theSecondPoint, Precision::Confusion()))
a6eb515f 627 {
60bf98ae 628 Standard_ProgramError::Raise ("Can not build presentation for equal points.");
a6eb515f 629 }
d7bffd44 630
a6eb515f 631 Handle(Prs3d_DimensionAspect) aDimensionAspect = myDrawer->DimensionAspect();
a6eb515f 632
d7bffd44 633 // For extensions we need to know arrow size, text size and extension size: get it from aspect
634 Quantity_Length anArrowLength = aDimensionAspect->ArrowAspect()->Length();
635 Standard_Real anExtensionSize = aDimensionAspect->ExtensionSize();
fe83e1ea 636 // prepare label string and compute its geometrical width
637 Standard_Real aLabelWidth;
60bf98ae 638 TCollection_ExtendedString aLabelString = GetValueString (aLabelWidth);
a6eb515f 639
fe83e1ea 640 // add margins to cut dimension lines for 3d text
641 if (aDimensionAspect->IsText3d())
642 {
643 aLabelWidth += aDimensionAspect->TextAspect()->Height() * THE_3D_TEXT_MARGIN * 2.0;
644 }
645
646 // handle user-defined and automatic arrow placement
af203d54 647 Standard_Boolean isArrowsExternal = Standard_False;
648 Standard_Integer aLabelPosition = LabelPosition_None;
fe83e1ea 649
af203d54 650 Prs3d_DimensionTextHorizontalPosition aHorisontalTextPos = aDimensionAspect->TextHorizontalPosition();
651 if (IsTextPositionCustom())
652 {
91b16a64 653 if (!AdjustParametersForLinear (myFixedTextPosition, theFirstPoint, theSecondPoint,
654 anExtensionSize, aHorisontalTextPos, myFlyout, myPlane, myIsPlaneCustom))
655 {
656 Standard_ProgramError::Raise ("Can not adjust plane to the custom label position.");
657 }
a6eb515f 658 }
a6eb515f 659
af203d54 660 FitTextAlignmentForLinear (theFirstPoint, theSecondPoint, theIsOneSide, aHorisontalTextPos,
661 aLabelPosition, isArrowsExternal);
662
663 // compute dimension line points
664 gp_Ax1 aPlaneNormal = GetPlane().Axis();
665 gp_Dir aTargetPointsVector = gce_MakeDir (theFirstPoint, theSecondPoint);
666
667 // compute flyout direction vector
668 gp_Dir aFlyoutVector = aPlaneNormal.Direction() ^ aTargetPointsVector;
669
670 // create lines for layouts
671 gp_Lin aLine1 (theFirstPoint, aFlyoutVector);
672 gp_Lin aLine2 (theSecondPoint, aFlyoutVector);
673
674 // Get flyout end points
675 gp_Pnt aLineBegPoint = ElCLib::Value (ElCLib::Parameter (aLine1, theFirstPoint) + GetFlyout(), aLine1);
676 gp_Pnt aLineEndPoint = ElCLib::Value (ElCLib::Parameter (aLine2, theSecondPoint) + GetFlyout(), aLine2);
677
678 gp_Lin aDimensionLine = gce_MakeLin (aLineBegPoint, aLineEndPoint);
679
fe83e1ea 680 // compute arrows positions and directions
d7bffd44 681 gp_Dir aFirstArrowDir = aDimensionLine.Direction().Reversed();
682 gp_Dir aSecondArrowDir = aDimensionLine.Direction();
683 gp_Dir aFirstExtensionDir = aDimensionLine.Direction().Reversed();
684 gp_Dir aSecondExtensionDir = aDimensionLine.Direction();
685
686 gp_Pnt aFirstArrowBegin (0.0, 0.0, 0.0);
687 gp_Pnt aFirstArrowEnd (0.0, 0.0, 0.0);
688 gp_Pnt aSecondArrowBegin (0.0, 0.0, 0.0);
689 gp_Pnt aSecondArrowEnd (0.0, 0.0, 0.0);
a6eb515f 690
d7bffd44 691 if (isArrowsExternal)
a6eb515f 692 {
693 aFirstArrowDir.Reverse();
694 aSecondArrowDir.Reverse();
a6eb515f 695 }
d7bffd44 696
697 aFirstArrowBegin = aLineBegPoint;
698 aSecondArrowBegin = aLineEndPoint;
699 aFirstArrowEnd = aLineBegPoint.Translated (-gp_Vec (aFirstArrowDir).Scaled (anArrowLength));
700 aSecondArrowEnd = aLineEndPoint.Translated (-gp_Vec (aSecondArrowDir).Scaled (anArrowLength));
701
702 gp_Pnt aCenterLineBegin = isArrowsExternal
60bf98ae 703 ? aLineBegPoint : aFirstArrowEnd;
d7bffd44 704
60bf98ae 705 gp_Pnt aCenterLineEnd = isArrowsExternal || theIsOneSide
d7bffd44 706 ? aLineEndPoint : aSecondArrowEnd;
707
a6eb515f 708
d7bffd44 709 switch (aLabelPosition & LabelPosition_HMask)
a6eb515f 710 {
d7bffd44 711 // ------------------------------------------------------------------------ //
712 // CENTER //
713 // -------------------------------------------------------------------------//
d7bffd44 714 case LabelPosition_HCenter:
a6eb515f 715 {
d7bffd44 716 // add label on dimension or extension line to presentation
717 Prs3d_Root::NewGroup (thePresentation);
a6eb515f 718
af203d54 719 gp_Pnt aTextPos = IsTextPositionCustom() ? myFixedTextPosition
720 : (aCenterLineBegin.XYZ() + aCenterLineEnd.XYZ()) * 0.5;
60bf98ae 721 gp_Dir aTextDir = aDimensionLine.Direction();
a6eb515f 722
fe83e1ea 723 // add text primitives
724 if (theMode == ComputeMode_All || theMode == ComputeMode_Text)
a6eb515f 725 {
60bf98ae 726 DrawText (thePresentation,
fe83e1ea 727 aTextPos,
728 aTextDir,
729 aLabelString,
730 aLabelPosition);
a6eb515f 731 }
d7bffd44 732
fe83e1ea 733 // add dimension line primitives
734 if (theMode == ComputeMode_All || theMode == ComputeMode_Line)
735 {
736 Standard_Boolean isLineBreak = aDimensionAspect->TextVerticalPosition() == Prs3d_DTVP_Center
737 && aDimensionAspect->IsText3d();
d7bffd44 738
fe83e1ea 739 Handle(Graphic3d_ArrayOfSegments) aPrimSegments = new Graphic3d_ArrayOfSegments (isLineBreak ? 4 : 2);
d7bffd44 740
fe83e1ea 741 // compute continuous or sectioned main line segments
742 if (isLineBreak)
743 {
744 Standard_Real aPTextPosition = ElCLib::Parameter (aDimensionLine, aTextPos);
745 gp_Pnt aSection1Beg = aCenterLineBegin;
746 gp_Pnt aSection1End = ElCLib::Value (aPTextPosition - (aLabelWidth * 0.5), aDimensionLine);
747 gp_Pnt aSection2Beg = ElCLib::Value (aPTextPosition + (aLabelWidth * 0.5), aDimensionLine);
748 gp_Pnt aSection2End = aCenterLineEnd;
749
750 aPrimSegments->AddVertex (aSection1Beg);
751 aPrimSegments->AddVertex (aSection1End);
752 aPrimSegments->AddVertex (aSection2Beg);
753 aPrimSegments->AddVertex (aSection2End);
754
755 SelectionGeometry::Curve& aLeftSensitiveCurve = mySelectionGeom.NewCurve();
756 SelectionGeometry::Curve& aRightSensitiveCurve = mySelectionGeom.NewCurve();
757 aLeftSensitiveCurve.Append (aSection1Beg);
758 aLeftSensitiveCurve.Append (aSection1End);
759 aRightSensitiveCurve.Append (aSection2Beg);
760 aRightSensitiveCurve.Append (aSection2End);
761 }
762 else
763 {
764 aPrimSegments->AddVertex (aCenterLineBegin);
765 aPrimSegments->AddVertex (aCenterLineEnd);
a6eb515f 766
fe83e1ea 767 SelectionGeometry::Curve& aSensitiveCurve = mySelectionGeom.NewCurve();
768 aSensitiveCurve.Append (aCenterLineBegin);
769 aSensitiveCurve.Append (aCenterLineEnd);
770 }
a6eb515f 771
fe83e1ea 772 // set text label justification
773 Graphic3d_VerticalTextAlignment aTextJustificaton = Graphic3d_VTA_BOTTOM;
774 switch (aLabelPosition & LabelPosition_VMask)
775 {
776 case LabelPosition_Above :
777 case LabelPosition_VCenter : aTextJustificaton = Graphic3d_VTA_BOTTOM; break;
778 case LabelPosition_Below : aTextJustificaton = Graphic3d_VTA_TOP; break;
779 }
780 aDimensionAspect->TextAspect()->SetVerticalJustification (aTextJustificaton);
a6eb515f 781
fe83e1ea 782 // main dimension line, short extension
783 if (!aDimensionAspect->IsText3d() && theMode == ComputeMode_All)
784 {
785 Prs3d_Root::CurrentGroup (thePresentation)->SetStencilTestOptions (Standard_True);
786 }
787 Prs3d_Root::CurrentGroup (thePresentation)->SetPrimitivesAspect (aDimensionAspect->LineAspect()->Aspect());
788 Prs3d_Root::CurrentGroup (thePresentation)->AddPrimitiveArray (aPrimSegments);
789 if (!aDimensionAspect->IsText3d() && theMode == ComputeMode_All)
790 {
791 Prs3d_Root::CurrentGroup (thePresentation)->SetStencilTestOptions (Standard_False);
792 }
d7bffd44 793
fe83e1ea 794 // add arrows to presentation
795 Prs3d_Root::NewGroup (thePresentation);
d7bffd44 796
60bf98ae 797 DrawArrow (thePresentation, aFirstArrowBegin, aFirstArrowDir);
798 if (!theIsOneSide)
fe83e1ea 799 {
60bf98ae 800 DrawArrow (thePresentation, aSecondArrowBegin, aSecondArrowDir);
fe83e1ea 801 }
d7bffd44 802
fe83e1ea 803 if (!isArrowsExternal)
804 {
805 break;
806 }
d7bffd44 807
fe83e1ea 808 // add arrow extension lines to presentation
809 Prs3d_Root::NewGroup (thePresentation);
d7bffd44 810
af203d54 811 DrawExtension (thePresentation, aDimensionAspect->ArrowTailSize(),
fe83e1ea 812 aFirstArrowEnd, aFirstExtensionDir,
813 THE_EMPTY_LABEL, 0.0, theMode, LabelPosition_None);
60bf98ae 814 if (!theIsOneSide)
fe83e1ea 815 {
af203d54 816 DrawExtension (thePresentation, aDimensionAspect->ArrowTailSize(),
fe83e1ea 817 aSecondArrowEnd, aSecondExtensionDir,
818 THE_EMPTY_LABEL, 0.0, theMode, LabelPosition_None);
819 }
a6eb515f 820 }
1d7ca641 821
822 break;
a6eb515f 823 }
d7bffd44 824 // ------------------------------------------------------------------------ //
825 // LEFT //
826 // -------------------------------------------------------------------------//
827
828 case LabelPosition_Left:
a6eb515f 829 {
d7bffd44 830 // add label on dimension or extension line to presentation
831 Prs3d_Root::NewGroup (thePresentation);
a6eb515f 832
a6eb515f 833 // Left extension with the text
60bf98ae 834 DrawExtension (thePresentation, anExtensionSize,
835 isArrowsExternal
836 ? aFirstArrowEnd
837 : aFirstArrowBegin,
d7bffd44 838 aFirstExtensionDir,
fe83e1ea 839 aLabelString,
840 aLabelWidth,
d7bffd44 841 theMode,
842 aLabelPosition);
843
fe83e1ea 844 // add dimension line primitives
845 if (theMode == ComputeMode_All || theMode == ComputeMode_Line)
d7bffd44 846 {
fe83e1ea 847 // add central dimension line
848 Prs3d_Root::NewGroup (thePresentation);
d7bffd44 849
fe83e1ea 850 // add graphical primitives
851 Handle(Graphic3d_ArrayOfSegments) aPrimSegments = new Graphic3d_ArrayOfSegments (2);
852 aPrimSegments->AddVertex (aCenterLineBegin);
853 aPrimSegments->AddVertex (aCenterLineEnd);
d7bffd44 854
fe83e1ea 855 Prs3d_Root::CurrentGroup (thePresentation)->SetPrimitivesAspect (aDimensionAspect->LineAspect()->Aspect());
856 Prs3d_Root::CurrentGroup (thePresentation)->AddPrimitiveArray (aPrimSegments);
d7bffd44 857
fe83e1ea 858 // add selection primitives
859 SelectionGeometry::Curve& aSensitiveCurve = mySelectionGeom.NewCurve();
860 aSensitiveCurve.Append (aCenterLineBegin);
861 aSensitiveCurve.Append (aCenterLineEnd);
a6eb515f 862
fe83e1ea 863 // add arrows to presentation
864 Prs3d_Root::NewGroup (thePresentation);
a6eb515f 865
60bf98ae 866 DrawArrow (thePresentation, aFirstArrowBegin, aFirstArrowDir);
867 if (!theIsOneSide)
fe83e1ea 868 {
60bf98ae 869 DrawArrow (thePresentation, aSecondArrowBegin, aSecondArrowDir);
fe83e1ea 870 }
d7bffd44 871
60bf98ae 872 if (!isArrowsExternal || theIsOneSide)
fe83e1ea 873 {
874 break;
875 }
d7bffd44 876
fe83e1ea 877 // add extension lines for external arrows
878 Prs3d_Root::NewGroup (thePresentation);
d7bffd44 879
af203d54 880 DrawExtension (thePresentation, aDimensionAspect->ArrowTailSize(),
fe83e1ea 881 aSecondArrowEnd, aSecondExtensionDir,
882 THE_EMPTY_LABEL, 0.0, theMode, LabelPosition_None);
883 }
d7bffd44 884
a6eb515f 885 break;
886 }
d7bffd44 887 // ------------------------------------------------------------------------ //
888 // RIGHT //
889 // -------------------------------------------------------------------------//
890
891 case LabelPosition_Right:
a6eb515f 892 {
d7bffd44 893 // add label on dimension or extension line to presentation
894 Prs3d_Root::NewGroup (thePresentation);
895
896 // Right extension with text
60bf98ae 897 DrawExtension (thePresentation, anExtensionSize,
898 isArrowsExternal
899 ? aSecondArrowEnd
900 : aSecondArrowBegin,
d7bffd44 901 aSecondExtensionDir,
fe83e1ea 902 aLabelString, aLabelWidth,
d7bffd44 903 theMode,
904 aLabelPosition);
905
fe83e1ea 906 if (theMode == ComputeMode_All || theMode == ComputeMode_Line)
a6eb515f 907 {
fe83e1ea 908 // add central dimension line
909 Prs3d_Root::NewGroup (thePresentation);
a6eb515f 910
fe83e1ea 911 // add graphical primitives
912 Handle(Graphic3d_ArrayOfSegments) aPrimSegments = new Graphic3d_ArrayOfSegments (2);
913 aPrimSegments->AddVertex (aCenterLineBegin);
914 aPrimSegments->AddVertex (aCenterLineEnd);
915 Prs3d_Root::CurrentGroup (thePresentation)->SetPrimitivesAspect (aDimensionAspect->LineAspect()->Aspect());
916 Prs3d_Root::CurrentGroup (thePresentation)->AddPrimitiveArray (aPrimSegments);
a6eb515f 917
fe83e1ea 918 // add selection primitives
919 SelectionGeometry::Curve& aSensitiveCurve = mySelectionGeom.NewCurve();
920 aSensitiveCurve.Append (aCenterLineBegin);
921 aSensitiveCurve.Append (aCenterLineEnd);
a6eb515f 922
fe83e1ea 923 // add arrows to presentation
924 Prs3d_Root::NewGroup (thePresentation);
d7bffd44 925
60bf98ae 926 DrawArrow (thePresentation, aSecondArrowBegin, aSecondArrowDir);
927 if (!theIsOneSide)
fe83e1ea 928 {
60bf98ae 929 DrawArrow (thePresentation, aFirstArrowBegin, aFirstArrowDir);
fe83e1ea 930 }
d7bffd44 931
60bf98ae 932 if (!isArrowsExternal || theIsOneSide)
fe83e1ea 933 {
934 break;
935 }
a6eb515f 936
fe83e1ea 937 // add extension lines for external arrows
938 Prs3d_Root::NewGroup (thePresentation);
d7bffd44 939
af203d54 940 DrawExtension (thePresentation, aDimensionAspect->ArrowTailSize(),
fe83e1ea 941 aFirstArrowEnd, aFirstExtensionDir,
942 THE_EMPTY_LABEL, 0.0, theMode, LabelPosition_None);
943 }
d7bffd44 944
a6eb515f 945 break;
946 }
947 }
948
d7bffd44 949 // add flyout lines to presentation
fe83e1ea 950 if (theMode == ComputeMode_All)
d7bffd44 951 {
952 Prs3d_Root::NewGroup (thePresentation);
953
954 Handle(Graphic3d_ArrayOfSegments) aPrimSegments = new Graphic3d_ArrayOfSegments(4);
60bf98ae 955 aPrimSegments->AddVertex (theFirstPoint);
d7bffd44 956 aPrimSegments->AddVertex (aLineBegPoint);
957
60bf98ae 958 aPrimSegments->AddVertex (theSecondPoint);
d7bffd44 959 aPrimSegments->AddVertex (aLineEndPoint);
960
d7bffd44 961 Prs3d_Root::CurrentGroup (thePresentation)->SetPrimitivesAspect (aDimensionAspect->LineAspect()->Aspect());
962 Prs3d_Root::CurrentGroup (thePresentation)->AddPrimitiveArray (aPrimSegments);
963 }
964
91b16a64 965 mySelectionGeom.IsComputed = Standard_True;
a6eb515f 966}
967
968//=======================================================================
60bf98ae 969//function : ComputeLinearFlyouts
970//purpose :
a6eb515f 971//=======================================================================
60bf98ae 972void AIS_Dimension::ComputeLinearFlyouts (const Handle(SelectMgr_Selection)& theSelection,
973 const Handle(SelectMgr_EntityOwner)& theOwner,
974 const gp_Pnt& theFirstPoint,
975 const gp_Pnt& theSecondPoint)
a6eb515f 976{
60bf98ae 977 // count flyout direction
978 gp_Ax1 aPlaneNormal = GetPlane().Axis();
979 gp_Dir aTargetPointsVector = gce_MakeDir (theFirstPoint, theSecondPoint);
a6eb515f 980
60bf98ae 981 // count a flyout direction vector.
982 gp_Dir aFlyoutVector = aPlaneNormal.Direction() ^ aTargetPointsVector;
a6eb515f 983
60bf98ae 984 // create lines for layouts
985 gp_Lin aLine1 (theFirstPoint, aFlyoutVector);
986 gp_Lin aLine2 (theSecondPoint, aFlyoutVector);
987
988 // get flyout end points
989 gp_Pnt aFlyoutEnd1 = ElCLib::Value (ElCLib::Parameter (aLine1, theFirstPoint) + GetFlyout(), aLine1);
990 gp_Pnt aFlyoutEnd2 = ElCLib::Value (ElCLib::Parameter (aLine2, theSecondPoint) + GetFlyout(), aLine2);
991
992 // fill sensitive entity for flyouts
993 Handle(Select3D_SensitiveGroup) aSensitiveEntity = new Select3D_SensitiveGroup (theOwner);
994 aSensitiveEntity->Add (new Select3D_SensitiveSegment (theOwner, theFirstPoint, aFlyoutEnd1));
995 aSensitiveEntity->Add (new Select3D_SensitiveSegment (theOwner, theSecondPoint, aFlyoutEnd2));
996 theSelection->Add (aSensitiveEntity);
a6eb515f 997}
998
999//=======================================================================
60bf98ae 1000//function : CircleFromPlanarFace
a6eb515f 1001//purpose : if possible computes circle from planar face
1002//=======================================================================
60bf98ae 1003Standard_Boolean AIS_Dimension::CircleFromPlanarFace (const TopoDS_Face& theFace,
a6eb515f 1004 Handle(Geom_Curve)& theCurve,
60bf98ae 1005 gp_Pnt& theFirstPoint,
1006 gp_Pnt& theLastPoint)
a6eb515f 1007{
1008 TopExp_Explorer anIt (theFace, TopAbs_EDGE);
1009 for ( ; anIt.More(); anIt.Next())
1010 {
1011 TopoDS_Edge aCurEdge = TopoDS::Edge (anIt.Current());
1012 if (AIS::ComputeGeometry (aCurEdge, theCurve, theFirstPoint, theLastPoint))
1013 {
1014 if (theCurve->IsInstance (STANDARD_TYPE(Geom_Circle)))
1015 {
1016 return Standard_True;
1017 }
1018 }
1019 }
1020 return Standard_False;
1021}
1022
1023//=======================================================================
0499eb06 1024//function : CircleFromEdge
1025//purpose : if possible computes circle from edge
1026//=======================================================================
1027Standard_Boolean AIS_Dimension::CircleFromEdge (const TopoDS_Edge& theEdge,
1028 gp_Circ& theCircle,
1029 gp_Pnt& theFirstPoint,
1030 gp_Pnt& theLastPoint)
1031{
1032 BRepAdaptor_Curve anAdaptedCurve (theEdge);
1033 switch (anAdaptedCurve.GetType())
1034 {
1035 case GeomAbs_Circle:
1036 {
1037 theCircle = anAdaptedCurve.Circle();
1038 break;
1039 }
1040 case GeomAbs_Ellipse:
1041 {
1042 gp_Elips anEll = anAdaptedCurve.Ellipse();
1043 if ((anEll.MinorRadius() - anEll.MajorRadius()) >= Precision::Confusion())
1044 {
1045 return Standard_False;
1046 }
1047 theCircle = gp_Circ(anEll.Position(),anEll.MinorRadius());
1048 break;
1049 }
1050 case GeomAbs_Line:
1051 case GeomAbs_Hyperbola:
1052 case GeomAbs_Parabola:
1053 case GeomAbs_BezierCurve:
1054 case GeomAbs_BSplineCurve:
1055 case GeomAbs_OtherCurve:
1056 default:
1057 return Standard_False;
1058 }
1059
1060 theFirstPoint = anAdaptedCurve.Value (anAdaptedCurve.FirstParameter());
1061 theLastPoint = anAdaptedCurve.Value (anAdaptedCurve.LastParameter());
1062 return Standard_True;
1063}
1064
1065//=======================================================================
60bf98ae 1066//function : InitCircularDimension
1067//purpose :
a6eb515f 1068//=======================================================================
60bf98ae 1069Standard_Boolean AIS_Dimension::InitCircularDimension (const TopoDS_Shape& theShape,
a6eb515f 1070 gp_Circ& theCircle,
1071 gp_Pnt& theMiddleArcPoint,
60bf98ae 1072 Standard_Boolean& theIsClosed)
a6eb515f 1073{
1074 gp_Pln aPln;
1075 Handle(Geom_Surface) aBasisSurf;
1076 AIS_KindOfSurface aSurfType = AIS_KOS_OtherSurface;
1077 gp_Pnt aFirstPoint, aLastPoint;
1078 Standard_Real anOffset = 0.0;
1079 Standard_Real aFirstParam = 0.0;
1080 Standard_Real aLastParam = 0.0;
a6eb515f 1081
0499eb06 1082 // Discover circular geometry
1083 switch (theShape.ShapeType())
a6eb515f 1084 {
0499eb06 1085 case TopAbs_FACE:
a6eb515f 1086 {
0499eb06 1087 AIS::GetPlaneFromFace (TopoDS::Face (theShape), aPln, aBasisSurf, aSurfType, anOffset);
a6eb515f 1088
0499eb06 1089 if (aSurfType == AIS_KOS_Plane)
a6eb515f 1090 {
0499eb06 1091 Handle(Geom_Curve) aCurve;
1092 if (!CircleFromPlanarFace (TopoDS::Face (theShape), aCurve, aFirstPoint, aLastPoint))
1093 {
1094 return Standard_False;
1095 }
1096
1097 theCircle = Handle(Geom_Circle)::DownCast (aCurve)->Circ();
a6eb515f 1098 }
1099 else
1100 {
0499eb06 1101 gp_Pnt aCurPos;
1102 BRepAdaptor_Surface aSurf1 (TopoDS::Face (theShape));
1103 Standard_Real aFirstU = aSurf1.FirstUParameter();
1104 Standard_Real aLastU = aSurf1.LastUParameter();
1105 Standard_Real aFirstV = aSurf1.FirstVParameter();
1106 Standard_Real aLastV = aSurf1.LastVParameter();
1107 Standard_Real aMidU = (aFirstU + aLastU) * 0.5;
1108 Standard_Real aMidV = (aFirstV + aLastV) * 0.5;
1109 aSurf1.D0 (aMidU, aMidV, aCurPos);
1110 Handle (Adaptor3d_HCurve) aBasisCurve;
1111 Standard_Boolean isExpectedType = Standard_False;
1112 if (aSurfType == AIS_KOS_Cylinder)
a6eb515f 1113 {
0499eb06 1114 isExpectedType = Standard_True;
a6eb515f 1115 }
0499eb06 1116 else
a6eb515f 1117 {
0499eb06 1118 if (aSurfType == AIS_KOS_Revolution)
a6eb515f 1119 {
0499eb06 1120 aBasisCurve = aSurf1.BasisCurve();
1121 if (aBasisCurve->GetType() == GeomAbs_Line)
1122 {
1123 isExpectedType = Standard_True;
1124 }
1125 }
1126 else if (aSurfType == AIS_KOS_Extrusion)
1127 {
1128 aBasisCurve = aSurf1.BasisCurve();
1129 if (aBasisCurve->GetType() == GeomAbs_Circle)
1130 {
1131 isExpectedType = Standard_True;
1132 }
a6eb515f 1133 }
1134 }
a6eb515f 1135
0499eb06 1136 if (!isExpectedType)
1137 {
1138 return Standard_False;
1139 }
60bf98ae 1140
0499eb06 1141 Handle(Geom_Curve) aCurve = aBasisSurf->VIso(aMidV);
1142 if (aCurve->DynamicType() == STANDARD_TYPE (Geom_Circle))
a6eb515f 1143 {
0499eb06 1144 theCircle = Handle(Geom_Circle)::DownCast (aCurve)->Circ();
1145 }
1146 else if (aCurve->DynamicType() == STANDARD_TYPE (Geom_TrimmedCurve))
1147 {
1148 Handle(Geom_TrimmedCurve) aTrimmedCurve = Handle(Geom_TrimmedCurve)::DownCast (aCurve);
1149 aFirstU = aTrimmedCurve->FirstParameter();
1150 aLastU = aTrimmedCurve->LastParameter();
1151 if (aTrimmedCurve->BasisCurve()->DynamicType() == STANDARD_TYPE (Geom_Circle))
1152 {
1153 theCircle = Handle(Geom_Circle)::DownCast(aTrimmedCurve->BasisCurve())->Circ();
1154 }
1155 }
1156 else
1157 {
1158 // Compute a circle from 3 points on "aCurve"
1159 gp_Pnt aP1, aP2;
1160 aSurf1.D0 (aFirstU, aMidV, aP1);
1161 aSurf1.D0 (aLastU, aMidV, aP2);
1162 GC_MakeCircle aMkCirc (aP1, aCurPos, aP2);
1163 theCircle = aMkCirc.Value()->Circ();
a6eb515f 1164 }
a6eb515f 1165
0499eb06 1166 aFirstPoint = ElCLib::Value (aFirstU, theCircle);
1167 aLastPoint = ElCLib::Value (aLastU, theCircle);
1168 }
1169 break;
a6eb515f 1170 }
0499eb06 1171 case TopAbs_WIRE:
a6eb515f 1172 {
0499eb06 1173 TopoDS_Edge anEdge;
a6eb515f 1174 TopExp_Explorer anIt (theShape, TopAbs_EDGE);
1175 if (anIt.More())
1176 {
1177 anEdge = TopoDS::Edge (anIt.Current());
1178 }
0499eb06 1179 if (!AIS_Dimension::CircleFromEdge (anEdge, theCircle, aFirstPoint, aLastPoint))
1180 {
1181 return Standard_False;
1182 }
1183 break;
a6eb515f 1184 }
0499eb06 1185 case TopAbs_EDGE:
a6eb515f 1186 {
0499eb06 1187 TopoDS_Edge anEdge = TopoDS::Edge (theShape);
1188 if (!AIS_Dimension::CircleFromEdge (anEdge, theCircle, aFirstPoint, aLastPoint))
1189 {
1190 return Standard_False;
1191 }
1192 break;
a6eb515f 1193 }
0499eb06 1194 case TopAbs_COMPOUND:
1195 case TopAbs_COMPSOLID:
1196 case TopAbs_SOLID:
1197 case TopAbs_SHELL:
1198 case TopAbs_VERTEX:
1199 case TopAbs_SHAPE:
1200 default:
a6eb515f 1201 return Standard_False;
a6eb515f 1202 }
60bf98ae 1203
1204 theIsClosed = aFirstPoint.IsEqual (aLastPoint, Precision::Confusion());
1205
a6eb515f 1206 gp_Pnt aCenter = theCircle.Location();
60bf98ae 1207
1208 if (theIsClosed) // Circle
a6eb515f 1209 {
a6eb515f 1210 gp_Dir anXDir = theCircle.XAxis().Direction();
1211 theMiddleArcPoint = aCenter.Translated (gp_Vec (anXDir) * theCircle.Radius());
a6eb515f 1212 }
60bf98ae 1213 else // Arc
a6eb515f 1214 {
a6eb515f 1215 aFirstParam = ElCLib::Parameter (theCircle, aFirstPoint);
1216 aLastParam = ElCLib::Parameter (theCircle, aLastPoint);
1217 if (aFirstParam > aLastParam)
1218 {
1219 aFirstParam -= 2.0 * M_PI;
1220 }
60bf98ae 1221
a6eb515f 1222 Standard_Real aParCurPos = (aFirstParam + aLastParam) * 0.5;
1223 gp_Vec aVec = gp_Vec (aCenter, ElCLib::Value (aParCurPos, theCircle)).Normalized () * theCircle.Radius ();
1224 theMiddleArcPoint = aCenter.Translated (aVec);
a6eb515f 1225 }
1226
1227 return Standard_True;
1228}
1229
1230//=======================================================================
a6eb515f 1231//function : ComputeSelection
1232//purpose :
1233//=======================================================================
a6eb515f 1234void AIS_Dimension::ComputeSelection (const Handle(SelectMgr_Selection)& theSelection,
1235 const Standard_Integer theMode)
1236{
91b16a64 1237 if (!mySelectionGeom.IsComputed)
a6eb515f 1238 {
1239 return;
1240 }
1241
fe83e1ea 1242 AIS_DimensionSelectionMode aSelectionMode = (AIS_DimensionSelectionMode)theMode;
a6eb515f 1243
fe83e1ea 1244 // init appropriate entity owner
1245 Handle(SelectMgr_EntityOwner) aSensitiveOwner;
a6eb515f 1246
fe83e1ea 1247 switch (aSelectionMode)
a6eb515f 1248 {
fe83e1ea 1249 // neutral selection owner
1250 case AIS_DSM_All :
1251 aSensitiveOwner = new SelectMgr_EntityOwner (this, THE_NEUTRAL_SEL_PRIORITY);
1252 break;
1253
1254 // local selection owners
1255 case AIS_DSM_Line :
1256 case AIS_DSM_Text :
1257 aSensitiveOwner = new AIS_DimensionOwner (this, aSelectionMode, THE_LOCAL_SEL_PRIORITY);
1258 break;
a6eb515f 1259 }
fe83e1ea 1260
1261 if (aSelectionMode == AIS_DSM_All || aSelectionMode == AIS_DSM_Line)
a6eb515f 1262 {
fe83e1ea 1263 // sensitives for dimension line segments
1264 Handle(Select3D_SensitiveGroup) aGroupOfSensitives = new Select3D_SensitiveGroup (aSensitiveOwner);
1265
1266 SelectionGeometry::SeqOfCurves::Iterator aCurveIt (mySelectionGeom.DimensionLine);
1267 for (; aCurveIt.More(); aCurveIt.Next())
1268 {
1269 const SelectionGeometry::HCurve& aCurveData = aCurveIt.Value();
1270
1271 TColgp_Array1OfPnt aSensitivePnts (1, aCurveData->Length());
1272 for (Standard_Integer aPntIt = 1; aPntIt <= aCurveData->Length(); ++aPntIt)
1273 {
1274 aSensitivePnts.ChangeValue (aPntIt) = aCurveData->Value (aPntIt);
1275 }
1276
1277 aGroupOfSensitives->Add (new Select3D_SensitiveCurve (aSensitiveOwner, aSensitivePnts));
1278 }
1279
1280 Quantity_Length anArrowLength = myDrawer->DimensionAspect()->ArrowAspect()->Length();
1281 Standard_Real anArrowAngle = myDrawer->DimensionAspect()->ArrowAspect()->Angle();
1282
1283 // sensitives for arrows
1284 SelectionGeometry::SeqOfArrows::Iterator anArrowIt (mySelectionGeom.Arrows);
1285 for (; anArrowIt.More(); anArrowIt.Next())
1286 {
1287 const SelectionGeometry::HArrow& anArrow = anArrowIt.Value();
1288
1289 gp_Pnt aSidePnt1 (gp::Origin());
1290 gp_Pnt aSidePnt2 (gp::Origin());
60bf98ae 1291 const gp_Dir& aPlane = GetPlane().Axis().Direction();
fe83e1ea 1292 const gp_Pnt& aPeak = anArrow->Position;
1293 const gp_Dir& aDir = anArrow->Direction;
1294
1295 // compute points for arrow in plane
1296 PointsForArrow (aPeak, aDir, aPlane, anArrowLength, anArrowAngle, aSidePnt1, aSidePnt2);
1297
1298 aGroupOfSensitives->Add (new Select3D_SensitiveTriangle (aSensitiveOwner, aPeak, aSidePnt1, aSidePnt2));
1299
1300 if (!myDrawer->DimensionAspect()->IsArrows3d())
1301 {
1302 continue;
1303 }
1304
1305 // compute points for orthogonal sensitive plane
1306 gp_Dir anOrthoPlane = anArrow->Direction.Crossed (aPlane);
1307
1308 PointsForArrow (aPeak, aDir, anOrthoPlane, anArrowLength, anArrowAngle, aSidePnt1, aSidePnt2);
1309
1310 aGroupOfSensitives->Add (new Select3D_SensitiveTriangle (aSensitiveOwner, aPeak, aSidePnt1, aSidePnt2));
1311 }
1312
1313 theSelection->Add (aGroupOfSensitives);
a6eb515f 1314 }
fe83e1ea 1315
1316 // sensitives for text element
1317 if (aSelectionMode == AIS_DSM_All || aSelectionMode == AIS_DSM_Text)
a6eb515f 1318 {
fe83e1ea 1319 Handle(Select3D_SensitiveEntity) aTextSensitive;
1320
1321 gp_Ax2 aTextAxes (mySelectionGeom.TextPos,
60bf98ae 1322 GetPlane().Axis().Direction(),
fe83e1ea 1323 mySelectionGeom.TextDir);
1324
1325 if (myDrawer->DimensionAspect()->IsText3d())
1326 {
1327 // sensitive planar rectangle for text
1328 Standard_Real aDx = mySelectionGeom.TextWidth * 0.5;
1329 Standard_Real aDy = mySelectionGeom.TextHeight * 0.5;
1330
1331 gp_Trsf aLabelPlane;
1332 aLabelPlane.SetTransformation (aTextAxes, gp::XOY());
1333
51f8f60c 1334 TColgp_Array1OfPnt aRectanglePoints(1, 4);
1335 aRectanglePoints.ChangeValue(1) = gp_Pnt (-aDx, -aDy, 0.0).Transformed (aLabelPlane);
1336 aRectanglePoints.ChangeValue(2) = gp_Pnt (-aDx, aDy, 0.0).Transformed (aLabelPlane);
1337 aRectanglePoints.ChangeValue(3) = gp_Pnt ( aDx, aDy, 0.0).Transformed (aLabelPlane);
1338 aRectanglePoints.ChangeValue(4) = gp_Pnt ( aDx, -aDy, 0.0).Transformed (aLabelPlane);
fe83e1ea 1339
51f8f60c 1340 Poly_Array1OfTriangle aTriangles(1, 2);
1341 aTriangles.ChangeValue(1) = Poly_Triangle(1, 2, 3);
1342 aTriangles.ChangeValue(2) = Poly_Triangle(1, 3, 4);
1343
1344 Handle(Poly_Triangulation) aRectanglePoly =
1345 new Poly_Triangulation(aRectanglePoints, aTriangles);
1346
1347 aTextSensitive =
1348 new Select3D_SensitiveTriangulation (aSensitiveOwner, aRectanglePoly, TopLoc_Location(), Standard_True);
fe83e1ea 1349 }
1350 else
1351 {
1352 gp_Circ aTextGeom (aTextAxes, mySelToleranceForText2d != 0.0
1353 ? mySelToleranceForText2d : 1.0);
1354
1355 Handle(Geom_Circle) aSensGeom = new Geom_Circle (aTextGeom);
1356
1357 aTextSensitive = new Select3D_SensitiveCircle (aSensitiveOwner, aSensGeom, Standard_True);
1358 }
1359
1360 theSelection->Add (aTextSensitive);
a6eb515f 1361 }
fe83e1ea 1362
1363 // callback for flyout sensitive calculation
1364 if (aSelectionMode == AIS_DSM_All)
d7bffd44 1365 {
60bf98ae 1366 ComputeFlyoutSelection (theSelection, aSensitiveOwner);
d7bffd44 1367 }
fe83e1ea 1368}
1369
1370//=======================================================================
1371//function : PointsForArrow
1372//purpose :
1373//=======================================================================
1374void AIS_Dimension::PointsForArrow (const gp_Pnt& thePeakPnt,
1375 const gp_Dir& theDirection,
1376 const gp_Dir& thePlane,
1377 const Standard_Real theArrowLength,
1378 const Standard_Real theArrowAngle,
1379 gp_Pnt& theSidePnt1,
1380 gp_Pnt& theSidePnt2)
1381{
1382 gp_Lin anArrowLin (thePeakPnt, theDirection.Reversed());
1383 gp_Pnt anArrowEnd = ElCLib::Value (theArrowLength, anArrowLin);
1384 gp_Lin anEdgeLin (anArrowEnd, theDirection.Crossed (thePlane));
1385
1386 Standard_Real anEdgeLength = Tan (theArrowAngle) * theArrowLength;
a6eb515f 1387
fe83e1ea 1388 theSidePnt1 = ElCLib::Value ( anEdgeLength, anEdgeLin);
1389 theSidePnt2 = ElCLib::Value (-anEdgeLength, anEdgeLin);
a6eb515f 1390}
af203d54 1391
1392//=======================================================================
1393//function : GetTextPositionForLinear
1394//purpose :
1395//=======================================================================
1396gp_Pnt AIS_Dimension::GetTextPositionForLinear (const gp_Pnt& theFirstPoint,
1397 const gp_Pnt& theSecondPoint,
1398 const Standard_Boolean theIsOneSide) const
1399{
91b16a64 1400 if (!IsValid())
af203d54 1401 {
1402 return gp::Origin();
1403 }
1404
1405 gp_Pnt aTextPosition (gp::Origin());
1406
1407 Handle(Prs3d_DimensionAspect) aDimensionAspect = myDrawer->DimensionAspect();
1408
1409 // Get label alignment and arrow orientation.
1410 Standard_Integer aLabelPosition = 0;
1411 Standard_Boolean isArrowsExternal = Standard_False;
1412 FitTextAlignmentForLinear (theFirstPoint, theSecondPoint, theIsOneSide,
1413 aDimensionAspect->TextHorizontalPosition(),
1414 aLabelPosition, isArrowsExternal);
1415
1416 // Compute dimension line points.
1417 gp_Dir aPlaneNormal = GetPlane().Axis().Direction();
1418 gp_Vec aTargetPointsVec (theFirstPoint, theSecondPoint);
1419
1420 // Compute flyout direction vector
1421 gp_Dir aFlyoutVector = aPlaneNormal ^ gp_Dir (aTargetPointsVec);
1422
1423 // create lines for layouts
1424 gp_Lin aLine1 (theFirstPoint, aFlyoutVector);
1425 gp_Lin aLine2 (theSecondPoint, aFlyoutVector);
1426 // Get flyout end points
1427 gp_Pnt aLineBegPoint = ElCLib::Value (ElCLib::Parameter (aLine1, theFirstPoint) + GetFlyout(), aLine1);
1428 gp_Pnt aLineEndPoint = ElCLib::Value (ElCLib::Parameter (aLine2, theSecondPoint) + GetFlyout(), aLine2);
1429
1430 // Get text position.
1431 switch (aLabelPosition & LabelPosition_HMask)
1432 {
1433 case LabelPosition_Left:
1434 {
1435 gp_Dir aTargetPointsDir = gce_MakeDir (theFirstPoint, theSecondPoint);
1436 Standard_Real anExtensionSize = aDimensionAspect->ExtensionSize();
1437
1438 Standard_Real anOffset = isArrowsExternal
1439 ? anExtensionSize + aDimensionAspect->ArrowAspect()->Length()
1440 : anExtensionSize;
1441 gp_Vec anExtensionVec = gp_Vec (aTargetPointsDir) * -anOffset;
1442 aTextPosition = aLineEndPoint.Translated (anExtensionVec);
1443 }
1444 break;
1445 case LabelPosition_Right:
1446 {
1447 gp_Dir aTargetPointsDir = gce_MakeDir (theFirstPoint, theSecondPoint);
1448 Standard_Real anExtensionSize = aDimensionAspect->ExtensionSize();
1449
1450 Standard_Real anOffset = isArrowsExternal
1451 ? anExtensionSize + aDimensionAspect->ArrowAspect()->Length()
1452 : anExtensionSize;
1453 gp_Vec anExtensionVec = gp_Vec (aTargetPointsDir) * anOffset;
1454 aTextPosition = aLineBegPoint.Translated (anExtensionVec);
1455 }
1456 break;
1457 case LabelPosition_HCenter:
1458 {
1459 aTextPosition = (aLineBegPoint.XYZ() + aLineEndPoint.XYZ()) * 0.5;
1460 }
1461 break;
1462 }
1463
1464 return aTextPosition;
1465}
1466
1467//=======================================================================
91b16a64 1468//function : AdjustParametersForLinear
af203d54 1469//purpose :
1470//=======================================================================
91b16a64 1471Standard_Boolean AIS_Dimension::AdjustParametersForLinear (const gp_Pnt& theTextPos,
1472 const gp_Pnt& theFirstPoint,
1473 const gp_Pnt& theSecondPoint,
1474 Standard_Real& theExtensionSize,
1475 Prs3d_DimensionTextHorizontalPosition& theAlignment,
1476 Standard_Real& theFlyout,
1477 gp_Pln& thePlane,
1478 Standard_Boolean& theIsPlaneOld) const
af203d54 1479{
1480 Handle(Prs3d_DimensionAspect) aDimensionAspect = myDrawer->DimensionAspect();
1481 Standard_Real anArrowLength = aDimensionAspect->ArrowAspect()->Length();
1482
af203d54 1483 gp_Dir aTargetPointsDir = gce_MakeDir (theFirstPoint, theSecondPoint);
1484 gp_Vec aTargetPointsVec (theFirstPoint, theSecondPoint);
1485
91b16a64 1486 // Don't set new plane if the text position lies on the attachment points line.
1487 gp_Lin aTargetPointsLin (theFirstPoint, aTargetPointsDir);
1488 if (!aTargetPointsLin.Contains (theTextPos, Precision::Confusion()))
1489 {
1490 //Set new automatic plane.
1491 thePlane = gce_MakePln (theTextPos, theFirstPoint, theSecondPoint);
1492 theIsPlaneOld = Standard_False;
1493 }
1494
af203d54 1495 // Compute flyout direction vector.
91b16a64 1496 gp_Dir aPlaneNormal = GetPlane().Axis().Direction();
af203d54 1497 gp_Dir aPositiveFlyout = aPlaneNormal ^ aTargetPointsDir;
1498
91b16a64 1499 // Additional check of collinearity of the plane normal and attachment points vector.
1500 if (aPlaneNormal.IsParallel (aTargetPointsDir, Precision::Angular()))
1501 {
1502 return Standard_False;
1503 }
1504
af203d54 1505 // Set flyout.
1506 gp_Vec aFirstToTextVec (theFirstPoint, theTextPos);
1507
1508 Standard_Real aCos = aFirstToTextVec.Normalized() * gp_Vec (aTargetPointsDir);
1509
1510 gp_Pnt aTextPosProj = theFirstPoint.Translated
1511 (gp_Vec (aTargetPointsDir) * aFirstToTextVec.Magnitude() * aCos);
1512
1513 // Compute flyout value and direction.
1514 gp_Vec aFlyoutVector = gp_Vec (aTextPosProj, theTextPos);
1515
91b16a64 1516 theFlyout = 0.0;
1517 if (aFlyoutVector.Magnitude() > Precision::Confusion())
1518 {
1519 theFlyout = gp_Dir (aFlyoutVector).IsOpposite (aPositiveFlyout, Precision::Angular())
1520 ? -aFlyoutVector.Magnitude()
1521 : aFlyoutVector.Magnitude();
1522 }
1523
af203d54 1524 // Compute attach points (through which main dimension line passes).
1525 gp_Pnt aFirstAttach = theFirstPoint.Translated (aFlyoutVector);
1526 gp_Pnt aSecondAttach = theSecondPoint.Translated (aFlyoutVector);
1527
91b16a64 1528 // Set horizontal text alignment.
af203d54 1529 if (aCos < 0.0)
1530 {
1531 theAlignment = Prs3d_DTHP_Left;
1532
1533 Standard_Real aNewExtSize = theTextPos.Distance (aFirstAttach) - anArrowLength;
1534 theExtensionSize = aNewExtSize < 0.0 ? 0.0 : aNewExtSize;
1535 }
1536 else if (aTextPosProj.Distance (theFirstPoint) > theFirstPoint.Distance (theSecondPoint))
1537 {
1538 theAlignment = Prs3d_DTHP_Right;
1539
1540 Standard_Real aNewExtSize = theTextPos.Distance (aSecondAttach) - anArrowLength;
1541 theExtensionSize = aNewExtSize < 0.0 ? 0.0 : aNewExtSize;
1542 }
1543 else
1544 {
1545 theAlignment = Prs3d_DTHP_Center;
1546 }
91b16a64 1547 return Standard_True;
af203d54 1548}
1549
1550//=======================================================================
1551//function : FitTextAlignmentForLinear
1552//purpose :
1553//=======================================================================
1554void AIS_Dimension::FitTextAlignmentForLinear (const gp_Pnt& theFirstPoint,
1555 const gp_Pnt& theSecondPoint,
1556 const Standard_Boolean theIsOneSide,
1557 const Prs3d_DimensionTextHorizontalPosition& theHorizontalTextPos,
1558 Standard_Integer& theLabelPosition,
1559 Standard_Boolean& theIsArrowsExternal) const
1560{
1561 theLabelPosition = LabelPosition_None;
1562 theIsArrowsExternal = Standard_False;
1563
1564 // Compute dimension line points
1565 gp_Ax1 aPlaneNormal = GetPlane().Axis();
1566 gp_Dir aTargetPointsVector = gce_MakeDir (theFirstPoint, theSecondPoint);
1567
1568 // compute flyout direction vector
1569 gp_Dir aFlyoutVector = aPlaneNormal.Direction() ^ aTargetPointsVector;
1570
1571 // create lines for layouts
1572 gp_Lin aLine1 (theFirstPoint, aFlyoutVector);
1573 gp_Lin aLine2 (theSecondPoint, aFlyoutVector);
1574
1575 // Get flyout end points
1576 gp_Pnt aLineBegPoint = ElCLib::Value (ElCLib::Parameter (aLine1, theFirstPoint) + GetFlyout(), aLine1);
1577 gp_Pnt aLineEndPoint = ElCLib::Value (ElCLib::Parameter (aLine2, theSecondPoint) + GetFlyout(), aLine2);
1578
1579 Handle(Prs3d_DimensionAspect) aDimensionAspect = myDrawer->DimensionAspect();
1580
af203d54 1581 // For extensions we need to know arrow size, text size and extension size: get it from aspect
1582 Quantity_Length anArrowLength = aDimensionAspect->ArrowAspect()->Length();
1583
1584 // prepare label string and compute its geometrical width
1585 Standard_Real aLabelWidth;
1586 TCollection_ExtendedString aLabelString = GetValueString (aLabelWidth);
1587
1588 // Add margins to cut dimension lines for 3d text
1589 if (aDimensionAspect->IsText3d())
1590 {
1591 aLabelWidth += aDimensionAspect->TextAspect()->Height() * THE_3D_TEXT_MARGIN * 2.0;
1592 }
1593
1594 // Handle user-defined and automatic arrow placement
1595 switch (aDimensionAspect->ArrowOrientation())
1596 {
1597 case Prs3d_DAO_External: theIsArrowsExternal = true; break;
1598 case Prs3d_DAO_Internal: theIsArrowsExternal = false; break;
1599 case Prs3d_DAO_Fit:
1600 {
1601 // Add margin to ensure a small tail between text and arrow
1602 Standard_Real anArrowMargin = aDimensionAspect->IsText3d()
1603 ? aDimensionAspect->TextAspect()->Height() * THE_3D_TEXT_MARGIN
1604 : 0.0;
1605
1606 Standard_Real aDimensionWidth = aLineBegPoint.Distance (aLineEndPoint);
1607 Standard_Real anArrowsWidth = theIsOneSide
1608 ? anArrowLength + anArrowMargin
1609 : (anArrowLength + anArrowMargin) * 2.0;
1610
1611 theIsArrowsExternal = aDimensionWidth < aLabelWidth + anArrowsWidth;
1612 break;
1613 }
1614 }
1615
1616 // Handle user-defined and automatic text placement
1617 switch (theHorizontalTextPos)
1618 {
1619 case Prs3d_DTHP_Left : theLabelPosition |= LabelPosition_Left; break;
1620 case Prs3d_DTHP_Right : theLabelPosition |= LabelPosition_Right; break;
1621 case Prs3d_DTHP_Center: theLabelPosition |= LabelPosition_HCenter; break;
1622 case Prs3d_DTHP_Fit:
1623 {
1624 Standard_Real aDimensionWidth = aLineBegPoint.Distance (aLineEndPoint);
1625 Standard_Real anArrowsWidth = theIsOneSide ? anArrowLength : 2.0 * anArrowLength;
1626 Standard_Real aContentWidth = theIsArrowsExternal ? aLabelWidth : aLabelWidth + anArrowsWidth;
1627
1628 theLabelPosition |= aDimensionWidth < aContentWidth ? LabelPosition_Left : LabelPosition_HCenter;
1629 break;
1630 }
1631 }
1632
1633 // Handle vertical text placement options
1634 switch (aDimensionAspect->TextVerticalPosition())
1635 {
1636 case Prs3d_DTVP_Above : theLabelPosition |= LabelPosition_Above; break;
1637 case Prs3d_DTVP_Below : theLabelPosition |= LabelPosition_Below; break;
1638 case Prs3d_DTVP_Center : theLabelPosition |= LabelPosition_VCenter; break;
1639 }
1640}
1641
1642//=======================================================================
1643//function : UnsetFixedTextPosition
1644//purpose :
1645//=======================================================================
1646void AIS_Dimension::UnsetFixedTextPosition()
1647{
1648 myIsTextPositionFixed = Standard_False;
1649 myFixedTextPosition = gp::Origin();
1650}