Salome HOME
c365fcfb5f59f90241cca3ad6c27393af2fbc1b3
[modules/geom.git] / src / GEOMImpl / GEOMImpl_TorusDriver.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_TorusDriver.hxx>
26 #include <GEOMImpl_ITorus.hxx>
27 #include <GEOMImpl_Types.hxx>
28 #include <GEOM_Function.hxx>
29
30 #include <BRepPrimAPI_MakeTorus.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 <Precision.hxx>
41 #include <StdFail_NotDone.hxx>
42 #include <Standard_TypeMismatch.hxx>
43 #include <Standard_ConstructionError.hxx>
44
45 //=======================================================================
46 //function : GetID
47 //purpose  :
48 //======================================================================= 
49 const Standard_GUID& GEOMImpl_TorusDriver::GetID()
50 {
51   static Standard_GUID aTorusDriver("FF1BBB12-5D14-4df2-980B-3A668264EA16");
52   return aTorusDriver; 
53 }
54
55
56 //=======================================================================
57 //function : GEOMImpl_TorusDriver
58 //purpose  : 
59 //=======================================================================
60 GEOMImpl_TorusDriver::GEOMImpl_TorusDriver() 
61 {
62 }
63
64 //=======================================================================
65 //function : Execute
66 //purpose  :
67 //======================================================================= 
68 Standard_Integer GEOMImpl_TorusDriver::Execute(Handle(TFunction_Logbook)& log) const
69 {
70   if (Label().IsNull()) return 0;    
71   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
72
73   GEOMImpl_ITorus aCI (aFunction);
74   Standard_Integer aType = aFunction->GetType();
75
76   TopoDS_Shape aShape;
77
78   if (aType == TORUS_RR) {
79     aShape = BRepPrimAPI_MakeTorus(aCI.GetRMajor(), aCI.GetRMinor()).Shape();
80
81   } else if (aType == TORUS_PNT_VEC_RR) {
82     Handle(GEOM_Function) aRefPoint  = aCI.GetCenter();
83     Handle(GEOM_Function) aRefVector = aCI.GetVector();
84     TopoDS_Shape aShapePnt = aRefPoint->GetValue();
85     TopoDS_Shape aShapeVec = aRefVector->GetValue();
86     if (aShapePnt.ShapeType() != TopAbs_VERTEX) {
87       Standard_TypeMismatch::Raise("Torus Center must be a vertex");
88     }
89     if (aShapeVec.ShapeType() != TopAbs_EDGE) {
90       Standard_TypeMismatch::Raise("Torus Axis must be an edge");
91     }
92
93     gp_Pnt aP = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt));
94     TopoDS_Edge anE = TopoDS::Edge(aShapeVec);
95     TopoDS_Vertex V1, V2;
96     TopExp::Vertices(anE, V1, V2, Standard_True);
97     if (V1.IsNull() || V2.IsNull()) {
98       Standard_ConstructionError::Raise("Bad edge for the Torus Axis given");
99     }
100
101     gp_Vec aV (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
102     if (aV.Magnitude() < Precision::Confusion()) {
103       Standard_ConstructionError::Raise
104         ("End vertices of edge, defining the Torus Axis, are too close");
105     }
106
107     gp_Ax2 anAxes (aP, aV);
108     BRepPrimAPI_MakeTorus MT (anAxes, aCI.GetRMajor(), aCI.GetRMinor());
109     if (!MT.IsDone()) MT.Build();
110     if (!MT.IsDone()) StdFail_NotDone::Raise("Torus construction algorithm has failed");
111     aShape = MT.Shape();
112   } else {
113   }
114
115   if (aShape.IsNull()) return 0;
116   aFunction->SetValue(aShape);
117
118   log->SetTouched(Label());
119
120   return 1;    
121 }
122
123 //================================================================================
124 /*!
125  * \brief Returns a name of creation operation and names and values of creation parameters
126  */
127 //================================================================================
128
129 bool GEOMImpl_TorusDriver::
130 GetCreationInformation(std::string&             theOperationName,
131                        std::vector<GEOM_Param>& theParams)
132 {
133   if (Label().IsNull()) return 0;
134   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
135
136   GEOMImpl_ITorus aCI( function );
137   Standard_Integer aType = function->GetType();
138
139   theOperationName = "TORUS";
140
141   switch ( aType ) {
142   case TORUS_RR:
143     AddParam( theParams, "Radius 1", aCI.GetRMajor() );
144     AddParam( theParams, "Radius 2", aCI.GetRMinor() );
145     break;
146   case TORUS_PNT_VEC_RR:
147     AddParam( theParams, "Base Point", aCI.GetCenter() );
148     AddParam( theParams, "Vector", aCI.GetVector() );
149     AddParam( theParams, "Radius 1", aCI.GetRMajor() );
150     AddParam( theParams, "Radius 2", aCI.GetRMinor() );
151     break;
152   default:
153     return false;
154   }
155
156   return true;
157 }
158
159 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_TorusDriver,GEOM_BaseDriver)