Back to index

salome-geom  6.5.0
GEOMImpl_LineDriver.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_LineDriver.hxx>
00026 #include <GEOMImpl_ILine.hxx>
00027 #include <GEOMImpl_Types.hxx>
00028 #include <GEOM_Function.hxx>
00029 
00030 #include <BRep_Tool.hxx>
00031 #include <BRepBuilderAPI_MakeEdge.hxx>
00032 #include <BRepBuilderAPI_MakeVertex.hxx>
00033 #include <BRepAlgoAPI_Section.hxx>
00034 #include <TopAbs.hxx>
00035 #include <TopExp.hxx>
00036 #include <TopoDS.hxx>
00037 #include <TopoDS_Edge.hxx>
00038 #include <TopoDS_Shape.hxx>
00039 #include <TopoDS_Vertex.hxx>
00040 #include <TopExp_Explorer.hxx>
00041 #include <TopTools_MapOfShape.hxx>
00042 
00043 #include <gp_Pnt.hxx>
00044 #include <Precision.hxx>
00045 #include <Standard_ConstructionError.hxx>
00046 #include <Standard_NullObject.hxx>
00047 
00048 //=======================================================================
00049 //function : GetID
00050 //purpose  :
00051 //=======================================================================
00052 const Standard_GUID& GEOMImpl_LineDriver::GetID()
00053 {
00054   static Standard_GUID aLineDriver("FF1BBB06-5D14-4df2-980B-3A668264EA16");
00055   return aLineDriver;
00056 }
00057 
00058 
00059 //=======================================================================
00060 //function : GEOMImpl_LineDriver
00061 //purpose  :
00062 //=======================================================================
00063 GEOMImpl_LineDriver::GEOMImpl_LineDriver()
00064 {
00065 }
00066 
00067 //=======================================================================
00068 //function : Execute
00069 //purpose  :
00070 //=======================================================================
00071 Standard_Integer GEOMImpl_LineDriver::Execute(TFunction_Logbook& log) const
00072 {
00073   if (Label().IsNull())  return 0;
00074   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
00075 
00076   GEOMImpl_ILine aPI (aFunction);
00077   Standard_Integer aType = aFunction->GetType();
00078 
00079   TopoDS_Shape aShape;
00080 
00081   if (aType == LINE_TWO_PNT) {
00082     Handle(GEOM_Function) aRefPnt1 = aPI.GetPoint1();
00083     Handle(GEOM_Function) aRefPnt2 = aPI.GetPoint2();
00084     TopoDS_Shape aShape1 = aRefPnt1->GetValue();
00085     TopoDS_Shape aShape2 = aRefPnt2->GetValue();
00086     if (aShape1.ShapeType() != TopAbs_VERTEX ||
00087         aShape2.ShapeType() != TopAbs_VERTEX) {
00088       Standard_ConstructionError::Raise("Wrong arguments: two points must be given");
00089     }
00090     if (aShape1.IsSame(aShape2)) {
00091       Standard_ConstructionError::Raise("The end points must be different");
00092     }
00093     TopoDS_Vertex V1 = TopoDS::Vertex(aShape1);
00094     TopoDS_Vertex V2 = TopoDS::Vertex(aShape2);
00095     gp_Pnt P1 = BRep_Tool::Pnt(V1);
00096     gp_Pnt P2 = BRep_Tool::Pnt(V2);
00097     if (P1.Distance(P2) < Precision::Confusion()) {
00098       Standard_ConstructionError::Raise("The end points are too close");
00099     }
00100     aShape = BRepBuilderAPI_MakeEdge(V1, V2).Shape();
00101 
00102   } else if (aType == LINE_TWO_FACES) {
00103     Handle(GEOM_Function) aRefFace1 = aPI.GetFace1();
00104     Handle(GEOM_Function) aRefFace2 = aPI.GetFace2();
00105     TopoDS_Shape aShape1 = aRefFace1->GetValue();
00106     TopoDS_Shape aShape2 = aRefFace2->GetValue();
00107     if (aShape1.ShapeType() != TopAbs_FACE ||
00108         aShape2.ShapeType() != TopAbs_FACE) {
00109       Standard_ConstructionError::Raise("Wrong arguments: two faces must be given");
00110     }
00111     if (aShape1.IsSame(aShape2)) {
00112       Standard_ConstructionError::Raise("The end faces must be different");
00113     }
00114     BRepAlgoAPI_Section E (aShape1, aShape2, Standard_False);
00115     E.Approximation(Standard_True);
00116     E.Build();
00117     if (!E.IsDone()) {
00118       Standard_ConstructionError::Raise("Line can not be performed on the given faces");
00119     }
00120     else
00121     {
00122         TopExp_Explorer Exp (E, TopAbs_EDGE);
00123         if ( Exp.More() ){
00124             aShape = Exp.Current();
00125             Exp.Next();
00126         }
00127         else
00128           {
00129             Standard_ConstructionError::Raise("Faces not have intersection line");
00130             aShape = E.Shape();
00131           }
00132         if ( Exp.More() )
00133           aShape = E.Shape();
00134     }
00135 
00136   } else if (aType == LINE_PNT_DIR) {
00137     Handle(GEOM_Function) aRefPnt = aPI.GetPoint1();
00138     Handle(GEOM_Function) aRefDir = aPI.GetPoint2();
00139     TopoDS_Shape aShape1 = aRefPnt->GetValue();
00140     TopoDS_Shape aShape2 = aRefDir->GetValue();
00141     if (aShape1.ShapeType() != TopAbs_VERTEX) {
00142       Standard_ConstructionError::Raise("Wrong first argument: must be point");
00143     }
00144     if (aShape2.ShapeType() != TopAbs_EDGE) {
00145       Standard_ConstructionError::Raise("Wrong second argument: must be vector");
00146     }
00147     if (aShape1.IsSame(aShape2)) {
00148       Standard_ConstructionError::Raise("The end points must be different");
00149     }
00150     TopoDS_Vertex Vstart = TopoDS::Vertex(aShape1);
00151     gp_Pnt P1 = BRep_Tool::Pnt(Vstart);
00152 
00153     TopoDS_Edge anE = TopoDS::Edge(aShape2);
00154     TopoDS_Vertex V1, V2;
00155     TopExp::Vertices(anE, V1, V2, Standard_True);
00156     if (V1.IsNull() || V2.IsNull()) {
00157       Standard_NullObject::Raise("Line creation aborted: vector is not defined");
00158     }
00159     gp_Pnt PV1 = BRep_Tool::Pnt(V1);
00160     gp_Pnt PV2 = BRep_Tool::Pnt(V2);
00161     if (PV1.Distance(PV2) < Precision::Confusion()) {
00162       Standard_ConstructionError::Raise("Vector with null magnitude");
00163     }
00164     TopoDS_Vertex Vend;
00165     if ( V1.IsSame( Vstart ) )
00166       Vend = V2;
00167     else if ( V2.IsSame( Vstart ) )
00168       Vend = V1;
00169     else
00170       Vend = BRepBuilderAPI_MakeVertex( gp_Pnt(P1.XYZ() + PV2.XYZ() - PV1.XYZ()) );
00171     aShape = BRepBuilderAPI_MakeEdge(Vstart, Vend).Shape();
00172   } else {
00173   }
00174 
00175   if (aShape.IsNull()) return 0;
00176   //aShape.Infinite(true); // VSR: 05/04/2010: Fix 20668 (Fit All for points & lines)
00177 
00178   aFunction->SetValue(aShape);
00179 
00180   log.SetTouched(Label());
00181 
00182   return 1;
00183 }
00184 
00185 
00186 //=======================================================================
00187 //function :  GEOMImpl_LineDriver_Type_
00188 //purpose  :
00189 //=======================================================================
00190 Standard_EXPORT Handle_Standard_Type& GEOMImpl_LineDriver_Type_()
00191 {
00192 
00193   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
00194   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
00195   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
00196   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
00197   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
00198   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
00199 
00200 
00201   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
00202   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_LineDriver",
00203                                                          sizeof(GEOMImpl_LineDriver),
00204                                                          1,
00205                                                          (Standard_Address)_Ancestors,
00206                                                          (Standard_Address)NULL);
00207 
00208   return _aType;
00209 }
00210 
00211 //=======================================================================
00212 //function : DownCast
00213 //purpose  :
00214 //=======================================================================
00215 const Handle(GEOMImpl_LineDriver) Handle(GEOMImpl_LineDriver)::DownCast
00216        (const Handle(Standard_Transient)& AnObject)
00217 {
00218   Handle(GEOMImpl_LineDriver) _anOtherObject;
00219 
00220   if (!AnObject.IsNull()) {
00221      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_LineDriver))) {
00222        _anOtherObject = Handle(GEOMImpl_LineDriver)((Handle(GEOMImpl_LineDriver)&)AnObject);
00223      }
00224   }
00225 
00226   return _anOtherObject ;
00227 }