0024002: Overall code and build procedure refactoring -- automatic
[occt.git] / src / AIS / AIS_EllipseRadiusDimension.cxx
1 // Created on: 1998-01-22
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
18 #include <AIS.hxx>
19 #include <AIS_EllipseRadiusDimension.hxx>
20 #include <BRepAdaptor_Curve.hxx>
21 #include <BRepAdaptor_Surface.hxx>
22 #include <ElCLib.hxx>
23 #include <ElSLib.hxx>
24 #include <Geom_CylindricalSurface.hxx>
25 #include <Geom_Ellipse.hxx>
26 #include <Geom_Line.hxx>
27 #include <Geom_OffsetCurve.hxx>
28 #include <Geom_Plane.hxx>
29 #include <Geom_Surface.hxx>
30 #include <Geom_SurfaceOfLinearExtrusion.hxx>
31 #include <Geom_SurfaceOfRevolution.hxx>
32 #include <Geom_TrimmedCurve.hxx>
33 #include <GeomAPI.hxx>
34 #include <GeomAPI_ExtremaCurveCurve.hxx>
35 #include <gp_Ax1.hxx>
36 #include <gp_Dir.hxx>
37 #include <gp_Elips.hxx>
38 #include <gp_Lin.hxx>
39 #include <gp_Pln.hxx>
40 #include <gp_Pnt.hxx>
41 #include <gp_Vec.hxx>
42 #include <Precision.hxx>
43 #include <Standard_Type.hxx>
44 #include <TCollection_ExtendedString.hxx>
45 #include <TopExp_Explorer.hxx>
46 #include <TopoDS.hxx>
47 #include <TopoDS_Shape.hxx>
48
49 //=======================================================================
50 //function : AIS_EllipseRadiusDimension
51 //purpose  : 
52 //=======================================================================
53 AIS_EllipseRadiusDimension::AIS_EllipseRadiusDimension(const TopoDS_Shape& aShape, 
54                                                        const TCollection_ExtendedString& aText)
55 :AIS_Relation()
56 {
57   myFShape = aShape;
58   myText = aText;
59 //  ComputeGeometry( );
60 }
61
62 //=======================================================================
63 //function : ComputeGeometry
64 //purpose  : 
65 //=======================================================================
66
67 void AIS_EllipseRadiusDimension::ComputeGeometry()
68 {
69
70  switch (myFShape.ShapeType()) {
71   case TopAbs_FACE :
72     {
73       // compute one face case
74       ComputeFaceGeometry ();
75       break;
76     }
77   case TopAbs_EDGE:
78     {
79       ComputeEdgeGeometry ();
80       break;
81     }
82   default:
83     break;
84   }
85  while (myFirstPar > 2*M_PI) myFirstPar -= 2*M_PI;
86  while (myLastPar > 2*M_PI)  myLastPar  -= 2*M_PI;
87  while (myFirstPar < 0.0)  myFirstPar += 2*M_PI;
88  while (myLastPar  < 0.0)  myLastPar  += 2*M_PI;
89 }
90
91 //=======================================================================
92 //function : ComputeFaceGeometry
93 //purpose  : 
94 //=======================================================================
95
96 void AIS_EllipseRadiusDimension::ComputeFaceGeometry()
97 {
98
99   gp_Pln aPln;
100   Handle( Geom_Surface ) aBasisSurf;
101   AIS_KindOfSurface aSurfType;
102   Standard_Real Offset;
103   AIS::GetPlaneFromFace( TopoDS::Face(  myFShape),
104                                         aPln,
105                                         aBasisSurf,
106                                         aSurfType,
107                                         Offset ) ;
108
109   if ( aSurfType == AIS_KOS_Plane )
110     ComputePlanarFaceGeometry( );
111   else 
112     ComputeCylFaceGeometry( aSurfType, aBasisSurf, Offset );
113
114 }
115
116 //=======================================================================
117 //function : ComputeCylFaceGeometry
118 //purpose  : defines Ellipse and plane of dimension
119 //=======================================================================
120
121 void AIS_EllipseRadiusDimension::ComputeCylFaceGeometry(const AIS_KindOfSurface  aSurfType,
122                                                         const Handle( Geom_Surface )&  aBasisSurf,
123                                                         const Standard_Real Offset)
124 {
125
126   BRepAdaptor_Surface surf1(TopoDS::Face(myFShape));
127   Standard_Real vFirst, vLast;
128   vFirst = surf1.FirstVParameter();
129   vLast  = surf1.LastVParameter();
130   Standard_Real vMid = (vFirst + vLast)*0.5;
131   gp_Pln aPlane;
132   gp_Ax1 Axis;
133 //  Standard_Real Param;
134   if (aSurfType == AIS_KOS_Extrusion)
135     {
136       Axis.SetDirection((Handle( Geom_SurfaceOfLinearExtrusion )::DownCast( aBasisSurf ))
137                         ->Direction() );
138       Axis.SetLocation( gp_Pnt((Handle( Geom_SurfaceOfLinearExtrusion )::DownCast( aBasisSurf ))
139                                ->Direction().XYZ() ) );
140       
141       aPlane.SetAxis(Axis);
142       aPlane.SetLocation(myEllipse.Location());
143       myPlane = new Geom_Plane(aPlane);
144       
145       Handle(Geom_Curve) aCurve;
146       aCurve =   aBasisSurf->VIso(vMid);
147       if (aCurve->DynamicType() == STANDARD_TYPE(Geom_Ellipse)) 
148         {
149           myEllipse = Handle(Geom_Ellipse)::DownCast(aCurve)-> Elips();//gp_Elips
150           myIsAnArc = Standard_False;
151         }
152       else if (aCurve->DynamicType() == STANDARD_TYPE(Geom_TrimmedCurve)) {
153         Handle(Geom_TrimmedCurve) tCurve = Handle(Geom_TrimmedCurve)::DownCast(aCurve); 
154         aCurve = tCurve->BasisCurve();
155         myFirstPar = tCurve->FirstParameter();
156         myLastPar  = tCurve->LastParameter();
157         myIsAnArc = Standard_True;
158         if (aCurve->DynamicType() == STANDARD_TYPE(Geom_Ellipse)) 
159           {
160             myEllipse = Handle(Geom_Ellipse)::DownCast(aCurve)->Elips();//gp_Elips
161           }
162       }
163       else 
164         {
165           Standard_ConstructionError::Raise("AIS:: Not expected type of surface") ;
166             return;
167           }
168       
169 // Offset  
170
171       if(surf1.GetType() ==  GeomAbs_OffsetSurface)
172         {
173           if(Offset <0.0 && Abs(Offset) > myEllipse.MinorRadius ())
174             {
175               Standard_ConstructionError::Raise("AIS:: Absolute value of negative offset is larger than MinorRadius");
176                 return;
177               }
178           
179           myOffsetCurve = new Geom_OffsetCurve(new Geom_Ellipse(myEllipse), Offset, 
180                                                myPlane->Pln().Axis().Direction());
181           myOffset = Offset;
182           myIsOffset = Standard_True;
183           gp_Elips elips = myEllipse;
184           Standard_Real Val = Offset + elips.MajorRadius ();//simulation
185           myEllipse.SetMajorRadius (Val);
186           Val = Offset + elips.MinorRadius ();
187           myEllipse.SetMinorRadius (Val);
188         }
189       else 
190         myIsOffset = Standard_False;
191     }
192 }
193
194
195 //=======================================================================
196 //function : ComputePlanarFaceGeometry
197 //purpose  : 
198 //=======================================================================
199
200 void AIS_EllipseRadiusDimension::ComputePlanarFaceGeometry()
201 {
202
203   Standard_Boolean find = Standard_False;
204   gp_Pnt ptfirst,ptend;
205   TopExp_Explorer ExploEd( TopoDS::Face(myFShape), TopAbs_EDGE );
206   for ( ; ExploEd.More(); ExploEd.Next())
207     {
208       TopoDS_Edge curedge =  TopoDS::Edge( ExploEd.Current() );
209       Handle(Geom_Curve) curv;
210       Handle(Geom_Ellipse) ellips;
211       if (AIS::ComputeGeometry(curedge,curv,ptfirst,ptend)) 
212         { 
213           if (curv->DynamicType() == STANDARD_TYPE(Geom_Ellipse))
214             {
215               ellips = Handle(Geom_Ellipse)::DownCast(curv);
216               if ( !ellips.IsNull() ) {
217                 myEllipse = ellips->Elips();
218                 find = Standard_True;
219                 break;
220               }
221             }
222         }
223     }
224   if( !find )
225     {
226       Standard_ConstructionError::Raise("AIS:: Curve is not an ellipsee or is Null") ;
227         return;
228       }
229   
230   if ( !ptfirst.IsEqual(ptend, Precision::Confusion()) )
231     {
232       myIsAnArc = Standard_True;
233       myFirstPar = ElCLib::Parameter(myEllipse, ptfirst);
234       myLastPar  = ElCLib::Parameter(myEllipse, ptend); 
235     }
236   else
237     myIsAnArc = Standard_False;
238
239   BRepAdaptor_Surface surfAlgo (TopoDS::Face(myFShape));
240   myPlane  = new Geom_Plane( surfAlgo.Plane() );
241   
242 }
243
244  
245
246 //=======================================================================
247 //function : ComputeEdgeGeometry
248 //purpose  : 
249 //=======================================================================
250
251 void AIS_EllipseRadiusDimension::ComputeEdgeGeometry()
252 {
253   gp_Pnt ptfirst,ptend;
254   Handle(Geom_Curve) curv;
255   if (!AIS::ComputeGeometry(TopoDS::Edge(myFShape),curv,ptfirst,ptend)) return;
256   
257   Handle(Geom_Ellipse) elips = Handle(Geom_Ellipse)::DownCast(curv);
258   if ( elips.IsNull()) return;
259   
260   myEllipse =  elips->Elips();
261   gp_Pln aPlane;
262   aPlane.SetPosition(gp_Ax3(myEllipse.Position()));
263   myPlane  = new Geom_Plane(aPlane);
264   
265   
266   if ( ptfirst.IsEqual(ptend, Precision::Confusion()) ) {
267     myIsAnArc = Standard_False;
268   }
269   else {
270     myIsAnArc = Standard_True;
271     myFirstPar = ElCLib::Parameter(myEllipse, ptfirst);
272     myLastPar  = ElCLib::Parameter(myEllipse, ptend); 
273   }
274 }
275 //=======================================================================
276 //function : KindOfDimension
277 //purpose  : 
278 //=======================================================================
279  AIS_KindOfDimension AIS_EllipseRadiusDimension::KindOfDimension() const 
280 {
281   return AIS_KOD_ELLIPSERADIUS;
282 }
283
284 //=======================================================================
285 //function : IsMovable
286 //purpose  : 
287 //=======================================================================
288  Standard_Boolean AIS_EllipseRadiusDimension::IsMovable() const 
289 {
290   return Standard_True;
291 }