1 // Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File: ShHealOper_Sewing.cxx
24 // Created: 29.04.04 15:07:10
25 // Author: Galina KULIKOVA
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>
38 #include <TopoDS_Iterator.hxx>
39 #include <TopTools_MapOfShape.hxx>
40 //=======================================================================
41 //function : ShHealOper_Sewing()
42 //purpose : Constructor
43 //=======================================================================
46 ShHealOper_Sewing::ShHealOper_Sewing (const TopoDS_Shape& theShape,
47 const Standard_Real theTolerance)
50 myTolerance = theTolerance;
52 //=======================================================================
55 //=======================================================================
57 void ShHealOper_Sewing::Init(const TopoDS_Shape& theShape)
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;
66 //=======================================================================
69 //=======================================================================
71 Standard_Boolean ShHealOper_Sewing::Perform()
73 TopTools_SequenceOfShape theSeqShapes;
74 return sewing(theSeqShapes);
76 //=======================================================================
79 //=======================================================================
81 Standard_Boolean ShHealOper_Sewing::Perform(const TopTools_SequenceOfShape& theSeqShapes)
83 return sewing(theSeqShapes);
86 //=======================================================================
89 //=======================================================================
91 Standard_Boolean ShHealOper_Sewing::sewing(const TopTools_SequenceOfShape& theSeqShapes)
93 myDone = Standard_False;
94 myErrorStatus = ShHealOper_NotError;
95 if(myInitShape.IsNull()) {
96 myErrorStatus = ShHealOper_InvalidParameters;
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));
111 const TopoDS_Shape aSewShape = aSewing->SewedShape();
112 if(aSewShape.IsNull()) {
113 myErrorStatus = ShHealOper_ErrorExecution;
116 if(aSewShape.IsSame(myInitShape))
119 //analysis either sewing was made by changing number of shells
120 myDone = isSewed(aSewShape);
122 //keep modification of the subshapes in the Context.
123 TopExp_Explorer aExp(myInitShape,TopAbs_FACE);
124 for( ; aExp.More(); aExp.Next())
125 myDone = (getModifications( aExp.Current(),aSewing) || myDone);
127 TopoDS_Shape aTempShape = myContext->Apply(aSewShape);
128 //obtained shells with fixed orientation for manifold and nonmanifold shells
130 myDone = getShells(aTempShape) || myDone;
132 //obtained manifold wires if sewing edges was performed.
134 myDone = getWires(aTempShape) || myDone;
137 myResultShape = myContext->Apply(aTempShape);
141 //=======================================================================
142 //function : getShells
144 //=======================================================================
146 Standard_Boolean ShHealOper_Sewing::getShells(const TopoDS_Shape& theSewShape) const
148 Standard_Boolean isDone = Standard_False;
149 TopoDS_Shape aTmpShape = theSewShape;
150 if(myNonManifoldMode) {
151 TopoDS_Shell tempShell;
153 aB.MakeShell(tempShell);
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);
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);
170 //=======================================================================
171 //function : getWires
173 //=======================================================================
175 Standard_Boolean ShHealOper_Sewing::getWires(const TopoDS_Shape& theSewShape) const
177 if(theSewShape.ShapeType() != TopAbs_COMPOUND)
178 return Standard_False;
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());
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);
193 TopTools_MapOfShape aMapEdges;
194 Standard_Integer i =1;
195 for( ; i <= aSeqEdges->Length(); i++)
196 aMapEdges.Add(aSeqEdges->Value(i));
198 //remove free edges from result shape.
199 TopoDS_Compound aNewComp;
200 deleteFreeEdges(theSewShape,aMapEdges,aNewComp);
202 //add new wires in the result shape.
204 for( i =1; i <= aTmpWires->Length(); i++) {
205 TopoDS_Iterator aite(aTmpWires->Value(i));
206 Standard_Integer nbe =0;
208 for( ; aite.More() && nbe < 3; aite.Next(),nbe++)
215 aB.Add(aNewComp,aTmpWires->Value(i));
218 myContext->Replace(theSewShape,aNewComp);
219 return Standard_True;
221 //=======================================================================
222 //function : getHistory
224 //=======================================================================
226 Standard_Boolean ShHealOper_Sewing::getModifications(const TopoDS_Shape& theShape,
227 const Handle(BRepBuilderAPI_Sewing)& theSewing) const
229 if((Standard_Integer)theShape.ShapeType() > (Standard_Integer)myHistoryLevel)
230 return Standard_False;
232 Standard_Boolean isDone = theSewing->IsModifiedSubShape(theShape);
234 TopoDS_Shape aNewShape = theSewing->ModifiedSubShape(theShape);
235 myContext->Replace(theShape,aNewShape);
237 TopoDS_Iterator aIt(theShape);
238 for( ; aIt.More(); aIt.Next()) {
239 isDone = (getModifications( aIt.Value(),theSewing) || isDone);
244 //=======================================================================
247 //=======================================================================
249 Standard_Boolean ShHealOper_Sewing::isSewed(const TopoDS_Shape& theShape) const
251 Standard_Integer nbNewShells =0;
252 Standard_Integer nbOldShells =0;
253 TopExp_Explorer aExpShells(theShape,TopAbs_SHELL);
254 for( ; aExpShells.More(); aExpShells.Next())
256 for( aExpShells.Init(myInitShape,TopAbs_SHELL); aExpShells.More(); aExpShells.Next())
258 return (nbNewShells != nbOldShells);
260 //=======================================================================
261 //function : deleteFreeEdges
263 //=======================================================================
265 void ShHealOper_Sewing::deleteFreeEdges(const TopoDS_Shape& theSewShape,
266 TopTools_MapOfShape& theMapEdges,
267 TopoDS_Compound& theNewComp) const
271 aB.MakeCompound(theNewComp);
272 TopoDS_Iterator aIt(theSewShape);
274 for( ; aIt.More(); aIt.Next()) {
275 if((aIt.Value().ShapeType() == TopAbs_EDGE ) && theMapEdges.Contains(aIt.Value()))
277 else if(aIt.Value().ShapeType() == TopAbs_COMPOUND) {
279 deleteFreeEdges(aIt.Value(),theMapEdges,aC);
280 aB.Add(theNewComp,aC);
283 aB.Add(theNewComp,aIt.Value());