summaryrefslogtreecommitdiff
path: root/src/TopoDSToStep/TopoDSToStep_MakeStepEdge.cxx
blob: a95e35b7afc29c513fe36ec592dd19415b7150d7 (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
// File:	TopoDSToStep_MakeStepEdge.cxx
// Created:	Wed Nov 30 14:25:33 1994
// Author:	Frederic MAUPAS
//		<fma@bibox>

#include <TopoDSToStep_MakeStepEdge.ixx>

#include <TopoDSToStep_MakeStepVertex.hxx>

#include <Geom_Line.hxx>
#include <Geom_Curve.hxx>
#include <Geom_Surface.hxx>
#include <Geom_Plane.hxx>
#include <Geom_BSplineCurve.hxx>

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

#include <BRepLib.hxx>
#include <BRep_Tool.hxx>
#include <TopoDS_Iterator.hxx>
#include <TopoDS.hxx>
#include <BRepAdaptor_Curve.hxx>
#include <BRepAdaptor_Surface.hxx>

#include <StepShape_Vertex.hxx>
#include <StepShape_EdgeCurve.hxx>
#include <StepGeom_Curve.hxx>
#include <StepGeom_HArray1OfPcurveOrSurface.hxx>
#include <StepGeom_SeamCurve.hxx>
#include <StepGeom_SurfaceCurve.hxx>

#include <GeomToStep_MakeCurve.hxx>
#include <GeomToStep_MakeLine.hxx>

#include <Adaptor3d_CurveOnSurface.hxx>

#include <TColgp_Array1OfPnt.hxx>
#include <TColgp_Array1OfPnt.hxx>
#include <TColStd_Array1OfReal.hxx>
#include <TColStd_Array1OfInteger.hxx>

#include <TopExp_Explorer.hxx>
#include <TopExp.hxx>
#include <TransferBRep_ShapeMapper.hxx>
#include <TCollection_HAsciiString.hxx>

// Processing of non-manifold topology (ssv; 11.11.2010)
#include <TransferBRep.hxx>
#include <Interface_Static.hxx>

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

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

TopoDSToStep_MakeStepEdge::TopoDSToStep_MakeStepEdge
(const TopoDS_Edge& E,
 TopoDSToStep_Tool& T,
 const Handle(Transfer_FinderProcess)& FP)
{
  done = Standard_False;
  Init(E, T, FP);
}

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

void TopoDSToStep_MakeStepEdge::Init(const TopoDS_Edge& aEdge, 
                                     TopoDSToStep_Tool& aTool,
                                     const Handle(Transfer_FinderProcess)& FP)
{
  // ------------------------------------------------------------------
  // The edge is given with its relative orientation (i.e. in the wire)
  // ------------------------------------------------------------------

  aTool.SetCurrentEdge(aEdge);

  // [BEGIN] Processing non-manifold topology (ssv; 11.11.2010)
  Standard_Boolean isNMMode = Interface_Static::IVal("write.step.nonmanifold");
  if (isNMMode) {
    Handle(StepShape_EdgeCurve) anEC;
    Handle(TransferBRep_ShapeMapper) aSTEPMapper = TransferBRep::ShapeMapper(FP, aEdge);
    if ( FP->FindTypedTransient(aSTEPMapper, STANDARD_TYPE(StepShape_EdgeCurve), anEC) ) {
      // Non-manifold topology detected
      myError  = TopoDSToStep_EdgeDone;
      myResult = anEC;
      done     = Standard_True;
      return;
    }
  }
  // [END] Processing non-manifold topology (ssv; 11.11.2010)

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

#define Nbpt 21
  TopoDS_Iterator  It;
  Standard_Integer i;
  Standard_Real    U, U1, U2;
  gp_Pnt           P;
  
  Standard_Boolean isSeam = BRep_Tool::IsClosed(aEdge, aTool.CurrentFace());
  
  //:i4 abv 02 Sep 98: ProSTEP TR8 Motor.rle f3 & f62: check that edge 
  // participates twice in the wires of the face before making it seam
  // (else it can have two pcurves on the same surface being shared by 
  // two faces on that surface)
  // This fix is necessary because sharing of surfaces is not preserved when 
  // writing faces to STEP (see TopoDSToSTEP_MakeStepFace)
  if ( isSeam ) {
    Standard_Integer count = 0;
    TopExp_Explorer exp ( aTool.CurrentFace(), TopAbs_EDGE );
    for ( ; exp.More(); exp.Next() )
      if ( aEdge.IsSame ( exp.Current() ) ) count++;
    if ( count < 2 ) isSeam = Standard_False;
  }

  BRepAdaptor_Curve   CA = BRepAdaptor_Curve(aEdge);
  BRepAdaptor_Surface SA = BRepAdaptor_Surface(aTool.CurrentFace());

  if (aEdge.Orientation() == TopAbs_INTERNAL  ||
      aEdge.Orientation() == TopAbs_EXTERNAL ) {
    Handle(TransferBRep_ShapeMapper) errShape =
      new TransferBRep_ShapeMapper(aEdge);
    FP->AddWarning(errShape, " Edge(internal/external) from Non Manifold Topology");
    myError = TopoDSToStep_NonManifoldEdge;
    done    = Standard_False;
    return;
  }
  
  // Vertices

  Handle(StepShape_Vertex) V1,V2;
  Handle(StepShape_TopologicalRepresentationItem) Gpms2;
  TopoDS_Vertex Vfirst, Vlast;

  TopExp::Vertices(aEdge,Vfirst, Vlast);

  TopoDSToStep_MakeStepVertex MkVertex;
  
  MkVertex.Init(Vfirst, aTool, FP);  
  if (MkVertex.IsDone())
    V1 = Handle(StepShape_Vertex)::DownCast(MkVertex.Value());
  else {
    Handle(TransferBRep_ShapeMapper) errShape =
      new TransferBRep_ShapeMapper(aEdge);
    FP->AddWarning(errShape, " First Vertex of Edge not mapped");
    myError = TopoDSToStep_EdgeOther;
    done    = Standard_False;
    return;
  }
  
  MkVertex.Init(Vlast, aTool, FP);
  if (MkVertex.IsDone())
    V2 = Handle(StepShape_Vertex)::DownCast(MkVertex.Value());
  else {
    Handle(TransferBRep_ShapeMapper) errShape =
      new TransferBRep_ShapeMapper(aEdge);
    FP->AddWarning(errShape, " Last Vertex of Edge not mapped");
    myError = TopoDSToStep_EdgeOther;
    done    = Standard_False;
    return;
  }
  
  // ---------------------------------------
  // Translate 3D representation of the Edge
  // ---------------------------------------
  
  Handle(StepGeom_Curve) Gpms;
  Handle(Geom_Curve) C = CA.Curve().Curve();
  if (!C.IsNull()) {
    C = Handle(Geom_Curve)::DownCast(C->Copy());
    gp_Trsf Tr1 = CA.Trsf();
    C->Transform(Tr1);
    GeomToStep_MakeCurve MkCurve(C);
    Gpms = MkCurve.Value();
  }
  else {
    
    // -------------------------
    // a 3D Curve is constructed 
    // -------------------------

#ifdef DEB
    cout << "Warning: TopoDSToStep_MakeStepEdge: edge without 3d curve; creating..." << endl;
#endif
    if ((SA.GetType() == GeomAbs_Plane) &&
	(CA.GetType() == GeomAbs_Line)) {
      U1 = CA.FirstParameter();
      U2 = CA.LastParameter();
      gp_Vec V = gp_Vec( CA.Value(U1), CA.Value(U2) );
      Handle(Geom_Line) L = 
	new Geom_Line(CA.Value(U1), gp_Dir(V));
      GeomToStep_MakeLine MkLine(L);
      Gpms = MkLine.Value();
    }
    else {
      // To Be Optimized : create an approximated BSpline
      //                   using GeomAPI_PointsToBSpline
      TColgp_Array1OfPnt Points(1,Nbpt);
      TColStd_Array1OfReal Knots(1,Nbpt);
      TColStd_Array1OfInteger Mult(1,Nbpt);
      U1 = CA.FirstParameter();
      U2 = CA.LastParameter();
      for ( i=1; i<=Nbpt; i++ ) {
	U = U1 + (i-1)*(U2 - U1)/(Nbpt - 1);
	P = CA.Value(U);
	Points.SetValue(i,P);
	Knots.SetValue(i,U);
	Mult.SetValue(i,1);
      }
      //Points.SetValue(1, BRep_Tool::Pnt(Vfirst));
      //Points.SetValue(Nbpt, BRep_Tool::Pnt(Vlast));
      Mult.SetValue(1,2);
      Mult.SetValue(Nbpt,2);
      Handle(Geom_BSplineCurve) Bs = 
	new Geom_BSplineCurve(Points, Knots, Mult, 1);
      GeomToStep_MakeCurve MkCurve(Bs);
      Gpms = MkCurve.Value();
    }
  }
  
  // ---------------------------------------------------------
  // Warning : if the edge is connected aGeom->Length = 2
  //           otherwise = 1 ;
  //           and enumeration is pscrPcurveS2 or pscrPcurveS1
  // This is corrected in the Write File phases !
  // ---------------------------------------------------------  

  //:abv 25.01.00 CAX-IF TRJ3
  // if PcurveMode is 1 (default), make surface_curve instead of simple 3d curve
  if ( aTool.PCurveMode() != 0 ) {
  
    Handle(StepGeom_HArray1OfPcurveOrSurface) aGeom =
      new StepGeom_HArray1OfPcurveOrSurface(1,2);
    Handle(TCollection_HAsciiString) aName = new TCollection_HAsciiString("");
  
    if (!isSeam) {
      Handle(StepGeom_SurfaceCurve) SurfaceCurve = new StepGeom_SurfaceCurve;
      SurfaceCurve->Init(aName, Gpms, aGeom, StepGeom_pscrPcurveS1);
      Gpms = SurfaceCurve;
    }
    else {
      Handle(StepGeom_SeamCurve) SeamCurve = new StepGeom_SeamCurve;
      SeamCurve->Init(aName, Gpms, aGeom, StepGeom_pscrPcurveS1);
      Gpms = SeamCurve;
    }
  }
  
  // Edge curve
  Handle(StepShape_EdgeCurve) Epms = new StepShape_EdgeCurve;
  Handle(TCollection_HAsciiString) aName = new TCollection_HAsciiString("");
  Epms->Init(aName, V1, V2, Gpms, Standard_True);
  
  aTool.Bind(aEdge, Epms);
  myError  = TopoDSToStep_EdgeDone;
  myResult = Epms;
  done     = Standard_True;
  return;
}

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

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

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

TopoDSToStep_MakeEdgeError TopoDSToStep_MakeStepEdge::Error() const 
{
  return myError;
}