summaryrefslogtreecommitdiff
path: root/src/TopoDSToStep/TopoDSToStep_WireframeBuilder.cxx
blob: 802cff2b0e057068f234642de9ae089dbfcb4657 (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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
//:S4134 abv 10.03.99: working methods moved from package TopoDSToGBWire
//:j1 modified by abv 22 Oct 98: CSR BUC60401
// - unused parts of code dropped
// - fixed trimming of circles and ellipses (radians used instead of degrees)
//szv#4 S4163

#include <TopoDSToStep_WireframeBuilder.ixx>
#include <TopoDSToStep_Tool.hxx>

#include <TColgp_Array1OfPnt.hxx>
#include <TColStd_Array1OfReal.hxx>
#include <TColStd_Array1OfInteger.hxx>
#include <TColStd_SequenceOfTransient.hxx>
#include <MoniTool_DataMapOfShapeTransient.hxx>
#include <TCollection_HAsciiString.hxx>
#include <TransferBRep_ShapeMapper.hxx>
#include <Transfer_FinderProcess.hxx>

#include <Precision.hxx>
#include <gp_Vec.hxx>

#include <Geom2d_Line.hxx>
#include <Geom_Surface.hxx>
#include <Geom_TrimmedCurve.hxx>
#include <Geom_BSplineCurve.hxx>
#include <Geom_Line.hxx>
#include <Geom_Circle.hxx>
#include <Geom_Ellipse.hxx>
#include <Geom_Plane.hxx>

#include <Adaptor3d_CurveOnSurface.hxx>
#include <BRepAdaptor_Curve.hxx>
#include <BRep_Tool.hxx>
#include <BRepTools.hxx>

#include <TopoDS.hxx>
#include <TopoDS_Vertex.hxx>
#include <TopoDS_Face.hxx>
#include <TopoDS_Shell.hxx>
#include <TopoDS_Iterator.hxx>
#include <TopExp_Explorer.hxx>
#include <TopLoc_Location.hxx>

#include <StepGeom_CartesianPoint.hxx>
#include <StepGeom_Curve.hxx>
#include <StepGeom_SurfaceCurve.hxx>
#include <StepGeom_SeamCurve.hxx>
#include <StepGeom_TrimmedCurve.hxx>
#include <StepGeom_TrimmingSelect.hxx>
#include <StepGeom_HArray1OfTrimmingSelect.hxx>

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

// ============================================================================
// Method  : TopoDSToStep_Builder::TopoDSToStep_Builder
// Purpose :
// ============================================================================

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

// ============================================================================
// Method  : TopoDSToStep_Builder::TopoDSToStep_Builder
// Purpose :
// ============================================================================

 TopoDSToStep_WireframeBuilder::TopoDSToStep_WireframeBuilder(const TopoDS_Shape& aShape, TopoDSToStep_Tool& aTool, const Handle(Transfer_FinderProcess)& FP)
{
  done = Standard_False;
  Init(aShape, aTool, FP);
}

void TopoDSToStep_WireframeBuilder::Init(const TopoDS_Shape& aShape, TopoDSToStep_Tool& /* T */, const Handle(Transfer_FinderProcess)& /* FP */)
{
  Handle(TColStd_HSequenceOfTransient) itemList =
    new TColStd_HSequenceOfTransient();
  MoniTool_DataMapOfShapeTransient aPmsMap;
  done = GetTrimmedCurveFromShape(aShape, aPmsMap, itemList);
  myResult = itemList;
}

// ============================================================================
// Method  : TopoDSToStep_Builder::Error
// Purpose :
// ============================================================================

TopoDSToStep_BuilderError TopoDSToStep_WireframeBuilder::Error() const 
{
	return myError;
}

// ============================================================================
// Method  : TopoDSToStep_Builder::Value
// Purpose :
// ============================================================================

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


// ============================================================================
//:S4134: abv 10 Mar 99: the methods below moved from package TopoDSToGBWire

#define Nbpt 23

static Handle(StepGeom_TrimmedCurve) MakeTrimmedCurve (const Handle(StepGeom_Curve) &C,
						       const Handle(StepGeom_CartesianPoint) P1, 
						       const Handle(StepGeom_CartesianPoint) P2, 
						       Standard_Real trim1,
						       Standard_Real trim2,
						       Standard_Boolean sense)
{
  Handle(StepGeom_HArray1OfTrimmingSelect) aSTS1 =
    new StepGeom_HArray1OfTrimmingSelect(1,2);
  StepGeom_TrimmingSelect tSel;
  tSel.SetValue(P1);
  aSTS1->SetValue(1,tSel);
  tSel.SetParameterValue(trim1);
  aSTS1->SetValue(2,tSel);
    
  Handle(StepGeom_HArray1OfTrimmingSelect) aSTS2 =
    new StepGeom_HArray1OfTrimmingSelect(1,2);
  tSel.SetValue(P2);
  aSTS2->SetValue(1,tSel);
  tSel.SetParameterValue(trim2);
  aSTS2->SetValue(2,tSel);
    
  Handle(TCollection_HAsciiString) empty = 
      new TCollection_HAsciiString("");
  Handle(StepGeom_TrimmedCurve) pmsTC = new StepGeom_TrimmedCurve;
  pmsTC->Init(empty,C,aSTS1,aSTS2,sense,StepGeom_tpParameter);
  return pmsTC;
}
  
Standard_Boolean TopoDSToStep_WireframeBuilder::
  GetTrimmedCurveFromEdge(const TopoDS_Edge& theEdge, 
			  const TopoDS_Face& aFace, 
			  MoniTool_DataMapOfShapeTransient& aMap, 
			  Handle(TColStd_HSequenceOfTransient)& curveList) const
{
  if (theEdge.Orientation() == TopAbs_INTERNAL  ||
      theEdge.Orientation() == TopAbs_EXTERNAL ) {
#ifdef DEB
    cout <<"Warning: TopoDSToStep_WireframeBuilder::GetTrimmedCurveFromEdge: Edge is internal or external; dropped" << endl;
#endif
    return Standard_False;
  }
  //szv#4:S4163:12Mar99 SGI warns
  TopoDS_Shape sh = theEdge.Oriented(TopAbs_FORWARD);
  TopoDS_Edge anEdge = TopoDS::Edge ( sh );

  // resulting curve
  Handle(StepGeom_Curve) Gpms;
  
  if ( aMap.IsBound(anEdge)) {
    Gpms = Handle(StepGeom_Curve)::DownCast ( aMap.Find(anEdge) );
    if ( Gpms.IsNull() ) return Standard_False;
//??    curveList->Append(Gpms);
    return Standard_True;
  }
    
  BRepAdaptor_Curve CA ( anEdge );

  // Vertices
  TopoDS_Vertex Vfirst, Vlast;
  Handle(StepGeom_CartesianPoint) pmsP1, pmsP2; 
  for (TopoDS_Iterator It(anEdge);It.More();It.Next()) {
    // Translates the Edge Vertices
    TopoDS_Vertex V = TopoDS::Vertex(It.Value());
    gp_Pnt gpP = BRep_Tool::Pnt(V);
    if ( V.Orientation() == TopAbs_FORWARD ) {
      Vfirst = V;
      // 1.point for trimming
      GeomToStep_MakeCartesianPoint gtpP(gpP);
      pmsP1 = gtpP.Value();
    }
    if ( V.Orientation() == TopAbs_REVERSED ) {
      Vlast = V;
      // 2.point for trimming
      GeomToStep_MakeCartesianPoint gtpP(gpP);
      pmsP2 = gtpP.Value();
    }
  }
    
  // ---------------------------------------
  // Translate 3D representation of the Edge
  // ---------------------------------------
    
    
  // Handle(Geom_Curve) C = CA.Curve().Curve();

  // UPDATE FMA 26-02-96 
  // General remark : this full code should be deaply reworked
  //                  Too many objects are not used !

  Standard_Real First, Last;
  Handle(Geom_Curve) C = BRep_Tool::Curve(anEdge, First, Last); 
  if ( ! C.IsNull() ) {
    if (C->IsKind(STANDARD_TYPE(Geom_TrimmedCurve)))
      C = Handle(Geom_TrimmedCurve)::DownCast(C)->BasisCurve();
    GeomToStep_MakeCurve  gtpC(C);
    Handle(StepGeom_Curve) pmsC = gtpC.Value();

    // trim the curve
    Standard_Real trim1 = CA.FirstParameter();
    Standard_Real trim2 = CA.LastParameter();
/* //:j1 abv 22 Oct 98: radians are used in the produced STEP file (at least by default)
   if(C->IsKind(STANDARD_TYPE(Geom_Circle)) ||
       C->IsKind(STANDARD_TYPE(Geom_Ellipse))) {
      Standard_Real fact = 180. / PI;
      trim1 = trim1 * fact;
      trim2 = trim2 * fact;
    }
*/
    Gpms = MakeTrimmedCurve (pmsC, pmsP1, pmsP2, trim1, trim2, Standard_True );
//			     (anEdge.Orientation() == TopAbs_FORWARD));
  }
  else {

    // -------------------------
    // a 3D Curve is constructed
    // -------------------------

    Standard_Boolean iaplan = Standard_False;
    if ( ! aFace.IsNull() ) {
      Standard_Real cf, cl;
      Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface(anEdge, aFace, cf, cl);
      Handle(Geom_Surface) S = BRep_Tool::Surface(aFace);
      if (S->IsKind(STANDARD_TYPE(Geom_Plane)) &&
	  C2d->IsKind(STANDARD_TYPE(Geom2d_Line))) iaplan = Standard_True;
    }

    // to be modified : cf and cl are the topological trimming prameter
    // these are computed after ! (U1 and U2) -> cf and cl instead
    if (iaplan) {
      gp_Pnt Pnt1 = CA.Value(CA.FirstParameter()), Pnt2 = CA.Value(CA.LastParameter());
      gp_Vec V ( Pnt1, Pnt2 );
      Standard_Real length = V.Magnitude();
      if ( length >= Precision::Confusion() ) {
	Handle(Geom_Line) L = new Geom_Line(Pnt1, gp_Dir(V));
	GeomToStep_MakeLine gtpL(L);
	Gpms = gtpL.Value();
	Gpms = MakeTrimmedCurve (gtpL.Value(), pmsP1, pmsP2, 0, length, Standard_True );
//				 (anEdge.Orientation() == TopAbs_FORWARD));
      }
#ifdef DEB
      else cout << "Warning: TopoDSToStep_WireframeBuilder::GetTrimmedCurveFromEdge: Null-length curve not mapped" << endl;
#endif
    }
    else {
      TColgp_Array1OfPnt Points(1,Nbpt);
      TColStd_Array1OfReal Knots(1,Nbpt);
      TColStd_Array1OfInteger Mult(1,Nbpt);
      Standard_Real U1 = CA.FirstParameter();
      Standard_Real U2 = CA.LastParameter();
      for ( Standard_Integer i=1; i<=Nbpt; i++ ) {
	Standard_Real U = U1 + (i-1)*(U2 - U1)/(Nbpt - 1);
	gp_Pnt 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 gtpC(Bs);
      Gpms = gtpC.Value();
    }
  }
  if( Gpms.IsNull() ) return Standard_False;

  aMap.Bind(anEdge, Gpms);
  curveList->Append(Gpms);
  return Standard_True;
}


Standard_Boolean TopoDSToStep_WireframeBuilder::
  GetTrimmedCurveFromFace(const TopoDS_Face& aFace, 
			  MoniTool_DataMapOfShapeTransient& aMap, 
			  Handle(TColStd_HSequenceOfTransient)& aCurveList) const
{
  TopoDS_Shape curShape;
  TopoDS_Edge  curEdge;
  TopExp_Explorer  exp;
  Standard_Boolean result = Standard_False; //szv#4:S4163:12Mar99 `done` hid one from this, initialisation needed

  for (exp.Init(aFace,TopAbs_EDGE); exp.More(); exp.Next()){
    curShape = exp.Current();
    curEdge  = TopoDS::Edge(curShape);
    if (GetTrimmedCurveFromEdge(curEdge, aFace, aMap, aCurveList)) result = Standard_True;
  }
  return result;
}

Standard_Boolean TopoDSToStep_WireframeBuilder::
  GetTrimmedCurveFromShape(const TopoDS_Shape& aShape, 
			   MoniTool_DataMapOfShapeTransient& aMap,  
			   Handle(TColStd_HSequenceOfTransient)& aCurveList) const
{
  TopoDS_Iterator  It;
  Standard_Boolean result = Standard_False; //szv#4:S4163:12Mar99 `done` hid one from this, initialisation needed

  //szv#4:S4163:12Mar99 optimized
  switch (aShape.ShapeType()) {
    case TopAbs_EDGE : {
      const TopoDS_Edge& curEdge = TopoDS::Edge(aShape);
      TopoDS_Face nulFace;
      result = GetTrimmedCurveFromEdge(curEdge, nulFace, aMap, aCurveList);
      break;
    }
    case TopAbs_WIRE : {
      TopoDS_Face nulFace;
      TopoDS_Shape curShape;
      TopoDS_Edge  curEdge;
      TopExp_Explorer  exp;

      for (exp.Init(aShape,TopAbs_EDGE); exp.More(); exp.Next()){
	curShape = exp.Current();
	curEdge  = TopoDS::Edge(curShape);
	if (GetTrimmedCurveFromEdge(curEdge, nulFace, aMap, aCurveList)) result = Standard_True;
      }
      break;
    }
    case TopAbs_FACE : {
      const TopoDS_Face& curFace = TopoDS::Face(aShape);
      result = GetTrimmedCurveFromFace(curFace, aMap, aCurveList);
      break;
    }
    case TopAbs_SHELL : {
      TopoDS_Shell Sh = TopoDS::Shell(aShape);	  
      It.Initialize(Sh);
      for (;It.More();It.Next()) {
	TopoDS_Face curFace = TopoDS::Face(It.Value());
	if (GetTrimmedCurveFromFace(curFace, aMap, aCurveList)) result = Standard_True;
#ifdef DEBUG
	if(!result) {
	  cout << "ERROR extracting trimmedCurve from Face" << endl;
	  //BRepTools::Dump(curFace,cout);  cout<<endl;
	}
#endif
      }
      break;
    }
    case TopAbs_SOLID : {
      It.Initialize(aShape);
      for (;It.More();It.Next()) {
	if  (It.Value().ShapeType() == TopAbs_SHELL) {
	  if (GetTrimmedCurveFromShape(It.Value(), aMap, aCurveList)) result = Standard_True;
	}
      } 
      break;
    }
    case TopAbs_COMPOUND : {
      It.Initialize(aShape);
      for (;It.More();It.Next()) {
/*	  if  ((It.Value().ShapeType() == TopAbs_SHELL) ||
	       (It.Value().ShapeType() == TopAbs_COMPOUND)) {
	    result = GetTrimmedCurveFromShape(It.Value(), aMap, aCurveList);
	    break;
	  }
	  else if (It.Value().ShapeType() == TopAbs_FACE) {
	    result = GetTrimmedCurveFromFace(TopoDS::Face(It.Value()), aMap, aCurveList);
	    break;
	  } */
	if (GetTrimmedCurveFromShape(It.Value(), aMap, aCurveList)) result = Standard_True;
      }
      break;
    }
    default : break;
  }
  return result;
}