0027957: Visualization, AIS_InteractiveContext - protect from displaying the same...
[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 {
78   myCircle          = theCircle;
79   myGeometryType    = GeometryType_Edge;
80   myShape           = BRepLib_MakeEdge (theCircle);
81   myAnchorPoint     = ElCLib::Value (0, myCircle);
82   myIsGeometryValid = IsValidCircle (myCircle);
83
84   if (myIsGeometryValid)
85   {
86     ComputePlane();
87   }
88
89   SetToUpdate();
90 }
91
92 //=======================================================================
93 //function : SetMeasuredGeometry
94 //purpose  : 
95 //=======================================================================
96 void AIS_RadiusDimension::SetMeasuredGeometry (const gp_Circ& theCircle,
97                                                const gp_Pnt&  theAnchorPoint)
98 {
99   myCircle          = theCircle;
100   myGeometryType    = GeometryType_Edge;
101   myShape           = BRepLib_MakeEdge (theCircle);
102   myAnchorPoint     = theAnchorPoint;
103   myIsGeometryValid = IsValidCircle (myCircle) && IsValidAnchor (myCircle, theAnchorPoint);
104
105   if (myIsGeometryValid)
106   {
107     ComputePlane();
108   }
109
110   SetToUpdate();
111 }
112
113 //=======================================================================
114 //function : SetMeasuredGeometry
115 //purpose  : 
116 //=======================================================================
117 void AIS_RadiusDimension::SetMeasuredGeometry (const TopoDS_Shape& theShape)
118 {
119   Standard_Boolean isClosed = Standard_False;
120   myShape                   = theShape;
121   myGeometryType            = GeometryType_UndefShapes;
122   myIsGeometryValid         = InitCircularDimension (theShape, myCircle, myAnchorPoint, isClosed) 
123                               && IsValidCircle (myCircle);
124
125   if (myIsGeometryValid)
126   {
127     ComputePlane();
128   }
129
130   SetToUpdate();
131 }
132
133 //=======================================================================
134 //function : CheckPlane
135 //purpose  : 
136 //=======================================================================
137 Standard_Boolean AIS_RadiusDimension::CheckPlane (const gp_Pln& thePlane) const
138 {
139   // Check if anchor point and circle center point belong to plane.
140   if (!thePlane.Contains (myAnchorPoint, Precision::Confusion()) &&
141       !thePlane.Contains (myCircle.Location(), Precision::Confusion()))
142   {
143     return Standard_False;
144   }
145
146   return Standard_True;
147 }
148
149 //=======================================================================
150 //function : ComputePlane
151 //purpose  : 
152 //=======================================================================
153 void AIS_RadiusDimension::ComputePlane()
154 {
155   if (!myIsGeometryValid)
156   {
157     return;
158   }
159
160   gp_Dir aDimensionX = gce_MakeDir (myAnchorPoint, myCircle.Location());
161
162   myPlane = gp_Pln (gp_Ax3 (myCircle.Location(),
163                             myCircle.Axis().Direction(),
164                             aDimensionX));
165 }
166
167 //=======================================================================
168 //function : GetModelUnits
169 //purpose  :
170 //=======================================================================
171 const TCollection_AsciiString& AIS_RadiusDimension::GetModelUnits() const
172 {
173   return myDrawer->DimLengthModelUnits();
174 }
175
176 //=======================================================================
177 //function : GetDisplayUnits
178 //purpose  :
179 //=======================================================================
180 const TCollection_AsciiString& AIS_RadiusDimension::GetDisplayUnits() const
181 {
182   return myDrawer->DimLengthDisplayUnits();
183 }
184
185 //=======================================================================
186 //function : SetModelUnits
187 //purpose  :
188 //=======================================================================
189 void AIS_RadiusDimension::SetModelUnits (const TCollection_AsciiString& theUnits)
190 {
191   myDrawer->SetDimLengthModelUnits (theUnits);
192 }
193
194 //=======================================================================
195 //function : SetDisplayUnits
196 //purpose  :
197 //=======================================================================
198 void AIS_RadiusDimension::SetDisplayUnits (const TCollection_AsciiString& theUnits)
199 {
200   myDrawer->SetDimLengthDisplayUnits(theUnits);
201 }
202
203 //=======================================================================
204 //function : ComputeValue
205 //purpose  : 
206 //=======================================================================
207 Standard_Real AIS_RadiusDimension::ComputeValue() const
208 {
209   if (!IsValid())
210   {
211     return 0.0;
212   }
213
214   return myCircle.Radius();
215 }
216
217 //=======================================================================
218 //function : Compute
219 //purpose  : 
220 //=======================================================================
221 void AIS_RadiusDimension::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePM*/,
222                                    const Handle(Prs3d_Presentation)& thePresentation,
223                                    const Standard_Integer theMode)
224 {
225   mySelectionGeom.Clear (theMode);
226
227   if (!IsValid())
228   {
229     return;
230   }
231
232   DrawLinearDimension (thePresentation, theMode, myAnchorPoint, myCircle.Location(), Standard_True);
233 }
234
235 //=======================================================================
236 //function : IsValidCircle
237 //purpose  : 
238 //=======================================================================
239 Standard_Boolean AIS_RadiusDimension::IsValidCircle (const gp_Circ& theCircle) const
240 {
241   return theCircle.Radius() > Precision::Confusion();
242 }
243
244 //=======================================================================
245 //function : IsValidAnchor
246 //purpose  : 
247 //=======================================================================
248 Standard_Boolean AIS_RadiusDimension::IsValidAnchor (const gp_Circ& theCircle,
249                                                      const gp_Pnt& theAnchor) const
250 {
251   gp_Pln aCirclePlane (theCircle.Location(), theCircle.Axis().Direction());
252   Standard_Real anAnchorDist = theAnchor.Distance (theCircle.Location());
253   Standard_Real aRadius      = myCircle.Radius();
254
255   return Abs (anAnchorDist - aRadius) > Precision::Confusion()
256       && aCirclePlane.Contains (theAnchor, Precision::Confusion());
257 }
258
259 //=======================================================================
260 //function : GetTextPosition
261 //purpose  : 
262 //=======================================================================
263 const gp_Pnt AIS_RadiusDimension::GetTextPosition() const
264 {
265   if (IsTextPositionCustom())
266   {
267     return myFixedTextPosition;
268   }
269
270   // Counts text position according to the dimension parameters
271   return GetTextPositionForLinear (myAnchorPoint, myCircle.Location(), Standard_True);
272 }
273
274 //=======================================================================
275 //function : GetTextPosition
276 //purpose  : 
277 //=======================================================================
278 void AIS_RadiusDimension::SetTextPosition (const gp_Pnt& theTextPos)
279 {
280   if (!myIsGeometryValid)
281   {
282     return;
283   }
284
285   myIsTextPositionFixed = Standard_True;
286   myFixedTextPosition = theTextPos;
287
288   SetToUpdate();
289 }