0032306: Draw Harness, ViewerTest - move window message processing to TKService
[occt.git] / src / AIS / AIS_Line.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_Line.hxx>
18
19 #include <AIS_GraphicTool.hxx>
20 #include <Aspect_TypeOfLine.hxx>
21 #include <GC_MakeSegment.hxx>
22 #include <Geom_Line.hxx>
23 #include <Geom_Point.hxx>
24 #include <GeomAdaptor_Curve.hxx>
25 #include <Graphic3d_AspectLine3d.hxx>
26 #include <Graphic3d_Structure.hxx>
27 #include <Precision.hxx>
28 #include <Prs3d_Drawer.hxx>
29 #include <Prs3d_LineAspect.hxx>
30 #include <Prs3d_Presentation.hxx>
31 #include <Quantity_Color.hxx>
32 #include <Select3D_SensitiveSegment.hxx>
33 #include <SelectMgr_EntityOwner.hxx>
34 #include <SelectMgr_Selection.hxx>
35 #include <Standard_Type.hxx>
36 #include <StdPrs_Curve.hxx>
37 #include <TColgp_Array1OfPnt.hxx>
38 #include <UnitsAPI.hxx>
39
40 IMPLEMENT_STANDARD_RTTIEXT(AIS_Line,AIS_InteractiveObject)
41
42 //=======================================================================
43 //function : AIS_Line
44 //purpose  : 
45 //=======================================================================
46 AIS_Line::AIS_Line(const Handle(Geom_Line)& aComponent):
47 myComponent (aComponent),
48 myLineIsSegment(Standard_False)
49 {
50   SetInfiniteState();
51 }
52
53 //=======================================================================
54 //function : AIS_Line
55 //purpose  : 
56 //=======================================================================
57 AIS_Line::AIS_Line(const Handle(Geom_Point)& aStartPoint,
58                    const Handle(Geom_Point)& aEndPoint):
59 myStartPoint(aStartPoint),
60 myEndPoint(aEndPoint),
61 myLineIsSegment(Standard_True)
62 {}
63
64 //=======================================================================
65 //function : Compute
66 //purpose  : 
67 //=======================================================================
68 void AIS_Line::Compute (const Handle(PrsMgr_PresentationManager)&,
69                         const Handle(Prs3d_Presentation)& thePrs,
70                         const Standard_Integer )
71 {
72   thePrs->SetDisplayPriority (5);
73
74   if (!myLineIsSegment) { ComputeInfiniteLine (thePrs); }
75   else { ComputeSegmentLine (thePrs); }
76 }
77
78 //=======================================================================
79 //function : ComputeSelection
80 //purpose  : 
81 //=======================================================================
82
83 void AIS_Line::ComputeSelection(const Handle(SelectMgr_Selection)& theSelection,
84                                 const Standard_Integer             theMode)
85 {
86   // Do not support selection modes different from 0 currently
87   if (theMode)
88     return;
89
90   if (!myLineIsSegment)
91   {
92     ComputeInfiniteLineSelection(theSelection);
93   }
94   else
95   {
96     ComputeSegmentLineSelection(theSelection);
97   }
98 }
99
100 //=======================================================================
101 //function : replaceWithNewLineAspect
102 //purpose  :
103 //=======================================================================
104 void AIS_Line::replaceWithNewLineAspect (const Handle(Prs3d_LineAspect)& theAspect)
105 {
106   if (!myDrawer->HasLink())
107   {
108     myDrawer->SetLineAspect (theAspect);
109     return;
110   }
111
112   const Handle(Graphic3d_Aspects)& anAspectOld = myDrawer->LineAspect()->Aspect();
113   const Handle(Graphic3d_Aspects)& anAspectNew = !theAspect.IsNull() ? theAspect->Aspect() : myDrawer->Link()->LineAspect()->Aspect();
114   if (anAspectNew != anAspectOld)
115   {
116     myDrawer->SetLineAspect (theAspect);
117     Graphic3d_MapOfAspectsToAspects aReplaceMap;
118     aReplaceMap.Bind (anAspectOld, anAspectNew);
119     replaceAspects (aReplaceMap);
120   }
121 }
122
123 //=======================================================================
124 //function : SetColor
125 //purpose  :
126 //=======================================================================
127 void AIS_Line::SetColor(const Quantity_Color &aCol)
128 {
129   hasOwnColor=Standard_True;
130   myDrawer->SetColor (aCol);
131
132   Standard_Real WW = HasWidth()? myOwnWidth:
133                                  myDrawer->HasLink() ?
134                                  AIS_GraphicTool::GetLineWidth (myDrawer->Link(), AIS_TOA_Line) : 1.;
135
136   if (!myDrawer->HasOwnLineAspect())
137   {
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 //=======================================================================
149 //function : UnsetColor 
150 //purpose  : 
151 //=======================================================================
152 void AIS_Line::UnsetColor()
153 {
154   hasOwnColor = Standard_False;
155
156   if (!HasWidth())
157   {
158     replaceWithNewLineAspect (Handle(Prs3d_LineAspect)());
159   }
160   else
161   {
162     Quantity_Color CC = Quantity_NOC_YELLOW;
163     if( HasColor() ) CC = myDrawer->Color();
164     else if (myDrawer->HasLink()) AIS_GraphicTool::GetLineColor (myDrawer->Link(), AIS_TOA_Line, CC);
165     myDrawer->LineAspect()->SetColor(CC);
166     myDrawer->SetColor (CC);
167     SynchronizeAspects();
168   }
169 }
170
171 //=======================================================================
172 //function : SetWidth 
173 //purpose  : 
174 //=======================================================================
175 void AIS_Line::SetWidth(const Standard_Real aValue)
176 {
177   myOwnWidth = (Standard_ShortReal )aValue;
178
179   if (!myDrawer->HasOwnLineAspect())
180   {
181     Quantity_Color CC = Quantity_NOC_YELLOW;
182     if( HasColor() ) CC = myDrawer->Color();
183     else if(myDrawer->HasLink()) AIS_GraphicTool::GetLineColor (myDrawer->Link(), AIS_TOA_Line, CC);
184     replaceWithNewLineAspect (new Prs3d_LineAspect (CC, Aspect_TOL_SOLID, aValue));
185   }
186   else
187   {
188     myDrawer->LineAspect()->SetWidth (aValue);
189     SynchronizeAspects();
190   }
191 }
192
193
194 //=======================================================================
195 //function : UnsetWidth 
196 //purpose  : 
197 //=======================================================================
198 void AIS_Line::UnsetWidth()
199 {
200   if (!HasColor())
201   {
202     replaceWithNewLineAspect (Handle(Prs3d_LineAspect)());
203   }
204   else
205   {
206    Standard_ShortReal WW = myDrawer->HasLink() ? (Standard_ShortReal )AIS_GraphicTool::GetLineWidth (myDrawer->Link(), AIS_TOA_Line) : 1.0f;
207    myDrawer->LineAspect()->SetWidth (WW);
208    myOwnWidth = WW;
209    SynchronizeAspects();
210   }
211 }
212
213 //=======================================================================
214 //function : ComputeInfiniteLine
215 //purpose  : 
216 //=======================================================================
217 void AIS_Line::ComputeInfiniteLine( const Handle(Prs3d_Presentation)& aPresentation)
218 {
219   GeomAdaptor_Curve curv(myComponent);
220   StdPrs_Curve::Add(aPresentation,curv,myDrawer);
221
222   //pas de prise en compte lors du FITALL
223   aPresentation->SetInfiniteState (Standard_True);
224 }
225
226 //=======================================================================
227 //function : ComputeSegmentLine
228 //purpose  : 
229 //=======================================================================
230 void AIS_Line::ComputeSegmentLine( const Handle(Prs3d_Presentation)& aPresentation)
231 {
232   gp_Pnt P1 = myStartPoint->Pnt();
233   gp_Pnt P2 = myEndPoint->Pnt();
234   
235   myComponent = new Geom_Line(P1,gp_Dir(P2.XYZ()-P1.XYZ()));
236
237   Standard_Real dist = P1.Distance(P2);
238   GeomAdaptor_Curve curv(myComponent,0.,dist);
239   StdPrs_Curve::Add(aPresentation,curv,myDrawer);
240 }
241
242
243 //=======================================================================
244 //function : ComputeInfiniteLineSelection
245 //purpose  : 
246 //=======================================================================
247
248 void AIS_Line::ComputeInfiniteLineSelection(const Handle(SelectMgr_Selection)& aSelection)
249 {
250
251 /*  // on calcule les points min max a partir desquels on cree un segment sensible...
252   GeomAdaptor_Curve curv(myComponent);
253   gp_Pnt P1,P2;
254   FindLimits(curv,myDrawer->MaximalParameterValue(),P1,P2);
255 */   
256   const gp_Dir& thedir = myComponent->Position().Direction();
257   const gp_Pnt& loc = myComponent->Position().Location();
258   const gp_XYZ& dir_xyz = thedir.XYZ();
259   const gp_XYZ& loc_xyz = loc.XYZ();
260 //POP  Standard_Real aLength = UnitsAPI::CurrentToLS (250000. ,"LENGTH");
261   Standard_Real aLength = UnitsAPI::AnyToLS (250000. ,"mm");
262   gp_Pnt P1 = loc_xyz + aLength*dir_xyz;
263   gp_Pnt P2 = loc_xyz - aLength*dir_xyz;
264   Handle(SelectMgr_EntityOwner) eown = new SelectMgr_EntityOwner(this,5);
265   Handle(Select3D_SensitiveSegment) seg = new Select3D_SensitiveSegment(eown,P1,P2);
266   aSelection->Add(seg);
267 }
268 //=======================================================================
269 //function : ComputeSegmentLineSelection
270 //purpose  : 
271 //=======================================================================
272
273 void AIS_Line::ComputeSegmentLineSelection(const Handle(SelectMgr_Selection)& aSelection)
274 {
275
276
277   Handle(SelectMgr_EntityOwner) eown = new SelectMgr_EntityOwner(this,5);
278   Handle(Select3D_SensitiveSegment) seg = new Select3D_SensitiveSegment(eown,
279                                                                         myStartPoint->Pnt(),
280                                                                         myEndPoint->Pnt());
281   aSelection->Add(seg);
282 }