0025773: Visualization - remove AIS_Drawer class and transfer its Link() logic to...
[occt.git] / src / AIS / AIS_MaxRadiusDimension.cxx
1 // Created on: 1998-01-23
2 // Created by: Sergey ZARITCHNY
3 // Copyright (c) 1998-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 <Standard_NotImplemented.hxx>
18
19 #include <AIS_MaxRadiusDimension.ixx>
20 #include <AIS_EllipseRadiusDimension.hxx>
21 #include <TCollection_ExtendedString.hxx>
22
23 #include <Prs3d_DimensionAspect.hxx>
24 #include <Prs3d_ArrowAspect.hxx>
25 #include <Prs3d_Drawer.hxx>
26 #include <Prs3d_TextAspect.hxx>
27 #include <Prs3d_Text.hxx> 
28
29 #include <Select3D_SensitiveSegment.hxx>
30 #include <Select3D_SensitiveCurve.hxx>
31 #include <Select3D_SensitiveBox.hxx>
32 #include <SelectMgr_EntityOwner.hxx>
33
34 #include <ElCLib.hxx>
35 #include <ElSLib.hxx>
36
37 #include <TopoDS.hxx>
38
39 #include <BRepAdaptor_Surface.hxx>
40 #include <BRepAdaptor_Curve.hxx>
41
42 #include <Geom_Ellipse.hxx>
43 #include <Geom_TrimmedCurve.hxx>
44 #include <Geom_Plane.hxx>
45 #include <Geom_Surface.hxx>
46 #include <Geom_CylindricalSurface.hxx>
47 #include <Geom_SurfaceOfRevolution.hxx>
48 #include <Geom_CylindricalSurface.hxx>
49 #include <Geom_SurfaceOfLinearExtrusion.hxx>
50
51 #include <gp_Pln.hxx>
52 #include <gp_Pnt.hxx>
53 #include <gp_Lin.hxx>
54 #include <gp_Ax1.hxx>
55 #include <gp_Dir.hxx>
56 #include <gp_Vec.hxx>
57
58 #include <AIS.hxx>
59
60 #include <Precision.hxx>
61 #include <DsgPrs_EllipseRadiusPresentation.hxx>
62
63 //=======================================================================
64 //function : AIS_MaxRadiusDimension
65 //purpose  : 
66 //=======================================================================
67
68 AIS_MaxRadiusDimension::AIS_MaxRadiusDimension(const TopoDS_Shape& aShape, 
69                                                const Standard_Real aVal, 
70                                                const TCollection_ExtendedString& aText)
71 :AIS_EllipseRadiusDimension(aShape, aText)
72 {
73   myVal = aVal;
74   mySymbolPrs = DsgPrs_AS_LASTAR;
75   myAutomaticPosition = Standard_True;
76   myArrowSize = myVal / 100.;
77 }
78
79 //=======================================================================
80 //function : AIS_MaxRadiusDimension
81 //purpose  : 
82 //=======================================================================
83
84 AIS_MaxRadiusDimension::AIS_MaxRadiusDimension(const TopoDS_Shape& aShape, 
85                                                const Standard_Real aVal, 
86                                                const TCollection_ExtendedString& aText,
87                                                const gp_Pnt& aPosition, 
88                                                const DsgPrs_ArrowSide aSymbolPrs,
89                                                const Standard_Real anArrowSize)
90 :AIS_EllipseRadiusDimension(aShape, aText)
91 {
92   myVal = aVal;
93   mySymbolPrs = aSymbolPrs;
94   myPosition = aPosition;
95   myAutomaticPosition = Standard_False;
96   SetArrowSize( anArrowSize );
97 }
98
99 //=======================================================================
100 //function : Compute
101 //purpose  : 
102 //=======================================================================
103
104 void AIS_MaxRadiusDimension::Compute(const Handle(PrsMgr_PresentationManager3d)& /*aPresentationManager*/,
105                                      const Handle(Prs3d_Presentation)& aPresentation, 
106                                      const Standard_Integer /*aMode*/)
107 {
108   aPresentation->Clear();
109
110 //  if( myAutomaticPosition )
111     {//ota : recompute in any case 
112       ComputeGeometry();
113       myEllipse.SetMajorRadius(myVal);
114       gp_Vec v1(myEllipse.XAxis().Direction());
115       v1 *=myVal;
116       myApexP = myEllipse.Location().Translated(v1); 
117       myApexN = myEllipse.Location().Translated(-v1); 
118     }
119   if(myIsAnArc) ComputeArcOfEllipse(aPresentation);
120   else 
121     ComputeEllipse(aPresentation);
122 }
123
124 //=======================================================================
125 //function : Compute
126 //purpose  : to avoid warning
127 //=======================================================================
128
129 void  AIS_MaxRadiusDimension::Compute(const Handle(Prs3d_Projector)& aProjector,
130                                       const Handle(Prs3d_Presentation)& aPresentation)
131 {
132 // Standard_NotImplemented::Raise("AIS_MaxRadiusDimension::Compute(const Handle(Prs3d_Projector)& aProjector, const Handle(Prs3d_Presentation)& aPresentation)");
133  PrsMgr_PresentableObject::Compute( aProjector , aPresentation ) ;
134 }
135
136 //=======================================================================
137 //function : Compute
138 //purpose  : 
139 //=======================================================================
140
141 void AIS_MaxRadiusDimension::Compute(const Handle(Prs3d_Projector)& aProjector,
142                                      const Handle(Geom_Transformation)& aTransformation,
143                                      const Handle(Prs3d_Presentation)& aPresentation)
144 {
145 // Standard_NotImplemented::Raise("AIS_MaxRadiusDimension::Compute(const Handle(Prs3d_Projector)&, const Handle(Geom_Transformation)&, const Handle(Prs3d_Presentation)&)");
146  PrsMgr_PresentableObject::Compute( aProjector , aTransformation , aPresentation ) ;
147 }
148
149
150 //=======================================================================
151 //function : ComputeEllipse
152 //purpose  : 
153 //=======================================================================
154
155 void AIS_MaxRadiusDimension::ComputeEllipse(const Handle(Prs3d_Presentation)& aPresentation)
156 {
157
158   Handle(Prs3d_DimensionAspect) la = myDrawer->DimensionAspect();
159   Handle(Prs3d_ArrowAspect) arr = la->ArrowAspect();
160   
161   // size
162   if( !myArrowSizeIsDefined ) {
163     myArrowSize = Min(myArrowSize,myVal / 5.);
164   }
165   arr->SetLength(myArrowSize);
166
167   Standard_Real U;//,V;
168   gp_Pnt curPos, Center;
169   Center = myEllipse.Location();
170   if( myAutomaticPosition )
171     {
172       myPosition = Center;
173       myEndOfArrow = myApexP;
174       myAutomaticPosition = Standard_True;   
175       
176       if ( myIsSetBndBox )
177         myPosition = AIS::TranslatePointToBound(myPosition, gp_Dir(gp_Vec( Center, myPosition )),
178                                                 myBndBox );        
179       curPos = myPosition;  
180     }
181   else //!AutomaticPosition
182     {
183       curPos = myPosition;
184       gp_Lin L1(myEllipse.XAxis());
185       U = ElCLib::Parameter ( L1, curPos );
186       curPos = ElCLib::Value (U, L1);
187       if (curPos.Distance(myApexP) < curPos.Distance(myApexN)) 
188         myEndOfArrow = myApexP ;
189       else
190         myEndOfArrow = myApexN ;
191     }
192  
193   // Presenatation  
194   DsgPrs_EllipseRadiusPresentation::Add(aPresentation, myDrawer, myVal, myText, curPos,
195                                         myEndOfArrow, Center, Standard_True, mySymbolPrs);
196
197 }
198
199 //=======================================================================
200 //function : ComputeArcOfEllipse
201 //purpose  : 
202 //=======================================================================
203
204 void AIS_MaxRadiusDimension::ComputeArcOfEllipse(const Handle(Prs3d_Presentation)& aPresentation)
205 {
206
207   Handle(Prs3d_DimensionAspect) la = myDrawer->DimensionAspect();
208   Handle(Prs3d_ArrowAspect) arr = la->ArrowAspect();
209   
210   // size
211   if( !myArrowSizeIsDefined ) {
212     myArrowSize = Min(myArrowSize,myVal / 5.);
213   }
214   arr->SetLength(myArrowSize);
215   
216   Standard_Real par;
217   gp_Pnt curPos, Center;
218   Center = myEllipse.Location();
219   Standard_Boolean IsInDomain = Standard_True;
220   if( myAutomaticPosition )
221     {
222       myEndOfArrow = AIS::NearestApex(myEllipse, myApexP, myApexN,
223                                       myFirstPar, myLastPar, IsInDomain);
224       myPosition = Center;
225       myAutomaticPosition = Standard_True;
226       if ( myIsSetBndBox )
227         myPosition = AIS::TranslatePointToBound(myPosition, gp_Dir(gp_Vec( Center, myPosition )),
228                                                 myBndBox );
229       curPos = myPosition;  
230     }
231   else //!AutomaticPosition
232     {
233       curPos = myPosition;
234 //      ElSLib::Parameters ( myPlane->Pln(), curPos, U, V );
235 //      curPos = ElSLib::Value (U, V, myPlane->Pln());
236       gp_Lin L1(myEllipse.XAxis());
237       par = ElCLib::Parameter ( L1, curPos );
238       curPos = ElCLib::Value (par, L1);
239       if (curPos.Distance(myApexP) < curPos.Distance(myApexN)) 
240         myEndOfArrow = myApexP ;
241       else
242         myEndOfArrow = myApexN ;
243       par = ElCLib::Parameter ( myEllipse, myEndOfArrow );
244       IsInDomain = AIS::InDomain( myFirstPar, myLastPar, par );
245       myPosition = curPos;
246     }
247
248 //  Standard_Real parEnd = ElCLib::Parameter ( myEllipse, myEndOfArrow );
249   Standard_Real parStart = 0.;
250   if( !IsInDomain )
251     {
252       if(AIS::DistanceFromApex (myEllipse, myEndOfArrow, myFirstPar) <
253          AIS::DistanceFromApex (myEllipse, myEndOfArrow, myLastPar))
254         parStart = myFirstPar;
255       else
256         parStart = myLastPar;
257     }
258   if(!myIsOffset)
259     DsgPrs_EllipseRadiusPresentation::Add(aPresentation, myDrawer, myVal, myText, myEllipse,
260                                           curPos, myEndOfArrow, Center, parStart, IsInDomain,
261                                           Standard_True, mySymbolPrs);
262   else 
263     DsgPrs_EllipseRadiusPresentation::Add(aPresentation, myDrawer, myVal, myText, myOffsetCurve,
264                                           curPos, myEndOfArrow, Center, parStart, IsInDomain,
265                                           Standard_True, mySymbolPrs);
266 }
267
268 //=======================================================================
269 //function : ComputeSelection
270 //purpose  : 
271 //=======================================================================
272
273 void AIS_MaxRadiusDimension::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection, 
274                                               const Standard_Integer /*aMode*/)
275 {
276
277     gp_Pnt        center  = myEllipse.Location();
278     gp_Pnt AttachmentPoint = myPosition;
279     Standard_Real dist    = center.Distance(AttachmentPoint);
280     Standard_Real aRadius = myVal;
281     //Standard_Real inside  = Standard_False;
282     gp_Pnt pt1;
283     if (dist > aRadius) pt1 = AttachmentPoint; 
284     else 
285       pt1 = myEndOfArrow;
286     Handle(SelectMgr_EntityOwner) own = new SelectMgr_EntityOwner(this,7);
287     Handle(Select3D_SensitiveSegment) 
288       seg = new Select3D_SensitiveSegment(own, center , pt1);
289     aSelection->Add(seg);
290
291     // Text
292     Standard_Real size(Min(myVal/100.+1.e-6,myArrowSize+1.e-6));
293     Handle( Select3D_SensitiveBox ) box = new Select3D_SensitiveBox( own,
294                                                                      AttachmentPoint.X(),
295                                                                      AttachmentPoint.Y(),
296                                                                      AttachmentPoint.Z(),
297                                                                      AttachmentPoint.X()+size,
298                                                                      AttachmentPoint.Y()+size,
299                                                                      AttachmentPoint.Z()+size);
300   aSelection->Add(box);
301
302   // Arc of Ellipse
303     if(myIsAnArc)
304       {
305         
306         Standard_Real parEnd = ElCLib::Parameter ( myEllipse, myEndOfArrow );
307         if(!AIS::InDomain(myFirstPar, myLastPar, parEnd))
308           {
309             Standard_Real parStart, par;
310             if(AIS::DistanceFromApex (myEllipse, myEndOfArrow, myFirstPar) <
311                AIS::DistanceFromApex (myEllipse, myEndOfArrow, myLastPar))
312               par = myFirstPar;
313             else
314               par = myLastPar;
315             gp_Vec Vapex(center, ElCLib::Value( parEnd, myEllipse )) ;
316             gp_Vec Vpnt (center, ElCLib::Value( par, myEllipse )) ;
317             gp_Dir dir(Vpnt ^ Vapex);
318             if(myEllipse.Position().Direction().IsOpposite( dir, Precision::Angular())) {
319               parStart = parEnd;
320               parEnd   = par;
321             }
322             else 
323               parStart = par;
324
325             Handle(Geom_TrimmedCurve)TrimCurve;
326             if(myIsOffset)
327               {
328                 Handle(Geom_Curve) aCurve = myOffsetCurve;
329                 TrimCurve = new Geom_TrimmedCurve( aCurve,  parStart, parEnd );
330               }
331             else
332               {
333                 Handle(Geom_Ellipse) Ellipse = new Geom_Ellipse( myEllipse );
334                 TrimCurve = new Geom_TrimmedCurve( Ellipse,  parStart, parEnd );
335               }
336             Handle( Select3D_SensitiveCurve ) SensArc;
337             SensArc = new Select3D_SensitiveCurve( own, TrimCurve );
338             aSelection->Add( SensArc );
339           }
340     }
341
342 }