0025235: Draw Harness - improve commands vdimension and vdimparam
[occt.git] / src / AIS / AIS_Dimension.cxx
old mode 100644 (file)
new mode 100755 (executable)
index ec5c3a8..ebd65da
@@ -1,21 +1,17 @@
 // Created on: 2013-11-11
 // Created by: Anastasia BORISOVA
-// Copyright (c) 2013 OPEN CASCADE SAS
+// Copyright (c) 2013-2014 OPEN CASCADE SAS
 //
-// The content of this file is subject to the Open CASCADE Technology Public
-// License Version 6.5 (the "License"). You may not use the content of this file
-// except in compliance with the License. Please obtain a copy of the License
-// at http://www.opencascade.org and read it completely before using this file.
+// This file is part of Open CASCADE Technology software library.
 //
-// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
-// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
+// This library is free software; you can redistribute it and/or modify it under
+// the terms of the GNU Lesser General Public License version 2.1 as published
+// by the Free Software Foundation, with special exception defined in the file
+// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
+// distribution for complete text of the license and disclaimer of any warranty.
 //
-// The Original Code and all software distributed under the License is
-// distributed on an "AS IS" basis, without warranty of any kind, and the
-// Initial Developer hereby disclaims all such warranties, including without
-// limitation, any warranties of merchantability, fitness for a particular
-// purpose or non-infringement. Please see the License for the specific terms
-// and conditions governing the rights and limitations under the License.
+// Alternatively, this file may be used under the terms of Open CASCADE
+// commercial license or contractual agreement.
 
 #include <AIS_Dimension.hxx>
 
 #include <ElCLib.hxx>
 #include <Font_BRepFont.hxx>
 #include <GC_MakeCircle.hxx>
+#include <Geom_Line.hxx>
 #include <GeomAdaptor_Curve.hxx>
 #include <Geom_Circle.hxx>
 #include <Geom_TrimmedCurve.hxx>
 #include <gce_MakeDir.hxx>
 #include <gce_MakeLin.hxx>
+#include <gce_MakePln.hxx>
 #include <Graphic3d_ArrayOfSegments.hxx>
 #include <Graphic3d_ArrayOfTriangles.hxx>
 #include <Graphic3d_AspectLine3d.hxx>
 #include <Select3D_SensitiveGroup.hxx>
 #include <Select3D_SensitiveCurve.hxx>
 #include <Select3D_SensitiveSegment.hxx>
-#include <Select3D_SensitiveFace.hxx>
 #include <Select3D_SensitiveTriangle.hxx>
+#include <Select3D_SensitiveTriangulation.hxx>
+#include <Poly_Array1OfTriangle.hxx>
+#include <Poly_Triangulation.hxx>
 #include <Standard_CString.hxx>
+#include <Standard_ProgramError.hxx>
 #include <StdPrs_ShadedShape.hxx>
 #include <StdPrs_WFShape.hxx>
 #include <TCollection_AsciiString.hxx>
 #include <TCollection_ExtendedString.hxx>
 #include <TopExp_Explorer.hxx>
 #include <TopoDS.hxx>
+#include <TopoDS_Edge.hxx>
 #include <TopoDS_Vertex.hxx>
 #include <Units.hxx>
 #include <Units_UnitsDictionary.hxx>
 #include <UnitsAPI.hxx>
 #include <UnitsAPI_SystemUnits.hxx>
-#include <Standard_ProgramError.hxx>
 
 IMPLEMENT_STANDARD_HANDLE(AIS_Dimension, AIS_InteractiveObject)
 IMPLEMENT_STANDARD_RTTIEXT(AIS_Dimension, AIS_InteractiveObject)
@@ -80,7 +81,6 @@ IMPLEMENT_STANDARD_RTTIEXT(AIS_Dimension, AIS_InteractiveObject)
 namespace
 {
   // default text strings
-  static const Standard_Utf32Char THE_FILL_CHARACTER ('0');
   static const TCollection_ExtendedString THE_EMPTY_LABEL;
   static const TCollection_AsciiString    THE_UNDEFINED_UNITS;
 
@@ -106,7 +106,7 @@ AIS_Dimension::AIS_Dimension (const AIS_KindOfDimension theType)
   myGeometryType (GeometryType_UndefShapes),
   myIsPlaneCustom (Standard_False),
   myFlyout (0.0),
-  myIsValid (Standard_False),
+  myIsGeometryValid (Standard_False),
   myKindOfDimension (theType)
 {
 }
@@ -156,10 +156,12 @@ void AIS_Dimension::SetCustomPlane (const gp_Pln& thePlane)
   myPlane = thePlane;
   myIsPlaneCustom = Standard_True;
 
+  // Disable fixed (custom) text position
+  UnsetFixedTextPosition();
+
   // Check validity if geometry has been set already.
-  if (myIsValid)
+  if (IsValid())
   {
-    myIsValid &= CheckPlane (myPlane);
     SetToUpdate();
   }
 }
@@ -236,6 +238,9 @@ void AIS_Dimension::SetFlyout (const Standard_Real theFlyout)
 
   myFlyout = theFlyout;
 
+  // Disable fixed text position
+  UnsetFixedTextPosition();
+
   SetToUpdate();
 }
 
