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