Salome HOME
Heal invalid shape after scaling, because different scaling along axes can produce...
[modules/geom.git] / src / GEOMImpl / GEOMImpl_CircleDriver.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_CircleDriver.hxx>
24 #include <GEOMImpl_ICircle.hxx>
25 #include <GEOMImpl_Types.hxx>
26 #include <GEOM_Function.hxx>
27
28 #include <BRepBuilderAPI_MakeEdge.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 <GC_MakeCircle.hxx>
38 #include <Geom_Circle.hxx>
39
40 #include <Standard_ConstructionError.hxx>
41 #include <Precision.hxx>
42 #include <gp_Pnt.hxx>
43 #include <gp_Vec.hxx>
44 #include <gp_Circ.hxx>
45
46 //=======================================================================
47 //function : GetID
48 //purpose  :
49 //======================================================================= 
50 const Standard_GUID& GEOMImpl_CircleDriver::GetID()
51 {
52   static Standard_GUID aCircleDriver("FF1BBB32-5D14-4df2-980B-3A668264EA16");
53   return aCircleDriver; 
54 }
55
56
57 //=======================================================================
58 //function : GEOMImpl_CircleDriver
59 //purpose  : 
60 //=======================================================================
61 GEOMImpl_CircleDriver::GEOMImpl_CircleDriver() 
62 {
63 }
64
65 //=======================================================================
66 //function : Execute
67 //purpose  :
68 //======================================================================= 
69 Standard_Integer GEOMImpl_CircleDriver::Execute(TFunction_Logbook& log) const
70 {
71   if (Label().IsNull()) return 0;    
72   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
73
74   GEOMImpl_ICircle aCI (aFunction);
75   Standard_Integer aType = aFunction->GetType();
76
77   TopoDS_Shape aShape;
78
79   if (aType == CIRCLE_PNT_VEC_R) {
80     // Center
81     gp_Pnt aP = gp::Origin();
82     Handle(GEOM_Function) aRefPoint = aCI.GetCenter();
83     if (!aRefPoint.IsNull()) {
84       TopoDS_Shape aShapePnt = aRefPoint->GetValue();
85       if (aShapePnt.ShapeType() != TopAbs_VERTEX) {
86         Standard_ConstructionError::Raise
87           ("Circle creation aborted: invalid center argument, must be a point");
88       }
89       aP = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt));
90     }
91     // Normal
92     gp_Vec aV = gp::DZ();
93     Handle(GEOM_Function) aRefVector = aCI.GetVector();
94     if (!aRefVector.IsNull()) {
95       TopoDS_Shape aShapeVec = aRefVector->GetValue();
96       if (aShapeVec.ShapeType() != TopAbs_EDGE) {
97         Standard_ConstructionError::Raise
98           ("Circle creation aborted: invalid vector argument, must be a vector or an edge");
99       }
100       TopoDS_Edge anE = TopoDS::Edge(aShapeVec);
101       TopoDS_Vertex V1, V2;
102       TopExp::Vertices(anE, V1, V2, Standard_True);
103       if (!V1.IsNull() && !V2.IsNull()) {
104         aV = gp_Vec(BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
105         if (aV.Magnitude() < gp::Resolution()) {
106           Standard_ConstructionError::Raise
107             ("Circle creation aborted: vector of zero length is given");
108         }
109       }
110     }
111     // Axes
112     gp_Ax2 anAxes (aP, aV);
113     // Circle
114     gp_Circ aCirc (anAxes, aCI.GetRadius());
115     aShape = BRepBuilderAPI_MakeEdge(aCirc).Edge();
116   }
117   else if (aType == CIRCLE_CENTER_TWO_PNT) {
118     Handle(GEOM_Function) aRefPoint1 = aCI.GetPoint1();
119     Handle(GEOM_Function) aRefPoint2 = aCI.GetPoint2();
120     Handle(GEOM_Function) aRefPoint3 = aCI.GetPoint3();
121     TopoDS_Shape aShapePnt1 = aRefPoint1->GetValue();
122     TopoDS_Shape aShapePnt2 = aRefPoint2->GetValue();
123     TopoDS_Shape aShapePnt3 = aRefPoint3->GetValue();
124     if (aShapePnt1.ShapeType() == TopAbs_VERTEX &&
125         aShapePnt2.ShapeType() == TopAbs_VERTEX &&
126         aShapePnt3.ShapeType() == TopAbs_VERTEX)
127     {
128       gp_Pnt aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt1));
129       gp_Pnt aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt2));
130       gp_Pnt aP3 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt3));
131
132       if (aP1.Distance(aP2) < gp::Resolution() ||
133           aP1.Distance(aP3) < gp::Resolution() ||
134           aP2.Distance(aP3) < gp::Resolution())
135         Standard_ConstructionError::Raise("Circle creation aborted: coincident points given");
136
137       if (gp_Vec(aP1, aP2).IsParallel(gp_Vec(aP1, aP3), Precision::Angular()))
138         Standard_ConstructionError::Raise("Circle creation aborted: points lay on one line");
139
140       double x, y, z, x1, y1, z1, x2, y2, z2, dx, dy, dz, dx2, dy2, dz2, dx3, dy3, dz3, aRadius;
141       //Calculations for Radius
142       x = aP1.X(); y = aP1.Y(); z = aP1.Z();
143       x1 = aP2.X(); y1 = aP2.Y(); z1 = aP2.Z();
144       dx = x1 - x;
145       dy = y1 - y;
146       dz = z1 - z;
147       aRadius = sqrt(dx*dx + dy*dy + dz*dz);
148       //Calculations for Plane Vector
149       x2 = aP3.X(); y2 = aP3.Y(); z2 = aP3.Z();
150       dx2 = x2 - x; dy2 = y2 - y; dz2 = z2 - z;
151       dx3 = ((dy*dz2) - (dy2*dz))/100;
152       dy3 = ((dx2*dz) - (dx*dz2))/100;
153       dz3 = ((dx*dy2) - (dx2*dy))/100;
154       //Make Plane Vector
155       gp_Dir aDir ( dx3, dy3, dz3 );
156       //Make Circle
157       gp_Ax2 anAxes (aP1, aDir);
158       gp_Circ aCirc (anAxes, aRadius);
159       aShape = BRepBuilderAPI_MakeEdge(aCirc).Edge();  
160     }
161   }
162   else if (aType == CIRCLE_THREE_PNT) {
163     Handle(GEOM_Function) aRefPoint1 = aCI.GetPoint1();
164     Handle(GEOM_Function) aRefPoint2 = aCI.GetPoint2();
165     Handle(GEOM_Function) aRefPoint3 = aCI.GetPoint3();
166     TopoDS_Shape aShapePnt1 = aRefPoint1->GetValue();
167     TopoDS_Shape aShapePnt2 = aRefPoint2->GetValue();
168     TopoDS_Shape aShapePnt3 = aRefPoint3->GetValue();
169     if (aShapePnt1.ShapeType() == TopAbs_VERTEX &&
170         aShapePnt2.ShapeType() == TopAbs_VERTEX &&
171         aShapePnt3.ShapeType() == TopAbs_VERTEX) {
172       gp_Pnt aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt1));
173       gp_Pnt aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt2));
174       gp_Pnt aP3 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt3));
175       if (aP1.Distance(aP2) < gp::Resolution() ||
176           aP1.Distance(aP3) < gp::Resolution() ||
177           aP2.Distance(aP3) < gp::Resolution())
178         Standard_ConstructionError::Raise("Circle creation aborted: coincident points given");
179       if (gp_Vec(aP1, aP2).IsParallel(gp_Vec(aP1, aP3), Precision::Angular()))
180         Standard_ConstructionError::Raise("Circle creation aborted: points lay on one line");
181       Handle(Geom_Circle) aCirc = GC_MakeCircle(aP1, aP2, aP3).Value();
182       aShape = BRepBuilderAPI_MakeEdge(aCirc).Edge();
183     }  
184   }
185   else {
186   }
187
188   if (aShape.IsNull()) return 0;
189
190   aFunction->SetValue(aShape);
191
192   log.SetTouched(Label()); 
193
194   return 1;    
195 }
196
197
198 //=======================================================================
199 //function :  GEOMImpl_CircleDriver_Type_
200 //purpose  :
201 //======================================================================= 
202 Standard_EXPORT Handle_Standard_Type& GEOMImpl_CircleDriver_Type_()
203 {
204
205   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
206   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
207   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
208   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
209   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
210   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
211  
212
213   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
214   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_CircleDriver",
215                                                          sizeof(GEOMImpl_CircleDriver),
216                                                          1,
217                                                          (Standard_Address)_Ancestors,
218                                                          (Standard_Address)NULL);
219
220   return _aType;
221 }
222
223 //=======================================================================
224 //function : DownCast
225 //purpose  :
226 //======================================================================= 
227 const Handle(GEOMImpl_CircleDriver) Handle(GEOMImpl_CircleDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
228 {
229   Handle(GEOMImpl_CircleDriver) _anOtherObject;
230
231   if (!AnObject.IsNull()) {
232      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_CircleDriver))) {
233        _anOtherObject = Handle(GEOMImpl_CircleDriver)((Handle(GEOMImpl_CircleDriver)&)AnObject);
234      }
235   }
236
237   return _anOtherObject ;
238 }