summaryrefslogtreecommitdiff
path: root/src/BRepAlgo/BRepAlgo.cxx
blob: e9f1842b3655ce547993273d742f03dfe5bd2a31 (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
// File:	BRepAlgo.cxx
// Created:	Mon Mar 10 16:42:56 1997
// Author:	Stagiaire Francois DUMONT
//		<dum@brunox.paris1.matra-dtv.fr>


#include <BRepAlgo.ixx>
#include <BRepTools_WireExplorer.hxx>
#include <BRep_Tool.hxx>
#include <BRepLib_MakeEdge.hxx>
#include <BRepLib_MakeWire.hxx>
#include <gp_Pnt.hxx>
#include <GeomConvert.hxx>
#include <GeomLProp.hxx>
#include <Geom_TrimmedCurve.hxx>
#include <Precision.hxx>
#include <Standard_ConstructionError.hxx>
#include <TColGeom_Array1OfBSplineCurve.hxx>
#include <TColGeom_HArray1OfBSplineCurve.hxx>
#include <TColStd_Array1OfReal.hxx>
#include <TColStd_Array1OfBoolean.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS_Vertex.hxx>
#include <TopLoc_Location.hxx>
#include <TopExp.hxx>

//=======================================================================
//function : ConcatenateWire
//purpose  : 
//=======================================================================

TopoDS_Wire  BRepAlgo::ConcatenateWire(const TopoDS_Wire& W,
				       const GeomAbs_Shape Option,
				       const Standard_Real TolAngular) 
{


  Standard_Integer        nb_curve,                         //number of curves in the Wire
                          index;
  BRepTools_WireExplorer  WExp(W) ;
  TopoDS_Edge             edge;
  TopLoc_Location         L ;
  Standard_Real           First=0.,Last=0.,                       //extremal values for the curve
                          First0 =0.,
                          toler =0.,
                          tolleft,tolright;                 //Vertex tolerances
  TopoDS_Vertex           Vfirst,Vlast;                     //Vertex of the Wire
  gp_Pnt                  Pfirst,Plast;            //, Pint;  corresponding points

  BRepLib_MakeWire        MakeResult;                       
  Standard_Real           closed_tolerance =0.0;
  Standard_Boolean        closed_flag = Standard_False ;
  
  nb_curve = 0;
  
  while ( WExp.More()){                                     //computation of the curve number
    nb_curve++ ;
    WExp.Next();
  }
  
  if (nb_curve > 1) {
    TColGeom_Array1OfBSplineCurve tab(0,nb_curve-1);          //array of the wire's curve
    TColStd_Array1OfReal tabtolvertex(0,nb_curve-2);          //array of the tolerance's vertex
    
    WExp.Init(W);
    
    for (index=0 ;index<nb_curve; index++){                   //main loop
      edge = WExp.Current() ;
      tab(index)  = GeomConvert::CurveToBSplineCurve(new      //storage in a array 
						     Geom_TrimmedCurve(BRep_Tool::Curve(edge,L,First,Last),First,Last));
      tab(index)->Transform(L.Transformation());
      GeomConvert::C0BSplineToC1BSplineCurve(tab(index),Precision::Confusion());
      
      if (index >= 1){                                         //continuity test loop
	if (edge.Orientation()==TopAbs_REVERSED)
	  tab(index)->Reverse();
	tolleft=BRep_Tool::Tolerance(TopExp::LastVertex(edge));
	tolright=BRep_Tool::Tolerance(TopExp::FirstVertex(edge));
	tabtolvertex(index-1)=Max(tolleft,tolright);
      }
      
      if(index==0){                                           //storage of the first edge features
	First0=First;
	if(edge.Orientation()==TopAbs_REVERSED){             //(usefull for the closed wire) 
	  Vfirst=TopExp::LastVertex(edge);
	  tab(index)->Reverse();
	}
	else
	  Vfirst=TopExp::FirstVertex(edge);
      }
      
      if(index==nb_curve-1){                                  //storage of the last edge features
	if(edge.Orientation()==TopAbs_REVERSED)
	  Vlast=TopExp::FirstVertex(edge);
	else
	  Vlast=TopExp::LastVertex(edge);
      }
      WExp.Next() ; 
    }
  
    if (BRep_Tool::Tolerance(Vfirst)>BRep_Tool::Tolerance(Vlast)) //computation of the closing tolerance
      toler=BRep_Tool::Tolerance(Vfirst);
    else
      toler=BRep_Tool::Tolerance(Vlast);
    
    Pfirst=BRep_Tool::Pnt(Vfirst);
    Plast=BRep_Tool::Pnt(Vlast); 
    
    if ((Pfirst.Distance(Plast)<=toler)&&                   //C0 continuity test at the closing point
	(GeomLProp::Continuity(tab(nb_curve-1),tab(0),Last,First0,
			       Standard_True,Standard_True, 
			       toler, TolAngular)>=GeomAbs_G1)) 
      {
	closed_tolerance =toler;                                        //if ClosedG1!=0 it will be True and
	closed_flag = Standard_True ;
      }                                                        //with the toler value
    Handle(TColGeom_HArray1OfBSplineCurve)  concatcurve;     //array of the concatenated curves
    Handle(TColStd_HArray1OfInteger)        ArrayOfIndices;  //array of the remining Vertex
    if (Option==GeomAbs_G1)
      GeomConvert::ConcatG1(tab,
			    tabtolvertex,
			    concatcurve,
			    closed_flag,
			    closed_tolerance) ;    //G1 concatenation
    else
      GeomConvert::ConcatC1(tab,
			    tabtolvertex,
			    ArrayOfIndices,
			    concatcurve,
			    closed_flag,
			    closed_tolerance);   //C1 concatenation
    
    for (index=0;index<=(concatcurve->Length()-1);index++){    //building of the resulting Wire
      BRepLib_MakeEdge EdgeBuilder(concatcurve->Value(index));
      edge = EdgeBuilder.Edge();
      MakeResult.Add(edge);
    } 

  }
  else {
    TColGeom_Array1OfBSplineCurve tab(0,0);          //array of the wire's curve
    TColStd_Array1OfReal tabtolvertex(0,0);          //array of the tolerance's vertex 
    WExp.Init(W);
    
    edge = WExp.Current() ;
    tab(0)  = GeomConvert::CurveToBSplineCurve(new      //storage in a array 
						     Geom_TrimmedCurve(BRep_Tool::Curve(edge,L,First,Last),First,Last));
    tab(0)->Transform(L.Transformation());
    GeomConvert::C0BSplineToC1BSplineCurve(tab(0),Precision::Confusion());
    if (edge.Orientation()==TopAbs_REVERSED)
	  tab(0)->Reverse();
    tolleft=BRep_Tool::Tolerance(TopExp::LastVertex(edge));
    tolright=BRep_Tool::Tolerance(TopExp::FirstVertex(edge));
    tabtolvertex(0)=Max(tolleft,tolright);
    if(edge.Orientation()==TopAbs_REVERSED){             //(usefull for the closed wire) 
	  Vfirst=TopExp::LastVertex(edge);
	  Vlast=TopExp::FirstVertex(edge);
    }
    else {
      
      Vfirst=TopExp::FirstVertex(edge);
      Vlast = TopExp::LastVertex(edge) ;
    }
    Pfirst=BRep_Tool::Pnt(Vfirst);
    Plast=BRep_Tool::Pnt(Vlast); 
    if ((Pfirst.Distance(Plast)<=toler)&&                   //C0 continuity test at the closing point
	(GeomLProp::Continuity(tab(0),tab(0),Last,First,
			       Standard_True,Standard_True,
			       toler, TolAngular)>=GeomAbs_G1)) 
      {
	closed_tolerance =toler;                                        //if ClosedG1!=0 it will be True and
	closed_flag = Standard_True ;
      }                                                        //with the toler value
    Handle(TColGeom_HArray1OfBSplineCurve)  concatcurve;     //array of the concatenated curves
    Handle(TColStd_HArray1OfInteger)        ArrayOfIndices;  //array of the remining Vertex
    if (Option==GeomAbs_G1)
      GeomConvert::ConcatG1(tab,
			    tabtolvertex,
			    concatcurve,
			    closed_flag,
			    closed_tolerance) ;    //G1 concatenation
    else
      GeomConvert::ConcatC1(tab,
			    tabtolvertex,
			    ArrayOfIndices,
			    concatcurve,
			    closed_flag,
			    closed_tolerance);   //C1 concatenation
    
    for (index=0;index<=(concatcurve->Length()-1);index++){    //building of the resulting Wire
      BRepLib_MakeEdge EdgeBuilder(concatcurve->Value(index));
      edge = EdgeBuilder.Edge();
      MakeResult.Add(edge);
    } 
  }
  return MakeResult.Wire() ;  
  
}