1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/
21 #include <Standard_Stream.hxx>
23 #include <GEOMImpl_PlaneDriver.hxx>
24 #include <GEOMImpl_IPlane.hxx>
25 #include <GEOMImpl_Types.hxx>
26 #include <GEOM_Function.hxx>
28 #include <BRepBuilderAPI_MakeFace.hxx>
29 #include <BRep_Tool.hxx>
33 #include <TopoDS_Shape.hxx>
34 #include <TopoDS_Edge.hxx>
35 #include <TopoDS_Vertex.hxx>
38 #include <GC_MakePlane.hxx>
39 #include <Geom_Surface.hxx>
41 #include <Precision.hxx>
46 #include <Standard_ConstructionError.hxx>
47 #include <Standard_TypeMismatch.hxx>
49 //=======================================================================
52 //=======================================================================
53 const Standard_GUID& GEOMImpl_PlaneDriver::GetID()
55 static Standard_GUID aPlaneDriver("FF1BBB05-5D14-4df2-980B-3A668264EA16");
60 //=======================================================================
61 //function : GEOMImpl_PlaneDriver
63 //=======================================================================
64 GEOMImpl_PlaneDriver::GEOMImpl_PlaneDriver()
68 //=======================================================================
71 //=======================================================================
72 Standard_Integer GEOMImpl_PlaneDriver::Execute(TFunction_Logbook& log) const
74 if (Label().IsNull()) return 0;
75 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
77 GEOMImpl_IPlane aPI (aFunction);
78 Standard_Integer aType = aFunction->GetType();
82 double aSize = aPI.GetSize() / 2.0;
83 if (aType == PLANE_PNT_VEC) {
84 Handle(GEOM_Function) aRefPnt = aPI.GetPoint();
85 Handle(GEOM_Function) aRefVec = aPI.GetVector();
86 TopoDS_Shape aShape1 = aRefPnt->GetValue();
87 TopoDS_Shape aShape2 = aRefVec->GetValue();
88 if (aShape1.ShapeType() != TopAbs_VERTEX ||
89 aShape2.ShapeType() != TopAbs_EDGE) return 0;
90 gp_Pnt aP = BRep_Tool::Pnt(TopoDS::Vertex(aShape1));
91 TopoDS_Edge anE = TopoDS::Edge(aShape2);
93 TopExp::Vertices(anE, V1, V2, Standard_True);
94 if (!V1.IsNull() && !V2.IsNull()) {
95 gp_Vec aV (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
97 aShape = BRepBuilderAPI_MakeFace(aPln, -aSize, +aSize, -aSize, +aSize).Shape();
99 } else if (aType == PLANE_THREE_PNT) {
100 Handle(GEOM_Function) aRefPnt1 = aPI.GetPoint1();
101 Handle(GEOM_Function) aRefPnt2 = aPI.GetPoint2();
102 Handle(GEOM_Function) aRefPnt3 = aPI.GetPoint3();
103 TopoDS_Shape aShape1 = aRefPnt1->GetValue();
104 TopoDS_Shape aShape2 = aRefPnt2->GetValue();
105 TopoDS_Shape aShape3 = aRefPnt3->GetValue();
106 if (aShape1.ShapeType() != TopAbs_VERTEX ||
107 aShape2.ShapeType() != TopAbs_VERTEX ||
108 aShape3.ShapeType() != TopAbs_VERTEX) return 0;
109 gp_Pnt aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aShape1));
110 gp_Pnt aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aShape2));
111 gp_Pnt aP3 = BRep_Tool::Pnt(TopoDS::Vertex(aShape3));
112 if (aP1.Distance(aP2) < gp::Resolution() ||
113 aP1.Distance(aP3) < gp::Resolution() ||
114 aP2.Distance(aP3) < gp::Resolution())
115 Standard_ConstructionError::Raise("Plane creation aborted: coincident points given");
116 if (gp_Vec(aP1, aP2).IsParallel(gp_Vec(aP1, aP3), Precision::Angular()))
117 Standard_ConstructionError::Raise("Plane creation aborted: points lay on one line");
118 GC_MakePlane aMakePlane (aP1, aP2, aP3);
119 aShape = BRepBuilderAPI_MakeFace(aMakePlane, -aSize, +aSize, -aSize, +aSize).Shape();
120 } else if (aType == PLANE_FACE) {
121 Handle(GEOM_Function) aRef = aPI.GetFace();
122 TopoDS_Shape aRefShape = aRef->GetValue();
123 if (aRefShape.ShapeType() != TopAbs_FACE) return 0;
124 Handle(Geom_Surface) aGS = BRep_Tool::Surface(TopoDS::Face(aRefShape));
125 if (!aGS->IsKind(STANDARD_TYPE(Geom_Plane))) {
126 Standard_TypeMismatch::Raise("Plane creation aborted: non-planar face given as argument");
128 aShape = BRepBuilderAPI_MakeFace(aGS, -aSize, +aSize, -aSize, +aSize).Shape();
132 if (aShape.IsNull()) return 0;
134 aFunction->SetValue(aShape);
136 log.SetTouched(Label());
142 //=======================================================================
143 //function : GEOMImpl_PlaneDriver_Type_
145 //=======================================================================
146 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PlaneDriver_Type_()
149 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
150 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
151 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
152 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
153 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
154 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
157 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
158 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_PlaneDriver",
159 sizeof(GEOMImpl_PlaneDriver),
161 (Standard_Address)_Ancestors,
162 (Standard_Address)NULL);
167 //=======================================================================
168 //function : DownCast
170 //=======================================================================
171 const Handle(GEOMImpl_PlaneDriver) Handle(GEOMImpl_PlaneDriver)::DownCast
172 (const Handle(Standard_Transient)& AnObject)
174 Handle(GEOMImpl_PlaneDriver) _anOtherObject;
176 if (!AnObject.IsNull()) {
177 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PlaneDriver))) {
178 _anOtherObject = Handle(GEOMImpl_PlaneDriver)((Handle(GEOMImpl_PlaneDriver)&)AnObject);
182 return _anOtherObject ;