1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 // File: ShHealOper_Sewing.cxx
22 // Created: 29.04.04 15:07:10
23 // Author: Galina KULIKOVA
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>
37 #include <TopoDS_Iterator.hxx>
38 #include <TopTools_MapOfShape.hxx>
39 //=======================================================================
40 //function : ShHealOper_Sewing()
41 //purpose : Constructor
42 //=======================================================================
45 ShHealOper_Sewing::ShHealOper_Sewing (const TopoDS_Shape& theShape,
46 const Standard_Real theTolerance)
49 myTolerance = theTolerance;
51 //=======================================================================
54 //=======================================================================
56 void ShHealOper_Sewing::Init(const TopoDS_Shape& theShape)
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;
65 //=======================================================================
68 //=======================================================================
70 Standard_Boolean ShHealOper_Sewing::Perform()
72 TopTools_SequenceOfShape theSeqShapes;
73 return sewing(theSeqShapes);
75 //=======================================================================
78 //=======================================================================
80 Standard_Boolean ShHealOper_Sewing::Perform(const TopTools_SequenceOfShape& theSeqShapes)
82 return sewing(theSeqShapes);
85 //=======================================================================
88 //=======================================================================
90 Standard_Boolean ShHealOper_Sewing::sewing(const TopTools_SequenceOfShape& theSeqShapes)
92 myDone = Standard_False;
93 myErrorStatus = ShHealOper_NotError;
94 if(myInitShape.IsNull()) {
95 myErrorStatus = ShHealOper_InvalidParameters;
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));
110 const TopoDS_Shape aSewShape = aSewing->SewedShape();
111 if(aSewShape.IsNull()) {
112 myErrorStatus = ShHealOper_ErrorExecution;
115 if(aSewShape.IsSame(myInitShape))
118 //analysis either sewing was made by changing number of shells
119 myDone = isSewed(aSewShape);
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);
126 TopoDS_Shape aTempShape = myContext->Apply(aSewShape);
127 //obtained shells with fixed orientation for manifold and nonmanifold shells
129 myDone = getShells(aTempShape) || myDone;
131 //obtained manifold wires if sewing edges was performed.
133 myDone = getWires(aTempShape) || myDone;
136 myResultShape = myContext->Apply(aTempShape);
140 //=======================================================================
141 //function : getShells
143 //=======================================================================
145 Standard_Boolean ShHealOper_Sewing::getShells(const TopoDS_Shape& theSewShape) const
147 Standard_Boolean isDone = Standard_False;
148 TopoDS_Shape aTmpShape = theSewShape;
149 if(myNonManifoldMode) {
150 TopoDS_Shell tempShell;
152 aB.MakeShell(tempShell);
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);
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);
169 //=======================================================================
170 //function : getWires
172 //=======================================================================
174 Standard_Boolean ShHealOper_Sewing::getWires(const TopoDS_Shape& theSewShape) const
176 if(theSewShape.ShapeType() != TopAbs_COMPOUND)
177 return Standard_False;
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());
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);
192 TopTools_MapOfShape aMapEdges;
193 Standard_Integer i =1;
194 for( ; i <= aSeqEdges->Length(); i++)
195 aMapEdges.Add(aSeqEdges->Value(i));
197 //remove free edges from result shape.
198 TopoDS_Compound aNewComp;
199 deleteFreeEdges(theSewShape,aMapEdges,aNewComp);
201 //add new wires in the result shape.
203 for( i =1; i <= aTmpWires->Length(); i++) {
204 TopoDS_Iterator aite(aTmpWires->Value(i));
205 Standard_Integer nbe =0;
207 for( ; aite.More() && nbe < 3; aite.Next(),nbe++)
214 aB.Add(aNewComp,aTmpWires->Value(i));
217 myContext->Replace(theSewShape,aNewComp);
218 return Standard_True;
220 //=======================================================================
221 //function : getHistory
223 //=======================================================================
225 Standard_Boolean ShHealOper_Sewing::getModifications(const TopoDS_Shape& theShape,
226 const Handle(BRepBuilderAPI_Sewing)& theSewing) const
228 if((Standard_Integer)theShape.ShapeType() > (Standard_Integer)myHistoryLevel)
229 return Standard_False;
231 Standard_Boolean isDone = theSewing->IsModifiedSubShape(theShape);
233 TopoDS_Shape aNewShape = theSewing->ModifiedSubShape(theShape);
234 myContext->Replace(theShape,aNewShape);
236 TopoDS_Iterator aIt(theShape);
237 for( ; aIt.More(); aIt.Next()) {
238 isDone = (getModifications( aIt.Value(),theSewing) || isDone);
243 //=======================================================================
246 //=======================================================================
248 Standard_Boolean ShHealOper_Sewing::isSewed(const TopoDS_Shape& theShape) const
250 Standard_Integer nbNewShells =0;
251 Standard_Integer nbOldShells =0;
252 TopExp_Explorer aExpShells(theShape,TopAbs_SHELL);
253 for( ; aExpShells.More(); aExpShells.Next())
255 for( aExpShells.Init(myInitShape,TopAbs_SHELL); aExpShells.More(); aExpShells.Next())
257 return (nbNewShells != nbOldShells);
259 //=======================================================================
260 //function : deleteFreeEdges
262 //=======================================================================
264 void ShHealOper_Sewing::deleteFreeEdges(const TopoDS_Shape& theSewShape,
265 TopTools_MapOfShape& theMapEdges,
266 TopoDS_Compound& theNewComp) const
270 aB.MakeCompound(theNewComp);
271 TopoDS_Iterator aIt(theSewShape);
273 for( ; aIt.More(); aIt.Next()) {
274 if((aIt.Value().ShapeType() == TopAbs_EDGE ) && theMapEdges.Contains(aIt.Value()))
276 else if(aIt.Value().ShapeType() == TopAbs_COMPOUND) {
278 deleteFreeEdges(aIt.Value(),theMapEdges,aC);
279 aB.Add(theNewComp,aC);
282 aB.Add(theNewComp,aIt.Value());