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.hxx>
19 #include <AIS_InteractiveContext.hxx>
20 #include <Aspect_TypeOfLine.hxx>
21 #include <DsgPrs_ShadedPlanePresentation.hxx>
22 #include <DsgPrs_XYZPlanePresentation.hxx>
24 #include <Geom_Axis2Placement.hxx>
25 #include <Geom_Plane.hxx>
26 #include <Geom_Transformation.hxx>
27 #include <GeomAdaptor_Surface.hxx>
29 #include <Graphic3d_ArrayOfQuadrangles.hxx>
30 #include <Graphic3d_AspectFillArea3d.hxx>
31 #include <Graphic3d_AspectLine3d.hxx>
32 #include <Graphic3d_Group.hxx>
33 #include <Graphic3d_MaterialAspect.hxx>
34 #include <Graphic3d_Structure.hxx>
35 #include <Poly_Triangulation.hxx>
36 #include <Precision.hxx>
37 #include <Prs3d_DatumAspect.hxx>
38 #include <Prs3d_Drawer.hxx>
39 #include <Prs3d_LineAspect.hxx>
40 #include <Prs3d_PlaneAspect.hxx>
41 #include <Prs3d_Presentation.hxx>
42 #include <Prs3d_Projector.hxx>
43 #include <Prs3d_ShadingAspect.hxx>
44 #include <Quantity_Color.hxx>
45 #include <Select3D_SensitiveTriangulation.hxx>
46 #include <SelectBasics_EntityOwner.hxx>
47 #include <SelectMgr_EntityOwner.hxx>
48 #include <Standard_Type.hxx>
49 #include <StdPrs_Plane.hxx>
50 #include <StdPrs_ShadedShape.hxx>
51 #include <StdPrs_ShadedSurface.hxx>
52 #include <TColgp_Array1OfPnt.hxx>
53 #include <TColgp_HArray1OfPnt.hxx>
54 #include <TCollection_AsciiString.hxx>
55 #include <UnitsAPI.hxx>
57 IMPLEMENT_STANDARD_RTTIEXT(AIS_Plane,AIS_InteractiveObject)
59 //=======================================================================
60 //function : AIS_Plane
62 //=======================================================================
63 AIS_Plane::AIS_Plane(const Handle(Geom_Plane)& aComponent,
64 const Standard_Boolean aCurrentMode):
65 myComponent(aComponent),
66 myCenter(gp_Pnt(0.,0.,0.)),
67 myCurrentMode(aCurrentMode),
68 myAutomaticPosition(Standard_True),
69 myTypeOfPlane(AIS_TOPL_Unknown),
70 myIsXYZPlane(Standard_False),
71 myTypeOfSensitivity (Select3D_TOS_BOUNDARY)
73 InitDrawerAttributes();
76 //=======================================================================
77 //function : AIS_Plane
78 //purpose : avec une position
79 //=======================================================================
80 AIS_Plane::AIS_Plane(const Handle(Geom_Plane)& aComponent,
81 const gp_Pnt& aCenter,
82 const Standard_Boolean aCurrentMode):
83 myComponent(aComponent),
85 myCurrentMode(aCurrentMode),
86 myAutomaticPosition(Standard_True),
87 myTypeOfPlane(AIS_TOPL_Unknown),
88 myIsXYZPlane(Standard_False),
89 myTypeOfSensitivity (Select3D_TOS_BOUNDARY)
91 InitDrawerAttributes();
94 //=======================================================================
95 //function : AIS_Plane
97 //=======================================================================
98 AIS_Plane::AIS_Plane(const Handle(Geom_Plane)& aComponent,
99 const gp_Pnt& aCenter,
102 const Standard_Boolean aCurrentMode):
103 myComponent(aComponent),
107 myCurrentMode(aCurrentMode),
108 myAutomaticPosition(Standard_False),
109 myTypeOfPlane(AIS_TOPL_Unknown),
110 myIsXYZPlane(Standard_False),
111 myTypeOfSensitivity (Select3D_TOS_BOUNDARY)
113 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();
135 //=======================================================================
136 //function : SetComponent
138 //=======================================================================
140 void AIS_Plane::SetComponent(const Handle(Geom_Plane)& aComponent)
142 myComponent = aComponent;
143 myTypeOfPlane = AIS_TOPL_Unknown;
144 myIsXYZPlane = Standard_False;
145 //myCenter = gp_Pnt(0.,0.,0.);
146 myAutomaticPosition = Standard_True;
149 //=======================================================================
152 //=======================================================================
154 AIS_KindOfInteractive AIS_Plane::Type() const
155 {return AIS_KOI_Datum;}
158 //=======================================================================
159 //function : Signature
161 //=======================================================================
163 Standard_Integer AIS_Plane::Signature() const
167 //=======================================================================
168 //function : Axis2Placement
170 //=======================================================================
172 Handle(Geom_Axis2Placement) AIS_Plane::Axis2Placement()
174 Handle(Geom_Axis2Placement) Bid;
175 return IsXYZPlane() ? myAx2:Bid;
177 //=======================================================================
178 //function : SetAxis2Placement
180 //=======================================================================
182 void AIS_Plane::SetAxis2Placement(const Handle(Geom_Axis2Placement)& aComponent,
183 const AIS_TypeOfPlane aPlaneType)
185 myTypeOfPlane = aPlaneType;
186 myIsXYZPlane = Standard_True;
188 myAutomaticPosition = Standard_True;
192 //=======================================================================
193 //function : PlaneAttributes
195 //=======================================================================
196 Standard_Boolean AIS_Plane::PlaneAttributes(Handle(Geom_Plane)& aComponent,
201 Standard_Boolean aStatus (Standard_False);
202 if (!myAutomaticPosition){
203 aComponent = myComponent;
207 aStatus = Standard_True;
212 //=======================================================================
213 //function : SetPlaneAttributes
215 //=======================================================================
216 void AIS_Plane::SetPlaneAttributes(const Handle(Geom_Plane)& aComponent,
217 const gp_Pnt& aCenter,
221 myAutomaticPosition = Standard_False;
222 myComponent = aComponent;
226 myTypeOfPlane = AIS_TOPL_Unknown;
227 myIsXYZPlane = Standard_False;
230 //=======================================================================
233 //=======================================================================
234 void AIS_Plane::Compute(const Handle(PrsMgr_PresentationManager3d)& ,
235 const Handle(Prs3d_Presentation)& aPresentation,
236 const Standard_Integer aMode)
239 aPresentation->SetInfiniteState(myInfiniteState);
240 myDrawer->PlaneAspect()->EdgesAspect()->SetWidth(myCurrentMode == 0? 1 : 3);
249 const Handle(Geom_Plane)& pl = myComponent;
250 Handle(Geom_Plane) thegoodpl (Handle(Geom_Plane)::DownCast(pl->Translated(pl->Location(),myCenter)));
251 GeomAdaptor_Surface surf(thegoodpl);
252 StdPrs_Plane::Add(aPresentation,surf,myDrawer);
255 DsgPrs_XYZPlanePresentation::Add(aPresentation,myDrawer,myCenter,myPmin,myPmax);
263 Handle(Prs3d_PlaneAspect) theaspect = myDrawer->PlaneAspect();
264 Handle(Graphic3d_Group) TheGroup = Prs3d_Root::CurrentGroup(aPresentation);
265 TheGroup->SetPrimitivesAspect(myDrawer->ShadingAspect()->Aspect());
267 const Standard_Real Xmax = 0.5*Standard_Real(theaspect->PlaneXLength());
268 const Standard_Real Ymax = 0.5*Standard_Real(theaspect->PlaneYLength());
270 Handle(Graphic3d_ArrayOfQuadrangles) aQuads = new Graphic3d_ArrayOfQuadrangles(4);
272 myComponent->D0(-Xmax,Ymax,p1);
273 aQuads->AddVertex(p1);
274 myComponent->D0(Xmax,Ymax,p1);
275 aQuads->AddVertex(p1);
276 myComponent->D0(Xmax,-Ymax,p1);
277 aQuads->AddVertex(p1);
278 myComponent->D0(-Xmax,-Ymax,p1);
279 aQuads->AddVertex(p1);
281 TheGroup->AddPrimitiveArray(aQuads);
284 DsgPrs_ShadedPlanePresentation::Add(aPresentation,myDrawer,myCenter,myPmin,myPmax);
290 void AIS_Plane::Compute(const Handle(Prs3d_Projector)& aProjector, const Handle(Geom_Transformation)& aTransformation, const Handle(Prs3d_Presentation)& aPresentation)
292 PrsMgr_PresentableObject::Compute(aProjector, aTransformation, aPresentation);
295 //=======================================================================
296 //function : ComputeSelection
298 //=======================================================================
299 void AIS_Plane::ComputeSelection (const Handle(SelectMgr_Selection)& theSelection, const Standard_Integer /*theMode*/)
301 theSelection->Clear();
302 Handle(SelectMgr_EntityOwner) aSensitiveOwner = new SelectMgr_EntityOwner (this, 10);
303 Handle(Poly_Triangulation) aSensitivePoly;
307 // plane representing rectangle
308 Standard_Real aLengthX = myDrawer->PlaneAspect()->PlaneXLength() / 2.0;
309 Standard_Real aLengthY = myDrawer->PlaneAspect()->PlaneYLength() / 2.0;
310 Handle(Geom_Plane) aPlane =
311 Handle(Geom_Plane)::DownCast (myComponent->Translated (myComponent->Location(), myCenter));
313 TColgp_Array1OfPnt aRectanglePoints (1, 4);
314 aPlane->D0 ( aLengthX, aLengthY, aRectanglePoints.ChangeValue (1));
315 aPlane->D0 ( aLengthX, -aLengthY, aRectanglePoints.ChangeValue (2));
316 aPlane->D0 (-aLengthX, -aLengthY, aRectanglePoints.ChangeValue (3));
317 aPlane->D0 (-aLengthX, aLengthY, aRectanglePoints.ChangeValue (4));
319 Poly_Array1OfTriangle aTriangles (1, 2);
320 aTriangles.ChangeValue (1) = Poly_Triangle (1, 2, 3);
321 aTriangles.ChangeValue (2) = Poly_Triangle (1, 3, 4);
323 aSensitivePoly = new Poly_Triangulation (aRectanglePoints, aTriangles);
327 // plane representing triangle
328 TColgp_Array1OfPnt aTrianglePoints (1, 3);
329 aTrianglePoints.ChangeValue (1) = myCenter;
330 aTrianglePoints.ChangeValue (2) = myPmin;
331 aTrianglePoints.ChangeValue (3) = myPmax;
333 Poly_Array1OfTriangle aTriangles (1, 1);
334 aTriangles.ChangeValue (1) = Poly_Triangle(1, 2, 3);
336 aSensitivePoly = new Poly_Triangulation (aTrianglePoints, aTriangles);
339 Standard_Boolean isSensitiveInterior = myTypeOfSensitivity == Select3D_TOS_INTERIOR;
341 Handle(Select3D_SensitiveTriangulation) aSensitive =
342 new Select3D_SensitiveTriangulation (aSensitiveOwner,
345 isSensitiveInterior);
347 theSelection->Add(aSensitive);
350 //=======================================================================
353 //=======================================================================
354 void AIS_Plane::SetSize(const Standard_Real aLength)
356 SetSize(aLength,aLength);
359 void AIS_Plane::SetSize(const Standard_Real aXLength,
360 const Standard_Real aYLength)
362 //if the plane already has a proper color or size,
363 //there is already a specific PlaneAspect and DatumAspect
365 Handle(Prs3d_PlaneAspect) PA;
366 Handle(Prs3d_DatumAspect) DA;
368 PA = myDrawer->PlaneAspect();
369 DA = myDrawer->DatumAspect();
371 Standard_Boolean yenavaitPA(Standard_True),yenavaitDA(Standard_True);
372 if(myDrawer->HasLink() && myDrawer->Link()->PlaneAspect() == PA){
373 yenavaitPA = Standard_False;
374 PA = new Prs3d_PlaneAspect();}
375 if(myDrawer->HasLink() && myDrawer->Link()->DatumAspect() == DA){
376 yenavaitDA = Standard_False;
377 DA = new Prs3d_DatumAspect();
380 PA->SetPlaneLength(aXLength,aYLength);
381 DA->SetAxisLength(aXLength,aYLength,aXLength);
384 myDrawer->SetPlaneAspect(PA);
386 myDrawer->SetDatumAspect(DA);
389 myHasOwnSize = Standard_True;
395 //=======================================================================
396 //function : UnsetSize
397 //purpose : If there is a color, the size is restaured from the drawer of the context...
398 //=======================================================================
399 void AIS_Plane::UnsetSize()
402 if(!myHasOwnSize) return;
405 myDrawer->SetPlaneAspect (Handle(Prs3d_PlaneAspect)());
406 myDrawer->SetDatumAspect (Handle(Prs3d_DatumAspect)());
409 const Handle(Prs3d_PlaneAspect) PA = myDrawer->HasLink() ? myDrawer->Link()->PlaneAspect() :
410 new Prs3d_PlaneAspect();
411 const Handle(Prs3d_DatumAspect) DA = myDrawer->HasLink() ? myDrawer->Link()->DatumAspect() :
412 new Prs3d_DatumAspect();
414 myDrawer->PlaneAspect()->SetPlaneLength(PA->PlaneXLength(),PA->PlaneYLength());
415 myDrawer->DatumAspect()->SetAxisLength(DA->AxisLength(Prs3d_DP_XAxis),
416 DA->AxisLength(Prs3d_DP_YAxis),
417 DA->AxisLength(Prs3d_DP_ZAxis));
420 myHasOwnSize = Standard_False;
426 //=======================================================================
429 //=======================================================================
431 Standard_Boolean AIS_Plane::Size(Standard_Real& X,Standard_Real& Y) const
433 X = myDrawer->PlaneAspect()->PlaneXLength();
434 Y = myDrawer->PlaneAspect()->PlaneYLength();
435 return Abs(X-Y)<=Precision::Confusion();
438 //=======================================================================
439 //function : SetColor
441 //=======================================================================
442 void AIS_Plane::SetColor(const Quantity_Color &aCol)
444 // if the plane already has its proper size, there is an already created planeaspect
445 // Standard_Boolean yenadeja = hasOwnColor || myHasOwnSize;
446 Handle(Prs3d_PlaneAspect) PA;
447 Handle(Prs3d_DatumAspect) DA;
449 PA = myDrawer->PlaneAspect();
450 DA = myDrawer->DatumAspect();
452 Standard_Boolean yenavaitPA(Standard_True),yenavaitDA(Standard_True);
453 if(myDrawer->HasLink() && myDrawer->Link()->PlaneAspect() == PA){
454 yenavaitPA = Standard_False;
455 PA = new Prs3d_PlaneAspect();}
456 if(myDrawer->HasLink() && myDrawer->Link()->DatumAspect() == DA){
457 yenavaitDA = Standard_False;
458 DA = new Prs3d_DatumAspect();
461 PA->EdgesAspect()->SetColor(aCol);
462 DA->LineAspect(Prs3d_DP_XAxis)->SetColor(aCol);
463 DA->LineAspect(Prs3d_DP_YAxis)->SetColor(aCol);
464 DA->LineAspect(Prs3d_DP_ZAxis)->SetColor(aCol);
467 myDrawer->SetPlaneAspect(PA);
469 myDrawer->SetDatumAspect(DA);
471 myDrawer->ShadingAspect()->SetColor(aCol);
473 hasOwnColor=Standard_True;
474 myDrawer->SetColor (aCol);
476 //=======================================================================
477 //function : SetColor
479 //=======================================================================
480 void AIS_Plane::UnsetColor()
482 if(!hasOwnColor) return;
485 myDrawer->SetPlaneAspect (Handle(Prs3d_PlaneAspect)());
486 myDrawer->SetDatumAspect (Handle(Prs3d_DatumAspect)());
489 const Handle(Prs3d_PlaneAspect) PA = myDrawer->HasLink() ? myDrawer->Link()->PlaneAspect() :
490 new Prs3d_PlaneAspect();
491 Quantity_Color Col = PA->EdgesAspect()->Aspect()->Color();
492 myDrawer->PlaneAspect()->EdgesAspect()->SetColor(Col);
494 myDrawer->DatumAspect()->LineAspect(Prs3d_DP_XAxis)->SetColor(Col);
495 myDrawer->DatumAspect()->LineAspect(Prs3d_DP_YAxis)->SetColor(Col);
496 myDrawer->DatumAspect()->LineAspect(Prs3d_DP_ZAxis)->SetColor(Col);
500 hasOwnColor=Standard_False;
504 //=======================================================================
505 //function : ComputeFrame
507 //=======================================================================
508 void AIS_Plane::ComputeFrame()
511 const Handle(Geom_Plane)& pl = myComponent;
514 if (myAutomaticPosition) {
515 ElSLib::Parameters(pl->Pln(),myCenter,U,V);
516 pl->D0 (U, V, myCenter);
519 Handle(Geom_Plane) thegoodpl (Handle(Geom_Plane)::DownCast(pl->Translated(pl->Location(),myCenter)));
520 ElSLib::Parameters(thegoodpl->Pln(),myPmin,U,V);
524 if (U < 10*Precision::Confusion()) U=0.1;
525 if (V < 10*Precision::Confusion()) V=0.1;
527 myDrawer->PlaneAspect()->SetPlaneLength(U,V);
531 //=======================================================================
533 //purpose : to avoid warning
534 //=======================================================================
535 void AIS_Plane::Compute(const Handle(Prs3d_Projector)&,
536 const Handle(Prs3d_Presentation)&)
540 //=======================================================================
541 //function : ComputeFields
543 //=======================================================================
544 void AIS_Plane::ComputeFields()
547 Handle (Prs3d_DatumAspect) DA = myDrawer->DatumAspect();
549 gp_Pnt Orig = myAx2->Ax2().Location();
550 gp_Dir oX = myAx2->Ax2().XDirection();
551 gp_Dir oY = myAx2->Ax2().YDirection();
552 gp_Dir oZ = myAx2->Ax2().Direction();
554 Standard_Real xo,yo,zo,x1,y1,z1,x2,y2,z2,x3,y3,z3,x4=0,y4=0,z4=0;
555 Standard_Real x5=0,y5=0,z5=0;
556 Orig.Coord(xo,yo,zo);
560 Standard_Real DS1 = DA->AxisLength(Prs3d_DP_XAxis);
561 Standard_Real DS2 = DA->AxisLength(Prs3d_DP_YAxis);
562 Standard_Real DS3 = DA->AxisLength(Prs3d_DP_ZAxis);
565 switch (myTypeOfPlane) {
566 case AIS_TOPL_XYPlane:
569 myComponent = new Geom_Plane(XYP);
570 x4=xo+x1*DS1; y4=yo+y1*DS1; z4=zo+z1*DS1;
571 x5=xo+x2*DS2; y5=yo+y2*DS2; z5=zo+z2*DS2;
574 case AIS_TOPL_XZPlane:
577 myComponent = new Geom_Plane(XZP);
578 x4=xo+x1*DS1; y4=yo+y1*DS1; z4=zo+z1*DS1;
579 x5=xo+x3*DS3; y5=yo+y3*DS3; z5=zo+z3*DS3;
582 case AIS_TOPL_YZPlane:
585 myComponent = new Geom_Plane(XZP);
586 x4=xo+x2*DS2; y4=yo+y2*DS2; z4=zo+z2*DS2;
587 x5=xo+x3*DS3; y5=yo+y3*DS3; z5=zo+z3*DS3;
593 myPmin.SetCoord(x4,y4,z4);
594 myPmax.SetCoord(x5,y5,z5);
598 //=======================================================================
599 //function : InitDrawerAttributes
601 //=======================================================================
603 void AIS_Plane::InitDrawerAttributes()
605 Handle(Prs3d_ShadingAspect) shasp = new Prs3d_ShadingAspect();
606 shasp->SetMaterial(Graphic3d_NOM_PLASTIC);
607 shasp->SetColor(Quantity_NOC_GRAY40);
608 myDrawer->SetShadingAspect(shasp);
609 Handle(Graphic3d_AspectFillArea3d) asf = shasp->Aspect();
610 Graphic3d_MaterialAspect asp = asf->FrontMaterial();
611 asp.SetTransparency (0.8f);
612 asf->SetFrontMaterial(asp);
613 asf->SetBackMaterial(asp);
616 //=======================================================================
617 //function : AcceptDisplayMode
619 //=======================================================================
621 Standard_Boolean AIS_Plane::
622 AcceptDisplayMode(const Standard_Integer aMode) const
626 //=======================================================================
627 //function : SetContext
629 //=======================================================================
631 void AIS_Plane::SetContext(const Handle(AIS_InteractiveContext)& Ctx)
633 AIS_InteractiveObject::SetContext(Ctx);