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