Back to index

salome-geom  6.5.0
GEOMImpl_Fillet1dDriver.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 #include <Standard_Stream.hxx>
00021 
00022 #include <GEOMImpl_Fillet1dDriver.hxx>
00023 
00024 #include <GEOMImpl_Fillet1d.hxx>
00025 #include <GEOMImpl_IFillet1d.hxx>
00026 #include <GEOMImpl_Types.hxx>
00027 #include <GEOMImpl_ILocalOperations.hxx>
00028 #include <GEOMImpl_IShapesOperations.hxx>
00029 #include <GEOM_Function.hxx>
00030 
00031 #include <gp_Pln.hxx>
00032 #include <gp_Dir.hxx>
00033 #include <gp_XYZ.hxx>
00034 
00035 #include <BRep_Tool.hxx>
00036 #include <BRepTools.hxx>
00037 #include <BRepBuilderAPI_MakeWire.hxx>
00038 #include <BRepCheck_Analyzer.hxx>
00039 
00040 #include <Precision.hxx>
00041 
00042 #include <ShapeFix_Wire.hxx>
00043 #include <StdFail_NotDone.hxx>
00044 #include <Standard_ConstructionError.hxx>
00045 
00046 #include <TopAbs.hxx>
00047 #include <TopoDS.hxx>
00048 #include <TopoDS_Edge.hxx>
00049 #include <TopoDS_Wire.hxx>
00050 #include <TopoDS_Shape.hxx>
00051 #include <TopExp.hxx>
00052 #include <TopExp_Explorer.hxx>
00053 #include <TopTools_DataMapOfShapeShape.hxx>
00054 #include <TopTools_ListOfShape.hxx>
00055 #include <TopTools_ListIteratorOfListOfShape.hxx>
00056 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
00057 
00058 //=======================================================================
00059 //function : GetID
00060 //purpose  :
00061 //=======================================================================
00062 const Standard_GUID& GEOMImpl_Fillet1dDriver::GetID()
00063 {
00064   static Standard_GUID aFillet1dDriver("FF60908B-AB2E-4b71-B098-5C256C37D961");
00065   return aFillet1dDriver;
00066 }
00067 
00068 //=======================================================================
00069 //function : GEOMImpl_Fillet1dDriver
00070 //purpose  :
00071 //=======================================================================
00072 GEOMImpl_Fillet1dDriver::GEOMImpl_Fillet1dDriver()
00073 {
00074 }
00075 
00076 //=======================================================================
00077 //function : anotherVertex
00078 //purpose  : local function to get vertex from edge
00079 //=======================================================================
00080 static TopoDS_Vertex anotherVertex( const TopoDS_Edge& theE,
00081                                     const TopoDS_Vertex& theV )
00082 {
00083   // here is an assumption that edge has different vertices
00084   TopoDS_Vertex aV;
00085   TopExp_Explorer anExp( theE, TopAbs_VERTEX );
00086   for ( ; anExp.More(); anExp.Next() )
00087   {
00088     if ( BRepTools::Compare(theV,TopoDS::Vertex(anExp.Current())) /*theV.IsSame(anExp.Current())*/ )
00089       continue;
00090     aV = TopoDS::Vertex( anExp.Current() );
00091     break;
00092   }
00093   return aV;
00094 }
00095 
00096 //=======================================================================
00097 //function : takePlane
00098 //purpose  : local function returns plane of given edges
00099 //=======================================================================
00100 static Standard_Boolean takePlane( const TopoDS_Edge& theE1,
00101                                    const TopoDS_Edge& theE2,
00102                                    const TopoDS_Vertex& theV,
00103                                    gp_Pln& thePlane )
00104 {
00105   TopoDS_Vertex aV12 = anotherVertex( theE1, theV );
00106   TopoDS_Vertex aV22 = anotherVertex( theE2, theV );
00107   // check can closed wire be created by two initial edges
00108   if ( aV12.IsNull()  || aV22.IsNull() || aV12.IsSame( aV22 ) )
00109     return false;
00110 
00111   // create plane by 3 points
00112   gp_XYZ aXYZ = BRep_Tool::Pnt( theV ).XYZ();
00113   gp_XYZ aXYZ1 = BRep_Tool::Pnt( aV12 ).XYZ();
00114   gp_XYZ aXYZ2 = BRep_Tool::Pnt( aV22 ).XYZ();
00115   try {
00116     gp_Dir aDir1( aXYZ - aXYZ1 );
00117     gp_Dir aDir2( aXYZ2 - aXYZ );
00118     Standard_Real anAngle = aDir1.Angle(aDir2);
00119     if ( fabs(anAngle) <= gp::Resolution() ||
00120          fabs(anAngle - M_PI) <= gp::Resolution() )
00121       return false;
00122     thePlane = gp_Pln( gp_Pnt(aXYZ), aDir1^ aDir2);
00123   }
00124   catch (Standard_Failure) {
00125     return false;
00126   }
00127   return true;
00128 }
00129 
00130 //=======================================================================
00131 //function : addEdgeRelation
00132 //purpose  : local function to remember relation between initial and modified edge
00133 //=======================================================================
00134 static void addEdgeRelation(TopTools_DataMapOfShapeShape& theMap,
00135                             const TopoDS_Edge& theInitE,
00136                             const TopoDS_Edge& theResE)
00137 {
00138   if ( theMap.IsBound( theInitE ) )
00139     theMap.ChangeFind( theInitE ) = theResE;
00140   else
00141     theMap.Bind( theInitE, theResE );
00142 }
00143 
00144 //=======================================================================
00145 //function : Execute
00146 //purpose  :
00147 //=======================================================================
00148 Standard_Integer GEOMImpl_Fillet1dDriver::Execute(TFunction_Logbook& log) const
00149 {
00150   if (Label().IsNull()) return 0;
00151   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
00152 
00153   GEOMImpl_IFillet1d aCI (aFunction);
00154 
00155   Handle(GEOM_Function) aRefShape = aCI.GetShape();
00156   TopoDS_Shape aShape = aRefShape->GetValue();
00157   if (aShape.IsNull())
00158     return 0;
00159   if (aShape.ShapeType() != TopAbs_WIRE)
00160     Standard_ConstructionError::Raise("Wrong arguments: polyline as wire must be given");
00161 
00162   TopoDS_Wire aWire = TopoDS::Wire(aShape);
00163 
00164   double rad = aCI.GetR();
00165 
00166   if ( rad < Precision::Confusion())
00167     return 0;
00168 
00169   // collect vertices for make fillet
00170   TopTools_ListOfShape aVertexList;
00171   TopTools_MapOfShape mapShape;
00172   int aLen = aCI.GetLength();
00173   if ( aLen > 0 ) {
00174     for (int ind = 1; ind <= aLen; ind++) {
00175       TopoDS_Shape aShapeVertex;
00176       if (GEOMImpl_ILocalOperations::GetSubShape
00177           (aWire, aCI.GetVertex(ind), aShapeVertex))
00178         if (mapShape.Add(aShapeVertex))
00179           aVertexList.Append( aShapeVertex );
00180     }
00181   } else { // get all vertices from wire
00182     TopExp_Explorer anExp( aWire, TopAbs_VERTEX );
00183     for ( ; anExp.More(); anExp.Next() ) {
00184       if (mapShape.Add(anExp.Current()))
00185         aVertexList.Append( anExp.Current() );
00186     }
00187   }
00188   if (aVertexList.IsEmpty())
00189     Standard_ConstructionError::Raise("Invalid input no vertices to make fillet");
00190 
00191   //INFO: this algorithm implemented in assumption that user can select both
00192   //  vertices of some edges to make fillet. In this case we should remember
00193   //  already modified initial edges to take care in next fillet step
00194   TopTools_DataMapOfShapeShape anEdgeToEdgeMap;
00195 
00196   //iterates on vertices, and make fillet on each couple of edges
00197   //collect result fillet edges in list
00198   TopTools_ListOfShape aListOfNewEdge;
00199   // remember relation between initial and modified map
00200   TopTools_IndexedDataMapOfShapeListOfShape aMapVToEdges;
00201   TopExp::MapShapesAndAncestors( aWire, TopAbs_VERTEX, TopAbs_EDGE, aMapVToEdges );
00202   TopTools_ListIteratorOfListOfShape anIt( aVertexList );
00203   for ( ; anIt.More(); anIt.Next() ) {
00204     TopoDS_Vertex aV = TopoDS::Vertex( anIt.Value() );
00205     if ( aV.IsNull() || !aMapVToEdges.Contains( aV ) )
00206       continue;
00207     const TopTools_ListOfShape& aVertexEdges = aMapVToEdges.FindFromKey( aV );
00208     if ( aVertexEdges.Extent() != 2 )
00209       continue; // no input data to make fillet
00210     TopoDS_Edge anEdge1 = TopoDS::Edge( aVertexEdges.First() );
00211     TopoDS_Edge anEdge2 = TopoDS::Edge( aVertexEdges.Last() );
00212     // check if initial edges already modified in previous fillet operation
00213     if ( anEdgeToEdgeMap.IsBound( anEdge1 ) ) anEdge1 = TopoDS::Edge(anEdgeToEdgeMap.Find( anEdge1 ));
00214     if ( anEdgeToEdgeMap.IsBound( anEdge2 ) ) anEdge2 = TopoDS::Edge(anEdgeToEdgeMap.Find( anEdge2 ));
00215     if ( anEdge1.IsNull() || anEdge2.IsNull() || anEdge1.IsSame( anEdge2 ) )
00216       continue; //no input data to make fillet
00217 
00218     // create plane on 2 edges
00219     gp_Pln aPlane;
00220     if ( !takePlane(anEdge1, anEdge2, aV, aPlane) )
00221       continue; // seems edges does not belong to same plane or parallel (fillet can not be build)
00222 
00223     GEOMImpl_Fillet1d aFilletAlgo(anEdge1, anEdge2, aPlane);
00224     if ( !aFilletAlgo.Perform(rad) )
00225       continue; // can not create fillet with given radius
00226 
00227     // take fillet result in given vertex
00228     TopoDS_Edge aModifE1, aModifE2;
00229     TopoDS_Edge aNewE = aFilletAlgo.Result(BRep_Tool::Pnt(aV), aModifE1, aModifE2);
00230     if (aNewE.IsNull())
00231       continue; // no result found
00232 
00233     // add  new created edges and take modified edges
00234     aListOfNewEdge.Append( aNewE );
00235 
00236     // check if face edges modified,
00237     // if yes, than map to original edges (from vertex-edges list), because edges can be modified before
00238     if (aModifE1.IsNull() || !anEdge1.IsSame( aModifE1 ))
00239       addEdgeRelation( anEdgeToEdgeMap, TopoDS::Edge(aVertexEdges.First()), aModifE1 );
00240     if (aModifE2.IsNull() || !anEdge2.IsSame( aModifE2 ))
00241       addEdgeRelation( anEdgeToEdgeMap, TopoDS::Edge(aVertexEdges.Last()), aModifE2 );
00242   }
00243 
00244   if ( anEdgeToEdgeMap.IsEmpty() && aListOfNewEdge.IsEmpty() ) {
00245     StdFail_NotDone::Raise("1D Fillet can't be computed on the given shape with the given radius");
00246     return 0;
00247   }
00248 
00249   // create new wire instead of original
00250   for ( TopExp_Explorer anExp( aWire, TopAbs_EDGE ); anExp.More(); anExp.Next() ) {
00251     TopoDS_Shape anEdge = anExp.Current();
00252     if ( !anEdgeToEdgeMap.IsBound( anEdge ) )
00253       aListOfNewEdge.Append( anEdge );
00254     else if (!anEdgeToEdgeMap.Find( anEdge ).IsNull())
00255       aListOfNewEdge.Append( anEdgeToEdgeMap.Find( anEdge ) );
00256   }
00257 
00258   GEOMImpl_IShapesOperations::SortShapes( aListOfNewEdge );
00259 
00260   BRepBuilderAPI_MakeWire aWireTool;
00261   aWireTool.Add( aListOfNewEdge );
00262   aWireTool.Build();
00263   if (!aWireTool.IsDone())
00264     return 0;
00265 
00266   aWire = aWireTool.Wire();
00267   aFunction->SetValue(aWire);
00268   log.SetTouched(Label());
00269 
00270   return 1;
00271 }
00272 
00273 
00274 //=======================================================================
00275 //function :  GEOMImpl_Fillet1dDriver_Type_
00276 //purpose  :
00277 //=======================================================================
00278 Standard_EXPORT Handle_Standard_Type& GEOMImpl_Fillet1dDriver_Type_()
00279 {
00280   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
00281   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
00282   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
00283   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
00284   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
00285   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
00286 
00287   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
00288   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_Fillet1dDriver",
00289                                                          sizeof(GEOMImpl_Fillet1dDriver),
00290                                                          1,
00291                                                          (Standard_Address)_Ancestors,
00292                                                          (Standard_Address)NULL);
00293 
00294   return _aType;
00295 }
00296 
00297 //=======================================================================
00298 //function : DownCast
00299 //purpose  :
00300 //=======================================================================
00301 const Handle(GEOMImpl_Fillet1dDriver) Handle(GEOMImpl_Fillet1dDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
00302 {
00303   Handle(GEOMImpl_Fillet1dDriver) _anOtherObject;
00304 
00305   if (!AnObject.IsNull()) {
00306      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_Fillet1dDriver))) {
00307        _anOtherObject = Handle(GEOMImpl_Fillet1dDriver)((Handle(GEOMImpl_Fillet1dDriver)&)AnObject);
00308      }
00309   }
00310 
00311   return _anOtherObject;
00312 }