summaryrefslogtreecommitdiff
path: root/src/ShapeUpgrade/ShapeUpgrade_RemoveLocations.cxx
blob: 33c16f7209a607f74653d6ad2fcb1316ec89381a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include <ShapeUpgrade_RemoveLocations.ixx>
#include <TopAbs_ShapeEnum.hxx>
#include <TopLoc_Location.hxx>
#include <TopoDS_Face.hxx>
#include <TopoDS.hxx>
#include <Geom_Surface.hxx>
#include <BRep_Builder.hxx>
#include <TopoDS_Shape.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS_Vertex.hxx>
#include <TopExp.hxx>
#include <Geom_Curve.hxx>
#include <BRep_Tool.hxx>
#include <gp_Pnt.hxx>
#include <TopoDS_Iterator.hxx>
#include <TopTools_DataMapOfShapeShape.hxx>  
#include <Geom2d_Curve.hxx>
#include <Geom_Geometry.hxx>
#include <gp_Trsf.hxx>
  
#include <BRep_ListIteratorOfListOfCurveRepresentation.hxx>
#include <BRep_TEdge.hxx>
#include <BRep_GCurve.hxx>
#include <BRep_CurveRepresentation.hxx>
#include <TColStd_ListIteratorOfListOfTransient.hxx>
#include <TopExp_Explorer.hxx>
#include <TColStd_ListOfTransient.hxx>
//#include <ShapeUpgrade_DataMapOfShapeListOfTransient.hxx>
#include <ShapeBuild_Edge.hxx>
#include <Geom_Plane.hxx>
  
//=======================================================================
//function : ShapeUpgrade_RemoveLocations
//purpose  : 
//=======================================================================

ShapeUpgrade_RemoveLocations::ShapeUpgrade_RemoveLocations()
{
  myLevelRemoving = TopAbs_SHAPE;
}

//=======================================================================
//function : Remove
//purpose  : 
//=======================================================================

 Standard_Boolean ShapeUpgrade_RemoveLocations::Remove(const TopoDS_Shape& theShape) 
{
  TopoDS_Shape aShape = theShape;
  myShape = aShape;
  TopAbs_ShapeEnum shtype = theShape.ShapeType();
  Standard_Boolean isRemoveLoc = ((shtype != TopAbs_COMPOUND && myLevelRemoving == TopAbs_SHAPE) || 
                   ((Standard_Integer)myLevelRemoving <= ((Standard_Integer)shtype)));
  TopoDS_Shape S;
  Standard_Boolean isDone = MakeNewShape(theShape,S,myShape,isRemoveLoc);
  
  return isDone;

}

