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