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