1 // Copyright (C) 2007-2020 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>
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(Handle(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.Value(),
129 -aSize, +aSize, -aSize, +aSize,
130 Precision::Confusion()).Shape();
131 } else if (aType == PLANE_FACE) {
132 Handle(GEOM_Function) aRef = aPI.GetFace();
133 TopoDS_Shape aRefShape = aRef->GetValue();
134 //if (aRefShape.ShapeType() != TopAbs_FACE) return 0;
135 //Handle(Geom_Surface) aGS = BRep_Tool::Surface(TopoDS::Face(aRefShape));
136 //if (!aGS->IsKind(STANDARD_TYPE(Geom_Plane))) {
137 // Standard_TypeMismatch::Raise("Plane creation aborted: non-planar face given as argument");
139 //aShape = BRepBuilderAPI_MakeFace(aGS, -aSize, +aSize, -aSize, +aSize).Shape();
140 gp_Ax3 anAx3 = GEOMUtils::GetPosition(aRefShape);
142 aShape = BRepBuilderAPI_MakeFace(aPln, -aSize, +aSize, -aSize, +aSize).Shape();
144 else if (aType == PLANE_TANGENT_FACE)
146 Handle(GEOM_Function) aRefFace = aPI.GetFace();
147 TopoDS_Shape aShape1 = aRefFace->GetValue();
149 Standard_TypeMismatch::Raise("Plane was not created.Basis face was not specified");
150 TopoDS_Face aFace = TopoDS::Face(aShape1);
152 Standard_Real aKoefU = aPI.GetParameterU();
153 Standard_Real aKoefV = aPI.GetParameterV();
154 Standard_Real aUmin,aUmax,aVmin,aVmax;
155 ShapeAnalysis::GetFaceUVBounds(aFace,aUmin,aUmax,aVmin,aVmax);
156 Standard_Real aDeltaU = aUmax - aUmin;
157 Standard_Real aDeltaV = aVmax - aVmin;
158 Standard_Real aParamU = aUmin + aDeltaU*aKoefU;
159 Standard_Real aParamV = aVmin + aDeltaV*aKoefV;
160 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
162 Standard_TypeMismatch::Raise("Plane was not created.Base surface is absent");
165 aSurf->D1(aParamU,aParamV,aPLoc,aVecU,aVecV);
166 BRepTopAdaptor_FClass2d clas(aFace,Precision::PConfusion());
168 TopAbs_State stOut= clas.PerformInfinitePoint();
169 gp_Pnt2d aP2d(aParamU,aParamV);
170 TopAbs_State st= clas.Perform(aP2d);
172 Standard_TypeMismatch::Raise("Plane was not created.Point lies outside the face");
173 gp_Vec aNorm = aVecU^aVecV;
174 gp_Ax3 anAxis(aPLoc,gp_Dir(aNorm),gp_Dir(aVecU));
175 gp_Pln aPlane(anAxis);
176 BRepBuilderAPI_MakeFace aTool(aPlane, -aSize, +aSize, -aSize, +aSize);
178 aShape = aTool.Shape();
180 else if (aType == PLANE_2_VEC) {
181 Handle(GEOM_Function) aRefVec1 = aPI.GetVector1();
182 Handle(GEOM_Function) aRefVec2 = aPI.GetVector2();
183 TopoDS_Shape aShape1 = aRefVec1->GetValue();
184 TopoDS_Shape aShape2 = aRefVec2->GetValue();
185 if (aShape1.ShapeType() != TopAbs_EDGE ||
186 aShape2.ShapeType() != TopAbs_EDGE) return 0;
187 TopoDS_Edge aVectX = TopoDS::Edge(aShape1);
188 TopoDS_Edge aVectZ = TopoDS::Edge(aShape2);
190 TopoDS_Vertex VX1, VX2, VZ1, VZ2;
191 TopExp::Vertices( aVectX, VX1, VX2, Standard_True );
192 TopExp::Vertices( aVectZ, VZ1, VZ2, Standard_True );
194 gp_Vec aVX = gp_Vec( BRep_Tool::Pnt( VX1 ), BRep_Tool::Pnt( VX2 ) );
195 gp_Vec aVZ = gp_Vec( BRep_Tool::Pnt( VZ1 ), BRep_Tool::Pnt( VZ2 ) );
197 if ( aVX.Magnitude() < Precision::Confusion() || aVZ.Magnitude() < Precision::Confusion())
198 Standard_TypeMismatch::Raise("Invalid vector selected");
200 gp_Dir aDirX = gp_Dir( aVX.X(), aVX.Y(), aVX.Z() );
201 gp_Dir aDirZ = gp_Dir( aVZ.X(), aVZ.Y(), aVZ.Z() );
203 if ( aDirX.IsParallel( aDirZ, Precision::Angular() ) )
204 Standard_TypeMismatch::Raise("Parallel vectors selected");
206 gp_Ax3 aPlane = gp_Ax3( BRep_Tool::Pnt( VX1 ), aDirZ, aDirX );
207 BRepBuilderAPI_MakeFace aTool(aPlane, -aSize, +aSize, -aSize, +aSize);
209 aShape = aTool.Shape();
210 } else if (aType == PLANE_LCS) {
211 Handle(GEOM_Function) aRef = aPI.GetLCS();
212 double anOrientation = aPI.GetOrientation();
215 gp_Ax2 anAx2 = gp::XOY();
216 anAx3 = gp_Ax3( anAx2 );
218 TopoDS_Shape aRefShape = aRef->GetValue();
219 if (aRefShape.ShapeType() != TopAbs_FACE)
221 anAx3 = GEOMUtils::GetPosition(aRefShape);
224 if ( anOrientation == 2)
225 anAx3 = gp_Ax3(anAx3.Location(), anAx3.XDirection(), anAx3.YDirection() );
226 else if ( anOrientation == 3 )
227 anAx3 = gp_Ax3(anAx3.Location(), anAx3.YDirection(), anAx3.XDirection() );
230 aShape = BRepBuilderAPI_MakeFace(aPln, -aSize, +aSize, -aSize, +aSize).Shape();
235 if (aShape.IsNull()) return 0;
237 aFunction->SetValue(aShape);
239 log->SetTouched(Label());
245 //================================================================================
247 * \brief Returns a name of creation operation and names and values of creation parameters
249 //================================================================================
251 bool GEOMImpl_PlaneDriver::
252 GetCreationInformation(std::string& theOperationName,
253 std::vector<GEOM_Param>& theParams)
255 if (Label().IsNull()) return 0;
256 Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
258 GEOMImpl_IPlane aCI( function );
259 Standard_Integer aType = function->GetType();
261 theOperationName = "PLANE";
265 AddParam( theParams, "Point", aCI.GetPoint() );
266 AddParam( theParams, "Vector", aCI.GetVector() );
267 AddParam( theParams, "Size of plane", aCI.GetSize() );
269 case PLANE_THREE_PNT:
270 AddParam( theParams, "Point 1", aCI.GetPoint1() );
271 AddParam( theParams, "Point 2", aCI.GetPoint2() );
272 AddParam( theParams, "Point 3", aCI.GetPoint3() );
273 AddParam( theParams, "Size of plane", aCI.GetSize() );
276 AddParam( theParams, "Face", aCI.GetFace() );
277 AddParam( theParams, "Size of plane", aCI.GetSize() );
279 case PLANE_TANGENT_FACE:
280 AddParam( theParams, "Face", aCI.GetFace() );
281 AddParam( theParams, "Parameter U", aCI.GetParameterU() );
282 AddParam( theParams, "Parameter V", aCI.GetParameterV() );
283 AddParam( theParams, "Size of plane", aCI.GetSize() );
286 AddParam( theParams, "Vector 1", aCI.GetVector1() );
287 AddParam( theParams, "Vector 2", aCI.GetVector2() );
288 AddParam( theParams, "Size of plane", aCI.GetSize() );
291 AddParam( theParams, "Local coordinate system", aCI.GetLCS() );
292 AddParam( theParams, "Orientation", aCI.GetOrientation() );
293 AddParam( theParams, "Size of plane", aCI.GetSize() );
302 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_PlaneDriver,GEOM_BaseDriver)