//=======================================================================
//function : RebuildShape
//purpose  : 
//=======================================================================
static Standard_Boolean RebuildShape(const TopoDS_Face& theFace, TopoDS_Face& theNewFace)
{
  BRep_Builder aB;
  TopLoc_Location aLoc;
  Handle(Geom_Surface) aSurf = BRep_Tool::Surface(theFace,aLoc);
  Standard_Boolean isRebuild = Standard_False;
  if(!aLoc.IsIdentity()) {
    Handle(Geom_Surface) anewSurf =Handle(Geom_Surface)::DownCast( aSurf->Transformed(aLoc.Transformation()));
    aB.MakeFace(theNewFace,anewSurf,BRep_Tool::Tolerance(theFace));
    isRebuild = Standard_True;
  }
  return isRebuild;
}
//=======================================================================
//function : RebuildShape
//purpose  : 
//=======================================================================
static Standard_Boolean RebuildShape(const TopoDS_Edge& theEdge, TopoDS_Edge& theNewEdge,
                                     const TopoDS_Face& theFace, TopoDS_Face& theNewFace,
                                     Standard_Boolean isBound)
{
  Standard_Boolean isRebuild = Standard_False;
  BRep_Builder aB;
  if(!isBound) {
    Handle(Geom_Curve) C3d;
    TopLoc_Location aLoc;
    Standard_Real First3d,Last3d;
    C3d = BRep_Tool::Curve( theEdge,aLoc,First3d,Last3d);
    aB.MakeEdge(theNewEdge);
    if(!C3d.IsNull()) {
       if(!aLoc.IsIdentity()) {
         Handle(Geom_Curve) anewC3d = Handle(Geom_Curve)::DownCast(C3d->Transformed(aLoc.Transformation()));
         C3d = anewC3d;
       }
       
       aB.UpdateEdge(theNewEdge,C3d,BRep_Tool::Tolerance(theEdge));
       aB.Range(theNewEdge,First3d,Last3d);
     }
    theNewEdge.Orientation(theEdge.Orientation());
    if(BRep_Tool::Degenerated(theEdge))
      aB.Degenerated(theNewEdge,Standard_True);
    isRebuild = Standard_True;
  }
  if(!theFace.IsNull()) {
    Handle(Geom_Surface) aSurf = BRep_Tool::Surface(theFace);
    if(!aSurf->IsKind(STANDARD_TYPE(Geom_Plane))) {
      Handle(Geom2d_Curve) c2d,c2d1;
      Standard_Real First2d,Last2d;
      
      c2d= BRep_Tool::CurveOnSurface( theEdge,theFace,First2d,Last2d);
      if(BRep_Tool::IsClosed(theEdge,theFace)) {
        if(!BRep_Tool::IsClosed(theNewEdge,theNewFace)) {
          TopoDS_Edge tmpE = TopoDS::Edge(theEdge.Reversed());
          c2d1= BRep_Tool::CurveOnSurface(tmpE,theFace,First2d,Last2d);
          TopAbs_Orientation OrEdge = theNewEdge.Orientation();
          
          if(theNewFace.Orientation() == TopAbs_REVERSED)
            OrEdge = ( OrEdge == TopAbs_FORWARD ? TopAbs_REVERSED : TopAbs_FORWARD);
          
          if(OrEdge == TopAbs_FORWARD)
            aB.UpdateEdge(theNewEdge,c2d, c2d1,theNewFace,0);
          else aB.UpdateEdge(theNewEdge,c2d1, c2d,theNewFace,0);
          
        }
      }
      else 
        aB.UpdateEdge(theNewEdge,c2d,theNewFace,0);
      
      if(!c2d.IsNull() || !c2d1.IsNull())
        aB.Range(theNewEdge,theNewFace,First2d,Last2d);
    }
  }
  return isRebuild;
}
//=======================================================================
//function :  RebuildShape
//purpose  : 
//=======================================================================
static Standard_Boolean RebuildShape(const TopoDS_Vertex& theVertex, TopoDS_Vertex& theNewVertex)
{
  BRep_Builder aB;
  aB.MakeVertex(theNewVertex);
  theNewVertex.Orientation( theVertex.Orientation());
  gp_Pnt p1 = BRep_Tool::Pnt( theVertex);
  aB.UpdateVertex(theNewVertex,p1,BRep_Tool::Tolerance( theVertex));
  return Standard_True;
}
//=======================================================================
//function : MakeNewShape
//purpose  : 
//=======================================================================

