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