1 // Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include <Standard_Stream.hxx>
25 #include <GEOMImpl_PlaneDriver.hxx>
26 #include <GEOMImpl_IPlane.hxx>
27 #include <GEOMImpl_Types.hxx>
28 #include <GEOM_Function.hxx>
30 #include <GEOMImpl_IMeasureOperations.hxx>
33 #include <BRepBuilderAPI_MakeFace.hxx>
34 #include <BRep_Tool.hxx>
35 #include <BRepTopAdaptor_FClass2d.hxx>
36 #include <ShapeAnalysis.hxx>
40 #include <TopoDS_Shape.hxx>
41 #include <TopoDS_Edge.hxx>
42 #include <TopoDS_Vertex.hxx>
45 #include <GC_MakePlane.hxx>
46 #include <Geom_Surface.hxx>
48 #include <Precision.hxx>
55 #include <Standard_ConstructionError.hxx>
56 #include <Standard_TypeMismatch.hxx>
58 //=======================================================================
61 //=======================================================================
62 const Standard_GUID& GEOMImpl_PlaneDriver::GetID()
64 static Standard_GUID aPlaneDriver("FF1BBB05-5D14-4df2-980B-3A668264EA16");
69 //=======================================================================
70 //function : GEOMImpl_PlaneDriver
72 //=======================================================================
73 GEOMImpl_PlaneDriver::GEOMImpl_PlaneDriver()
77 //=======================================================================
80 //=======================================================================
81 Standard_Integer GEOMImpl_PlaneDriver::Execute(TFunction_Logbook& log) const
83 if (Label().IsNull()) return 0;
84 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
86 GEOMImpl_IPlane aPI (aFunction);
87 Standard_Integer aType = aFunction->GetType();
91 double aSize = aPI.GetSize() / 2.0;
92 if (aType == PLANE_PNT_VEC) {
93 Handle(GEOM_Function) aRefPnt = aPI.GetPoint();
94 Handle(GEOM_Function) aRefVec = aPI.GetVector();
95 TopoDS_Shape aShape1 = aRefPnt->GetValue();
96 TopoDS_Shape aShape2 = aRefVec->GetValue();
97 if (aShape1.ShapeType() != TopAbs_VERTEX ||
98 aShape2.ShapeType() != TopAbs_EDGE) return 0;
99 gp_Pnt aP = BRep_Tool::Pnt(TopoDS::Vertex(aShape1));
100 TopoDS_Edge anE = TopoDS::Edge(aShape2);
101 TopoDS_Vertex V1, V2;
102 TopExp::Vertices(anE, V1, V2, Standard_True);
103 if (!V1.IsNull() && !V2.IsNull()) {
104 gp_Vec aV (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
105 gp_Pln aPln (aP, aV);
106 aShape = BRepBuilderAPI_MakeFace(aPln, -aSize, +aSize, -aSize, +aSize).Shape();
108 } else if (aType == PLANE_THREE_PNT) {
109 Handle(GEOM_Function) aRefPnt1 = aPI.GetPoint1();
110 Handle(GEOM_Function) aRefPnt2 = aPI.GetPoint2();
111 Handle(GEOM_Function) aRefPnt3 = aPI.GetPoint3();
112 TopoDS_Shape aShape1 = aRefPnt1->GetValue();
113 TopoDS_Shape aShape2 = aRefPnt2->GetValue();
114 TopoDS_Shape aShape3 = aRefPnt3->GetValue();
115 if (aShape1.ShapeType() != TopAbs_VERTEX ||
116 aShape2.ShapeType() != TopAbs_VERTEX ||
117 aShape3.ShapeType() != TopAbs_VERTEX) return 0;
118 gp_Pnt aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aShape1));
119 gp_Pnt aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aShape2));
120 gp_Pnt aP3 = BRep_Tool::Pnt(TopoDS::Vertex(aShape3));
121 if (aP1.Distance(aP2) < gp::Resolution() ||
122 aP1.Distance(aP3) < gp::Resolution() ||
123 aP2.Distance(aP3) < gp::Resolution())
124 Standard_ConstructionError::Raise("Plane creation aborted: coincident points given");
125 if (gp_Vec(aP1, aP2).IsParallel(gp_Vec(aP1, aP3), Precision::Angular()))
126 Standard_ConstructionError::Raise("Plane creation aborted: points lay on one line");
127 GC_MakePlane aMakePlane (aP1, aP2, aP3);
128 aShape = BRepBuilderAPI_MakeFace(aMakePlane, -aSize, +aSize, -aSize, +aSize).Shape();
129 } else if (aType == PLANE_FACE) {
130 Handle(GEOM_Function) aRef = aPI.GetFace();
131 TopoDS_Shape aRefShape = aRef->GetValue();
132 //if (aRefShape.ShapeType() != TopAbs_FACE) return 0;
133 //Handle(Geom_Surface) aGS = BRep_Tool::Surface(TopoDS::Face(aRefShape));
134 //if (!aGS->IsKind(STANDARD_TYPE(Geom_Plane))) {
135 // Standard_TypeMismatch::Raise("Plane creation aborted: non-planar face given as argument");
137 //aShape = BRepBuilderAPI_MakeFace(aGS, -aSize, +aSize, -aSize, +aSize).Shape();
138 gp_Ax3 anAx3 = GEOMImpl_IMeasureOperations::GetPosition(aRefShape);
140 aShape = BRepBuilderAPI_MakeFace(aPln, -aSize, +aSize, -aSize, +aSize).Shape();
142 else if (aType == PLANE_TANGENT_FACE)
144 Handle(GEOM_Function) aRefFace = aPI.GetFace();
145 TopoDS_Shape aShape1 = aRefFace->GetValue();
147 Standard_TypeMismatch::Raise("Plane was not created.Basis face was not specified");
148 TopoDS_Face aFace = TopoDS::Face(aShape1);
150 Standard_Real aKoefU = aPI.GetParameterU();
151 Standard_Real aKoefV = aPI.GetParameterV();
152 Standard_Real aUmin,aUmax,aVmin,aVmax;
153 ShapeAnalysis::GetFaceUVBounds(aFace,aUmin,aUmax,aVmin,aVmax);
154 Standard_Real aDeltaU = aUmax - aUmin;
155 Standard_Real aDeltaV = aVmax - aVmin;
156 Standard_Real aParamU = aUmin + aDeltaU*aKoefU;
157 Standard_Real aParamV = aVmin + aDeltaV*aKoefV;
158 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
160 Standard_TypeMismatch::Raise("Plane was not created.Base surface is absent");
163 aSurf->D1(aParamU,aParamV,aPLoc,aVecU,aVecV);
164 BRepTopAdaptor_FClass2d clas(aFace,Precision::PConfusion());
166 TopAbs_State stOut= clas.PerformInfinitePoint();
167 gp_Pnt2d aP2d(aParamU,aParamV);
168 TopAbs_State st= clas.Perform(aP2d);
170 Standard_TypeMismatch::Raise("Plane was not created.Point lies outside the face");
171 gp_Vec aNorm = aVecU^aVecV;
172 gp_Ax3 anAxis(aPLoc,gp_Dir(aNorm),gp_Dir(aVecU));
173 gp_Pln aPlane(anAxis);
174 BRepBuilderAPI_MakeFace aTool(aPlane, -aSize, +aSize, -aSize, +aSize);
176 aShape = aTool.Shape();
178 else if (aType == PLANE_2_VEC) {
179 Handle(GEOM_Function) aRefVec1 = aPI.GetVector1();
180 Handle(GEOM_Function) aRefVec2 = aPI.GetVector2();
181 TopoDS_Shape aShape1 = aRefVec1->GetValue();
182 TopoDS_Shape aShape2 = aRefVec2->GetValue();
183 if (aShape1.ShapeType() != TopAbs_EDGE ||
184 aShape2.ShapeType() != TopAbs_EDGE) return 0;
185 TopoDS_Edge aVectX = TopoDS::Edge(aShape1);
186 TopoDS_Edge aVectZ = TopoDS::Edge(aShape2);
188 TopoDS_Vertex VX1, VX2, VZ1, VZ2;
189 TopExp::Vertices( aVectX, VX1, VX2, Standard_True );
190 TopExp::Vertices( aVectZ, VZ1, VZ2, Standard_True );
192 gp_Vec aVX = gp_Vec( BRep_Tool::Pnt( VX1 ), BRep_Tool::Pnt( VX2 ) );
193 gp_Vec aVZ = gp_Vec( BRep_Tool::Pnt( VZ1 ), BRep_Tool::Pnt( VZ2 ) );
195 if ( aVX.Magnitude() < Precision::Confusion() || aVZ.Magnitude() < Precision::Confusion())
196 Standard_TypeMismatch::Raise("Invalid vector selected");
198 gp_Dir aDirX = gp_Dir( aVX.X(), aVX.Y(), aVX.Z() );
199 gp_Dir aDirZ = gp_Dir( aVZ.X(), aVZ.Y(), aVZ.Z() );
201 if ( aDirX.IsParallel( aDirZ, Precision::Angular() ) )
202 Standard_TypeMismatch::Raise("Parallel vectors selected");
204 gp_Ax3 aPlane = gp_Ax3( BRep_Tool::Pnt( VX1 ), aDirZ, aDirX );
205 BRepBuilderAPI_MakeFace aTool(aPlane, -aSize, +aSize, -aSize, +aSize);
207 aShape = aTool.Shape();
208 } else if (aType == PLANE_LCS) {
209 Handle(GEOM_Function) aRef = aPI.GetLCS();
210 double anOrientation = aPI.GetOrientation();
213 gp_Ax2 anAx2 = gp::XOY();
214 anAx3 = gp_Ax3( anAx2 );
216 TopoDS_Shape aRefShape = aRef->GetValue();
217 if (aRefShape.ShapeType() != TopAbs_FACE)
219 anAx3 = GEOMImpl_IMeasureOperations::GetPosition(aRefShape);
222 if ( anOrientation == 2)
223 anAx3 = gp_Ax3(anAx3.Location(), anAx3.XDirection(), anAx3.YDirection() );
224 else if ( anOrientation == 3 )
225 anAx3 = gp_Ax3(anAx3.Location(), anAx3.YDirection(), anAx3.XDirection() );
228 aShape = BRepBuilderAPI_MakeFace(aPln, -aSize, +aSize, -aSize, +aSize).Shape();
233 if (aShape.IsNull()) return 0;
235 aFunction->SetValue(aShape);
237 log.SetTouched(Label());
243 //=======================================================================
244 //function : GEOMImpl_PlaneDriver_Type_
246 //=======================================================================
247 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PlaneDriver_Type_()
250 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
251 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
252 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
253 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
254 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
255 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
258 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
259 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_PlaneDriver",
260 sizeof(GEOMImpl_PlaneDriver),
262 (Standard_Address)_Ancestors,
263 (Standard_Address)NULL);
268 //=======================================================================
269 //function : DownCast
271 //=======================================================================
272 const Handle(GEOMImpl_PlaneDriver) Handle(GEOMImpl_PlaneDriver)::DownCast
273 (const Handle(Standard_Transient)& AnObject)
275 Handle(GEOMImpl_PlaneDriver) _anOtherObject;
277 if (!AnObject.IsNull()) {
278 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PlaneDriver))) {
279 _anOtherObject = Handle(GEOMImpl_PlaneDriver)((Handle(GEOMImpl_PlaneDriver)&)AnObject);
283 return _anOtherObject ;