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