@@ -622,25 +627,8 @@ void AIS_Dimension::DrawLinearDimension (const Handle(Prs3d_Presentation)& thePr
     Standard_ProgramError::Raise ("Can not build presentation for equal points.");
   }
 
-  // compute dimension line points
-  gp_Ax1 aPlaneNormal = GetPlane().Axis();
-  gp_Dir aTargetPointsVector = gce_MakeDir (theFirstPoint, theSecondPoint);
-
-  // compute flyout direction vector
-  gp_Dir aFlyoutVector = aPlaneNormal.Direction() ^ aTargetPointsVector;
-
-  // create lines for layouts
-  gp_Lin aLine1 (theFirstPoint, aFlyoutVector);
-  gp_Lin aLine2 (theSecondPoint, aFlyoutVector);
-
-  // Get flyout end points
-  gp_Pnt aLineBegPoint = ElCLib::Value (ElCLib::Parameter (aLine1, theFirstPoint)  + GetFlyout(), aLine1);
-  gp_Pnt aLineEndPoint = ElCLib::Value (ElCLib::Parameter (aLine2, theSecondPoint) + GetFlyout(), aLine2);
-
   Handle(Prs3d_DimensionAspect) aDimensionAspect = myDrawer->DimensionAspect();
 
-  gp_Lin aDimensionLine = gce_MakeLin (aLineBegPoint, aLineEndPoint);
-
   // For extensions we need to know arrow size, text size and extension size: get it from aspect
   Quantity_Length anArrowLength   = aDimensionAspect->ArrowAspect()->Length();
   Standard_Real   anExtensionSize = aDimensionAspect->ExtensionSize();
@@ -655,28 +643,39 @@ void AIS_Dimension::DrawLinearDimension (const Handle(Prs3d_Presentation)& thePr
   }
 
   // handle user-defined and automatic arrow placement
-  bool isArrowsExternal = false;
-  switch (aDimensionAspect->ArrowOrientation())
+  Standard_Boolean isArrowsExternal = Standard_False;
+  Standard_Integer aLabelPosition = LabelPosition_None;
+
+  Prs3d_DimensionTextHorizontalPosition aHorisontalTextPos = aDimensionAspect->TextHorizontalPosition();
+  if (IsTextPositionCustom())
   {
-    case Prs3d_DAO_External: isArrowsExternal = true; break;
-    case Prs3d_DAO_Internal: isArrowsExternal = false; break;
-    case Prs3d_DAO_Fit:
+    if (!AdjustParametersForLinear (myFixedTextPosition, theFirstPoint, theSecondPoint,
+                                    anExtensionSize, aHorisontalTextPos, myFlyout, myPlane, myIsPlaneCustom))
     {
-      // add margin to ensure a small tail between text and arrow
-      Standard_Real anArrowMargin   = aDimensionAspect->IsText3d() 
-                                    ? aDimensionAspect->TextAspect()->Height() * THE_3D_TEXT_MARGIN
-                                    : 0.0;
-
-      Standard_Real aDimensionWidth = aLineBegPoint.Distance (aLineEndPoint);
-      Standard_Real anArrowsWidth   = theIsOneSide 
-                                    ?  anArrowLength + anArrowMargin
-                                    : (anArrowLength + anArrowMargin) * 2.0;
-
-      isArrowsExternal = aDimensionWidth < aLabelWidth + anArrowsWidth;
-      break;
+      Standard_ProgramError::Raise ("Can not adjust plane to the custom label position.");
     }
   }
 
+  FitTextAlignmentForLinear (theFirstPoint, theSecondPoint, theIsOneSide, aHorisontalTextPos,
+                             aLabelPosition, isArrowsExternal);
+
+    // compute dimension line points
+  gp_Ax1 aPlaneNormal = GetPlane().Axis();
+  gp_Dir aTargetPointsVector = gce_MakeDir (theFirstPoint, theSecondPoint);
+
+  // compute flyout direction vector
+  gp_Dir aFlyoutVector = aPlaneNormal.Direction() ^ aTargetPointsVector;
+
+  // create lines for layouts
+  gp_Lin aLine1 (theFirstPoint, aFlyoutVector);
+  gp_Lin aLine2 (theSecondPoint, aFlyoutVector);
+
+  // Get flyout end points
+  gp_Pnt aLineBegPoint = ElCLib::Value (ElCLib::Parameter (aLine1, theFirstPoint)  + GetFlyout(), aLine1);
+  gp_Pnt aLineEndPoint = ElCLib::Value (ElCLib::Parameter (aLine2, theSecondPoint) + GetFlyout(), aLine2);
+
+  gp_Lin aDimensionLine = gce_MakeLin (aLineBegPoint, aLineEndPoint);
+
   // compute arrows positions and directions
   gp_Dir aFirstArrowDir       = aDimensionLine.Direction().Reversed();
   gp_Dir aSecondArrowDir      = aDimensionLine.Direction();
