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/ or email : webmaster.salome@opencascade.com
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 <GEOMImpl_IMeasureOperations.hxx>
31 #include <BRepBuilderAPI_MakeFace.hxx>
32 #include <BRep_Tool.hxx>
33 #include <BRepTopAdaptor_FClass2d.hxx>
34 #include <ShapeAnalysis.hxx>
38 #include <TopoDS_Shape.hxx>
39 #include <TopoDS_Edge.hxx>
40 #include <TopoDS_Vertex.hxx>
43 #include <GC_MakePlane.hxx>
44 #include <Geom_Surface.hxx>
46 #include <Precision.hxx>
53 #include <Standard_ConstructionError.hxx>
54 #include <Standard_TypeMismatch.hxx>
56 //=======================================================================
59 //=======================================================================
60 const Standard_GUID& GEOMImpl_PlaneDriver::GetID()
62 static Standard_GUID aPlaneDriver("FF1BBB05-5D14-4df2-980B-3A668264EA16");
67 //=======================================================================
68 //function : GEOMImpl_PlaneDriver
70 //=======================================================================
71 GEOMImpl_PlaneDriver::GEOMImpl_PlaneDriver()
75 //=======================================================================
78 //=======================================================================
79 Standard_Integer GEOMImpl_PlaneDriver::Execute(TFunction_Logbook& log) const
81 if (Label().IsNull()) return 0;
82 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
84 GEOMImpl_IPlane aPI (aFunction);
85 Standard_Integer aType = aFunction->GetType();
89 double aSize = aPI.GetSize() / 2.0;
90 if (aType == PLANE_PNT_VEC) {
91 Handle(GEOM_Function) aRefPnt = aPI.GetPoint();
92 Handle(GEOM_Function) aRefVec = aPI.GetVector();
93 TopoDS_Shape aShape1 = aRefPnt->GetValue();
94 TopoDS_Shape aShape2 = aRefVec->GetValue();
95 if (aShape1.ShapeType() != TopAbs_VERTEX ||
96 aShape2.ShapeType() != TopAbs_EDGE) return 0;
97 gp_Pnt aP = BRep_Tool::Pnt(TopoDS::Vertex(aShape1));
98 TopoDS_Edge anE = TopoDS::Edge(aShape2);
100 TopExp::Vertices(anE, V1, V2, Standard_True);
101 if (!V1.IsNull() && !V2.IsNull()) {
102 gp_Vec aV (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
103 gp_Pln aPln (aP, aV);
104 aShape = BRepBuilderAPI_MakeFace(aPln, -aSize, +aSize, -aSize, +aSize).Shape();
106 } else if (aType == PLANE_THREE_PNT) {
107 Handle(GEOM_Function) aRefPnt1 = aPI.GetPoint1();
108 Handle(GEOM_Function) aRefPnt2 = aPI.GetPoint2();
109 Handle(GEOM_Function) aRefPnt3 = aPI.GetPoint3();
110 TopoDS_Shape aShape1 = aRefPnt1->GetValue();
111 TopoDS_Shape aShape2 = aRefPnt2->GetValue();
112 TopoDS_Shape aShape3 = aRefPnt3->GetValue();
113 if (aShape1.ShapeType() != TopAbs_VERTEX ||
114 aShape2.ShapeType() != TopAbs_VERTEX ||
115 aShape3.ShapeType() != TopAbs_VERTEX) return 0;
116 gp_Pnt aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aShape1));
117 gp_Pnt aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aShape2));
118 gp_Pnt aP3 = BRep_Tool::Pnt(TopoDS::Vertex(aShape3));
119 if (aP1.Distance(aP2) < gp::Resolution() ||
120 aP1.Distance(aP3) < gp::Resolution() ||
121 aP2.Distance(aP3) < gp::Resolution())
122 Standard_ConstructionError::Raise("Plane creation aborted: coincident points given");
123 if (gp_Vec(aP1, aP2).IsParallel(gp_Vec(aP1, aP3), Precision::Angular()))
124 Standard_ConstructionError::Raise("Plane creation aborted: points lay on one line");
125 GC_MakePlane aMakePlane (aP1, aP2, aP3);
126 aShape = BRepBuilderAPI_MakeFace(aMakePlane, -aSize, +aSize, -aSize, +aSize).Shape();
127 } else if (aType == PLANE_FACE) {
128 Handle(GEOM_Function) aRef = aPI.GetFace();
129 TopoDS_Shape aRefShape = aRef->GetValue();
130 //if (aRefShape.ShapeType() != TopAbs_FACE) return 0;
131 //Handle(Geom_Surface) aGS = BRep_Tool::Surface(TopoDS::Face(aRefShape));
132 //if (!aGS->IsKind(STANDARD_TYPE(Geom_Plane))) {
133 // Standard_TypeMismatch::Raise("Plane creation aborted: non-planar face given as argument");
135 //aShape = BRepBuilderAPI_MakeFace(aGS, -aSize, +aSize, -aSize, +aSize).Shape();
136 gp_Ax3 anAx3 = GEOMImpl_IMeasureOperations::GetPosition(aRefShape);
138 aShape = BRepBuilderAPI_MakeFace(aPln, -aSize, +aSize, -aSize, +aSize).Shape();
140 else if (aType == PLANE_TANGENT_FACE)
142 Handle(GEOM_Function) aRefFace = aPI.GetFace();
143 TopoDS_Shape aShape1 = aRefFace->GetValue();
145 Standard_TypeMismatch::Raise("Plane was not created.Basis face was not specified");
146 TopoDS_Face aFace = TopoDS::Face(aShape1);
148 Standard_Real aKoefU = aPI.GetParameterU();
149 Standard_Real aKoefV = aPI.GetParameterV();
150 Standard_Real aUmin,aUmax,aVmin,aVmax;
151 ShapeAnalysis::GetFaceUVBounds(aFace,aUmin,aUmax,aVmin,aVmax);
152 Standard_Real aDeltaU = aUmax - aUmin;
153 Standard_Real aDeltaV = aVmax - aVmin;
154 Standard_Real aParamU = aUmin + aDeltaU*aKoefU;
155 Standard_Real aParamV = aVmin + aDeltaV*aKoefV;
156 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
158 Standard_TypeMismatch::Raise("Plane was not created.Base surface is absent");
161 aSurf->D1(aParamU,aParamV,aPLoc,aVecU,aVecV);
162 BRepTopAdaptor_FClass2d clas(aFace,Precision::PConfusion());
164 TopAbs_State stOut= clas.PerformInfinitePoint();
165 gp_Pnt2d aP2d(aParamU,aParamV);
166 TopAbs_State st= clas.Perform(aP2d);
168 Standard_TypeMismatch::Raise("Plane was not created.Point lies outside the face");
169 gp_Vec aNorm = aVecU^aVecV;
170 gp_Ax3 anAxis(aPLoc,gp_Dir(aNorm),gp_Dir(aVecU));
171 gp_Pln aPlane(anAxis);
172 BRepBuilderAPI_MakeFace aTool(aPlane, -aSize, +aSize, -aSize, +aSize);
174 aShape = aTool.Shape();
179 if (aShape.IsNull()) return 0;
181 aFunction->SetValue(aShape);
183 log.SetTouched(Label());
189 //=======================================================================
190 //function : GEOMImpl_PlaneDriver_Type_
192 //=======================================================================
193 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PlaneDriver_Type_()
196 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
197 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
198 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
199 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
200 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
201 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
204 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
205 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_PlaneDriver",
206 sizeof(GEOMImpl_PlaneDriver),
208 (Standard_Address)_Ancestors,
209 (Standard_Address)NULL);
214 //=======================================================================
215 //function : DownCast
217 //=======================================================================
218 const Handle(GEOMImpl_PlaneDriver) Handle(GEOMImpl_PlaneDriver)::DownCast
219 (const Handle(Standard_Transient)& AnObject)
221 Handle(GEOMImpl_PlaneDriver) _anOtherObject;
223 if (!AnObject.IsNull()) {
224 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PlaneDriver))) {
225 _anOtherObject = Handle(GEOMImpl_PlaneDriver)((Handle(GEOMImpl_PlaneDriver)&)AnObject);
229 return _anOtherObject ;