0030687: Visualization - remove redundant interfaces SelectBasics_EntityOwner and...
[occt.git] / src / AIS / AIS_Axis.cxx
1 // Created on: 1995-08-09
2 // Created by: Arnaud BOUZY/Odile Olivier
3 // Copyright (c) 1995-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_Axis.hxx>
18
19 #include <Aspect_TypeOfLine.hxx>
20 #include <DsgPrs_XYZAxisPresentation.hxx>
21 #include <Geom_Axis1Placement.hxx>
22 #include <Geom_Axis2Placement.hxx>
23 #include <Geom_Line.hxx>
24 #include <Geom_Transformation.hxx>
25 #include <GeomAdaptor_Curve.hxx>
26 #include <gp_Ax1.hxx>
27 #include <gp_Ax2.hxx>
28 #include <Graphic3d_AspectLine3d.hxx>
29 #include <Graphic3d_Structure.hxx>
30 #include <Prs3d_DatumAspect.hxx>
31 #include <Prs3d_Drawer.hxx>
32 #include <Prs3d_LineAspect.hxx>
33 #include <Prs3d_Presentation.hxx>
34 #include <Prs3d_Projector.hxx>
35 #include <Quantity_Color.hxx>
36 #include <Select3D_SensitiveSegment.hxx>
37 #include <SelectMgr_EntityOwner.hxx>
38 #include <SelectMgr_Selection.hxx>
39 #include <StdPrs_Curve.hxx>
40 #include <TColgp_Array1OfPnt.hxx>
41 #include <TopoDS.hxx>
42 #include <UnitsAPI.hxx>
43
44 IMPLEMENT_STANDARD_RTTIEXT(AIS_Axis,AIS_InteractiveObject)
45
46 //=======================================================================
47 //function : AIS_Axis
48 //purpose  :
49 //=======================================================================
50 AIS_Axis::AIS_Axis(const Handle(Geom_Line)& aComponent):
51 myComponent(aComponent),
52 myTypeOfAxis(AIS_TOAX_Unknown),
53 myIsXYZAxis(Standard_False)
54 {
55   myDrawer->SetLineAspect(new Prs3d_LineAspect
56      (Quantity_NOC_RED,Aspect_TOL_DOTDASH,1.));
57   SetInfiniteState();
58
59   gp_Dir thedir = myComponent->Position().Direction();
60   gp_Pnt loc = myComponent->Position().Location();
61 //POP  Standard_Real aLength = UnitsAPI::CurrentToLS (250000. ,"LENGTH");
62   Standard_Real aLength = UnitsAPI::AnyToLS(250000., "mm");
63   myPfirst = loc.XYZ() + aLength*thedir.XYZ();
64   myPlast = loc.XYZ() - aLength*thedir.XYZ();
65 }
66
67 //=======================================================================
68 //function : AIS_Axis
69 //purpose  :  Xaxis, YAxis, ZAxis
70 //=======================================================================
71 AIS_Axis::AIS_Axis(const Handle(Geom_Axis2Placement)& aComponent,
72                    const AIS_TypeOfAxis anAxisType):
73 myAx2(aComponent),
74 myTypeOfAxis(anAxisType),
75 myIsXYZAxis(Standard_True)
76 {
77   Handle (Prs3d_DatumAspect) DA = new Prs3d_DatumAspect();
78 //POP  Standard_Real aLength = UnitsAPI::CurrentToLS (100. ,"LENGTH"); 
79   Standard_Real aLength;
80   try {
81     aLength = UnitsAPI::AnyToLS(100. ,"mm");
82   } catch (Standard_Failure const&) {
83     aLength = 0.1;
84   }
85   DA->SetAxisLength(aLength,aLength,aLength);
86   Quantity_Color col (Quantity_NOC_TURQUOISE);
87   DA->LineAspect(Prs3d_DP_XAxis)->SetColor(col);
88   DA->LineAspect(Prs3d_DP_YAxis)->SetColor(col);
89   DA->LineAspect(Prs3d_DP_ZAxis)->SetColor(col);
90   myDrawer->SetDatumAspect(DA); 
91   
92   ComputeFields();
93 }
94
95 //=======================================================================
96 //function : AIS_Axis
97 //purpose  : 
98 //=======================================================================
99 AIS_Axis::AIS_Axis(const Handle(Geom_Axis1Placement)& anAxis)
100 :myComponent(new Geom_Line(anAxis->Ax1())),
101  myTypeOfAxis(AIS_TOAX_Unknown),
102  myIsXYZAxis(Standard_False)
103 {
104   myDrawer->SetLineAspect(new Prs3d_LineAspect(Quantity_NOC_RED,Aspect_TOL_DOTDASH,1.));
105   SetInfiniteState();
106
107   gp_Dir thedir = myComponent->Position().Direction();
108   gp_Pnt loc = myComponent->Position().Location();
109 //POP  Standard_Real aLength = UnitsAPI::CurrentToLS (250000. ,"LENGTH");
110   Standard_Real aLength = UnitsAPI::AnyToLS(250000. ,"mm"); 
111   myPfirst = loc.XYZ() + aLength*thedir.XYZ();
112   myPlast = loc.XYZ() - aLength*thedir.XYZ();
113 }
114
115
116 //=======================================================================
117 //function : SetComponent
118 //purpose  : 
119 //=======================================================================
120
121 void AIS_Axis::SetComponent(const Handle(Geom_Line)& aComponent)
122 {
123   myComponent = aComponent;
124   myTypeOfAxis = AIS_TOAX_Unknown;
125   myIsXYZAxis = Standard_False;
126   SetInfiniteState();
127
128   gp_Dir thedir = myComponent->Position().Direction();
129   gp_Pnt loc = myComponent->Position().Location();
130 //POP  Standard_Real aLength = UnitsAPI::CurrentToLS (250000. ,"LENGTH");
131   Standard_Real aLength = UnitsAPI::AnyToLS(250000. ,"mm");
132   myPfirst = loc.XYZ() + aLength*thedir.XYZ();
133   myPlast = loc.XYZ() - aLength*thedir.XYZ();
134 }
135
136
137
138 //=======================================================================
139 //function : SetAxis2Placement
140 //purpose  : 
141 //=======================================================================
142
143 void AIS_Axis::SetAxis2Placement(const Handle(Geom_Axis2Placement)& aComponent,
144                                  const AIS_TypeOfAxis anAxisType)
145 {
146   myAx2 = aComponent;
147   myTypeOfAxis = anAxisType;
148   myIsXYZAxis = Standard_True;
149   ComputeFields();
150 }
151
152 //=======================================================================
153 //function : SetAxis1Placement
154 //purpose  : 
155 //=======================================================================
156
157 void AIS_Axis::SetAxis1Placement(const Handle(Geom_Axis1Placement)& anAxis)
158 {
159   SetComponent(new Geom_Line(anAxis->Ax1()));
160 }
161
162 //=======================================================================
163 //function : Compute
164 //purpose  : 
165 //=======================================================================
166 void AIS_Axis::Compute(const Handle(PrsMgr_PresentationManager3d)&,
167                        const Handle(Prs3d_Presentation)& aPresentation, 
168                        const Standard_Integer)
169 {
170   aPresentation->SetInfiniteState (myInfiniteState);
171
172   aPresentation->SetDisplayPriority(5);
173   if (!myIsXYZAxis ){
174     GeomAdaptor_Curve curv(myComponent);
175     StdPrs_Curve::Add(aPresentation,curv,myDrawer);
176   }
177   else
178   {
179     DsgPrs_XYZAxisPresentation::Add (aPresentation,myLineAspect,myDir,myVal,
180                                      myDrawer->DatumAspect()->ToDrawLabels() ? myText : "",
181                                      myPfirst, myPlast);
182   }
183
184 }
185
186 void AIS_Axis::Compute(const Handle(Prs3d_Projector)& aProjector, const Handle(Geom_Transformation)& aTransformation, const Handle(Prs3d_Presentation)& aPresentation)
187 {
188 // throw Standard_NotImplemented("AIS_Axis::Compute(const Handle(Prs3d_Projector)&, const Handle(Geom_Transformation)&, const Handle(Prs3d_Presentation)&)");
189   PrsMgr_PresentableObject::Compute( aProjector , aTransformation , aPresentation ) ;
190 }
191
192 //=======================================================================
193 //function : ComputeSelection
194 //purpose  : 
195 //=======================================================================
196
197 void AIS_Axis::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
198                                 const Standard_Integer)
199 {
200   Handle(SelectMgr_EntityOwner) eown = new SelectMgr_EntityOwner (this, 3);
201   Handle(Select3D_SensitiveSegment) seg = new Select3D_SensitiveSegment(eown,
202                                                                         myPfirst,
203                                                                         myPlast);
204   aSelection->Add(seg);
205 }
206
207 //=======================================================================
208 //function : SetColor
209 //purpose  :
210 //=======================================================================
211 void AIS_Axis::SetColor(const Quantity_Color &aCol)
212 {
213   hasOwnColor=Standard_True;
214   myDrawer->SetColor (aCol);
215   myDrawer->LineAspect()->SetColor(aCol);
216   
217   const Handle(Prs3d_DatumAspect)& DA = myDrawer->DatumAspect();
218   DA->LineAspect(Prs3d_DP_XAxis)->SetColor(aCol);
219   DA->LineAspect(Prs3d_DP_YAxis)->SetColor(aCol);
220   DA->LineAspect(Prs3d_DP_ZAxis)->SetColor(aCol);
221   SynchronizeAspects();
222 }
223
224 //=======================================================================
225 //function : SetWidth 
226 //purpose  : 
227 //=======================================================================
228 void AIS_Axis::SetWidth(const Standard_Real aValue)
229 {
230   if(aValue<0.0) return;
231   if(aValue==0) UnsetWidth();
232   
233   myDrawer->LineAspect()->SetWidth(aValue);
234   
235   const Handle(Prs3d_DatumAspect)& DA = myDrawer->DatumAspect();
236   DA->LineAspect(Prs3d_DP_XAxis)->SetWidth(aValue);
237   DA->LineAspect(Prs3d_DP_YAxis)->SetWidth(aValue);
238   DA->LineAspect(Prs3d_DP_ZAxis)->SetWidth(aValue);
239   SynchronizeAspects();
240 }
241
242
243 //=======================================================================
244 //function : Compute
245 //purpose  : to avoid warning
246 //=======================================================================
247 void AIS_Axis::Compute(const Handle(Prs3d_Projector)&, 
248                        const Handle(Prs3d_Presentation)&)
249 {
250 }
251
252 //=======================================================================
253 //function : ComputeFields
254 //purpose  : 
255 //=======================================================================
256 void AIS_Axis::ComputeFields()
257 {
258   if (myIsXYZAxis){
259     // calcul de myPFirst,myPlast
260     Handle(Prs3d_DatumAspect) DA = myDrawer->DatumAspect();
261     gp_Ax2 anAxis = myAx2->Ax2();
262     const gp_Pnt& Orig = anAxis.Location();
263     const gp_Dir& oX   = anAxis.XDirection();
264     const gp_Dir& oY   = anAxis.YDirection();
265     const gp_Dir& oZ   = anAxis.Direction();
266     Standard_Real xo,yo,zo,x = 0.,y = 0.,z = 0.;
267     Orig.Coord(xo,yo,zo);
268     myPfirst.SetCoord(xo,yo,zo);
269     
270     switch (myTypeOfAxis) {
271     case AIS_TOAX_XAxis:
272       {
273         oX.Coord(x,y,z);
274     myVal = DA->AxisLength(Prs3d_DP_XAxis);
275         myDir = oX;
276         myLineAspect = DA->LineAspect(Prs3d_DP_XAxis);
277         myText = Standard_CString ("X");
278         break;
279       }
280     case AIS_TOAX_YAxis:
281       {
282         oY.Coord(x,y,z);
283         myVal = DA->AxisLength(Prs3d_DP_YAxis);
284         myDir = oY;
285         myLineAspect = DA->LineAspect(Prs3d_DP_YAxis);
286         myText = Standard_CString ("Y");
287         break;
288       }
289     case AIS_TOAX_ZAxis:
290       {
291         oZ.Coord(x,y,z); 
292         myVal = DA->AxisLength(Prs3d_DP_ZAxis);
293         myDir = oZ;
294         myLineAspect = DA->LineAspect(Prs3d_DP_ZAxis);
295         myText = Standard_CString ("Z");
296         break;
297       }
298      default:
299       break;
300     }
301     
302     myComponent = new Geom_Line(Orig,myDir);
303     x = xo + x*myVal;   y = yo + y*myVal;  z = zo + z*myVal;
304     myPlast.SetCoord(x,y,z);
305     SetInfiniteState();
306   }
307 }
308
309 //=======================================================================
310 //function : AcceptDisplayMode
311 //purpose  : 
312 //=======================================================================
313
314  Standard_Boolean  AIS_Axis::
315 AcceptDisplayMode(const Standard_Integer aMode) const
316 {return aMode == 0;}
317
318 //=======================================================================
319 //function : UnsetColor
320 //purpose  : 
321 //=======================================================================
322 void AIS_Axis::UnsetColor()
323 {
324   myDrawer->LineAspect()->SetColor(Quantity_NOC_RED);
325   hasOwnColor = Standard_False;
326
327   myDrawer->DatumAspect()->LineAspect(Prs3d_DP_XAxis)->SetColor(Quantity_NOC_TURQUOISE);
328   myDrawer->DatumAspect()->LineAspect(Prs3d_DP_YAxis)->SetColor(Quantity_NOC_TURQUOISE);
329   myDrawer->DatumAspect()->LineAspect(Prs3d_DP_ZAxis)->SetColor(Quantity_NOC_TURQUOISE);
330   SynchronizeAspects();
331 }
332 //=======================================================================
333 //function : UnsetWidth
334 //purpose  : 
335 //=======================================================================
336
337 void AIS_Axis::UnsetWidth()
338 {
339   myOwnWidth = 0.0f;
340   myDrawer->LineAspect()->SetWidth(1.);
341   myDrawer->DatumAspect()->LineAspect(Prs3d_DP_XAxis)->SetWidth(1.);
342   myDrawer->DatumAspect()->LineAspect(Prs3d_DP_YAxis)->SetWidth(1.);
343   myDrawer->DatumAspect()->LineAspect(Prs3d_DP_ZAxis)->SetWidth(1.);
344   SynchronizeAspects();
345 }