03c08ca4367a1b0864d71d393d67557ff7d75583
[occt.git] / src / AIS / AIS_Trihedron.cxx
1 // File:        AIS_Trihedron.cxx
2 // Created:     Mon Oct  9 16:24:53 1995
3 // Author:      Arnaud BOUZY/Odile Olivier
4 //              <adn>
5
6 #define GER61351                //GG_171199     Enable to set an object RGB color
7 //                                                instead a restricted object NameOfColor.
8
9 #define IMP120100       // GG Add SetTextColor() and SetArrowColor() methods
10
11 #include <AIS_Trihedron.ixx>
12 #include <DsgPrs_DatumPrs.hxx>
13 #include <SelectBasics_EntityOwner.hxx>
14 #include <SelectMgr_EntityOwner.hxx>
15 #include <Select3D_SensitiveSegment.hxx>
16 #include <Select3D_SensitiveFace.hxx>
17 #include <Select3D_SensitivePoint.hxx>
18 #include <Geom_Axis2Placement.hxx>
19 #include <Geom_Line.hxx>
20 #include <Geom_Point.hxx>
21 #include <Geom_Plane.hxx>
22 #include <Geom_CartesianPoint.hxx>
23 #include <gp_Dir.hxx>
24 #include <gp_Ax1.hxx>
25 #include <gp_Ax2.hxx>
26 #include <gp_Pnt.hxx>
27 #include <gp_Vec.hxx>
28 #include <Prs3d_Drawer.hxx>
29 #include <Prs3d_LineAspect.hxx>
30 #include <Prs3d_TextAspect.hxx>
31 #include <Prs3d_ArrowAspect.hxx>
32 #include <Prs3d_DatumAspect.hxx>
33 #include <Graphic3d_AspectLine3d.hxx>
34 #include <Graphic3d_Structure.hxx>
35 #include <Graphic3d_MaterialAspect.hxx>
36 #include <Graphic3d_AspectFillArea3d.hxx>
37 #include <Aspect_TypeOfLine.hxx>
38 #include <AIS_Drawer.hxx>
39 #include <AIS_Plane.hxx>
40 #include <AIS_Axis.hxx>
41 #include <AIS_Point.hxx>
42 #include <UnitsAPI.hxx>
43
44 #include <Select3D_SensitiveBox.hxx>
45 #include <Select3D_SensitiveTriangle.hxx>
46 #include <TColgp_Array1OfPnt.hxx>
47
48
49
50 //=======================================================================
51 //function : AIS_Trihedron
52 //purpose  : 
53 //=======================================================================
54 AIS_Trihedron::AIS_Trihedron(const Handle(Geom_Axis2Placement)& aComponent):
55 myComponent(aComponent),
56 myHasOwnSize(Standard_False)
57 #ifdef IMP120100
58 ,myHasOwnTextColor(Standard_False)
59 ,myHasOwnArrowColor(Standard_False)
60 #endif
61
62 {  LoadSubObjects();}
63
64
65
66 //=======================================================================
67 //function : SetComponent
68 //purpose  : 
69 //=======================================================================
70
71 void AIS_Trihedron::SetComponent(const Handle(Geom_Axis2Placement)& aComponent)
72 {
73   myComponent = aComponent;
74   LoadSubObjects();
75 }
76
77
78 //=======================================================================
79 //function : SetSize
80 //purpose  : 
81 //=======================================================================
82 void AIS_Trihedron::SetSize(const Standard_Real aValue)
83 {
84   myHasOwnSize = Standard_True;
85   if(!myDrawer->HasDatumAspect()){
86     Handle (Prs3d_DatumAspect) DA = new Prs3d_DatumAspect();
87     myDrawer->SetDatumAspect(DA);
88   }
89   
90   myDrawer->DatumAspect()->SetAxisLength(aValue,aValue,aValue);
91   
92   for(Standard_Integer i=4;i<=6;i++) 
93     (*((Handle(AIS_Plane)*)&myShapes[i]))->SetSize(aValue);
94
95   Update();
96   UpdateSelection();
97 }
98
99
100
101 //=======================================================================
102 //function : UnsetSize
103 //purpose  : if the object has 1 color, the default size of the 
104 //           drawer is reproduced, otherwise DatumAspect becomes null
105 //=======================================================================
106
107 void AIS_Trihedron::UnsetSize()
108 {
109   if(!myHasOwnSize) return;
110   
111   myHasOwnSize = Standard_False;
112   if(hasOwnColor){
113     const Handle(Prs3d_DatumAspect)& DA = myDrawer->Link()->DatumAspect();
114     myDrawer->DatumAspect()->SetAxisLength(DA->FirstAxisLength(),
115                                            DA->SecondAxisLength(),
116                                            DA->ThirdAxisLength());
117   }
118   else
119     myDrawer->DatumAspect().Nullify();
120   Update();
121   UpdateSelection();
122
123 }
124
125 //=======================================================================
126 //function : Size
127 //purpose  : 
128 //=======================================================================
129
130 Standard_Real AIS_Trihedron::Size() const 
131 {
132   if(myDrawer->HasDatumAspect()){
133     myDrawer->Link()->DatumAspect(); // ? to ensure that myDrawer->myLink is not null for next call ?
134     return myDrawer->DatumAspect()->FirstAxisLength();
135   }
136   else
137     //return the Defaut value
138     return 100. ;
139     
140
141 }
142
143 //=======================================================================
144 //function : XAxis
145 //purpose  : 
146 //=======================================================================
147 Handle(AIS_Axis) AIS_Trihedron::XAxis() const 
148 {
149   Handle(AIS_Axis) anAxis = Handle(AIS_Axis)::DownCast(myShapes[1]);
150   if (anAxis.IsNull()) anAxis = new AIS_Axis (myComponent,AIS_TOAX_XAxis);
151   return anAxis;
152 }
153
154 //=======================================================================
155 //function : YAxis
156 //purpose  : 
157 //=======================================================================
158 Handle(AIS_Axis) AIS_Trihedron::YAxis() const 
159 {
160   Handle(AIS_Axis) anAxis = Handle(AIS_Axis)::DownCast(myShapes[2]);
161   if (anAxis.IsNull()) anAxis = new AIS_Axis (myComponent,AIS_TOAX_YAxis);
162   return anAxis;
163 }
164
165 //=======================================================================
166 //function : Axis
167 //purpose  : 
168 //=======================================================================
169 Handle(AIS_Axis) AIS_Trihedron::Axis() const 
170 {
171   Handle(AIS_Axis) anAxis = Handle(AIS_Axis)::DownCast(myShapes[3]);
172   if (anAxis.IsNull()) anAxis = new AIS_Axis (myComponent,AIS_TOAX_ZAxis);
173   return anAxis;
174 }
175
176 //=======================================================================
177 //function : Position
178 //purpose  : 
179 //=======================================================================
180 Handle(AIS_Point) AIS_Trihedron::Position() const 
181 {
182   Handle(AIS_Point) aPt = Handle(AIS_Point)::DownCast(myShapes[0]);
183   if (aPt.IsNull()) {
184     gp_Pnt aPnt = myComponent->Ax2().Location();
185     Handle(Geom_Point) aPoint = new Geom_CartesianPoint(aPnt);
186     aPt = new AIS_Point (aPoint);
187   }
188   return aPt;
189 }
190
191 //=======================================================================
192 //function : XYPlane
193 //purpose  : 
194 //=======================================================================
195 Handle(AIS_Plane) AIS_Trihedron::XYPlane() const 
196 {
197   Handle(AIS_Plane) aPl = Handle(AIS_Plane)::DownCast(myShapes[4]);
198   if (aPl.IsNull()) aPl = new AIS_Plane (myComponent,AIS_TOPL_XYPlane);
199   return aPl;
200 }
201
202 //=======================================================================
203 //function : XZPlane
204 //purpose  : 
205 //=======================================================================
206 Handle(AIS_Plane) AIS_Trihedron::XZPlane() const 
207 {
208   Handle(AIS_Plane) aPl = Handle(AIS_Plane)::DownCast(myShapes[5]);
209   if (aPl.IsNull()) aPl = new AIS_Plane (myComponent,AIS_TOPL_XZPlane);
210   return aPl;
211 }
212
213 //=======================================================================
214 //function : YZPlane
215 //purpose  : 
216 //=======================================================================
217 Handle(AIS_Plane) AIS_Trihedron::YZPlane() const 
218 {
219   Handle(AIS_Plane) aPl = Handle(AIS_Plane)::DownCast(myShapes[6]);
220   if (aPl.IsNull()) aPl = new AIS_Plane (myComponent,AIS_TOPL_YZPlane);
221   return aPl;
222 }
223
224 //=======================================================================
225 //function : Compute
226 //purpose  : 
227 //=======================================================================
228 void AIS_Trihedron::Compute(
229  const Handle(PrsMgr_PresentationManager3d)& /*aPresentationManager*/,
230  const Handle(Prs3d_Presentation)& aPresentation, 
231  const Standard_Integer aMode)
232 {
233   aPresentation->Clear();
234
235   aPresentation->SetInfiniteState (Standard_True);
236   switch(aMode){
237   case 0: 
238     DsgPrs_DatumPrs::Add(aPresentation,myComponent->Ax2(),myDrawer);
239     break;
240   case 1:
241     break;
242   case 2:
243     break;
244   }
245 }
246
247 //=======================================================================
248 //function : Compute
249 //purpose  : 
250 //=======================================================================
251
252 void AIS_Trihedron::Compute(const Handle_Prs3d_Projector& aProjector,
253                             const Handle_Geom_Transformation& aTransformation,
254                             const Handle_Prs3d_Presentation& aPresentation)
255 {
256 // Standard_NotImplemented::Raise("AIS_Trihedron::Compute(const Handle_Prs3d_Projector&, const Handle_Geom_Transformation&, const Handle_Prs3d_Presentation&)");
257  PrsMgr_PresentableObject::Compute( aProjector , aTransformation , aPresentation) ;
258 }
259
260 //=======================================================================
261 //function : ComputeSelection
262 //purpose  : 
263 //=======================================================================
264
265 void AIS_Trihedron::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
266                                      const Standard_Integer aMode)
267 {
268   // retrieve the tops of the trihedron.
269   Standard_Integer Prior;
270   Handle(SelectMgr_EntityOwner) eown;
271   TColgp_Array1OfPnt PP(1,4),PO(1,4);
272   ExtremityPoints(PP);
273   switch (aMode) {
274   case 0:
275     {   // complete triedron only 1 owner : this... priority 5 (same as faces)
276       Prior = 5;
277       eown = new SelectMgr_EntityOwner(this,Prior);
278       for (Standard_Integer i=1; i<=3;i++)
279         aSelection->Add(new Select3D_SensitiveSegment(eown,PP(1),PP(i+1)));
280       break;
281     }
282   case 1:
283     {  //origin : 
284       Prior = 8;
285       eown= new SelectMgr_EntityOwner(myShapes[0],Prior);
286       
287       aSelection->Add(new Select3D_SensitivePoint (eown,myComponent->Location()));
288
289       break;
290     }
291   case 2:
292     {  //axes ... priority 7
293       Prior = 7;
294       for (Standard_Integer i=1; i<=3;i++){
295         eown= new SelectMgr_EntityOwner(myShapes[i],Prior);
296         aSelection->Add(new Select3D_SensitiveSegment(eown,PP(1),PP(i+1)));
297
298       }
299       break;
300     }
301     
302   case 3:
303     {  // main planes priority 6
304 //      PO(1) = PP(1);
305 //      PO(4) = PP(1);
306       Prior =5;
307       
308       
309       eown= new SelectMgr_EntityOwner(myShapes[4],Prior);
310 //      PO(2) = PP(2);PO(3) = PP(3);
311       aSelection->Add(new Select3D_SensitiveTriangle(eown,PP(1),PP(2),PP(3)));
312
313       eown= new SelectMgr_EntityOwner(myShapes[5],Prior);
314 //      PO(2) = PP(3);PO(3) = PP(4);
315       aSelection->Add(new Select3D_SensitiveTriangle(eown,PP(1),PP(2),PP(4)));
316
317       eown= new SelectMgr_EntityOwner(myShapes[6],Prior);
318 //      PO(2) = PP(4);PO(3) = PP(2);
319       aSelection->Add(new Select3D_SensitiveTriangle(eown,PP(1),PP(3),PP(4)));
320       
321     }
322   }
323   
324 }
325
326 //=======================================================================
327 //function : SetColor
328 //purpose  : 
329 //=======================================================================
330
331 void AIS_Trihedron::SetColor(const Quantity_NameOfColor aCol)
332 #ifdef GER61351
333 {
334   SetColor(Quantity_Color(aCol));
335 }
336
337 void AIS_Trihedron::SetColor(const Quantity_Color &aCol)
338 #endif
339 {
340   hasOwnColor=Standard_True;
341   myOwnColor = aCol;
342   
343   if(!myDrawer->HasDatumAspect()){
344     Handle (Prs3d_DatumAspect) DA = new Prs3d_DatumAspect();
345     
346     DA->SetAxisLength(myDrawer->DatumAspect()->FirstAxisLength(),
347                       myDrawer->DatumAspect()->SecondAxisLength(),
348                       myDrawer->DatumAspect()->ThirdAxisLength());
349     myDrawer->SetDatumAspect(DA);
350   }
351   myDrawer->DatumAspect()->FirstAxisAspect()->SetColor(aCol);
352   myDrawer->DatumAspect()->SecondAxisAspect()->SetColor(aCol);
353   myDrawer->DatumAspect()->ThirdAxisAspect()->SetColor(aCol);
354
355 }
356
357 //=======================================================================
358 //function : SetTextColor
359 //purpose  : 
360 //=======================================================================
361
362 #ifdef IMP120100
363 void AIS_Trihedron::SetTextColor(const Quantity_NameOfColor aCol)
364 {
365   myHasOwnTextColor = Standard_True;
366   myOwnTextColor = aCol;
367   
368   if(!myDrawer->HasDatumAspect()){
369     Handle (Prs3d_DatumAspect) DA = new Prs3d_DatumAspect();
370     
371     DA->SetAxisLength(myDrawer->DatumAspect()->FirstAxisLength(),
372                       myDrawer->DatumAspect()->SecondAxisLength(),
373                       myDrawer->DatumAspect()->ThirdAxisLength());
374     myDrawer->SetDatumAspect(DA);
375   }
376   Handle(Prs3d_TextAspect) aspect = myDrawer->TextAspect();
377   aspect->SetColor(aCol);
378   myDrawer->SetTextAspect(aspect);
379 }
380
381 void AIS_Trihedron::SetArrowColor(const Quantity_NameOfColor aCol)
382 {
383   myHasOwnArrowColor = Standard_True;
384   myOwnArrowColor = aCol;
385   
386   if(!myDrawer->HasDatumAspect()){
387     Handle (Prs3d_DatumAspect) DA = new Prs3d_DatumAspect();
388     
389     DA->SetAxisLength(myDrawer->DatumAspect()->FirstAxisLength(),
390                       myDrawer->DatumAspect()->SecondAxisLength(),
391                       myDrawer->DatumAspect()->ThirdAxisLength());
392     myDrawer->SetDatumAspect(DA);
393   }
394   Handle(Prs3d_ArrowAspect) aspect = myDrawer->ArrowAspect();
395   aspect->SetColor(aCol);
396   myDrawer->SetArrowAspect(aspect);
397 }
398
399 //=======================================================================
400 Standard_Boolean AIS_Trihedron::HasTextColor() const {
401
402   return myHasOwnTextColor;
403 }
404
405 //=======================================================================
406 Quantity_NameOfColor AIS_Trihedron::TextColor() const {
407
408   return myOwnTextColor;
409 }
410
411 //=======================================================================
412 Standard_Boolean AIS_Trihedron::HasArrowColor() const {
413
414   return myHasOwnArrowColor;
415 }
416
417 //=======================================================================
418 Quantity_NameOfColor AIS_Trihedron::ArrowColor() const {
419
420   return myOwnArrowColor;
421 }
422 #endif
423
424 //=======================================================================
425 //function : Compute
426 //purpose  : to avoid warning
427 //=======================================================================
428 void AIS_Trihedron::Compute(const Handle(PrsMgr_PresentationManager2d)&, 
429                                const Handle(Graphic2d_GraphicObject)&,
430                                const Standard_Integer)
431 {
432 }
433
434 //=======================================================================
435 //function : Compute
436 //purpose  : to avoid warning
437 //=======================================================================
438 void AIS_Trihedron::Compute(const Handle(Prs3d_Projector)&, 
439                                const Handle(Prs3d_Presentation)&)
440 {
441 }
442 //=======================================================================
443 //function : Type
444 //purpose  : 
445 //=======================================================================
446
447 AIS_KindOfInteractive AIS_Trihedron::Type() const 
448 {return AIS_KOI_Datum;}
449
450
451 //=======================================================================
452 //function : Signature
453 //purpose  : 
454 //=======================================================================
455
456  Standard_Integer AIS_Trihedron::Signature() const 
457 {return 3;}
458
459 //=======================================================================
460 //function : ExtremityPoints
461 //purpose  : to avoid warning
462 //=======================================================================
463 void  AIS_Trihedron::ExtremityPoints(TColgp_Array1OfPnt& PP) const 
464 {
465   gp_Ax2 theax(myComponent->Ax2());
466   PP(1) = theax.Location();
467
468   Standard_Real len = myDrawer->DatumAspect()->FirstAxisLength();
469   gp_Vec vec = theax.XDirection();
470   vec *= len;
471   PP(2) = PP(1).Translated(vec);
472   
473   len = myDrawer->DatumAspect()->SecondAxisLength();
474   vec = theax.YDirection();
475   vec *= len;
476   PP(3) = PP(1).Translated(vec);
477
478   len = myDrawer->DatumAspect()->ThirdAxisLength();
479   vec = theax.Direction();
480   vec *= len;
481   PP(4) = PP(1).Translated(vec);
482 }
483
484 //=======================================================================
485 //function : AcceptDisplayMode
486 //purpose  : 
487 //=======================================================================
488
489  Standard_Boolean  AIS_Trihedron::
490 AcceptDisplayMode(const Standard_Integer aMode) const
491 {return aMode == 0;}
492
493
494
495 //=======================================================================
496 //function : UnsetColor
497 //purpose  : 
498 //=======================================================================
499
500 void AIS_Trihedron::UnsetColor()
501 {
502   hasOwnColor=Standard_False;
503   myOwnColor = Quantity_NOC_LIGHTSTEELBLUE4;
504   myDrawer->DatumAspect()->FirstAxisAspect()->SetColor(myOwnColor);
505   myDrawer->DatumAspect()->SecondAxisAspect()->SetColor(myOwnColor);
506   myDrawer->DatumAspect()->ThirdAxisAspect()->SetColor(myOwnColor);
507 #ifdef IMP120100
508   if( HasTextColor() ) {
509     SetTextColor(myOwnColor.Name());
510     myHasOwnTextColor = Standard_False;
511   }
512   if( HasArrowColor() ) {
513     SetArrowColor(myOwnColor.Name());
514     myHasOwnArrowColor = Standard_False;
515   }
516 #endif
517   
518 }
519
520 //=======================================================================
521 //function : UnsetWitdth
522 //purpose  : 
523 //=======================================================================
524
525 void AIS_Trihedron::UnsetWidth()
526 {
527   myOwnWidth =0.0;
528   myDrawer->DatumAspect()->FirstAxisAspect()->SetWidth(1.);
529   myDrawer->DatumAspect()->SecondAxisAspect()->SetWidth(1.);
530   myDrawer->DatumAspect()->ThirdAxisAspect()->SetWidth(1.);
531
532 }
533 void AIS_Trihedron::LoadSubObjects()
534 {
535   myShapes[0] = Position();
536   myShapes[1] = XAxis();
537   myShapes[2] = YAxis();
538   myShapes[3] = Axis();
539   myShapes[4] = XYPlane();
540   myShapes[5] = XZPlane();
541   myShapes[6] = YZPlane();
542 }
543
544 //=======================================================================
545 //function : SetContext
546 //purpose  : 
547 //=======================================================================
548
549 void AIS_Trihedron::SetContext(const Handle(AIS_InteractiveContext)& Ctx)
550 {
551 //  Standard_Boolean same_DA = myDrawer->Link() == Ctx->DefaultDrawer();
552
553   AIS_InteractiveObject::SetContext(Ctx);
554   
555   LoadSubObjects();
556   for(Standard_Integer i= 0;i<=6;i++)
557     myShapes[i]->SetContext(Ctx);
558 }