0031459: Visualization, AIS_TextLabel - add missing getters
[occt.git] / src / AIS / AIS_ParallelRelation.cxx
1 // Created on: 1996-12-05
2 // Created by: Jean-Pierre COMBE/Odile Olivier
3 // Copyright (c) 1996-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_ParallelRelation.hxx>
20 #include <BRep_Tool.hxx>
21 #include <BRepAdaptor_Curve.hxx>
22 #include <BRepAdaptor_Surface.hxx>
23 #include <DsgPrs_LengthPresentation.hxx>
24 #include <ElCLib.hxx>
25 #include <gce_MakeLin.hxx>
26 #include <Geom_Ellipse.hxx>
27 #include <Geom_Line.hxx>
28 #include <Geom_Plane.hxx>
29 #include <Geom_Transformation.hxx>
30 #include <gp_Ax1.hxx>
31 #include <gp_Ax2.hxx>
32 #include <gp_Dir.hxx>
33 #include <gp_Lin.hxx>
34 #include <gp_Pln.hxx>
35 #include <gp_Pnt.hxx>
36 #include <Precision.hxx>
37 #include <Prs3d_ArrowAspect.hxx>
38 #include <Prs3d_DimensionAspect.hxx>
39 #include <Prs3d_Drawer.hxx>
40 #include <Prs3d_Presentation.hxx>
41 #include <Select3D_SensitiveBox.hxx>
42 #include <Select3D_SensitiveSegment.hxx>
43 #include <SelectMgr_EntityOwner.hxx>
44 #include <SelectMgr_Selection.hxx>
45 #include <Standard_DomainError.hxx>
46 #include <Standard_NotImplemented.hxx>
47 #include <Standard_Type.hxx>
48 #include <TCollection_AsciiString.hxx>
49 #include <TCollection_ExtendedString.hxx>
50 #include <TopoDS.hxx>
51 #include <TopoDS_Shape.hxx>
52
53 IMPLEMENT_STANDARD_RTTIEXT(AIS_ParallelRelation,AIS_Relation)
54
55 //=======================================================================
56 //function : Constructor
57 //purpose  : 
58 //=======================================================================
59 AIS_ParallelRelation::AIS_ParallelRelation(const TopoDS_Shape& aFShape, 
60                                            const TopoDS_Shape& aSShape, 
61                                            const Handle(Geom_Plane)& aPlane)
62 {
63   myFShape = aFShape;
64   mySShape = aSShape;
65   myPlane = aPlane;
66   myAutomaticPosition = Standard_True;
67   myArrowSize = 0.01;
68   mySymbolPrs = DsgPrs_AS_BOTHAR;
69 }
70
71 //=======================================================================
72 //function : Constructor
73 //purpose  : 
74 //=======================================================================
75 AIS_ParallelRelation::AIS_ParallelRelation(const TopoDS_Shape& aFShape, 
76                                            const TopoDS_Shape& aSShape, 
77                                            const Handle(Geom_Plane)& aPlane, 
78                                            const gp_Pnt& aPosition,
79                                            const DsgPrs_ArrowSide aSymbolPrs, 
80                                            const Standard_Real anArrowSize)
81 {
82   myFShape = aFShape;
83   mySShape = aSShape;
84   myPlane = aPlane;
85   myAutomaticPosition = Standard_False;
86   SetArrowSize( anArrowSize );
87   myPosition = aPosition;
88   mySymbolPrs = aSymbolPrs;
89 }
90
91 //=======================================================================
92 //function : Compute
93 //purpose  : 
94 //=======================================================================
95 void AIS_ParallelRelation::Compute(const Handle(PrsMgr_PresentationManager3d)&, 
96                                    const Handle(Prs3d_Presentation)& aPresentation, 
97                                    const Standard_Integer)
98 {
99   switch (myFShape.ShapeType())
100     {
101     case TopAbs_FACE :
102       {
103         // cas longueur entre deux faces
104         ComputeTwoFacesParallel(aPresentation);
105       }
106       break;
107     case TopAbs_EDGE :
108       {
109         // cas longueur entre deux edges
110         ComputeTwoEdgesParallel(aPresentation);
111       }
112       break;
113     default:
114       break;
115     }
116 }
117
118 //=======================================================================
119 //function : ComputeSelection
120 //purpose  : 
121 //=======================================================================
122 void AIS_ParallelRelation::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection, 
123                                             const Standard_Integer)
124 {
125   gp_Lin L1 (myFAttach,myDirAttach);
126   gp_Lin L2 (mySAttach,myDirAttach);
127   gp_Pnt Proj1 = ElCLib::Value(ElCLib::Parameter(L1,myPosition),L1);
128   gp_Pnt Proj2 = ElCLib::Value(ElCLib::Parameter(L2,myPosition),L2);
129   
130   gp_Lin L3;
131   Handle(SelectMgr_EntityOwner) own = new SelectMgr_EntityOwner(this,7);
132
133   if (!Proj1.IsEqual(Proj2,Precision::Confusion()))
134     {
135       L3 = gce_MakeLin(Proj1,Proj2);
136     }
137   else
138     {
139       L3 = gce_MakeLin(Proj1,myDirAttach);
140       Standard_Real size(Min(myVal/100.+1.e-6,myArrowSize+1.e-6));
141       Handle(Select3D_SensitiveBox) box =
142         new Select3D_SensitiveBox(own,
143                                   myPosition.X(),
144                                   myPosition.Y(),
145                                   myPosition.Z(),
146                                   myPosition.X()+size,
147                                   myPosition.Y()+size,
148                                   myPosition.Z()+size);
149       aSelection->Add(box);
150     }
151   Standard_Real parmin,parmax,parcur;
152   parmin = ElCLib::Parameter(L3,Proj1);
153   parmax = parmin;
154   
155   parcur = ElCLib::Parameter(L3,Proj2);
156   parmin = Min(parmin,parcur);
157   parmax = Max(parmax,parcur);
158   
159   parcur = ElCLib::Parameter(L3,myPosition);
160   parmin = Min(parmin,parcur);
161   parmax = Max(parmax,parcur);
162   
163   gp_Pnt PointMin = ElCLib::Value(parmin,L3);
164   gp_Pnt PointMax = ElCLib::Value(parmax,L3);
165
166   Handle(Select3D_SensitiveSegment) seg;
167   
168   if (!PointMin.IsEqual(PointMax,Precision::Confusion()))
169     {
170       seg = new Select3D_SensitiveSegment(own,
171                                           PointMin,
172                                           PointMax);
173       aSelection->Add(seg);
174     }
175   if (!myFAttach.IsEqual(Proj1,Precision::Confusion()))
176     {
177       seg = new Select3D_SensitiveSegment(own, myFAttach, Proj1);
178       aSelection->Add(seg);
179     }
180   if (!mySAttach.IsEqual(Proj2,Precision::Confusion()))
181     {
182       seg = new Select3D_SensitiveSegment(own, mySAttach, Proj2);
183       aSelection->Add(seg);
184     }
185 }
186
187 //=======================================================================
188 //function : ComputeTwoFacesParallel
189 //purpose  : 
190 //=======================================================================
191 void AIS_ParallelRelation::ComputeTwoFacesParallel(const Handle(Prs3d_Presentation)&)
192 {
193   throw Standard_NotImplemented("AIS_ParallelRelation::ComputeTwoFacesParallel not implemented");
194 }
195
196 //=======================================================================
197 //function : ComputeTwoEdgesParallel
198 //purpose  : 
199 //=======================================================================
200 void AIS_ParallelRelation::ComputeTwoEdgesParallel(const Handle(Prs3d_Presentation)& aPresentation)
201 {
202   TopoDS_Edge E1 = TopoDS::Edge(myFShape);
203   TopoDS_Edge E2 = TopoDS::Edge(mySShape);
204
205   gp_Pnt ptat11,ptat12,ptat21,ptat22;//,pint3d;
206   Handle(Geom_Curve) geom1,geom2;
207   Standard_Boolean isInfinite1,isInfinite2;
208   Handle(Geom_Curve) extCurv;
209   if (!AIS::ComputeGeometry(E1,E2,myExtShape,
210                             geom1,geom2,
211                             ptat11,ptat12,ptat21,ptat22,
212                             extCurv,
213                             isInfinite1,isInfinite2,
214                             myPlane))
215     {
216       return;
217     }
218
219   aPresentation->SetInfiniteState((isInfinite1 || isInfinite2) && (myExtShape != 0));
220
221   gp_Lin l1;
222   gp_Lin l2;
223   Standard_Boolean isEl1 = Standard_False, isEl2 = Standard_False;
224
225   if (geom1->IsInstance(STANDARD_TYPE(Geom_Ellipse)))
226     {
227       Handle(Geom_Ellipse) geom_el1 (Handle(Geom_Ellipse)::DownCast (geom1));
228       // construct lines through focuses
229       gp_Ax1 elAx = geom_el1->XAxis();
230       l1 = gp_Lin(elAx);
231       Standard_Real focex = geom_el1->MajorRadius() - geom_el1->Focal()/2.0;
232       gp_Vec transvec = gp_Vec(elAx.Direction())*focex;
233       ptat11 = geom_el1->Focus1().Translated(transvec);
234       ptat12 = geom_el1->Focus2().Translated(-transvec);
235       isEl1 = Standard_True;
236     }
237   else if (geom1->IsInstance(STANDARD_TYPE(Geom_Line)))
238     {
239       Handle(Geom_Line) geom_lin1 (Handle(Geom_Line)::DownCast (geom1));
240       l1 = geom_lin1->Lin();
241     }
242   else return;
243
244   if (geom2->IsInstance(STANDARD_TYPE(Geom_Ellipse)))
245     {
246       Handle(Geom_Ellipse) geom_el2 (Handle(Geom_Ellipse)::DownCast (geom2));
247       // construct lines through focuses
248       gp_Ax1 elAx = geom_el2->XAxis();
249       l2 = gp_Lin(elAx);
250       Standard_Real focex = geom_el2->MajorRadius() - geom_el2->Focal()/2.0;
251       gp_Vec transvec = gp_Vec(elAx.Direction())*focex;
252       ptat21 = geom_el2->Focus1().Translated(transvec);
253       ptat22 = geom_el2->Focus2().Translated(-transvec);
254       isEl2 = Standard_True;
255     }
256   else if (geom2->IsInstance(STANDARD_TYPE(Geom_Line)))
257     {
258       Handle(Geom_Line) geom_lin2 (Handle(Geom_Line)::DownCast (geom2));
259       l2 = geom_lin2->Lin();
260     }
261   else return;
262
263   const Handle(Geom_Line)& geom_lin1 = new Geom_Line(l1);
264   const Handle(Geom_Line)& geom_lin2 = new Geom_Line(l2);
265
266   myDirAttach = l1.Direction();
267   // size
268   if( !myArrowSizeIsDefined ) {
269     Standard_Real arrSize1 (myArrowSize), arrSize2 (myArrowSize);
270     if (!isInfinite1) arrSize1 = ptat11.Distance(ptat12)/50.;
271     if (!isInfinite2) arrSize2 = ptat21.Distance(ptat22)/50.;
272     myArrowSize = Max(myArrowSize,Max(arrSize1,arrSize2));
273 //  myArrowSize = Min(myArrowSize,Min(arrSize1,arrSize2));
274   }
275
276   if ( myAutomaticPosition )
277     {    
278       gp_Pnt curpos;
279       if ( !isInfinite1 )
280         {
281           gp_Pnt p2 = ElCLib::Value(ElCLib::Parameter(l2,ptat11),l2);
282           curpos.SetXYZ((ptat11.XYZ() + p2.XYZ())/2.);
283         }
284       else if ( !isInfinite2 )
285         {
286           gp_Pnt p2 = ElCLib::Value(ElCLib::Parameter(l1,ptat21),l1);
287           curpos.SetXYZ((ptat21.XYZ()+p2.XYZ())/2.);
288         }
289       else
290         {
291           curpos.SetXYZ((l1.Location().XYZ()+l2.Location().XYZ())/2.);
292         }
293       // offset pour eviter confusion Edge et Dimension
294       gp_Vec offset (myDirAttach);
295       offset = offset*myArrowSize*(-10.);
296       curpos.Translate(offset);
297       myPosition = curpos;
298     }
299
300   // recherche points attache
301   if (!isInfinite1)
302     {
303       if ( isEl1 )
304         {
305           if (myPosition.Distance(ptat11) < myPosition.Distance(ptat12)) myFAttach = ptat12;
306           else myFAttach = ptat11;
307         }
308       else
309         {
310           if (myPosition.Distance(ptat11) > myPosition.Distance(ptat12)) myFAttach = ptat12;
311           else myFAttach = ptat11;
312         }
313     }
314   else
315     {
316       myFAttach = ElCLib::Value(ElCLib::Parameter(l1,myPosition),l1);
317     }
318   
319   if (!isInfinite2)
320     {
321       if ( isEl2 )
322         {
323           if (myPosition.Distance(ptat21) < myPosition.Distance(ptat22)) mySAttach = ptat22;
324           else mySAttach = ptat21;
325         }
326       else
327         {
328           if (myPosition.Distance(ptat21) > myPosition.Distance(ptat22)) mySAttach = ptat22;
329           else mySAttach = ptat21;
330         }
331     }
332   else
333     {
334       mySAttach = ElCLib::Value(ElCLib::Parameter(l2,myPosition),l2);
335     }
336   TCollection_ExtendedString aText (" //");
337   
338   if (l1.Distance(l2) <= Precision::Confusion())
339     {
340       myArrowSize = 0.;
341     }
342   Handle(Prs3d_DimensionAspect) la = myDrawer->DimensionAspect();
343   Handle(Prs3d_ArrowAspect) arr = la->ArrowAspect();
344   arr->SetLength(myArrowSize);
345   arr = la->ArrowAspect();
346   arr->SetLength(myArrowSize);
347   if ( myExtShape == 1)
348     mySymbolPrs = DsgPrs_AS_FIRSTPT_LASTAR;
349   else if ( myExtShape == 2)
350     mySymbolPrs = DsgPrs_AS_FIRSTAR_LASTPT;
351
352   DsgPrs_LengthPresentation::Add(aPresentation,
353                                  myDrawer,
354                                  aText,
355                                  myFAttach,
356                                  mySAttach,
357                                  myDirAttach,
358                                  myPosition,
359                                  mySymbolPrs);
360   if ( (myExtShape != 0) &&  !extCurv.IsNull())
361     {
362       gp_Pnt pf, pl;
363       if ( myExtShape == 1 )
364         {
365           if (!isInfinite1)
366             {
367               pf = ptat11; 
368               pl = ptat12;
369             }
370           ComputeProjEdgePresentation(aPresentation,E1,geom_lin1,pf,pl);
371         }
372       else
373         {
374           if (!isInfinite2)
375             {
376               pf = ptat21; 
377               pl = ptat22;
378             }
379           ComputeProjEdgePresentation(aPresentation,E2,geom_lin2,pf,pl);
380         }
381     }
382 }