1 // Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <Standard_Stream.hxx>
22 #include <GEOMImpl_Fillet1dDriver.hxx>
24 #include <GEOMImpl_Fillet1d.hxx>
25 #include <GEOMImpl_IFillet1d.hxx>
26 #include <GEOMImpl_Types.hxx>
27 #include <GEOMImpl_ILocalOperations.hxx>
28 #include <GEOMImpl_IShapesOperations.hxx>
29 #include <GEOM_Function.hxx>
35 #include <BRep_Tool.hxx>
36 #include <BRepTools.hxx>
37 #include <BRepBuilderAPI_MakeWire.hxx>
38 #include <BRepCheck_Analyzer.hxx>
40 #include <Precision.hxx>
42 #include <ShapeFix_Wire.hxx>
43 #include <StdFail_NotDone.hxx>
44 #include <Standard_ConstructionError.hxx>
48 #include <TopoDS_Edge.hxx>
49 #include <TopoDS_Wire.hxx>
50 #include <TopoDS_Shape.hxx>
52 #include <TopExp_Explorer.hxx>
53 #include <TopTools_DataMapOfShapeShape.hxx>
54 #include <TopTools_ListOfShape.hxx>
55 #include <TopTools_ListIteratorOfListOfShape.hxx>
56 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
58 //=======================================================================
61 //=======================================================================
62 const Standard_GUID& GEOMImpl_Fillet1dDriver::GetID()
64 static Standard_GUID aFillet1dDriver("FF60908B-AB2E-4b71-B098-5C256C37D961");
65 return aFillet1dDriver;
68 //=======================================================================
69 //function : GEOMImpl_Fillet1dDriver
71 //=======================================================================
72 GEOMImpl_Fillet1dDriver::GEOMImpl_Fillet1dDriver()
76 //=======================================================================
77 //function : anotherVertex
78 //purpose : local function to get vertex from edge
79 //=======================================================================
80 static TopoDS_Vertex anotherVertex( const TopoDS_Edge& theE,
81 const TopoDS_Vertex& theV )
83 // here is an assumption that edge has different vertices
85 TopExp_Explorer anExp( theE, TopAbs_VERTEX );
86 for ( ; anExp.More(); anExp.Next() )
88 if ( BRepTools::Compare(theV,TopoDS::Vertex(anExp.Current())) /*theV.IsSame(anExp.Current())*/ )
90 aV = TopoDS::Vertex( anExp.Current() );
96 //=======================================================================
97 //function : takePlane
98 //purpose : local function returns plane of given edges
99 //=======================================================================
100 static Standard_Boolean takePlane( const TopoDS_Edge& theE1,
101 const TopoDS_Edge& theE2,
102 const TopoDS_Vertex& theV,
105 TopoDS_Vertex aV12 = anotherVertex( theE1, theV );
106 TopoDS_Vertex aV22 = anotherVertex( theE2, theV );
107 // check can closed wire be created by two initial edges
108 if ( aV12.IsNull() || aV22.IsNull() || aV12.IsSame( aV22 ) )
111 // create plane by 3 points
112 gp_XYZ aXYZ = BRep_Tool::Pnt( theV ).XYZ();
113 gp_XYZ aXYZ1 = BRep_Tool::Pnt( aV12 ).XYZ();
114 gp_XYZ aXYZ2 = BRep_Tool::Pnt( aV22 ).XYZ();
116 gp_Dir aDir1( aXYZ - aXYZ1 );
117 gp_Dir aDir2( aXYZ2 - aXYZ );
118 Standard_Real anAngle = aDir1.Angle(aDir2);
119 if ( fabs(anAngle) <= gp::Resolution() ||
120 fabs(anAngle - PI) <= gp::Resolution() )
122 thePlane = gp_Pln( gp_Pnt(aXYZ), aDir1^ aDir2);
124 catch (Standard_Failure) {
130 //=======================================================================
131 //function : addEdgeRelation
132 //purpose : local function to remember relation between initial and modified edge
133 //=======================================================================
134 static void addEdgeRelation(TopTools_DataMapOfShapeShape& theMap,
135 const TopoDS_Edge& theInitE,
136 const TopoDS_Edge& theResE)
138 if ( theMap.IsBound( theInitE ) )
139 theMap.ChangeFind( theInitE ) = theResE;
141 theMap.Bind( theInitE, theResE );
144 //=======================================================================
147 //=======================================================================
148 Standard_Integer GEOMImpl_Fillet1dDriver::Execute(TFunction_Logbook& log) const
150 if (Label().IsNull()) return 0;
151 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
153 GEOMImpl_IFillet1d aCI (aFunction);
155 Handle(GEOM_Function) aRefShape = aCI.GetShape();
156 TopoDS_Shape aShape = aRefShape->GetValue();
159 if (aShape.ShapeType() != TopAbs_WIRE)
160 Standard_ConstructionError::Raise("Wrong arguments: polyline as wire must be given");
162 TopoDS_Wire aWire = TopoDS::Wire(aShape);
164 double rad = aCI.GetR();
166 if ( rad < Precision::Confusion())
169 // collect vertices for make fillet
170 TopTools_ListOfShape aVertexList;
171 TopTools_MapOfShape mapShape;
172 int aLen = aCI.GetLength();
174 for (int ind = 1; ind <= aLen; ind++) {
175 TopoDS_Shape aShapeVertex;
176 if (GEOMImpl_ILocalOperations::GetSubShape
177 (aWire, aCI.GetVertex(ind), aShapeVertex))
178 if (mapShape.Add(aShapeVertex))
179 aVertexList.Append( aShapeVertex );
181 } else { // get all vertices from wire
182 TopExp_Explorer anExp( aWire, TopAbs_VERTEX );
183 for ( ; anExp.More(); anExp.Next() ) {
184 if (mapShape.Add(anExp.Current()))
185 aVertexList.Append( anExp.Current() );
188 if (aVertexList.IsEmpty())
189 Standard_ConstructionError::Raise("Invalid input no vertices to make fillet");
191 //INFO: this algorithm implemented in assumption that user can select both
192 // vertices of some edges to make fillet. In this case we should remember
193 // already modified initial edges to take care in next fillet step
194 TopTools_DataMapOfShapeShape anEdgeToEdgeMap;
196 //iterates on vertices, and make fillet on each couple of edges
197 //collect result fillet edges in list
198 TopTools_ListOfShape aListOfNewEdge;
199 // remember relation between initial and modified map
200 TopTools_IndexedDataMapOfShapeListOfShape aMapVToEdges;
201 TopExp::MapShapesAndAncestors( aWire, TopAbs_VERTEX, TopAbs_EDGE, aMapVToEdges );
202 TopTools_ListIteratorOfListOfShape anIt( aVertexList );
203 for ( ; anIt.More(); anIt.Next() ) {
204 TopoDS_Vertex aV = TopoDS::Vertex( anIt.Value() );
205 if ( aV.IsNull() || !aMapVToEdges.Contains( aV ) )
207 const TopTools_ListOfShape& aVertexEdges = aMapVToEdges.FindFromKey( aV );
208 if ( aVertexEdges.Extent() != 2 )
209 continue; // no input data to make fillet
210 TopoDS_Edge anEdge1 = TopoDS::Edge( aVertexEdges.First() );
211 TopoDS_Edge anEdge2 = TopoDS::Edge( aVertexEdges.Last() );
212 // check if initial edges already modified in previous fillet operation
213 if ( anEdgeToEdgeMap.IsBound( anEdge1 ) ) anEdge1 = TopoDS::Edge(anEdgeToEdgeMap.Find( anEdge1 ));
214 if ( anEdgeToEdgeMap.IsBound( anEdge2 ) ) anEdge2 = TopoDS::Edge(anEdgeToEdgeMap.Find( anEdge2 ));
215 if ( anEdge1.IsNull() || anEdge2.IsNull() || anEdge1.IsSame( anEdge2 ) )
216 continue; //no input data to make fillet
218 // create plane on 2 edges
220 if ( !takePlane(anEdge1, anEdge2, aV, aPlane) )
221 continue; // seems edges does not belong to same plane or parallel (fillet can not be build)
223 GEOMImpl_Fillet1d aFilletAlgo(anEdge1, anEdge2, aPlane);
224 if ( !aFilletAlgo.Perform(rad) )
225 continue; // can not create fillet with given radius
227 // take fillet result in given vertex
228 TopoDS_Edge aModifE1, aModifE2;
229 TopoDS_Edge aNewE = aFilletAlgo.Result(BRep_Tool::Pnt(aV), aModifE1, aModifE2);
231 continue; // no result found
233 // add new created edges and take modified edges
234 aListOfNewEdge.Append( aNewE );
236 // check if face edges modified,
237 // if yes, than map to original edges (from vertex-edges list), because edges can be modified before
238 if (aModifE1.IsNull() || !anEdge1.IsSame( aModifE1 ))
239 addEdgeRelation( anEdgeToEdgeMap, TopoDS::Edge(aVertexEdges.First()), aModifE1 );
240 if (aModifE2.IsNull() || !anEdge2.IsSame( aModifE2 ))
241 addEdgeRelation( anEdgeToEdgeMap, TopoDS::Edge(aVertexEdges.Last()), aModifE2 );
244 if ( anEdgeToEdgeMap.IsEmpty() && aListOfNewEdge.IsEmpty() ) {
245 StdFail_NotDone::Raise("1D Fillet can't be computed on the given shape with the given radius");
249 // create new wire instead of original
250 for ( TopExp_Explorer anExp( aWire, TopAbs_EDGE ); anExp.More(); anExp.Next() ) {
251 TopoDS_Shape anEdge = anExp.Current();
252 if ( !anEdgeToEdgeMap.IsBound( anEdge ) )
253 aListOfNewEdge.Append( anEdge );
254 else if (!anEdgeToEdgeMap.Find( anEdge ).IsNull())
255 aListOfNewEdge.Append( anEdgeToEdgeMap.Find( anEdge ) );
258 GEOMImpl_IShapesOperations::SortShapes( aListOfNewEdge );
260 BRepBuilderAPI_MakeWire aWireTool;
261 aWireTool.Add( aListOfNewEdge );
263 if (!aWireTool.IsDone())
266 aWire = aWireTool.Wire();
267 aFunction->SetValue(aWire);
268 log.SetTouched(Label());
274 //=======================================================================
275 //function : GEOMImpl_Fillet1dDriver_Type_
277 //=======================================================================
278 Standard_EXPORT Handle_Standard_Type& GEOMImpl_Fillet1dDriver_Type_()
281 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
282 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
283 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
284 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
285 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
286 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
289 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
290 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_Fillet1dDriver",
291 sizeof(GEOMImpl_Fillet1dDriver),
293 (Standard_Address)_Ancestors,
294 (Standard_Address)NULL);
299 //=======================================================================
300 //function : DownCast
302 //=======================================================================
303 const Handle(GEOMImpl_Fillet1dDriver) Handle(GEOMImpl_Fillet1dDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
305 Handle(GEOMImpl_Fillet1dDriver) _anOtherObject;
307 if (!AnObject.IsNull()) {
308 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_Fillet1dDriver))) {
309 _anOtherObject = Handle(GEOMImpl_Fillet1dDriver)((Handle(GEOMImpl_Fillet1dDriver)&)AnObject);
313 return _anOtherObject ;