Standard_Boolean ShapeUpgrade_RemoveLocations::MakeNewShape(const TopoDS_Shape& theShape,
                                     const TopoDS_Shape& theAncShape,
                                     TopoDS_Shape& theNewShape,                     
                                     const Standard_Boolean theRemoveLoc)
{
  Standard_Boolean isDone = Standard_False;
  TopoDS_Shape aNewShape;
  TopAbs_ShapeEnum shtype = theShape.ShapeType();
  BRep_Builder aB;
  TopoDS_Shape aShape = theShape;
  if(!theRemoveLoc && !theShape.Location().IsIdentity()) {
    TopLoc_Location nulLoc;
    aShape.Location(nulLoc);
  }
  Standard_Boolean isBound = myMapNewShapes.IsBound(aShape);
  if(isBound) {
     aNewShape= myMapNewShapes.Find(aShape);
     aNewShape.Orientation(theShape.Orientation());
     if(!theRemoveLoc && !theShape.Location().IsIdentity()) {
       TopLoc_Location aL = theShape.Location();
       aNewShape.Location(aL);
     }
     if(shtype != TopAbs_EDGE) {
       
       theNewShape = aNewShape;
    
      return Standard_True;
    }
  }
  
  
  Standard_Boolean isRemoveLoc = theRemoveLoc;
  if(!theRemoveLoc) {
    isRemoveLoc = ((shtype != TopAbs_COMPOUND && myLevelRemoving == TopAbs_SHAPE) || 
                   ((Standard_Integer)myLevelRemoving <= ((Standard_Integer)shtype)));
  }
    
  Standard_Boolean aRebuild = Standard_False;
  TopoDS_Shape anAncShape = theAncShape;
  if(shtype == TopAbs_FACE) 
    anAncShape = aShape;
  if(isRemoveLoc && (!aShape.Location().IsIdentity() || shtype == TopAbs_EDGE || shtype == TopAbs_FACE )) {
    
    //Rebuild geometry for shape with location.
    if(shtype == TopAbs_FACE) {
      TopoDS_Face anewFace;
      TopoDS_Face oldFace = TopoDS::Face(aShape);
      aRebuild = RebuildShape(oldFace,anewFace);
      if(aRebuild) {
        aNewShape = anewFace;
        myMapNewShapes.Bind(oldFace,aNewShape);
      }
      
    }
    else if(shtype == TopAbs_EDGE) {
      TopoDS_Edge oldEdge = TopoDS::Edge(aShape);
      TopoDS_Edge anewEdge;
      TopoDS_Face F,newFace;
      
      if(!anAncShape.IsNull()) {
        F = TopoDS::Face(anAncShape);
        newFace = F;
        if(myMapNewShapes.IsBound(F))
          newFace = TopoDS::Face(myMapNewShapes.Find(F));
      }
      if(isBound)
        anewEdge = TopoDS::Edge(aNewShape);
      aRebuild = RebuildShape(oldEdge,anewEdge,F,newFace,isBound);
      aNewShape = anewEdge;
        
    }
    else if(shtype == TopAbs_VERTEX) {
      TopoDS_Vertex aVnew;
      TopoDS_Vertex aV = TopoDS::Vertex(aShape);
      aRebuild = RebuildShape(aV,aVnew);
      if(aRebuild)
        aNewShape = aVnew;
    }
  }
  isDone = aRebuild;
  
  //Removing location from sub-shapes in dependance of LevelRemoving and re-building shape.
  
  if(!isBound) {
    if(!aRebuild) 
       aNewShape = theShape.EmptyCopied();
    TopLoc_Location oldLoc,nullloc;
    oldLoc = theShape.Location();
    if(!oldLoc.IsIdentity())
      aNewShape.Location(nullloc);
    TopAbs_Orientation orient = theShape.Orientation();
    aNewShape.Orientation(TopAbs_FORWARD);
    TopoDS_Iterator aIt(aShape,Standard_False,isRemoveLoc);
    for( ; aIt.More(); aIt.Next()) {
      TopoDS_Shape subshape = aIt.Value();
      TopoDS_Shape anewsubshape;
      Standard_Boolean isDoneSubShape = MakeNewShape(subshape,anAncShape,anewsubshape,isRemoveLoc);
      isDone = (isDone || isDoneSubShape);
      aB.Add(aNewShape,anewsubshape);
      
    }
    if(isDone) 
      aNewShape.Orientation(orient);
    else   aNewShape = aShape;
    myMapNewShapes.Bind(aShape,aNewShape);
    if(!theRemoveLoc && !oldLoc.IsIdentity())
      aNewShape.Location(oldLoc);
    
  }
  theNewShape = aNewShape;
  
  
  return (isDone || isBound);
}