64adef0cc92b913cf21b64c40c7dd6ee9f8ea548
[occt.git] / src / AIS / AIS_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 <AIS_RadiusDimension.hxx>
18
19 #include <AIS.hxx>
20 #include <BRepLib_MakeEdge.hxx>
21 #include <ElCLib.hxx>
22 #include <gce_MakeDir.hxx>
23
24
25 IMPLEMENT_STANDARD_RTTIEXT(AIS_RadiusDimension,AIS_Dimension)
26
27 namespace
28 {
29   static const Standard_ExtCharacter THE_RADIUS_SYMBOL ('R');
30 }
31
32 //=======================================================================
33 //function : Constructor
34 //purpose  : 
35 //=======================================================================
36 AIS_RadiusDimension::AIS_RadiusDimension (const gp_Circ& theCircle)
37 : AIS_Dimension (AIS_KOD_RADIUS)
38 {
39   SetMeasuredGeometry (theCircle);
40   SetSpecialSymbol (THE_RADIUS_SYMBOL);
41   SetDisplaySpecialSymbol (AIS_DSS_Before);
42   SetFlyout (0.0);
43 }
44
45 //=======================================================================
46 //function : Constructor
47 //purpose  : 
48 //=======================================================================
49 AIS_RadiusDimension::AIS_RadiusDimension (const gp_Circ& theCircle,
50                                           const gp_Pnt& theAttachPoint)
51 : AIS_Dimension (AIS_KOD_RADIUS)
52 {
53   SetMeasuredGeometry (theCircle, theAttachPoint);
54   SetSpecialSymbol (THE_RADIUS_SYMBOL);
55   SetDisplaySpecialSymbol (AIS_DSS_Before);
56   SetFlyout (0.0);
57 }
58
59 //=======================================================================
60 //function : Constructor
61 //purpose  :
62 //=======================================================================
63 AIS_RadiusDimension::AIS_RadiusDimension (const TopoDS_Shape& theShape)
64 : AIS_Dimension (AIS_KOD_RADIUS)
65 {
66   SetMeasuredGeometry (theShape);
67   SetSpecialSymbol (THE_RADIUS_SYMBOL);
68   SetDisplaySpecialSymbol (AIS_DSS_Before);
69   SetFlyout (0.0);
70 }
71
72 //=======================================================================
73 //function : SetMeasuredGeometry
74 //purpose  : 
75 //=======================================================================
76 void AIS_RadiusDimension::SetMeasuredGeometry (const gp_Circ& theCircle,
77                                                const gp_Pnt&  theAnchorPoint,
78                                                const Standard_Boolean theHasAnchor)
79 {
80   myCircle          = theCircle;
81   myGeometryType    = GeometryType_Edge;
82   myShape           = BRepLib_MakeEdge (theCircle);
83   myAnchorPoint     = theHasAnchor ? theAnchorPoint : ElCLib::Value (0, myCircle);
84   myIsGeometryValid = IsValidCircle (myCircle) && IsValidAnchor (myCircle, myAnchorPoint);
85
86   if (myIsGeometryValid)
87   {
88     ComputePlane();
89   }
90
91   SetToUpdate();
92 }
93
94 //=======================================================================
95 //function : SetMeasuredGeometry
96 //purpose  : 
97 //=======================================================================
98 void AIS_RadiusDimension::SetMeasuredGeometry (const TopoDS_Shape& theShape,
99                                                const gp_Pnt& theAnchorPoint,
100                                                const Standard_Boolean theHasAnchor)
101 {
102   Standard_Boolean isClosed = Standard_False;
103   myShape                   = theShape;
104   myGeometryType            = GeometryType_UndefShapes;
105   myIsGeometryValid         = InitCircularDimension (theShape, myCircle, myAnchorPoint, isClosed) 
106                            && IsValidCircle (myCircle);
107   if (theHasAnchor)
108   {
109     myAnchorPoint = theAnchorPoint;
110     myIsGeometryValid = myIsGeometryValid && IsValidAnchor (myCircle, myAnchorPoint);
111   }
112
113   if (myIsGeometryValid)
114   {
115     ComputePlane();
116   }
117
118   SetToUpdate();
119 }
120
121 //=======================================================================
122 //function : CheckPlane
123 //purpose  : 
124 //=======================================================================
125 Standard_Boolean AIS_RadiusDimension::CheckPlane (const gp_Pln& thePlane) const
126 {
127   // Check if anchor point and circle center point belong to plane.
128   if (!thePlane.Contains (myAnchorPoint, Precision::Confusion()) &&
129       !thePlane.Contains (myCircle.Location(), Precision::Confusion()))
130   {
131     return Standard_False;
132   }
133
134   return Standard_True;
135 }
136
137 //=======================================================================
138 //function : ComputePlane
139 //purpose  : 
140 //=======================================================================
141 void AIS_RadiusDimension::ComputePlane()
142 {
143   if (!myIsGeometryValid)
144   {
145     return;
146   }
147
148   gp_Dir aDimensionX = gce_MakeDir (myAnchorPoint, myCircle.Location());
149
150   myPlane = gp_Pln (gp_Ax3 (myCircle.Location(),
151                             myCircle.Axis().Direction(),
152                             aDimensionX));
153 }
154
155 //=======================================================================
156 //function : GetModelUnits
157 //purpose  :
158 //=======================================================================
159 const TCollection_AsciiString& AIS_RadiusDimension::GetModelUnits() const
160 {
161   return myDrawer->DimLengthModelUnits();
162 }
163
164 //=======================================================================
165 //function : GetDisplayUnits
166 //purpose  :
167 //=======================================================================
168 const TCollection_AsciiString& AIS_RadiusDimension::GetDisplayUnits() const
169 {
170   return myDrawer->DimLengthDisplayUnits();
171 }
172
173 //=======================================================================
174 //function : SetModelUnits
175 //purpose  :
176 //=======================================================================
177 void AIS_RadiusDimension::SetModelUnits (const TCollection_AsciiString& theUnits)
178 {
179   myDrawer->SetDimLengthModelUnits (theUnits);
180 }
181
182 //=======================================================================
183 //function : SetDisplayUnits
184 //purpose  :
185 //=======================================================================
186 void AIS_RadiusDimension::SetDisplayUnits (const TCollection_AsciiString& theUnits)
187 {
188   myDrawer->SetDimLengthDisplayUnits(theUnits);
189 }
190
191 //=======================================================================
192 //function : ComputeValue
193 //purpose  : 
194 //=======================================================================
195 Standard_Real AIS_RadiusDimension::ComputeValue() const
196 {
197   if (!IsValid())
198   {
199     return 0.0;
200   }
201
202   return myCircle.Radius();
203 }
204
205 //=======================================================================
206 //function : Compute
207 //purpose  : 
208 //=======================================================================
209 void AIS_RadiusDimension::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePM*/,
210                                    const Handle(Prs3d_Presentation)& thePresentation,
211                                    const Standard_Integer theMode)
212 {
213   mySelectionGeom.Clear (theMode);
214
215   if (!IsValid())
216   {
217     return;
218   }
219
220   DrawLinearDimension (thePresentation, theMode, myAnchorPoint, myCircle.Location(), Standard_True);
221 }
222
223 //=======================================================================
224 //function : IsValidCircle
225 //purpose  : 
226 //=======================================================================
227 Standard_Boolean AIS_RadiusDimension::IsValidCircle (const gp_Circ& theCircle) const
228 {
229   return theCircle.Radius() > Precision::Confusion();
230 }
231
232 //=======================================================================
233 //function : IsValidAnchor
234 //purpose  : 
235 //=======================================================================
236 Standard_Boolean AIS_RadiusDimension::IsValidAnchor (const gp_Circ& theCircle,
237                                                      const gp_Pnt& theAnchor) const
238 {
239   gp_Pln aCirclePlane (theCircle.Location(), theCircle.Axis().Direction());
240   Standard_Real anAnchorDist = theAnchor.Distance (theCircle.Location());
241   Standard_Real aRadius      = myCircle.Radius();
242
243   return Abs (anAnchorDist - aRadius) <= Precision::Confusion()
244       && aCirclePlane.Contains (theAnchor, Precision::Confusion());
245 }
246
247 //=======================================================================
248 //function : GetTextPosition
249 //purpose  : 
250 //=======================================================================
251 const gp_Pnt AIS_RadiusDimension::GetTextPosition() const
252 {
253   if (IsTextPositionCustom())
254   {
255     return myFixedTextPosition;
256   }
257
258   // Counts text position according to the dimension parameters
259   return GetTextPositionForLinear (myAnchorPoint, myCircle.Location(), Standard_True);
260 }
261
262 //=======================================================================
263 //function : GetTextPosition
264 //purpose  : 
265 //=======================================================================
266 void AIS_RadiusDimension::SetTextPosition (const gp_Pnt& theTextPos)
267 {
268   if (!myIsGeometryValid)
269   {
270     return;
271   }
272
273   myIsTextPositionFixed = Standard_True;
274   myFixedTextPosition = theTextPos;
275
276   SetToUpdate();
277 }