1 // Copyright (C) 2007-2016 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(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.Value(),
131 -aSize, +aSize, -aSize, +aSize,
132 Precision::Confusion()).Shape();
133 } else if (aType == PLANE_FACE) {
134 Handle(GEOM_Function) aRef = aPI.GetFace();
135 TopoDS_Shape aRefShape = aRef->GetValue();
136 //if (aRefShape.ShapeType() != TopAbs_FACE) return 0;
137 //Handle(Geom_Surface) aGS = BRep_Tool::Surface(TopoDS::Face(aRefShape));
138 //if (!aGS->IsKind(STANDARD_TYPE(Geom_Plane))) {
139 // Standard_TypeMismatch::Raise("Plane creation aborted: non-planar face given as argument");
141 //aShape = BRepBuilderAPI_MakeFace(aGS, -aSize, +aSize, -aSize, +aSize).Shape();
142 gp_Ax3 anAx3 = GEOMUtils::GetPosition(aRefShape);
144 aShape = BRepBuilderAPI_MakeFace(aPln, -aSize, +aSize, -aSize, +aSize).Shape();
146 else if (aType == PLANE_TANGENT_FACE)
148 Handle(GEOM_Function) aRefFace = aPI.GetFace();
149 TopoDS_Shape aShape1 = aRefFace->GetValue();
151 Standard_TypeMismatch::Raise("Plane was not created.Basis face was not specified");
152 TopoDS_Face aFace = TopoDS::Face(aShape1);
154 Standard_Real aKoefU = aPI.GetParameterU();
155 Standard_Real aKoefV = aPI.GetParameterV();
156 Standard_Real aUmin,aUmax,aVmin,aVmax;
157 ShapeAnalysis::GetFaceUVBounds(aFace,aUmin,aUmax,aVmin,aVmax);
158 Standard_Real aDeltaU = aUmax - aUmin;
159 Standard_Real aDeltaV = aVmax - aVmin;
160 Standard_Real aParamU = aUmin + aDeltaU*aKoefU;
161 Standard_Real aParamV = aVmin + aDeltaV*aKoefV;
162 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
164 Standard_TypeMismatch::Raise("Plane was not created.Base surface is absent");
167 aSurf->D1(aParamU,aParamV,aPLoc,aVecU,aVecV);
168 BRepTopAdaptor_FClass2d clas(aFace,Precision::PConfusion());
170 TopAbs_State stOut= clas.PerformInfinitePoint();
171 gp_Pnt2d aP2d(aParamU,aParamV);
172 TopAbs_State st= clas.Perform(aP2d);
174 Standard_TypeMismatch::Raise("Plane was not created.Point lies outside the face");
175 gp_Vec aNorm = aVecU^aVecV;
176 gp_Ax3 anAxis(aPLoc,gp_Dir(aNorm),gp_Dir(aVecU));
177 gp_Pln aPlane(anAxis);
178 BRepBuilderAPI_MakeFace aTool(aPlane, -aSize, +aSize, -aSize, +aSize);
180 aShape = aTool.Shape();
182 else if (aType == PLANE_2_VEC) {
183 Handle(GEOM_Function) aRefVec1 = aPI.GetVector1();
184 Handle(GEOM_Function) aRefVec2 = aPI.GetVector2();
185 TopoDS_Shape aShape1 = aRefVec1->GetValue();
186 TopoDS_Shape aShape2 = aRefVec2->GetValue();
187 if (aShape1.ShapeType() != TopAbs_EDGE ||
188 aShape2.ShapeType() != TopAbs_EDGE) return 0;
189 TopoDS_Edge aVectX = TopoDS::Edge(aShape1);
190 TopoDS_Edge aVectZ = TopoDS::Edge(aShape2);
192 TopoDS_Vertex VX1, VX2, VZ1, VZ2;
193 TopExp::Vertices( aVectX, VX1, VX2, Standard_True );
194 TopExp::Vertices( aVectZ, VZ1, VZ2, Standard_True );
196 gp_Vec aVX = gp_Vec( BRep_Tool::Pnt( VX1 ), BRep_Tool::Pnt( VX2 ) );
197 gp_Vec aVZ = gp_Vec( BRep_Tool::Pnt( VZ1 ), BRep_Tool::Pnt( VZ2 ) );
199 if ( aVX.Magnitude() < Precision::Confusion() || aVZ.Magnitude() < Precision::Confusion())
200 Standard_TypeMismatch::Raise("Invalid vector selected");
202 gp_Dir aDirX = gp_Dir( aVX.X(), aVX.Y(), aVX.Z() );
203 gp_Dir aDirZ = gp_Dir( aVZ.X(), aVZ.Y(), aVZ.Z() );
205 if ( aDirX.IsParallel( aDirZ, Precision::Angular() ) )
206 Standard_TypeMismatch::Raise("Parallel vectors selected");
208 gp_Ax3 aPlane = gp_Ax3( BRep_Tool::Pnt( VX1 ), aDirZ, aDirX );
209 BRepBuilderAPI_MakeFace aTool(aPlane, -aSize, +aSize, -aSize, +aSize);
211 aShape = aTool.Shape();
212 } else if (aType == PLANE_LCS) {
213 Handle(GEOM_Function) aRef = aPI.GetLCS();
214 double anOrientation = aPI.GetOrientation();
217 gp_Ax2 anAx2 = gp::XOY();
218 anAx3 = gp_Ax3( anAx2 );
220 TopoDS_Shape aRefShape = aRef->GetValue();
221 if (aRefShape.ShapeType() != TopAbs_FACE)
223 anAx3 = GEOMUtils::GetPosition(aRefShape);
226 if ( anOrientation == 2)
227 anAx3 = gp_Ax3(anAx3.Location(), anAx3.XDirection(), anAx3.YDirection() );
228 else if ( anOrientation == 3 )
229 anAx3 = gp_Ax3(anAx3.Location(), anAx3.YDirection(), anAx3.XDirection() );
232 aShape = BRepBuilderAPI_MakeFace(aPln, -aSize, +aSize, -aSize, +aSize).Shape();
237 if (aShape.IsNull()) return 0;
239 aFunction->SetValue(aShape);
241 #if OCC_VERSION_MAJOR < 7
242 log.SetTouched(Label());
244 log->SetTouched(Label());
251 //================================================================================
253 * \brief Returns a name of creation operation and names and values of creation parameters
255 //================================================================================
257 bool GEOMImpl_PlaneDriver::
258 GetCreationInformation(std::string& theOperationName,
259 std::vector<GEOM_Param>& theParams)
261 if (Label().IsNull()) return 0;
262 Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
264 GEOMImpl_IPlane aCI( function );
265 Standard_Integer aType = function->GetType();
267 theOperationName = "PLANE";
271 AddParam( theParams, "Point", aCI.GetPoint() );
272 AddParam( theParams, "Vector", aCI.GetVector() );
273 AddParam( theParams, "Size of plane", aCI.GetSize() );
275 case PLANE_THREE_PNT:
276 AddParam( theParams, "Point 1", aCI.GetPoint1() );
277 AddParam( theParams, "Point 2", aCI.GetPoint2() );
278 AddParam( theParams, "Point 3", aCI.GetPoint3() );
279 AddParam( theParams, "Size of plane", aCI.GetSize() );
282 AddParam( theParams, "Face", aCI.GetFace() );
283 AddParam( theParams, "Size of plane", aCI.GetSize() );
285 case PLANE_TANGENT_FACE:
286 AddParam( theParams, "Face", aCI.GetFace() );
287 AddParam( theParams, "Parameter U", aCI.GetParameterU() );
288 AddParam( theParams, "Parameter V", aCI.GetParameterV() );
289 AddParam( theParams, "Size of plane", aCI.GetSize() );
292 AddParam( theParams, "Vector 1", aCI.GetVector1() );
293 AddParam( theParams, "Vector 2", aCI.GetVector2() );
294 AddParam( theParams, "Size of plane", aCI.GetSize() );
297 AddParam( theParams, "Local coordinate system", aCI.GetLCS() );
298 AddParam( theParams, "Orientation", aCI.GetOrientation() );
299 AddParam( theParams, "Size of plane", aCI.GetSize() );
308 OCCT_IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_PlaneDriver,GEOM_BaseDriver);