summaryrefslogtreecommitdiff
path: root/src/TopoDSToStep/TopoDSToStep_MakeStepWire.cxx
blob: c5357efb2ddf39ba28ceec48cfbe045abeb5a0fe (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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// File:	TopoDSToStep_MakeStepWire.cxx
// Created:	Wed Nov 30 14:14:31 1994
// Author:	Frederic MAUPAS
//		<fma@bibox>
//szv#4 S4163

#include <TopoDSToStep_MakeStepWire.ixx>

#include <TopoDSToStep_MakeStepVertex.hxx>
#include <TopoDSToStep_MakeStepEdge.hxx>

#include <BRepTools_WireExplorer.hxx>

#include <TColStd_SequenceOfTransient.hxx>

#include <TopoDS_Iterator.hxx>

#include <BRep_Tool.hxx>

#include <Geom2d_Curve.hxx>
#include <Geom2d_Line.hxx>

#include <gp_Pnt.hxx>

#include <TopExp.hxx>

#include <TopoDS.hxx>

#include <StepShape_VertexPoint.hxx>
#include <StepShape_Edge.hxx>
#include <StepShape_EdgeLoop.hxx>
#include <StepShape_VertexLoop.hxx>
#include <StepShape_HArray1OfOrientedEdge.hxx>
#include <StepShape_OrientedEdge.hxx>
#include <StepShape_PolyLoop.hxx>
#include <StepGeom_CartesianPoint.hxx>
#include <StepGeom_HArray1OfCartesianPoint.hxx>
#include <TransferBRep_ShapeMapper.hxx>
#include <TCollection_HAsciiString.hxx>
#include <ShapeExtend_WireData.hxx>
#include <ShapeFix_Wire.hxx>
#include <ShapeAnalysis_Edge.hxx>

// ----------------------------------------------------------------------------
// Constructors
// ----------------------------------------------------------------------------

TopoDSToStep_MakeStepWire::TopoDSToStep_MakeStepWire()
{
  done = Standard_False;
}

TopoDSToStep_MakeStepWire::TopoDSToStep_MakeStepWire
(const TopoDS_Wire& W, 
 TopoDSToStep_Tool& T,
 const Handle(Transfer_FinderProcess)& FP)
{
  done = Standard_False;
  Init(W, T, FP);
}


// ----------------------------------------------------------------------------
// Method  : Init
// Purpose :
// ----------------------------------------------------------------------------

void TopoDSToStep_MakeStepWire::Init(const TopoDS_Wire& aWire, 
				    TopoDSToStep_Tool& aTool,
				    const Handle(Transfer_FinderProcess)& FP)
{

  // ----------------------------------------------------------------
  // The Wire is given in its relative orientation (i.e. in the face)
  // ----------------------------------------------------------------

  aTool.SetCurrentWire(aWire);

  if (aTool.IsBound(aWire)) {
    myError  = TopoDSToStep_WireDone;
    done     = Standard_True;
    myResult = aTool.Find(aWire);
    return;
  }

  TopoDS_Iterator  It;
  Standard_Integer i;
  
  if (aWire.Orientation() == TopAbs_INTERNAL ||
      aWire.Orientation() == TopAbs_EXTERNAL ) {
    Handle(TransferBRep_ShapeMapper) errShape =
      new TransferBRep_ShapeMapper(aWire);
    FP->AddWarning(errShape, " Wire(internal/external) from Non Manifold Topology");
    myError = TopoDSToStep_NonManifoldWire;
    done    = Standard_False;
    return;
  }

  BRepTools_WireExplorer      ItW;
  TopoDS_Edge                 CurrentEdge;
  TColStd_SequenceOfTransient mySeq;
  
  // --------
  // Polyloop
  // --------
  
  if (aTool.Faceted()) {
    Handle(StepShape_VertexPoint)                   VertexPoint;
    Handle(StepGeom_Point)                         Point;
    Handle(StepShape_TopologicalRepresentationItem) Gpms;
    TopoDS_Vertex                               TopoDSVertex1, TopoDSVertex2;
    
    TopoDSToStep_MakeStepVertex MkVertex;
//    TopoDS_Wire ForwardWire = TopoDS::Wire(aWire.Oriented(TopAbs_FORWARD));
    
    for (ItW.Init(aWire, aTool.CurrentFace()); 
	 ItW.More();ItW.Next()) {
      CurrentEdge = ItW.Current();
      if (CurrentEdge.Orientation() == TopAbs_FORWARD)
	TopExp::Vertices(CurrentEdge, TopoDSVertex1, TopoDSVertex2);
      else 
	TopExp::Vertices(CurrentEdge, TopoDSVertex2, TopoDSVertex1);
      
      MkVertex.Init(TopoDSVertex1, aTool, FP);
      if (MkVertex.IsDone()) {
	VertexPoint = Handle(StepShape_VertexPoint)::DownCast(MkVertex.Value());
	Point = VertexPoint->VertexGeometry();
	mySeq.Append(Point);
      }
      else {
	Handle(TransferBRep_ShapeMapper) errShape =
	  new TransferBRep_ShapeMapper(aWire);
	FP->AddWarning(errShape, " a Vertex Point not mapped");
	myError = TopoDSToStep_WireOther;
	done = Standard_False;
	return;
      }
    }
    Standard_Integer nbPoints = mySeq.Length();
    if (nbPoints>=3) {
      Handle(StepGeom_HArray1OfCartesianPoint) aPolygon =
	new StepGeom_HArray1OfCartesianPoint(1,nbPoints);
      for ( i=1; i<=nbPoints; i++) {
	aPolygon->SetValue(i, Handle(StepGeom_CartesianPoint)::
			   DownCast(mySeq.Value(i))); 
      }
      Handle(StepShape_PolyLoop) PL = new StepShape_PolyLoop();
      Handle(TCollection_HAsciiString) aName = 
	new TCollection_HAsciiString("");
      PL->Init(aName, aPolygon);
      
      aTool.Bind(aWire, PL);
      myError  = TopoDSToStep_WireDone;
      done     = Standard_True;
      myResult = PL;
      return;
    }
    else {
      Handle(TransferBRep_ShapeMapper) errShape =
	new TransferBRep_ShapeMapper(aWire);
      FP->AddWarning(errShape, " PolyLoop: Wire has less than 3 points");
      myError = TopoDSToStep_WireOther;
      done = Standard_False;
      return;
    }
  }
  
  // --------
  // EdgeLoop
  // --------
  
  else {

    Handle(StepShape_TopologicalRepresentationItem) Gpms;
    Handle(StepShape_Edge)                          Epms;
    Handle(StepShape_OrientedEdge)                  OrientedEdge;
    
    TopoDSToStep_MakeStepEdge MkEdge;

    //szv#4:S4163:12Mar99 SGI warns
    TopoDS_Shape sh = aWire.Oriented(TopAbs_FORWARD);
    const TopoDS_Wire ForwardWire = TopoDS::Wire(sh);
    // test 25-01-96 FMA  supprime CKY 2-JUN-1997, cf MakeStepFace->Face FWD]
    // remis CKY 9-DEC-1997 : chaque niveau se traite en FWD
//#11 rln 16/03/98
//TestRally8 file carter2.rle face#333 (wire is not sorted, not sorted edges are seam and iso-curve):
//aWire is REVERSED but ForwardWire is FORWARD, when exploding not connected seams their pcurves are
//returned in incorrect order (because of mismatched orientation)
//As a result not sorted edges are lost (not returned by BRepTools_WireExplorer)
//By the way, in the case of aTool.Faceted() aWire is used

//#11 ItW.Init(ForwardWire, aTool.CurrentFace());
//#11 for (;ItW.More();ItW.Next()) {
    Handle(ShapeFix_Wire) STW = new ShapeFix_Wire;
    STW->Load (ForwardWire);
    STW->FixReorder();
    Handle(ShapeExtend_WireData) sbwd = STW->WireData();
    Standard_Integer nb = sbwd->NbEdges();
    
    //:abv 04.05.00: CAX-IF TRJ4: writing complete sphere with single vertex_loop
    // check that whole wire is one seam (perhaps made of several seam edges)
    //pdn remove degenerated pcurves
    Handle(ShapeExtend_WireData) cwd = new ShapeExtend_WireData;
    Standard_Integer ie;
    for (ie = 1; ie <=nb; ie++) {
      TopoDS_Edge edge = sbwd->Edge(ie);
      if (!BRep_Tool::Degenerated(edge))
	cwd->Add(edge);
    }

    nb = cwd->NbEdges();
    if(nb%2 == 0 ) {
      for ( ie = 1; ie < nb; ie++) {
	if ( cwd->Edge(ie).IsSame(cwd->Edge(ie+1)) ) break;
      }
      if ( ie < nb ) {
	cwd->SetLast(ie);
	for ( ie=nb/2+1; ie <= nb; ie++ ) {
	  if ( ! cwd->Edge(ie).IsSame(cwd->Edge(nb-ie+1)) ) break;
	}
	if ( ie > nb ) { // make vertex_loop
	  ShapeAnalysis_Edge sae;
	  TopoDS_Vertex V = sae.FirstVertex(cwd->Edge(1));
	  TopoDSToStep_MakeStepVertex mkV ( V, aTool, FP );
	  Handle(StepShape_VertexLoop) vloop = new StepShape_VertexLoop;
	  Handle(TCollection_HAsciiString) name = new TCollection_HAsciiString ( "" );
	  vloop->Init ( name, Handle(StepShape_Vertex)::DownCast ( mkV.Value() ) );
	  aTool.Bind(aWire, vloop);
	  myError  = TopoDSToStep_WireDone;
	  done     = Standard_True;
	  myResult = vloop;
	  return;
	}
      }
    }
    nb = sbwd->NbEdges();
    
    for (Standard_Integer nEdge = 1; nEdge <= sbwd->NbEdges(); nEdge++) {
  
      CurrentEdge = sbwd->Edge(nEdge);
//#11 CurrentEdge = ItW.Current();
      
      //if (ItW.Current().Orientation() != ItW.Orientation())
      //cout << "DEBUG : Attention WireExplorer Orientation" << endl;

      // ---------------------------------
      // --- Is the edge Degenerated ? ---
      // ---------------------------------

      Standard_Real cf, cl;
      Handle(Geom2d_Curve) theC2d = 
	BRep_Tool::CurveOnSurface(CurrentEdge, aTool.CurrentFace(), cf, cl);
      //BRepAdaptor_Curve CA;
      //CA = BRepAdaptor_Curve(CurrentEdge, 
      //aTool.CurrentFace());
      //GeomAbs_CurveType typC = CA.CurveOnSurface().GetCurve().GetType();
      //if (typC == GeomAbs_Line && BRep_Tool::Degenerated(CurrentEdge)) {
	//Handle(TransferBRep_ShapeMapper) errShape =
	  //new TransferBRep_ShapeMapper(aWire);
	//FP->AddWarning(errShape, " EdgeLoop: Degenerated Pcurve not mapped");
      //}
      if ( //:abv 26Jan00, CAX-IF TRJ3: ! theC2d.IsNull() && theC2d->IsKind(STANDARD_TYPE(Geom2d_Line)) && 
	  BRep_Tool::Degenerated(CurrentEdge)) {
	Handle(TransferBRep_ShapeMapper) errShape =
	  new TransferBRep_ShapeMapper(aWire);
	FP->AddWarning(errShape, " EdgeLoop: Degenerated Pcurve not mapped");
	continue;
      }
      else {
	//szv#4:S4163:12Mar99 SGI warns
	//TopoDS_Shape ssh = CurrentEdge.Oriented(TopAbs_FORWARD);
	//const TopoDS_Edge ForwardEdge = TopoDS::Edge(ssh);

	MkEdge.Init(CurrentEdge, aTool, FP);
	if (MkEdge.IsDone()) {
	  OrientedEdge = new StepShape_OrientedEdge();
	  Epms = Handle(StepShape_Edge)::DownCast(MkEdge.Value());
	  Handle(TCollection_HAsciiString) aName = new TCollection_HAsciiString("");
	  OrientedEdge->Init(aName, Epms, (CurrentEdge.Orientation() == TopAbs_FORWARD));
	  mySeq.Append(OrientedEdge);
	}
	else {
	  Handle(TransferBRep_ShapeMapper) errShape =
	    new TransferBRep_ShapeMapper(aWire);
	  FP->AddWarning(errShape, " EdgeLoop: an Edge not mapped");
	  myError = TopoDSToStep_WireOther;
	  done    = Standard_False;
	  return;
	}
      }
    }
    Standard_Integer nbEdges = mySeq.Length();
    if ( nbEdges >0 ) {
      Handle(StepShape_HArray1OfOrientedEdge) aList =
	new StepShape_HArray1OfOrientedEdge(1,nbEdges);
      for ( i=1; i<=nbEdges; i++ ) {
	aList->SetValue(i, Handle(StepShape_OrientedEdge)::
			DownCast(mySeq.Value(i))); 
      }
      Handle(StepShape_EdgeLoop) Epmsl = new StepShape_EdgeLoop;
      Handle(TCollection_HAsciiString) aName = 
	new TCollection_HAsciiString("");
      Epmsl->Init(aName, aList);
      aTool.Bind(aWire, Epmsl);
      done = Standard_True;
      myResult = Epmsl;   
      return;
    }
    else {
      Handle(TransferBRep_ShapeMapper) errShape =
	new TransferBRep_ShapeMapper(aWire);
      FP->AddWarning(errShape, " No Edges of this Wire were mapped");
      myError = TopoDSToStep_WireOther;
      done = Standard_False;
      return;
    }
  }
}

// ----------------------------------------------------------------------------
// Method  : Value
// Purpose :
// ----------------------------------------------------------------------------

const Handle(StepShape_TopologicalRepresentationItem)& TopoDSToStep_MakeStepWire::Value() const 
{
  StdFail_NotDone_Raise_if(!done,"");
  return myResult;
}

// ----------------------------------------------------------------------------
// Method  : Error
// Purpose :
// ----------------------------------------------------------------------------

TopoDSToStep_MakeWireError TopoDSToStep_MakeStepWire::Error() const 
{
  return myError;
}