1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include <GEOMImpl_CircleDriver.hxx>
25 #include <GEOMImpl_ICircle.hxx>
26 #include <GEOMImpl_Types.hxx>
28 #include <GEOM_Function.hxx>
30 #include <GEOMUtils.hxx>
32 #include <BRepBuilderAPI_MakeEdge.hxx>
33 #include <BRep_Tool.hxx>
36 #include <TopoDS_Shape.hxx>
37 #include <TopoDS_Edge.hxx>
38 #include <TopoDS_Vertex.hxx>
42 #include <GC_MakeCircle.hxx>
43 #include <Geom_Circle.hxx>
45 #include <Standard_ConstructionError.hxx>
46 #include <Precision.hxx>
49 #include <gp_Circ.hxx>
51 //=======================================================================
54 //=======================================================================
55 const Standard_GUID& GEOMImpl_CircleDriver::GetID()
57 static Standard_GUID aCircleDriver("FF1BBB32-5D14-4df2-980B-3A668264EA16");
62 //=======================================================================
63 //function : GEOMImpl_CircleDriver
65 //=======================================================================
66 GEOMImpl_CircleDriver::GEOMImpl_CircleDriver()
70 //=======================================================================
73 //=======================================================================
74 Standard_Integer GEOMImpl_CircleDriver::Execute(LOGBOOK& log) const
76 if (Label().IsNull()) return 0;
77 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
79 GEOMImpl_ICircle aCI (aFunction);
80 Standard_Integer aType = aFunction->GetType();
84 if (aType == CIRCLE_PNT_VEC_R) {
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");
94 aP = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt));
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);
105 gp_Ax2 anAxes (aP, aV);
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);
112 gp_Circ aCirc (anAxes, anR);
113 aShape = BRepBuilderAPI_MakeEdge(aCirc).Edge();
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)
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));
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");
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");
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();
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;
153 gp_Dir aDir ( dx3, dy3, dz3 );
155 gp_Ax2 anAxes (aP1, aDir);
156 gp_Circ aCirc (anAxes, aRadius);
157 aShape = BRepBuilderAPI_MakeEdge(aCirc).Edge();
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();
186 if (aShape.IsNull()) return 0;
188 aFunction->SetValue(aShape);
190 #if OCC_VERSION_MAJOR < 7
191 log.SetTouched(Label());
193 log->SetTouched(Label());
199 //================================================================================
201 * \brief Returns a name of creation operation and names and values of creation parameters
203 //================================================================================
205 bool GEOMImpl_CircleDriver::
206 GetCreationInformation(std::string& theOperationName,
207 std::vector<GEOM_Param>& theParams)
209 if (Label().IsNull()) return 0;
210 Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
212 GEOMImpl_ICircle aCI( function );
213 Standard_Integer aType = function->GetType();
215 theOperationName = "CIRCLE";
218 case CIRCLE_PNT_VEC_R:
219 AddParam( theParams, "Center Point", aCI.GetCenter(), "Origin" );
220 AddParam( theParams, "Vector", aCI.GetVector(), "Z axis" );
221 AddParam( theParams, "Radius", aCI.GetRadius() );
223 case CIRCLE_CENTER_TWO_PNT:
224 AddParam( theParams, "Center Point", aCI.GetPoint1() );
225 AddParam( theParams, "Point 1", aCI.GetPoint2() );
226 AddParam( theParams, "Point 2", aCI.GetPoint3() );
228 case CIRCLE_THREE_PNT:
229 AddParam( theParams, "Point 1", aCI.GetPoint1() );
230 AddParam( theParams, "Point 2", aCI.GetPoint2() );
231 AddParam( theParams, "Point 3", aCI.GetPoint3() );
240 OCCT_IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_CircleDriver,GEOM_BaseDriver);