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 <SelectMgr_EntityOwner.hxx>
47 #include <Standard_Type.hxx>
48 #include <StdPrs_Plane.hxx>
49 #include <StdPrs_ShadedShape.hxx>
50 #include <StdPrs_ShadedSurface.hxx>
51 #include <TColgp_Array1OfPnt.hxx>
52 #include <TColgp_HArray1OfPnt.hxx>
53 #include <TCollection_AsciiString.hxx>
54 #include <UnitsAPI.hxx>
56 IMPLEMENT_STANDARD_RTTIEXT(AIS_Plane,AIS_InteractiveObject)
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();
115 //=======================================================================
116 //function : AIS_Plane
117 //purpose : XYPlane, XZPlane, YZPlane
118 //=======================================================================
119 AIS_Plane::AIS_Plane(const Handle(Geom_Axis2Placement)& aComponent,
120 const AIS_TypeOfPlane aPlaneType,
121 const Standard_Boolean aCurrentMode):
123 myCurrentMode(aCurrentMode),
124 myAutomaticPosition(Standard_True),
125 myTypeOfPlane(aPlaneType),
126 myIsXYZPlane(Standard_True),
127 myTypeOfSensitivity (Select3D_TOS_BOUNDARY)
129 InitDrawerAttributes();
134 //=======================================================================
135 //function : SetComponent
137 //=======================================================================
139 void AIS_Plane::SetComponent(const Handle(Geom_Plane)& aComponent)
141 myComponent = aComponent;
142 myTypeOfPlane = AIS_TOPL_Unknown;
143 myIsXYZPlane = Standard_False;
144 //myCenter = gp_Pnt(0.,0.,0.);
145 myAutomaticPosition = Standard_True;
148 //=======================================================================
151 //=======================================================================
153 AIS_KindOfInteractive AIS_Plane::Type() const
154 {return AIS_KOI_Datum;}
157 //=======================================================================
158 //function : Signature
160 //=======================================================================
162 Standard_Integer AIS_Plane::Signature() const
166 //=======================================================================
167 //function : Axis2Placement
169 //=======================================================================
171 Handle(Geom_Axis2Placement) AIS_Plane::Axis2Placement()
173 Handle(Geom_Axis2Placement) Bid;
174 return IsXYZPlane() ? myAx2:Bid;
176 //=======================================================================
177 //function : SetAxis2Placement
179 //=======================================================================
181 void AIS_Plane::SetAxis2Placement(const Handle(Geom_Axis2Placement)& aComponent,
182 const AIS_TypeOfPlane aPlaneType)
184 myTypeOfPlane = aPlaneType;
185 myIsXYZPlane = Standard_True;
187 myAutomaticPosition = Standard_True;
191 //=======================================================================
192 //function : PlaneAttributes
194 //=======================================================================
195 Standard_Boolean AIS_Plane::PlaneAttributes(Handle(Geom_Plane)& aComponent,
200 Standard_Boolean aStatus (Standard_False);
201 if (!myAutomaticPosition){
202 aComponent = myComponent;
206 aStatus = Standard_True;
211 //=======================================================================
212 //function : SetPlaneAttributes
214 //=======================================================================
215 void AIS_Plane::SetPlaneAttributes(const Handle(Geom_Plane)& aComponent,
216 const gp_Pnt& aCenter,
220 myAutomaticPosition = Standard_False;
221 myComponent = aComponent;
225 myTypeOfPlane = AIS_TOPL_Unknown;
226 myIsXYZPlane = Standard_False;
229 //=======================================================================
232 //=======================================================================
233 void AIS_Plane::Compute(const Handle(PrsMgr_PresentationManager3d)& ,
234 const Handle(Prs3d_Presentation)& aPresentation,
235 const Standard_Integer aMode)
238 aPresentation->SetInfiniteState(myInfiniteState);
239 myDrawer->PlaneAspect()->EdgesAspect()->SetWidth(myCurrentMode == 0? 1 : 3);
248 const Handle(Geom_Plane)& pl = myComponent;
249 Handle(Geom_Plane) thegoodpl (Handle(Geom_Plane)::DownCast(pl->Translated(pl->Location(),myCenter)));
250 GeomAdaptor_Surface surf(thegoodpl);
251 StdPrs_Plane::Add(aPresentation,surf,myDrawer);
254 DsgPrs_XYZPlanePresentation::Add(aPresentation,myDrawer,myCenter,myPmin,myPmax);
262 Handle(Prs3d_PlaneAspect) theaspect = myDrawer->PlaneAspect();
263 Handle(Graphic3d_Group) TheGroup = Prs3d_Root::CurrentGroup(aPresentation);
264 TheGroup->SetPrimitivesAspect(myDrawer->ShadingAspect()->Aspect());
266 const Standard_Real Xmax = 0.5*Standard_Real(theaspect->PlaneXLength());
267 const Standard_Real Ymax = 0.5*Standard_Real(theaspect->PlaneYLength());
269 Handle(Graphic3d_ArrayOfQuadrangles) aQuads = new Graphic3d_ArrayOfQuadrangles(4);
271 myComponent->D0(-Xmax,Ymax,p1);
272 aQuads->AddVertex(p1);
273 myComponent->D0(Xmax,Ymax,p1);
274 aQuads->AddVertex(p1);
275 myComponent->D0(Xmax,-Ymax,p1);
276 aQuads->AddVertex(p1);
277 myComponent->D0(-Xmax,-Ymax,p1);
278 aQuads->AddVertex(p1);
280 TheGroup->AddPrimitiveArray(aQuads);
283 DsgPrs_ShadedPlanePresentation::Add(aPresentation,myDrawer,myCenter,myPmin,myPmax);
289 void AIS_Plane::Compute(const Handle(Prs3d_Projector)& aProjector, const Handle(Geom_Transformation)& aTransformation, const Handle(Prs3d_Presentation)& aPresentation)
291 PrsMgr_PresentableObject::Compute(aProjector, aTransformation, aPresentation);
294 //=======================================================================
295 //function : ComputeSelection
297 //=======================================================================
298 void AIS_Plane::ComputeSelection (const Handle(SelectMgr_Selection)& theSelection, const Standard_Integer /*theMode*/)
300 theSelection->Clear();
301 Handle(SelectMgr_EntityOwner) aSensitiveOwner = new SelectMgr_EntityOwner (this, 10);
302 Handle(Poly_Triangulation) aSensitivePoly;
306 // plane representing rectangle
307 Standard_Real aLengthX = myDrawer->PlaneAspect()->PlaneXLength() / 2.0;
308 Standard_Real aLengthY = myDrawer->PlaneAspect()->PlaneYLength() / 2.0;
309 Handle(Geom_Plane) aPlane =
310 Handle(Geom_Plane)::DownCast (myComponent->Translated (myComponent->Location(), myCenter));
312 TColgp_Array1OfPnt aRectanglePoints (1, 4);
313 aPlane->D0 ( aLengthX, aLengthY, aRectanglePoints.ChangeValue (1));
314 aPlane->D0 ( aLengthX, -aLengthY, aRectanglePoints.ChangeValue (2));
315 aPlane->D0 (-aLengthX, -aLengthY, aRectanglePoints.ChangeValue (3));
316 aPlane->D0 (-aLengthX, aLengthY, aRectanglePoints.ChangeValue (4));
318 Poly_Array1OfTriangle aTriangles (1, 2);
319 aTriangles.ChangeValue (1) = Poly_Triangle (1, 2, 3);
320 aTriangles.ChangeValue (2) = Poly_Triangle (1, 3, 4);
322 aSensitivePoly = new Poly_Triangulation (aRectanglePoints, aTriangles);
326 // plane representing triangle
327 TColgp_Array1OfPnt aTrianglePoints (1, 3);
328 aTrianglePoints.ChangeValue (1) = myCenter;
329 aTrianglePoints.ChangeValue (2) = myPmin;
330 aTrianglePoints.ChangeValue (3) = myPmax;
332 Poly_Array1OfTriangle aTriangles (1, 1);
333 aTriangles.ChangeValue (1) = Poly_Triangle(1, 2, 3);
335 aSensitivePoly = new Poly_Triangulation (aTrianglePoints, aTriangles);
338 Standard_Boolean isSensitiveInterior = myTypeOfSensitivity == Select3D_TOS_INTERIOR;
340 Handle(Select3D_SensitiveTriangulation) aSensitive =
341 new Select3D_SensitiveTriangulation (aSensitiveOwner,
344 isSensitiveInterior);
346 theSelection->Add(aSensitive);
349 //=======================================================================
352 //=======================================================================
353 void AIS_Plane::SetSize(const Standard_Real aLength)
355 SetSize(aLength,aLength);
358 void AIS_Plane::SetSize(const Standard_Real aXLength,
359 const Standard_Real aYLength)
361 //if the plane already has a proper color or size,
362 //there is already a specific PlaneAspect and DatumAspect
364 Handle(Prs3d_PlaneAspect) PA;
365 Handle(Prs3d_DatumAspect) DA;
367 PA = myDrawer->PlaneAspect();
368 DA = myDrawer->DatumAspect();
370 Standard_Boolean yenavaitPA(Standard_True),yenavaitDA(Standard_True);
371 if(myDrawer->HasLink() && myDrawer->Link()->PlaneAspect() == PA){
372 yenavaitPA = Standard_False;
373 PA = new Prs3d_PlaneAspect();}
374 if(myDrawer->HasLink() && myDrawer->Link()->DatumAspect() == DA){
375 yenavaitDA = Standard_False;
376 DA = new Prs3d_DatumAspect();
379 PA->SetPlaneLength(aXLength,aYLength);
380 DA->SetAxisLength(aXLength,aYLength,aXLength);
383 myDrawer->SetPlaneAspect(PA);
385 myDrawer->SetDatumAspect(DA);
388 myHasOwnSize = Standard_True;
390 UpdatePresentations();
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;
422 UpdatePresentations();
427 //=======================================================================
430 //=======================================================================
432 Standard_Boolean AIS_Plane::Size(Standard_Real& X,Standard_Real& Y) const
434 X = myDrawer->PlaneAspect()->PlaneXLength();
435 Y = myDrawer->PlaneAspect()->PlaneYLength();
436 return Abs(X-Y)<=Precision::Confusion();
439 //=======================================================================
440 //function : SetColor
442 //=======================================================================
443 void AIS_Plane::SetColor(const Quantity_Color &aCol)
445 // if the plane already has its proper size, there is an already created planeaspect
446 // Standard_Boolean yenadeja = hasOwnColor || myHasOwnSize;
447 Handle(Prs3d_PlaneAspect) PA;
448 Handle(Prs3d_DatumAspect) DA;
450 PA = myDrawer->PlaneAspect();
451 DA = myDrawer->DatumAspect();
453 Standard_Boolean yenavaitPA(Standard_True),yenavaitDA(Standard_True);
454 if(myDrawer->HasLink() && myDrawer->Link()->PlaneAspect() == PA){
455 yenavaitPA = Standard_False;
456 PA = new Prs3d_PlaneAspect();}
457 if(myDrawer->HasLink() && myDrawer->Link()->DatumAspect() == DA){
458 yenavaitDA = Standard_False;
459 DA = new Prs3d_DatumAspect();
462 PA->EdgesAspect()->SetColor(aCol);
463 DA->LineAspect(Prs3d_DP_XAxis)->SetColor(aCol);
464 DA->LineAspect(Prs3d_DP_YAxis)->SetColor(aCol);
465 DA->LineAspect(Prs3d_DP_ZAxis)->SetColor(aCol);
468 myDrawer->SetPlaneAspect(PA);
470 myDrawer->SetDatumAspect(DA);
472 myDrawer->ShadingAspect()->SetColor(aCol);
474 hasOwnColor=Standard_True;
475 myDrawer->SetColor (aCol);
477 //=======================================================================
478 //function : SetColor
480 //=======================================================================
481 void AIS_Plane::UnsetColor()
483 if(!hasOwnColor) return;
486 myDrawer->SetPlaneAspect (Handle(Prs3d_PlaneAspect)());
487 myDrawer->SetDatumAspect (Handle(Prs3d_DatumAspect)());
490 const Handle(Prs3d_PlaneAspect) PA = myDrawer->HasLink() ? myDrawer->Link()->PlaneAspect() :
491 new Prs3d_PlaneAspect();
492 Quantity_Color Col = PA->EdgesAspect()->Aspect()->Color();
493 myDrawer->PlaneAspect()->EdgesAspect()->SetColor(Col);
495 myDrawer->DatumAspect()->LineAspect(Prs3d_DP_XAxis)->SetColor(Col);
496 myDrawer->DatumAspect()->LineAspect(Prs3d_DP_YAxis)->SetColor(Col);
497 myDrawer->DatumAspect()->LineAspect(Prs3d_DP_ZAxis)->SetColor(Col);
501 hasOwnColor=Standard_False;
505 //=======================================================================
506 //function : ComputeFrame
508 //=======================================================================
509 void AIS_Plane::ComputeFrame()
512 const Handle(Geom_Plane)& pl = myComponent;
515 if (myAutomaticPosition) {
516 ElSLib::Parameters(pl->Pln(),myCenter,U,V);
517 pl->D0 (U, V, myCenter);
520 Handle(Geom_Plane) thegoodpl (Handle(Geom_Plane)::DownCast(pl->Translated(pl->Location(),myCenter)));
521 ElSLib::Parameters(thegoodpl->Pln(),myPmin,U,V);
525 if (U < 10*Precision::Confusion()) U=0.1;
526 if (V < 10*Precision::Confusion()) V=0.1;
528 myDrawer->PlaneAspect()->SetPlaneLength(U,V);
532 //=======================================================================
534 //purpose : to avoid warning
535 //=======================================================================
536 void AIS_Plane::Compute(const Handle(Prs3d_Projector)&,
537 const Handle(Prs3d_Presentation)&)
541 //=======================================================================
542 //function : ComputeFields
544 //=======================================================================
545 void AIS_Plane::ComputeFields()
548 Handle (Prs3d_DatumAspect) DA = myDrawer->DatumAspect();
550 gp_Pnt Orig = myAx2->Ax2().Location();
551 gp_Dir oX = myAx2->Ax2().XDirection();
552 gp_Dir oY = myAx2->Ax2().YDirection();
553 gp_Dir oZ = myAx2->Ax2().Direction();
555 Standard_Real xo,yo,zo,x1,y1,z1,x2,y2,z2,x3,y3,z3,x4=0,y4=0,z4=0;
556 Standard_Real x5=0,y5=0,z5=0;
557 Orig.Coord(xo,yo,zo);
561 Standard_Real DS1 = DA->AxisLength(Prs3d_DP_XAxis);
562 Standard_Real DS2 = DA->AxisLength(Prs3d_DP_YAxis);
563 Standard_Real DS3 = DA->AxisLength(Prs3d_DP_ZAxis);
566 switch (myTypeOfPlane) {
567 case AIS_TOPL_XYPlane:
570 myComponent = new Geom_Plane(XYP);
571 x4=xo+x1*DS1; y4=yo+y1*DS1; z4=zo+z1*DS1;
572 x5=xo+x2*DS2; y5=yo+y2*DS2; z5=zo+z2*DS2;
575 case AIS_TOPL_XZPlane:
578 myComponent = new Geom_Plane(XZP);
579 x4=xo+x1*DS1; y4=yo+y1*DS1; z4=zo+z1*DS1;
580 x5=xo+x3*DS3; y5=yo+y3*DS3; z5=zo+z3*DS3;
583 case AIS_TOPL_YZPlane:
586 myComponent = new Geom_Plane(XZP);
587 x4=xo+x2*DS2; y4=yo+y2*DS2; z4=zo+z2*DS2;
588 x5=xo+x3*DS3; y5=yo+y3*DS3; z5=zo+z3*DS3;
594 myPmin.SetCoord(x4,y4,z4);
595 myPmax.SetCoord(x5,y5,z5);
599 //=======================================================================
600 //function : InitDrawerAttributes
602 //=======================================================================
604 void AIS_Plane::InitDrawerAttributes()
606 Handle(Prs3d_ShadingAspect) shasp = new Prs3d_ShadingAspect();
607 shasp->SetMaterial(Graphic3d_NOM_PLASTIC);
608 shasp->SetColor(Quantity_NOC_GRAY40);
609 myDrawer->SetShadingAspect(shasp);
610 Handle(Graphic3d_AspectFillArea3d) asf = shasp->Aspect();
611 Graphic3d_MaterialAspect asp = asf->FrontMaterial();
612 asp.SetTransparency (0.8f);
613 asf->SetFrontMaterial(asp);
614 asf->SetBackMaterial(asp);
617 //=======================================================================
618 //function : AcceptDisplayMode
620 //=======================================================================
622 Standard_Boolean AIS_Plane::
623 AcceptDisplayMode(const Standard_Integer aMode) const
627 //=======================================================================
628 //function : SetContext
630 //=======================================================================
632 void AIS_Plane::SetContext(const Handle(AIS_InteractiveContext)& Ctx)
634 AIS_InteractiveObject::SetContext(Ctx);