0031456: Visualization - move out Dimensions and Relations from package AIS to PrsDims
[occt.git] / src / PrsDim / PrsDim_RadiusDimension.cxx
1 // Created on: 1996-12-05
2 // Created by: Jean-Pierre COMBE/Odile Olivier/Serguei Zaritchny
3 // Copyright (c) 1996-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #include <PrsDim_RadiusDimension.hxx>
18
19 #include <PrsDim.hxx>
20 #include <BRepLib_MakeEdge.hxx>
21 #include <ElCLib.hxx>
22 #include <gce_MakeDir.hxx>
23
24 IMPLEMENT_STANDARD_RTTIEXT(PrsDim_RadiusDimension, PrsDim_Dimension)
25
26 namespace
27 {
28   static const Standard_ExtCharacter THE_RADIUS_SYMBOL ('R');
29 }
30
31 //=======================================================================
32 //function : Constructor
33 //purpose  : 
34 //=======================================================================
35 PrsDim_RadiusDimension::PrsDim_RadiusDimension (const gp_Circ& theCircle)
36 : PrsDim_Dimension (PrsDim_KOD_RADIUS)
37 {
38   SetMeasuredGeometry (theCircle);
39   SetSpecialSymbol (THE_RADIUS_SYMBOL);
40   SetDisplaySpecialSymbol (PrsDim_DisplaySpecialSymbol_Before);
41   SetFlyout (0.0);
42 }
43
44 //=======================================================================
45 //function : Constructor
46 //purpose  : 
47 //=======================================================================
48 PrsDim_RadiusDimension::PrsDim_RadiusDimension (const gp_Circ& theCircle,
49                                                 const gp_Pnt& theAttachPoint)
50 : PrsDim_Dimension (PrsDim_KOD_RADIUS)
51 {
52   SetMeasuredGeometry (theCircle, theAttachPoint);
53   SetSpecialSymbol (THE_RADIUS_SYMBOL);
54   SetDisplaySpecialSymbol (PrsDim_DisplaySpecialSymbol_Before);
55   SetFlyout (0.0);
56 }
57
58 //=======================================================================
59 //function : Constructor
60 //purpose  :
61 //=======================================================================
62 PrsDim_RadiusDimension::PrsDim_RadiusDimension (const TopoDS_Shape& theShape)
63 : PrsDim_Dimension (PrsDim_KOD_RADIUS)
64 {
65   SetMeasuredGeometry (theShape);
66   SetSpecialSymbol (THE_RADIUS_SYMBOL);
67   SetDisplaySpecialSymbol (PrsDim_DisplaySpecialSymbol_Before);
68   SetFlyout (0.0);
69 }
70
71 //=======================================================================
72 //function : SetMeasuredGeometry
73 //purpose  : 
74 //=======================================================================
75 void PrsDim_RadiusDimension::SetMeasuredGeometry (const gp_Circ& theCircle,
76                                                   const gp_Pnt&  theAnchorPoint,
77                                                   const Standard_Boolean theHasAnchor)
78 {
79   myCircle          = theCircle;
80   myGeometryType    = GeometryType_Edge;
81   myShape           = BRepLib_MakeEdge (theCircle);
82   myAnchorPoint     = theHasAnchor ? theAnchorPoint : ElCLib::Value (0, myCircle);
83   myIsGeometryValid = IsValidCircle (myCircle) && IsValidAnchor (myCircle, myAnchorPoint);
84
85   if (myIsGeometryValid)
86   {
87     ComputePlane();
88   }
89
90   SetToUpdate();
91 }
92
93 //=======================================================================
94 //function : SetMeasuredGeometry
95 //purpose  : 
96 //=======================================================================
97 void PrsDim_RadiusDimension::SetMeasuredGeometry (const TopoDS_Shape& theShape,
98                                                   const gp_Pnt& theAnchorPoint,
99                                                   const Standard_Boolean theHasAnchor)
100 {
101   Standard_Boolean isClosed = Standard_False;
102   myShape                   = theShape;
103   myGeometryType            = GeometryType_UndefShapes;
104   myIsGeometryValid         = InitCircularDimension (theShape, myCircle, myAnchorPoint, isClosed) 
105                            && IsValidCircle (myCircle);
106   if (theHasAnchor)
107   {
108     myAnchorPoint = theAnchorPoint;
109     myIsGeometryValid = myIsGeometryValid && IsValidAnchor (myCircle, myAnchorPoint);
110   }
111
112   if (myIsGeometryValid)
113   {
114     ComputePlane();
115   }
116
117   SetToUpdate();
118 }
119
120 //=======================================================================
121 //function : CheckPlane
122 //purpose  : 
123 //=======================================================================
124 Standard_Boolean PrsDim_RadiusDimension::CheckPlane (const gp_Pln& thePlane) const
125 {
126   // Check if anchor point and circle center point belong to plane.
127   if (!thePlane.Contains (myAnchorPoint, Precision::Confusion()) &&
128       !thePlane.Contains (myCircle.Location(), Precision::Confusion()))
129   {
130     return Standard_False;
131   }
132
133   return Standard_True;
134 }
135
136 //=======================================================================
137 //function : ComputePlane
138 //purpose  : 
139 //=======================================================================
140 void PrsDim_RadiusDimension::ComputePlane()
141 {
142   if (!myIsGeometryValid)
143   {
144     return;
145   }
146
147   gp_Dir aDimensionX = gce_MakeDir (myAnchorPoint, myCircle.Location());
148
149   myPlane = gp_Pln (gp_Ax3 (myCircle.Location(),
150                             myCircle.Axis().Direction(),
151                             aDimensionX));
152 }
153
154 //=======================================================================
155 //function : GetModelUnits
156 //purpose  :
157 //=======================================================================
158 const TCollection_AsciiString& PrsDim_RadiusDimension::GetModelUnits() const
159 {
160   return myDrawer->DimLengthModelUnits();
161 }
162
163 //=======================================================================
164 //function : GetDisplayUnits
165 //purpose  :
166 //=======================================================================
167 const TCollection_AsciiString& PrsDim_RadiusDimension::GetDisplayUnits() const
168 {
169   return myDrawer->DimLengthDisplayUnits();
170 }
171
172 //=======================================================================
173 //function : SetModelUnits
174 //purpose  :
175 //=======================================================================
176 void PrsDim_RadiusDimension::SetModelUnits (const TCollection_AsciiString& theUnits)
177 {
178   myDrawer->SetDimLengthModelUnits (theUnits);
179 }
180
181 //=======================================================================
182 //function : SetDisplayUnits
183 //purpose  :
184 //=======================================================================
185 void PrsDim_RadiusDimension::SetDisplayUnits (const TCollection_AsciiString& theUnits)
186 {
187   myDrawer->SetDimLengthDisplayUnits(theUnits);
188 }
189
190 //=======================================================================
191 //function : ComputeValue
192 //purpose  : 
193 //=======================================================================
194 Standard_Real PrsDim_RadiusDimension::ComputeValue() const
195 {
196   if (!IsValid())
197   {
198     return 0.0;
199   }
200
201   return myCircle.Radius();
202 }
203
204 //=======================================================================
205 //function : Compute
206 //purpose  : 
207 //=======================================================================
208 void PrsDim_RadiusDimension::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePM*/,
209                                       const Handle(Prs3d_Presentation)& thePresentation,
210                                       const Standard_Integer theMode)
211 {
212   mySelectionGeom.Clear (theMode);
213
214   if (!IsValid())
215   {
216     return;
217   }
218
219   DrawLinearDimension (thePresentation, theMode, myAnchorPoint, myCircle.Location(), Standard_True);
220 }
221
222 //=======================================================================
223 //function : IsValidCircle
224 //purpose  : 
225 //=======================================================================
226 Standard_Boolean PrsDim_RadiusDimension::IsValidCircle (const gp_Circ& theCircle) const
227 {
228   return theCircle.Radius() > Precision::Confusion();
229 }
230
231 //=======================================================================
232 //function : IsValidAnchor
233 //purpose  : 
234 //=======================================================================
235 Standard_Boolean PrsDim_RadiusDimension::IsValidAnchor (const gp_Circ& theCircle,
236                                                         const gp_Pnt& theAnchor) const
237 {
238   gp_Pln aCirclePlane (theCircle.Location(), theCircle.Axis().Direction());
239   Standard_Real anAnchorDist = theAnchor.Distance (theCircle.Location());
240
241   return anAnchorDist > Precision::Confusion()
242       && aCirclePlane.Contains (theAnchor, Precision::Confusion());
243 }
244
245 //=======================================================================
246 //function : GetTextPosition
247 //purpose  : 
248 //=======================================================================
249 gp_Pnt PrsDim_RadiusDimension::GetTextPosition() const
250 {
251   if (IsTextPositionCustom())
252   {
253     return myFixedTextPosition;
254   }
255
256   // Counts text position according to the dimension parameters
257   return GetTextPositionForLinear (myAnchorPoint, myCircle.Location(), Standard_True);
258 }
259
260 //=======================================================================
261 //function : GetTextPosition
262 //purpose  : 
263 //=======================================================================
264 void PrsDim_RadiusDimension::SetTextPosition (const gp_Pnt& theTextPos)
265 {
266   if (!myIsGeometryValid)
267   {
268     return;
269   }
270
271   myIsTextPositionFixed = Standard_True;
272   myFixedTextPosition = theTextPos;
273
274   SetToUpdate();
275 }