d6a98f38e0dbc11a9e297e533aff5f7c4ee4ceba
[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-2012 OPEN CASCADE SAS
5 //
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
10 //
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 //
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
20
21
22 //GER61351              //GG_171199     Enable to set an object RGB color instead a restricted object NameOfColor.
23
24 #include <AIS_Axis.ixx>
25 #include <Aspect_TypeOfLine.hxx>
26 #include <Prs3d_Drawer.hxx>
27 #include <Prs3d_LineAspect.hxx>
28 #include <Prs3d_DatumAspect.hxx>
29 #include <Graphic3d_AspectLine3d.hxx>
30 #include <Graphic3d_Structure.hxx>
31 #include <TColgp_Array1OfPnt.hxx>
32 #include <SelectMgr_EntityOwner.hxx>
33 #include <SelectBasics_EntityOwner.hxx>
34 #include <Select3D_SensitiveSegment.hxx>
35 #include <StdPrs_Curve.hxx>
36 #include <TopoDS.hxx>
37 #include <Geom_Axis1Placement.hxx>
38 #include <gp_Ax1.hxx>
39 #include <gp_Ax2.hxx>
40 #include <Geom_Line.hxx>
41 #include <GeomAdaptor_Curve.hxx>
42 #include <AIS_Drawer.hxx>
43 #include <DsgPrs_XYZAxisPresentation.hxx>
44 #include <UnitsAPI.hxx>
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) {
83     aLength = 0.1;
84   }
85   DA->SetAxisLength(aLength,aLength,aLength);
86   Quantity_NameOfColor col = Quantity_NOC_TURQUOISE;
87   DA->FirstAxisAspect()->SetColor(col);
88   DA->SecondAxisAspect()->SetColor(col);
89   DA->ThirdAxisAspect()->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->Clear();
171
172   //Pro.... : pas de prise en compte des axes lors du FITALL (jmi)
173   aPresentation->SetInfiniteState (myInfiniteState);
174
175   aPresentation->SetDisplayPriority(5);
176   if (!myIsXYZAxis ){
177     GeomAdaptor_Curve curv(myComponent);
178     StdPrs_Curve::Add(aPresentation,curv,myDrawer);
179   }
180   else {
181     DsgPrs_XYZAxisPresentation::Add(aPresentation,myLineAspect,myDir,myVal,myText,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 // Standard_NotImplemented::Raise("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);
201   eown -> SelectBasics_EntityOwner::Set(3);
202   Handle(Select3D_SensitiveSegment) seg = new Select3D_SensitiveSegment(eown,
203                                                                         myPfirst,
204                                                                         myPlast);
205   aSelection->Add(seg);
206 }
207
208 //=======================================================================
209 //function : SetColor
210 //purpose  : 
211 //=======================================================================
212
213
214 void AIS_Axis::SetColor(const Quantity_NameOfColor aCol)
215 {
216   SetColor(Quantity_Color(aCol));
217 }
218
219 void AIS_Axis::SetColor(const Quantity_Color &aCol)
220 {
221   hasOwnColor=Standard_True;
222   myOwnColor=aCol;
223   myDrawer->LineAspect()->SetColor(aCol);
224   
225   const Handle(Prs3d_DatumAspect)& DA = myDrawer->DatumAspect();
226   DA->FirstAxisAspect()->SetColor(aCol);
227   DA->SecondAxisAspect()->SetColor(aCol);
228   DA->ThirdAxisAspect()->SetColor(aCol);
229   
230 }
231
232 //=======================================================================
233 //function : SetWidth 
234 //purpose  : 
235 //=======================================================================
236 void AIS_Axis::SetWidth(const Standard_Real aValue)
237 {
238   
239   if(aValue<0.0) return;
240   if(aValue==0) UnsetWidth();
241   
242   myDrawer->LineAspect()->SetWidth(aValue);
243   
244   const Handle(Prs3d_DatumAspect)& DA = myDrawer->DatumAspect();
245   DA->FirstAxisAspect()->SetWidth(aValue);
246   DA->SecondAxisAspect()->SetWidth(aValue);
247   DA->ThirdAxisAspect()->SetWidth(aValue);
248 }
249
250
251 //=======================================================================
252 //function : Compute
253 //purpose  : to avoid warning
254 //=======================================================================
255 void AIS_Axis::Compute(const Handle(PrsMgr_PresentationManager2d)&, 
256                           const Handle(Graphic2d_GraphicObject)&,
257                           const Standard_Integer)
258 {
259 }
260
261 //=======================================================================
262 //function : Compute
263 //purpose  : to avoid warning
264 //=======================================================================
265 void AIS_Axis::Compute(const Handle(Prs3d_Projector)&, 
266                        const Handle(Prs3d_Presentation)&)
267 {
268 }
269
270 //=======================================================================
271 //function : ComputeFields
272 //purpose  : 
273 //=======================================================================
274 void AIS_Axis::ComputeFields()
275 {
276   if (myIsXYZAxis){
277     // calcul de myPFirst,myPlast
278     Handle(Prs3d_DatumAspect) DA = myDrawer->DatumAspect();
279     gp_Pnt Orig = myAx2->Ax2().Location();
280     gp_Dir oX = myAx2->Ax2().XDirection();
281     gp_Dir oY = myAx2->Ax2().YDirection();
282     gp_Dir oZ = myAx2->Ax2().Direction();
283     Quantity_Length xo,yo,zo,x,y,z;
284     Orig.Coord(xo,yo,zo);
285     myPfirst.SetCoord(xo,yo,zo);
286     
287     switch (myTypeOfAxis) {
288     case AIS_TOAX_XAxis:
289       {
290         oX.Coord(x,y,z);
291         myVal = DA->FirstAxisLength();
292         myDir = oX;
293         myLineAspect = DA->FirstAxisAspect();
294         myText = Standard_CString ("X");
295         break;
296       }
297     case AIS_TOAX_YAxis:
298       {
299         oY.Coord(x,y,z);
300         myVal = DA->SecondAxisLength();
301         myDir = oY;
302         myLineAspect = DA->SecondAxisAspect();
303         myText = Standard_CString ("Y");
304         break;
305       }
306     case AIS_TOAX_ZAxis:
307       {
308         oZ.Coord(x,y,z); 
309         myVal = DA->ThirdAxisLength();
310         myDir = oZ;
311         myLineAspect = DA->ThirdAxisAspect();
312         myText = Standard_CString ("Z");
313         break;
314       }
315      default:
316       break;
317     }
318     
319     myComponent = new Geom_Line(Orig,myDir);
320     x = xo + x*myVal;   y = yo + y*myVal;  z = zo + z*myVal;
321     myPlast.SetCoord(x,y,z);
322     SetInfiniteState();
323   }
324 }
325
326 //=======================================================================
327 //function : AcceptDisplayMode
328 //purpose  : 
329 //=======================================================================
330
331  Standard_Boolean  AIS_Axis::
332 AcceptDisplayMode(const Standard_Integer aMode) const
333 {return aMode == 0;}
334
335 //=======================================================================
336 //function : UnsetColor
337 //purpose  : 
338 //=======================================================================
339 void AIS_Axis::UnsetColor()
340 {
341   
342   myDrawer->LineAspect()->SetColor(Quantity_NOC_RED);
343
344   hasOwnColor=Standard_False;
345
346   myDrawer->DatumAspect()->FirstAxisAspect()->SetColor(Quantity_NOC_TURQUOISE);
347   myDrawer->DatumAspect()->SecondAxisAspect()->SetColor(Quantity_NOC_TURQUOISE);
348   myDrawer->DatumAspect()->ThirdAxisAspect()->SetColor(Quantity_NOC_TURQUOISE);
349 }
350 //=======================================================================
351 //function : UnsetWidth
352 //purpose  : 
353 //=======================================================================
354
355 void AIS_Axis::UnsetWidth()
356 {
357   myOwnWidth = 0.0;
358   myDrawer->LineAspect()->SetWidth(1.);
359   myDrawer->DatumAspect()->FirstAxisAspect()->SetWidth(1.);
360   myDrawer->DatumAspect()->SecondAxisAspect()->SetWidth(1.);
361   myDrawer->DatumAspect()->ThirdAxisAspect()->SetWidth(1.);
362 }
363