Back to index

salome-geom  6.5.0
GEOMImpl_PlaneDriver.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #include <Standard_Stream.hxx>
00024 
00025 #include <GEOMImpl_PlaneDriver.hxx>
00026 #include <GEOMImpl_IPlane.hxx>
00027 #include <GEOMImpl_Types.hxx>
00028 #include <GEOM_Function.hxx>
00029 
00030 #include <GEOMImpl_IMeasureOperations.hxx>
00031 
00032 #include <Basics_OCCTVersion.hxx>
00033 
00034 // OCCT Includes
00035 #include <BRepBuilderAPI_MakeFace.hxx>
00036 #include <BRep_Tool.hxx>
00037 #include <BRepTopAdaptor_FClass2d.hxx>
00038 #include <ShapeAnalysis.hxx>
00039 
00040 #include <TopAbs.hxx>
00041 #include <TopoDS.hxx>
00042 #include <TopoDS_Shape.hxx>
00043 #include <TopoDS_Edge.hxx>
00044 #include <TopoDS_Vertex.hxx>
00045 #include <TopExp.hxx>
00046 
00047 #include <GC_MakePlane.hxx>
00048 #include <Geom_Surface.hxx>
00049 
00050 #include <Precision.hxx>
00051 #include <gp_Pnt.hxx>
00052 #include <gp_Pln.hxx>
00053 #include <gp_Vec.hxx>
00054 #include <gp_Dir.hxx>
00055 #include <gp_Ax3.hxx>
00056 
00057 #include <Standard_ConstructionError.hxx>
00058 #include <Standard_TypeMismatch.hxx>
00059 
00060 //=======================================================================
00061 //function : GetID
00062 //purpose  :
00063 //=======================================================================
00064 const Standard_GUID& GEOMImpl_PlaneDriver::GetID()
00065 {
00066   static Standard_GUID aPlaneDriver("FF1BBB05-5D14-4df2-980B-3A668264EA16");
00067   return aPlaneDriver;
00068 }
00069 
00070 
00071 //=======================================================================
00072 //function : GEOMImpl_PlaneDriver
00073 //purpose  :
00074 //=======================================================================
00075 GEOMImpl_PlaneDriver::GEOMImpl_PlaneDriver()
00076 {
00077 }
00078 
00079 //=======================================================================
00080 //function : Execute
00081 //purpose  :
00082 //=======================================================================
00083 Standard_Integer GEOMImpl_PlaneDriver::Execute(TFunction_Logbook& log) const
00084 {
00085   if (Label().IsNull())  return 0;
00086   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
00087 
00088   GEOMImpl_IPlane aPI (aFunction);
00089   Standard_Integer aType = aFunction->GetType();
00090 
00091   TopoDS_Shape aShape;
00092 
00093   double aSize = aPI.GetSize() / 2.0;
00094   if (aType == PLANE_PNT_VEC) {
00095     Handle(GEOM_Function) aRefPnt = aPI.GetPoint();
00096     Handle(GEOM_Function) aRefVec = aPI.GetVector();
00097     TopoDS_Shape aShape1 = aRefPnt->GetValue();
00098     TopoDS_Shape aShape2 = aRefVec->GetValue();
00099     if (aShape1.ShapeType() != TopAbs_VERTEX ||
00100         aShape2.ShapeType() != TopAbs_EDGE) return 0;
00101     gp_Pnt aP = BRep_Tool::Pnt(TopoDS::Vertex(aShape1));
00102     TopoDS_Edge anE = TopoDS::Edge(aShape2);
00103     TopoDS_Vertex V1, V2;
00104     TopExp::Vertices(anE, V1, V2, Standard_True);
00105     if (!V1.IsNull() && !V2.IsNull()) {
00106       gp_Vec aV (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
00107       gp_Pln aPln (aP, aV);
00108       aShape = BRepBuilderAPI_MakeFace(aPln, -aSize, +aSize, -aSize, +aSize).Shape();
00109     }
00110   } else if (aType == PLANE_THREE_PNT) {
00111     Handle(GEOM_Function) aRefPnt1 = aPI.GetPoint1();
00112     Handle(GEOM_Function) aRefPnt2 = aPI.GetPoint2();
00113     Handle(GEOM_Function) aRefPnt3 = aPI.GetPoint3();
00114     TopoDS_Shape aShape1 = aRefPnt1->GetValue();
00115     TopoDS_Shape aShape2 = aRefPnt2->GetValue();
00116     TopoDS_Shape aShape3 = aRefPnt3->GetValue();
00117     if (aShape1.ShapeType() != TopAbs_VERTEX ||
00118         aShape2.ShapeType() != TopAbs_VERTEX ||
00119         aShape3.ShapeType() != TopAbs_VERTEX) return 0;
00120     gp_Pnt aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aShape1));
00121     gp_Pnt aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aShape2));
00122     gp_Pnt aP3 = BRep_Tool::Pnt(TopoDS::Vertex(aShape3));
00123     if (aP1.Distance(aP2) < gp::Resolution() ||
00124         aP1.Distance(aP3) < gp::Resolution() ||
00125         aP2.Distance(aP3) < gp::Resolution())
00126       Standard_ConstructionError::Raise("Plane creation aborted: coincident points given");
00127     if (gp_Vec(aP1, aP2).IsParallel(gp_Vec(aP1, aP3), Precision::Angular()))
00128       Standard_ConstructionError::Raise("Plane creation aborted: points lay on one line");
00129     GC_MakePlane aMakePlane (aP1, aP2, aP3);
00130 #if OCC_VERSION_LARGE > 0x06050100 // for OCC-6.5.2 and higher version
00131     aShape = BRepBuilderAPI_MakeFace(aMakePlane, -aSize, +aSize, -aSize, +aSize,
00132                                      Precision::Confusion()).Shape();
00133 #else
00134     aShape = BRepBuilderAPI_MakeFace(aMakePlane, -aSize, +aSize, -aSize, +aSize).Shape();
00135 #endif
00136   } else if (aType == PLANE_FACE) {
00137     Handle(GEOM_Function) aRef = aPI.GetFace();
00138     TopoDS_Shape aRefShape = aRef->GetValue();
00139     //if (aRefShape.ShapeType() != TopAbs_FACE) return 0;
00140     //Handle(Geom_Surface) aGS = BRep_Tool::Surface(TopoDS::Face(aRefShape));
00141     //if (!aGS->IsKind(STANDARD_TYPE(Geom_Plane))) {
00142     //  Standard_TypeMismatch::Raise("Plane creation aborted: non-planar face given as argument");
00143     //}
00144     //aShape = BRepBuilderAPI_MakeFace(aGS, -aSize, +aSize, -aSize, +aSize).Shape();
00145     gp_Ax3 anAx3 = GEOMImpl_IMeasureOperations::GetPosition(aRefShape);
00146     gp_Pln aPln (anAx3);
00147     aShape = BRepBuilderAPI_MakeFace(aPln, -aSize, +aSize, -aSize, +aSize).Shape();
00148   }
00149   else if (aType == PLANE_TANGENT_FACE)
00150   {
00151     Handle(GEOM_Function) aRefFace = aPI.GetFace();
00152     TopoDS_Shape aShape1 = aRefFace->GetValue();
00153     if(aShape1.IsNull())
00154       Standard_TypeMismatch::Raise("Plane was not created.Basis face was not specified");
00155     TopoDS_Face aFace = TopoDS::Face(aShape1);
00156 
00157     Standard_Real aKoefU = aPI.GetParameterU();
00158     Standard_Real aKoefV = aPI.GetParameterV();
00159     Standard_Real aUmin,aUmax,aVmin,aVmax;
00160     ShapeAnalysis::GetFaceUVBounds(aFace,aUmin,aUmax,aVmin,aVmax);
00161     Standard_Real aDeltaU = aUmax - aUmin;
00162     Standard_Real aDeltaV = aVmax - aVmin;
00163     Standard_Real aParamU =  aUmin + aDeltaU*aKoefU;
00164     Standard_Real aParamV =  aVmin + aDeltaV*aKoefV;
00165     Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
00166     if(aSurf.IsNull())
00167       Standard_TypeMismatch::Raise("Plane was not created.Base surface is absent");
00168     gp_Vec aVecU,aVecV;
00169     gp_Pnt aPLoc;
00170     aSurf->D1(aParamU,aParamV,aPLoc,aVecU,aVecV);
00171     BRepTopAdaptor_FClass2d clas(aFace,Precision::PConfusion());
00172 
00173     TopAbs_State stOut= clas.PerformInfinitePoint();
00174     gp_Pnt2d aP2d(aParamU,aParamV);
00175     TopAbs_State st= clas.Perform(aP2d);
00176     if(st == stOut)
00177       Standard_TypeMismatch::Raise("Plane was not created.Point lies outside the face");
00178     gp_Vec aNorm = aVecU^aVecV;
00179     gp_Ax3 anAxis(aPLoc,gp_Dir(aNorm),gp_Dir(aVecU));
00180     gp_Pln aPlane(anAxis);
00181     BRepBuilderAPI_MakeFace aTool(aPlane, -aSize, +aSize, -aSize, +aSize);
00182     if(aTool.IsDone())
00183       aShape = aTool.Shape();
00184   }
00185   else if (aType == PLANE_2_VEC) {
00186     Handle(GEOM_Function) aRefVec1 = aPI.GetVector1();
00187     Handle(GEOM_Function) aRefVec2 = aPI.GetVector2();
00188     TopoDS_Shape aShape1 = aRefVec1->GetValue();
00189     TopoDS_Shape aShape2 = aRefVec2->GetValue();
00190     if (aShape1.ShapeType() != TopAbs_EDGE ||
00191         aShape2.ShapeType() != TopAbs_EDGE) return 0;
00192     TopoDS_Edge aVectX = TopoDS::Edge(aShape1);
00193     TopoDS_Edge aVectZ = TopoDS::Edge(aShape2);
00194 
00195     TopoDS_Vertex VX1, VX2, VZ1, VZ2;
00196     TopExp::Vertices( aVectX, VX1, VX2, Standard_True );
00197     TopExp::Vertices( aVectZ, VZ1, VZ2, Standard_True );
00198 
00199     gp_Vec aVX = gp_Vec( BRep_Tool::Pnt( VX1 ), BRep_Tool::Pnt( VX2 ) );
00200     gp_Vec aVZ = gp_Vec( BRep_Tool::Pnt( VZ1 ), BRep_Tool::Pnt( VZ2 ) );
00201 
00202     if ( aVX.Magnitude() < Precision::Confusion() || aVZ.Magnitude() < Precision::Confusion())
00203       Standard_TypeMismatch::Raise("Invalid vector selected");
00204 
00205     gp_Dir aDirX = gp_Dir( aVX.X(), aVX.Y(), aVX.Z() );
00206     gp_Dir aDirZ = gp_Dir( aVZ.X(), aVZ.Y(), aVZ.Z() );
00207 
00208     if ( aDirX.IsParallel( aDirZ, Precision::Angular() ) )
00209       Standard_TypeMismatch::Raise("Parallel vectors selected");
00210 
00211     gp_Ax3 aPlane = gp_Ax3( BRep_Tool::Pnt( VX1 ), aDirZ, aDirX );
00212     BRepBuilderAPI_MakeFace aTool(aPlane, -aSize, +aSize, -aSize, +aSize);
00213     if(aTool.IsDone())
00214       aShape = aTool.Shape();
00215   }   else if (aType == PLANE_LCS) {
00216     Handle(GEOM_Function) aRef = aPI.GetLCS();
00217     double anOrientation = aPI.GetOrientation();    
00218     gp_Ax3 anAx3;
00219     if (aRef.IsNull()) {
00220       gp_Ax2 anAx2 = gp::XOY();
00221       anAx3 = gp_Ax3( anAx2 );
00222     } else {
00223       TopoDS_Shape aRefShape = aRef->GetValue();
00224       if (aRefShape.ShapeType() != TopAbs_FACE)
00225         return 0;
00226       anAx3 = GEOMImpl_IMeasureOperations::GetPosition(aRefShape);
00227     }
00228 
00229     if ( anOrientation == 2)
00230       anAx3 = gp_Ax3(anAx3.Location(), anAx3.XDirection(), anAx3.YDirection() );
00231     else if ( anOrientation == 3 )
00232       anAx3 = gp_Ax3(anAx3.Location(), anAx3.YDirection(), anAx3.XDirection() );
00233 
00234     gp_Pln aPln(anAx3);
00235     aShape = BRepBuilderAPI_MakeFace(aPln, -aSize, +aSize, -aSize, +aSize).Shape();
00236   }
00237   else {
00238   }
00239 
00240   if (aShape.IsNull()) return 0;
00241 
00242   aFunction->SetValue(aShape);
00243 
00244   log.SetTouched(Label());
00245 
00246   return 1;
00247 }
00248 
00249 
00250 //=======================================================================
00251 //function :  GEOMImpl_PlaneDriver_Type_
00252 //purpose  :
00253 //=======================================================================
00254 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PlaneDriver_Type_()
00255 {
00256 
00257   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
00258   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
00259   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
00260   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
00261   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
00262   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
00263 
00264 
00265   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
00266   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_PlaneDriver",
00267                                                          sizeof(GEOMImpl_PlaneDriver),
00268                                                          1,
00269                                                          (Standard_Address)_Ancestors,
00270                                                          (Standard_Address)NULL);
00271 
00272   return _aType;
00273 }
00274 
00275 //=======================================================================
00276 //function : DownCast
00277 //purpose  :
00278 //=======================================================================
00279 const Handle(GEOMImpl_PlaneDriver) Handle(GEOMImpl_PlaneDriver)::DownCast
00280        (const Handle(Standard_Transient)& AnObject)
00281 {
00282   Handle(GEOMImpl_PlaneDriver) _anOtherObject;
00283 
00284   if (!AnObject.IsNull()) {
00285      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PlaneDriver))) {
00286        _anOtherObject = Handle(GEOMImpl_PlaneDriver)((Handle(GEOMImpl_PlaneDriver)&)AnObject);
00287      }
00288   }
00289 
00290   return _anOtherObject ;
00291 }