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