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