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 <GeomAdaptor_Surface.hxx>
28 #include <Graphic3d_ArrayOfQuadrangles.hxx>
29 #include <Graphic3d_AspectFillArea3d.hxx>
30 #include <Graphic3d_AspectLine3d.hxx>
31 #include <Graphic3d_Group.hxx>
32 #include <Graphic3d_MaterialAspect.hxx>
33 #include <Graphic3d_Structure.hxx>
34 #include <Poly_Triangulation.hxx>
35 #include <Precision.hxx>
36 #include <Prs3d_DatumAspect.hxx>
37 #include <Prs3d_Drawer.hxx>
38 #include <Prs3d_LineAspect.hxx>
39 #include <Prs3d_PlaneAspect.hxx>
40 #include <Prs3d_Presentation.hxx>
41 #include <Prs3d_ShadingAspect.hxx>
42 #include <Quantity_Color.hxx>
43 #include <Select3D_SensitiveTriangulation.hxx>
44 #include <SelectMgr_EntityOwner.hxx>
45 #include <Standard_Type.hxx>
46 #include <StdPrs_Plane.hxx>
47 #include <StdPrs_ShadedShape.hxx>
48 #include <StdPrs_ShadedSurface.hxx>
49 #include <TColgp_Array1OfPnt.hxx>
50 #include <TColgp_HArray1OfPnt.hxx>
51 #include <TCollection_AsciiString.hxx>
52 #include <UnitsAPI.hxx>
54 IMPLEMENT_STANDARD_RTTIEXT(AIS_Plane,AIS_InteractiveObject)
56 //=======================================================================
57 //function : AIS_Plane
59 //=======================================================================
60 AIS_Plane::AIS_Plane(const Handle(Geom_Plane)& aComponent,
61 const Standard_Boolean aCurrentMode):
62 myComponent(aComponent),
63 myCenter(gp_Pnt(0.,0.,0.)),
64 myCurrentMode(aCurrentMode),
65 myAutomaticPosition(Standard_True),
66 myTypeOfPlane(AIS_TOPL_Unknown),
67 myIsXYZPlane(Standard_False),
68 myTypeOfSensitivity (Select3D_TOS_BOUNDARY)
70 InitDrawerAttributes();
73 //=======================================================================
74 //function : AIS_Plane
75 //purpose : avec une position
76 //=======================================================================
77 AIS_Plane::AIS_Plane(const Handle(Geom_Plane)& aComponent,
78 const gp_Pnt& aCenter,
79 const Standard_Boolean aCurrentMode):
80 myComponent(aComponent),
82 myCurrentMode(aCurrentMode),
83 myAutomaticPosition(Standard_True),
84 myTypeOfPlane(AIS_TOPL_Unknown),
85 myIsXYZPlane(Standard_False),
86 myTypeOfSensitivity (Select3D_TOS_BOUNDARY)
88 InitDrawerAttributes();
91 //=======================================================================
92 //function : AIS_Plane
94 //=======================================================================
95 AIS_Plane::AIS_Plane(const Handle(Geom_Plane)& aComponent,
96 const gp_Pnt& aCenter,
99 const Standard_Boolean aCurrentMode):
100 myComponent(aComponent),
104 myCurrentMode(aCurrentMode),
105 myAutomaticPosition(Standard_False),
106 myTypeOfPlane(AIS_TOPL_Unknown),
107 myIsXYZPlane(Standard_False),
108 myTypeOfSensitivity (Select3D_TOS_BOUNDARY)
110 InitDrawerAttributes();
113 //=======================================================================
114 //function : AIS_Plane
115 //purpose : XYPlane, XZPlane, YZPlane
116 //=======================================================================
117 AIS_Plane::AIS_Plane(const Handle(Geom_Axis2Placement)& aComponent,
118 const AIS_TypeOfPlane aPlaneType,
119 const Standard_Boolean aCurrentMode):
121 myCurrentMode(aCurrentMode),
122 myAutomaticPosition(Standard_True),
123 myTypeOfPlane(aPlaneType),
124 myIsXYZPlane(Standard_True),
125 myTypeOfSensitivity (Select3D_TOS_BOUNDARY)
127 InitDrawerAttributes();
132 //=======================================================================
133 //function : SetComponent
135 //=======================================================================
137 void AIS_Plane::SetComponent(const Handle(Geom_Plane)& aComponent)
139 myComponent = aComponent;
140 myTypeOfPlane = AIS_TOPL_Unknown;
141 myIsXYZPlane = Standard_False;
142 //myCenter = gp_Pnt(0.,0.,0.);
143 myAutomaticPosition = Standard_True;
146 //=======================================================================
149 //=======================================================================
151 AIS_KindOfInteractive AIS_Plane::Type() const
152 {return AIS_KOI_Datum;}
155 //=======================================================================
156 //function : Signature
158 //=======================================================================
160 Standard_Integer AIS_Plane::Signature() const
164 //=======================================================================
165 //function : Axis2Placement
167 //=======================================================================
169 Handle(Geom_Axis2Placement) AIS_Plane::Axis2Placement()
171 Handle(Geom_Axis2Placement) Bid;
172 return IsXYZPlane() ? myAx2:Bid;
174 //=======================================================================
175 //function : SetAxis2Placement
177 //=======================================================================
179 void AIS_Plane::SetAxis2Placement(const Handle(Geom_Axis2Placement)& aComponent,
180 const AIS_TypeOfPlane aPlaneType)
182 myTypeOfPlane = aPlaneType;
183 myIsXYZPlane = Standard_True;
185 myAutomaticPosition = Standard_True;
189 //=======================================================================
190 //function : PlaneAttributes
192 //=======================================================================
193 Standard_Boolean AIS_Plane::PlaneAttributes(Handle(Geom_Plane)& aComponent,
198 Standard_Boolean aStatus (Standard_False);
199 if (!myAutomaticPosition){
200 aComponent = myComponent;
204 aStatus = Standard_True;
209 //=======================================================================
210 //function : SetPlaneAttributes
212 //=======================================================================
213 void AIS_Plane::SetPlaneAttributes(const Handle(Geom_Plane)& aComponent,
214 const gp_Pnt& aCenter,
218 myAutomaticPosition = Standard_False;
219 myComponent = aComponent;
223 myTypeOfPlane = AIS_TOPL_Unknown;
224 myIsXYZPlane = Standard_False;
227 //=======================================================================
230 //=======================================================================
231 void AIS_Plane::Compute(const Handle(PrsMgr_PresentationManager3d)& ,
232 const Handle(Prs3d_Presentation)& aPresentation,
233 const Standard_Integer aMode)
236 aPresentation->SetInfiniteState(myInfiniteState);
237 myDrawer->PlaneAspect()->EdgesAspect()->SetWidth(myCurrentMode == 0? 1 : 3);
246 const Handle(Geom_Plane)& pl = myComponent;
247 Handle(Geom_Plane) thegoodpl (Handle(Geom_Plane)::DownCast(pl->Translated(pl->Location(),myCenter)));
248 GeomAdaptor_Surface surf(thegoodpl);
249 StdPrs_Plane::Add(aPresentation,surf,myDrawer);
252 DsgPrs_XYZPlanePresentation::Add(aPresentation,myDrawer,myCenter,myPmin,myPmax);
260 Handle(Prs3d_PlaneAspect) theaspect = myDrawer->PlaneAspect();
261 Handle(Graphic3d_Group) TheGroup = aPresentation->CurrentGroup();
262 TheGroup->SetPrimitivesAspect(myDrawer->ShadingAspect()->Aspect());
264 const Standard_Real Xmax = 0.5*Standard_Real(theaspect->PlaneXLength());
265 const Standard_Real Ymax = 0.5*Standard_Real(theaspect->PlaneYLength());
267 Handle(Graphic3d_ArrayOfQuadrangles) aQuads = new Graphic3d_ArrayOfQuadrangles(4);
269 myComponent->D0(-Xmax,Ymax,p1);
270 aQuads->AddVertex(p1);
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);
278 TheGroup->AddPrimitiveArray(aQuads);
281 DsgPrs_ShadedPlanePresentation::Add(aPresentation,myDrawer,myCenter,myPmin,myPmax);
287 //=======================================================================
288 //function : ComputeSelection
290 //=======================================================================
291 void AIS_Plane::ComputeSelection (const Handle(SelectMgr_Selection)& theSelection, const Standard_Integer /*theMode*/)
293 theSelection->Clear();
294 Handle(SelectMgr_EntityOwner) aSensitiveOwner = new SelectMgr_EntityOwner (this, 10);
295 Handle(Poly_Triangulation) aSensitivePoly;
299 // plane representing rectangle
300 Standard_Real aLengthX = myDrawer->PlaneAspect()->PlaneXLength() / 2.0;
301 Standard_Real aLengthY = myDrawer->PlaneAspect()->PlaneYLength() / 2.0;
302 Handle(Geom_Plane) aPlane =
303 Handle(Geom_Plane)::DownCast (myComponent->Translated (myComponent->Location(), myCenter));
305 TColgp_Array1OfPnt aRectanglePoints (1, 4);
306 aPlane->D0 ( aLengthX, aLengthY, aRectanglePoints.ChangeValue (1));
307 aPlane->D0 ( aLengthX, -aLengthY, aRectanglePoints.ChangeValue (2));
308 aPlane->D0 (-aLengthX, -aLengthY, aRectanglePoints.ChangeValue (3));
309 aPlane->D0 (-aLengthX, aLengthY, aRectanglePoints.ChangeValue (4));
311 Poly_Array1OfTriangle aTriangles (1, 2);
312 aTriangles.ChangeValue (1) = Poly_Triangle (1, 2, 3);
313 aTriangles.ChangeValue (2) = Poly_Triangle (1, 3, 4);
315 aSensitivePoly = new Poly_Triangulation (aRectanglePoints, aTriangles);
319 // plane representing triangle
320 TColgp_Array1OfPnt aTrianglePoints (1, 3);
321 aTrianglePoints.ChangeValue (1) = myCenter;
322 aTrianglePoints.ChangeValue (2) = myPmin;
323 aTrianglePoints.ChangeValue (3) = myPmax;
325 Poly_Array1OfTriangle aTriangles (1, 1);
326 aTriangles.ChangeValue (1) = Poly_Triangle(1, 2, 3);
328 aSensitivePoly = new Poly_Triangulation (aTrianglePoints, aTriangles);
331 Standard_Boolean isSensitiveInterior = myTypeOfSensitivity == Select3D_TOS_INTERIOR;
333 Handle(Select3D_SensitiveTriangulation) aSensitive =
334 new Select3D_SensitiveTriangulation (aSensitiveOwner,
337 isSensitiveInterior);
339 theSelection->Add(aSensitive);
342 //=======================================================================
345 //=======================================================================
346 void AIS_Plane::SetSize(const Standard_Real aLength)
348 SetSize(aLength,aLength);
351 void AIS_Plane::SetSize(const Standard_Real aXLength,
352 const Standard_Real aYLength)
354 //if the plane already has a proper color or size,
355 //there is already a specific PlaneAspect and DatumAspect
357 Handle(Prs3d_PlaneAspect) PA;
358 Handle(Prs3d_DatumAspect) DA;
360 PA = myDrawer->PlaneAspect();
361 DA = myDrawer->DatumAspect();
363 Standard_Boolean yenavaitPA(Standard_True),yenavaitDA(Standard_True);
364 if(myDrawer->HasLink() && myDrawer->Link()->PlaneAspect() == PA){
365 yenavaitPA = Standard_False;
366 PA = new Prs3d_PlaneAspect();}
367 if(myDrawer->HasLink() && myDrawer->Link()->DatumAspect() == DA){
368 yenavaitDA = Standard_False;
369 DA = new Prs3d_DatumAspect();
372 PA->SetPlaneLength(aXLength,aYLength);
373 DA->SetAxisLength(aXLength,aYLength,aXLength);
376 myDrawer->SetPlaneAspect(PA);
378 myDrawer->SetDatumAspect(DA);
381 myHasOwnSize = Standard_True;
383 UpdatePresentations();
388 //=======================================================================
389 //function : UnsetSize
390 //purpose : If there is a color, the size is restaured from the drawer of the context...
391 //=======================================================================
392 void AIS_Plane::UnsetSize()
395 if(!myHasOwnSize) return;
398 myDrawer->SetPlaneAspect (Handle(Prs3d_PlaneAspect)());
399 myDrawer->SetDatumAspect (Handle(Prs3d_DatumAspect)());
402 const Handle(Prs3d_PlaneAspect) PA = myDrawer->HasLink() ? myDrawer->Link()->PlaneAspect() :
403 new Prs3d_PlaneAspect();
404 const Handle(Prs3d_DatumAspect) DA = myDrawer->HasLink() ? myDrawer->Link()->DatumAspect() :
405 new Prs3d_DatumAspect();
407 myDrawer->PlaneAspect()->SetPlaneLength(PA->PlaneXLength(),PA->PlaneYLength());
408 myDrawer->DatumAspect()->SetAxisLength(DA->AxisLength(Prs3d_DP_XAxis),
409 DA->AxisLength(Prs3d_DP_YAxis),
410 DA->AxisLength(Prs3d_DP_ZAxis));
413 myHasOwnSize = Standard_False;
415 UpdatePresentations();
420 //=======================================================================
423 //=======================================================================
425 Standard_Boolean AIS_Plane::Size(Standard_Real& X,Standard_Real& Y) const
427 X = myDrawer->PlaneAspect()->PlaneXLength();
428 Y = myDrawer->PlaneAspect()->PlaneYLength();
429 return Abs(X-Y)<=Precision::Confusion();
432 //=======================================================================
433 //function : SetColor
435 //=======================================================================
436 void AIS_Plane::SetColor(const Quantity_Color &aCol)
438 // if the plane already has its proper size, there is an already created planeaspect
439 // Standard_Boolean yenadeja = hasOwnColor || myHasOwnSize;
440 Handle(Prs3d_PlaneAspect) PA;
441 Handle(Prs3d_DatumAspect) DA;
443 PA = myDrawer->PlaneAspect();
444 DA = myDrawer->DatumAspect();
446 Standard_Boolean yenavaitPA(Standard_True),yenavaitDA(Standard_True);
447 if(myDrawer->HasLink() && myDrawer->Link()->PlaneAspect() == PA){
448 yenavaitPA = Standard_False;
449 PA = new Prs3d_PlaneAspect();}
450 if(myDrawer->HasLink() && myDrawer->Link()->DatumAspect() == DA){
451 yenavaitDA = Standard_False;
452 DA = new Prs3d_DatumAspect();
455 PA->EdgesAspect()->SetColor(aCol);
456 DA->LineAspect(Prs3d_DP_XAxis)->SetColor(aCol);
457 DA->LineAspect(Prs3d_DP_YAxis)->SetColor(aCol);
458 DA->LineAspect(Prs3d_DP_ZAxis)->SetColor(aCol);
461 myDrawer->SetPlaneAspect(PA);
463 myDrawer->SetDatumAspect(DA);
465 myDrawer->ShadingAspect()->SetColor(aCol);
467 hasOwnColor=Standard_True;
468 myDrawer->SetColor (aCol);
470 //=======================================================================
471 //function : SetColor
473 //=======================================================================
474 void AIS_Plane::UnsetColor()
476 if(!hasOwnColor) return;
479 myDrawer->SetPlaneAspect (Handle(Prs3d_PlaneAspect)());
480 myDrawer->SetDatumAspect (Handle(Prs3d_DatumAspect)());
483 const Handle(Prs3d_PlaneAspect) PA = myDrawer->HasLink() ? myDrawer->Link()->PlaneAspect() :
484 new Prs3d_PlaneAspect();
485 Quantity_Color Col = PA->EdgesAspect()->Aspect()->Color();
486 myDrawer->PlaneAspect()->EdgesAspect()->SetColor(Col);
488 myDrawer->DatumAspect()->LineAspect(Prs3d_DP_XAxis)->SetColor(Col);
489 myDrawer->DatumAspect()->LineAspect(Prs3d_DP_YAxis)->SetColor(Col);
490 myDrawer->DatumAspect()->LineAspect(Prs3d_DP_ZAxis)->SetColor(Col);
494 hasOwnColor=Standard_False;
498 //=======================================================================
499 //function : ComputeFrame
501 //=======================================================================
502 void AIS_Plane::ComputeFrame()
505 const Handle(Geom_Plane)& pl = myComponent;
508 if (myAutomaticPosition) {
509 ElSLib::Parameters(pl->Pln(),myCenter,U,V);
510 pl->D0 (U, V, myCenter);
513 Handle(Geom_Plane) thegoodpl (Handle(Geom_Plane)::DownCast(pl->Translated(pl->Location(),myCenter)));
514 ElSLib::Parameters(thegoodpl->Pln(),myPmin,U,V);
518 if (U < 10*Precision::Confusion()) U=0.1;
519 if (V < 10*Precision::Confusion()) V=0.1;
521 myDrawer->PlaneAspect()->SetPlaneLength(U,V);
525 //=======================================================================
526 //function : ComputeFields
528 //=======================================================================
529 void AIS_Plane::ComputeFields()
532 Handle (Prs3d_DatumAspect) DA = myDrawer->DatumAspect();
534 gp_Pnt Orig = myAx2->Ax2().Location();
535 gp_Dir oX = myAx2->Ax2().XDirection();
536 gp_Dir oY = myAx2->Ax2().YDirection();
537 gp_Dir oZ = myAx2->Ax2().Direction();
539 Standard_Real xo,yo,zo,x1,y1,z1,x2,y2,z2,x3,y3,z3,x4=0,y4=0,z4=0;
540 Standard_Real x5=0,y5=0,z5=0;
541 Orig.Coord(xo,yo,zo);
545 Standard_Real DS1 = DA->AxisLength(Prs3d_DP_XAxis);
546 Standard_Real DS2 = DA->AxisLength(Prs3d_DP_YAxis);
547 Standard_Real DS3 = DA->AxisLength(Prs3d_DP_ZAxis);
550 switch (myTypeOfPlane) {
551 case AIS_TOPL_XYPlane:
554 myComponent = new Geom_Plane(XYP);
555 x4=xo+x1*DS1; y4=yo+y1*DS1; z4=zo+z1*DS1;
556 x5=xo+x2*DS2; y5=yo+y2*DS2; z5=zo+z2*DS2;
559 case AIS_TOPL_XZPlane:
562 myComponent = new Geom_Plane(XZP);
563 x4=xo+x1*DS1; y4=yo+y1*DS1; z4=zo+z1*DS1;
564 x5=xo+x3*DS3; y5=yo+y3*DS3; z5=zo+z3*DS3;
567 case AIS_TOPL_YZPlane:
570 myComponent = new Geom_Plane(XZP);
571 x4=xo+x2*DS2; y4=yo+y2*DS2; z4=zo+z2*DS2;
572 x5=xo+x3*DS3; y5=yo+y3*DS3; z5=zo+z3*DS3;
578 myPmin.SetCoord(x4,y4,z4);
579 myPmax.SetCoord(x5,y5,z5);
583 //=======================================================================
584 //function : InitDrawerAttributes
586 //=======================================================================
588 void AIS_Plane::InitDrawerAttributes()
590 Handle(Prs3d_ShadingAspect) shasp = new Prs3d_ShadingAspect();
591 shasp->SetMaterial(Graphic3d_NOM_PLASTIC);
592 shasp->SetColor(Quantity_NOC_GRAY40);
593 myDrawer->SetShadingAspect(shasp);
594 Handle(Graphic3d_AspectFillArea3d) asf = shasp->Aspect();
595 Graphic3d_MaterialAspect asp = asf->FrontMaterial();
596 asp.SetTransparency (0.8f);
597 asf->SetFrontMaterial(asp);
598 asf->SetBackMaterial(asp);
601 //=======================================================================
602 //function : AcceptDisplayMode
604 //=======================================================================
606 Standard_Boolean AIS_Plane::
607 AcceptDisplayMode(const Standard_Integer aMode) const
611 //=======================================================================
612 //function : SetContext
614 //=======================================================================
616 void AIS_Plane::SetContext(const Handle(AIS_InteractiveContext)& Ctx)
618 AIS_InteractiveObject::SetContext(Ctx);