Salome HOME
Heal invalid shape after scaling, because different scaling along axes can produce...
[modules/geom.git] / src / GEOMImpl / GEOMImpl_DiskDriver.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_DiskDriver.hxx>
24 #include <GEOMImpl_IDisk.hxx>
25 #include <GEOMImpl_Types.hxx>
26 #include <GEOM_Function.hxx>
27
28 #include <BRepBuilderAPI_MakeEdge.hxx>
29 #include <BRepBuilderAPI_MakeWire.hxx>
30 #include <BRepBuilderAPI_MakeFace.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 <TopoDS_Wire.hxx>
37 #include <TopAbs.hxx>
38 #include <TopExp.hxx>
39
40 #include <GC_MakeCircle.hxx>
41 #include <Geom_Circle.hxx>
42
43 #include <Standard_ConstructionError.hxx>
44 #include <Precision.hxx>
45 #include <gp_Pnt.hxx>
46 #include <gp_Vec.hxx>
47 #include <gp_Circ.hxx>
48
49 //=======================================================================
50 //function : GetID
51 //purpose  :
52 //======================================================================= 
53 const Standard_GUID& GEOMImpl_DiskDriver::GetID()
54 {
55   static Standard_GUID aDiskDriver("C1FEEF9D-1C6D-41ce-9507-F10D75430CE1");
56   return aDiskDriver; 
57 }
58
59
60 //=======================================================================
61 //function : GEOMImpl_DiskDriver
62 //purpose  : 
63 //=======================================================================
64 GEOMImpl_DiskDriver::GEOMImpl_DiskDriver() 
65 {
66 }
67
68 //=======================================================================
69 //function : Execute
70 //purpose  :
71 //======================================================================= 
72 Standard_Integer GEOMImpl_DiskDriver::Execute(TFunction_Logbook& log) const
73 {
74   if (Label().IsNull()) return 0;    
75   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
76
77   GEOMImpl_IDisk aCI (aFunction);
78   Standard_Integer aType = aFunction->GetType();
79
80   TopoDS_Shape aShape;
81
82   if (aType == DISK_PNT_VEC_R) {
83     Handle(GEOM_Function) aRefPoint  = aCI.GetCenter();
84     Handle(GEOM_Function) aRefVector = aCI.GetVector();
85     TopoDS_Shape aShapePnt = aRefPoint->GetValue();
86     TopoDS_Shape aShapeVec = aRefVector->GetValue();
87     if (aShapePnt.ShapeType() == TopAbs_VERTEX &&
88         aShapeVec.ShapeType() == TopAbs_EDGE) {
89       gp_Pnt aP = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt));
90       TopoDS_Edge anE = TopoDS::Edge(aShapeVec);
91       TopoDS_Vertex V1, V2;
92       TopExp::Vertices(anE, V1, V2, Standard_True);
93       if (!V1.IsNull() && !V2.IsNull()) {
94         gp_Vec aV (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
95         gp_Ax2 anAxes (aP, aV);
96         gp_Circ aCirc (anAxes, aCI.GetRadius());
97         TopoDS_Shape aCircle = BRepBuilderAPI_MakeEdge(aCirc).Edge();
98         BRepBuilderAPI_MakeWire MW;
99         MW.Add(TopoDS::Edge(aCircle));
100         BRepBuilderAPI_MakeFace MF (MW, Standard_False);
101         aShape = MF.Shape();
102       }
103     }
104   }
105   else if (aType == DISK_THREE_PNT) {
106     Handle(GEOM_Function) aRefPoint1 = aCI.GetPoint1();
107     Handle(GEOM_Function) aRefPoint2 = aCI.GetPoint2();
108     Handle(GEOM_Function) aRefPoint3 = aCI.GetPoint3();
109     TopoDS_Shape aShapePnt1 = aRefPoint1->GetValue();
110     TopoDS_Shape aShapePnt2 = aRefPoint2->GetValue();
111     TopoDS_Shape aShapePnt3 = aRefPoint3->GetValue();
112     if (aShapePnt1.ShapeType() == TopAbs_VERTEX &&
113         aShapePnt2.ShapeType() == TopAbs_VERTEX &&
114         aShapePnt3.ShapeType() == TopAbs_VERTEX) {
115       gp_Pnt aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt1));
116       gp_Pnt aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt2));
117       gp_Pnt aP3 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt3));
118       if (aP1.Distance(aP2) < gp::Resolution() ||
119           aP1.Distance(aP3) < gp::Resolution() ||
120           aP2.Distance(aP3) < gp::Resolution())
121         Standard_ConstructionError::Raise("Disk creation aborted: coincident points given");
122       if (gp_Vec(aP1, aP2).IsParallel(gp_Vec(aP1, aP3), Precision::Angular()))
123         Standard_ConstructionError::Raise("Disk creation aborted: points lay on one line");
124       Handle(Geom_Circle) aCirc = GC_MakeCircle(aP1, aP2, aP3).Value();
125       TopoDS_Shape aCircle = BRepBuilderAPI_MakeEdge(aCirc).Edge();
126       BRepBuilderAPI_MakeWire MW;
127       MW.Add(TopoDS::Edge(aCircle));
128       BRepBuilderAPI_MakeFace MF (MW, Standard_False);
129       aShape = MF.Shape();
130     }  
131   }
132   else if (aType == DISK_R) {
133     int anOrient = aCI.GetOrientation();
134     gp_Pnt aP = gp::Origin();
135     gp_Vec aV;
136     if (anOrient == 1)
137       aV = gp::DZ();
138     else if (anOrient == 2)
139       aV = gp::DX();
140     else if (anOrient == 3)
141       aV = gp::DY();
142
143     gp_Ax2 anAxes (aP, aV);
144     gp_Circ aCirc (anAxes, aCI.GetRadius());
145     TopoDS_Shape aCircle = BRepBuilderAPI_MakeEdge(aCirc).Edge();
146     BRepBuilderAPI_MakeWire MW;
147     MW.Add(TopoDS::Edge(aCircle));
148     BRepBuilderAPI_MakeFace MF (MW, Standard_False);
149     aShape = MF.Shape();
150   }
151    else {
152   }
153
154   if (aShape.IsNull()) return 0;
155
156   aFunction->SetValue(aShape);
157
158   log.SetTouched(Label()); 
159
160   return 1;    
161 }
162
163
164 //=======================================================================
165 //function :  GEOMImpl_DiskDriver_Type_
166 //purpose  :
167 //======================================================================= 
168 Standard_EXPORT Handle_Standard_Type& GEOMImpl_DiskDriver_Type_()
169 {
170
171   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
172   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
173   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
174   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
175   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
176   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
177  
178
179   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
180   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_DiskDriver",
181                                                          sizeof(GEOMImpl_DiskDriver),
182                                                          1,
183                                                          (Standard_Address)_Ancestors,
184                                                          (Standard_Address)NULL);
185
186   return _aType;
187 }
188
189 //=======================================================================
190 //function : DownCast
191 //purpose  :
192 //======================================================================= 
193 const Handle(GEOMImpl_DiskDriver) Handle(GEOMImpl_DiskDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
194 {
195   Handle(GEOMImpl_DiskDriver) _anOtherObject;
196
197   if (!AnObject.IsNull()) {
198      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_DiskDriver))) {
199        _anOtherObject = Handle(GEOMImpl_DiskDriver)((Handle(GEOMImpl_DiskDriver)&)AnObject);
200      }
201   }
202
203   return _anOtherObject ;
204 }