Salome HOME
PAL12608: Add possibility to check geometry of a shape.
[modules/geom.git] / src / GEOMImpl / GEOMImpl_TorusDriver.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20
21 #include <Standard_Stream.hxx>
22
23 #include <GEOMImpl_TorusDriver.hxx>
24 #include <GEOMImpl_ITorus.hxx>
25 #include <GEOMImpl_Types.hxx>
26 #include <GEOM_Function.hxx>
27
28 #include <BRepPrimAPI_MakeTorus.hxx>
29 #include <BRep_Tool.hxx>
30 #include <TopoDS.hxx>
31 #include <TopoDS_Shape.hxx>
32 #include <TopoDS_Edge.hxx>
33 #include <TopoDS_Vertex.hxx>
34 #include <TopAbs.hxx>
35 #include <TopExp.hxx>
36
37 #include <gp_Pnt.hxx>
38 #include <Precision.hxx>
39 #include <StdFail_NotDone.hxx>
40 #include <Standard_TypeMismatch.hxx>
41 #include <Standard_ConstructionError.hxx>
42
43 //=======================================================================
44 //function : GetID
45 //purpose  :
46 //======================================================================= 
47 const Standard_GUID& GEOMImpl_TorusDriver::GetID()
48 {
49   static Standard_GUID aTorusDriver("FF1BBB12-5D14-4df2-980B-3A668264EA16");
50   return aTorusDriver; 
51 }
52
53
54 //=======================================================================
55 //function : GEOMImpl_TorusDriver
56 //purpose  : 
57 //=======================================================================
58 GEOMImpl_TorusDriver::GEOMImpl_TorusDriver() 
59 {
60 }
61
62 //=======================================================================
63 //function : Execute
64 //purpose  :
65 //======================================================================= 
66 Standard_Integer GEOMImpl_TorusDriver::Execute(TFunction_Logbook& log) const
67 {
68   if (Label().IsNull()) return 0;    
69   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
70
71   GEOMImpl_ITorus aCI (aFunction);
72   Standard_Integer aType = aFunction->GetType();
73
74   TopoDS_Shape aShape;
75
76   if (aType == TORUS_RR) {
77     aShape = BRepPrimAPI_MakeTorus(aCI.GetRMajor(), aCI.GetRMinor()).Shape();
78
79   } else if (aType == TORUS_PNT_VEC_RR) {
80     Handle(GEOM_Function) aRefPoint  = aCI.GetCenter();
81     Handle(GEOM_Function) aRefVector = aCI.GetVector();
82     TopoDS_Shape aShapePnt = aRefPoint->GetValue();
83     TopoDS_Shape aShapeVec = aRefVector->GetValue();
84     if (aShapePnt.ShapeType() != TopAbs_VERTEX) {
85       Standard_TypeMismatch::Raise("Torus Center must be a vertex");
86     }
87     if (aShapeVec.ShapeType() != TopAbs_EDGE) {
88       Standard_TypeMismatch::Raise("Torus Axis must be an edge");
89     }
90
91     gp_Pnt aP = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt));
92     TopoDS_Edge anE = TopoDS::Edge(aShapeVec);
93     TopoDS_Vertex V1, V2;
94     TopExp::Vertices(anE, V1, V2, Standard_True);
95     if (V1.IsNull() || V2.IsNull()) {
96       Standard_ConstructionError::Raise("Bad edge for the Torus Axis given");
97     }
98
99     gp_Vec aV (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
100     if (aV.Magnitude() < Precision::Confusion()) {
101       Standard_ConstructionError::Raise
102         ("End vertices of edge, defining the Torus Axis, are too close");
103     }
104
105     gp_Ax2 anAxes (aP, aV);
106     BRepPrimAPI_MakeTorus MT (anAxes, aCI.GetRMajor(), aCI.GetRMinor());
107     if (!MT.IsDone()) MT.Build();
108     if (!MT.IsDone()) StdFail_NotDone::Raise("Torus construction algorithm has failed");
109     aShape = MT.Shape();
110   } else {
111   }
112
113   if (aShape.IsNull()) return 0;
114   aFunction->SetValue(aShape);
115
116   log.SetTouched(Label()); 
117
118   return 1;    
119 }
120
121
122 //=======================================================================
123 //function :  GEOMImpl_TorusDriver_Type_
124 //purpose  :
125 //======================================================================= 
126 Standard_EXPORT Handle_Standard_Type& GEOMImpl_TorusDriver_Type_()
127 {
128
129   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
130   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
131   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
132   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
133   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
134   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
135  
136
137   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
138   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_TorusDriver",
139                                                          sizeof(GEOMImpl_TorusDriver),
140                                                          1,
141                                                          (Standard_Address)_Ancestors,
142                                                          (Standard_Address)NULL);
143
144   return _aType;
145 }
146
147 //=======================================================================
148 //function : DownCast
149 //purpose  :
150 //======================================================================= 
151 const Handle(GEOMImpl_TorusDriver) Handle(GEOMImpl_TorusDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
152 {
153   Handle(GEOMImpl_TorusDriver) _anOtherObject;
154
155   if (!AnObject.IsNull()) {
156      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_TorusDriver))) {
157        _anOtherObject = Handle(GEOMImpl_TorusDriver)((Handle(GEOMImpl_TorusDriver)&)AnObject);
158      }
159   }
160
161   return _anOtherObject ;
162 }