Salome HOME
036225d036d14dd2ac114fa22d9db87c4ce467d1
[modules/geom.git] / src / GEOMImpl / GEOMImpl_CircleDriver.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 <GEOMImpl_CircleDriver.hxx>
24
25 #include <GEOMImpl_ICircle.hxx>
26 #include <GEOMImpl_Types.hxx>
27
28 #include <GEOM_Function.hxx>
29
30 #include <GEOMUtils.hxx>
31
32 #include <BRepBuilderAPI_MakeEdge.hxx>
33 #include <BRep_Tool.hxx>
34
35 #include <TopoDS.hxx>
36 #include <TopoDS_Shape.hxx>
37 #include <TopoDS_Edge.hxx>
38 #include <TopoDS_Vertex.hxx>
39 #include <TopAbs.hxx>
40 #include <TopExp.hxx>
41
42 #include <GC_MakeCircle.hxx>
43 #include <Geom_Circle.hxx>
44
45 #include <Standard_ConstructionError.hxx>
46 #include <Precision.hxx>
47 #include <gp_Pnt.hxx>
48 #include <gp_Vec.hxx>
49 #include <gp_Circ.hxx>
50
51 //=======================================================================
52 //function : GetID
53 //purpose  :
54 //=======================================================================
55 const Standard_GUID& GEOMImpl_CircleDriver::GetID()
56 {
57   static Standard_GUID aCircleDriver("FF1BBB32-5D14-4df2-980B-3A668264EA16");
58   return aCircleDriver;
59 }
60
61
62 //=======================================================================
63 //function : GEOMImpl_CircleDriver
64 //purpose  :
65 //=======================================================================
66 GEOMImpl_CircleDriver::GEOMImpl_CircleDriver()
67 {
68 }
69
70 //=======================================================================
71 //function : Execute
72 //purpose  :
73 //=======================================================================
74 Standard_Integer GEOMImpl_CircleDriver::Execute(Handle(TFunction_Logbook)& log) const
75 {
76   if (Label().IsNull()) return 0;
77   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
78
79   GEOMImpl_ICircle aCI (aFunction);
80   Standard_Integer aType = aFunction->GetType();
81
82   TopoDS_Shape aShape;
83
84   if (aType == CIRCLE_PNT_VEC_R) {
85     // Center
86     gp_Pnt aP = gp::Origin();
87     Handle(GEOM_Function) aRefPoint = aCI.GetCenter();
88     if (!aRefPoint.IsNull()) {
89       TopoDS_Shape aShapePnt = aRefPoint->GetValue();
90       if (aShapePnt.ShapeType() != TopAbs_VERTEX) {
91         Standard_ConstructionError::Raise
92           ("Circle creation aborted: invalid center argument, must be a point");
93       }
94       aP = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt));
95     }
96     // Normal
97     gp_Vec aV = gp::DZ();
98     Handle(GEOM_Function) aRefVector = aCI.GetVector();
99     if (!aRefVector.IsNull()) {
100       TopoDS_Shape aShapeVec = aRefVector->GetValue();
101       // take orientation of edge into account to avoid regressions, as it was implemented so
102       aV = GEOMUtils::GetVector(aShapeVec, Standard_True);
103     }
104     // Axes
105     gp_Ax2 anAxes (aP, aV);
106     // Radius
107     double anR = aCI.GetRadius();
108     char aMsg[] = "Circle creation aborted: radius value less than 1e-07 is not acceptable";
109     if (anR < Precision::Confusion())
110       Standard_ConstructionError::Raise(aMsg);
111     // Circle
112     gp_Circ aCirc (anAxes, anR);
113     aShape = BRepBuilderAPI_MakeEdge(aCirc).Edge();
114   }
115   else if (aType == CIRCLE_CENTER_TWO_PNT) {
116     Handle(GEOM_Function) aRefPoint1 = aCI.GetPoint1();
117     Handle(GEOM_Function) aRefPoint2 = aCI.GetPoint2();
118     Handle(GEOM_Function) aRefPoint3 = aCI.GetPoint3();
119     TopoDS_Shape aShapePnt1 = aRefPoint1->GetValue();
120     TopoDS_Shape aShapePnt2 = aRefPoint2->GetValue();
121     TopoDS_Shape aShapePnt3 = aRefPoint3->GetValue();
122     if (aShapePnt1.ShapeType() == TopAbs_VERTEX &&
123         aShapePnt2.ShapeType() == TopAbs_VERTEX &&
124         aShapePnt3.ShapeType() == TopAbs_VERTEX)
125     {
126       gp_Pnt aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt1));
127       gp_Pnt aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt2));
128       gp_Pnt aP3 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt3));
129
130       if (aP1.Distance(aP2) < gp::Resolution() ||
131           aP1.Distance(aP3) < gp::Resolution() ||
132           aP2.Distance(aP3) < gp::Resolution())
133         Standard_ConstructionError::Raise("Circle creation aborted: coincident points given");
134
135       if (gp_Vec(aP1, aP2).IsParallel(gp_Vec(aP1, aP3), Precision::Angular()))
136         Standard_ConstructionError::Raise("Circle creation aborted: points lay on one line");
137
138       double x, y, z, x1, y1, z1, x2, y2, z2, dx, dy, dz, dx2, dy2, dz2, dx3, dy3, dz3, aRadius;
139       //Calculations for Radius
140       x = aP1.X(); y = aP1.Y(); z = aP1.Z();
141       x1 = aP2.X(); y1 = aP2.Y(); z1 = aP2.Z();
142       dx = x1 - x;
143       dy = y1 - y;
144       dz = z1 - z;
145       aRadius = sqrt(dx*dx + dy*dy + dz*dz);
146       //Calculations for Plane Vector
147       x2 = aP3.X(); y2 = aP3.Y(); z2 = aP3.Z();
148       dx2 = x2 - x; dy2 = y2 - y; dz2 = z2 - z;
149       dx3 = ((dy*dz2) - (dy2*dz))/100;
150       dy3 = ((dx2*dz) - (dx*dz2))/100;
151       dz3 = ((dx*dy2) - (dx2*dy))/100;
152       //Make Plane Vector
153       gp_Dir aDir ( dx3, dy3, dz3 );
154       //Make Circle
155       gp_Ax2 anAxes (aP1, aDir);
156       gp_Circ aCirc (anAxes, aRadius);
157       aShape = BRepBuilderAPI_MakeEdge(aCirc).Edge();
158     }
159   }
160   else if (aType == CIRCLE_THREE_PNT) {
161     Handle(GEOM_Function) aRefPoint1 = aCI.GetPoint1();
162     Handle(GEOM_Function) aRefPoint2 = aCI.GetPoint2();
163     Handle(GEOM_Function) aRefPoint3 = aCI.GetPoint3();
164     TopoDS_Shape aShapePnt1 = aRefPoint1->GetValue();
165     TopoDS_Shape aShapePnt2 = aRefPoint2->GetValue();
166     TopoDS_Shape aShapePnt3 = aRefPoint3->GetValue();
167     if (aShapePnt1.ShapeType() == TopAbs_VERTEX &&
168         aShapePnt2.ShapeType() == TopAbs_VERTEX &&
169         aShapePnt3.ShapeType() == TopAbs_VERTEX) {
170       gp_Pnt aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt1));
171       gp_Pnt aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt2));
172       gp_Pnt aP3 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt3));
173       if (aP1.Distance(aP2) < gp::Resolution() ||
174           aP1.Distance(aP3) < gp::Resolution() ||
175           aP2.Distance(aP3) < gp::Resolution())
176         Standard_ConstructionError::Raise("Circle creation aborted: coincident points given");
177       if (gp_Vec(aP1, aP2).IsParallel(gp_Vec(aP1, aP3), Precision::Angular()))
178         Standard_ConstructionError::Raise("Circle creation aborted: points lay on one line");
179       Handle(Geom_Circle) aCirc = GC_MakeCircle(aP1, aP2, aP3).Value();
180       aShape = BRepBuilderAPI_MakeEdge(aCirc).Edge();
181     }
182   }
183   else {
184   }
185
186   if (aShape.IsNull()) return 0;
187
188   aFunction->SetValue(aShape);
189
190   log->SetTouched(Label());
191
192   return 1;
193 }
194
195 //================================================================================
196 /*!
197  * \brief Returns a name of creation operation and names and values of creation parameters
198  */
199 //================================================================================
200
201 bool GEOMImpl_CircleDriver::
202 GetCreationInformation(std::string&             theOperationName,
203                        std::vector<GEOM_Param>& theParams)
204 {
205   if (Label().IsNull()) return 0;
206   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
207
208   GEOMImpl_ICircle aCI( function );
209   Standard_Integer aType = function->GetType();
210
211   theOperationName = "CIRCLE";
212
213   switch ( aType ) {
214   case CIRCLE_PNT_VEC_R:
215     AddParam( theParams, "Center Point", aCI.GetCenter(), "Origin" );
216     AddParam( theParams, "Vector", aCI.GetVector(), "Z axis" );
217     AddParam( theParams, "Radius", aCI.GetRadius() );
218     break;
219   case CIRCLE_CENTER_TWO_PNT:
220     AddParam( theParams, "Center Point", aCI.GetPoint1() );
221     AddParam( theParams, "Point 1", aCI.GetPoint2() );
222     AddParam( theParams, "Point 2", aCI.GetPoint3() );
223     break;
224   case CIRCLE_THREE_PNT:
225     AddParam( theParams, "Point 1", aCI.GetPoint1() );
226     AddParam( theParams, "Point 2", aCI.GetPoint2() );
227     AddParam( theParams, "Point 3", aCI.GetPoint3() );
228     break;
229   default:
230     return false;
231   }
232   
233   return true;
234 }
235
236 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_CircleDriver,GEOM_BaseDriver)