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