1b5a5aa1828f9535bec9f358cf0c9553b0800e33
[occt.git] / src / AIS / AIS_MinRadiusDimension.cxx
1 // Created on: 1998-01-27
2 // Created by: Sergey ZARITCHNY
3 // Copyright (c) 1998-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
5 //
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
10 //
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 //
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
20
21
22 #define BUC60915        //GG 05/06/01 Enable to compute the requested arrow size
23 //                      if any in all dimensions.
24
25 #include <Standard_NotImplemented.hxx>
26
27 #include <AIS_MinRadiusDimension.ixx>
28
29 #include <AIS_EllipseRadiusDimension.hxx>
30 #include <TCollection_ExtendedString.hxx>
31
32 #include <Prs3d_LengthAspect.hxx>
33 #include <Prs3d_ArrowAspect.hxx>
34 #include <Prs3d_Drawer.hxx>
35 #include <Prs3d_TextAspect.hxx>
36 #include <Prs3d_Text.hxx>
37
38 #include <Select3D_SensitiveSegment.hxx>
39 #include <Select3D_SensitiveCurve.hxx>
40 #include <Select3D_SensitiveBox.hxx>
41 #include <SelectMgr_EntityOwner.hxx>
42
43 #include <ElCLib.hxx>
44 #include <ElSLib.hxx>
45
46 #include <TopoDS.hxx>
47
48 #include <BRepAdaptor_Surface.hxx>
49 #include <BRepAdaptor_Curve.hxx>
50
51 #include <Geom_Ellipse.hxx>
52 #include <Geom_TrimmedCurve.hxx>
53 #include <Geom_Plane.hxx>
54 #include <Geom_Surface.hxx>
55 #include <Geom_CylindricalSurface.hxx>
56 #include <Geom_SurfaceOfRevolution.hxx>
57 #include <Geom_CylindricalSurface.hxx>
58 #include <Geom_SurfaceOfLinearExtrusion.hxx>
59
60 #include <gp_Pln.hxx>
61 #include <gp_Pnt.hxx>
62 #include <gp_Lin.hxx>
63 #include <gp_Ax1.hxx>
64 #include <gp_Dir.hxx>
65 #include <gp_Vec.hxx>
66
67 #include <AIS.hxx>
68 #include <AIS_Drawer.hxx>
69
70 #include <Precision.hxx>
71 #include <DsgPrs_EllipseRadiusPresentation.hxx>
72
73
74 //=======================================================================
75 //function : AIS_MinRadiusDimension
76 //purpose  : 
77 //=======================================================================
78
79 AIS_MinRadiusDimension::AIS_MinRadiusDimension(const TopoDS_Shape& aShape, 
80                                                const Standard_Real aVal, 
81                                                const TCollection_ExtendedString& aText)
82 :AIS_EllipseRadiusDimension(aShape, aText)
83 {
84   myVal = aVal;
85   mySymbolPrs = DsgPrs_AS_LASTAR;
86   myAutomaticPosition = Standard_True;
87   myArrowSize = myVal / 100.;
88 }
89
90 //=======================================================================
91 //function : AIS_MinRadiusDimension
92 //purpose  : 
93 //=======================================================================
94
95 AIS_MinRadiusDimension::AIS_MinRadiusDimension(const TopoDS_Shape& aShape, 
96                                                const Standard_Real aVal, 
97                                                const TCollection_ExtendedString& aText,
98                                                const gp_Pnt& aPosition, 
99                                                const DsgPrs_ArrowSide aSymbolPrs,
100                                                const Standard_Real anArrowSize)
101 :AIS_EllipseRadiusDimension(aShape, aText)
102 {
103   myVal = aVal;
104   mySymbolPrs = aSymbolPrs;
105   myPosition = aPosition;
106   myAutomaticPosition = Standard_False;
107 #ifdef BUC60915
108   SetArrowSize( anArrowSize );
109 #else
110   myArrowSize = anArrowSize;
111 #endif
112 }
113
114 //=======================================================================
115 //function : Compute
116 //purpose  : 
117 //=======================================================================
118
119 void AIS_MinRadiusDimension::Compute(const Handle(PrsMgr_PresentationManager3d)& /*aPresentationManager*/,
120                                      const Handle(Prs3d_Presentation)& aPresentation, 
121                                      const Standard_Integer /*aMode*/)
122 {
123   aPresentation->Clear();
124
125 //  if( myAutomaticPosition )
126     //{ //ota : recompute ellipse always
127   ComputeGeometry();
128   myEllipse.SetMinorRadius(myVal);
129   gp_Vec v1(myEllipse.YAxis().Direction());
130   v1 *=myVal;
131   myApexP = myEllipse.Location().Translated(v1); 
132   myApexN = myEllipse.Location().Translated(-v1); 
133 //   }
134   if(myIsAnArc) ComputeArcOfEllipse(aPresentation);
135   else 
136     ComputeEllipse(aPresentation);
137 }
138
139 //=======================================================================
140 //function : Compute
141 //purpose  : to avoid warning
142 //=======================================================================
143
144 void  AIS_MinRadiusDimension::Compute(const Handle(Prs3d_Projector)& aProjector,
145                                       const Handle(Prs3d_Presentation)& aPresentation)
146 {
147 // Standard_NotImplemented::Raise("AIS_MinRadiusDimension::Compute(const Handle(Prs3d_Projector)& aProjector, const Handle(Prs3d_Presentation)& aPresentation)");
148  PrsMgr_PresentableObject::Compute( aProjector , aPresentation ) ;
149 }
150
151 //=======================================================================
152 //function : Compute
153 //purpose  : to avoid warning
154 //=======================================================================
155
156 void  AIS_MinRadiusDimension::Compute(const Handle(PrsMgr_PresentationManager2d)& aPresentationManager,
157                                       const Handle(Graphic2d_GraphicObject)& aPresentation,
158                                       const Standard_Integer aMode)
159 {
160 // Standard_NotImplemented::Raise("AIS_MinRadiusDimension::Compute(const Handle(PrsMgr_PresentationManager2d)& aPresentationManager, const Handle(Graphic2d_GraphicObject)& aPresentation, const Standard_Integer aMode)");
161  PrsMgr_PresentableObject::Compute( aPresentationManager ,aPresentation,aMode) ;
162 }
163
164 //=======================================================================
165 //function : Compute
166 //purpose  : 
167 //=======================================================================
168
169 void AIS_MinRadiusDimension::Compute(const Handle_Prs3d_Projector& aProjector,
170                                      const Handle_Geom_Transformation& aTransformation,
171                                      const Handle_Prs3d_Presentation& aPresentation)
172 {
173 // Standard_NotImplemented::Raise("AIS_MinRadiusDimension::Compute(const Handle_Prs3d_Projector&, const Handle_Geom_Transformation&, const Handle_Prs3d_Presentation&)");
174  PrsMgr_PresentableObject::Compute( aProjector , aTransformation , aPresentation ) ;
175 }
176
177 //=======================================================================
178 //function : ComputeEllipse
179 //purpose  : 
180 //=======================================================================
181
182 void AIS_MinRadiusDimension::ComputeEllipse(const Handle(Prs3d_Presentation)& aPresentation)
183 {
184
185   Handle(Prs3d_LengthAspect) la = myDrawer->LengthAspect();
186   Handle(Prs3d_ArrowAspect) arr = la->Arrow1Aspect();
187   
188   // size
189 #ifdef BUC60915
190   if( !myArrowSizeIsDefined ) {
191     myArrowSize = Min(myArrowSize,myVal/5.);
192   }
193   arr->SetLength(myArrowSize);
194 #else
195   if (myVal/5. > myArrowSize) {
196     arr->SetLength(myArrowSize);
197   }
198   else {
199     arr->SetLength(myVal/5.);
200   }
201 #endif
202
203   Standard_Real U;//,V;
204   gp_Pnt curPos, Center;
205   Center = myEllipse.Location();
206   if( myAutomaticPosition )
207     {
208       myPosition = Center;
209       myEndOfArrow = myApexP;
210       myAutomaticPosition = Standard_True;   
211       
212       if ( myIsSetBndBox )
213         myPosition = AIS::TranslatePointToBound(myPosition, gp_Dir(gp_Vec( Center, myPosition )),
214                                                 myBndBox );        
215       curPos = myPosition;  
216     }
217   else //!AutomaticPosition
218     {
219       curPos = myPosition;
220       gp_Lin L1(myEllipse.YAxis());
221       U = ElCLib::Parameter ( L1, curPos );
222       curPos = ElCLib::Value (U, L1);
223       if (curPos.Distance(myApexP) < curPos.Distance(myApexN)) 
224         myEndOfArrow = myApexP ;
225       else
226         myEndOfArrow = myApexN ;
227       myPosition = curPos;
228     }
229  
230   // Presenatation  
231   DsgPrs_EllipseRadiusPresentation::Add(aPresentation, myDrawer, myVal, myText, curPos,
232                                         myEndOfArrow, Center, Standard_False, mySymbolPrs);
233
234 }
235
236 //=======================================================================
237 //function : ComputeArcOfEllipse
238 //purpose  : 
239 //=======================================================================
240
241 void AIS_MinRadiusDimension::ComputeArcOfEllipse(const Handle(Prs3d_Presentation)& aPresentation)
242 {
243
244   Handle(Prs3d_LengthAspect) la = myDrawer->LengthAspect();
245   Handle(Prs3d_ArrowAspect) arr = la->Arrow1Aspect();
246   
247   // size
248 #ifdef BUC60915
249   if( !myArrowSizeIsDefined ) {
250     myArrowSize = Min(myArrowSize,myVal/5.);
251   }
252   arr->SetLength(myArrowSize);
253 #else
254   if (myVal/5. > myArrowSize) {
255     arr->SetLength(myArrowSize);
256   }
257   else {
258     arr->SetLength(myVal/5.);
259   }
260 #endif
261   
262   Standard_Real par;
263   gp_Pnt curPos, Center;
264   Center = myEllipse.Location();
265   Standard_Boolean IsInDomain = Standard_True;
266   if( myAutomaticPosition )
267     {
268       myEndOfArrow = AIS::NearestApex(myEllipse, myApexP, myApexN,
269                                       myFirstPar, myLastPar, IsInDomain);
270       myPosition = Center;
271       myAutomaticPosition = Standard_True;
272       if ( myIsSetBndBox )
273         myPosition = AIS::TranslatePointToBound(myPosition, gp_Dir(gp_Vec( Center, myPosition )),
274                                                 myBndBox );
275       curPos = myPosition;  
276
277     }
278   else //!AutomaticPosition
279     {
280       curPos = myPosition;
281       gp_Lin L1(myEllipse.YAxis());
282       par = ElCLib::Parameter ( L1, curPos );
283       curPos = ElCLib::Value (par, L1);
284       if (curPos.Distance(myApexP) < curPos.Distance(myApexN)) 
285         myEndOfArrow = myApexP ;
286       else
287         myEndOfArrow = myApexN ;
288       par = ElCLib::Parameter ( myEllipse, myEndOfArrow );
289       IsInDomain = AIS::InDomain(myFirstPar, myLastPar, par);
290       myPosition = curPos;
291     }
292
293   Standard_Real parStart =0.;
294   if( !IsInDomain )
295     {
296       if(AIS::DistanceFromApex (myEllipse, myEndOfArrow, myFirstPar) <
297          AIS::DistanceFromApex (myEllipse, myEndOfArrow, myLastPar))
298         parStart = myFirstPar;
299       else
300         parStart = myLastPar;
301
302     }
303
304   if(!myIsOffset)
305     DsgPrs_EllipseRadiusPresentation::Add(aPresentation, myDrawer, myVal, myText, myEllipse,
306                                           curPos, myEndOfArrow, Center, parStart, IsInDomain,
307                                           Standard_True, mySymbolPrs);
308   else 
309     DsgPrs_EllipseRadiusPresentation::Add(aPresentation, myDrawer, myVal, myText, myOffsetCurve,
310                                           curPos, myEndOfArrow, Center, parStart, IsInDomain,
311                                           Standard_True, mySymbolPrs);
312 }
313
314
315 //=======================================================================
316 //function : ComputeSelection
317 //purpose  : 
318 //=======================================================================
319
320 void AIS_MinRadiusDimension::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection, 
321                                               const Standard_Integer /*aMode*/)
322 {
323
324     gp_Pnt        center   = myEllipse.Location();
325     gp_Pnt AttachmentPoint = myPosition;
326     Standard_Real dist    = center.Distance(AttachmentPoint);
327     Standard_Real aRadius = myVal;
328     //Standard_Real inside  = Standard_False;
329     gp_Pnt pt1;
330     if (dist > aRadius) pt1 = AttachmentPoint; 
331     else 
332       pt1 = myEndOfArrow;
333     Handle(SelectMgr_EntityOwner) own = new SelectMgr_EntityOwner(this,7);
334     Handle(Select3D_SensitiveSegment) 
335       seg = new Select3D_SensitiveSegment(own, center , pt1);
336     aSelection->Add(seg);
337
338     // Text
339     Standard_Real size(Min(myVal/100.+1.e-6,myArrowSize+1.e-6));
340     Handle( Select3D_SensitiveBox ) box = new Select3D_SensitiveBox( own,
341                                                                     AttachmentPoint.X(),
342                                                                     AttachmentPoint.Y(),
343                                                                     AttachmentPoint.Z(),
344                                                                     AttachmentPoint.X()+size,
345                                                                     AttachmentPoint.Y()+size,
346                                                                     AttachmentPoint.Z()+size);
347     aSelection->Add(box);
348
349   // Arc of Ellipse
350     if(myIsAnArc)
351       {
352         
353         Standard_Real parEnd = ElCLib::Parameter ( myEllipse, myEndOfArrow );
354         if(!AIS::InDomain(myFirstPar, myLastPar, parEnd))
355           {
356             Standard_Real parStart, par;
357             if(AIS::DistanceFromApex (myEllipse, myEndOfArrow, myFirstPar) <
358                AIS::DistanceFromApex (myEllipse, myEndOfArrow, myLastPar))
359               par = myFirstPar;
360             else
361               par = myLastPar;
362             gp_Vec Vapex(center, ElCLib::Value( parEnd, myEllipse )) ;
363             gp_Vec Vpnt (center, ElCLib::Value( par, myEllipse )) ;
364             gp_Dir dir(Vpnt ^ Vapex);
365             if(myEllipse.Position().Direction().IsOpposite( dir, Precision::Angular())) {
366               parStart = parEnd;
367               parEnd   = par;
368             }
369             else 
370               parStart = par;
371             Handle(Geom_TrimmedCurve)TrimCurve;
372             if(myIsOffset)
373               {
374                 Handle(Geom_Curve) aCurve = myOffsetCurve;
375                 TrimCurve = new Geom_TrimmedCurve( aCurve,  parStart, parEnd );
376               }
377             else
378               {
379                 Handle(Geom_Ellipse) Ellipse = new Geom_Ellipse( myEllipse );
380                 TrimCurve = new Geom_TrimmedCurve( Ellipse,  parStart, parEnd );
381               }
382             Handle( Select3D_SensitiveCurve ) SensArc;
383             SensArc = new Select3D_SensitiveCurve( own, TrimCurve );
384             aSelection->Add( SensArc );
385           }
386     }
387
388 }