Salome HOME
if a shape to be rotated is a vertex, and it is located ON the Revolution Axis, then...
[modules/geom.git] / src / GEOMImpl / GEOMImpl_PlaneDriver.cxx
1
2 using namespace std;
3 #include "GEOMImpl_PlaneDriver.hxx"
4 #include "GEOMImpl_IPlane.hxx"
5 #include "GEOMImpl_Types.hxx"
6 #include "GEOM_Function.hxx"
7
8 #include <BRepBuilderAPI_MakeFace.hxx>
9 #include <BRep_Tool.hxx>
10
11 #include <TopAbs.hxx>
12 #include <TopoDS.hxx>
13 #include <TopoDS_Shape.hxx>
14 #include <TopoDS_Edge.hxx>
15 #include <TopoDS_Vertex.hxx>
16 #include <TopExp.hxx>
17
18 #include <GC_MakePlane.hxx>
19 #include <Geom_Surface.hxx>
20
21 #include <Precision.hxx>
22 #include <gp_Pnt.hxx>
23 #include <gp_Pln.hxx>
24 #include <gp_Vec.hxx>
25
26 #include <Standard_ConstructionError.hxx>
27 #include <Standard_TypeMismatch.hxx>
28
29 //=======================================================================
30 //function : GetID
31 //purpose  :
32 //======================================================================= 
33 const Standard_GUID& GEOMImpl_PlaneDriver::GetID()
34 {
35   static Standard_GUID aPlaneDriver("FF1BBB05-5D14-4df2-980B-3A668264EA16");
36   return aPlaneDriver; 
37 }
38
39
40 //=======================================================================
41 //function : GEOMImpl_PlaneDriver
42 //purpose  : 
43 //=======================================================================
44 GEOMImpl_PlaneDriver::GEOMImpl_PlaneDriver() 
45 {
46 }
47
48 //=======================================================================
49 //function : Execute
50 //purpose  :
51 //======================================================================= 
52 Standard_Integer GEOMImpl_PlaneDriver::Execute(TFunction_Logbook& log) const
53 {
54   if (Label().IsNull())  return 0;    
55   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
56
57   GEOMImpl_IPlane aPI (aFunction);
58   Standard_Integer aType = aFunction->GetType();
59
60   TopoDS_Shape aShape;
61
62   double aSize = aPI.GetSize() / 2.0;
63   if (aType == PLANE_PNT_VEC) {
64     Handle(GEOM_Function) aRefPnt = aPI.GetPoint();
65     Handle(GEOM_Function) aRefVec = aPI.GetVector();
66     TopoDS_Shape aShape1 = aRefPnt->GetValue();
67     TopoDS_Shape aShape2 = aRefVec->GetValue();
68     if (aShape1.ShapeType() != TopAbs_VERTEX ||
69         aShape2.ShapeType() != TopAbs_EDGE) return 0;
70     gp_Pnt aP = BRep_Tool::Pnt(TopoDS::Vertex(aShape1));
71     TopoDS_Edge anE = TopoDS::Edge(aShape2);
72     TopoDS_Vertex V1, V2;
73     TopExp::Vertices(anE, V1, V2, Standard_True);
74     if (!V1.IsNull() && !V2.IsNull()) {
75       gp_Vec aV (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
76       gp_Pln aPln (aP, aV);
77       aShape = BRepBuilderAPI_MakeFace(aPln, -aSize, +aSize, -aSize, +aSize).Shape();
78     }
79   } else if (aType == PLANE_THREE_PNT) {
80     Handle(GEOM_Function) aRefPnt1 = aPI.GetPoint1();
81     Handle(GEOM_Function) aRefPnt2 = aPI.GetPoint2();
82     Handle(GEOM_Function) aRefPnt3 = aPI.GetPoint3();
83     TopoDS_Shape aShape1 = aRefPnt1->GetValue();
84     TopoDS_Shape aShape2 = aRefPnt2->GetValue();
85     TopoDS_Shape aShape3 = aRefPnt3->GetValue();
86     if (aShape1.ShapeType() != TopAbs_VERTEX ||
87         aShape2.ShapeType() != TopAbs_VERTEX ||
88         aShape3.ShapeType() != TopAbs_VERTEX) return 0;
89     gp_Pnt aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aShape1));
90     gp_Pnt aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aShape2));
91     gp_Pnt aP3 = BRep_Tool::Pnt(TopoDS::Vertex(aShape3));
92     if (aP1.Distance(aP2) < gp::Resolution() ||
93         aP1.Distance(aP3) < gp::Resolution() ||
94         aP2.Distance(aP3) < gp::Resolution())
95       Standard_ConstructionError::Raise("Plane creation aborted: coincident points given");
96     if (gp_Vec(aP1, aP2).IsParallel(gp_Vec(aP1, aP3), Precision::Angular()))
97       Standard_ConstructionError::Raise("Plane creation aborted: points lay on one line");
98     GC_MakePlane aMakePlane (aP1, aP2, aP3);
99     aShape = BRepBuilderAPI_MakeFace(aMakePlane, -aSize, +aSize, -aSize, +aSize).Shape();
100   } else if (aType == PLANE_FACE) {
101     Handle(GEOM_Function) aRef = aPI.GetFace();
102     TopoDS_Shape aRefShape = aRef->GetValue();
103     if (aRefShape.ShapeType() != TopAbs_FACE) return 0;
104     Handle(Geom_Surface) aGS = BRep_Tool::Surface(TopoDS::Face(aRefShape));
105     if (!aGS->IsKind(STANDARD_TYPE(Geom_Plane))) {
106       Standard_TypeMismatch::Raise("Plane creation aborted: non-planar face given as argument");
107     }
108     aShape = BRepBuilderAPI_MakeFace(aGS, -aSize, +aSize, -aSize, +aSize).Shape();
109   } else {
110   }
111
112   if (aShape.IsNull()) return 0;
113
114   aFunction->SetValue(aShape);
115
116   log.SetTouched(Label()); 
117
118   return 1;
119 }
120
121
122 //=======================================================================
123 //function :  GEOMImpl_PlaneDriver_Type_
124 //purpose  :
125 //======================================================================= 
126 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PlaneDriver_Type_()
127 {
128
129   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
130   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
131   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
132   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
133   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
134   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
135  
136
137   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
138   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_PlaneDriver",
139                                                          sizeof(GEOMImpl_PlaneDriver),
140                                                          1,
141                                                          (Standard_Address)_Ancestors,
142                                                          (Standard_Address)NULL);
143
144   return _aType;
145 }
146
147 //=======================================================================
148 //function : DownCast
149 //purpose  :
150 //======================================================================= 
151 const Handle(GEOMImpl_PlaneDriver) Handle(GEOMImpl_PlaneDriver)::DownCast
152        (const Handle(Standard_Transient)& AnObject)
153 {
154   Handle(GEOMImpl_PlaneDriver) _anOtherObject;
155
156   if (!AnObject.IsNull()) {
157      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PlaneDriver))) {
158        _anOtherObject = Handle(GEOMImpl_PlaneDriver)((Handle(GEOMImpl_PlaneDriver)&)AnObject);
159      }
160   }
161
162   return _anOtherObject ;
163 }