0025418: Debug output to be limited to OCC development environment
[occt.git] / src / TopoDSToStep / TopoDSToStep_WireframeBuilder.cxx
CommitLineData
973c2be1 1// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 2//
973c2be1 3// This file is part of Open CASCADE Technology software library.
b311480e 4//
d5f74e42 5// This library is free software; you can redistribute it and/or modify it under
6// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 7// by the Free Software Foundation, with special exception defined in the file
8// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9// distribution for complete text of the license and disclaimer of any warranty.
b311480e 10//
973c2be1 11// Alternatively, this file may be used under the terms of Open CASCADE
12// commercial license or contractual agreement.
b311480e 13
7fd59977 14//:S4134 abv 10.03.99: working methods moved from package TopoDSToGBWire
15//:j1 modified by abv 22 Oct 98: CSR BUC60401
16// - unused parts of code dropped
17// - fixed trimming of circles and ellipses (radians used instead of degrees)
18//szv#4 S4163
19
20#include <TopoDSToStep_WireframeBuilder.ixx>
21#include <TopoDSToStep_Tool.hxx>
22
23#include <TColgp_Array1OfPnt.hxx>
24#include <TColStd_Array1OfReal.hxx>
25#include <TColStd_Array1OfInteger.hxx>
26#include <TColStd_SequenceOfTransient.hxx>
27#include <MoniTool_DataMapOfShapeTransient.hxx>
28#include <TCollection_HAsciiString.hxx>
29#include <TransferBRep_ShapeMapper.hxx>
30#include <Transfer_FinderProcess.hxx>
31
32#include <Precision.hxx>
33#include <gp_Vec.hxx>
34
35#include <Geom2d_Line.hxx>
36#include <Geom_Surface.hxx>
37#include <Geom_TrimmedCurve.hxx>
38#include <Geom_BSplineCurve.hxx>
39#include <Geom_Line.hxx>
40#include <Geom_Circle.hxx>
41#include <Geom_Ellipse.hxx>
42#include <Geom_Plane.hxx>
43
44#include <Adaptor3d_CurveOnSurface.hxx>
45#include <BRepAdaptor_Curve.hxx>
46#include <BRep_Tool.hxx>
47#include <BRepTools.hxx>
48
49#include <TopoDS.hxx>
50#include <TopoDS_Vertex.hxx>
51#include <TopoDS_Face.hxx>
52#include <TopoDS_Shell.hxx>
53#include <TopoDS_Iterator.hxx>
54#include <TopExp_Explorer.hxx>
55#include <TopLoc_Location.hxx>
56
57#include <StepGeom_CartesianPoint.hxx>
58#include <StepGeom_Curve.hxx>
59#include <StepGeom_SurfaceCurve.hxx>
60#include <StepGeom_SeamCurve.hxx>
61#include <StepGeom_TrimmedCurve.hxx>
62#include <StepGeom_TrimmingSelect.hxx>
63#include <StepGeom_HArray1OfTrimmingSelect.hxx>
64
65#include <GeomToStep_MakeCartesianPoint.hxx>
66#include <GeomToStep_MakeCurve.hxx>
67#include <GeomToStep_MakeLine.hxx>
68
69// ============================================================================
70// Method : TopoDSToStep_Builder::TopoDSToStep_Builder
71// Purpose :
72// ============================================================================
73
b311480e 74TopoDSToStep_WireframeBuilder::TopoDSToStep_WireframeBuilder()
7fd59977 75{
76 done = Standard_False;
77}
78
79// ============================================================================
80// Method : TopoDSToStep_Builder::TopoDSToStep_Builder
81// Purpose :
82// ============================================================================
83
84 TopoDSToStep_WireframeBuilder::TopoDSToStep_WireframeBuilder(const TopoDS_Shape& aShape, TopoDSToStep_Tool& aTool, const Handle(Transfer_FinderProcess)& FP)
85{
86 done = Standard_False;
87 Init(aShape, aTool, FP);
88}
89
90void TopoDSToStep_WireframeBuilder::Init(const TopoDS_Shape& aShape, TopoDSToStep_Tool& /* T */, const Handle(Transfer_FinderProcess)& /* FP */)
91{
92 Handle(TColStd_HSequenceOfTransient) itemList =
93 new TColStd_HSequenceOfTransient();
94 MoniTool_DataMapOfShapeTransient aPmsMap;
95 done = GetTrimmedCurveFromShape(aShape, aPmsMap, itemList);
96 myResult = itemList;
97}
98
99// ============================================================================
100// Method : TopoDSToStep_Builder::Error
101// Purpose :
102// ============================================================================
103
104TopoDSToStep_BuilderError TopoDSToStep_WireframeBuilder::Error() const
105{
106 return myError;
107}
108
109// ============================================================================
110// Method : TopoDSToStep_Builder::Value
111// Purpose :
112// ============================================================================
113
114const Handle(TColStd_HSequenceOfTransient)& TopoDSToStep_WireframeBuilder::Value() const
115{
116 StdFail_NotDone_Raise_if(!done,"");
117 return myResult;
118}
119
120
121// ============================================================================
122//:S4134: abv 10 Mar 99: the methods below moved from package TopoDSToGBWire
123
124#define Nbpt 23
125
126static Handle(StepGeom_TrimmedCurve) MakeTrimmedCurve (const Handle(StepGeom_Curve) &C,
127 const Handle(StepGeom_CartesianPoint) P1,
128 const Handle(StepGeom_CartesianPoint) P2,
129 Standard_Real trim1,
130 Standard_Real trim2,
131 Standard_Boolean sense)
132{
133 Handle(StepGeom_HArray1OfTrimmingSelect) aSTS1 =
134 new StepGeom_HArray1OfTrimmingSelect(1,2);
135 StepGeom_TrimmingSelect tSel;
136 tSel.SetValue(P1);
137 aSTS1->SetValue(1,tSel);
138 tSel.SetParameterValue(trim1);
139 aSTS1->SetValue(2,tSel);
140
141 Handle(StepGeom_HArray1OfTrimmingSelect) aSTS2 =
142 new StepGeom_HArray1OfTrimmingSelect(1,2);
143 tSel.SetValue(P2);
144 aSTS2->SetValue(1,tSel);
145 tSel.SetParameterValue(trim2);
146 aSTS2->SetValue(2,tSel);
147
148 Handle(TCollection_HAsciiString) empty =
149 new TCollection_HAsciiString("");
150 Handle(StepGeom_TrimmedCurve) pmsTC = new StepGeom_TrimmedCurve;
151 pmsTC->Init(empty,C,aSTS1,aSTS2,sense,StepGeom_tpParameter);
152 return pmsTC;
153}
154
155Standard_Boolean TopoDSToStep_WireframeBuilder::
156 GetTrimmedCurveFromEdge(const TopoDS_Edge& theEdge,
157 const TopoDS_Face& aFace,
158 MoniTool_DataMapOfShapeTransient& aMap,
159 Handle(TColStd_HSequenceOfTransient)& curveList) const
160{
161 if (theEdge.Orientation() == TopAbs_INTERNAL ||
162 theEdge.Orientation() == TopAbs_EXTERNAL ) {
0797d9d3 163#ifdef OCCT_DEBUG
7fd59977 164 cout <<"Warning: TopoDSToStep_WireframeBuilder::GetTrimmedCurveFromEdge: Edge is internal or external; dropped" << endl;
165#endif
166 return Standard_False;
167 }
168 //szv#4:S4163:12Mar99 SGI warns
169 TopoDS_Shape sh = theEdge.Oriented(TopAbs_FORWARD);
170 TopoDS_Edge anEdge = TopoDS::Edge ( sh );
171
172 // resulting curve
173 Handle(StepGeom_Curve) Gpms;
174
175 if ( aMap.IsBound(anEdge)) {
176 Gpms = Handle(StepGeom_Curve)::DownCast ( aMap.Find(anEdge) );
177 if ( Gpms.IsNull() ) return Standard_False;
178//?? curveList->Append(Gpms);
179 return Standard_True;
180 }
181
182 BRepAdaptor_Curve CA ( anEdge );
183
184 // Vertices
185 TopoDS_Vertex Vfirst, Vlast;
186 Handle(StepGeom_CartesianPoint) pmsP1, pmsP2;
187 for (TopoDS_Iterator It(anEdge);It.More();It.Next()) {
188 // Translates the Edge Vertices
189 TopoDS_Vertex V = TopoDS::Vertex(It.Value());
190 gp_Pnt gpP = BRep_Tool::Pnt(V);
191 if ( V.Orientation() == TopAbs_FORWARD ) {
192 Vfirst = V;
193 // 1.point for trimming
194 GeomToStep_MakeCartesianPoint gtpP(gpP);
195 pmsP1 = gtpP.Value();
196 }
197 if ( V.Orientation() == TopAbs_REVERSED ) {
198 Vlast = V;
199 // 2.point for trimming
200 GeomToStep_MakeCartesianPoint gtpP(gpP);
201 pmsP2 = gtpP.Value();
202 }
203 }
204
205 // ---------------------------------------
206 // Translate 3D representation of the Edge
207 // ---------------------------------------
208
209
210 // Handle(Geom_Curve) C = CA.Curve().Curve();
211
212 // UPDATE FMA 26-02-96
213 // General remark : this full code should be deaply reworked
214 // Too many objects are not used !
215
216 Standard_Real First, Last;
217 Handle(Geom_Curve) C = BRep_Tool::Curve(anEdge, First, Last);
218 if ( ! C.IsNull() ) {
219 if (C->IsKind(STANDARD_TYPE(Geom_TrimmedCurve)))
220 C = Handle(Geom_TrimmedCurve)::DownCast(C)->BasisCurve();
221 GeomToStep_MakeCurve gtpC(C);
9e20ed57 222
223 if(!gtpC.IsDone())
224 return Standard_False;
225
7fd59977 226 Handle(StepGeom_Curve) pmsC = gtpC.Value();
227
228 // trim the curve
229 Standard_Real trim1 = CA.FirstParameter();
230 Standard_Real trim2 = CA.LastParameter();
231/* //:j1 abv 22 Oct 98: radians are used in the produced STEP file (at least by default)
232 if(C->IsKind(STANDARD_TYPE(Geom_Circle)) ||
233 C->IsKind(STANDARD_TYPE(Geom_Ellipse))) {
c6541a0c 234 Standard_Real fact = 180. / M_PI;
7fd59977 235 trim1 = trim1 * fact;
236 trim2 = trim2 * fact;
237 }
238*/
239 Gpms = MakeTrimmedCurve (pmsC, pmsP1, pmsP2, trim1, trim2, Standard_True );
240// (anEdge.Orientation() == TopAbs_FORWARD));
241 }
242 else {
243
244 // -------------------------
245 // a 3D Curve is constructed
246 // -------------------------
247
248 Standard_Boolean iaplan = Standard_False;
249 if ( ! aFace.IsNull() ) {
250 Standard_Real cf, cl;
251 Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface(anEdge, aFace, cf, cl);
252 Handle(Geom_Surface) S = BRep_Tool::Surface(aFace);
253 if (S->IsKind(STANDARD_TYPE(Geom_Plane)) &&
254 C2d->IsKind(STANDARD_TYPE(Geom2d_Line))) iaplan = Standard_True;
255 }
256
257 // to be modified : cf and cl are the topological trimming prameter
258 // these are computed after ! (U1 and U2) -> cf and cl instead
259 if (iaplan) {
260 gp_Pnt Pnt1 = CA.Value(CA.FirstParameter()), Pnt2 = CA.Value(CA.LastParameter());
261 gp_Vec V ( Pnt1, Pnt2 );
262 Standard_Real length = V.Magnitude();
263 if ( length >= Precision::Confusion() ) {
264 Handle(Geom_Line) L = new Geom_Line(Pnt1, gp_Dir(V));
265 GeomToStep_MakeLine gtpL(L);
266 Gpms = gtpL.Value();
267 Gpms = MakeTrimmedCurve (gtpL.Value(), pmsP1, pmsP2, 0, length, Standard_True );
268// (anEdge.Orientation() == TopAbs_FORWARD));
269 }
0797d9d3 270#ifdef OCCT_DEBUG
7fd59977 271 else cout << "Warning: TopoDSToStep_WireframeBuilder::GetTrimmedCurveFromEdge: Null-length curve not mapped" << endl;
272#endif
273 }
274 else {
275 TColgp_Array1OfPnt Points(1,Nbpt);
276 TColStd_Array1OfReal Knots(1,Nbpt);
277 TColStd_Array1OfInteger Mult(1,Nbpt);
278 Standard_Real U1 = CA.FirstParameter();
279 Standard_Real U2 = CA.LastParameter();
280 for ( Standard_Integer i=1; i<=Nbpt; i++ ) {
281 Standard_Real U = U1 + (i-1)*(U2 - U1)/(Nbpt - 1);
282 gp_Pnt P = CA.Value(U);
283 Points.SetValue(i,P);
284 Knots.SetValue(i,U);
285 Mult.SetValue(i,1);
286 }
287 Points.SetValue(1, BRep_Tool::Pnt(Vfirst));
288 Points.SetValue(Nbpt, BRep_Tool::Pnt(Vlast));
289 Mult.SetValue(1,2);
290 Mult.SetValue(Nbpt,2);
291 Handle(Geom_BSplineCurve) Bs =
292 new Geom_BSplineCurve(Points, Knots, Mult, 1);
293 GeomToStep_MakeCurve gtpC(Bs);
294 Gpms = gtpC.Value();
295 }
296 }
297 if( Gpms.IsNull() ) return Standard_False;
298
299 aMap.Bind(anEdge, Gpms);
300 curveList->Append(Gpms);
301 return Standard_True;
302}
303
304
305Standard_Boolean TopoDSToStep_WireframeBuilder::
306 GetTrimmedCurveFromFace(const TopoDS_Face& aFace,
307 MoniTool_DataMapOfShapeTransient& aMap,
308 Handle(TColStd_HSequenceOfTransient)& aCurveList) const
309{
310 TopoDS_Shape curShape;
311 TopoDS_Edge curEdge;
312 TopExp_Explorer exp;
313 Standard_Boolean result = Standard_False; //szv#4:S4163:12Mar99 `done` hid one from this, initialisation needed
314
315 for (exp.Init(aFace,TopAbs_EDGE); exp.More(); exp.Next()){
316 curShape = exp.Current();
317 curEdge = TopoDS::Edge(curShape);
318 if (GetTrimmedCurveFromEdge(curEdge, aFace, aMap, aCurveList)) result = Standard_True;
319 }
320 return result;
321}
322
323Standard_Boolean TopoDSToStep_WireframeBuilder::
324 GetTrimmedCurveFromShape(const TopoDS_Shape& aShape,
325 MoniTool_DataMapOfShapeTransient& aMap,
326 Handle(TColStd_HSequenceOfTransient)& aCurveList) const
327{
328 TopoDS_Iterator It;
329 Standard_Boolean result = Standard_False; //szv#4:S4163:12Mar99 `done` hid one from this, initialisation needed
330
331 //szv#4:S4163:12Mar99 optimized
332 switch (aShape.ShapeType()) {
333 case TopAbs_EDGE : {
334 const TopoDS_Edge& curEdge = TopoDS::Edge(aShape);
335 TopoDS_Face nulFace;
336 result = GetTrimmedCurveFromEdge(curEdge, nulFace, aMap, aCurveList);
337 break;
338 }
339 case TopAbs_WIRE : {
340 TopoDS_Face nulFace;
341 TopoDS_Shape curShape;
342 TopoDS_Edge curEdge;
343 TopExp_Explorer exp;
344
345 for (exp.Init(aShape,TopAbs_EDGE); exp.More(); exp.Next()){
346 curShape = exp.Current();
347 curEdge = TopoDS::Edge(curShape);
348 if (GetTrimmedCurveFromEdge(curEdge, nulFace, aMap, aCurveList)) result = Standard_True;
349 }
350 break;
351 }
352 case TopAbs_FACE : {
353 const TopoDS_Face& curFace = TopoDS::Face(aShape);
354 result = GetTrimmedCurveFromFace(curFace, aMap, aCurveList);
355 break;
356 }
357 case TopAbs_SHELL : {
358 TopoDS_Shell Sh = TopoDS::Shell(aShape);
359 It.Initialize(Sh);
360 for (;It.More();It.Next()) {
361 TopoDS_Face curFace = TopoDS::Face(It.Value());
362 if (GetTrimmedCurveFromFace(curFace, aMap, aCurveList)) result = Standard_True;
0797d9d3 363#ifdef OCCT_DEBUG
7fd59977 364 if(!result) {
365 cout << "ERROR extracting trimmedCurve from Face" << endl;
366 //BRepTools::Dump(curFace,cout); cout<<endl;
367 }
368#endif
369 }
370 break;
371 }
372 case TopAbs_SOLID : {
373 It.Initialize(aShape);
374 for (;It.More();It.Next()) {
375 if (It.Value().ShapeType() == TopAbs_SHELL) {
376 if (GetTrimmedCurveFromShape(It.Value(), aMap, aCurveList)) result = Standard_True;
377 }
378 }
379 break;
380 }
381 case TopAbs_COMPOUND : {
382 It.Initialize(aShape);
383 for (;It.More();It.Next()) {
384/* if ((It.Value().ShapeType() == TopAbs_SHELL) ||
385 (It.Value().ShapeType() == TopAbs_COMPOUND)) {
386 result = GetTrimmedCurveFromShape(It.Value(), aMap, aCurveList);
387 break;
388 }
389 else if (It.Value().ShapeType() == TopAbs_FACE) {
390 result = GetTrimmedCurveFromFace(TopoDS::Face(It.Value()), aMap, aCurveList);
391 break;
392 } */
393 if (GetTrimmedCurveFromShape(It.Value(), aMap, aCurveList)) result = Standard_True;
394 }
395 break;
396 }
397 default : break;
398 }
399 return result;
400}