1 // Created on: 1995-01-03
2 // Created by: Frederic MAUPAS
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 //:o0 abv 16.02.99: POLYLINE allowed as 3d curve of edge
18 //gka,abv 05.04.99: S4136: improving tolerance management, eliminate BRepAPI::Precision()
20 #include <BRep_Builder.hxx>
21 #include <BRep_Tool.hxx>
22 #include <BRepLib.hxx>
23 #include <BRepLib_MakeEdge.hxx>
24 #include <GCPnts_AbscissaPoint.hxx>
25 #include <Geom2d_Curve.hxx>
26 #include <Geom_CartesianPoint.hxx>
27 #include <Geom_Curve.hxx>
28 #include <Geom_Line.hxx>
29 #include <Geom_Surface.hxx>
30 #include <GeomAbs_Shape.hxx>
31 #include <GeomAdaptor_Curve.hxx>
35 #include <Precision.hxx>
36 #include <ShapeAnalysis_Curve.hxx>
37 #include <ShapeConstruct_Curve.hxx>
38 #include <StdFail_NotDone.hxx>
39 #include <StepGeom_CartesianPoint.hxx>
40 #include <StepGeom_Curve.hxx>
41 #include <StepGeom_Pcurve.hxx>
42 #include <StepGeom_Polyline.hxx>
43 #include <StepGeom_SurfaceCurve.hxx>
44 #include <StepRepr_DefinitionalRepresentation.hxx>
45 #include <StepShape_Edge.hxx>
46 #include <StepShape_EdgeCurve.hxx>
47 #include <StepShape_OrientedEdge.hxx>
48 #include <StepShape_Vertex.hxx>
49 #include <StepShape_VertexPoint.hxx>
50 #include <StepToGeom.hxx>
51 #include <StepToTopoDS.hxx>
52 #include <StepToTopoDS_GeometricTool.hxx>
53 #include <StepToTopoDS_NMTool.hxx>
54 #include <StepToTopoDS_Tool.hxx>
55 #include <StepToTopoDS_TranslateEdge.hxx>
56 #include <StepToTopoDS_TranslateVertex.hxx>
57 #include <TCollection_HAsciiString.hxx>
59 #include <TopoDS_Edge.hxx>
60 #include <TopoDS_Shape.hxx>
61 #include <TopoDS_Vertex.hxx>
62 #include <Transfer_TransientProcess.hxx>
63 #include <UnitsMethods.hxx>
64 #include <Standard_Failure.hxx>
66 //#include <StepGeom_Polyline.hxx>
67 //#include <TransferBRep.hxx>
69 // Used in I-DEAS-like STP processing (ssv; 15.11.2010)
71 // ============================================================================
72 // Method : DecodeMakeEdgeError
74 // ============================================================================
75 static void DecodeMakeEdgeError(const BRepLib_MakeEdge& ME,
76 const Handle(Standard_Transient)& orig,
77 const Handle(Geom_Curve)& myCurve,
78 const TopoDS_Vertex& V1,
79 const TopoDS_Vertex& V2,
80 const Standard_Real& U1,
81 const Standard_Real& U2,
82 StepToTopoDS_Tool& aTool,
83 const Handle(StepShape_TopologicalRepresentationItem)& /*tobind*/)
85 (void)U1, (void)U2; // avoid compiler warning
87 Handle(Transfer_TransientProcess) TP = aTool.TransientProcess();
88 // if (!myCurve.IsNull() && !tobind.IsNull()) {
89 // TransferBRep::SetShapeResult
90 // (TP,tobind, MakeEdge(myCurve,V1,V2,U1,U2,BRepAPI::Precision()) );
91 // aTool.Bind (tobind,E); SURTOUT PAS : noter pour debug/erreur
94 cout << "------------------------------------" << endl;
95 cout << "MakeEdge Error : " << ME.Error()<<" - ";
99 case (BRepLib_EdgeDone): return;
100 case (BRepLib_PointProjectionFailed):
101 TP->AddFail(orig," Point Projection failed");
103 case (BRepLib_ParameterOutOfRange):
104 TP->AddFail(orig," Parameter Out Of Range");
106 case (BRepLib_DifferentPointsOnClosedCurve):
107 TP->AddFail(orig," Different Points on Closed Curve");
109 case (BRepLib_PointWithInfiniteParameter):
110 TP->AddFail(orig," Point with infinite Parameter");
112 case (BRepLib_DifferentsPointAndParameter):
113 if (!ShapeConstruct_Curve().AdjustCurve
114 (myCurve,BRep_Tool::Pnt(V1),BRep_Tool::Pnt(V2),Standard_True,Standard_True))
115 TP->AddFail(orig," Different Points and Parameters");
116 else TP->AddWarning(orig,"Different Points and Parameters, adjusted");
118 case (BRepLib_LineThroughIdenticPoints):
119 TP->AddFail(orig," Line through identic Points");
123 cout << "Original Type : " << orig->DynamicType() << endl;
124 cout << "3D Curve Type : " << myCurve->DynamicType() << endl;
125 cout << "First Parameter : " << U1 << endl;
126 gp_Pnt p1 = BRep_Tool::Pnt(V1);
127 // cout << "First Point : ";
128 cout << "First Vertex : "<<p1.X()<<" "<<p1.Y()<<" "<<p1.Z()<<" ";
129 cout << "Distance Point - Vertex : ";
130 Standard_Real d1 = p1.Distance(myCurve->Value(U1));
132 cout << "Last Parameter : " << U2 << endl;
133 gp_Pnt p2 = BRep_Tool::Pnt(V2);
134 // cout << "Last Point : ";
135 cout << "Last Vertex : "<<p2.X()<<" "<<p2.Y()<<" "<<p2.Z()<<" ";
136 cout << "Distance Point - Vertex : ";
137 Standard_Real d2 = BRep_Tool::Pnt(V2).Distance(myCurve->Value(U2));
142 // ============================================================================
143 // Method : StepToTopoDS_TranslateEdge::StepToTopoDS_TranslateEdge
144 // Purpose : Empty Constructor
145 // ============================================================================
147 static Handle(Geom_Curve) MakeCurve
148 (const Handle(StepGeom_Curve)& C1, const Handle(Transfer_TransientProcess) TP)
150 Handle(Geom_Curve) C2 = Handle(Geom_Curve)::DownCast (TP->FindTransient(C1));
151 if (!C2.IsNull()) return C2;
152 C2 = StepToGeom::MakeCurve (C1);
154 TP->BindTransient (C1,C2);
158 static TopoDS_Edge MakeEdge
159 (const Handle(Geom_Curve)& C3D,
160 const TopoDS_Vertex& V1, const TopoDS_Vertex& V2,
161 const Standard_Real U1, const Standard_Real U2) //, const Standard_Real preci)
163 // fait son edge quoi qu il arrive
166 B.MakeEdge (E,C3D,Precision::Confusion());//preci);
167 B.Add (E,V1); B.Add (E,V2);
168 B.UpdateVertex(V1, U1, E, 0.);//preci);
169 B.UpdateVertex(V2, U2, E, 0.);//preci);
173 StepToTopoDS_TranslateEdge::StepToTopoDS_TranslateEdge()
175 done = Standard_False;
178 // ============================================================================
179 // Method : StepToTopoDS_TranslateEdge::StepToTopoDS_TranslateEdge()
180 // Purpose : Constructor with an Edge and a Tool
181 // ============================================================================
183 StepToTopoDS_TranslateEdge::StepToTopoDS_TranslateEdge(const Handle(StepShape_Edge)& E,
184 StepToTopoDS_Tool& T,
185 StepToTopoDS_NMTool& NMTool)
190 // ============================================================================
192 // Purpose : Init with an Edge and a Tool.
193 // This method builds an Edge With 2 Vertices and 2 Parameters.
194 // The Edge is always build like FORWARD (BRepLib_MakeEdge)
195 // ============================================================================
197 void StepToTopoDS_TranslateEdge::Init(const Handle(StepShape_Edge)& aEdge,
198 StepToTopoDS_Tool& aTool,
199 StepToTopoDS_NMTool& NMTool)
201 Handle(Transfer_TransientProcess) TP = aTool.TransientProcess();
203 Handle(StepShape_OrientedEdge) OE =
204 Handle(StepShape_OrientedEdge)::DownCast(aEdge);
205 Handle(StepShape_Edge) wEdge = aEdge;
206 if ( ! OE.IsNull() ) wEdge = OE->EdgeElement();
207 Handle(StepShape_EdgeCurve) EC = Handle(StepShape_EdgeCurve)::DownCast(wEdge);
209 if (aTool.IsBound(EC)) {
210 myResult = aTool.Find(EC);
211 if (BRep_Tool::Degenerated(TopoDS::Edge(myResult))) {
212 TP->AddWarning(EC,"Degenerated Edge in several faces : transferred for each face");
215 myError = StepToTopoDS_TranslateEdgeDone;
216 done = Standard_True;
218 //:S4136 B.SameRange(TopoDS::Edge(myResult), Standard_False); //:a5 abv 11 Feb 98
219 //:S4136 B.SameParameter(TopoDS::Edge(myResult), Standard_False);//:a5
224 // [BEGIN] Proceed with non-manifold cases (ssv; 12.11.2010)
225 if ( NMTool.IsActive() && NMTool.IsBound(EC) ) {
226 TopoDS_Shape existingShape = NMTool.Find(EC);
227 // Reverse shape's orientation if needed
228 if ( !OE->Orientation() )
229 existingShape.Reverse();
230 myResult = existingShape;
231 myError = StepToTopoDS_TranslateEdgeDone;
232 done = Standard_True;
235 // [END] Proceed with non-manifold cases (ssv; 12.11.2010)
237 // [BEGIN] Proceed with I-DEAS-like STP (ssv; 15.11.2010)
238 const Handle(TCollection_HAsciiString) anECName = EC->Name();
239 if ( NMTool.IsIDEASCase() && !anECName.IsNull() && !anECName->IsEmpty() &&
240 NMTool.IsBound(anECName->String()) ) {
241 TopoDS_Shape existingShape = NMTool.Find(anECName->String());
242 // Reverse shape's orientation if needed
243 if ( !OE->Orientation() )
244 existingShape.Reverse();
245 // Register Edge for final processing (I-DEAS case)
246 NMTool.RegisterNMEdge(existingShape);
247 myResult = existingShape;
248 myError = StepToTopoDS_TranslateEdgeDone;
249 done = Standard_True;
252 // [END] Proceed with I-DEAS-like STP (ssv; 15.11.2010)
256 // Standard_Real preci = BRepAPI::Precision();
258 // Standard_Real precision = BRepAPI::Precision();
260 Handle(StepGeom_Curve) C = EC->EdgeGeometry();
263 TP->AddFail(EC," Geom Curve in EdgeCurve is equal to 0");
264 myError = StepToTopoDS_TranslateEdgeOther;
265 done = Standard_False;
269 Handle(StepShape_Vertex) Vstart, Vend;
271 // -----------------------------------------------------------
272 // Extract the start and end Vertices corresponding to FORWARD
273 // (following the geometrical sense)
274 // -----------------------------------------------------------
276 // Standard_Boolean OrientedEdgeOrientation = OE->Orientation();
277 Standard_Boolean EdgeCurveSameSense = EC->SameSense();
279 if (EdgeCurveSameSense) {
280 Vstart = EC->EdgeStart();
281 Vend = EC->EdgeEnd();
284 Vend = EC->EdgeStart();
285 Vstart = EC->EdgeEnd();
288 TopoDS_Vertex V1, V2;
290 StepToTopoDS_TranslateVertex myTranVertex1(Vstart, aTool, NMTool);
291 StepToTopoDS_TranslateVertex myTranVertex2(Vend, aTool, NMTool);
293 if (myTranVertex1.IsDone()) {
294 V1 = TopoDS::Vertex(myTranVertex1.Value());
295 V1.Orientation(TopAbs_FORWARD);
297 if (Vend == Vstart) {
299 V2.Orientation(TopAbs_REVERSED);
301 else if (myTranVertex2.IsDone()) {
302 V2 = TopoDS::Vertex(myTranVertex2.Value());
303 V2.Orientation(TopAbs_REVERSED);
305 done = Standard_True;
307 // ----------------------------------------------------------
308 // --- The EdgeCurve Geometry is of StepGeom_Curve Type
309 // --- It can be : * a Pcurve : no 3D curve is constructed
310 // --- * a Surface Curve, Intersection Curve
311 // --- or a Seam Curve
313 // ----------------------------------------------------------
315 if ( C->IsKind(STANDARD_TYPE(StepGeom_Pcurve))) {
317 //:S4136 B.UpdateEdge (E,preci);
318 B.Add(E, V1); // ?? en fin de TranslateEdgeLoop
321 else if (C->IsKind(STANDARD_TYPE(StepGeom_SurfaceCurve)) ) {
322 // qui reprend les types SeamCurve et IntersectionCurve
323 // --- The Edge Geometry is a Surface Curve ---
324 // --- (3d + 2 Pcurve Or Surface) ---
325 Handle(StepGeom_SurfaceCurve) Sc =
326 Handle(StepGeom_SurfaceCurve)::DownCast(C);
327 Handle(StepGeom_Curve) C1 = Sc->Curve3d();
328 // if (C1->IsKind(STANDARD_TYPE(StepGeom_Polyline))) {
330 // B.UpdateEdge (E,preci);
331 // B.Add(E, V1); // ?? en fin de TranslateEdgeLoop
335 MakeFromCurve3D (C1,EC,Vend,Precision(), E,V1,V2 , aTool);
338 // else if (C->IsKind(STANDARD_TYPE(StepGeom_Polyline))) {
340 // B.UpdateEdge (E,preci);
341 // B.Add(E, V1); // ?? en fin de TranslateEdgeLoop
345 // --- The Edge Geometry is a Single 3d Curve ---
346 MakeFromCurve3D (C,EC,Vend,Precision(), E,V1,V2 , aTool);
348 // On force les flags SameRange et SameParameter a Standard_False
350 //:S4136 B.SameRange(E, Standard_False);
351 //:S4136 B.SameParameter(E, Standard_False);
354 // Bind Edge in NM tool (ssv; 15.11.2010)
355 if ( NMTool.IsActive() ) {
357 if ( NMTool.IsIDEASCase() && !anECName.IsNull() && !anECName->IsEmpty() )
358 NMTool.Bind(anECName->String(), E);
362 myError = StepToTopoDS_TranslateEdgeDone;
367 // ============================================================================
368 // Method : MakeFromCurve3D
369 // Purpose : case of a Curve 3D (alone or in SurfaceCurve)
370 // ============================================================================
372 // auxiliary function
373 //:e6 abv 16 Apr 98: ProSTEP TR8, r0601_sy.stp, #14907
374 static void GetCartesianPoints ( const Handle(StepShape_EdgeCurve)& EC,
375 gp_Pnt &P1, gp_Pnt &P2)
377 for ( Standard_Integer i=1; i<=2; i++ ) {
378 const Handle(StepShape_Vertex) V = ((i == 1) == EC->SameSense() ? EC->EdgeStart() : EC->EdgeEnd() );
379 const Handle(StepShape_VertexPoint) VP = Handle(StepShape_VertexPoint)::DownCast(V);
380 if ( VP.IsNull() ) continue;
381 const Handle(StepGeom_CartesianPoint) P = Handle(StepGeom_CartesianPoint)::DownCast(VP->VertexGeometry());
382 Handle(Geom_CartesianPoint) CP = StepToGeom::MakeCartesianPoint (P);
383 ( i==1 ? P1 : P2 ) = CP->Pnt();
387 void StepToTopoDS_TranslateEdge::MakeFromCurve3D
388 (const Handle(StepGeom_Curve)& C3D, const Handle(StepShape_EdgeCurve)& EC,
389 const Handle(StepShape_Vertex)& Vend,
390 const Standard_Real preci, TopoDS_Edge& E,
391 TopoDS_Vertex& V1, TopoDS_Vertex& V2,
392 StepToTopoDS_Tool& aTool)
394 Handle(Transfer_TransientProcess) TP = aTool.TransientProcess();
395 Handle(Geom_Curve) C1 = MakeCurve(C3D,TP);
397 TP->AddFail(C3D," Make Geom_Curve (3D) failed");
398 myError = StepToTopoDS_TranslateEdgeOther;
399 done = Standard_False;
402 // -- Statistics -- -> No Warning message
403 aTool.AddContinuity (C1);
405 Standard_Real temp1,temp2, U1,U2;
407 gp_Pnt pv1 = BRep_Tool::Pnt(V1);
408 gp_Pnt pv2 = BRep_Tool::Pnt(V2);
411 gp_Pnt pnt1 = pv1, pnt2 = pv2;
412 if ( V1.IsSame ( V2 ) ) GetCartesianPoints ( EC, pnt1, pnt2 );
413 ShapeAnalysis_Curve sac;
414 temp1 = sac.Project (C1,pnt1,preci,pproj,U1,Standard_False);
415 temp2 = sac.Project (C1,pnt2,preci,pproj,U2,Standard_False);
417 if (!StepToTopoDS_GeometricTool::UpdateParam3d(C1, U1, U2, preci))
418 TP->AddWarning(C3D,"Update of 3D-Parameters has failed");
420 //:d5: instead of AdjustCurve above which is incorrect if U1 and U2 are not ends
421 GeomAdaptor_Curve aCA(C1);
422 gp_Pnt pU1 = aCA.Value ( U1 ), pU2 = aCA.Value ( U2 );
423 temp1 = pU1.Distance ( pv1 );
424 temp2 = pU2.Distance ( pv2 );
425 if ( temp1 > preci || temp2 > preci ) {
426 TP->AddWarning (C3D,"Poor result from projection vertex / curve 3d");
428 B.UpdateVertex ( V1, 1.000001*temp1 ); //:h6 abv 14 Jul 98: PRO8845 #2746: *=1.0001
429 B.UpdateVertex ( V2, 1.000001*temp2 ); //:h6
431 BRepLib_MakeEdge ME(C1, V1, V2, U1, U2);
434 B.Range ( E, U1, U2 ); // abv 14 Mar 00: trj3_pm1-ug.stp #91739, edge 2
437 if (ME.Error() == BRepLib_DifferentPointsOnClosedCurve) {
438 // The Edge could be closed and trimmed by 2 Differents
440 if (C1->IsClosed()) {
441 // Attention : il faudra mettre a jour la topologie des
442 // vertex pour avoir des edges cul a cul ...... Good Luck!
443 aTool.Bind (Vend,V1);
444 TopoDS_Shape aLocalShape = V1.Reversed();
445 V2 = TopoDS::Vertex(aLocalShape);
446 ME.Init(C1, V1, V2, U1, U2);
448 TP->AddWarning(EC, "Wrong topology corrected : Closed Edge with TWO different Vertices");
452 DecodeMakeEdgeError(ME, C3D, C1, V1, V2, U1, U2, aTool, EC);
453 E = MakeEdge (C1,V1,V2,U1,U2);//preci
454 myError = StepToTopoDS_TranslateEdgeDone; // ????
455 done = Standard_True;
460 // Then, this is should be coded as degenerated
461 // To be performed later !!!
462 // DecodeMakeEdgeError(ME, C3D, C1, V1, V2, U1, U2, aTool, EC);
463 myError = StepToTopoDS_TranslateEdgeDone; // ????
464 // Bon, on la fait cette petite edge, mais faudra repasser
465 // pour l enlever ET FUSIONNER LES VERTEX, pour tout le shell !
466 // courbe trop petite pour etre mise -> fait planter
467 done = Standard_True;
468 if (!V1.IsSame(V2)) {
469 TP->AddFail(EC, "This edge has null arc length");
470 gp_Pnt P1 = BRep_Tool::Pnt(V1);
471 gp_Pnt P2 = BRep_Tool::Pnt(V2);
472 gp_Vec avec (P1,P2); gp_Dir adir (avec); gp_Lin alin (P1,adir);
473 C1 = new Geom_Line (alin);
474 U1 = 0.; U2 = P1.Distance(P2);
475 E = MakeEdge (C1,V1,V2,U1,U2);//,preci
478 TP->AddFail(EC,"NULL EDGE, SKIPPED");
485 DecodeMakeEdgeError(ME, C3D, C1, V1, V2, U1, U2, aTool, EC);
486 E = MakeEdge (C1,V1,V2,U1,U2);//,preci
487 myError = StepToTopoDS_TranslateEdgeDone; // ????
488 done = Standard_True;
494 // ============================================================================
495 // Method : MakePCurve
496 // Purpose : Computes an individual pcurve (i.e. curve 2d)
497 // ============================================================================
498 Handle(Geom2d_Curve) StepToTopoDS_TranslateEdge::MakePCurve
499 (const Handle(StepGeom_Pcurve)& PCU, const Handle(Geom_Surface)& ConvSurf) const
501 Handle(Geom2d_Curve) C2d;
502 const Handle(StepRepr_DefinitionalRepresentation) DRI = PCU->ReferenceToCurve();
503 if( DRI.IsNull()) return C2d;
504 const Handle(StepGeom_Curve) StepCurve = Handle(StepGeom_Curve)::DownCast(DRI->ItemsValue(1));
507 C2d = StepToGeom::MakeCurve2d (StepCurve);
508 if (! C2d.IsNull()) {
509 // -- if the surface is a RectangularTrimmedSurface,
510 // -- send the BasisSurface.
511 C2d = UnitsMethods::DegreeToRadian(C2d, ConvSurf);
515 catch(Standard_Failure)
523 // ============================================================================
525 // Purpose : Returns the mapped edge
526 // ============================================================================
528 const TopoDS_Shape& StepToTopoDS_TranslateEdge::Value() const
530 StdFail_NotDone_Raise_if(!done,"");
534 // ============================================================================
536 // Purpose : Returns the error code
537 // ============================================================================
539 StepToTopoDS_TranslateEdgeError StepToTopoDS_TranslateEdge::Error() const