0023634: Eliminate Polyline and Polygon usage in drawers
[occt.git] / src / AIS / AIS_Plane.cxx
CommitLineData
b311480e 1// Created on: 1995-08-02
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
7fd59977 21
22#define GER61351 //GG_171199 Enable to set an object RGB color
23// instead a restricted object NameOfColor.
24
25#include <AIS_Plane.ixx>
26
27#include <TColgp_Array1OfPnt.hxx>
28#include <TColgp_HArray1OfPnt.hxx>
29
30#include <Precision.hxx>
31#include <gp_Pnt.hxx>
32#include <ElSLib.hxx>
33
b8ddfc2f 34#include <Graphic3d_ArrayOfQuadrangles.hxx>
7fd59977 35#include <Graphic3d_Group.hxx>
36#include <Prs3d_Drawer.hxx>
37#include <Prs3d_LineAspect.hxx>
38#include <Prs3d_PlaneAspect.hxx>
39#include <Prs3d_DatumAspect.hxx>
40#include <Prs3d_ShadingAspect.hxx>
41#include <StdPrs_Plane.hxx>
42#include <StdPrs_ShadedSurface.hxx>
43
44#include <Geom_Plane.hxx>
45#include <GeomAdaptor_Surface.hxx>
46
47#include <Aspect_TypeOfLine.hxx>
48#include <Graphic3d_AspectLine3d.hxx>
49#include <Graphic3d_Structure.hxx>
50#include <Graphic3d_MaterialAspect.hxx>
51#include <Graphic3d_AspectFillArea3d.hxx>
52
53#include <DsgPrs_XYZPlanePresentation.hxx>
54#include <DsgPrs_ShadedPlanePresentation.hxx>
55#include <UnitsAPI.hxx>
56
57#include <SelectBasics_EntityOwner.hxx>
58#include <SelectMgr_EntityOwner.hxx>
59#include <Select3D_SensitiveFace.hxx>
60#include <StdPrs_ShadedShape.hxx>
61
62#include <AIS_Drawer.hxx>
63
64#include <TCollection_AsciiString.hxx>
65
66//=======================================================================
67//function : AIS_Plane
68//purpose :
69//=======================================================================
70AIS_Plane::AIS_Plane(const Handle(Geom_Plane)& aComponent,
71 const Standard_Boolean aCurrentMode):
72myComponent(aComponent),
73myCenter(gp_Pnt(0.,0.,0.)),
74myCurrentMode(aCurrentMode),
75myAutomaticPosition(Standard_True),
76myTypeOfPlane(AIS_TOPL_Unknown),
77myIsXYZPlane(Standard_False)
78{
79 InitDrawerAttributes();
80}
81
82//=======================================================================
83//function : AIS_Plane
84//purpose : avec une position
85//=======================================================================
86AIS_Plane::AIS_Plane(const Handle(Geom_Plane)& aComponent,
87 const gp_Pnt& aCenter,
88 const Standard_Boolean aCurrentMode):
89myComponent(aComponent),
90myCenter(aCenter),
91myCurrentMode(aCurrentMode),
92myAutomaticPosition(Standard_True),
93myTypeOfPlane(AIS_TOPL_Unknown),
94myIsXYZPlane(Standard_False)
95{
96 InitDrawerAttributes();
97}
98
99//=======================================================================
100//function : AIS_Plane
101//purpose :
102//=======================================================================
103AIS_Plane::AIS_Plane(const Handle(Geom_Plane)& aComponent,
104 const gp_Pnt& aCenter,
105 const gp_Pnt& aPmin,
106 const gp_Pnt& aPmax,
107 const Standard_Boolean aCurrentMode):
108myComponent(aComponent),
109myCenter(aCenter),
110myPmin(aPmin),
111myPmax(aPmax),
112myCurrentMode(aCurrentMode),
113myAutomaticPosition(Standard_False),
114myTypeOfPlane(AIS_TOPL_Unknown),
115myIsXYZPlane(Standard_False)
116{
117 InitDrawerAttributes();
118 SetHilightMode(0);
119}
120
121//=======================================================================
122//function : AIS_Plane
123//purpose : XYPlane, XZPlane, YZPlane
124//=======================================================================
125AIS_Plane::AIS_Plane(const Handle(Geom_Axis2Placement)& aComponent,
126 const AIS_TypeOfPlane aPlaneType,
127 const Standard_Boolean aCurrentMode):
128myAx2(aComponent),
129myCurrentMode(aCurrentMode),
130myAutomaticPosition(Standard_True),
131myTypeOfPlane(aPlaneType),
132myIsXYZPlane(Standard_True)
133{
134 InitDrawerAttributes();
135 ComputeFields();
136 SetHilightMode(0);
137
138}
139
140
141//=======================================================================
142//function : SetComponent
143//purpose :
144//=======================================================================
145
146void AIS_Plane::SetComponent(const Handle(Geom_Plane)& aComponent)
147{
148 myComponent = aComponent;
149 myTypeOfPlane = AIS_TOPL_Unknown;
150 myIsXYZPlane = Standard_False;
151 //myCenter = gp_Pnt(0.,0.,0.);
152 myAutomaticPosition = Standard_True;
153}
154
155//=======================================================================
156//function : Type
157//purpose :
158//=======================================================================
159
160AIS_KindOfInteractive AIS_Plane::Type() const
161{return AIS_KOI_Datum;}
162
163
164//=======================================================================
165//function : Signature
166//purpose :
167//=======================================================================
168
169Standard_Integer AIS_Plane::Signature() const
170{return 7;}
171
172
173//=======================================================================
174//function : Axis2Placement
175//purpose :
176//=======================================================================
177
178Handle(Geom_Axis2Placement) AIS_Plane::Axis2Placement()
179{
180 Handle(Geom_Axis2Placement) Bid;
181 return IsXYZPlane() ? myAx2:Bid;
182}
183//=======================================================================
184//function : SetAxis2Placement
185//purpose :
186//=======================================================================
187
188 void AIS_Plane::SetAxis2Placement(const Handle(Geom_Axis2Placement)& aComponent,
189 const AIS_TypeOfPlane aPlaneType)
190{
191 myTypeOfPlane = aPlaneType;
192 myIsXYZPlane = Standard_True;
193 myAx2= aComponent;
194 myAutomaticPosition = Standard_True;
195 ComputeFields();
196}
197
198//=======================================================================
199//function : PlaneAttributes
200//purpose :
201//=======================================================================
202Standard_Boolean AIS_Plane::PlaneAttributes(Handle(Geom_Plane)& aComponent,
203 gp_Pnt& aCenter,
204 gp_Pnt& aPmin,
205 gp_Pnt& aPmax)
206{
207 Standard_Boolean aStatus (Standard_False);
208 if (!myAutomaticPosition){
209 aComponent = myComponent;
210 aCenter = myCenter;
211 aPmin = myPmin;
212 aPmax = myPmax;
213 aStatus = Standard_True;
214 }
215 return aStatus;
216}
217
218//=======================================================================
219//function : SetPlaneAttributes
220//purpose :
221//=======================================================================
222void AIS_Plane::SetPlaneAttributes(const Handle(Geom_Plane)& aComponent,
223 const gp_Pnt& aCenter,
224 const gp_Pnt& aPmin,
225 const gp_Pnt& aPmax)
226{
227 myAutomaticPosition = Standard_False;
228 myComponent = aComponent;
229 myCenter = aCenter;
230 myPmin = aPmin;
231 myPmax = aPmax;
232 myTypeOfPlane = AIS_TOPL_Unknown;
233 myIsXYZPlane = Standard_False;
234}
235
236//=======================================================================
237//function : Compute
238//purpose :
239//=======================================================================
240void AIS_Plane::Compute(const Handle(PrsMgr_PresentationManager3d)& ,
241 const Handle(Prs3d_Presentation)& aPresentation,
242 const Standard_Integer aMode)
243{
244 aPresentation->Clear();
245
246 ComputeFields();
247 aPresentation->SetInfiniteState(myInfiniteState);
b8ddfc2f 248 myDrawer->PlaneAspect()->EdgesAspect()->SetWidth(myCurrentMode == 0? 1 : 3);
249
250 switch (aMode)
251 {
252 case 0:
253 {
254 if (!myIsXYZPlane)
255 {
256 ComputeFrame();
257 const Handle(Geom_Plane)& pl = myComponent;
258 const Handle(Geom_Plane)& thegoodpl = Handle(Geom_Plane)::DownCast(pl->Translated(pl->Location(),myCenter));
259 GeomAdaptor_Surface surf(thegoodpl);
260 StdPrs_Plane::Add(aPresentation,surf,myDrawer);
261 }
262 else
263 DsgPrs_XYZPlanePresentation::Add(aPresentation,myDrawer,myCenter,myPmin,myPmax);
264 break;
7fd59977 265 }
b8ddfc2f 266 case 1:
267 {
268 if (!myIsXYZPlane)
269 {
270 ComputeFrame();
271 Handle(Prs3d_PlaneAspect) theaspect = myDrawer->PlaneAspect();
272 Handle(Graphic3d_Group) TheGroup = Prs3d_Root::CurrentGroup(aPresentation);
273 TheGroup->SetPrimitivesAspect(myDrawer->ShadingAspect()->Aspect());
274 gp_Pnt p1;
275 const Standard_Real Xmax = 0.5*Standard_Real(theaspect->PlaneXLength());
276 const Standard_Real Ymax = 0.5*Standard_Real(theaspect->PlaneYLength());
277
278 Handle(Graphic3d_ArrayOfQuadrangles) aQuads = new Graphic3d_ArrayOfQuadrangles(4);
279
280 myComponent->D0(-Xmax,Ymax,p1);
281 aQuads->AddVertex(p1);
282 myComponent->D0(Xmax,Ymax,p1);
283 aQuads->AddVertex(p1);
284 myComponent->D0(Xmax,-Ymax,p1);
285 aQuads->AddVertex(p1);
286 myComponent->D0(-Xmax,-Ymax,p1);
287 aQuads->AddVertex(p1);
288
289 TheGroup->AddPrimitiveArray(aQuads);
7fd59977 290 }
b8ddfc2f 291 else
292 DsgPrs_ShadedPlanePresentation::Add(aPresentation,myDrawer,myCenter,myPmin,myPmax);
293 break;
7fd59977 294 }
295 }
7fd59977 296}
297
298void AIS_Plane::Compute(const Handle_Prs3d_Projector& aProjector, const Handle_Geom_Transformation& aTransformation, const Handle_Prs3d_Presentation& aPresentation)
299{
b8ddfc2f 300 PrsMgr_PresentableObject::Compute(aProjector, aTransformation, aPresentation);
7fd59977 301}
302
303//=======================================================================
304//function : ComputeSelection
305//purpose :
306//=======================================================================
307void AIS_Plane::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
308 const Standard_Integer)
309{
310 aSelection->Clear();
311 Handle(SelectMgr_EntityOwner) eown = new SelectMgr_EntityOwner(this,10);
312 Handle(Select3D_SensitiveFace) sfac;
313
314 if (!myIsXYZPlane){
315
316 Handle(TColgp_HArray1OfPnt) harr = new TColgp_HArray1OfPnt(1,5);
317 TColgp_Array1OfPnt& arr = harr->ChangeArray1();
318 Standard_Real lx = myDrawer->PlaneAspect()->PlaneXLength()/2.;
319 Standard_Real ly = myDrawer->PlaneAspect()->PlaneYLength()/2.;
320 const Handle(Geom_Plane)& pl = myComponent;
321 const Handle(Geom_Plane)& thegoodpl = Handle(Geom_Plane)::DownCast(pl->Translated(pl->Location(),myCenter));
322
323 thegoodpl->D0(lx,ly,arr(1));
324 thegoodpl->D0(lx,-ly,arr(2));
325 thegoodpl->D0(-lx,-ly,arr(3));
326 thegoodpl->D0(-lx,ly,arr(4));
327 arr(5) = arr(1);
328 sfac = new Select3D_SensitiveFace(eown,harr,Select3D_TOS_BOUNDARY);
329
330 }
331 else {
332 Handle(TColgp_HArray1OfPnt) harr1 = new TColgp_HArray1OfPnt(1,4);
333 TColgp_Array1OfPnt& arr1 = harr1->ChangeArray1();
334
335 arr1(1) = myCenter;
336 arr1(2) = myPmin;
337 arr1(3) = myPmax;
338 arr1(4) = myCenter;
339 sfac = new Select3D_SensitiveFace(eown,harr1,Select3D_TOS_BOUNDARY);
340
341 }
342 aSelection->Add(sfac);
343}
344
345
346
347
348//=======================================================================
349//function : SetSize
350//purpose :
351//=======================================================================
352void AIS_Plane::SetSize(const Standard_Real aLength)
353{
354 SetSize(aLength,aLength);
355}
356
357void AIS_Plane::SetSize(const Standard_Real aXLength,
358 const Standard_Real aYLength)
359{
81bba717 360 //if the plane already has a proper color or size,
361 //there is already a specific PlaneAspect and DatumAspect
7fd59977 362
363 Handle(Prs3d_PlaneAspect) PA;
364 Handle(Prs3d_DatumAspect) DA;
365
366 PA = myDrawer->PlaneAspect();
367 DA = myDrawer->DatumAspect();
368
369 Standard_Boolean yenavaitPA(Standard_True),yenavaitDA(Standard_True);
370 if(myDrawer->Link()->PlaneAspect() == PA){
371 yenavaitPA = Standard_False;
372 PA = new Prs3d_PlaneAspect();}
373 if(myDrawer->Link()->DatumAspect() == DA){
374 yenavaitDA = Standard_False;
375 DA = new Prs3d_DatumAspect();
376 }
377
378 PA->SetPlaneLength(aXLength,aYLength);
379 DA->SetAxisLength(aXLength,aYLength,aXLength);
380
381 if(!yenavaitPA)
382 myDrawer->SetPlaneAspect(PA);
383 if(!yenavaitDA)
384 myDrawer->SetDatumAspect(DA);
385
386
387 myHasOwnSize = Standard_True;
388 Update();
389 UpdateSelection();
390}
391
392
393//=======================================================================
394//function : UnsetSize
81bba717 395//purpose : If there is a color, the size is restaured from the drawer of the context...
7fd59977 396//=======================================================================
397void AIS_Plane::UnsetSize()
398{
399
400 if(!myHasOwnSize) return;
401 if(!hasOwnColor){
402 myDrawer->PlaneAspect().Nullify();
403 myDrawer->DatumAspect().Nullify();
404 }
405 else{
406 const Handle(Prs3d_PlaneAspect)& PA = myDrawer->Link()->PlaneAspect();
407 const Handle(Prs3d_DatumAspect)& DA = myDrawer->Link()->DatumAspect();
408
409 myDrawer->PlaneAspect()->SetPlaneLength(PA->PlaneXLength(),PA->PlaneYLength());
410 myDrawer->DatumAspect()->SetAxisLength(DA->FirstAxisLength(),
411 DA->SecondAxisLength(),
412 DA->ThirdAxisLength());
413 }
414
415 myHasOwnSize = Standard_False;
416 Update();
417 UpdateSelection();
418
419}
420
421//=======================================================================
422//function : Size
423//purpose :
424//=======================================================================
425
426Standard_Boolean AIS_Plane::Size(Standard_Real& X,Standard_Real& Y) const
427{
428 X = myDrawer->PlaneAspect()->PlaneXLength();
429 Y = myDrawer->PlaneAspect()->PlaneYLength();
430 return Abs(X-Y)<=Precision::Confusion();
431}
432
433
434//=======================================================================
435//function : SetColor
436//purpose :
437//=======================================================================
438
439
440void AIS_Plane::SetColor(const Quantity_NameOfColor aCol)
441#ifdef GER61351
442{
443 SetColor(Quantity_Color(aCol));
444}
445
446void AIS_Plane::SetColor(const Quantity_Color &aCol)
447#endif
448{
81bba717 449 // if the plane already has its proper size, there is an already created planeaspect
7fd59977 450// Standard_Boolean yenadeja = hasOwnColor || myHasOwnSize;
451 Handle(Prs3d_PlaneAspect) PA;
452 Handle(Prs3d_DatumAspect) DA;
453
454 PA = myDrawer->PlaneAspect();
455 DA = myDrawer->DatumAspect();
456
457 Standard_Boolean yenavaitPA(Standard_True),yenavaitDA(Standard_True);
458 if(myDrawer->Link()->PlaneAspect() == PA){
459 yenavaitPA = Standard_False;
460 PA = new Prs3d_PlaneAspect();}
461 if(myDrawer->Link()->DatumAspect() == DA){
462 yenavaitDA = Standard_False;
463 DA = new Prs3d_DatumAspect();
464 }
465
466 PA->EdgesAspect()->SetColor(aCol);
467 DA->FirstAxisAspect()->SetColor(aCol);
468 DA->SecondAxisAspect()->SetColor(aCol);
469 DA->ThirdAxisAspect()->SetColor(aCol);
470
471 if(!yenavaitPA)
472 myDrawer->SetPlaneAspect(PA);
473 if(!yenavaitDA)
474 myDrawer->SetDatumAspect(DA);
475
476 myDrawer->ShadingAspect()->SetColor(aCol);
477
478 hasOwnColor=Standard_True;
479 myOwnColor = aCol;
480}
481//=======================================================================
482//function : SetColor
483//purpose :
484//=======================================================================
485void AIS_Plane::UnsetColor()
486{
487 if(!hasOwnColor) return;
488 if(!myHasOwnSize){
489 myDrawer->PlaneAspect().Nullify();
490 myDrawer->DatumAspect().Nullify();
491 }
492 else{
493 const Handle(Prs3d_PlaneAspect)& PA = myDrawer->Link()->PlaneAspect();
494// const Handle(Prs3d_DatumAspect)& DA = myDrawer->Link()->DatumAspect();
495 Quantity_Color C;Aspect_TypeOfLine T;Standard_Real W;
496 PA->EdgesAspect()->Aspect()->Values(C,T,W);
497 Quantity_NameOfColor Col = C.Name();
498 myDrawer->PlaneAspect()->EdgesAspect()->SetColor(Col);
499
500 myDrawer->DatumAspect()->FirstAxisAspect()->SetColor(Col);
501 myDrawer->DatumAspect()->SecondAxisAspect()->SetColor(Col);
502 myDrawer->DatumAspect()->ThirdAxisAspect()->SetColor(Col);
503 }
504
505
506 hasOwnColor=Standard_False;
507
508}
509
510//=======================================================================
511//function : ComputeFrame
512//purpose :
513//=======================================================================
514void AIS_Plane::ComputeFrame()
515{
516
517 const Handle(Geom_Plane)& pl = myComponent;
518 Standard_Real U,V;
519
520 if (myAutomaticPosition) {
521 ElSLib::Parameters(pl->Pln(),myCenter,U,V);
522 pl->D0 (U, V, myCenter);
523 }
524 else {
525 const Handle(Geom_Plane)& thegoodpl =
526 Handle(Geom_Plane)::DownCast(pl->Translated(pl->Location(),myCenter));
527 ElSLib::Parameters(thegoodpl->Pln(),myPmin,U,V);
528
529 U = 2.4*Abs(U);
530 V = 2.4*Abs(V);
531 if (U < 10*Precision::Confusion()) U=0.1;
532 if (V < 10*Precision::Confusion()) V=0.1;
533 SetSize(U,V);
534 myDrawer->PlaneAspect()->SetPlaneLength(U,V);
535 }
536}
537
538//=======================================================================
539//function : Compute
540//purpose : to avoid warning
541//=======================================================================
542void AIS_Plane::Compute(const Handle(PrsMgr_PresentationManager2d)&,
543 const Handle(Graphic2d_GraphicObject)&,
544 const Standard_Integer)
545{
546}
547
548//=======================================================================
549//function : Compute
550//purpose : to avoid warning
551//=======================================================================
552void AIS_Plane::Compute(const Handle(Prs3d_Projector)&,
553 const Handle(Prs3d_Presentation)&)
554{
555}
556
557//=======================================================================
558//function : ComputeFields
559//purpose :
560//=======================================================================
561void AIS_Plane::ComputeFields()
562{
563 if (myIsXYZPlane){
564 Handle (Prs3d_DatumAspect) DA = myDrawer->DatumAspect();
565
566 gp_Pnt Orig = myAx2->Ax2().Location();
567 gp_Dir oX = myAx2->Ax2().XDirection();
568 gp_Dir oY = myAx2->Ax2().YDirection();
569 gp_Dir oZ = myAx2->Ax2().Direction();
570 myCenter = Orig;
571 Standard_Real xo,yo,zo,x1,y1,z1,x2,y2,z2,x3,y3,z3,x4=0,y4=0,z4=0;
572 Standard_Real x5=0,y5=0,z5=0;
573 Orig.Coord(xo,yo,zo);
574 oX.Coord(x1,y1,z1);
575 oY.Coord(x2,y2,z2);
576 oZ.Coord(x3,y3,z3);
577 Standard_Real DS1 = DA->FirstAxisLength();
578 Standard_Real DS2 = DA->SecondAxisLength();
579 Standard_Real DS3 = DA->ThirdAxisLength();
580// gp_Pnt aPt2,aPt3;
581
582 switch (myTypeOfPlane) {
583 case AIS_TOPL_XYPlane:
584 {
585 gp_Pln XYP(0,0,1,0);
586 myComponent = new Geom_Plane(XYP);
587 x4=xo+x1*DS1; y4=yo+y1*DS1; z4=zo+z1*DS1;
588 x5=xo+x2*DS2; y5=yo+y2*DS2; z5=zo+z2*DS2;
589 break;
590 }
591 case AIS_TOPL_XZPlane:
592 {
593 gp_Pln XZP(0,1,0,0);
594 myComponent = new Geom_Plane(XZP);
595 x4=xo+x1*DS1; y4=yo+y1*DS1; z4=zo+z1*DS1;
596 x5=xo+x3*DS3; y5=yo+y3*DS3; z5=zo+z3*DS3;
597 break;
598 }
599 case AIS_TOPL_YZPlane:
600 {
601 gp_Pln XZP(1,0,0,0);
602 myComponent = new Geom_Plane(XZP);
603 x4=xo+x2*DS2; y4=yo+y2*DS2; z4=zo+z2*DS2;
604 x5=xo+x3*DS3; y5=yo+y3*DS3; z5=zo+z3*DS3;
605 break;
606 }
607 default:
608 break;
609 }
610 myPmin.SetCoord(x4,y4,z4);
611 myPmax.SetCoord(x5,y5,z5);
612 }
613
614}
615//=======================================================================
616//function : InitDrawerAttributes
617//purpose :
618//=======================================================================
619
620void AIS_Plane::InitDrawerAttributes()
621{
622
623 Handle(Prs3d_ShadingAspect) shasp = new Prs3d_ShadingAspect();
624 shasp->SetMaterial(Graphic3d_NOM_PLASTIC);
625 shasp->SetColor(Quantity_NOC_GRAY40);
626 myDrawer->SetShadingAspect(shasp);
627 Handle(Graphic3d_AspectFillArea3d) asf = shasp->Aspect();
628 Graphic3d_MaterialAspect asp = asf->FrontMaterial();
629 asp.SetTransparency(0.8);
630 asf->SetFrontMaterial(asp);
631 asf->SetBackMaterial(asp);
632
633
634}
635
636//=======================================================================
637//function : AcceptDisplayMode
638//purpose :
639//=======================================================================
640
641Standard_Boolean AIS_Plane::
642AcceptDisplayMode(const Standard_Integer aMode) const
643{return aMode == 0;}
644
645
646//=======================================================================
647//function : SetContext
648//purpose :
649//=======================================================================
650
651void AIS_Plane::SetContext(const Handle(AIS_InteractiveContext)& Ctx)
652{
653 AIS_InteractiveObject::SetContext(Ctx);
654 ComputeFields();
655
656}