1 // Copyright (C) 2007-2008 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.
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
22 #include <Standard_Stream.hxx>
24 #include <GEOMImpl_CircleDriver.hxx>
25 #include <GEOMImpl_ICircle.hxx>
26 #include <GEOMImpl_Types.hxx>
27 #include <GEOM_Function.hxx>
29 #include <BRepBuilderAPI_MakeEdge.hxx>
30 #include <BRep_Tool.hxx>
32 #include <TopoDS_Shape.hxx>
33 #include <TopoDS_Edge.hxx>
34 #include <TopoDS_Vertex.hxx>
38 #include <GC_MakeCircle.hxx>
39 #include <Geom_Circle.hxx>
41 #include <Standard_ConstructionError.hxx>
42 #include <Precision.hxx>
45 #include <gp_Circ.hxx>
47 //=======================================================================
50 //=======================================================================
51 const Standard_GUID& GEOMImpl_CircleDriver::GetID()
53 static Standard_GUID aCircleDriver("FF1BBB32-5D14-4df2-980B-3A668264EA16");
58 //=======================================================================
59 //function : GEOMImpl_CircleDriver
61 //=======================================================================
62 GEOMImpl_CircleDriver::GEOMImpl_CircleDriver()
66 //=======================================================================
69 //=======================================================================
70 Standard_Integer GEOMImpl_CircleDriver::Execute(TFunction_Logbook& log) const
72 if (Label().IsNull()) return 0;
73 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
75 GEOMImpl_ICircle aCI (aFunction);
76 Standard_Integer aType = aFunction->GetType();
80 if (aType == CIRCLE_PNT_VEC_R) {
81 Handle(GEOM_Function) aRefPoint = aCI.GetCenter();
82 Handle(GEOM_Function) aRefVector = aCI.GetVector();
83 TopoDS_Shape aShapePnt = aRefPoint->GetValue();
84 TopoDS_Shape aShapeVec = aRefVector->GetValue();
85 if (aShapePnt.ShapeType() == TopAbs_VERTEX &&
86 aShapeVec.ShapeType() == TopAbs_EDGE) {
87 gp_Pnt aP = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt));
88 TopoDS_Edge anE = TopoDS::Edge(aShapeVec);
90 TopExp::Vertices(anE, V1, V2, Standard_True);
91 if (!V1.IsNull() && !V2.IsNull()) {
92 gp_Vec aV (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
93 gp_Ax2 anAxes (aP, aV);
94 gp_Circ aCirc (anAxes, aCI.GetRadius());
95 aShape = BRepBuilderAPI_MakeEdge(aCirc).Edge();
99 else if (aType == CIRCLE_CENTER_TWO_PNT) {
100 Handle(GEOM_Function) aRefPoint1 = aCI.GetPoint1();
101 Handle(GEOM_Function) aRefPoint2 = aCI.GetPoint2();
102 Handle(GEOM_Function) aRefPoint3 = aCI.GetPoint3();
103 TopoDS_Shape aShapePnt1 = aRefPoint1->GetValue();
104 TopoDS_Shape aShapePnt2 = aRefPoint2->GetValue();
105 TopoDS_Shape aShapePnt3 = aRefPoint3->GetValue();
106 if (aShapePnt1.ShapeType() == TopAbs_VERTEX && aShapePnt2.ShapeType() == TopAbs_VERTEX &&
107 aShapePnt3.ShapeType() == TopAbs_VERTEX)
109 gp_Pnt aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt1));
110 gp_Pnt aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt2));
111 gp_Pnt aP3 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt3));
113 if (aP1.Distance(aP2) < gp::Resolution() ||
114 aP1.Distance(aP3) < gp::Resolution() ||
115 aP2.Distance(aP3) < gp::Resolution())
116 Standard_ConstructionError::Raise("Circle creation aborted: coincident points given");
117 if (gp_Vec(aP1, aP2).IsParallel(gp_Vec(aP1, aP3), Precision::Angular()))
118 Standard_ConstructionError::Raise("Circle creation aborted: points lay on one line");
119 double x, y, z, x1, y1, z1, x2, y2, z2, dx, dy, dz, dx2, dy2, dz2, dx3, dy3, dz3, aRadius;
120 //Calculations for Radius
121 x = aP1.X(); y = aP1.Y(); z = aP1.Z();
122 x1 = aP2.X(); y1 = aP2.Y(); z1 = aP2.Z();
126 aRadius = sqrt(dx*dx + dy*dy + dz*dz);
127 //Calculations for Plane Vector
128 x2 = aP3.X(); y2 = aP3.Y(); z2 = aP3.Z();
129 dx2 = x2 - x; dy2 = y2 - y; dz2 = z2 - z;
130 dx3 = ((dy*dz2) - (dy2*dz))/100;
131 dy3 = ((dx2*dz) - (dx*dz2))/100;
132 dz3 = ((dx*dy2) - (dx2*dy))/100;
134 gp_Dir aDir ( dx3, dy3, dz3 );
136 gp_Ax2 anAxes (aP1, aDir);
137 gp_Circ aCirc (anAxes, aRadius);
138 aShape = BRepBuilderAPI_MakeEdge(aCirc).Edge();
141 else if (aType == CIRCLE_THREE_PNT) {
142 Handle(GEOM_Function) aRefPoint1 = aCI.GetPoint1();
143 Handle(GEOM_Function) aRefPoint2 = aCI.GetPoint2();
144 Handle(GEOM_Function) aRefPoint3 = aCI.GetPoint3();
145 TopoDS_Shape aShapePnt1 = aRefPoint1->GetValue();
146 TopoDS_Shape aShapePnt2 = aRefPoint2->GetValue();
147 TopoDS_Shape aShapePnt3 = aRefPoint3->GetValue();
148 if (aShapePnt1.ShapeType() == TopAbs_VERTEX &&
149 aShapePnt2.ShapeType() == TopAbs_VERTEX &&
150 aShapePnt3.ShapeType() == TopAbs_VERTEX) {
151 gp_Pnt aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt1));
152 gp_Pnt aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt2));
153 gp_Pnt aP3 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt3));
154 if (aP1.Distance(aP2) < gp::Resolution() ||
155 aP1.Distance(aP3) < gp::Resolution() ||
156 aP2.Distance(aP3) < gp::Resolution())
157 Standard_ConstructionError::Raise("Circle creation aborted: coincident points given");
158 if (gp_Vec(aP1, aP2).IsParallel(gp_Vec(aP1, aP3), Precision::Angular()))
159 Standard_ConstructionError::Raise("Circle creation aborted: points lay on one line");
160 Handle(Geom_Circle) aCirc = GC_MakeCircle(aP1, aP2, aP3).Value();
161 aShape = BRepBuilderAPI_MakeEdge(aCirc).Edge();
167 if (aShape.IsNull()) return 0;
169 aFunction->SetValue(aShape);
171 log.SetTouched(Label());
177 //=======================================================================
178 //function : GEOMImpl_CircleDriver_Type_
180 //=======================================================================
181 Standard_EXPORT Handle_Standard_Type& GEOMImpl_CircleDriver_Type_()
184 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
185 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
186 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
187 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
188 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
189 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
192 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
193 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_CircleDriver",
194 sizeof(GEOMImpl_CircleDriver),
196 (Standard_Address)_Ancestors,
197 (Standard_Address)NULL);
202 //=======================================================================
203 //function : DownCast
205 //=======================================================================
206 const Handle(GEOMImpl_CircleDriver) Handle(GEOMImpl_CircleDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
208 Handle(GEOMImpl_CircleDriver) _anOtherObject;
210 if (!AnObject.IsNull()) {
211 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_CircleDriver))) {
212 _anOtherObject = Handle(GEOMImpl_CircleDriver)((Handle(GEOMImpl_CircleDriver)&)AnObject);
216 return _anOtherObject ;