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