Salome HOME
Merge with version on tag OCC-V2_1_0d
[modules/geom.git] / src / ShHealOper / ShHealOper_RemoveInternalWires.cxx
1 // File:      ShHealOper_RemoveInternalWires.cxx
2 // Created:   26.04.04 14:46:45
3 // Author:    Galina KULIKOVA
4 //  < MODULE = KERNEL> <PACKAGE = ShHealOper> : <Shape Healing Operations>
5 //  Copyright (C) 2003  CEA
6 //
7 //  This library is free software; you can redistribute it and/or
8 //  modify it under the terms of the GNU Lesser General Public
9 //  License as published by the Free Software Foundation; either
10 //  version 2.1 of the License.
11 //
12
13 //  This library is distributed in the hope that it will be useful,
14 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 //  Lesser General Public License for more details.
17 //
18 //  You should have received a copy of the GNU Lesser General Public
19 //  License along with this library; if not, write to the Free Software
20 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21 //
22
23
24 #include <ShHealOper_RemoveInternalWires.hxx>
25 #include <TopExp.hxx>
26 #include <TopExp_Explorer.hxx>
27 #include <TopAbs_ShapeEnum.hxx>
28 #include <TopoDS.hxx>
29 #include <TopTools_ListOfShape.hxx>
30 #include <TopTools_ListIteratorOfListOfShape.hxx>
31 #include <ShapeAnalysis.hxx>
32 #include <ShapeFix_Shape.hxx>
33
34 //=======================================================================
35 //function : ShHealOper_RemoveInternalWires()
36 //purpose  : Constructor
37 //=======================================================================
38
39 ShHealOper_RemoveInternalWires::ShHealOper_RemoveInternalWires ( const TopoDS_Shape& theShape )
40 {
41   Init(theShape);
42 }
43 //=======================================================================
44 //function : Init
45 //purpose  : 
46 //=======================================================================
47
48 void ShHealOper_RemoveInternalWires::Init(const TopoDS_Shape& theShape)
49 {
50   ShHealOper_Tool::Init(theShape);
51   myMapWiresFace.Clear();
52   TopExp::MapShapesAndAncestors(theShape,TopAbs_WIRE,TopAbs_FACE,myMapWiresFace);
53 }
54 //=======================================================================
55 //function : Remove
56 //purpose  : 
57 //=======================================================================
58
59 Standard_Boolean ShHealOper_RemoveInternalWires::Remove()
60 {
61   TopExp_Explorer aexpFaces(myInitShape,TopAbs_FACE);
62   //removes internal wires from all faces in the shape
63   Standard_Boolean isDone = Standard_False;
64   for( ; aexpFaces.More(); aexpFaces.Next())
65     isDone =  removeWire(TopoDS::Face(aexpFaces.Current()),TopoDS_Wire()) || isDone;
66   if(isDone ) {
67     TopoDS_Shape aNewShape = myContext->Apply(myInitShape);
68     //fix not-connected shell and solids obtained after removing wires
69     fixShape(aNewShape);
70   }
71   myDone = isDone;
72   return myDone;
73 }
74 //=======================================================================
75 //function : Remove
76 //purpose  : 
77 //=======================================================================
78
79 Standard_Boolean ShHealOper_RemoveInternalWires::Remove(const TopTools_SequenceOfShape& theRemovedShapes)
80 {
81   myDone = Standard_False;
82   
83   Standard_Integer i =1;
84   for( ; i <= theRemovedShapes.Length(); i++) {
85     //removes internal wires from specified faces.
86     if(theRemovedShapes.Value(i).ShapeType() == TopAbs_FACE)
87       myDone = (removeWire(TopoDS::Face(theRemovedShapes.Value(i)),TopoDS_Wire()) || myDone) ;
88     else if(theRemovedShapes.Value(i).ShapeType() == TopAbs_WIRE)
89     {
90       //removes specified internal wires. 
91       TopoDS_Wire awire = TopoDS::Wire(theRemovedShapes.Value(i));
92       if(myMapWiresFace.Contains(awire)) {
93         const TopTools_ListOfShape& aLfaces = myMapWiresFace.FindFromKey(awire);
94         TopTools_ListIteratorOfListOfShape liter(aLfaces);
95         for( ; liter.More(); liter.Next())
96           myDone = (removeWire(TopoDS::Face(liter.Value()),awire) || myDone);
97       }
98     }
99   }
100   if(myDone ) {
101     TopoDS_Shape aNewShape = myContext->Apply(myInitShape);
102
103     //fix not-connected shell and solids obtained after removing wires
104     fixShape(aNewShape);
105     
106   }
107   return myDone;
108 }
109 //=======================================================================
110 //function : removeWire
111 //purpose  : 
112 //=======================================================================
113
114 Standard_Boolean ShHealOper_RemoveInternalWires::removeWire(const TopoDS_Face& theFace, 
115                                                             const TopoDS_Wire& theWire)
116 {
117   TopoDS_Wire aBoundWire = ShapeAnalysis::OuterWire(theFace);
118   if(!theWire.IsNull() && aBoundWire.IsSame(theWire)) {
119     myErrorStatus = ShHealOper_InvalidParameters;
120     return Standard_False;
121   }
122   
123   Standard_Boolean isremove = Standard_False;
124   if(!theWire.IsNull()) {
125     myContext->Remove(theWire);
126     isremove= Standard_True;
127   }
128   else {  
129     TopExp_Explorer aExpW(theFace,TopAbs_WIRE);
130     for( ; aExpW.More(); aExpW.Next()) {
131       if(!aBoundWire.IsSame(aExpW.Current())) {
132         myContext->Remove(aExpW.Current());
133         isremove= Standard_True;
134       }
135     }
136   }
137   return isremove;
138 }
139 //=======================================================================
140 //function : fixShape
141 //purpose  : 
142 //=======================================================================
143
144 void ShHealOper_RemoveInternalWires::fixShape(const TopoDS_Shape& theShape )
145 {
146   Handle(ShapeFix_Shape) aFixTool = new ShapeFix_Shape(theShape);
147   aFixTool->SetContext(myContext);
148   aFixTool->FixShellTool()->FixFaceMode() = Standard_False;
149   aFixTool->FixFreeFaceMode() = Standard_False;
150   aFixTool->FixFreeWireMode() = Standard_False;
151   aFixTool->FixSameParameterMode() = Standard_False;
152   aFixTool->Perform();
153   myResultShape = aFixTool->Shape();
154 }