1 // Created on: 1995-08-02
2 // Created by: Arnaud BOUZY/Odile Olivier
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
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.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 #include <AIS_Plane.ixx>
19 #include <TColgp_Array1OfPnt.hxx>
20 #include <TColgp_HArray1OfPnt.hxx>
22 #include <Precision.hxx>
26 #include <Graphic3d_ArrayOfQuadrangles.hxx>
27 #include <Graphic3d_Group.hxx>
28 #include <Prs3d_Drawer.hxx>
29 #include <Prs3d_LineAspect.hxx>
30 #include <Prs3d_PlaneAspect.hxx>
31 #include <Prs3d_DatumAspect.hxx>
32 #include <Prs3d_ShadingAspect.hxx>
33 #include <StdPrs_Plane.hxx>
34 #include <StdPrs_ShadedSurface.hxx>
36 #include <Geom_Plane.hxx>
37 #include <GeomAdaptor_Surface.hxx>
39 #include <Aspect_TypeOfLine.hxx>
40 #include <Graphic3d_AspectLine3d.hxx>
41 #include <Graphic3d_Structure.hxx>
42 #include <Graphic3d_MaterialAspect.hxx>
43 #include <Graphic3d_AspectFillArea3d.hxx>
45 #include <DsgPrs_XYZPlanePresentation.hxx>
46 #include <DsgPrs_ShadedPlanePresentation.hxx>
47 #include <UnitsAPI.hxx>
49 #include <Select3D_SensitiveTriangulation.hxx>
50 #include <SelectBasics_EntityOwner.hxx>
51 #include <SelectMgr_EntityOwner.hxx>
52 #include <StdPrs_ShadedShape.hxx>
54 #include <Poly_Triangulation.hxx>
56 #include <TCollection_AsciiString.hxx>
58 //=======================================================================
59 //function : AIS_Plane
61 //=======================================================================
62 AIS_Plane::AIS_Plane(const Handle(Geom_Plane)& aComponent,
63 const Standard_Boolean aCurrentMode):
64 myComponent(aComponent),
65 myCenter(gp_Pnt(0.,0.,0.)),
66 myCurrentMode(aCurrentMode),
67 myAutomaticPosition(Standard_True),
68 myTypeOfPlane(AIS_TOPL_Unknown),
69 myIsXYZPlane(Standard_False),
70 myTypeOfSensitivity (Select3D_TOS_BOUNDARY)
72 InitDrawerAttributes();
75 //=======================================================================
76 //function : AIS_Plane
77 //purpose : avec une position
78 //=======================================================================
79 AIS_Plane::AIS_Plane(const Handle(Geom_Plane)& aComponent,
80 const gp_Pnt& aCenter,
81 const Standard_Boolean aCurrentMode):
82 myComponent(aComponent),
84 myCurrentMode(aCurrentMode),
85 myAutomaticPosition(Standard_True),
86 myTypeOfPlane(AIS_TOPL_Unknown),
87 myIsXYZPlane(Standard_False),
88 myTypeOfSensitivity (Select3D_TOS_BOUNDARY)
90 InitDrawerAttributes();
93 //=======================================================================
94 //function : AIS_Plane
96 //=======================================================================
97 AIS_Plane::AIS_Plane(const Handle(Geom_Plane)& aComponent,
98 const gp_Pnt& aCenter,
101 const Standard_Boolean aCurrentMode):
102 myComponent(aComponent),
106 myCurrentMode(aCurrentMode),
107 myAutomaticPosition(Standard_False),
108 myTypeOfPlane(AIS_TOPL_Unknown),
109 myIsXYZPlane(Standard_False),
110 myTypeOfSensitivity (Select3D_TOS_BOUNDARY)
112 InitDrawerAttributes();
116 //=======================================================================
117 //function : AIS_Plane
118 //purpose : XYPlane, XZPlane, YZPlane
119 //=======================================================================
120 AIS_Plane::AIS_Plane(const Handle(Geom_Axis2Placement)& aComponent,
121 const AIS_TypeOfPlane aPlaneType,
122 const Standard_Boolean aCurrentMode):
124 myCurrentMode(aCurrentMode),
125 myAutomaticPosition(Standard_True),
126 myTypeOfPlane(aPlaneType),
127 myIsXYZPlane(Standard_True),
128 myTypeOfSensitivity (Select3D_TOS_BOUNDARY)
130 InitDrawerAttributes();
137 //=======================================================================
138 //function : SetComponent
140 //=======================================================================
142 void AIS_Plane::SetComponent(const Handle(Geom_Plane)& aComponent)
144 myComponent = aComponent;
145 myTypeOfPlane = AIS_TOPL_Unknown;
146 myIsXYZPlane = Standard_False;
147 //myCenter = gp_Pnt(0.,0.,0.);
148 myAutomaticPosition = Standard_True;
151 //=======================================================================
154 //=======================================================================
156 AIS_KindOfInteractive AIS_Plane::Type() const
157 {return AIS_KOI_Datum;}
160 //=======================================================================
161 //function : Signature
163 //=======================================================================
165 Standard_Integer AIS_Plane::Signature() const
169 //=======================================================================
170 //function : Axis2Placement
172 //=======================================================================
174 Handle(Geom_Axis2Placement) AIS_Plane::Axis2Placement()
176 Handle(Geom_Axis2Placement) Bid;
177 return IsXYZPlane() ? myAx2:Bid;
179 //=======================================================================
180 //function : SetAxis2Placement
182 //=======================================================================
184 void AIS_Plane::SetAxis2Placement(const Handle(Geom_Axis2Placement)& aComponent,
185 const AIS_TypeOfPlane aPlaneType)
187 myTypeOfPlane = aPlaneType;
188 myIsXYZPlane = Standard_True;
190 myAutomaticPosition = Standard_True;
194 //=======================================================================
195 //function : PlaneAttributes
197 //=======================================================================
198 Standard_Boolean AIS_Plane::PlaneAttributes(Handle(Geom_Plane)& aComponent,
203 Standard_Boolean aStatus (Standard_False);
204 if (!myAutomaticPosition){
205 aComponent = myComponent;
209 aStatus = Standard_True;
214 //=======================================================================
215 //function : SetPlaneAttributes
217 //=======================================================================
218 void AIS_Plane::SetPlaneAttributes(const Handle(Geom_Plane)& aComponent,
219 const gp_Pnt& aCenter,
223 myAutomaticPosition = Standard_False;
224 myComponent = aComponent;
228 myTypeOfPlane = AIS_TOPL_Unknown;
229 myIsXYZPlane = Standard_False;
232 //=======================================================================
235 //=======================================================================
236 void AIS_Plane::Compute(const Handle(PrsMgr_PresentationManager3d)& ,
237 const Handle(Prs3d_Presentation)& aPresentation,
238 const Standard_Integer aMode)
240 aPresentation->Clear();
243 aPresentation->SetInfiniteState(myInfiniteState);
244 myDrawer->PlaneAspect()->EdgesAspect()->SetWidth(myCurrentMode == 0? 1 : 3);
253 const Handle(Geom_Plane)& pl = myComponent;
254 Handle(Geom_Plane) thegoodpl (Handle(Geom_Plane)::DownCast(pl->Translated(pl->Location(),myCenter)));
255 GeomAdaptor_Surface surf(thegoodpl);
256 StdPrs_Plane::Add(aPresentation,surf,myDrawer);
259 DsgPrs_XYZPlanePresentation::Add(aPresentation,myDrawer,myCenter,myPmin,myPmax);
267 Handle(Prs3d_PlaneAspect) theaspect = myDrawer->PlaneAspect();
268 Handle(Graphic3d_Group) TheGroup = Prs3d_Root::CurrentGroup(aPresentation);
269 TheGroup->SetPrimitivesAspect(myDrawer->ShadingAspect()->Aspect());
271 const Standard_Real Xmax = 0.5*Standard_Real(theaspect->PlaneXLength());
272 const Standard_Real Ymax = 0.5*Standard_Real(theaspect->PlaneYLength());
274 Handle(Graphic3d_ArrayOfQuadrangles) aQuads = new Graphic3d_ArrayOfQuadrangles(4);
276 myComponent->D0(-Xmax,Ymax,p1);
277 aQuads->AddVertex(p1);
278 myComponent->D0(Xmax,Ymax,p1);
279 aQuads->AddVertex(p1);
280 myComponent->D0(Xmax,-Ymax,p1);
281 aQuads->AddVertex(p1);
282 myComponent->D0(-Xmax,-Ymax,p1);
283 aQuads->AddVertex(p1);
285 TheGroup->AddPrimitiveArray(aQuads);
288 DsgPrs_ShadedPlanePresentation::Add(aPresentation,myDrawer,myCenter,myPmin,myPmax);
294 void AIS_Plane::Compute(const Handle(Prs3d_Projector)& aProjector, const Handle(Geom_Transformation)& aTransformation, const Handle(Prs3d_Presentation)& aPresentation)
296 PrsMgr_PresentableObject::Compute(aProjector, aTransformation, aPresentation);
299 //=======================================================================
300 //function : ComputeSelection
302 //=======================================================================
303 void AIS_Plane::ComputeSelection (const Handle(SelectMgr_Selection)& theSelection, const Standard_Integer /*theMode*/)
305 theSelection->Clear();
306 Handle(SelectMgr_EntityOwner) aSensitiveOwner = new SelectMgr_EntityOwner (this, 10);
307 Handle(Poly_Triangulation) aSensitivePoly;
311 // plane representing rectangle
312 Standard_Real aLengthX = myDrawer->PlaneAspect()->PlaneXLength() / 2.0;
313 Standard_Real aLengthY = myDrawer->PlaneAspect()->PlaneYLength() / 2.0;
314 Handle(Geom_Plane) aPlane =
315 Handle(Geom_Plane)::DownCast (myComponent->Translated (myComponent->Location(), myCenter));
317 TColgp_Array1OfPnt aRectanglePoints (1, 4);
318 aPlane->D0 ( aLengthX, aLengthY, aRectanglePoints.ChangeValue (1));
319 aPlane->D0 ( aLengthX, -aLengthY, aRectanglePoints.ChangeValue (2));
320 aPlane->D0 (-aLengthX, -aLengthY, aRectanglePoints.ChangeValue (3));
321 aPlane->D0 (-aLengthX, aLengthY, aRectanglePoints.ChangeValue (4));
323 Poly_Array1OfTriangle aTriangles (1, 2);
324 aTriangles.ChangeValue (1) = Poly_Triangle (1, 2, 3);
325 aTriangles.ChangeValue (2) = Poly_Triangle (1, 3, 4);
327 aSensitivePoly = new Poly_Triangulation (aRectanglePoints, aTriangles);
331 // plane representing triangle
332 TColgp_Array1OfPnt aTrianglePoints (1, 3);
333 aTrianglePoints.ChangeValue (1) = myCenter;
334 aTrianglePoints.ChangeValue (2) = myPmin;
335 aTrianglePoints.ChangeValue (3) = myPmax;
337 Poly_Array1OfTriangle aTriangles (1, 1);
338 aTriangles.ChangeValue (1) = Poly_Triangle(1, 2, 3);
340 aSensitivePoly = new Poly_Triangulation (aTrianglePoints, aTriangles);
343 Standard_Boolean isSensitiveInterior = myTypeOfSensitivity == Select3D_TOS_INTERIOR;
345 Handle(Select3D_SensitiveTriangulation) aSensitive =
346 new Select3D_SensitiveTriangulation (aSensitiveOwner,
349 isSensitiveInterior);
351 theSelection->Add(aSensitive);
354 //=======================================================================
357 //=======================================================================
358 void AIS_Plane::SetSize(const Standard_Real aLength)
360 SetSize(aLength,aLength);
363 void AIS_Plane::SetSize(const Standard_Real aXLength,
364 const Standard_Real aYLength)
366 //if the plane already has a proper color or size,
367 //there is already a specific PlaneAspect and DatumAspect
369 Handle(Prs3d_PlaneAspect) PA;
370 Handle(Prs3d_DatumAspect) DA;
372 PA = myDrawer->PlaneAspect();
373 DA = myDrawer->DatumAspect();
375 Standard_Boolean yenavaitPA(Standard_True),yenavaitDA(Standard_True);
376 if(myDrawer->HasLink() && myDrawer->Link()->PlaneAspect() == PA){
377 yenavaitPA = Standard_False;
378 PA = new Prs3d_PlaneAspect();}
379 if(myDrawer->HasLink() && myDrawer->Link()->DatumAspect() == DA){
380 yenavaitDA = Standard_False;
381 DA = new Prs3d_DatumAspect();
384 PA->SetPlaneLength(aXLength,aYLength);
385 DA->SetAxisLength(aXLength,aYLength,aXLength);
388 myDrawer->SetPlaneAspect(PA);
390 myDrawer->SetDatumAspect(DA);
393 myHasOwnSize = Standard_True;
399 //=======================================================================
400 //function : UnsetSize
401 //purpose : If there is a color, the size is restaured from the drawer of the context...
402 //=======================================================================
403 void AIS_Plane::UnsetSize()
406 if(!myHasOwnSize) return;
409 myDrawer->SetPlaneAspect (Handle(Prs3d_PlaneAspect)());
410 myDrawer->SetDatumAspect (Handle(Prs3d_DatumAspect)());
413 const Handle(Prs3d_PlaneAspect) PA = myDrawer->HasLink() ? myDrawer->Link()->PlaneAspect() :
414 new Prs3d_PlaneAspect();
415 const Handle(Prs3d_DatumAspect) DA = myDrawer->HasLink() ? myDrawer->Link()->DatumAspect() :
416 new Prs3d_DatumAspect();
418 myDrawer->PlaneAspect()->SetPlaneLength(PA->PlaneXLength(),PA->PlaneYLength());
419 myDrawer->DatumAspect()->SetAxisLength(DA->FirstAxisLength(),
420 DA->SecondAxisLength(),
421 DA->ThirdAxisLength());
424 myHasOwnSize = Standard_False;
430 //=======================================================================
433 //=======================================================================
435 Standard_Boolean AIS_Plane::Size(Standard_Real& X,Standard_Real& Y) const
437 X = myDrawer->PlaneAspect()->PlaneXLength();
438 Y = myDrawer->PlaneAspect()->PlaneYLength();
439 return Abs(X-Y)<=Precision::Confusion();
443 //=======================================================================
444 //function : SetColor
446 //=======================================================================
449 void AIS_Plane::SetColor(const Quantity_NameOfColor aCol)
451 SetColor(Quantity_Color(aCol));
454 void AIS_Plane::SetColor(const Quantity_Color &aCol)
456 // if the plane already has its proper size, there is an already created planeaspect
457 // Standard_Boolean yenadeja = hasOwnColor || myHasOwnSize;
458 Handle(Prs3d_PlaneAspect) PA;
459 Handle(Prs3d_DatumAspect) DA;
461 PA = myDrawer->PlaneAspect();
462 DA = myDrawer->DatumAspect();
464 Standard_Boolean yenavaitPA(Standard_True),yenavaitDA(Standard_True);
465 if(myDrawer->HasLink() && myDrawer->Link()->PlaneAspect() == PA){
466 yenavaitPA = Standard_False;
467 PA = new Prs3d_PlaneAspect();}
468 if(myDrawer->HasLink() && myDrawer->Link()->DatumAspect() == DA){
469 yenavaitDA = Standard_False;
470 DA = new Prs3d_DatumAspect();
473 PA->EdgesAspect()->SetColor(aCol);
474 DA->FirstAxisAspect()->SetColor(aCol);
475 DA->SecondAxisAspect()->SetColor(aCol);
476 DA->ThirdAxisAspect()->SetColor(aCol);
479 myDrawer->SetPlaneAspect(PA);
481 myDrawer->SetDatumAspect(DA);
483 myDrawer->ShadingAspect()->SetColor(aCol);
485 hasOwnColor=Standard_True;
488 //=======================================================================
489 //function : SetColor
491 //=======================================================================
492 void AIS_Plane::UnsetColor()
494 if(!hasOwnColor) return;
497 myDrawer->SetPlaneAspect (Handle(Prs3d_PlaneAspect)());
498 myDrawer->SetDatumAspect (Handle(Prs3d_DatumAspect)());
501 const Handle(Prs3d_PlaneAspect) PA = myDrawer->HasLink() ? myDrawer->Link()->PlaneAspect() :
502 new Prs3d_PlaneAspect();
503 // const Handle(Prs3d_DatumAspect)& DA = myDrawer->Link()->DatumAspect();
504 Quantity_Color C;Aspect_TypeOfLine T;Standard_Real W;
505 PA->EdgesAspect()->Aspect()->Values(C,T,W);
506 Quantity_NameOfColor Col = C.Name();
507 myDrawer->PlaneAspect()->EdgesAspect()->SetColor(Col);
509 myDrawer->DatumAspect()->FirstAxisAspect()->SetColor(Col);
510 myDrawer->DatumAspect()->SecondAxisAspect()->SetColor(Col);
511 myDrawer->DatumAspect()->ThirdAxisAspect()->SetColor(Col);
515 hasOwnColor=Standard_False;
519 //=======================================================================
520 //function : ComputeFrame
522 //=======================================================================
523 void AIS_Plane::ComputeFrame()
526 const Handle(Geom_Plane)& pl = myComponent;
529 if (myAutomaticPosition) {
530 ElSLib::Parameters(pl->Pln(),myCenter,U,V);
531 pl->D0 (U, V, myCenter);
534 Handle(Geom_Plane) thegoodpl (Handle(Geom_Plane)::DownCast(pl->Translated(pl->Location(),myCenter)));
535 ElSLib::Parameters(thegoodpl->Pln(),myPmin,U,V);
539 if (U < 10*Precision::Confusion()) U=0.1;
540 if (V < 10*Precision::Confusion()) V=0.1;
542 myDrawer->PlaneAspect()->SetPlaneLength(U,V);
546 //=======================================================================
548 //purpose : to avoid warning
549 //=======================================================================
550 void AIS_Plane::Compute(const Handle(Prs3d_Projector)&,
551 const Handle(Prs3d_Presentation)&)
555 //=======================================================================
556 //function : ComputeFields
558 //=======================================================================
559 void AIS_Plane::ComputeFields()
562 Handle (Prs3d_DatumAspect) DA = myDrawer->DatumAspect();
564 gp_Pnt Orig = myAx2->Ax2().Location();
565 gp_Dir oX = myAx2->Ax2().XDirection();
566 gp_Dir oY = myAx2->Ax2().YDirection();
567 gp_Dir oZ = myAx2->Ax2().Direction();
569 Standard_Real xo,yo,zo,x1,y1,z1,x2,y2,z2,x3,y3,z3,x4=0,y4=0,z4=0;
570 Standard_Real x5=0,y5=0,z5=0;
571 Orig.Coord(xo,yo,zo);
575 Standard_Real DS1 = DA->FirstAxisLength();
576 Standard_Real DS2 = DA->SecondAxisLength();
577 Standard_Real DS3 = DA->ThirdAxisLength();
580 switch (myTypeOfPlane) {
581 case AIS_TOPL_XYPlane:
584 myComponent = new Geom_Plane(XYP);
585 x4=xo+x1*DS1; y4=yo+y1*DS1; z4=zo+z1*DS1;
586 x5=xo+x2*DS2; y5=yo+y2*DS2; z5=zo+z2*DS2;
589 case AIS_TOPL_XZPlane:
592 myComponent = new Geom_Plane(XZP);
593 x4=xo+x1*DS1; y4=yo+y1*DS1; z4=zo+z1*DS1;
594 x5=xo+x3*DS3; y5=yo+y3*DS3; z5=zo+z3*DS3;
597 case AIS_TOPL_YZPlane:
600 myComponent = new Geom_Plane(XZP);
601 x4=xo+x2*DS2; y4=yo+y2*DS2; z4=zo+z2*DS2;
602 x5=xo+x3*DS3; y5=yo+y3*DS3; z5=zo+z3*DS3;
608 myPmin.SetCoord(x4,y4,z4);
609 myPmax.SetCoord(x5,y5,z5);
613 //=======================================================================
614 //function : InitDrawerAttributes
616 //=======================================================================
618 void AIS_Plane::InitDrawerAttributes()
621 Handle(Prs3d_ShadingAspect) shasp = new Prs3d_ShadingAspect();
622 shasp->SetMaterial(Graphic3d_NOM_PLASTIC);
623 shasp->SetColor(Quantity_NOC_GRAY40);
624 myDrawer->SetShadingAspect(shasp);
625 Handle(Graphic3d_AspectFillArea3d) asf = shasp->Aspect();
626 Graphic3d_MaterialAspect asp = asf->FrontMaterial();
627 asp.SetTransparency(0.8);
628 asf->SetFrontMaterial(asp);
629 asf->SetBackMaterial(asp);
634 //=======================================================================
635 //function : AcceptDisplayMode
637 //=======================================================================
639 Standard_Boolean AIS_Plane::
640 AcceptDisplayMode(const Standard_Integer aMode) const
644 //=======================================================================
645 //function : SetContext
647 //=======================================================================
649 void AIS_Plane::SetContext(const Handle(AIS_InteractiveContext)& Ctx)
651 AIS_InteractiveObject::SetContext(Ctx);