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