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