1 // Copyright (C) 2007-2014 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, or (at your option) any later version.
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 <GEOMUtils.hxx>
32 #include <Basics_OCCTVersion.hxx>
35 #include <BRepBuilderAPI_MakeFace.hxx>
36 #include <BRep_Tool.hxx>
37 #include <BRepTopAdaptor_FClass2d.hxx>
38 #include <ShapeAnalysis.hxx>
42 #include <TopoDS_Shape.hxx>
43 #include <TopoDS_Edge.hxx>
44 #include <TopoDS_Vertex.hxx>
47 #include <GC_MakePlane.hxx>
48 #include <Geom_Surface.hxx>
50 #include <Precision.hxx>
57 #include <Standard_ConstructionError.hxx>
58 #include <Standard_TypeMismatch.hxx>
60 //=======================================================================
63 //=======================================================================
64 const Standard_GUID& GEOMImpl_PlaneDriver::GetID()
66 static Standard_GUID aPlaneDriver("FF1BBB05-5D14-4df2-980B-3A668264EA16");
71 //=======================================================================
72 //function : GEOMImpl_PlaneDriver
74 //=======================================================================
75 GEOMImpl_PlaneDriver::GEOMImpl_PlaneDriver()
79 //=======================================================================
82 //=======================================================================
83 Standard_Integer GEOMImpl_PlaneDriver::Execute(TFunction_Logbook& log) const
85 if (Label().IsNull()) return 0;
86 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
88 GEOMImpl_IPlane aPI (aFunction);
89 Standard_Integer aType = aFunction->GetType();
93 double aSize = aPI.GetSize() / 2.0;
94 if (aType == PLANE_PNT_VEC) {
95 Handle(GEOM_Function) aRefPnt = aPI.GetPoint();
96 Handle(GEOM_Function) aRefVec = aPI.GetVector();
97 TopoDS_Shape aShape1 = aRefPnt->GetValue();
98 TopoDS_Shape aShape2 = aRefVec->GetValue();
99 if (aShape1.ShapeType() != TopAbs_VERTEX ||
100 aShape2.ShapeType() != TopAbs_EDGE) return 0;
101 gp_Pnt aP = BRep_Tool::Pnt(TopoDS::Vertex(aShape1));
102 TopoDS_Edge anE = TopoDS::Edge(aShape2);
103 TopoDS_Vertex V1, V2;
104 TopExp::Vertices(anE, V1, V2, Standard_True);
105 if (!V1.IsNull() && !V2.IsNull()) {
106 gp_Vec aV (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
107 gp_Pln aPln (aP, aV);
108 aShape = BRepBuilderAPI_MakeFace(aPln, -aSize, +aSize, -aSize, +aSize).Shape();
110 } else if (aType == PLANE_THREE_PNT) {
111 Handle(GEOM_Function) aRefPnt1 = aPI.GetPoint1();
112 Handle(GEOM_Function) aRefPnt2 = aPI.GetPoint2();
113 Handle(GEOM_Function) aRefPnt3 = aPI.GetPoint3();
114 TopoDS_Shape aShape1 = aRefPnt1->GetValue();
115 TopoDS_Shape aShape2 = aRefPnt2->GetValue();
116 TopoDS_Shape aShape3 = aRefPnt3->GetValue();
117 if (aShape1.ShapeType() != TopAbs_VERTEX ||
118 aShape2.ShapeType() != TopAbs_VERTEX ||
119 aShape3.ShapeType() != TopAbs_VERTEX) return 0;
120 gp_Pnt aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aShape1));
121 gp_Pnt aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aShape2));
122 gp_Pnt aP3 = BRep_Tool::Pnt(TopoDS::Vertex(aShape3));
123 if (aP1.Distance(aP2) < gp::Resolution() ||
124 aP1.Distance(aP3) < gp::Resolution() ||
125 aP2.Distance(aP3) < gp::Resolution())
126 Standard_ConstructionError::Raise("Plane creation aborted: coincident points given");
127 if (gp_Vec(aP1, aP2).IsParallel(gp_Vec(aP1, aP3), Precision::Angular()))
128 Standard_ConstructionError::Raise("Plane creation aborted: points lay on one line");
129 GC_MakePlane aMakePlane (aP1, aP2, aP3);
130 aShape = BRepBuilderAPI_MakeFace(aMakePlane, -aSize, +aSize, -aSize, +aSize,
131 Precision::Confusion()).Shape();
132 } else if (aType == PLANE_FACE) {
133 Handle(GEOM_Function) aRef = aPI.GetFace();
134 TopoDS_Shape aRefShape = aRef->GetValue();
135 //if (aRefShape.ShapeType() != TopAbs_FACE) return 0;
136 //Handle(Geom_Surface) aGS = BRep_Tool::Surface(TopoDS::Face(aRefShape));
137 //if (!aGS->IsKind(STANDARD_TYPE(Geom_Plane))) {
138 // Standard_TypeMismatch::Raise("Plane creation aborted: non-planar face given as argument");
140 //aShape = BRepBuilderAPI_MakeFace(aGS, -aSize, +aSize, -aSize, +aSize).Shape();
141 gp_Ax3 anAx3 = GEOMUtils::GetPosition(aRefShape);
143 aShape = BRepBuilderAPI_MakeFace(aPln, -aSize, +aSize, -aSize, +aSize).Shape();
145 else if (aType == PLANE_TANGENT_FACE)
147 Handle(GEOM_Function) aRefFace = aPI.GetFace();
148 TopoDS_Shape aShape1 = aRefFace->GetValue();
150 Standard_TypeMismatch::Raise("Plane was not created.Basis face was not specified");
151 TopoDS_Face aFace = TopoDS::Face(aShape1);
153 Standard_Real aKoefU = aPI.GetParameterU();
154 Standard_Real aKoefV = aPI.GetParameterV();
155 Standard_Real aUmin,aUmax,aVmin,aVmax;
156 ShapeAnalysis::GetFaceUVBounds(aFace,aUmin,aUmax,aVmin,aVmax);
157 Standard_Real aDeltaU = aUmax - aUmin;
158 Standard_Real aDeltaV = aVmax - aVmin;
159 Standard_Real aParamU = aUmin + aDeltaU*aKoefU;
160 Standard_Real aParamV = aVmin + aDeltaV*aKoefV;
161 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
163 Standard_TypeMismatch::Raise("Plane was not created.Base surface is absent");
166 aSurf->D1(aParamU,aParamV,aPLoc,aVecU,aVecV);
167 BRepTopAdaptor_FClass2d clas(aFace,Precision::PConfusion());
169 TopAbs_State stOut= clas.PerformInfinitePoint();
170 gp_Pnt2d aP2d(aParamU,aParamV);
171 TopAbs_State st= clas.Perform(aP2d);
173 Standard_TypeMismatch::Raise("Plane was not created.Point lies outside the face");
174 gp_Vec aNorm = aVecU^aVecV;
175 gp_Ax3 anAxis(aPLoc,gp_Dir(aNorm),gp_Dir(aVecU));
176 gp_Pln aPlane(anAxis);
177 BRepBuilderAPI_MakeFace aTool(aPlane, -aSize, +aSize, -aSize, +aSize);
179 aShape = aTool.Shape();
181 else if (aType == PLANE_2_VEC) {
182 Handle(GEOM_Function) aRefVec1 = aPI.GetVector1();
183 Handle(GEOM_Function) aRefVec2 = aPI.GetVector2();
184 TopoDS_Shape aShape1 = aRefVec1->GetValue();
185 TopoDS_Shape aShape2 = aRefVec2->GetValue();
186 if (aShape1.ShapeType() != TopAbs_EDGE ||
187 aShape2.ShapeType() != TopAbs_EDGE) return 0;
188 TopoDS_Edge aVectX = TopoDS::Edge(aShape1);
189 TopoDS_Edge aVectZ = TopoDS::Edge(aShape2);
191 TopoDS_Vertex VX1, VX2, VZ1, VZ2;
192 TopExp::Vertices( aVectX, VX1, VX2, Standard_True );
193 TopExp::Vertices( aVectZ, VZ1, VZ2, Standard_True );
195 gp_Vec aVX = gp_Vec( BRep_Tool::Pnt( VX1 ), BRep_Tool::Pnt( VX2 ) );
196 gp_Vec aVZ = gp_Vec( BRep_Tool::Pnt( VZ1 ), BRep_Tool::Pnt( VZ2 ) );
198 if ( aVX.Magnitude() < Precision::Confusion() || aVZ.Magnitude() < Precision::Confusion())
199 Standard_TypeMismatch::Raise("Invalid vector selected");
201 gp_Dir aDirX = gp_Dir( aVX.X(), aVX.Y(), aVX.Z() );
202 gp_Dir aDirZ = gp_Dir( aVZ.X(), aVZ.Y(), aVZ.Z() );
204 if ( aDirX.IsParallel( aDirZ, Precision::Angular() ) )
205 Standard_TypeMismatch::Raise("Parallel vectors selected");
207 gp_Ax3 aPlane = gp_Ax3( BRep_Tool::Pnt( VX1 ), aDirZ, aDirX );
208 BRepBuilderAPI_MakeFace aTool(aPlane, -aSize, +aSize, -aSize, +aSize);
210 aShape = aTool.Shape();
211 } else if (aType == PLANE_LCS) {
212 Handle(GEOM_Function) aRef = aPI.GetLCS();
213 double anOrientation = aPI.GetOrientation();
216 gp_Ax2 anAx2 = gp::XOY();
217 anAx3 = gp_Ax3( anAx2 );
219 TopoDS_Shape aRefShape = aRef->GetValue();
220 if (aRefShape.ShapeType() != TopAbs_FACE)
222 anAx3 = GEOMUtils::GetPosition(aRefShape);
225 if ( anOrientation == 2)
226 anAx3 = gp_Ax3(anAx3.Location(), anAx3.XDirection(), anAx3.YDirection() );
227 else if ( anOrientation == 3 )
228 anAx3 = gp_Ax3(anAx3.Location(), anAx3.YDirection(), anAx3.XDirection() );
231 aShape = BRepBuilderAPI_MakeFace(aPln, -aSize, +aSize, -aSize, +aSize).Shape();
236 if (aShape.IsNull()) return 0;
238 aFunction->SetValue(aShape);
240 log.SetTouched(Label());
246 //================================================================================
248 * \brief Returns a name of creation operation and names and values of creation parameters
250 //================================================================================
252 bool GEOMImpl_PlaneDriver::
253 GetCreationInformation(std::string& theOperationName,
254 std::vector<GEOM_Param>& theParams)
256 if (Label().IsNull()) return 0;
257 Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
259 GEOMImpl_IPlane aCI( function );
260 Standard_Integer aType = function->GetType();
262 theOperationName = "PLANE";
266 AddParam( theParams, "Point", aCI.GetPoint() );
267 AddParam( theParams, "Vector", aCI.GetVector() );
268 AddParam( theParams, "Size of plane", aCI.GetSize() );
270 case PLANE_THREE_PNT:
271 AddParam( theParams, "Point 1", aCI.GetPoint1() );
272 AddParam( theParams, "Point 2", aCI.GetPoint2() );
273 AddParam( theParams, "Point 3", aCI.GetPoint3() );
274 AddParam( theParams, "Size of plane", aCI.GetSize() );
277 AddParam( theParams, "Face", aCI.GetFace() );
278 AddParam( theParams, "Size of plane", aCI.GetSize() );
280 case PLANE_TANGENT_FACE:
281 AddParam( theParams, "Face", aCI.GetFace() );
282 AddParam( theParams, "Parameter U", aCI.GetParameterU() );
283 AddParam( theParams, "Parameter V", aCI.GetParameterV() );
284 AddParam( theParams, "Size of plane", aCI.GetSize() );
287 AddParam( theParams, "Vector 1", aCI.GetVector1() );
288 AddParam( theParams, "Vector 2", aCI.GetVector2() );
289 AddParam( theParams, "Size of plane", aCI.GetSize() );
292 AddParam( theParams, "Local coordinate system", aCI.GetLCS() );
293 AddParam( theParams, "Orientation", aCI.GetOrientation() );
294 AddParam( theParams, "Size of plane", aCI.GetSize() );
303 IMPLEMENT_STANDARD_HANDLE (GEOMImpl_PlaneDriver,GEOM_BaseDriver);
304 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_PlaneDriver,GEOM_BaseDriver);