0030480: Visualization - Clear of Select3D_SensitiveGroup does not update internal...
[occt.git] / src / AIS / AIS_OffsetDimension.cxx
1 // Created on: 1997-03-04
2 // Created by: Jean-Pierre COMBE
3 // Copyright (c) 1997-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
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.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17
18 #include <AIS.hxx>
19 #include <AIS_OffsetDimension.hxx>
20 #include <BRep_Tool.hxx>
21 #include <BRepAdaptor_Surface.hxx>
22 #include <BRepBuilderAPI_Transform.hxx>
23 #include <DsgPrs_OffsetPresentation.hxx>
24 #include <ElCLib.hxx>
25 #include <ElSLib.hxx>
26 #include <gce_MakeLin.hxx>
27 #include <Geom_Transformation.hxx>
28 #include <GeomAbs_SurfaceType.hxx>
29 #include <gp_Ax1.hxx>
30 #include <gp_Ax2.hxx>
31 #include <gp_Ax3.hxx>
32 #include <gp_Lin.hxx>
33 #include <gp_Pln.hxx>
34 #include <gp_Pnt.hxx>
35 #include <gp_Trsf.hxx>
36 #include <gp_Vec.hxx>
37 #include <Graphic3d_Structure.hxx>
38 #include <Precision.hxx>
39 #include <Prs3d_ArrowAspect.hxx>
40 #include <Prs3d_DimensionAspect.hxx>
41 #include <Prs3d_Drawer.hxx>
42 #include <Prs3d_LineAspect.hxx>
43 #include <Prs3d_Presentation.hxx>
44 #include <Prs3d_Projector.hxx>
45 #include <Select3D_SensitiveBox.hxx>
46 #include <Select3D_SensitiveSegment.hxx>
47 #include <SelectMgr_EntityOwner.hxx>
48 #include <SelectMgr_Selection.hxx>
49 #include <Standard_NotImplemented.hxx>
50 #include <Standard_Type.hxx>
51 #include <StdPrs_WFShape.hxx>
52 #include <TCollection_ExtendedString.hxx>
53 #include <TColStd_Array2OfReal.hxx>
54 #include <TopExp_Explorer.hxx>
55 #include <TopoDS.hxx>
56 #include <TopoDS_Shape.hxx>
57 #include <TopoDS_Vertex.hxx>
58
59 IMPLEMENT_STANDARD_RTTIEXT(AIS_OffsetDimension,AIS_Relation)
60
61 //=======================================================================
62 //function : AIS_OffsetDimension
63 //purpose  : 
64 //=======================================================================
65 AIS_OffsetDimension::AIS_OffsetDimension(const TopoDS_Shape& FistShape, 
66                                          const TopoDS_Shape& SecondShape,
67                                          const Standard_Real aVal,
68                                          const TCollection_ExtendedString& aText)
69 :AIS_Relation(),
70 myFAttach(0.,0.,0.),
71 mySAttach(0.,0.,0.)
72 {
73   myFShape = FistShape;
74   mySShape = SecondShape;
75   mySymbolPrs = DsgPrs_AS_BOTHAR;
76   myVal = aVal;
77   myText = aText;
78   //myArrowSize = fabs (myVal/5.);
79   myArrowSize = fabs (myVal/10.0);
80   if (myArrowSize > 30.) myArrowSize = 30.;
81   if (myArrowSize < 15.) myArrowSize = 15.;
82   //cout<<"AIS_OffsetDimension::AIS_OffsetDimension " <<  myArrowSize << " myArrowSize"<<endl;
83 }
84
85 //=======================================================================
86 //function : Compute
87 //purpose  : 
88 //=======================================================================
89 void AIS_OffsetDimension::Compute(const Handle(PrsMgr_PresentationManager3d)&,
90                                   const Handle(Prs3d_Presentation)& aprs,
91                                   const Standard_Integer)
92 {
93   gp_Trsf aInvertTrsf = myRelativePos;
94   //myArrowSize = fabs (myVal/5.);
95   myArrowSize = fabs (myVal/10.0);
96   if (myArrowSize > 30.) myArrowSize = 30.;
97   if (myArrowSize < 15.) myArrowSize = 15.;
98   //cout<<"AIS_OffsetDimension::AIS_OffsetDimension " <<  myArrowSize << " myArrowSize"<<endl;
99   
100   BRepAdaptor_Surface surf1(TopoDS::Face(myFShape));
101   BRepAdaptor_Surface surf2(TopoDS::Face(mySShape));
102   
103   if (surf1.GetType() == GeomAbs_Cylinder || 
104       surf1.GetType() == GeomAbs_Cone     || 
105       surf1.GetType() == GeomAbs_Torus) {
106     if (surf2.GetType() == GeomAbs_Cylinder ||
107         surf2.GetType() == GeomAbs_Cone     ||
108         surf2.GetType() == GeomAbs_Torus) {
109       ComputeTwoAxesOffset(aprs, aInvertTrsf);
110     } else {
111       ComputeAxeFaceOffset(aprs, aInvertTrsf);
112     }
113   }
114   else {
115     //myDirAttach : oriente de myFShape vers mySShape
116     gp_Pln aPln = surf1.Plane();
117     gp_Pnt aPnt = aPln.Location();
118     
119     gp_Pln bPln = surf2.Plane();
120     
121     Standard_Real uPnt, vPnt;
122     ElSLib::Parameters (bPln , aPnt , uPnt, vPnt);
123     gp_Pnt bPnt = ElSLib::Value (uPnt, vPnt, bPln);
124     if (aPnt.IsEqual(bPnt,Precision::Confusion())) {
125       gp_Ax1 aAx1 = aPln.Axis();
126       myDirAttach = aAx1.Direction();
127     } else {
128       gp_Vec aVec (aPnt,bPnt);
129       myDirAttach.SetCoord(aVec.X(),aVec.Y(),aVec.Z());
130     }
131     ComputeTwoFacesOffset(aprs, aInvertTrsf);
132   }  
133 }
134
135
136 //=======================================================================
137 //function : Compute
138 //purpose  : to avoid warning at compilation (SUN)
139 //=======================================================================
140 void AIS_OffsetDimension::Compute(const Handle(Prs3d_Projector)& /*aProjector*/,
141                                   const Handle(Prs3d_Presentation)& /*aPresentation*/)
142 {
143 // throw Standard_NotImplemented("AIS_OffsetDimension::Compute(const Handle(Prs3d_Projector)& aProjector,const Handle(Prs3d_Presentation)& aPresentation)");
144 // PrsMgr_PresentableObject::Compute( aProjector , aPresentation ) ;
145 }
146
147 void AIS_OffsetDimension::Compute(const Handle(Prs3d_Projector)& aProjector, 
148                                   const Handle(Geom_Transformation)& aTransformation,
149                                   const Handle(Prs3d_Presentation)& aPresentation)
150 {
151 // throw Standard_NotImplemented("AIS_OffsetDimension::Compute(const Handle(Prs3d_Projector)&,const Handle(Geom_Transformation)&,const Handle(Prs3d_Presentation)&)");
152  PrsMgr_PresentableObject::Compute( aProjector , aTransformation , aPresentation ) ;
153 }
154
155 //=======================================================================
156 //function : ComputeSelection
157 //purpose  : 
158 //=======================================================================
159 void AIS_OffsetDimension::ComputeSelection(const Handle(SelectMgr_Selection)& aSel, 
160                                            const Standard_Integer)
161 {
162   //myArrowSize = fabs (myVal/5.);
163   myArrowSize = fabs (myVal/10.0);
164   if (myArrowSize > 30.) myArrowSize = 30.;
165   if (myArrowSize < 15.) myArrowSize = 15.;
166   //cout<<"AIS_OffsetDimension::AIS_OffsetDimension " <<  myArrowSize << " myArrowSize"<<endl;
167   gp_Pnt myTFAttach = myFAttach.Transformed (myRelativePos);
168   gp_Pnt myTSAttach = mySAttach.Transformed (myRelativePos);
169   gp_Dir myTDirAttach = myDirAttach.Transformed (myRelativePos);
170   gp_Dir myTDirAttach2 = myDirAttach2.Transformed (myRelativePos);
171   gp_Pnt Tcurpos = myPosition.Transformed (myRelativePos);;
172
173   gp_Lin L1 (myTFAttach,myTDirAttach);
174   gp_Lin L2 (myTSAttach,myTDirAttach2);
175   gp_Pnt Proj1 = ElCLib::Value(ElCLib::Parameter(L1,Tcurpos),L1);
176   gp_Pnt Proj2 = ElCLib::Value(ElCLib::Parameter(L2,Tcurpos),L2);
177   gp_Lin L3;
178
179   Handle(SelectMgr_EntityOwner) own = new SelectMgr_EntityOwner(this,7);
180
181   if (!Proj1.IsEqual(Proj2,Precision::Confusion())) {
182     L3 = gce_MakeLin(Proj1,Proj2);
183   }
184   else {    // cas ou la dimension est nulle
185     if (!Proj1.IsEqual(Tcurpos,Precision::Confusion())) {
186       gp_Vec v3 (Proj1,Tcurpos);
187       gp_Dir d3 (v3);
188       L3 = gce_MakeLin(Proj1,d3);
189     } else {
190       L3 = gce_MakeLin(Proj1,myTDirAttach);
191     }
192
193   // Text
194     Standard_Real size(Min(myVal/100.+1.e-6,myArrowSize+1.e-6));
195     Handle( Select3D_SensitiveBox ) box = new Select3D_SensitiveBox( own,
196                                                                      Tcurpos.X(),
197                                                                      Tcurpos.Y(),
198                                                                      Tcurpos.Z(),
199                                                                      Tcurpos.X() + size,
200                                                                      Tcurpos.Y() + size,
201                                                                      Tcurpos.Z() + size);
202     aSel->Add(box);
203   }
204
205   Standard_Real parmin,parmax,parcur;
206   parmin = ElCLib::Parameter(L3,Proj1);
207   parmax = parmin;
208
209   parcur = ElCLib::Parameter(L3,Proj2);
210   parmin = Min(parmin,parcur);
211   parmax = Max(parmax,parcur);
212
213   parcur = ElCLib::Parameter(L3,Tcurpos);
214   parmin = Min(parmin,parcur);
215   parmax = Max(parmax,parcur);
216
217   gp_Pnt PointMin = ElCLib::Value(parmin,L3);
218   gp_Pnt PointMax = ElCLib::Value(parmax,L3);
219
220   Handle(Select3D_SensitiveSegment) seg;
221   if (!PointMin.IsEqual(PointMax,Precision::Confusion())) {
222     seg = new Select3D_SensitiveSegment(own,
223                                         PointMin,
224                                         PointMax);
225     aSel->Add(seg);
226   }
227   
228   if (!myTFAttach.IsEqual(Proj1,Precision::Confusion())) {
229     seg = new Select3D_SensitiveSegment(own,
230                                         myTFAttach,
231                                         Proj1);
232     aSel->Add(seg);
233   }
234   if (!myTSAttach.IsEqual(Proj2,Precision::Confusion())) {
235     seg = new Select3D_SensitiveSegment(own,
236                                         myTSAttach,
237                                         Proj2);
238     aSel->Add(seg);
239   }
240 }
241
242 //=======================================================================
243 //function : ComputeTwoAxesOffset
244 //purpose  : 
245 //=======================================================================
246 void AIS_OffsetDimension::ComputeTwoAxesOffset(const Handle(Prs3d_Presentation)& aprs, 
247                                                 const gp_Trsf& aTrsf)
248 {
249   BRepAdaptor_Surface surf1(TopoDS::Face(myFShape));
250   BRepAdaptor_Surface surf2(TopoDS::Face(mySShape));
251
252   gp_Ax1 Ax1Surf1, Ax1Surf2;
253
254   if (surf1.GetType() == GeomAbs_Cylinder) {
255     gp_Cylinder aCyl= surf1.Cylinder();
256     Ax1Surf1 = aCyl.Axis();    
257   } else if (surf1.GetType() == GeomAbs_Cone) {
258     gp_Cone aCone= surf1.Cone();
259     Ax1Surf1 = aCone.Axis();    
260   } else if (surf1.GetType() == GeomAbs_Torus) {
261     gp_Torus aTore= surf1.Torus();
262     Ax1Surf1 = aTore.Axis();    
263   }
264   Standard_Real FirstUParam = surf1.FirstUParameter();
265   Standard_Real FirstVParam = surf1.FirstVParameter();
266   Standard_Real LastVParam  = surf1.LastVParameter();
267   gp_Pnt P1First = surf1.Value(FirstUParam,FirstVParam);
268   gp_Pnt P1Last  = surf1.Value(FirstUParam,LastVParam);
269
270
271   if (surf2.GetType() == GeomAbs_Cylinder) {
272     gp_Cylinder aCyl= surf2.Cylinder();
273     Ax1Surf2 = aCyl.Axis();    
274   } else if (surf2.GetType() == GeomAbs_Cone) {
275     gp_Cone aCone= surf2.Cone();
276     Ax1Surf2 = aCone.Axis();    
277   } else if (surf2.GetType() == GeomAbs_Torus) {
278     gp_Torus aTore= surf2.Torus();
279     Ax1Surf2 = aTore.Axis();    
280   }
281   FirstUParam = surf2.FirstUParameter();
282   FirstVParam = surf2.FirstVParameter();
283   LastVParam  = surf2.LastVParameter();
284   gp_Pnt P2First = surf2.Value(FirstUParam,FirstVParam);
285   gp_Pnt P2Last  = surf2.Value(FirstUParam,LastVParam);
286
287   
288   
289   myFAttach = Ax1Surf1.Location();
290   mySAttach = Ax1Surf2.Location();
291   myDirAttach = Ax1Surf1.Direction();
292   myDirAttach2 = myDirAttach;
293   gp_Pnt curpos;
294   gp_Lin aProjLine  = gce_MakeLin(myFAttach,myDirAttach);
295
296   if (myAutomaticPosition) {
297     curpos.SetX ( (myFAttach.X() +  mySAttach.X()) /2. + 0.01);
298     curpos.SetY ( (myFAttach.Y() +  mySAttach.Y()) /2. + 0.01);
299     curpos.SetZ ( (myFAttach.Z() +  mySAttach.Z()) /2. + 0.01);
300     // + 0.01 pour eviter un raise de ComputeSelection...
301
302     myPosition = curpos;
303   } 
304   else {
305     curpos = myPosition;
306   }
307   
308   curpos = ElCLib::Value(ElCLib::Parameter(aProjLine,curpos),aProjLine);
309   // on projette pour la presentation
310
311   gp_Pnt P1FirstProj  = ElCLib::Value(ElCLib::Parameter(aProjLine,P1First),aProjLine);
312   gp_Pnt P1LastProj   = ElCLib::Value(ElCLib::Parameter(aProjLine,P1Last),aProjLine);
313   if (P1FirstProj.Distance(curpos) > P1LastProj.Distance(curpos))
314     myFAttach = P1FirstProj;
315   else
316     myFAttach = P1LastProj;
317   
318   gp_Pnt P2FirstProj  = ElCLib::Value(ElCLib::Parameter(aProjLine,P2First),aProjLine);
319   gp_Pnt P2LastProj   = ElCLib::Value(ElCLib::Parameter(aProjLine,P2Last),aProjLine);
320   if (P2FirstProj.Distance(curpos) > P2LastProj.Distance(curpos))
321     mySAttach = P2FirstProj;
322   else
323     mySAttach = P2LastProj;
324
325
326   Handle(Prs3d_DimensionAspect) la = myDrawer->DimensionAspect();
327   Handle(Prs3d_ArrowAspect) arr = la->ArrowAspect();
328   //cout<<"AIS_OffsetDimension::AIS_OffsetDimension " <<  myArrowSize << " myArrowSize"<<endl;
329   arr->SetLength(myArrowSize);
330   arr = la->ArrowAspect();
331   arr->SetLength(myArrowSize);
332
333   gp_Pnt myTFAttach = myFAttach.Transformed (aTrsf);
334   gp_Pnt myTSAttach = mySAttach.Transformed (aTrsf);
335   gp_Dir myTDirAttach = myDirAttach.Transformed (aTrsf);
336   gp_Dir myTDirAttach2 = myTDirAttach;
337   gp_Pnt Tcurpos = curpos.Transformed (aTrsf);
338  
339   if (myIsSetBndBox)
340     Tcurpos = AIS::TranslatePointToBound( Tcurpos, myDirAttach, myBndBox );
341
342   DsgPrs_OffsetPresentation::AddAxes(aprs,
343                                      myDrawer,
344                                      myText,
345                                      myTFAttach,
346                                      myTSAttach,
347                                      myTDirAttach,
348                                      myTDirAttach2,
349                                      Tcurpos);
350   
351   BRepBuilderAPI_Transform transform1 (myFShape, aTrsf, Standard_True);
352   TopoDS_Shape myTFShape = transform1.Shape();
353   BRepBuilderAPI_Transform transform2 (mySShape, aTrsf, Standard_True);
354   TopoDS_Shape myTSShape = transform2.Shape();
355
356   StdPrs_WFShape::Add (aprs, myTFShape, myDrawer);
357   StdPrs_WFShape::Add (aprs, myTSShape, myDrawer);
358 }
359
360 //=======================================================================
361 //function : ComputeTwoFacesOffset
362 //purpose  : 
363 //=======================================================================
364 void AIS_OffsetDimension::ComputeTwoFacesOffset(const Handle(Prs3d_Presentation)& aprs, 
365                                                 const gp_Trsf& aTrsf)
366 {
367   gp_Dir norm1 = myDirAttach;
368   gp_Pnt curpos;
369   gp_Ax2 myax2;
370   if (myAutomaticPosition && ! myIsSetBndBox) {
371     TopExp_Explorer explo(myFShape,TopAbs_VERTEX);
372     if (explo.More()) {
373       TopoDS_Vertex vertref = TopoDS::Vertex(explo.Current());
374       myFAttach = BRep_Tool::Pnt(vertref);
375       gp_Vec trans = norm1.XYZ()*fabs(myVal/2);
376       gp_Ax2 ax2(myFAttach,norm1);
377       myDirAttach = ax2.XDirection();
378       curpos = myFAttach.Translated(trans);
379       if (myVal <= Precision::Confusion()) {
380         gp_Vec vecnorm1 = norm1.XYZ()*.001;
381         curpos.Translate(vecnorm1);
382       }
383       myPosition = curpos;
384       myax2 = ax2;
385     }
386   }
387   else {
388     if (myAutomaticPosition && myIsSetBndBox)
389       {
390         Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
391         myBndBox.Get( aXmin, aYmin, aZmin, aXmax, aYmax, aZmax );
392         myPosition.SetCoord( aXmax, aYmax, aZmax );
393       }
394      
395     curpos = myPosition;
396     myFAttach = AIS::Nearest(myFShape,curpos);
397     if (myFAttach.Distance(curpos) <= Precision::Confusion()) {
398       gp_Ax2 ax2(myFAttach,norm1);
399       myDirAttach = ax2.XDirection();
400       myax2 = ax2;
401     }
402     else {
403       gp_Dir orient(myFAttach.XYZ()-curpos.XYZ());
404       gp_Ax2 ax2(myFAttach,norm1);
405       if (orient.Angle(norm1) <= Precision::Angular()) {
406         myDirAttach = ax2.XDirection();
407       }
408       else {
409         gp_Dir adir = norm1 ^ orient;
410         myDirAttach = adir ^ norm1;
411       }
412       myax2 = ax2;
413     }
414   }
415   // en attendant mieux
416   mySAttach = AIS::Nearest(mySShape,curpos);
417   gp_Ax3 anax3 (myax2);
418   gp_Pln apln (anax3);
419   
420   //gp_Pnt proj2;
421   Standard_Real u2,v2, uatt, vatt;
422   ElSLib::Parameters (apln , mySAttach, uatt,vatt);
423   ElSLib::Parameters (apln , curpos   , u2,v2);
424   
425   if (uatt== u2 && vatt == v2) {
426     myDirAttach2 = myDirAttach;
427   } else {
428     gp_Vec avec (ElSLib::Value (uatt,vatt, apln) , ElSLib::Value (u2,v2, apln));
429     myDirAttach2.SetCoord (avec.X(),avec.Y(),avec.Z());
430   }
431   
432   Handle(Prs3d_DimensionAspect) la = myDrawer->DimensionAspect();
433   Handle(Prs3d_ArrowAspect) arr = la->ArrowAspect();
434   //cout<<"AIS_OffsetDimension::AIS_OffsetDimension " <<  myArrowSize << " myArrowSize"<<endl;
435   arr->SetLength(myArrowSize);
436   arr = la->ArrowAspect();
437   arr->SetLength(myArrowSize);
438
439   gp_Pnt myTFAttach = myFAttach.Transformed (aTrsf);
440   gp_Pnt myTSAttach = mySAttach.Transformed (aTrsf);
441   gp_Dir myTDirAttach = myDirAttach.Transformed (aTrsf);
442   gp_Dir myTDirAttach2 = myDirAttach2.Transformed (aTrsf);
443   gp_Pnt Tcurpos = curpos.Transformed (aTrsf);
444
445 /*
446   if (myIsSetBndBox)
447     {
448       BRepAdaptor_Surface surf1(TopoDS::Face(myFShape));
449       Tcurpos = AIS::TranslatePointToBound( Tcurpos, surf1.Plane().XAxis().Direction(), myBndBox );
450     }
451 */
452   DsgPrs_OffsetPresentation::Add(aprs,
453                                  myDrawer,
454                                  myText,
455                                  myTFAttach,
456                                  myTSAttach,
457                                  myTDirAttach,
458                                  myTDirAttach2,
459                                  Tcurpos);
460   
461
462   BRepBuilderAPI_Transform transform1 (myFShape, aTrsf, Standard_True);
463   TopoDS_Shape myTFShape = transform1.Shape();
464   BRepBuilderAPI_Transform transform2 (mySShape, aTrsf, Standard_True);
465   TopoDS_Shape myTSShape = transform2.Shape();
466
467   StdPrs_WFShape::Add (aprs, myTFShape, myDrawer);
468   StdPrs_WFShape::Add (aprs, myTSShape, myDrawer);
469 }
470
471 //=======================================================================
472 //function : ComputeAxeFaceOffset
473 //purpose  : 
474 //=======================================================================
475 void AIS_OffsetDimension::ComputeAxeFaceOffset(const Handle(Prs3d_Presentation)& aprs, 
476                                                const gp_Trsf& aTrsf)
477 {
478   BRepBuilderAPI_Transform transform1 (myFShape, aTrsf, Standard_True);
479   TopoDS_Shape myTFShape = transform1.Shape();
480   BRepBuilderAPI_Transform transform2 (mySShape, aTrsf, Standard_True);
481   TopoDS_Shape myTSShape = transform2.Shape();
482
483   StdPrs_WFShape::Add (aprs, myTFShape, myDrawer);
484   StdPrs_WFShape::Add (aprs, myTSShape, myDrawer);
485 }
486