Salome HOME
Update copyright information
[modules/geom.git] / src / ShHealOper / ShHealOper_Sewing.cxx
1 //  Copyright (C) 2007-2008  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.
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 // File:      ShHealOper_Sewing.cxx
23 // Created:   29.04.04 15:07:10
24 // Author:    Galina KULIKOVA
25 //
26 #include <ShHealOper_Sewing.hxx>
27 #include <TopExp_Explorer.hxx>
28 #include <TopoDS_Shell.hxx>
29 #include <BRep_Builder.hxx>
30 #include <ShapeFix_Shell.hxx>
31 #include <TopTools_HSequenceOfShape.hxx>
32 #include <TopoDS_Shell.hxx>
33 #include <BRep_Builder.hxx>
34 #include <ShapeAnalysis_FreeBounds.hxx>
35 #include <TopoDS_Compound.hxx>
36 #include <TopoDS.hxx>
37 #include <TopoDS_Iterator.hxx>
38 #include <TopTools_MapOfShape.hxx>
39 //=======================================================================
40 //function : ShHealOper_Sewing()
41 //purpose  : Constructor
42 //=======================================================================
43
44
45 ShHealOper_Sewing::ShHealOper_Sewing (const TopoDS_Shape& theShape,
46                                      const Standard_Real theTolerance)
47 {
48   Init(theShape);
49   myTolerance = theTolerance;
50 }
51 //=======================================================================
52 //function : Init
53 //purpose  : 
54 //=======================================================================
55
56 void ShHealOper_Sewing::Init(const TopoDS_Shape& theShape)
57 {
58   ShHealOper_Tool::Init(theShape);
59   myTolerance = Precision::Confusion();
60   myEdgesMode = Standard_False;
61   myFacesMode = Standard_True;
62   myNonManifoldMode = Standard_False;
63   myHistoryLevel = TopAbs_FACE;
64 }
65 //=======================================================================
66 //function : Perform
67 //purpose  : 
68 //=======================================================================
69
70 Standard_Boolean ShHealOper_Sewing::Perform()
71 {
72   TopTools_SequenceOfShape theSeqShapes;
73   return sewing(theSeqShapes);
74 }
75 //=======================================================================
76 //function : Perform
77 //purpose  : 
78 //=======================================================================
79
80 Standard_Boolean ShHealOper_Sewing::Perform(const TopTools_SequenceOfShape& theSeqShapes)
81 {
82   return sewing(theSeqShapes);
83 }
84
85 //=======================================================================
86 //function : sewing
87 //purpose  : 
88 //=======================================================================
89
90 Standard_Boolean ShHealOper_Sewing::sewing(const TopTools_SequenceOfShape& theSeqShapes)
91 {
92   myDone = Standard_False;
93   myErrorStatus = ShHealOper_NotError;
94   if(myInitShape.IsNull()) {
95     myErrorStatus = ShHealOper_InvalidParameters;
96     return myDone;
97   }
98   //sewing shape
99   Handle(BRepBuilderAPI_Sewing) aSewing = new BRepBuilderAPI_Sewing;
100   aSewing->Load(myInitShape);
101   aSewing->SetTolerance(myTolerance);
102   aSewing->SetFaceMode(myFacesMode);
103   aSewing->SetFloatingEdgesMode(myEdgesMode);
104   aSewing->SetNonManifoldMode(myNonManifoldMode);
105   Standard_Integer j =1;
106   for( ; j <= theSeqShapes.Length();j++)
107     aSewing->Add(theSeqShapes.Value(j));
108
109   aSewing->Perform();
110   const TopoDS_Shape aSewShape = aSewing->SewedShape();
111   if(aSewShape.IsNull()) {
112     myErrorStatus = ShHealOper_ErrorExecution;
113     return myDone;
114   }
115   if(aSewShape.IsSame(myInitShape))
116     return myDone;
117
118   //analysis either sewing was made by changing number of shells
119   myDone = isSewed(aSewShape);
120
121   //keep modification of the subshapes in the Context.
122   TopExp_Explorer aExp(myInitShape,TopAbs_FACE);
123   for( ; aExp.More(); aExp.Next())
124     myDone = (getModifications( aExp.Current(),aSewing) || myDone);
125
126   TopoDS_Shape aTempShape = myContext->Apply(aSewShape);
127   //obtained shells with fixed orientation for manifold and nonmanifold shells
128   if(myFacesMode) 
129     myDone = getShells(aTempShape) || myDone;
130
131   //obtained manifold wires if sewing edges was performed.
132   if(myEdgesMode) 
133     myDone = getWires(aTempShape) || myDone;
134   
135   if(myDone)
136     myResultShape = myContext->Apply(aTempShape);
137     
138   return myDone;
139 }
140 //=======================================================================
141 //function : getShells
142 //purpose  : 
143 //=======================================================================
144
145 Standard_Boolean ShHealOper_Sewing::getShells(const TopoDS_Shape& theSewShape) const
146 {
147   Standard_Boolean isDone = Standard_False;
148   TopoDS_Shape aTmpShape = theSewShape;
149   if(myNonManifoldMode) {
150     TopoDS_Shell tempShell;
151     BRep_Builder aB;
152     aB.MakeShell(tempShell);
153     
154     for(TopExp_Explorer aExpf(theSewShape,TopAbs_FACE);aExpf.More(); aExpf.Next()) 
155       aB.Add(tempShell,aExpf.Current());
156     aTmpShape = tempShell;
157     myContext->Replace(theSewShape,aTmpShape);
158   }
159   
160   Handle(ShapeFix_Shell) asfs = new ShapeFix_Shell;
161   asfs->SetContext(myContext);
162   TopExp_Explorer aexpShell(aTmpShape,TopAbs_SHELL);
163   for ( ; aexpShell.More(); aexpShell.Next()) 
164     isDone = (asfs->FixFaceOrientation(TopoDS::Shell(aexpShell.Current()),
165                                        Standard_True,myNonManifoldMode) || isDone);
166   
167   return isDone;
168 }
169 //=======================================================================
170 //function : getWires
171 //purpose  : 
172 //=======================================================================
173
174 Standard_Boolean ShHealOper_Sewing::getWires(const TopoDS_Shape& theSewShape) const
175 {
176   if(theSewShape.ShapeType() != TopAbs_COMPOUND)
177     return Standard_False;
178   
179   Handle(TopTools_HSequenceOfShape) aSeqEdges = new TopTools_HSequenceOfShape;
180   TopExp_Explorer aexpEdges(theSewShape,TopAbs_EDGE,TopAbs_WIRE);
181   for ( ; aexpEdges.More(); aexpEdges.Next()) {
182     aSeqEdges->Append(aexpEdges.Current());
183   }
184   if(aSeqEdges->Length() <2)
185     return Standard_False;
186   //get manifold wires from sewed edges.
187   Standard_Real aTol = 0.;
188   Standard_Boolean aShared = Standard_True;
189   Handle(TopTools_HSequenceOfShape) aTmpWires = new TopTools_HSequenceOfShape;
190   ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdges, aTol, aShared, aTmpWires);
191
192   TopTools_MapOfShape aMapEdges;
193   Standard_Integer i =1;
194   for( ; i <= aSeqEdges->Length(); i++)
195     aMapEdges.Add(aSeqEdges->Value(i));
196   
197   //remove free edges from result shape.
198   TopoDS_Compound aNewComp;
199   deleteFreeEdges(theSewShape,aMapEdges,aNewComp);
200   
201   //add new wires in the result shape.
202   BRep_Builder aB;
203   for( i =1; i <= aTmpWires->Length(); i++) {
204     TopoDS_Iterator aite(aTmpWires->Value(i));
205     Standard_Integer nbe =0;
206     TopoDS_Shape aE;
207     for( ; aite.More() && nbe < 3; aite.Next(),nbe++)
208       aE = aite.Value();
209     if(!nbe)
210       continue;
211     else if(nbe ==1)
212       aB.Add(aNewComp,aE);
213     else
214       aB.Add(aNewComp,aTmpWires->Value(i));
215   }
216   
217   myContext->Replace(theSewShape,aNewComp);
218   return Standard_True;
219 }
220 //=======================================================================
221 //function : getHistory
222 //purpose  : 
223 //=======================================================================
224
225 Standard_Boolean ShHealOper_Sewing::getModifications(const TopoDS_Shape& theShape,
226                                                      const Handle(BRepBuilderAPI_Sewing)& theSewing) const
227 {
228   if((Standard_Integer)theShape.ShapeType() > (Standard_Integer)myHistoryLevel)
229     return Standard_False;
230   
231   Standard_Boolean isDone = theSewing->IsModifiedSubShape(theShape);
232   if(isDone) {
233     TopoDS_Shape aNewShape = theSewing->ModifiedSubShape(theShape);
234     myContext->Replace(theShape,aNewShape);
235   }
236   TopoDS_Iterator aIt(theShape);
237   for( ; aIt.More(); aIt.Next()) {
238     isDone = (getModifications( aIt.Value(),theSewing) || isDone);
239   }
240   return isDone;
241     
242 }
243 //=======================================================================
244 //function : isSewed
245 //purpose  : 
246 //=======================================================================
247
248 Standard_Boolean ShHealOper_Sewing::isSewed(const TopoDS_Shape& theShape) const
249 {
250   Standard_Integer nbNewShells =0;
251   Standard_Integer nbOldShells =0;
252   TopExp_Explorer aExpShells(theShape,TopAbs_SHELL);
253   for( ; aExpShells.More(); aExpShells.Next())
254     nbNewShells++;
255   for( aExpShells.Init(myInitShape,TopAbs_SHELL); aExpShells.More(); aExpShells.Next())
256     nbOldShells++;
257   return (nbNewShells != nbOldShells);
258 }
259 //=======================================================================
260 //function : deleteFreeEdges
261 //purpose  : 
262 //=======================================================================
263
264 void ShHealOper_Sewing::deleteFreeEdges(const TopoDS_Shape& theSewShape, 
265                                         TopTools_MapOfShape& theMapEdges,
266                                         TopoDS_Compound& theNewComp) const
267 {
268  
269   BRep_Builder aB;
270   aB.MakeCompound(theNewComp);
271   TopoDS_Iterator aIt(theSewShape);
272   
273   for( ; aIt.More(); aIt.Next()) {
274     if((aIt.Value().ShapeType() == TopAbs_EDGE ) && theMapEdges.Contains(aIt.Value()))
275       continue;
276     else if(aIt.Value().ShapeType() == TopAbs_COMPOUND) {
277       TopoDS_Compound aC;
278       deleteFreeEdges(aIt.Value(),theMapEdges,aC);
279       aB.Add(theNewComp,aC);
280     }
281     else
282       aB.Add(theNewComp,aIt.Value());
283   }
284 }