Salome HOME
87a463bddc1a2348e0840ca8bfbdb412e3ddbc9a
[modules/geom.git] / src / ShHealOper / ShHealOper_RemoveInternalWires.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_RemoveInternalWires.cxx
23 // Created:   26.04.04 14:46:45
24 // Author:    Galina KULIKOVA
25 //
26 #include <ShHealOper_RemoveInternalWires.hxx>
27 #include <TopExp.hxx>
28 #include <TopExp_Explorer.hxx>
29 #include <TopAbs_ShapeEnum.hxx>
30 #include <TopoDS.hxx>
31 #include <TopTools_ListOfShape.hxx>
32 #include <TopTools_ListIteratorOfListOfShape.hxx>
33 #include <ShapeAnalysis.hxx>
34 #include <ShapeFix_Shape.hxx>
35
36 //=======================================================================
37 //function : ShHealOper_RemoveInternalWires()
38 //purpose  : Constructor
39 //=======================================================================
40
41 ShHealOper_RemoveInternalWires::ShHealOper_RemoveInternalWires ( const TopoDS_Shape& theShape )
42 {
43   Init(theShape);
44 }
45 //=======================================================================
46 //function : Init
47 //purpose  : 
48 //=======================================================================
49
50 void ShHealOper_RemoveInternalWires::Init(const TopoDS_Shape& theShape)
51 {
52   ShHealOper_Tool::Init(theShape);
53   myMapWiresFace.Clear();
54   TopExp::MapShapesAndAncestors(theShape,TopAbs_WIRE,TopAbs_FACE,myMapWiresFace);
55 }
56 //=======================================================================
57 //function : Remove
58 //purpose  : 
59 //=======================================================================
60
61 Standard_Boolean ShHealOper_RemoveInternalWires::Remove()
62 {
63   TopExp_Explorer aexpFaces(myInitShape,TopAbs_FACE);
64   //removes internal wires from all faces in the shape
65   Standard_Boolean isDone = Standard_False;
66   for( ; aexpFaces.More(); aexpFaces.Next())
67     isDone =  removeWire(TopoDS::Face(aexpFaces.Current()),TopoDS_Wire()) || isDone;
68   if(isDone ) {
69     TopoDS_Shape aNewShape = myContext->Apply(myInitShape);
70     //fix not-connected shell and solids obtained after removing wires
71     fixShape(aNewShape);
72   }
73   myDone = isDone;
74   return myDone;
75 }
76 //=======================================================================
77 //function : Remove
78 //purpose  : 
79 //=======================================================================
80
81 Standard_Boolean ShHealOper_RemoveInternalWires::Remove(const TopTools_SequenceOfShape& theRemovedShapes)
82 {
83   myDone = Standard_False;
84   
85   Standard_Integer i =1;
86   for( ; i <= theRemovedShapes.Length(); i++) {
87     //removes internal wires from specified faces.
88     if(theRemovedShapes.Value(i).ShapeType() == TopAbs_FACE)
89       myDone = (removeWire(TopoDS::Face(theRemovedShapes.Value(i)),TopoDS_Wire()) || myDone) ;
90     else if(theRemovedShapes.Value(i).ShapeType() == TopAbs_WIRE)
91     {
92       //removes specified internal wires. 
93       TopoDS_Wire awire = TopoDS::Wire(theRemovedShapes.Value(i));
94       if(myMapWiresFace.Contains(awire)) {
95         const TopTools_ListOfShape& aLfaces = myMapWiresFace.FindFromKey(awire);
96         TopTools_ListIteratorOfListOfShape liter(aLfaces);
97         for( ; liter.More(); liter.Next())
98           myDone = (removeWire(TopoDS::Face(liter.Value()),awire) || myDone);
99       }
100     }
101   }
102   if(myDone ) {
103     TopoDS_Shape aNewShape = myContext->Apply(myInitShape);
104
105     //fix not-connected shell and solids obtained after removing wires
106     fixShape(aNewShape);
107     
108   }
109   return myDone;
110 }
111 //=======================================================================
112 //function : removeWire
113 //purpose  : 
114 //=======================================================================
115
116 Standard_Boolean ShHealOper_RemoveInternalWires::removeWire(const TopoDS_Face& theFace, 
117                                                             const TopoDS_Wire& theWire)
118 {
119   TopoDS_Wire aBoundWire = ShapeAnalysis::OuterWire(theFace);
120   if(!theWire.IsNull() && aBoundWire.IsSame(theWire)) {
121     myErrorStatus = ShHealOper_InvalidParameters;
122     return Standard_False;
123   }
124   
125   Standard_Boolean isremove = Standard_False;
126   if(!theWire.IsNull()) {
127     myContext->Remove(theWire);
128     isremove= Standard_True;
129   }
130   else {  
131     TopExp_Explorer aExpW(theFace,TopAbs_WIRE);
132     for( ; aExpW.More(); aExpW.Next()) {
133       if(!aBoundWire.IsSame(aExpW.Current())) {
134         myContext->Remove(aExpW.Current());
135         isremove= Standard_True;
136       }
137     }
138   }
139   return isremove;
140 }
141 //=======================================================================
142 //function : fixShape
143 //purpose  : 
144 //=======================================================================
145
146 void ShHealOper_RemoveInternalWires::fixShape(const TopoDS_Shape& theShape )
147 {
148   Handle(ShapeFix_Shape) aFixTool = new ShapeFix_Shape(theShape);
149   aFixTool->SetContext(myContext);
150   aFixTool->FixShellTool()->FixFaceMode() = Standard_False;
151   aFixTool->FixFreeFaceMode() = Standard_False;
152   aFixTool->FixFreeWireMode() = Standard_False;
153   aFixTool->FixSameParameterMode() = Standard_False;
154   aFixTool->Perform();
155   myResultShape = aFixTool->Shape();
156 }