1 // Created on: 1995-01-04
2 // Created by: Bruno DUMORTIER
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.
18 #include <BRep_Builder.hxx>
19 #include <BRep_Tool.hxx>
20 #include <BRepLib.hxx>
21 #include <BRepLib_MakeEdge2d.hxx>
24 #include <Extrema_ExtPC2d.hxx>
25 #include <Geom2d_Circle.hxx>
26 #include <Geom2d_Curve.hxx>
27 #include <Geom2d_Ellipse.hxx>
28 #include <Geom2d_Hyperbola.hxx>
29 #include <Geom2d_Line.hxx>
30 #include <Geom2d_Parabola.hxx>
31 #include <Geom2d_TrimmedCurve.hxx>
32 #include <Geom2dAdaptor_Curve.hxx>
33 #include <Geom_Plane.hxx>
35 #include <gp_Circ2d.hxx>
36 #include <gp_Elips2d.hxx>
37 #include <gp_Hypr2d.hxx>
38 #include <gp_Lin2d.hxx>
39 #include <gp_Parab2d.hxx>
40 #include <gp_Pnt2d.hxx>
41 #include <Precision.hxx>
42 #include <StdFail_NotDone.hxx>
44 #include <TopoDS_Edge.hxx>
45 #include <TopoDS_Vertex.hxx>
47 //=======================================================================
49 //purpose : make a 3d point on the current plane
50 //=======================================================================
51 static gp_Pnt Point(const gp_Pnt2d& P)
53 return BRepLib::Plane()->Value(P.X(),P.Y());
56 //=======================================================================
58 //purpose : project a vertex on the current plane
59 //=======================================================================
61 static gp_Pnt2d Project(const TopoDS_Vertex& Ve)
63 gp_Pnt P = BRep_Tool::Pnt(Ve);
65 ElSLib::Parameters(BRepLib::Plane()->Pln(),P,U,V);
69 //=======================================================================
71 //purpose : project a vertex on a curve
72 //=======================================================================
74 static Standard_Boolean Project(const Handle(Geom2d_Curve)& C,
75 const TopoDS_Vertex& V,
78 gp_Pnt2d P = Project(V);
79 Geom2dAdaptor_Curve AC(C);
80 if (AC.GetType() == GeomAbs_Line) {
81 p = ElCLib::LineParameter(AC.Line().Position(),P);
83 else if (AC.GetType() == GeomAbs_Circle) {
84 p = ElCLib::CircleParameter(AC.Circle().Position(),P);
87 Extrema_ExtPC2d extrema(P,AC);
88 if (extrema.IsDone()) {
89 Standard_Integer i,n = extrema.NbExt();
91 Standard_Real d2 = RealLast();
92 for (i = 1; i <= n; i++) {
93 //OCC16852:if (extrema.IsMin(i)) {
94 const Standard_Real dd2 = extrema.SquareDistance(i);
97 p = extrema.Point(i).Parameter();
103 return Standard_False;
105 return Standard_True;
108 //=======================================================================
109 //function : BRepLib_MakeEdge2d
111 //=======================================================================
113 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const TopoDS_Vertex& V1,
114 const TopoDS_Vertex& V2)
116 gp_Pnt2d P1 = Project(V1);
117 gp_Pnt2d P2 = Project(V2);
118 Standard_Real l = P1.Distance(P2);
119 if (l <= gp::Resolution()) {
120 myError = BRepLib_LineThroughIdenticPoints;
123 gp_Lin2d L(P1,gp_Vec2d(P1,P2));
124 Handle(Geom2d_Line) GL = new Geom2d_Line(L);
128 //=======================================================================
129 //function : BRepLib_MakeEdge2d
131 //=======================================================================
133 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Pnt2d& P1,
136 Standard_Real l = P1.Distance(P2);
137 if (l <= gp::Resolution()) {
138 myError = BRepLib_LineThroughIdenticPoints;
141 gp_Lin2d L(P1,gp_Vec2d(P1,P2));
142 Handle(Geom2d_Line) GL = new Geom2d_Line(L);
147 //=======================================================================
148 //function : BRepLib_MakeEdge2d
150 //=======================================================================
152 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Lin2d& L)
154 Handle(Geom2d_Line) GL = new Geom2d_Line(L);
158 //=======================================================================
159 //function : BRepLib_MakeEdge2d
161 //=======================================================================
163 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Lin2d& L,
164 const Standard_Real p1,
165 const Standard_Real p2)
167 Handle(Geom2d_Line) GL = new Geom2d_Line(L);
171 //=======================================================================
172 //function : BRepLib_MakeEdge2d
174 //=======================================================================
176 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Lin2d& L,
180 Handle(Geom2d_Line) GL = new Geom2d_Line(L);
184 //=======================================================================
185 //function : BRepLib_MakeEdge2d
187 //=======================================================================
189 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Lin2d& L,
190 const TopoDS_Vertex& V1,
191 const TopoDS_Vertex& V2)
193 Handle(Geom2d_Line) GL = new Geom2d_Line(L);
197 //=======================================================================
198 //function : BRepLib_MakeEdge2d
200 //=======================================================================
202 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Circ2d& C)
204 Handle(Geom2d_Circle) GC = new Geom2d_Circle(C);
208 //=======================================================================
209 //function : BRepLib_MakeEdge2d
211 //=======================================================================
213 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Circ2d& C,
214 const Standard_Real p1,
215 const Standard_Real p2)
217 Handle(Geom2d_Circle) GC = new Geom2d_Circle(C);
221 //=======================================================================
222 //function : BRepLib_MakeEdge2d
224 //=======================================================================
226 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Circ2d& C,
230 Handle(Geom2d_Circle) GC = new Geom2d_Circle(C);
234 //=======================================================================
235 //function : BRepLib_MakeEdge2d
237 //=======================================================================
239 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Circ2d& C,
240 const TopoDS_Vertex& V1,
241 const TopoDS_Vertex& V2)
243 Handle(Geom2d_Circle) GC = new Geom2d_Circle(C);
247 //=======================================================================
248 //function : BRepLib_MakeEdge2d
250 //=======================================================================
252 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Elips2d& E)
254 Handle(Geom2d_Ellipse) GE = new Geom2d_Ellipse(E);
258 //=======================================================================
259 //function : BRepLib_MakeEdge2d
261 //=======================================================================
263 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Elips2d& E,
264 const Standard_Real p1,
265 const Standard_Real p2)
267 Handle(Geom2d_Ellipse) GE = new Geom2d_Ellipse(E);
271 //=======================================================================
272 //function : BRepLib_MakeEdge2d
274 //=======================================================================
276 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Elips2d& E,
280 Handle(Geom2d_Ellipse) GE = new Geom2d_Ellipse(E);
284 //=======================================================================
285 //function : BRepLib_MakeEdge2d
287 //=======================================================================
289 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Elips2d& E,
290 const TopoDS_Vertex& V1,
291 const TopoDS_Vertex& V2)
293 Handle(Geom2d_Ellipse) GE = new Geom2d_Ellipse(E);
297 //=======================================================================
298 //function : BRepLib_MakeEdge2d
300 //=======================================================================
302 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Hypr2d& H)
304 Handle(Geom2d_Hyperbola) GH = new Geom2d_Hyperbola(H);
308 //=======================================================================
309 //function : BRepLib_MakeEdge2d
311 //=======================================================================
313 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Hypr2d& H,
314 const Standard_Real p1,
315 const Standard_Real p2)
317 Handle(Geom2d_Hyperbola) GH = new Geom2d_Hyperbola(H);
321 //=======================================================================
322 //function : BRepLib_MakeEdge2d
324 //=======================================================================
326 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Hypr2d& H,
330 Handle(Geom2d_Hyperbola) GH = new Geom2d_Hyperbola(H);
334 //=======================================================================
335 //function : BRepLib_MakeEdge2d
337 //=======================================================================
339 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Hypr2d& H,
340 const TopoDS_Vertex& V1,
341 const TopoDS_Vertex& V2)
343 Handle(Geom2d_Hyperbola) GH = new Geom2d_Hyperbola(H);
347 //=======================================================================
348 //function : BRepLib_MakeEdge2d
350 //=======================================================================
352 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Parab2d& P)
354 Handle(Geom2d_Parabola) GP = new Geom2d_Parabola(P);
358 //=======================================================================
359 //function : BRepLib_MakeEdge2d
361 //=======================================================================
363 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Parab2d& P,
364 const Standard_Real p1,
365 const Standard_Real p2)
367 Handle(Geom2d_Parabola) GP = new Geom2d_Parabola(P);
371 //=======================================================================
372 //function : BRepLib_MakeEdge2d
374 //=======================================================================
376 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Parab2d& P,
380 Handle(Geom2d_Parabola) GP = new Geom2d_Parabola(P);
384 //=======================================================================
385 //function : BRepLib_MakeEdge2d
387 //=======================================================================
389 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Parab2d& P,
390 const TopoDS_Vertex& V1,
391 const TopoDS_Vertex& V2)
393 Handle(Geom2d_Parabola) GP = new Geom2d_Parabola(P);
397 //=======================================================================
398 //function : BRepLib_MakeEdge2d
400 //=======================================================================
402 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const Handle(Geom2d_Curve)& L)
407 //=======================================================================
408 //function : BRepLib_MakeEdge2d
410 //=======================================================================
412 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const Handle(Geom2d_Curve)& L,
413 const Standard_Real p1,
414 const Standard_Real p2)
419 //=======================================================================
420 //function : BRepLib_MakeEdge2d
422 //=======================================================================
424 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const Handle(Geom2d_Curve)& L,
431 //=======================================================================
432 //function : BRepLib_MakeEdge2d
434 //=======================================================================
436 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const Handle(Geom2d_Curve)& L,
437 const TopoDS_Vertex& V1,
438 const TopoDS_Vertex& V2)
443 //=======================================================================
444 //function : BRepLib_MakeEdge2d
446 //=======================================================================
448 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const Handle(Geom2d_Curve)& L,
451 const Standard_Real p1,
452 const Standard_Real p2)
457 //=======================================================================
458 //function : BRepLib_MakeEdge2d
460 //=======================================================================
462 BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const Handle(Geom2d_Curve)& L,
463 const TopoDS_Vertex& V1,
464 const TopoDS_Vertex& V2,
465 const Standard_Real p1,
466 const Standard_Real p2)
471 //=======================================================================
474 //=======================================================================
476 void BRepLib_MakeEdge2d::Init(const Handle(Geom2d_Curve)& C)
478 Init(C,C->FirstParameter(),C->LastParameter());
481 //=======================================================================
484 //=======================================================================
486 void BRepLib_MakeEdge2d::Init(const Handle(Geom2d_Curve)& C,
487 const Standard_Real p1,
488 const Standard_Real p2)
496 //=======================================================================
499 //=======================================================================
501 void BRepLib_MakeEdge2d::Init(const Handle(Geom2d_Curve)& C,
507 B.MakeVertex(V1,Point(P1),Precision::Confusion());
508 if (P1.Distance(P2) < Precision::Confusion())
511 B.MakeVertex(V2,Point(P2),Precision::Confusion());
515 //=======================================================================
518 //=======================================================================
520 void BRepLib_MakeEdge2d::Init(const Handle(Geom2d_Curve)& C,
521 const TopoDS_Vertex& V1,
522 const TopoDS_Vertex& V2)
524 // try projecting the vertices on the curve
529 p1 = C->FirstParameter();
531 if (!Project(C,V1,p1)) {
532 myError = BRepLib_PointProjectionFailed;
536 p2 = C->LastParameter();
538 if (!Project(C,V2,p2)) {
539 myError = BRepLib_PointProjectionFailed;
547 //=======================================================================
550 //=======================================================================
552 void BRepLib_MakeEdge2d::Init(const Handle(Geom2d_Curve)& C,
555 const Standard_Real p1,
556 const Standard_Real p2)
561 B.MakeVertex(V1,Point(P1),Precision::Confusion());
562 if (P1.Distance(P2) < Precision::Confusion())
565 B.MakeVertex(V2,Point(P2),Precision::Confusion());
572 //=======================================================================
574 //purpose : this one really makes the job ...
575 //=======================================================================
577 void BRepLib_MakeEdge2d::Init(const Handle(Geom2d_Curve)& CC,
578 const TopoDS_Vertex& VV1,
579 const TopoDS_Vertex& VV2,
580 const Standard_Real pp1,
581 const Standard_Real pp2)
583 // kill trimmed curves
584 Handle(Geom2d_Curve) C = CC;
585 Handle(Geom2d_TrimmedCurve) CT = Handle(Geom2d_TrimmedCurve)::DownCast(C);
586 while (!CT.IsNull()) {
587 C = CT->BasisCurve();
588 CT = Handle(Geom2d_TrimmedCurve)::DownCast(C);
592 Standard_Real p1 = pp1;
593 Standard_Real p2 = pp2;
594 Standard_Real cf = C->FirstParameter();
595 Standard_Real cl = C->LastParameter();
596 Standard_Real epsilon = Precision::Confusion();
597 Standard_Boolean periodic = C->IsPeriodic();
603 ElCLib::AdjustPeriodic(cf,cl,epsilon,p1,p2);
616 Standard_Real x = p1;
622 if ((cf - p1 > epsilon) || (p2 - cl > epsilon)) {
623 myError = BRepLib_ParameterOutOfRange;
628 // compute points on the curve
629 Standard_Boolean p1inf = Precision::IsNegativeInfinite(p1);
630 Standard_Boolean p2inf = Precision::IsPositiveInfinite(p2);
632 if (!p1inf) P1 = C->Value(p1);
633 if (!p2inf) P2 = C->Value(p2);
635 Standard_Real preci = Precision::Confusion();
638 // check for closed curve
639 Standard_Boolean closed = Standard_False;
640 if (!p1inf && !p2inf)
641 closed = (P1.Distance(P2) <= preci);
643 // check if the vertices are on the curve
645 if (V1.IsNull() && V2.IsNull()) {
646 B.MakeVertex(V1,Point(P1),preci);
649 else if (V1.IsNull())
651 else if (V2.IsNull())
654 if (!V1.IsSame(V2)) {
655 myError = BRepLib_DifferentPointsOnClosedCurve;
658 else if (Point(P1).Distance(BRep_Tool::Pnt(V1)) > preci) {
659 myError = BRepLib_DifferentPointsOnClosedCurve;
669 myError = BRepLib_PointWithInfiniteParameter;
674 gp_Pnt P = Point(P1);
676 B.MakeVertex(V1,P,preci);
679 // desctivate control (RLE) for speed in sketcher
680 else if (P.Distance(BRep_Tool::Pnt(V1)) > preci) {
681 myError = BRepLib_DifferentsPointAndParameter;
689 myError = BRepLib_PointWithInfiniteParameter;
694 gp_Pnt P = Point(P2);
696 B.MakeVertex(V2,P,preci);
699 // desctivate control (RLE) for speed in sketcher
700 else if (P.Distance(BRep_Tool::Pnt(V2)) > preci){
701 myError = BRepLib_DifferentsPointAndParameter;
708 V1.Orientation(TopAbs_FORWARD);
709 V2.Orientation(TopAbs_REVERSED);
713 TopoDS_Edge& E = TopoDS::Edge(myShape);
715 B.UpdateEdge(E,C,BRepLib::Plane(),TopLoc_Location(),preci);
726 //=======================================================================
729 //=======================================================================
731 BRepLib_EdgeError BRepLib_MakeEdge2d::Error() const
736 //=======================================================================
739 //=======================================================================
741 const TopoDS_Edge& BRepLib_MakeEdge2d::Edge()
743 return TopoDS::Edge(Shape());
747 //=======================================================================
750 //=======================================================================
752 const TopoDS_Vertex& BRepLib_MakeEdge2d::Vertex1()const
759 //=======================================================================
762 //=======================================================================
764 const TopoDS_Vertex& BRepLib_MakeEdge2d::Vertex2()const
772 //=======================================================================
773 //function : operator
775 //=======================================================================
777 BRepLib_MakeEdge2d::operator TopoDS_Edge()