Salome HOME
183b17a4613a4682cac8d40ffac5cd2a9651ce5f
[modules/geom.git] / src / ShHealOper / ShHealOper_Sewing.cxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // File:      ShHealOper_Sewing.cxx
24 // Created:   29.04.04 15:07:10
25 // Author:    Galina KULIKOVA
26 //
27 #include <ShHealOper_Sewing.hxx>
28 #include <TopExp_Explorer.hxx>
29 #include <TopoDS_Shell.hxx>
30 #include <BRep_Builder.hxx>
31 #include <ShapeFix_Shell.hxx>
32 #include <TopTools_HSequenceOfShape.hxx>
33 #include <TopoDS_Shell.hxx>
34 #include <BRep_Builder.hxx>
35 #include <ShapeAnalysis_FreeBounds.hxx>
36 #include <TopoDS_Compound.hxx>
37 #include <TopoDS.hxx>
38 #include <TopoDS_Iterator.hxx>
39 #include <TopTools_MapOfShape.hxx>
40 //=======================================================================
41 //function : ShHealOper_Sewing()
42 //purpose  : Constructor
43 //=======================================================================
44
45
46 ShHealOper_Sewing::ShHealOper_Sewing (const TopoDS_Shape& theShape,
47                                      const Standard_Real theTolerance)
48 {
49   Init(theShape);
50   myTolerance = theTolerance;
51 }
52 //=======================================================================
53 //function : Init
54 //purpose  : 
55 //=======================================================================
56
57 void ShHealOper_Sewing::Init(const TopoDS_Shape& theShape)
58 {
59   ShHealOper_Tool::Init(theShape);
60   myTolerance = Precision::Confusion();
61   myEdgesMode = Standard_False;
62   myFacesMode = Standard_True;
63   myNonManifoldMode = Standard_False;
64   myHistoryLevel = TopAbs_FACE;
65 }
66 //=======================================================================
67 //function : Perform
68 //purpose  : 
69 //=======================================================================
70
71 Standard_Boolean ShHealOper_Sewing::Perform()
72 {
73   TopTools_SequenceOfShape theSeqShapes;
74   return sewing(theSeqShapes);
75 }
76 //=======================================================================
77 //function : Perform
78 //purpose  : 
79 //=======================================================================
80
81 Standard_Boolean ShHealOper_Sewing::Perform(const TopTools_SequenceOfShape& theSeqShapes)
82 {
83   return sewing(theSeqShapes);
84 }
85
86 //=======================================================================
87 //function : sewing
88 //purpose  : 
89 //=======================================================================
90
91 Standard_Boolean ShHealOper_Sewing::sewing(const TopTools_SequenceOfShape& theSeqShapes)
92 {
93   myDone = Standard_False;
94   myErrorStatus = ShHealOper_NotError;
95   if(myInitShape.IsNull()) {
96     myErrorStatus = ShHealOper_InvalidParameters;
97     return myDone;
98   }
99   //sewing shape
100   Handle(BRepBuilderAPI_Sewing) aSewing = new BRepBuilderAPI_Sewing;
101   aSewing->Load(myInitShape);
102   aSewing->SetTolerance(myTolerance);
103   aSewing->SetFaceMode(myFacesMode);
104   aSewing->SetFloatingEdgesMode(myEdgesMode);
105   aSewing->SetNonManifoldMode(myNonManifoldMode);
106   Standard_Integer j =1;
107   for( ; j <= theSeqShapes.Length();j++)
108     aSewing->Add(theSeqShapes.Value(j));
109
110   aSewing->Perform();
111   const TopoDS_Shape aSewShape = aSewing->SewedShape();
112   if(aSewShape.IsNull()) {
113     myErrorStatus = ShHealOper_ErrorExecution;
114     return myDone;
115   }
116   if(aSewShape.IsSame(myInitShape))
117     return myDone;
118
119   //analysis either sewing was made by changing number of shells
120   myDone = isSewed(aSewShape);
121
122   //keep modification of the sub-shapes in the Context.
123   TopExp_Explorer aExp(myInitShape,TopAbs_FACE);
124   for( ; aExp.More(); aExp.Next())
125     myDone = (getModifications( aExp.Current(),aSewing) || myDone);
126
127   TopoDS_Shape aTempShape = myContext->Apply(aSewShape);
128   //obtained shells with fixed orientation for manifold and nonmanifold shells
129   if(myFacesMode) 
130     myDone = getShells(aTempShape) || myDone;
131
132   //obtained manifold wires if sewing edges was performed.
133   if(myEdgesMode) 
134     myDone = getWires(aTempShape) || myDone;
135   
136   if(myDone)
137     myResultShape = myContext->Apply(aTempShape);
138     
139   return myDone;
140 }
141 //=======================================================================
142 //function : getShells
143 //purpose  : 
144 //=======================================================================
145
146 Standard_Boolean ShHealOper_Sewing::getShells(const TopoDS_Shape& theSewShape) const
147 {
148   Standard_Boolean isDone = Standard_False;
149   TopoDS_Shape aTmpShape = theSewShape;
150   if(myNonManifoldMode) {
151     TopoDS_Shell tempShell;
152     BRep_Builder aB;
153     aB.MakeShell(tempShell);
154     
155     for(TopExp_Explorer aExpf(theSewShape,TopAbs_FACE);aExpf.More(); aExpf.Next()) 
156       aB.Add(tempShell,aExpf.Current());
157     aTmpShape = tempShell;
158     myContext->Replace(theSewShape,aTmpShape);
159   }
160   
161   Handle(ShapeFix_Shell) asfs = new ShapeFix_Shell;
162   asfs->SetContext(myContext);
163   TopExp_Explorer aexpShell(aTmpShape,TopAbs_SHELL);
164   for ( ; aexpShell.More(); aexpShell.Next()) 
165     isDone = (asfs->FixFaceOrientation(TopoDS::Shell(aexpShell.Current()),
166                                        Standard_True,myNonManifoldMode) || isDone);
167   
168   return isDone;
169 }
170 //=======================================================================
171 //function : getWires
172 //purpose  : 
173 //=======================================================================
174
175 Standard_Boolean ShHealOper_Sewing::getWires(const TopoDS_Shape& theSewShape) const
176 {
177   if(theSewShape.ShapeType() != TopAbs_COMPOUND)
178     return Standard_False;
179   
180   Handle(TopTools_HSequenceOfShape) aSeqEdges = new TopTools_HSequenceOfShape;
181   TopExp_Explorer aexpEdges(theSewShape,TopAbs_EDGE,TopAbs_WIRE);
182   for ( ; aexpEdges.More(); aexpEdges.Next()) {
183     aSeqEdges->Append(aexpEdges.Current());
184   }
185   if(aSeqEdges->Length() <2)
186     return Standard_False;
187   //get manifold wires from sewed edges.
188   Standard_Real aTol = 0.;
189   Standard_Boolean aShared = Standard_True;
190   Handle(TopTools_HSequenceOfShape) aTmpWires = new TopTools_HSequenceOfShape;
191   ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdges, aTol, aShared, aTmpWires);
192
193   TopTools_MapOfShape aMapEdges;
194   Standard_Integer i =1;
195   for( ; i <= aSeqEdges->Length(); i++)
196     aMapEdges.Add(aSeqEdges->Value(i));
197   
198   //remove free edges from result shape.
199   TopoDS_Compound aNewComp;
200   deleteFreeEdges(theSewShape,aMapEdges,aNewComp);
201   
202   //add new wires in the result shape.
203   BRep_Builder aB;
204   for( i =1; i <= aTmpWires->Length(); i++) {
205     TopoDS_Iterator aite(aTmpWires->Value(i));
206     Standard_Integer nbe =0;
207     TopoDS_Shape aE;
208     for( ; aite.More() && nbe < 3; aite.Next(),nbe++)
209       aE = aite.Value();
210     if(!nbe)
211       continue;
212     else if(nbe ==1)
213       aB.Add(aNewComp,aE);
214     else
215       aB.Add(aNewComp,aTmpWires->Value(i));
216   }
217   
218   myContext->Replace(theSewShape,aNewComp);
219   return Standard_True;
220 }
221 //=======================================================================
222 //function : getHistory
223 //purpose  : 
224 //=======================================================================
225
226 Standard_Boolean ShHealOper_Sewing::getModifications(const TopoDS_Shape& theShape,
227                                                      const Handle(BRepBuilderAPI_Sewing)& theSewing) const
228 {
229   if((Standard_Integer)theShape.ShapeType() > (Standard_Integer)myHistoryLevel)
230     return Standard_False;
231   
232   Standard_Boolean isDone = theSewing->IsModifiedSubShape(theShape);
233   if(isDone) {
234     TopoDS_Shape aNewShape = theSewing->ModifiedSubShape(theShape);
235     myContext->Replace(theShape,aNewShape);
236   }
237   TopoDS_Iterator aIt(theShape);
238   for( ; aIt.More(); aIt.Next()) {
239     isDone = (getModifications( aIt.Value(),theSewing) || isDone);
240   }
241   return isDone;
242     
243 }
244 //=======================================================================
245 //function : isSewed
246 //purpose  : 
247 //=======================================================================
248
249 Standard_Boolean ShHealOper_Sewing::isSewed(const TopoDS_Shape& theShape) const
250 {
251   Standard_Integer nbNewShells =0;
252   Standard_Integer nbOldShells =0;
253   TopExp_Explorer aExpShells(theShape,TopAbs_SHELL);
254   for( ; aExpShells.More(); aExpShells.Next())
255     nbNewShells++;
256   for( aExpShells.Init(myInitShape,TopAbs_SHELL); aExpShells.More(); aExpShells.Next())
257     nbOldShells++;
258   return (nbNewShells != nbOldShells);
259 }
260 //=======================================================================
261 //function : deleteFreeEdges
262 //purpose  : 
263 //=======================================================================
264
265 void ShHealOper_Sewing::deleteFreeEdges(const TopoDS_Shape& theSewShape, 
266                                         TopTools_MapOfShape& theMapEdges,
267                                         TopoDS_Compound& theNewComp) const
268 {
269  
270   BRep_Builder aB;
271   aB.MakeCompound(theNewComp);
272   TopoDS_Iterator aIt(theSewShape);
273   
274   for( ; aIt.More(); aIt.Next()) {
275     if((aIt.Value().ShapeType() == TopAbs_EDGE ) && theMapEdges.Contains(aIt.Value()))
276       continue;
277     else if(aIt.Value().ShapeType() == TopAbs_COMPOUND) {
278       TopoDS_Compound aC;
279       deleteFreeEdges(aIt.Value(),theMapEdges,aC);
280       aB.Add(theNewComp,aC);
281     }
282     else
283       aB.Add(theNewComp,aIt.Value());
284   }
285 }