0031431: Visualization, PrsMgr_PresentableObject - simplify HLR computing interface
[occt.git] / src / AIS / AIS_Circle.cxx
1 // Created on: 1997-01-21
2 // Created by: Prestataire Christiane ARMAND
3 // Copyright (c) 1997-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_Circle.hxx>
18
19 #include <AIS_GraphicTool.hxx>
20 #include <Aspect_TypeOfLine.hxx>
21 #include <GC_MakeArcOfCircle.hxx>
22 #include <Geom_Circle.hxx>
23 #include <Geom_Transformation.hxx>
24 #include <GeomAdaptor_Curve.hxx>
25 #include <Graphic3d_AspectLine3d.hxx>
26 #include <Graphic3d_Structure.hxx>
27 #include <Prs3d_Drawer.hxx>
28 #include <Prs3d_LineAspect.hxx>
29 #include <Prs3d_Presentation.hxx>
30 #include <Quantity_Color.hxx>
31 #include <Select3D_SensitiveCircle.hxx>
32 #include <SelectMgr_EntityOwner.hxx>
33 #include <SelectMgr_Selection.hxx>
34 #include <Standard_Type.hxx>
35 #include <StdPrs_DeflectionCurve.hxx>
36 #include <TColgp_Array1OfPnt.hxx>
37 #include <TopoDS.hxx>
38
39 IMPLEMENT_STANDARD_RTTIEXT(AIS_Circle,AIS_InteractiveObject)
40
41 //=======================================================================
42 //function : AIS_Circle
43 //purpose  : 
44 //=======================================================================
45 AIS_Circle::AIS_Circle(const Handle(Geom_Circle)& aComponent):
46 AIS_InteractiveObject(PrsMgr_TOP_AllView),
47 myComponent(aComponent),
48 myUStart(0.),
49 myUEnd(2*M_PI),
50 myCircleIsArc(Standard_False),
51 myIsFilledCircleSens (Standard_False)
52 {
53 }
54
55 //=======================================================================
56 //function : AIS_Circle
57 //purpose  : 
58 //=======================================================================
59 AIS_Circle::AIS_Circle(const Handle(Geom_Circle)& theComponent,
60                        const Standard_Real theUStart,
61                        const Standard_Real theUEnd,
62                        const Standard_Boolean theIsFilledCircleSens)
63 : AIS_InteractiveObject(PrsMgr_TOP_AllView),
64   myComponent (theComponent),
65   myUStart (theUStart),
66   myUEnd (theUEnd),
67   myCircleIsArc (Standard_True),
68   myIsFilledCircleSens (theIsFilledCircleSens)
69 {
70 }
71
72 //=======================================================================
73 //function : Compute
74 //purpose  : 
75 //=======================================================================
76 void AIS_Circle::Compute(const Handle(PrsMgr_PresentationManager3d)& /*aPresentationManager*/,
77                          const Handle(Prs3d_Presentation)& aPresentation, 
78                          const Standard_Integer /*aMode*/)
79 {
80   aPresentation->SetDisplayPriority(5);
81
82   if (myCircleIsArc) ComputeArc(aPresentation);
83   else ComputeCircle(aPresentation);
84
85 }
86
87 //=======================================================================
88 //function : ComputeSelection
89 //purpose  : 
90 //=======================================================================
91
92 void AIS_Circle::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
93                                   const Standard_Integer /*aMode*/)
94 {
95
96   if (myCircleIsArc) ComputeArcSelection(aSelection);
97   else ComputeCircleSelection(aSelection);
98
99 }
100
101 //=======================================================================
102 //function : replaceWithNewLineAspect
103 //purpose  :
104 //=======================================================================
105 void AIS_Circle::replaceWithNewLineAspect (const Handle(Prs3d_LineAspect)& theAspect)
106 {
107   if (!myDrawer->HasLink())
108   {
109     myDrawer->SetLineAspect (theAspect);
110     return;
111   }
112
113   const Handle(Graphic3d_AspectLine3d) anAspectOld = myDrawer->LineAspect()->Aspect();
114   const Handle(Graphic3d_AspectLine3d) anAspectNew = !theAspect.IsNull() ? theAspect->Aspect() : myDrawer->Link()->LineAspect()->Aspect();
115   if (anAspectNew != anAspectOld)
116   {
117     myDrawer->SetLineAspect (theAspect);
118     Graphic3d_MapOfAspectsToAspects aReplaceMap;
119     aReplaceMap.Bind (anAspectOld, anAspectNew);
120     replaceAspects (aReplaceMap);
121   }
122 }
123
124 //=======================================================================
125 //function : SetColor
126 //purpose  : 
127 //=======================================================================
128
129 void AIS_Circle::SetColor(const Quantity_Color &aCol)
130 {
131   hasOwnColor=Standard_True;
132   myDrawer->SetColor (aCol);
133
134   if (!myDrawer->HasOwnLineAspect())
135   {
136     Standard_Real WW = HasWidth() ? myOwnWidth :
137                                     myDrawer->HasLink() ?
138                                     AIS_GraphicTool::GetLineWidth (myDrawer->Link(), AIS_TOA_Line) :
139                                     1.;
140     replaceWithNewLineAspect (new Prs3d_LineAspect (aCol, Aspect_TOL_SOLID, WW));
141   }
142   else
143   {
144     myDrawer->LineAspect()->SetColor(aCol);
145     SynchronizeAspects();
146   }
147 }
148
149 //=======================================================================
150 //function : SetWidth 
151 //purpose  : 
152 //=======================================================================
153 void AIS_Circle::SetWidth(const Standard_Real aValue)
154 {
155   myOwnWidth = (Standard_ShortReal )aValue;
156
157   if (!myDrawer->HasOwnLineAspect())
158   {
159     Quantity_Color CC = Quantity_NOC_YELLOW;
160     if( HasColor() ) CC = myDrawer->Color();
161     else if(myDrawer->HasLink()) AIS_GraphicTool::GetLineColor (myDrawer->Link(), AIS_TOA_Line, CC);
162     replaceWithNewLineAspect (new Prs3d_LineAspect (CC, Aspect_TOL_SOLID, aValue));
163   }
164   else
165   {
166     myDrawer->LineAspect()->SetWidth(aValue);
167     SynchronizeAspects();
168   }
169 }
170
171
172 //=======================================================================
173 //function : UnsetColor 
174 //purpose  : 
175 //=======================================================================
176 void AIS_Circle::UnsetColor()
177 {
178   hasOwnColor = Standard_False;
179
180   if (!HasWidth())
181   {
182     replaceWithNewLineAspect (Handle(Prs3d_LineAspect)());
183   }
184   else
185   {
186     Quantity_Color CC = Quantity_NOC_YELLOW;
187     if( HasColor() ) CC = myDrawer->Color();
188     else if (myDrawer->HasLink()) AIS_GraphicTool::GetLineColor(myDrawer->Link(),AIS_TOA_Line,CC);
189     myDrawer->LineAspect()->SetColor(CC);
190     myDrawer->SetColor (CC);
191     SynchronizeAspects();
192   }
193 }
194
195 //=======================================================================
196 //function : UnsetWidth 
197 //purpose  : 
198 //=======================================================================
199 void AIS_Circle::UnsetWidth()
200 {
201   if (!HasColor())
202   {
203     replaceWithNewLineAspect (Handle(Prs3d_LineAspect)());
204   }
205   else
206   {
207    Standard_ShortReal WW = myDrawer->HasLink() ? (Standard_ShortReal )AIS_GraphicTool::GetLineWidth(myDrawer->Link(),AIS_TOA_Line) : 1.0f;
208    myDrawer->LineAspect()->SetWidth(WW);
209    myOwnWidth = WW;
210   }
211 }
212
213 //=======================================================================
214 //function : ComputeCircle
215 //purpose  : 
216 //=======================================================================
217 void AIS_Circle::ComputeCircle( const Handle(Prs3d_Presentation)& aPresentation)
218 {
219
220   GeomAdaptor_Curve curv(myComponent);
221   Standard_Real prevdev = myDrawer->DeviationCoefficient();
222   myDrawer->SetDeviationCoefficient(1.e-5);
223   StdPrs_DeflectionCurve::Add(aPresentation,curv,myDrawer);
224   myDrawer->SetDeviationCoefficient(prevdev);
225
226 }
227
228 //=======================================================================
229 //function : ComputeArc
230
231 //purpose  : 
232 //=======================================================================
233 void AIS_Circle::ComputeArc( const Handle(Prs3d_Presentation)& aPresentation)
234 {
235   GeomAdaptor_Curve curv(myComponent,myUStart,myUEnd);
236   Standard_Real prevdev = myDrawer->DeviationCoefficient();
237   myDrawer->SetDeviationCoefficient(1.e-5);
238   StdPrs_DeflectionCurve::Add(aPresentation,curv,myDrawer);
239   myDrawer->SetDeviationCoefficient(prevdev);
240 }
241
242 //=======================================================================
243 //function : ComputeCircleSelection
244 //purpose  : 
245 //=======================================================================
246
247 void AIS_Circle::ComputeCircleSelection(const Handle(SelectMgr_Selection)& aSelection)
248 {
249   Handle(SelectMgr_EntityOwner) eown = new SelectMgr_EntityOwner(this);
250   Handle(Select3D_SensitiveCircle) seg = new Select3D_SensitiveCircle (eown,
251                                                                        myComponent,
252                                                                        myIsFilledCircleSens);
253   aSelection->Add(seg);
254 }
255 //=======================================================================
256 //function : ComputeArcSelection
257 //purpose  : 
258 //=======================================================================
259
260 void AIS_Circle::ComputeArcSelection(const Handle(SelectMgr_Selection)& aSelection)
261 {
262
263
264   Handle(SelectMgr_EntityOwner) eown = new SelectMgr_EntityOwner(this);
265   Handle(Select3D_SensitiveCircle) seg = new Select3D_SensitiveCircle (eown,
266                                                                        myComponent,
267                                                                        myUStart, myUEnd,
268                                                                        myIsFilledCircleSens);
269   aSelection->Add(seg);
270 }