@@ -705,32 +704,6 @@ void AIS_Dimension::DrawLinearDimension (const Handle(Prs3d_Presentation)& thePr
   gp_Pnt aCenterLineEnd = isArrowsExternal || theIsOneSide
     ? aLineEndPoint : aSecondArrowEnd;
 
-  Standard_Integer aLabelPosition = LabelPosition_None;
-
-  // handle user-defined and automatic text placement
-  switch (aDimensionAspect->TextHorizontalPosition())
-  {
-    case Prs3d_DTHP_Left  : aLabelPosition |= LabelPosition_Left; break;
-    case Prs3d_DTHP_Right : aLabelPosition |= LabelPosition_Right; break;
-    case Prs3d_DTHP_Center: aLabelPosition |= LabelPosition_HCenter; break;
-    case Prs3d_DTHP_Fit:
-    {
-      Standard_Real aDimensionWidth = aLineBegPoint.Distance (aLineEndPoint);
-      Standard_Real anArrowsWidth   = theIsOneSide ? anArrowLength : 2.0 * anArrowLength;
-      Standard_Real aContentWidth   = isArrowsExternal ? aLabelWidth : aLabelWidth + anArrowsWidth;
-
-      aLabelPosition |= aDimensionWidth < aContentWidth ? LabelPosition_Left : LabelPosition_HCenter;
-      break;
-    }
-  }
-
-  // handle vertical text placement options
-  switch (aDimensionAspect->TextVerticalPosition())
-  {
-    case Prs3d_DTVP_Above  : aLabelPosition |= LabelPosition_Above; break;
-    case Prs3d_DTVP_Below  : aLabelPosition |= LabelPosition_Below; break;
-    case Prs3d_DTVP_Center : aLabelPosition |= LabelPosition_VCenter; break;
-  }
 
   switch (aLabelPosition & LabelPosition_HMask)
   {
@@ -742,7 +715,8 @@ void AIS_Dimension::DrawLinearDimension (const Handle(Prs3d_Presentation)& thePr
       // add label on dimension or extension line to presentation
       Prs3d_Root::NewGroup (thePresentation);
 
-      gp_Pnt aTextPos = (aCenterLineBegin.XYZ() + aCenterLineEnd.XYZ()) * 0.5;
+      gp_Pnt aTextPos = IsTextPositionCustom() ? myFixedTextPosition
+                                              : (aCenterLineBegin.XYZ() + aCenterLineEnd.XYZ()) * 0.5;
       gp_Dir aTextDir = aDimensionLine.Direction();
 
       // add text primitives
@@ -833,12 +807,12 @@ void AIS_Dimension::DrawLinearDimension (const Handle(Prs3d_Presentation)& thePr
         // add arrow extension lines to presentation
         Prs3d_Root::NewGroup (thePresentation);
 
-        DrawExtension (thePresentation, anExtensionSize,
+        DrawExtension (thePresentation, aDimensionAspect->ArrowTailSize(),
                        aFirstArrowEnd, aFirstExtensionDir,
                        THE_EMPTY_LABEL, 0.0, theMode, LabelPosition_None);
         if (!theIsOneSide)
         {
-          DrawExtension (thePresentation, anExtensionSize,
+          DrawExtension (thePresentation, aDimensionAspect->ArrowTailSize(),
                          aSecondArrowEnd, aSecondExtensionDir,
                          THE_EMPTY_LABEL, 0.0, theMode, LabelPosition_None);
         }
@@ -902,7 +876,7 @@ void AIS_Dimension::DrawLinearDimension (const Handle(Prs3d_Presentation)& thePr
         // add extension lines for external arrows
         Prs3d_Root::NewGroup (thePresentation);
 
-        DrawExtension (thePresentation, anExtensionSize,
+        DrawExtension (thePresentation, aDimensionAspect->ArrowTailSize(),
                        aSecondArrowEnd, aSecondExtensionDir,
                        THE_EMPTY_LABEL, 0.0, theMode, LabelPosition_None);
       }
@@ -962,7 +936,7 @@ void AIS_Dimension::DrawLinearDimension (const Handle(Prs3d_Presentation)& thePr
         // add extension lines for external arrows
         Prs3d_Root::NewGroup (thePresentation);
 
-        DrawExtension (thePresentation, anExtensionSize,
+        DrawExtension (thePresentation, aDimensionAspect->ArrowTailSize(),
                        aFirstArrowEnd, aFirstExtensionDir,
                        THE_EMPTY_LABEL, 0.0, theMode, LabelPosition_None);
       }
@@ -988,7 +962,7 @@ void AIS_Dimension::DrawLinearDimension (const Handle(Prs3d_Presentation)& thePr
     Prs3d_Root::CurrentGroup (thePresentation)->AddPrimitiveArray (aPrimSegments);
   }
 
-  myIsComputed = Standard_True;
+  mySelectionGeom.IsComputed = Standard_True;
 }
 
 //=======================================================================
@@ -1046,6 +1020,48 @@ Standard_Boolean AIS_Dimension::CircleFromPlanarFace (const TopoDS_Face& theFace
   return Standard_False;
 }
 
+//=======================================================================
+//function : CircleFromEdge
+//purpose  : if possible computes circle from edge
+//=======================================================================
+Standard_Boolean AIS_Dimension::CircleFromEdge (const TopoDS_Edge& theEdge,
+                                                gp_Circ&           theCircle,
+                                                gp_Pnt&            theFirstPoint,
+                                                gp_Pnt&            theLastPoint)
+{
+  BRepAdaptor_Curve anAdaptedCurve (theEdge);
+  switch (anAdaptedCurve.GetType())
+  {
+    case GeomAbs_Circle:
+    {
+      theCircle = anAdaptedCurve.Circle();
+      break;
+    }
+    case GeomAbs_Ellipse:
+    {
+      gp_Elips anEll = anAdaptedCurve.Ellipse();
+      if ((anEll.MinorRadius() - anEll.MajorRadius()) >= Precision::Confusion())
+      {
+        return Standard_False;
+      }
+      theCircle = gp_Circ(anEll.Position(),anEll.MinorRadius());
+      break;
+    }
+    case GeomAbs_Line:
+    case GeomAbs_Hyperbola:
+    case GeomAbs_Parabola:
+    case GeomAbs_BezierCurve:
+    case GeomAbs_BSplineCurve:
+    case GeomAbs_OtherCurve:
+    default:
+      return Standard_False;
+  }
+
+  theFirstPoint = anAdaptedCurve.Value (anAdaptedCurve.FirstParameter());
+  theLastPoint  = anAdaptedCurve.Value (anAdaptedCurve.LastParameter());
+  return Standard_True;
+}
+
 //=======================================================================
 //function : InitCircularDimension
 //purpose  : 
@@ -1063,123 +1079,126 @@ Standard_Boolean AIS_Dimension::InitCircularDimension (const TopoDS_Shape& theSh
   Standard_Real aFirstParam = 0.0;
   Standard_Real aLastParam  = 0.0;
 
-  // discover circular geometry
-  if (theShape.ShapeType() == TopAbs_FACE)
+  // Discover circular geometry
+  switch (theShape.ShapeType())
   {
-    AIS::GetPlaneFromFace (TopoDS::Face (theShape), aPln, aBasisSurf, aSurfType, anOffset);
-
-    if (aSurfType == AIS_KOS_Plane)
+    case TopAbs_FACE:
     {
-      Handle(Geom_Curve) aCurve;
-      if (!CircleFromPlanarFace (TopoDS::Face (theShape), aCurve, aFirstPoint, aLastPoint))
-      {
-        return Standard_False;
-      }
+      AIS::GetPlaneFromFace (TopoDS::Face (theShape), aPln, aBasisSurf, aSurfType, anOffset);
 
-      theCircle = Handle(Geom_Circle)::DownCast (aCurve)->Circ();
-    }
-    else
-    {
-      gp_Pnt aCurPos;
-      BRepAdaptor_Surface aSurf1 (TopoDS::Face (theShape));
-      Standard_Real aFirstU = aSurf1.FirstUParameter();
-      Standard_Real aLastU  = aSurf1.LastUParameter();
-      Standard_Real aFirstV = aSurf1.FirstVParameter();
-      Standard_Real aLastV  = aSurf1.LastVParameter();
-      Standard_Real aMidU   = (aFirstU + aLastU) * 0.5;
-      Standard_Real aMidV   = (aFirstV + aLastV) * 0.5;
-      aSurf1.D0 (aMidU, aMidV, aCurPos);
-      Handle (Adaptor3d_HCurve) aBasisCurve;
-      Standard_Boolean isExpectedType = Standard_False;
-      if (aSurfType == AIS_KOS_Cylinder)
+      if (aSurfType == AIS_KOS_Plane)
       {
-        isExpectedType = Standard_True;
+        Handle(Geom_Curve) aCurve;
+        if (!CircleFromPlanarFace (TopoDS::Face (theShape), aCurve, aFirstPoint, aLastPoint))
+        {
+          return Standard_False;
+        }
+
+        theCircle = Handle(Geom_Circle)::DownCast (aCurve)->Circ();
       }
       else
       {
-        if (aSurfType == AIS_KOS_Revolution)
+        gp_Pnt aCurPos;
+        BRepAdaptor_Surface aSurf1 (TopoDS::Face (theShape));
+        Standard_Real aFirstU = aSurf1.FirstUParameter();
+        Standard_Real aLastU  = aSurf1.LastUParameter();
+        Standard_Real aFirstV = aSurf1.FirstVParameter();
+        Standard_Real aLastV  = aSurf1.LastVParameter();
+        Standard_Real aMidU   = (aFirstU + aLastU) * 0.5;
+        Standard_Real aMidV   = (aFirstV + aLastV) * 0.5;
+        aSurf1.D0 (aMidU, aMidV, aCurPos);
+        Handle (Adaptor3d_HCurve) aBasisCurve;
+        Standard_Boolean isExpectedType = Standard_False;
+        if (aSurfType == AIS_KOS_Cylinder)
         {
-          aBasisCurve = aSurf1.BasisCurve();
-          if (aBasisCurve->GetType() == GeomAbs_Line)
-          {
-            isExpectedType = Standard_True;
-          }
+          isExpectedType = Standard_True;
         }
-        else if (aSurfType == AIS_KOS_Extrusion)
+        else
         {
-          aBasisCurve = aSurf1.BasisCurve();
-          if (aBasisCurve->GetType() == GeomAbs_Circle)
+          if (aSurfType == AIS_KOS_Revolution)
+          {
+            aBasisCurve = aSurf1.BasisCurve();
+            if (aBasisCurve->GetType() == GeomAbs_Line)
+            {
+              isExpectedType = Standard_True;
+            }
+          }
+          else if (aSurfType == AIS_KOS_Extrusion)
           {
-            isExpectedType = Standard_True;
+            aBasisCurve = aSurf1.BasisCurve();
+            if (aBasisCurve->GetType() == GeomAbs_Circle)
+            {
+              isExpectedType = Standard_True;
+            }
           }
         }
-      }
 
-      if (!isExpectedType)
-      {
-        return Standard_False;
-      }
+        if (!isExpectedType)
+        {
+          return Standard_False;
+        }
 
-      Handle(Geom_Curve) aCurve;
-      aCurve = aBasisSurf->VIso(aMidV);
-      if (aCurve->DynamicType() == STANDARD_TYPE (Geom_Circle))
-      {
-        theCircle = Handle(Geom_Circle)::DownCast (aCurve)->Circ();
-      }
-      else if (aCurve->DynamicType() == STANDARD_TYPE (Geom_TrimmedCurve))
-      {
-        Handle(Geom_TrimmedCurve) aTrimmedCurve = Handle(Geom_TrimmedCurve)::DownCast (aCurve);
-        aFirstU = aTrimmedCurve->FirstParameter();
-        aLastU  = aTrimmedCurve->LastParameter();
-        if (aTrimmedCurve->DynamicType() == STANDARD_TYPE (Geom_Circle))
+        Handle(Geom_Curve) aCurve = aBasisSurf->VIso(aMidV);
+        if (aCurve->DynamicType() == STANDARD_TYPE (Geom_Circle))
         {
-          theCircle = Handle(Geom_Circle)::DownCast(aTrimmedCurve)->Circ();
+          theCircle = Handle(Geom_Circle)::DownCast (aCurve)->Circ();
+        }
+        else if (aCurve->DynamicType() == STANDARD_TYPE (Geom_TrimmedCurve))
+        {
+          Handle(Geom_TrimmedCurve) aTrimmedCurve = Handle(Geom_TrimmedCurve)::DownCast (aCurve);
+          aFirstU = aTrimmedCurve->FirstParameter();
+          aLastU  = aTrimmedCurve->LastParameter();
+          if (aTrimmedCurve->BasisCurve()->DynamicType() == STANDARD_TYPE (Geom_Circle))
+          {
+            theCircle = Handle(Geom_Circle)::DownCast(aTrimmedCurve->BasisCurve())->Circ();
+          }
+        }
+        else
+        {
+          // Compute a circle from 3 points on "aCurve"
+          gp_Pnt aP1, aP2;
+          aSurf1.D0 (aFirstU, aMidV, aP1);
+          aSurf1.D0 (aLastU, aMidV, aP2);
+          GC_MakeCircle aMkCirc (aP1, aCurPos, aP2);
+          theCircle = aMkCirc.Value()->Circ();
         }
-      }
-      else
-      {
-        // Compute a circle from 3 points on "aCurve"
-        gp_Pnt aP1, aP2;
-        aSurf1.D0 (aFirstU, aMidV, aP1);
-        aSurf1.D0 (aLastU, aMidV, aP2);
-        GC_MakeCircle aMkCirc (aP1, aCurPos, aP2);
-        theCircle = aMkCirc.Value()->Circ();
-      }
 
-      gp_Vec aVec = gp_Vec (theCircle.Location(), aCurPos).Normalized();
-      aFirstPoint = ElCLib::Value (aFirstU, theCircle);
-      aLastPoint = ElCLib::Value (aLastU,  theCircle);
+        aFirstPoint = ElCLib::Value (aFirstU, theCircle);
+        aLastPoint = ElCLib::Value (aLastU,  theCircle);
+      }
+      break;
     }
-  }
-  else // TopAbs_EDGE | TopAbs_WIRE
-  {
-    TopoDS_Edge anEdge;
-    if (theShape.ShapeType() == TopAbs_WIRE)
+    case TopAbs_WIRE:
     {
+      TopoDS_Edge anEdge;
       TopExp_Explorer anIt (theShape, TopAbs_EDGE);
       if (anIt.More())
       {
         anEdge = TopoDS::Edge (anIt.Current());
       }
+      if (!AIS_Dimension::CircleFromEdge (anEdge, theCircle, aFirstPoint, aLastPoint))
+      {
+        return Standard_False;
+      }
+      break;
     }
-    else if (theShape.ShapeType() == TopAbs_EDGE)
-    {
-      anEdge = TopoDS::Edge (theShape);
-    }
-    else // Unexpected type of shape
+    case TopAbs_EDGE:
     {
-      return Standard_False;
+      TopoDS_Edge anEdge = TopoDS::Edge (theShape);
+      if (!AIS_Dimension::CircleFromEdge (anEdge, theCircle, aFirstPoint, aLastPoint))
+      {
+        return Standard_False;
+      }
+      break;
     }
-
-    BRepAdaptor_Curve anAdaptedCurve (anEdge);
-    if (!anAdaptedCurve.GetType() == GeomAbs_Circle)
-    {
+    case TopAbs_COMPOUND:
+    case TopAbs_COMPSOLID:
+    case TopAbs_SOLID:
+    case TopAbs_SHELL:
+    case TopAbs_VERTEX:
+    case TopAbs_SHAPE:
+    default:
       return Standard_False;
-    }
-
-    theCircle   = anAdaptedCurve.Circle();
-    aFirstPoint = anAdaptedCurve.Value (anAdaptedCurve.FirstParameter());
-    aLastPoint  = anAdaptedCurve.Value (anAdaptedCurve.LastParameter());
   }
 
   theIsClosed = aFirstPoint.IsEqual (aLastPoint, Precision::Confusion());
@@ -1215,7 +1234,7 @@ Standard_Boolean AIS_Dimension::InitCircularDimension (const TopoDS_Shape& theSh
 void AIS_Dimension::ComputeSelection (const Handle(SelectMgr_Selection)& theSelection,
                                       const Standard_Integer theMode)
 {
-  if (!myIsComputed)
+  if (!mySelectionGeom.IsComputed)
   {
     return;
   }
@@ -1312,13 +1331,21 @@ void AIS_Dimension::ComputeSelection (const Handle(SelectMgr_Selection)& theSele
       gp_Trsf aLabelPlane;
       aLabelPlane.SetTransformation (aTextAxes, gp::XOY());
 
-      TColgp_Array1OfPnt aRectanglePoints (1, 4);
-      aRectanglePoints.ChangeValue (1) = gp_Pnt (-aDx, -aDy, 0.0).Transformed (aLabelPlane);
-      aRectanglePoints.ChangeValue (2) = gp_Pnt (-aDx,  aDy, 0.0).Transformed (aLabelPlane);
-      aRectanglePoints.ChangeValue (3) = gp_Pnt ( aDx,  aDy, 0.0).Transformed (aLabelPlane);
-      aRectanglePoints.ChangeValue (4) = gp_Pnt ( aDx, -aDy, 0.0).Transformed (aLabelPlane);
+      TColgp_Array1OfPnt aRectanglePoints(1, 4);
+      aRectanglePoints.ChangeValue(1) = gp_Pnt (-aDx, -aDy, 0.0).Transformed (aLabelPlane);
+      aRectanglePoints.ChangeValue(2) = gp_Pnt (-aDx,  aDy, 0.0).Transformed (aLabelPlane);
+      aRectanglePoints.ChangeValue(3) = gp_Pnt ( aDx,  aDy, 0.0).Transformed (aLabelPlane);
+      aRectanglePoints.ChangeValue(4) = gp_Pnt ( aDx, -aDy, 0.0).Transformed (aLabelPlane);
+
+      Poly_Array1OfTriangle aTriangles(1, 2);
+      aTriangles.ChangeValue(1) = Poly_Triangle(1, 2, 3);
+      aTriangles.ChangeValue(2) = Poly_Triangle(1, 3, 4);
 
-      aTextSensitive = new Select3D_SensitiveFace (aSensitiveOwner, aRectanglePoints);
+      Handle(Poly_Triangulation) aRectanglePoly = 
+        new Poly_Triangulation(aRectanglePoints, aTriangles);
+
+      aTextSensitive =
+        new Select3D_SensitiveTriangulation (aSensitiveOwner, aRectanglePoly, TopLoc_Location(), Standard_True);
     }
     else
     {
@@ -1361,3 +1388,263 @@ void AIS_Dimension::PointsForArrow (const gp_Pnt& thePeakPnt,
   theSidePnt1 = ElCLib::Value ( anEdgeLength, anEdgeLin);
   theSidePnt2 = ElCLib::Value (-anEdgeLength, anEdgeLin);
 }
+
+//=======================================================================
+//function : GetTextPositionForLinear
+//purpose  : 
+//=======================================================================
+gp_Pnt AIS_Dimension::GetTextPositionForLinear (const gp_Pnt& theFirstPoint,
+                                                const gp_Pnt& theSecondPoint,
+                                                const Standard_Boolean theIsOneSide) const
+{
+  if (!IsValid())
+  {
+    return gp::Origin();
+  }
+
+  gp_Pnt aTextPosition (gp::Origin());
+
+  Handle(Prs3d_DimensionAspect) aDimensionAspect = myDrawer->DimensionAspect();
+
+  // Get label alignment and arrow orientation.
+  Standard_Integer aLabelPosition = 0;
+  Standard_Boolean isArrowsExternal = Standard_False;
+  FitTextAlignmentForLinear (theFirstPoint, theSecondPoint, theIsOneSide,
+                             aDimensionAspect->TextHorizontalPosition(),
+                             aLabelPosition, isArrowsExternal);
+
+  // Compute dimension line points.
+  gp_Dir aPlaneNormal = GetPlane().Axis().Direction();
+  gp_Vec aTargetPointsVec (theFirstPoint, theSecondPoint);
+
+  // Compute flyout direction vector
+  gp_Dir aFlyoutVector = aPlaneNormal ^ gp_Dir (aTargetPointsVec);
+
+  // create lines for layouts
+  gp_Lin aLine1 (theFirstPoint, aFlyoutVector);
+  gp_Lin aLine2 (theSecondPoint, aFlyoutVector);
+  // Get flyout end points
+  gp_Pnt aLineBegPoint = ElCLib::Value (ElCLib::Parameter (aLine1, theFirstPoint)  + GetFlyout(), aLine1);
+  gp_Pnt aLineEndPoint = ElCLib::Value (ElCLib::Parameter (aLine2, theSecondPoint) + GetFlyout(), aLine2);
+
+  // Get text position.
+  switch (aLabelPosition & LabelPosition_HMask)
+  {
+  case LabelPosition_Left:
+    {
+      gp_Dir aTargetPointsDir = gce_MakeDir (theFirstPoint, theSecondPoint);
+      Standard_Real anExtensionSize = aDimensionAspect->ExtensionSize();
+
+      Standard_Real anOffset = isArrowsExternal
+                                 ? anExtensionSize + aDimensionAspect->ArrowAspect()->Length()
+                                 : anExtensionSize;
+      gp_Vec anExtensionVec = gp_Vec (aTargetPointsDir) * -anOffset;
+      aTextPosition = aLineEndPoint.Translated (anExtensionVec);
+    }
+    break;
+  case LabelPosition_Right:
+    {
+      gp_Dir aTargetPointsDir = gce_MakeDir (theFirstPoint, theSecondPoint);
+      Standard_Real anExtensionSize = aDimensionAspect->ExtensionSize();
+
+      Standard_Real anOffset = isArrowsExternal
+                                 ? anExtensionSize + aDimensionAspect->ArrowAspect()->Length()
+                                 : anExtensionSize;
+      gp_Vec anExtensionVec = gp_Vec (aTargetPointsDir) * anOffset;
+      aTextPosition = aLineBegPoint.Translated (anExtensionVec);
+    }
+    break;
+  case LabelPosition_HCenter:
+    {
+      aTextPosition = (aLineBegPoint.XYZ() + aLineEndPoint.XYZ()) * 0.5;
+    }
+    break;
+  }
+
+  return aTextPosition;
+}
+
+//=======================================================================
+//function : AdjustParametersForLinear
+//purpose  : 
+//=======================================================================
+Standard_Boolean AIS_Dimension::AdjustParametersForLinear (const gp_Pnt& theTextPos,
+                                                           const gp_Pnt& theFirstPoint,
+                                                           const gp_Pnt& theSecondPoint,
+                                                           Standard_Real& theExtensionSize,
+                                                           Prs3d_DimensionTextHorizontalPosition& theAlignment,
+                                                           Standard_Real& theFlyout,
+                                                           gp_Pln& thePlane,
+                                                           Standard_Boolean& theIsPlaneOld) const
+{
+  Handle(Prs3d_DimensionAspect) aDimensionAspect = myDrawer->DimensionAspect();
+  Standard_Real anArrowLength = aDimensionAspect->ArrowAspect()->Length();
+
+  gp_Dir aTargetPointsDir = gce_MakeDir (theFirstPoint, theSecondPoint);
+  gp_Vec aTargetPointsVec (theFirstPoint, theSecondPoint);
+
+  // Don't set new plane if the text position lies on the attachment points line.
+  gp_Lin aTargetPointsLin (theFirstPoint, aTargetPointsDir);
+  if (!aTargetPointsLin.Contains (theTextPos, Precision::Confusion()))
+  {
+    //Set new automatic plane.
+    thePlane = gce_MakePln (theTextPos, theFirstPoint, theSecondPoint);
+    theIsPlaneOld = Standard_False;
+  }
+
+  // Compute flyout direction vector.
+  gp_Dir aPlaneNormal = GetPlane().Axis().Direction();
+  gp_Dir aPositiveFlyout = aPlaneNormal ^ aTargetPointsDir;
+
+  // Additional check of collinearity of the plane normal and attachment points vector.
+  if (aPlaneNormal.IsParallel (aTargetPointsDir, Precision::Angular()))
+  {
+    return Standard_False;
+  }
+
+  // Set flyout.
+  gp_Vec aFirstToTextVec (theFirstPoint, theTextPos);
+
+  Standard_Real aCos = aFirstToTextVec.Normalized() * gp_Vec (aTargetPointsDir);
+
+  gp_Pnt aTextPosProj = theFirstPoint.Translated
+    (gp_Vec (aTargetPointsDir) * aFirstToTextVec.Magnitude() * aCos);
+
+  // Compute flyout value and direction.
+  gp_Vec aFlyoutVector = gp_Vec (aTextPosProj, theTextPos);
+
+  theFlyout = 0.0;
+  if (aFlyoutVector.Magnitude() > Precision::Confusion())
+  {
+    theFlyout = gp_Dir (aFlyoutVector).IsOpposite (aPositiveFlyout, Precision::Angular())
+                ? -aFlyoutVector.Magnitude()
+                :  aFlyoutVector.Magnitude();
+  }
+  
+  // Compute attach points (through which main dimension line passes).
+  gp_Pnt aFirstAttach  = theFirstPoint.Translated (aFlyoutVector);
+  gp_Pnt aSecondAttach = theSecondPoint.Translated (aFlyoutVector);
+
+  // Set horizontal text alignment.
+  if (aCos < 0.0)
+  {
+    theAlignment = Prs3d_DTHP_Left;
+
+    Standard_Real aNewExtSize = theTextPos.Distance (aFirstAttach) - anArrowLength;
+    theExtensionSize = aNewExtSize < 0.0 ? 0.0 : aNewExtSize;
+  }
+  else if (aTextPosProj.Distance (theFirstPoint) > theFirstPoint.Distance (theSecondPoint))
+  {
+    theAlignment = Prs3d_DTHP_Right;
+
+    Standard_Real aNewExtSize = theTextPos.Distance (aSecondAttach) - anArrowLength;
+    theExtensionSize = aNewExtSize < 0.0 ? 0.0 : aNewExtSize;
+  }
+  else
+  {
+    theAlignment = Prs3d_DTHP_Center;
+  }
+  return Standard_True;
+}
+
+//=======================================================================
+//function : FitTextAlignmentForLinear
+//purpose  : 
+//=======================================================================
+void AIS_Dimension::FitTextAlignmentForLinear (const gp_Pnt& theFirstPoint,
+                                               const gp_Pnt& theSecondPoint,
+                                               const Standard_Boolean theIsOneSide,
+                                               const Prs3d_DimensionTextHorizontalPosition& theHorizontalTextPos,
+                                               Standard_Integer& theLabelPosition,
+                                               Standard_Boolean& theIsArrowsExternal) const
+{
+  theLabelPosition = LabelPosition_None;
+  theIsArrowsExternal = Standard_False;
+
+  // Compute dimension line points
+  gp_Ax1 aPlaneNormal = GetPlane().Axis();
+  gp_Dir aTargetPointsVector = gce_MakeDir (theFirstPoint, theSecondPoint);
+
+  // compute flyout direction vector
+  gp_Dir aFlyoutVector = aPlaneNormal.Direction() ^ aTargetPointsVector;
+
+  // create lines for layouts
+  gp_Lin aLine1 (theFirstPoint, aFlyoutVector);
+  gp_Lin aLine2 (theSecondPoint, aFlyoutVector);
+
+  // Get flyout end points
+  gp_Pnt aLineBegPoint = ElCLib::Value (ElCLib::Parameter (aLine1, theFirstPoint)  + GetFlyout(), aLine1);
+  gp_Pnt aLineEndPoint = ElCLib::Value (ElCLib::Parameter (aLine2, theSecondPoint) + GetFlyout(), aLine2);
+
+  Handle(Prs3d_DimensionAspect) aDimensionAspect = myDrawer->DimensionAspect();
+
+  // For extensions we need to know arrow size, text size and extension size: get it from aspect
+  Quantity_Length anArrowLength = aDimensionAspect->ArrowAspect()->Length();
+
+  // prepare label string and compute its geometrical width
+  Standard_Real aLabelWidth;
+  TCollection_ExtendedString aLabelString = GetValueString (aLabelWidth);
+
+  // Add margins to cut dimension lines for 3d text
+  if (aDimensionAspect->IsText3d())
+  {
+    aLabelWidth += aDimensionAspect->TextAspect()->Height() * THE_3D_TEXT_MARGIN * 2.0;
+  }
+
+  // Handle user-defined and automatic arrow placement
+  switch (aDimensionAspect->ArrowOrientation())
+  {
+    case Prs3d_DAO_External: theIsArrowsExternal = true; break;
+    case Prs3d_DAO_Internal: theIsArrowsExternal = false; break;
+    case Prs3d_DAO_Fit:
+    {
+      // Add margin to ensure a small tail between text and arrow
+      Standard_Real anArrowMargin   = aDimensionAspect->IsText3d() 
+                                    ? aDimensionAspect->TextAspect()->Height() * THE_3D_TEXT_MARGIN
+                                    : 0.0;
+
+      Standard_Real aDimensionWidth = aLineBegPoint.Distance (aLineEndPoint);
+      Standard_Real anArrowsWidth   = theIsOneSide 
+                                      ?  anArrowLength + anArrowMargin
+                                      : (anArrowLength + anArrowMargin) * 2.0;
+
+      theIsArrowsExternal = aDimensionWidth < aLabelWidth + anArrowsWidth;
+      break;
+    }
+  }
+
+  // Handle user-defined and automatic text placement
+  switch (theHorizontalTextPos)
+  {
+    case Prs3d_DTHP_Left  : theLabelPosition |= LabelPosition_Left; break;
+    case Prs3d_DTHP_Right : theLabelPosition |= LabelPosition_Right; break;
+    case Prs3d_DTHP_Center: theLabelPosition |= LabelPosition_HCenter; break;
+    case Prs3d_DTHP_Fit:
+    {
+      Standard_Real aDimensionWidth = aLineBegPoint.Distance (aLineEndPoint);
+      Standard_Real anArrowsWidth   = theIsOneSide ? anArrowLength : 2.0 * anArrowLength;
+      Standard_Real aContentWidth   = theIsArrowsExternal ? aLabelWidth : aLabelWidth + anArrowsWidth;
+
+      theLabelPosition |= aDimensionWidth < aContentWidth ? LabelPosition_Left : LabelPosition_HCenter;
+      break;
+    }
+  }
+
+  // Handle vertical text placement options
+  switch (aDimensionAspect->TextVerticalPosition())
+  {
+    case Prs3d_DTVP_Above  : theLabelPosition |= LabelPosition_Above; break;
+    case Prs3d_DTVP_Below  : theLabelPosition |= LabelPosition_Below; break;
+    case Prs3d_DTVP_Center : theLabelPosition |= LabelPosition_VCenter; break;
+  }
+}
+
+//=======================================================================
+//function : UnsetFixedTextPosition
+//purpose  : 
+//=======================================================================
+void AIS_Dimension::UnsetFixedTextPosition()
+{
+  myIsTextPositionFixed = Standard_False;
+  myFixedTextPosition = gp::Origin();
+}