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 //=======================================================================
147 //function : Axis2Placement
149 //=======================================================================
151 Handle(Geom_Axis2Placement) AIS_Plane::Axis2Placement()
153 Handle(Geom_Axis2Placement) Bid;
154 return IsXYZPlane() ? myAx2:Bid;
156 //=======================================================================
157 //function : SetAxis2Placement
159 //=======================================================================
161 void AIS_Plane::SetAxis2Placement(const Handle(Geom_Axis2Placement)& aComponent,
162 const AIS_TypeOfPlane aPlaneType)
164 myTypeOfPlane = aPlaneType;
165 myIsXYZPlane = Standard_True;
167 myAutomaticPosition = Standard_True;
171 //=======================================================================
172 //function : PlaneAttributes
174 //=======================================================================
175 Standard_Boolean AIS_Plane::PlaneAttributes(Handle(Geom_Plane)& aComponent,
180 Standard_Boolean aStatus (Standard_False);
181 if (!myAutomaticPosition){
182 aComponent = myComponent;
186 aStatus = Standard_True;
191 //=======================================================================
192 //function : SetPlaneAttributes
194 //=======================================================================
195 void AIS_Plane::SetPlaneAttributes(const Handle(Geom_Plane)& aComponent,
196 const gp_Pnt& aCenter,
200 myAutomaticPosition = Standard_False;
201 myComponent = aComponent;
205 myTypeOfPlane = AIS_TOPL_Unknown;
206 myIsXYZPlane = Standard_False;
209 //=======================================================================
212 //=======================================================================
213 void AIS_Plane::Compute (const Handle(PrsMgr_PresentationManager)& ,
214 const Handle(Prs3d_Presentation)& thePrs,
215 const Standard_Integer theMode)
218 thePrs->SetInfiniteState (myInfiniteState);
219 myDrawer->PlaneAspect()->EdgesAspect()->SetWidth(myCurrentMode == 0? 1 : 3);
228 const Handle(Geom_Plane)& pl = myComponent;
229 Handle(Geom_Plane) thegoodpl (Handle(Geom_Plane)::DownCast(pl->Translated(pl->Location(),myCenter)));
230 GeomAdaptor_Surface surf(thegoodpl);
231 StdPrs_Plane::Add (thePrs, surf, myDrawer);
235 DsgPrs_XYZPlanePresentation::Add (thePrs, myDrawer, myCenter, myPmin, myPmax);
244 Handle(Prs3d_PlaneAspect) anAspect = myDrawer->PlaneAspect();
245 Handle(Graphic3d_Group) aGroup = thePrs->CurrentGroup();
246 aGroup->SetPrimitivesAspect (myDrawer->ShadingAspect()->Aspect());
248 const Standard_Real Xmax = 0.5*Standard_Real(anAspect->PlaneXLength());
249 const Standard_Real Ymax = 0.5*Standard_Real(anAspect->PlaneYLength());
251 Handle(Graphic3d_ArrayOfQuadrangles) aQuads = new Graphic3d_ArrayOfQuadrangles(4);
253 myComponent->D0(-Xmax,Ymax,p1);
254 aQuads->AddVertex(p1);
255 myComponent->D0(Xmax,Ymax,p1);
256 aQuads->AddVertex(p1);
257 myComponent->D0(Xmax,-Ymax,p1);
258 aQuads->AddVertex(p1);
259 myComponent->D0(-Xmax,-Ymax,p1);
260 aQuads->AddVertex(p1);
262 aGroup->AddPrimitiveArray (aQuads);
266 DsgPrs_ShadedPlanePresentation::Add (thePrs, myDrawer, myCenter, myPmin, myPmax);
273 //=======================================================================
274 //function : ComputeSelection
276 //=======================================================================
277 void AIS_Plane::ComputeSelection (const Handle(SelectMgr_Selection)& theSelection, const Standard_Integer /*theMode*/)
279 theSelection->Clear();
280 Handle(SelectMgr_EntityOwner) aSensitiveOwner = new SelectMgr_EntityOwner (this, 10);
281 Handle(Poly_Triangulation) aSensitivePoly;
285 // plane representing rectangle
286 Standard_Real aLengthX = myDrawer->PlaneAspect()->PlaneXLength() / 2.0;
287 Standard_Real aLengthY = myDrawer->PlaneAspect()->PlaneYLength() / 2.0;
288 Handle(Geom_Plane) aPlane =
289 Handle(Geom_Plane)::DownCast (myComponent->Translated (myComponent->Location(), myCenter));
291 TColgp_Array1OfPnt aRectanglePoints (1, 4);
292 aPlane->D0 ( aLengthX, aLengthY, aRectanglePoints.ChangeValue (1));
293 aPlane->D0 ( aLengthX, -aLengthY, aRectanglePoints.ChangeValue (2));
294 aPlane->D0 (-aLengthX, -aLengthY, aRectanglePoints.ChangeValue (3));
295 aPlane->D0 (-aLengthX, aLengthY, aRectanglePoints.ChangeValue (4));
297 Poly_Array1OfTriangle aTriangles (1, 2);
298 aTriangles.ChangeValue (1) = Poly_Triangle (1, 2, 3);
299 aTriangles.ChangeValue (2) = Poly_Triangle (1, 3, 4);
301 aSensitivePoly = new Poly_Triangulation (aRectanglePoints, aTriangles);
305 // plane representing triangle
306 TColgp_Array1OfPnt aTrianglePoints (1, 3);
307 aTrianglePoints.ChangeValue (1) = myCenter;
308 aTrianglePoints.ChangeValue (2) = myPmin;
309 aTrianglePoints.ChangeValue (3) = myPmax;
311 Poly_Array1OfTriangle aTriangles (1, 1);
312 aTriangles.ChangeValue (1) = Poly_Triangle(1, 2, 3);
314 aSensitivePoly = new Poly_Triangulation (aTrianglePoints, aTriangles);
317 Standard_Boolean isSensitiveInterior = myTypeOfSensitivity == Select3D_TOS_INTERIOR;
319 Handle(Select3D_SensitiveTriangulation) aSensitive =
320 new Select3D_SensitiveTriangulation (aSensitiveOwner,
323 isSensitiveInterior);
325 theSelection->Add(aSensitive);
328 //=======================================================================
331 //=======================================================================
332 void AIS_Plane::SetSize(const Standard_Real aLength)
334 SetSize(aLength,aLength);
337 void AIS_Plane::SetSize(const Standard_Real aXLength,
338 const Standard_Real aYLength)
340 //if the plane already has a proper color or size,
341 //there is already a specific PlaneAspect and DatumAspect
343 Handle(Prs3d_PlaneAspect) PA;
344 Handle(Prs3d_DatumAspect) DA;
346 PA = myDrawer->PlaneAspect();
347 DA = myDrawer->DatumAspect();
349 Standard_Boolean yenavaitPA(Standard_True),yenavaitDA(Standard_True);
350 if(myDrawer->HasLink() && myDrawer->Link()->PlaneAspect() == PA){
351 yenavaitPA = Standard_False;
352 PA = new Prs3d_PlaneAspect();}
353 if(myDrawer->HasLink() && myDrawer->Link()->DatumAspect() == DA){
354 yenavaitDA = Standard_False;
355 DA = new Prs3d_DatumAspect();
358 PA->SetPlaneLength(aXLength,aYLength);
359 DA->SetAxisLength(aXLength,aYLength,aXLength);
362 myDrawer->SetPlaneAspect(PA);
364 myDrawer->SetDatumAspect(DA);
367 myHasOwnSize = Standard_True;
369 UpdatePresentations();
374 //=======================================================================
375 //function : UnsetSize
376 //purpose : If there is a color, the size is restaured from the drawer of the context...
377 //=======================================================================
378 void AIS_Plane::UnsetSize()
381 if(!myHasOwnSize) return;
384 myDrawer->SetPlaneAspect (Handle(Prs3d_PlaneAspect)());
385 myDrawer->SetDatumAspect (Handle(Prs3d_DatumAspect)());
388 const Handle(Prs3d_PlaneAspect) PA = myDrawer->HasLink() ? myDrawer->Link()->PlaneAspect() :
389 new Prs3d_PlaneAspect();
390 const Handle(Prs3d_DatumAspect) DA = myDrawer->HasLink() ? myDrawer->Link()->DatumAspect() :
391 new Prs3d_DatumAspect();
393 myDrawer->PlaneAspect()->SetPlaneLength(PA->PlaneXLength(),PA->PlaneYLength());
394 myDrawer->DatumAspect()->SetAxisLength(DA->AxisLength(Prs3d_DatumParts_XAxis),
395 DA->AxisLength(Prs3d_DatumParts_YAxis),
396 DA->AxisLength(Prs3d_DatumParts_ZAxis));
399 myHasOwnSize = Standard_False;
401 UpdatePresentations();
406 //=======================================================================
409 //=======================================================================
411 Standard_Boolean AIS_Plane::Size(Standard_Real& X,Standard_Real& Y) const
413 X = myDrawer->PlaneAspect()->PlaneXLength();
414 Y = myDrawer->PlaneAspect()->PlaneYLength();
415 return Abs(X-Y)<=Precision::Confusion();
418 //=======================================================================
419 //function : SetColor
421 //=======================================================================
422 void AIS_Plane::SetColor(const Quantity_Color &aCol)
424 // if the plane already has its proper size, there is an already created planeaspect
425 // Standard_Boolean yenadeja = hasOwnColor || myHasOwnSize;
426 Handle(Prs3d_PlaneAspect) PA;
427 Handle(Prs3d_DatumAspect) DA;
429 PA = myDrawer->PlaneAspect();
430 DA = myDrawer->DatumAspect();
432 Standard_Boolean yenavaitPA(Standard_True),yenavaitDA(Standard_True);
433 if(myDrawer->HasLink() && myDrawer->Link()->PlaneAspect() == PA){
434 yenavaitPA = Standard_False;
435 PA = new Prs3d_PlaneAspect();}
436 if(myDrawer->HasLink() && myDrawer->Link()->DatumAspect() == DA){
437 yenavaitDA = Standard_False;
438 DA = new Prs3d_DatumAspect();
441 PA->EdgesAspect()->SetColor(aCol);
442 DA->LineAspect(Prs3d_DatumParts_XAxis)->SetColor(aCol);
443 DA->LineAspect(Prs3d_DatumParts_YAxis)->SetColor(aCol);
444 DA->LineAspect(Prs3d_DatumParts_ZAxis)->SetColor(aCol);
447 myDrawer->SetPlaneAspect(PA);
449 myDrawer->SetDatumAspect(DA);
451 myDrawer->ShadingAspect()->SetColor(aCol);
453 hasOwnColor=Standard_True;
454 myDrawer->SetColor (aCol);
456 //=======================================================================
457 //function : SetColor
459 //=======================================================================
460 void AIS_Plane::UnsetColor()
462 if(!hasOwnColor) return;
465 myDrawer->SetPlaneAspect (Handle(Prs3d_PlaneAspect)());
466 myDrawer->SetDatumAspect (Handle(Prs3d_DatumAspect)());
469 const Handle(Prs3d_PlaneAspect) PA = myDrawer->HasLink() ? myDrawer->Link()->PlaneAspect() :
470 new Prs3d_PlaneAspect();
471 Quantity_Color Col = PA->EdgesAspect()->Aspect()->Color();
472 myDrawer->PlaneAspect()->EdgesAspect()->SetColor(Col);
474 myDrawer->DatumAspect()->LineAspect(Prs3d_DatumParts_XAxis)->SetColor(Col);
475 myDrawer->DatumAspect()->LineAspect(Prs3d_DatumParts_YAxis)->SetColor(Col);
476 myDrawer->DatumAspect()->LineAspect(Prs3d_DatumParts_ZAxis)->SetColor(Col);
480 hasOwnColor=Standard_False;
484 //=======================================================================
485 //function : ComputeFrame
487 //=======================================================================
488 void AIS_Plane::ComputeFrame()
491 const Handle(Geom_Plane)& pl = myComponent;
494 if (myAutomaticPosition) {
495 ElSLib::Parameters(pl->Pln(),myCenter,U,V);
496 pl->D0 (U, V, myCenter);
499 Handle(Geom_Plane) thegoodpl (Handle(Geom_Plane)::DownCast(pl->Translated(pl->Location(),myCenter)));
500 ElSLib::Parameters(thegoodpl->Pln(),myPmin,U,V);
504 if (U < 10*Precision::Confusion()) U=0.1;
505 if (V < 10*Precision::Confusion()) V=0.1;
507 myDrawer->PlaneAspect()->SetPlaneLength(U,V);
511 //=======================================================================
512 //function : ComputeFields
514 //=======================================================================
515 void AIS_Plane::ComputeFields()
518 Handle (Prs3d_DatumAspect) DA = myDrawer->DatumAspect();
520 gp_Pnt Orig = myAx2->Ax2().Location();
521 gp_Dir oX = myAx2->Ax2().XDirection();
522 gp_Dir oY = myAx2->Ax2().YDirection();
523 gp_Dir oZ = myAx2->Ax2().Direction();
525 Standard_Real xo,yo,zo,x1,y1,z1,x2,y2,z2,x3,y3,z3,x4=0,y4=0,z4=0;
526 Standard_Real x5=0,y5=0,z5=0;
527 Orig.Coord(xo,yo,zo);
531 Standard_Real DS1 = DA->AxisLength(Prs3d_DatumParts_XAxis);
532 Standard_Real DS2 = DA->AxisLength(Prs3d_DatumParts_YAxis);
533 Standard_Real DS3 = DA->AxisLength(Prs3d_DatumParts_ZAxis);
536 switch (myTypeOfPlane) {
537 case AIS_TOPL_XYPlane:
540 myComponent = new Geom_Plane(XYP);
541 x4=xo+x1*DS1; y4=yo+y1*DS1; z4=zo+z1*DS1;
542 x5=xo+x2*DS2; y5=yo+y2*DS2; z5=zo+z2*DS2;
545 case AIS_TOPL_XZPlane:
548 myComponent = new Geom_Plane(XZP);
549 x4=xo+x1*DS1; y4=yo+y1*DS1; z4=zo+z1*DS1;
550 x5=xo+x3*DS3; y5=yo+y3*DS3; z5=zo+z3*DS3;
553 case AIS_TOPL_YZPlane:
556 myComponent = new Geom_Plane(XZP);
557 x4=xo+x2*DS2; y4=yo+y2*DS2; z4=zo+z2*DS2;
558 x5=xo+x3*DS3; y5=yo+y3*DS3; z5=zo+z3*DS3;
564 myPmin.SetCoord(x4,y4,z4);
565 myPmax.SetCoord(x5,y5,z5);
569 //=======================================================================
570 //function : InitDrawerAttributes
572 //=======================================================================
574 void AIS_Plane::InitDrawerAttributes()
576 Handle(Prs3d_ShadingAspect) shasp = new Prs3d_ShadingAspect();
577 shasp->SetMaterial(Graphic3d_NameOfMaterial_Plastified);
578 shasp->SetColor(Quantity_NOC_GRAY40);
579 myDrawer->SetShadingAspect(shasp);
580 Handle(Graphic3d_AspectFillArea3d) asf = shasp->Aspect();
581 Graphic3d_MaterialAspect asp = asf->FrontMaterial();
582 asp.SetTransparency (0.8f);
583 asf->SetFrontMaterial(asp);
584 asf->SetBackMaterial(asp);
587 //=======================================================================
588 //function : AcceptDisplayMode
590 //=======================================================================
592 Standard_Boolean AIS_Plane::
593 AcceptDisplayMode(const Standard_Integer aMode) const
597 //=======================================================================
598 //function : SetContext
600 //=======================================================================
602 void AIS_Plane::SetContext(const Handle(AIS_InteractiveContext)& Ctx)
604 AIS_InteractiveObject::SetContext(Ctx);