Salome HOME
da705722eff0ce20a62f7c2c75dbffbc24a0fdde
[modules/geom.git] / src / GEOMImpl / GEOMImpl_EllipseDriver.cxx
1 // Copyright (C) 2007-2023  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include <Standard_Stream.hxx>
24
25 #include <GEOMImpl_EllipseDriver.hxx>
26 #include <GEOMImpl_IEllipse.hxx>
27 #include <GEOMImpl_Types.hxx>
28 #include <GEOM_Function.hxx>
29
30 #include <BRepBuilderAPI_MakeEdge.hxx>
31 #include <BRep_Tool.hxx>
32 #include <TopoDS.hxx>
33 #include <TopoDS_Shape.hxx>
34 #include <TopoDS_Edge.hxx>
35 #include <TopoDS_Vertex.hxx>
36 #include <TopAbs.hxx>
37 #include <TopExp.hxx>
38
39 #include <gp_Pnt.hxx>
40 #include <gp_Elips.hxx>
41
42 //=======================================================================
43 //function : GetID
44 //purpose  :
45 //======================================================================= 
46 const Standard_GUID& GEOMImpl_EllipseDriver::GetID()
47 {
48   static Standard_GUID aEllipseDriver("FF1BBB34-5D14-4df2-980B-3A668264EA16");
49   return aEllipseDriver; 
50 }
51
52
53 //=======================================================================
54 //function : GEOMImpl_EllipseDriver
55 //purpose  : 
56 //=======================================================================
57 GEOMImpl_EllipseDriver::GEOMImpl_EllipseDriver() 
58 {
59 }
60
61 //=======================================================================
62 //function : Execute
63 //purpose  :
64 //======================================================================= 
65 Standard_Integer GEOMImpl_EllipseDriver::Execute(Handle(TFunction_Logbook)& log) const
66 {
67   if (Label().IsNull()) return 0;    
68   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
69
70   GEOMImpl_IEllipse aCI (aFunction);
71   Standard_Integer aType = aFunction->GetType();
72
73   TopoDS_Shape aShape;
74
75   if (aType == ELLIPSE_PNT_VEC_RR) {
76     // Center
77     gp_Pnt aP = gp::Origin();
78     Handle(GEOM_Function) aRefPoint = aCI.GetCenter();
79     if (!aRefPoint.IsNull()) {
80       TopoDS_Shape aShapePnt = aRefPoint->GetValue();
81       if (aShapePnt.ShapeType() != TopAbs_VERTEX) {
82         Standard_ConstructionError::Raise
83           ("Ellipse creation aborted: invalid center argument, must be a point");
84       }
85       aP = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt));
86     }
87     // Normal
88     gp_Vec aV = gp::DZ();
89     Handle(GEOM_Function) aRefVector = aCI.GetVector();
90     if (!aRefVector.IsNull()) {
91       TopoDS_Shape aShapeVec = aRefVector->GetValue();
92       if (aShapeVec.ShapeType() != TopAbs_EDGE) {
93         Standard_ConstructionError::Raise
94           ("Ellipse creation aborted: invalid normal vector argument, must be a vector or an edge");
95       }
96       TopoDS_Edge anE = TopoDS::Edge(aShapeVec);
97       TopoDS_Vertex V1, V2;
98       TopExp::Vertices(anE, V1, V2, Standard_True);
99       if (!V1.IsNull() && !V2.IsNull()) {
100         aV = gp_Vec(BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
101         if (aV.Magnitude() < gp::Resolution()) {
102           Standard_ConstructionError::Raise
103             ("Ellipse creation aborted: normal vector of zero length is given");
104         }
105       }
106     }
107
108     // Axes
109     gp_Ax2 anAxes (aP, aV);
110
111     // Main Axis vector
112     Handle(GEOM_Function) aRefVectorMaj = aCI.GetVectorMajor();
113     if (!aRefVectorMaj.IsNull()) {
114       TopoDS_Shape aShapeVec = aRefVectorMaj->GetValue();
115       if (aShapeVec.ShapeType() != TopAbs_EDGE) {
116         Standard_ConstructionError::Raise
117           ("Ellipse creation aborted: invalid major axis vector argument, must be a vector or an edge");
118       }
119       TopoDS_Edge anE = TopoDS::Edge(aShapeVec);
120       TopoDS_Vertex V1, V2;
121       TopExp::Vertices(anE, V1, V2, Standard_True);
122       if (!V1.IsNull() && !V2.IsNull()) {
123         gp_Vec aVM (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
124         if (aVM.Magnitude() < gp::Resolution()) {
125           Standard_ConstructionError::Raise
126             ("Ellipse creation aborted: major axis vector of zero length is given");
127         }
128         if (aV.IsParallel(aVM, Precision::Angular())) {
129           Standard_ConstructionError::Raise
130             ("Ellipse creation aborted: normal and major axis vectors are parallel");
131         }
132         // Axes defined with main axis vector
133         anAxes  = gp_Ax2 (aP, aV, aVM);
134       }
135     }
136     // Radiuses
137     double radiusMaj = aCI.GetRMajor();
138     double radiusMin = aCI.GetRMinor();
139     if ( radiusMaj < radiusMin )
140       Standard_ConstructionError::Raise
141         ("Ellipse creation aborted: a major radius is less that a minor one");
142     if ( radiusMin < 0.0 )
143       Standard_ConstructionError::Raise
144         ("Ellipse creation aborted: raduis must be positive");
145
146     // Ellipse
147     gp_Elips anEll (anAxes, radiusMaj, radiusMin);
148     aShape = BRepBuilderAPI_MakeEdge(anEll).Edge();
149   }
150   else {
151   }
152
153   if (aShape.IsNull()) return 0;
154
155   aFunction->SetValue(aShape);
156
157   log->SetTouched(Label());
158
159   return 1;    
160 }
161
162 //================================================================================
163 /*!
164  * \brief Returns a name of creation operation and names and values of creation parameters
165  */
166 //================================================================================
167
168 bool GEOMImpl_EllipseDriver::
169 GetCreationInformation(std::string&             theOperationName,
170                        std::vector<GEOM_Param>& theParams)
171 {
172   if (Label().IsNull()) return 0;
173   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
174
175   GEOMImpl_IEllipse aCI( function );
176   Standard_Integer aType = function->GetType();
177
178   theOperationName = "ELLIPSE";
179
180   switch ( aType ) {
181   case ELLIPSE_PNT_VEC_RR:
182     AddParam( theParams, "Center", aCI.GetCenter(), "Origin" );
183     AddParam( theParams, "Vector", aCI.GetVector(), "Z axis" );
184     AddParam( theParams, "Major Axis", aCI.GetVectorMajor(), "X axis");
185     AddParam( theParams, "Major radius", aCI.GetRMajor() );
186     AddParam( theParams, "Minor radius", aCI.GetRMinor() );
187     break;
188   default:
189     return false;
190   }
191   
192   return true;
193 }
194
195 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_EllipseDriver,GEOM_BaseDriver)