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