Salome HOME
998c6384d8018340c93800de4b3a3249eb33cf60
[modules/geom.git] / src / GEOMImpl / GEOMImpl_CircleDriver.cxx
1
2 #include <Standard_Stream.hxx>
3
4 #include <GEOMImpl_CircleDriver.hxx>
5 #include <GEOMImpl_ICircle.hxx>
6 #include <GEOMImpl_Types.hxx>
7 #include <GEOM_Function.hxx>
8
9 #include <BRepBuilderAPI_MakeEdge.hxx>
10 #include <BRep_Tool.hxx>
11 #include <TopoDS.hxx>
12 #include <TopoDS_Shape.hxx>
13 #include <TopoDS_Edge.hxx>
14 #include <TopoDS_Vertex.hxx>
15 #include <TopAbs.hxx>
16 #include <TopExp.hxx>
17
18 #include <GC_MakeCircle.hxx>
19 #include <Geom_Circle.hxx>
20
21 #include <Standard_ConstructionError.hxx>
22 #include <Precision.hxx>
23 #include <gp_Pnt.hxx>
24 #include <gp_Vec.hxx>
25 #include <gp_Circ.hxx>
26
27 //=======================================================================
28 //function : GetID
29 //purpose  :
30 //======================================================================= 
31 const Standard_GUID& GEOMImpl_CircleDriver::GetID()
32 {
33   static Standard_GUID aCircleDriver("FF1BBB32-5D14-4df2-980B-3A668264EA16");
34   return aCircleDriver; 
35 }
36
37
38 //=======================================================================
39 //function : GEOMImpl_CircleDriver
40 //purpose  : 
41 //=======================================================================
42 GEOMImpl_CircleDriver::GEOMImpl_CircleDriver() 
43 {
44 }
45
46 //=======================================================================
47 //function : Execute
48 //purpose  :
49 //======================================================================= 
50 Standard_Integer GEOMImpl_CircleDriver::Execute(TFunction_Logbook& log) const
51 {
52   if (Label().IsNull()) return 0;    
53   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
54
55   GEOMImpl_ICircle aCI (aFunction);
56   Standard_Integer aType = aFunction->GetType();
57
58   TopoDS_Shape aShape;
59
60   if (aType == CIRCLE_PNT_VEC_R) {
61     Handle(GEOM_Function) aRefPoint  = aCI.GetCenter();
62     Handle(GEOM_Function) aRefVector = aCI.GetVector();
63     TopoDS_Shape aShapePnt = aRefPoint->GetValue();
64     TopoDS_Shape aShapeVec = aRefVector->GetValue();
65     if (aShapePnt.ShapeType() == TopAbs_VERTEX &&
66         aShapeVec.ShapeType() == TopAbs_EDGE) {
67       gp_Pnt aP = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt));
68       TopoDS_Edge anE = TopoDS::Edge(aShapeVec);
69       TopoDS_Vertex V1, V2;
70       TopExp::Vertices(anE, V1, V2, Standard_True);
71       if (!V1.IsNull() && !V2.IsNull()) {
72         gp_Vec aV (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
73         gp_Ax2 anAxes (aP, aV);
74         gp_Circ aCirc (anAxes, aCI.GetRadius());
75         aShape = BRepBuilderAPI_MakeEdge(aCirc).Edge();
76       }
77     }
78   }
79   else if (aType == CIRCLE_THREE_PNT) {
80     Handle(GEOM_Function) aRefPoint1 = aCI.GetPoint1();
81     Handle(GEOM_Function) aRefPoint2 = aCI.GetPoint2();
82     Handle(GEOM_Function) aRefPoint3 = aCI.GetPoint3();
83     TopoDS_Shape aShapePnt1 = aRefPoint1->GetValue();
84     TopoDS_Shape aShapePnt2 = aRefPoint2->GetValue();
85     TopoDS_Shape aShapePnt3 = aRefPoint3->GetValue();
86     if (aShapePnt1.ShapeType() == TopAbs_VERTEX &&
87         aShapePnt2.ShapeType() == TopAbs_VERTEX &&
88         aShapePnt3.ShapeType() == TopAbs_VERTEX) {
89       gp_Pnt aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt1));
90       gp_Pnt aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt2));
91       gp_Pnt aP3 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt3));
92       if (aP1.Distance(aP2) < gp::Resolution() ||
93           aP1.Distance(aP3) < gp::Resolution() ||
94           aP2.Distance(aP3) < gp::Resolution())
95         Standard_ConstructionError::Raise("Circle creation aborted: coincident points given");
96       if (gp_Vec(aP1, aP2).IsParallel(gp_Vec(aP1, aP3), Precision::Angular()))
97         Standard_ConstructionError::Raise("Circle creation aborted: points lay on one line");
98       Handle(Geom_Circle) aCirc = GC_MakeCircle(aP1, aP2, aP3).Value();
99       aShape = BRepBuilderAPI_MakeEdge(aCirc).Edge();
100     }
101   }
102   else {
103   }
104
105   if (aShape.IsNull()) return 0;
106
107   aFunction->SetValue(aShape);
108
109   log.SetTouched(Label()); 
110
111   return 1;    
112 }
113
114
115 //=======================================================================
116 //function :  GEOMImpl_CircleDriver_Type_
117 //purpose  :
118 //======================================================================= 
119 Standard_EXPORT Handle_Standard_Type& GEOMImpl_CircleDriver_Type_()
120 {
121
122   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
123   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
124   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
125   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
126   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
127   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
128  
129
130   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
131   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_CircleDriver",
132                                                          sizeof(GEOMImpl_CircleDriver),
133                                                          1,
134                                                          (Standard_Address)_Ancestors,
135                                                          (Standard_Address)NULL);
136
137   return _aType;
138 }
139
140 //=======================================================================
141 //function : DownCast
142 //purpose  :
143 //======================================================================= 
144 const Handle(GEOMImpl_CircleDriver) Handle(GEOMImpl_CircleDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
145 {
146   Handle(GEOMImpl_CircleDriver) _anOtherObject;
147
148   if (!AnObject.IsNull()) {
149      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_CircleDriver))) {
150        _anOtherObject = Handle(GEOMImpl_CircleDriver)((Handle(GEOMImpl_CircleDriver)&)AnObject);
151      }
152   }
153
154   return _anOtherObject ;
155 }