]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMImpl/GEOMImpl_Fillet1dDriver.cxx
Salome HOME
Added a method to disable waiting cursor in the preview operation
[modules/geom.git] / src / GEOMImpl / GEOMImpl_Fillet1dDriver.cxx
1 // Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18
19 #include <Standard_Stream.hxx>
20
21 #include <GEOMImpl_Fillet1dDriver.hxx>
22
23 #include <GEOMImpl_Fillet1d.hxx>
24 #include <GEOMImpl_IFillet1d.hxx>
25 #include <GEOMImpl_Types.hxx>
26 #include <GEOMImpl_ILocalOperations.hxx>
27 #include <GEOMImpl_IShapesOperations.hxx>
28 #include <GEOM_Function.hxx>
29
30 #include <gp_Pln.hxx>
31 #include <gp_Dir.hxx>
32 #include <gp_XYZ.hxx>
33
34 #include <BRep_Tool.hxx>
35 #include <BRepTools.hxx>
36 #include <BRepBuilderAPI_MakeWire.hxx>
37 #include <BRepCheck_Analyzer.hxx>
38
39 #include <Precision.hxx>
40
41 #include <ShapeFix_Wire.hxx>
42 #include <StdFail_NotDone.hxx>
43 #include <Standard_ConstructionError.hxx>
44
45 #include <TopAbs.hxx>
46 #include <TopoDS.hxx>
47 #include <TopoDS_Edge.hxx>
48 #include <TopoDS_Wire.hxx>
49 #include <TopoDS_Shape.hxx>
50 #include <TopExp.hxx>
51 #include <TopExp_Explorer.hxx>
52 #include <TopTools_DataMapOfShapeShape.hxx>
53 #include <TopTools_ListOfShape.hxx>
54 #include <TopTools_ListIteratorOfListOfShape.hxx>
55 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
56
57 //=======================================================================
58 //function : GetID
59 //purpose  :
60 //=======================================================================
61 const Standard_GUID& GEOMImpl_Fillet1dDriver::GetID()
62 {
63   static Standard_GUID aFillet1dDriver("FF60908B-AB2E-4b71-B098-5C256C37D961");
64   return aFillet1dDriver;
65 }
66
67 //=======================================================================
68 //function : GEOMImpl_Fillet1dDriver
69 //purpose  :
70 //=======================================================================
71 GEOMImpl_Fillet1dDriver::GEOMImpl_Fillet1dDriver()
72 {
73 }
74
75 //=======================================================================
76 //function : anotherVertex
77 //purpose  : local function to get vertex from edge
78 //=======================================================================
79 static TopoDS_Vertex anotherVertex( const TopoDS_Edge& theE,
80                                     const TopoDS_Vertex& theV )
81 {
82   // here is an assumption that edge has different vertices
83   TopoDS_Vertex aV;
84   TopExp_Explorer anExp( theE, TopAbs_VERTEX );
85   for ( ; anExp.More(); anExp.Next() )
86   {
87     if ( BRepTools::Compare(theV,TopoDS::Vertex(anExp.Current())) /*theV.IsSame(anExp.Current())*/ )
88       continue;
89     aV = TopoDS::Vertex( anExp.Current() );
90     break;
91   }
92   return aV;
93 }
94
95 //=======================================================================
96 //function : takePlane
97 //purpose  : local function returns plane of given edges
98 //=======================================================================
99 static Standard_Boolean takePlane( const TopoDS_Edge& theE1,
100                                    const TopoDS_Edge& theE2,
101                                    const TopoDS_Vertex& theV,
102                                    gp_Pln& thePlane )
103 {
104   TopoDS_Vertex aV12 = anotherVertex( theE1, theV );
105   TopoDS_Vertex aV22 = anotherVertex( theE2, theV );
106   // check can closed wire be created by two initial edges
107   if ( aV12.IsNull()  || aV22.IsNull() || aV12.IsSame( aV22 ) )
108     return false;
109
110   // create plane by 3 points
111   gp_XYZ aXYZ = BRep_Tool::Pnt( theV ).XYZ();
112   gp_XYZ aXYZ1 = BRep_Tool::Pnt( aV12 ).XYZ();
113   gp_XYZ aXYZ2 = BRep_Tool::Pnt( aV22 ).XYZ();
114   try {
115     gp_Dir aDir1( aXYZ - aXYZ1 );
116     gp_Dir aDir2( aXYZ2 - aXYZ );
117     Standard_Real anAngle = aDir1.Angle(aDir2);
118     if ( fabs(anAngle) <= gp::Resolution() ||
119          fabs(anAngle - M_PI) <= gp::Resolution() )
120       return false;
121     thePlane = gp_Pln( gp_Pnt(aXYZ), aDir1^ aDir2);
122   }
123   catch (Standard_Failure) {
124     return false;
125   }
126   return true;
127 }
128
129 //=======================================================================
130 //function : addEdgeRelation
131 //purpose  : local function to remember relation between initial and modified edge
132 //=======================================================================
133 static void addEdgeRelation(TopTools_DataMapOfShapeShape& theMap,
134                             const TopoDS_Edge& theInitE,
135                             const TopoDS_Edge& theResE)
136 {
137   if ( theMap.IsBound( theInitE ) )
138     theMap.ChangeFind( theInitE ) = theResE;
139   else
140     theMap.Bind( theInitE, theResE );
141 }
142
143 //=======================================================================
144 //function : Execute
145 //purpose  :
146 //=======================================================================
147 Standard_Integer GEOMImpl_Fillet1dDriver::Execute(TFunction_Logbook& log) const
148 {
149   if (Label().IsNull()) return 0;
150   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
151
152   GEOMImpl_IFillet1d aCI (aFunction);
153
154   Handle(GEOM_Function) aRefShape = aCI.GetShape();
155   TopoDS_Shape aShape = aRefShape->GetValue();
156   if (aShape.IsNull())
157     return 0;
158   if (aShape.ShapeType() != TopAbs_WIRE)
159     Standard_ConstructionError::Raise("Wrong arguments: polyline as wire must be given");
160
161   TopoDS_Wire aWire = TopoDS::Wire(aShape);
162
163   double rad = aCI.GetR();
164
165   if ( rad < Precision::Confusion())
166     return 0;
167
168   // collect vertices for make fillet
169   TopTools_ListOfShape aVertexList;
170   TopTools_MapOfShape mapShape;
171   int aLen = aCI.GetLength();
172   if ( aLen > 0 ) {
173     for (int ind = 1; ind <= aLen; ind++) {
174       TopoDS_Shape aShapeVertex;
175       if (GEOMImpl_ILocalOperations::GetSubShape
176           (aWire, aCI.GetVertex(ind), aShapeVertex))
177         if (mapShape.Add(aShapeVertex))
178           aVertexList.Append( aShapeVertex );
179     }
180   } else { // get all vertices from wire
181     TopExp_Explorer anExp( aWire, TopAbs_VERTEX );
182     for ( ; anExp.More(); anExp.Next() ) {
183       if (mapShape.Add(anExp.Current()))
184         aVertexList.Append( anExp.Current() );
185     }
186   }
187   if (aVertexList.IsEmpty())
188     Standard_ConstructionError::Raise("Invalid input no vertices to make fillet");
189
190   //INFO: this algorithm implemented in assumption that user can select both
191   //  vertices of some edges to make fillet. In this case we should remember
192   //  already modified initial edges to take care in next fillet step
193   TopTools_DataMapOfShapeShape anEdgeToEdgeMap;
194
195   //iterates on vertices, and make fillet on each couple of edges
196   //collect result fillet edges in list
197   TopTools_ListOfShape aListOfNewEdge;
198   // remember relation between initial and modified map
199   TopTools_IndexedDataMapOfShapeListOfShape aMapVToEdges;
200   TopExp::MapShapesAndAncestors( aWire, TopAbs_VERTEX, TopAbs_EDGE, aMapVToEdges );
201   TopTools_ListIteratorOfListOfShape anIt( aVertexList );
202   for ( ; anIt.More(); anIt.Next() ) {
203     TopoDS_Vertex aV = TopoDS::Vertex( anIt.Value() );
204     if ( aV.IsNull() || !aMapVToEdges.Contains( aV ) )
205       continue;
206     const TopTools_ListOfShape& aVertexEdges = aMapVToEdges.FindFromKey( aV );
207     if ( aVertexEdges.Extent() != 2 )
208       continue; // no input data to make fillet
209     TopoDS_Edge anEdge1 = TopoDS::Edge( aVertexEdges.First() );
210     TopoDS_Edge anEdge2 = TopoDS::Edge( aVertexEdges.Last() );
211     // check if initial edges already modified in previous fillet operation
212     if ( anEdgeToEdgeMap.IsBound( anEdge1 ) ) anEdge1 = TopoDS::Edge(anEdgeToEdgeMap.Find( anEdge1 ));
213     if ( anEdgeToEdgeMap.IsBound( anEdge2 ) ) anEdge2 = TopoDS::Edge(anEdgeToEdgeMap.Find( anEdge2 ));
214     if ( anEdge1.IsNull() || anEdge2.IsNull() || anEdge1.IsSame( anEdge2 ) )
215       continue; //no input data to make fillet
216
217     // create plane on 2 edges
218     gp_Pln aPlane;
219     if ( !takePlane(anEdge1, anEdge2, aV, aPlane) )
220       continue; // seems edges does not belong to same plane or parallel (fillet can not be build)
221
222     GEOMImpl_Fillet1d aFilletAlgo(anEdge1, anEdge2, aPlane);
223     if ( !aFilletAlgo.Perform(rad) )
224       continue; // can not create fillet with given radius
225
226     // take fillet result in given vertex
227     TopoDS_Edge aModifE1, aModifE2;
228     TopoDS_Edge aNewE = aFilletAlgo.Result(BRep_Tool::Pnt(aV), aModifE1, aModifE2);
229     if (aNewE.IsNull())
230       continue; // no result found
231
232     // add  new created edges and take modified edges
233     aListOfNewEdge.Append( aNewE );
234
235     // check if face edges modified,
236     // if yes, than map to original edges (from vertex-edges list), because edges can be modified before
237     if (aModifE1.IsNull() || !anEdge1.IsSame( aModifE1 ))
238       addEdgeRelation( anEdgeToEdgeMap, TopoDS::Edge(aVertexEdges.First()), aModifE1 );
239     if (aModifE2.IsNull() || !anEdge2.IsSame( aModifE2 ))
240       addEdgeRelation( anEdgeToEdgeMap, TopoDS::Edge(aVertexEdges.Last()), aModifE2 );
241   }
242
243   if ( anEdgeToEdgeMap.IsEmpty() && aListOfNewEdge.IsEmpty() ) {
244     StdFail_NotDone::Raise("1D Fillet can't be computed on the given shape with the given radius");
245     return 0;
246   }
247
248   // create new wire instead of original
249   for ( TopExp_Explorer anExp( aWire, TopAbs_EDGE ); anExp.More(); anExp.Next() ) {
250     TopoDS_Shape anEdge = anExp.Current();
251     if ( !anEdgeToEdgeMap.IsBound( anEdge ) )
252       aListOfNewEdge.Append( anEdge );
253     else if (!anEdgeToEdgeMap.Find( anEdge ).IsNull())
254       aListOfNewEdge.Append( anEdgeToEdgeMap.Find( anEdge ) );
255   }
256
257   GEOMImpl_IShapesOperations::SortShapes( aListOfNewEdge );
258
259   BRepBuilderAPI_MakeWire aWireTool;
260   aWireTool.Add( aListOfNewEdge );
261   aWireTool.Build();
262   if (!aWireTool.IsDone())
263     return 0;
264
265   aWire = aWireTool.Wire();
266   aFunction->SetValue(aWire);
267   log.SetTouched(Label());
268
269   return 1;
270 }
271
272
273 //=======================================================================
274 //function :  GEOMImpl_Fillet1dDriver_Type_
275 //purpose  :
276 //=======================================================================
277 Standard_EXPORT Handle_Standard_Type& GEOMImpl_Fillet1dDriver_Type_()
278 {
279   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
280   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
281   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
282   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
283   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
284   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
285
286   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
287   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_Fillet1dDriver",
288                                                          sizeof(GEOMImpl_Fillet1dDriver),
289                                                          1,
290                                                          (Standard_Address)_Ancestors,
291                                                          (Standard_Address)NULL);
292
293   return _aType;
294 }
295
296 //=======================================================================
297 //function : DownCast
298 //purpose  :
299 //=======================================================================
300 const Handle(GEOMImpl_Fillet1dDriver) Handle(GEOMImpl_Fillet1dDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
301 {
302   Handle(GEOMImpl_Fillet1dDriver) _anOtherObject;
303
304   if (!AnObject.IsNull()) {
305      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_Fillet1dDriver))) {
306        _anOtherObject = Handle(GEOMImpl_Fillet1dDriver)((Handle(GEOMImpl_Fillet1dDriver)&)AnObject);
307      }
308   }
309
310   return _anOtherObject;
311 }