Salome HOME
- Patch for recent Debian distrib:
[modules/geom.git] / src / GEOMImpl / GEOMImpl_TorusDriver.cxx
1 //  Copyright (C) 2007-2008  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.
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 #include <Standard_Stream.hxx>
23
24 #include <GEOMImpl_TorusDriver.hxx>
25 #include <GEOMImpl_ITorus.hxx>
26 #include <GEOMImpl_Types.hxx>
27 #include <GEOM_Function.hxx>
28
29 #include <BRepPrimAPI_MakeTorus.hxx>
30 #include <BRep_Tool.hxx>
31 #include <TopoDS.hxx>
32 #include <TopoDS_Shape.hxx>
33 #include <TopoDS_Edge.hxx>
34 #include <TopoDS_Vertex.hxx>
35 #include <TopAbs.hxx>
36 #include <TopExp.hxx>
37
38 #include <gp_Pnt.hxx>
39 #include <Precision.hxx>
40 #include <StdFail_NotDone.hxx>
41 #include <Standard_TypeMismatch.hxx>
42 #include <Standard_ConstructionError.hxx>
43
44 //=======================================================================
45 //function : GetID
46 //purpose  :
47 //======================================================================= 
48 const Standard_GUID& GEOMImpl_TorusDriver::GetID()
49 {
50   static Standard_GUID aTorusDriver("FF1BBB12-5D14-4df2-980B-3A668264EA16");
51   return aTorusDriver; 
52 }
53
54
55 //=======================================================================
56 //function : GEOMImpl_TorusDriver
57 //purpose  : 
58 //=======================================================================
59 GEOMImpl_TorusDriver::GEOMImpl_TorusDriver() 
60 {
61 }
62
63 //=======================================================================
64 //function : Execute
65 //purpose  :
66 //======================================================================= 
67 Standard_Integer GEOMImpl_TorusDriver::Execute(TFunction_Logbook& log) const
68 {
69   if (Label().IsNull()) return 0;    
70   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
71
72   GEOMImpl_ITorus aCI (aFunction);
73   Standard_Integer aType = aFunction->GetType();
74
75   TopoDS_Shape aShape;
76
77   if (aType == TORUS_RR) {
78     aShape = BRepPrimAPI_MakeTorus(aCI.GetRMajor(), aCI.GetRMinor()).Shape();
79
80   } else if (aType == TORUS_PNT_VEC_RR) {
81     Handle(GEOM_Function) aRefPoint  = aCI.GetCenter();
82     Handle(GEOM_Function) aRefVector = aCI.GetVector();
83     TopoDS_Shape aShapePnt = aRefPoint->GetValue();
84     TopoDS_Shape aShapeVec = aRefVector->GetValue();
85     if (aShapePnt.ShapeType() != TopAbs_VERTEX) {
86       Standard_TypeMismatch::Raise("Torus Center must be a vertex");
87     }
88     if (aShapeVec.ShapeType() != TopAbs_EDGE) {
89       Standard_TypeMismatch::Raise("Torus Axis must be an edge");
90     }
91
92     gp_Pnt aP = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt));
93     TopoDS_Edge anE = TopoDS::Edge(aShapeVec);
94     TopoDS_Vertex V1, V2;
95     TopExp::Vertices(anE, V1, V2, Standard_True);
96     if (V1.IsNull() || V2.IsNull()) {
97       Standard_ConstructionError::Raise("Bad edge for the Torus Axis given");
98     }
99
100     gp_Vec aV (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
101     if (aV.Magnitude() < Precision::Confusion()) {
102       Standard_ConstructionError::Raise
103         ("End vertices of edge, defining the Torus Axis, are too close");
104     }
105
106     gp_Ax2 anAxes (aP, aV);
107     BRepPrimAPI_MakeTorus MT (anAxes, aCI.GetRMajor(), aCI.GetRMinor());
108     if (!MT.IsDone()) MT.Build();
109     if (!MT.IsDone()) StdFail_NotDone::Raise("Torus construction algorithm has failed");
110     aShape = MT.Shape();
111   } else {
112   }
113
114   if (aShape.IsNull()) return 0;
115   aFunction->SetValue(aShape);
116
117   log.SetTouched(Label()); 
118
119   return 1;    
120 }
121
122
123 //=======================================================================
124 //function :  GEOMImpl_TorusDriver_Type_
125 //purpose  :
126 //======================================================================= 
127 Standard_EXPORT Handle_Standard_Type& GEOMImpl_TorusDriver_Type_()
128 {
129
130   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
131   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
132   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
133   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
134   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
135   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
136  
137
138   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
139   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_TorusDriver",
140                                                          sizeof(GEOMImpl_TorusDriver),
141                                                          1,
142                                                          (Standard_Address)_Ancestors,
143                                                          (Standard_Address)NULL);
144
145   return _aType;
146 }
147
148 //=======================================================================
149 //function : DownCast
150 //purpose  :
151 //======================================================================= 
152 const Handle(GEOMImpl_TorusDriver) Handle(GEOMImpl_TorusDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
153 {
154   Handle(GEOMImpl_TorusDriver) _anOtherObject;
155
156   if (!AnObject.IsNull()) {
157      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_TorusDriver))) {
158        _anOtherObject = Handle(GEOMImpl_TorusDriver)((Handle(GEOMImpl_TorusDriver)&)AnObject);
159      }
160   }
161
162   return _anOtherObject ;
163 }