1 // Copyright (C) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #include <GEOMImpl_ShapeDriver.hxx>
24 #include <GEOMImpl_IShapes.hxx>
25 #include <GEOMImpl_IVector.hxx>
26 #include <GEOMImpl_Types.hxx>
27 #include <GEOMImpl_Block6Explorer.hxx>
29 #include <GEOM_Function.hxx>
32 #include <ShapeFix_Wire.hxx>
33 #include <ShapeFix_Edge.hxx>
34 #include <ShapeFix_Shape.hxx>
36 #include <BRep_Builder.hxx>
37 #include <BRep_Tool.hxx>
38 #include <BRepAdaptor_Curve.hxx>
39 #include <BRepAlgo_FaceRestrictor.hxx>
40 #include <BRepBuilderAPI_Copy.hxx>
41 #include <BRepBuilderAPI_Sewing.hxx>
42 #include <BRepBuilderAPI_MakeWire.hxx>
43 #include <BRepBuilderAPI_MakeEdge.hxx>
44 #include <BRepCheck.hxx>
45 #include <BRepCheck_Analyzer.hxx>
46 #include <BRepCheck_Shell.hxx>
47 #include <BRepClass3d_SolidClassifier.hxx>
48 #include <BRepLib.hxx>
49 #include <BRepLib_MakeEdge.hxx>
50 #include <BRepTools_WireExplorer.hxx>
52 #include <ShapeAnalysis_FreeBounds.hxx>
56 #include <TopExp_Explorer.hxx>
58 #include <TopoDS_Shape.hxx>
59 #include <TopoDS_Edge.hxx>
60 #include <TopoDS_Wire.hxx>
61 #include <TopoDS_Shell.hxx>
62 #include <TopoDS_Solid.hxx>
63 #include <TopoDS_Compound.hxx>
64 #include <TopoDS_Iterator.hxx>
66 #include <TopTools_MapOfShape.hxx>
67 #include <TopTools_HSequenceOfShape.hxx>
71 #include <GCPnts_AbscissaPoint.hxx>
73 #include <Geom_TrimmedCurve.hxx>
74 #include <GeomAbs_CurveType.hxx>
75 #include <GeomConvert_CompCurveToBSplineCurve.hxx>
76 #include <GeomConvert.hxx>
77 #include <GeomLProp.hxx>
79 #include <TColStd_SequenceOfReal.hxx>
80 #include <TColStd_HSequenceOfTransient.hxx>
81 #include <TColStd_Array1OfReal.hxx>
82 #include <TColGeom_SequenceOfCurve.hxx>
83 #include <TColGeom_Array1OfBSplineCurve.hxx>
84 #include <TColGeom_HArray1OfBSplineCurve.hxx>
86 #include <Precision.hxx>
88 #include <Standard_NullObject.hxx>
89 #include <Standard_TypeMismatch.hxx>
90 #include <Standard_ConstructionError.hxx>
92 //=======================================================================
95 //=======================================================================
96 const Standard_GUID& GEOMImpl_ShapeDriver::GetID()
98 static Standard_GUID aShapeDriver("FF1BBB54-5D14-4df2-980B-3A668264EA16");
103 //=======================================================================
104 //function : GEOMImpl_ShapeDriver
106 //=======================================================================
107 GEOMImpl_ShapeDriver::GEOMImpl_ShapeDriver()
111 //=======================================================================
114 //=======================================================================
115 Standard_Integer GEOMImpl_ShapeDriver::Execute(TFunction_Logbook& log) const
117 if (Label().IsNull()) return 0;
118 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
120 GEOMImpl_IShapes aCI (aFunction);
121 Standard_Integer aType = aFunction->GetType();
126 if (aType == WIRE_EDGES) {
127 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
132 for (unsigned int ind = 1; ind <= aShapes->Length(); ind++) {
133 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
134 TopoDS_Shape aShape_i = aRefShape->GetValue();
135 if (aShape_i.IsNull()) {
136 Standard_NullObject::Raise("Shape for wire construction is null");
138 if (aShape_i.ShapeType() == TopAbs_EDGE || aShape_i.ShapeType() == TopAbs_WIRE) {
139 TopExp_Explorer exp (aShape_i, TopAbs_EDGE);
140 for (; exp.More(); exp.Next())
141 B.Add(aWire, TopoDS::Edge(exp.Current()));
143 Standard_TypeMismatch::Raise
144 ("Shape for wire construction is neither an edge nor a wire");
149 Handle(ShapeFix_Wire) aFW = new ShapeFix_Wire;
153 if (aFW->StatusReorder(ShapeExtend_FAIL1)) {
154 Standard_ConstructionError::Raise("Wire construction failed: several loops detected");
155 } else if (aFW->StatusReorder(ShapeExtend_FAIL)) {
156 Standard_ConstructionError::Raise("Wire construction failed");
160 // IMP 0019766: Building a Wire from unconnected edges by introducing a tolerance
161 Standard_Real aTolerance = aCI.GetTolerance();
162 if (aTolerance < Precision::Confusion())
163 aTolerance = Precision::Confusion();
165 aFW->ClosedWireMode() = Standard_False;
166 aFW->FixConnected(aTolerance);
167 if (aFW->StatusConnected(ShapeExtend_FAIL)) {
168 Standard_ConstructionError::Raise("Wire construction failed: cannot build connected wire");
171 if (aFW->StatusConnected(ShapeExtend_DONE3)) {
172 // Confused with <prec> but not Analyzer.Precision(), set the same
173 aFW->FixGapsByRangesMode() = Standard_True;
174 if (aFW->FixGaps3d()) {
175 Handle(ShapeExtend_WireData) sbwd = aFW->WireData();
176 Handle(ShapeFix_Edge) aFe = new ShapeFix_Edge;
177 for (Standard_Integer iedge = 1; iedge <= sbwd->NbEdges(); iedge++) {
178 TopoDS_Edge aEdge = TopoDS::Edge(sbwd->Edge(iedge));
179 aFe->FixVertexTolerance(aEdge);
180 aFe->FixSameParameter(aEdge);
183 else if (aFW->StatusGaps3d(ShapeExtend_FAIL)) {
184 Standard_ConstructionError::Raise("Wire construction failed: cannot fix 3d gaps");
187 aShape = aFW->WireAPIMake();
189 else if (aType == FACE_WIRE) {
190 Handle(GEOM_Function) aRefBase = aCI.GetBase();
191 TopoDS_Shape aShapeBase = aRefBase->GetValue();
192 if (aShapeBase.IsNull()) Standard_NullObject::Raise("Argument Shape is null");
194 if (aShapeBase.ShapeType() == TopAbs_WIRE) {
195 W = TopoDS::Wire(aShapeBase);
196 // check the wire is closed
197 TopoDS_Vertex aV1, aV2;
198 TopExp::Vertices(W, aV1, aV2);
199 if ( !aV1.IsNull() && !aV2.IsNull() && aV1.IsSame(aV2) )
200 aShapeBase.Closed(true);
202 Standard_NullObject::Raise
203 ("Shape for face construction is not closed");
205 else if (aShapeBase.ShapeType() == TopAbs_EDGE && aShapeBase.Closed()) {
206 BRepBuilderAPI_MakeWire MW;
207 MW.Add(TopoDS::Edge(aShapeBase));
209 Standard_ConstructionError::Raise("Wire construction failed");
214 Standard_NullObject::Raise
215 ("Shape for face construction is neither a wire nor a closed edge");
217 GEOMImpl_Block6Explorer::MakeFace(W, aCI.GetIsPlanar(), aShape);
218 if (aShape.IsNull()) {
219 Standard_ConstructionError::Raise("Face construction failed");
222 else if (aType == FACE_WIRES) {
223 // Try to build a face from a set of wires and edges
226 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
227 int nbshapes = aShapes->Length();
229 Standard_ConstructionError::Raise("No wires or edges given");
232 // 1. Extract all edges from the given arguments
233 TopTools_MapOfShape aMapEdges;
234 Handle(TopTools_HSequenceOfShape) aSeqEdgesIn = new TopTools_HSequenceOfShape;
236 for (ind = 1; ind <= nbshapes; ind++) {
237 Handle(GEOM_Function) aRefSh_i = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
238 TopoDS_Shape aSh_i = aRefSh_i->GetValue();
240 TopExp_Explorer anExpE_i (aSh_i, TopAbs_EDGE);
241 for (; anExpE_i.More(); anExpE_i.Next()) {
242 if (aMapEdges.Add(anExpE_i.Current())) {
243 aSeqEdgesIn->Append(anExpE_i.Current());
248 // 2. Connect edges to wires of maximum length
249 Handle(TopTools_HSequenceOfShape) aSeqWiresOut;
250 ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdgesIn, Precision::Confusion(),
251 /*shared*/Standard_False, aSeqWiresOut);
253 // 3. Separate closed wires
254 Handle(TopTools_HSequenceOfShape) aSeqClosedWires = new TopTools_HSequenceOfShape;
255 Handle(TopTools_HSequenceOfShape) aSeqOpenWires = new TopTools_HSequenceOfShape;
256 for (ind = 1; ind <= aSeqWiresOut->Length(); ind++) {
257 if (aSeqWiresOut->Value(ind).Closed())
258 aSeqClosedWires->Append(aSeqWiresOut->Value(ind));
260 aSeqOpenWires->Append(aSeqWiresOut->Value(ind));
263 if (aSeqClosedWires->Length() < 1) {
264 Standard_ConstructionError::Raise
265 ("There is no closed contour can be built from the given arguments");
268 // 4. Build a face / list of faces from all the obtained closed wires
272 TopoDS_Wire aW1 = TopoDS::Wire(aSeqClosedWires->Value(1));
273 GEOMImpl_Block6Explorer::MakeFace(aW1, aCI.GetIsPlanar(), aFFace);
274 if (aFFace.IsNull()) {
275 Standard_ConstructionError::Raise("Face construction failed");
278 // 4.b. Add other wires
279 if (aSeqClosedWires->Length() == 1) {
284 BRep_Builder aBuilder;
285 aBuilder.MakeCompound(C);
286 BRepAlgo_FaceRestrictor FR;
288 TopAbs_Orientation OriF = aFFace.Orientation();
289 TopoDS_Shape aLocalS = aFFace.Oriented(TopAbs_FORWARD);
290 FR.Init(TopoDS::Face(aLocalS), Standard_False, Standard_True);
292 for (ind = 1; ind <= aSeqClosedWires->Length(); ind++) {
293 TopoDS_Wire aW = TopoDS::Wire(aSeqClosedWires->Value(ind));
302 for (; FR.More(); FR.Next()) {
303 aFace = FR.Current().Oriented(OriF);
304 aBuilder.Add(C, aFace);
315 // 5. Add all open wires to the result
316 if (aSeqOpenWires->Length() > 0) {
317 //Standard_ConstructionError::Raise("There are some open wires");
319 BRep_Builder aBuilder;
320 if (aSeqClosedWires->Length() == 1) {
321 aBuilder.MakeCompound(C);
322 aBuilder.Add(C, aShape);
325 C = TopoDS::Compound(aShape);
328 for (ind = 1; ind <= aSeqOpenWires->Length(); ind++) {
329 aBuilder.Add(C, aSeqOpenWires->Value(ind));
335 else if (aType == SHELL_FACES) {
336 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
337 unsigned int ind, nbshapes = aShapes->Length();
340 BRepBuilderAPI_Sewing aSewing(Precision::Confusion()*10.0);
341 for (ind = 1; ind <= nbshapes; ind++) {
342 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
343 TopoDS_Shape aShape_i = aRefShape->GetValue();
344 if (aShape_i.IsNull()) {
345 Standard_NullObject::Raise("Face for shell construction is null");
347 aSewing.Add(aShape_i);
352 TopoDS_Shape sh = aSewing.SewedShape();
353 if( sh.ShapeType()==TopAbs_FACE && nbshapes==1 ) {
354 // case for creation of shell from one face - PAL12722 (skl 26.06.2006)
361 //TopExp_Explorer exp (aSewing.SewedShape(), TopAbs_SHELL);
362 TopExp_Explorer exp (sh, TopAbs_SHELL);
363 Standard_Integer ish = 0;
364 for (; exp.More(); exp.Next()) {
365 aShape = exp.Current();
370 aShape = aSewing.SewedShape();
374 else if (aType == SOLID_SHELL) {
375 Handle(GEOM_Function) aRefShell = aCI.GetBase();
376 TopoDS_Shape aShapeShell = aRefShell->GetValue();
377 if (!aShapeShell.IsNull() && aShapeShell.ShapeType() == TopAbs_COMPOUND) {
378 TopoDS_Iterator It (aShapeShell, Standard_True, Standard_True);
379 if (It.More()) aShapeShell = It.Value();
381 if (aShapeShell.IsNull() || aShapeShell.ShapeType() != TopAbs_SHELL) {
382 Standard_NullObject::Raise("Shape for solid construction is null or not a shell");
385 BRepCheck_Shell chkShell(TopoDS::Shell(aShapeShell));
386 if (chkShell.Closed() == BRepCheck_NotClosed) return 0;
390 B.Add(Sol, aShapeShell);
391 BRepClass3d_SolidClassifier SC (Sol);
392 SC.PerformInfinitePoint(Precision::Confusion());
393 if (SC.State() == TopAbs_IN) {
395 B.Add(Sol, aShapeShell.Reversed());
401 else if (aType == SOLID_SHELLS) {
402 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
403 unsigned int ind, nbshapes = aShapes->Length();
404 Standard_Integer ish = 0;
409 for (ind = 1; ind <= nbshapes; ind++) {
410 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
411 TopoDS_Shape aShapeShell = aRefShape->GetValue();
412 if (aShapeShell.IsNull()) {
413 Standard_NullObject::Raise("Shell for solid construction is null");
415 if (aShapeShell.ShapeType() == TopAbs_COMPOUND) {
416 TopoDS_Iterator It (aShapeShell, Standard_True, Standard_True);
417 if (It.More()) aShapeShell = It.Value();
419 if (aShapeShell.ShapeType() == TopAbs_SHELL) {
420 B.Add(Sol, aShapeShell);
424 if (ish == 0) return 0;
425 BRepClass3d_SolidClassifier SC (Sol);
426 SC.PerformInfinitePoint(Precision::Confusion());
427 if (SC.State() == TopAbs_IN)
428 aShape = Sol.Reversed();
432 else if (aType == COMPOUND_SHAPES) {
433 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
434 unsigned int ind, nbshapes = aShapes->Length();
439 for (ind = 1; ind <= nbshapes; ind++) {
440 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
441 TopoDS_Shape aShape_i = aRefShape->GetValue();
442 if (aShape_i.IsNull()) {
443 Standard_NullObject::Raise("Shape for compound construction is null");
451 else if (aType == REVERSE_ORIENTATION) {
452 Handle(GEOM_Function) aRefShape = aCI.GetBase();
453 TopoDS_Shape aShape_i = aRefShape->GetValue();
454 if (aShape_i.IsNull()) {
455 Standard_NullObject::Raise("Shape for reverse is null");
458 BRepBuilderAPI_Copy Copy(aShape_i);
459 if( Copy.IsDone() ) {
460 TopoDS_Shape tds = Copy.Shape();
462 Standard_ConstructionError::Raise("Orientation aborted : Can not reverse the shape");
465 if( tds.Orientation() == TopAbs_FORWARD)
466 tds.Orientation(TopAbs_REVERSED);
468 tds.Orientation(TopAbs_FORWARD);
473 else if (aType == EDGE_WIRE) {
474 Handle(GEOM_Function) aRefBase = aCI.GetBase();
475 TopoDS_Shape aWire = aRefBase->GetValue();
476 Standard_Real LinTol = aCI.GetTolerance();
477 Standard_Real AngTol = aCI.GetAngularTolerance();
478 if (aWire.IsNull()) Standard_NullObject::Raise("Argument Wire is null");
482 BRepLib::BuildCurves3d(aWire);
483 Handle(ShapeFix_Shape) Fixer = new ShapeFix_Shape(aWire);
484 Fixer->SetPrecision(LinTol);
485 Fixer->SetMaxTolerance(LinTol);
487 TopoDS_Wire theWire = TopoDS::Wire(Fixer->Shape());
489 TColGeom_SequenceOfCurve CurveSeq;
490 TopTools_SequenceOfShape LocSeq;
491 TColStd_SequenceOfReal FparSeq;
492 TColStd_SequenceOfReal LparSeq;
493 TColStd_SequenceOfReal TolSeq;
494 GeomAbs_CurveType CurType;
495 TopoDS_Vertex FirstVertex, LastVertex;
497 BRepTools_WireExplorer wexp(theWire) ;
498 for (; wexp.More(); wexp.Next())
500 TopoDS_Edge anEdge = wexp.Current();
501 Standard_Real fpar, lpar;
502 TopLoc_Location aLoc;
503 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aLoc, fpar, lpar);
507 BRepAdaptor_Curve BAcurve(anEdge);
508 GeomAbs_CurveType aType = BAcurve.GetType();
510 Handle(Geom_Curve) aBasisCurve = BAcurve.Curve().Curve();
512 if (aBasisCurve->IsPeriodic())
513 ElCLib::AdjustPeriodic(aBasisCurve->FirstParameter(), aBasisCurve->LastParameter(),
514 Precision::PConfusion(), fpar, lpar);
516 if (CurveSeq.IsEmpty())
518 CurveSeq.Append(aCurve);
519 TopoDS_Shape aLocShape;
520 aLocShape.Location(aLoc);
521 aLocShape.Orientation(wexp.Orientation());
522 LocSeq.Append(aLocShape);
523 FparSeq.Append(fpar);
524 LparSeq.Append(lpar);
526 FirstVertex = wexp.CurrentVertex();
530 Standard_Boolean Done = Standard_False;
531 Standard_Real NewFpar, NewLpar;
532 GeomAdaptor_Curve GAprevcurve(CurveSeq.Last());
533 TopoDS_Vertex CurVertex = wexp.CurrentVertex();
534 TopoDS_Vertex CurFirstVer = TopExp::FirstVertex(anEdge);
535 TopAbs_Orientation ConnectByOrigin = (CurVertex.IsSame(CurFirstVer))? TopAbs_FORWARD : TopAbs_REVERSED;
536 if (aCurve == CurveSeq.Last())
540 if (aBasisCurve->IsPeriodic())
542 if (NewLpar < NewFpar)
543 NewLpar += aBasisCurve->Period();
544 if (ConnectByOrigin == TopAbs_FORWARD)
545 ElCLib::AdjustPeriodic(FparSeq.Last(),
546 FparSeq.Last() + aBasisCurve->Period(),
547 Precision::PConfusion(), NewFpar, NewLpar);
549 ElCLib::AdjustPeriodic(FparSeq.Last() - aBasisCurve->Period(),
551 Precision::PConfusion(), NewFpar, NewLpar);
553 Done = Standard_True;
555 else if (aType == CurType &&
556 aType != GeomAbs_BezierCurve &&
557 aType != GeomAbs_BSplineCurve &&
558 aType != GeomAbs_OtherCurve)
564 gp_Lin aLine = BAcurve.Line();
565 gp_Lin PrevLine = GAprevcurve.Line();
566 if (aLine.Contains(PrevLine.Location(), LinTol) &&
567 aLine.Direction().IsParallel(PrevLine.Direction(), AngTol))
569 gp_Pnt P1 = ElCLib::Value(fpar, aLine);
570 gp_Pnt P2 = ElCLib::Value(lpar, aLine);
571 NewFpar = ElCLib::Parameter(PrevLine, P1);
572 NewLpar = ElCLib::Parameter(PrevLine, P2);
573 if (NewLpar < NewFpar)
575 Standard_Real MemNewFpar = NewFpar;
577 NewLpar = MemNewFpar;
578 ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
580 Done = Standard_True;
586 gp_Circ aCircle = BAcurve.Circle();
587 gp_Circ PrevCircle = GAprevcurve.Circle();
588 if (aCircle.Location().Distance(PrevCircle.Location()) <= LinTol &&
589 Abs(aCircle.Radius() - PrevCircle.Radius()) <= LinTol &&
590 aCircle.Axis().IsParallel(PrevCircle.Axis(), AngTol))
592 if (aCircle.Axis().Direction() * PrevCircle.Axis().Direction() < 0.)
594 Standard_Real memfpar = fpar;
597 ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
599 gp_Pnt P1 = ElCLib::Value(fpar, aCircle);
600 gp_Pnt P2 = ElCLib::Value(lpar, aCircle);
601 NewFpar = ElCLib::Parameter(PrevCircle, P1);
602 NewLpar = ElCLib::Parameter(PrevCircle, P2);
603 if (NewLpar < NewFpar)
605 //Standard_Real MemNewFpar = NewFpar, MemNewLpar = NewLpar;
606 if (ConnectByOrigin == TopAbs_FORWARD)
607 ElCLib::AdjustPeriodic(FparSeq.Last(),
608 FparSeq.Last() + 2.*PI,
609 Precision::PConfusion(), NewFpar, NewLpar);
611 ElCLib::AdjustPeriodic(FparSeq.Last() - 2.*PI,
613 Precision::PConfusion(), NewFpar, NewLpar);
614 Done = Standard_True;
618 case GeomAbs_Ellipse:
620 gp_Elips anEllipse = BAcurve.Ellipse();
621 gp_Elips PrevEllipse = GAprevcurve.Ellipse();
622 if (anEllipse.Focus1().Distance(PrevEllipse.Focus1()) <= LinTol &&
623 anEllipse.Focus2().Distance(PrevEllipse.Focus2()) <= LinTol &&
624 Abs(anEllipse.MajorRadius() - PrevEllipse.MajorRadius()) <= LinTol &&
625 Abs(anEllipse.MinorRadius() - PrevEllipse.MinorRadius()) <= LinTol &&
626 anEllipse.Axis().IsParallel(PrevEllipse.Axis(), AngTol))
628 if (anEllipse.Axis().Direction() * PrevEllipse.Axis().Direction() < 0.)
630 Standard_Real memfpar = fpar;
633 ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
635 gp_Pnt P1 = ElCLib::Value(fpar, anEllipse);
636 gp_Pnt P2 = ElCLib::Value(lpar, anEllipse);
637 NewFpar = ElCLib::Parameter(PrevEllipse, P1);
638 NewLpar = ElCLib::Parameter(PrevEllipse, P2);
639 if (NewLpar < NewFpar)
641 if (ConnectByOrigin == TopAbs_FORWARD)
642 ElCLib::AdjustPeriodic(FparSeq.Last(),
643 FparSeq.Last() + 2.*PI,
644 Precision::PConfusion(), NewFpar, NewLpar);
646 ElCLib::AdjustPeriodic(FparSeq.Last() - 2.*PI,
648 Precision::PConfusion(), NewFpar, NewLpar);
649 Done = Standard_True;
653 case GeomAbs_Hyperbola:
655 gp_Hypr aHypr = BAcurve.Hyperbola();
656 gp_Hypr PrevHypr = GAprevcurve.Hyperbola();
657 if (aHypr.Focus1().Distance(PrevHypr.Focus1()) <= LinTol &&
658 aHypr.Focus2().Distance(PrevHypr.Focus2()) <= LinTol &&
659 Abs(aHypr.MajorRadius() - PrevHypr.MajorRadius()) <= LinTol &&
660 Abs(aHypr.MinorRadius() - PrevHypr.MinorRadius()) <= LinTol &&
661 aHypr.Axis().IsParallel(PrevHypr.Axis(), AngTol))
663 gp_Pnt P1 = ElCLib::Value(fpar, aHypr);
664 gp_Pnt P2 = ElCLib::Value(lpar, aHypr);
665 NewFpar = ElCLib::Parameter(PrevHypr, P1);
666 NewLpar = ElCLib::Parameter(PrevHypr, P2);
667 if (NewLpar < NewFpar)
669 Standard_Real MemNewFpar = NewFpar;
671 NewLpar = MemNewFpar;
672 ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
674 Done = Standard_True;
678 case GeomAbs_Parabola:
680 gp_Parab aParab = BAcurve.Parabola();
681 gp_Parab PrevParab = GAprevcurve.Parabola();
682 if (aParab.Location().Distance(PrevParab.Location()) <= LinTol &&
683 aParab.Focus().Distance(PrevParab.Focus()) <= LinTol &&
684 Abs(aParab.Focal() - PrevParab.Focal()) <= LinTol &&
685 aParab.Axis().IsParallel(PrevParab.Axis(), AngTol))
687 gp_Pnt P1 = ElCLib::Value(fpar, aParab);
688 gp_Pnt P2 = ElCLib::Value(lpar, aParab);
689 NewFpar = ElCLib::Parameter(PrevParab, P1);
690 NewLpar = ElCLib::Parameter(PrevParab, P2);
691 if (NewLpar < NewFpar)
693 Standard_Real MemNewFpar = NewFpar;
695 NewLpar = MemNewFpar;
696 ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
698 Done = Standard_True;
702 } //end of switch (aType)
703 } // end of else if (aType == CurType && ...
706 if (NewFpar < FparSeq.Last())
707 FparSeq(FparSeq.Length()) = NewFpar;
709 LparSeq(LparSeq.Length()) = NewLpar;
713 CurveSeq.Append(aCurve);
714 TopoDS_Shape aLocShape;
715 aLocShape.Location(aLoc);
716 aLocShape.Orientation(wexp.Orientation());
717 LocSeq.Append(aLocShape);
718 FparSeq.Append(fpar);
719 LparSeq.Append(lpar);
720 TolSeq.Append(BRep_Tool::Tolerance(CurVertex));
723 } // end of else (CurveSeq.IsEmpty()) -> not first time
724 } // end for (; wexp.More(); wexp.Next())
726 LastVertex = wexp.CurrentVertex();
727 TolSeq.Append(BRep_Tool::Tolerance(LastVertex));
729 if (!CurveSeq.IsEmpty())
731 Standard_Integer nb_curve = CurveSeq.Length(); //number of curves
732 TColGeom_Array1OfBSplineCurve tab(0,nb_curve-1); //array of the curves
733 TColStd_Array1OfReal tabtolvertex(0,nb_curve-1); //(0,nb_curve-2); //array of the tolerances
739 for (i = 1; i <= nb_curve; i++)
741 if (CurveSeq(i)->IsInstance(STANDARD_TYPE(Geom_TrimmedCurve)))
742 CurveSeq(i) = (*((Handle(Geom_TrimmedCurve)*)&(CurveSeq(i))))->BasisCurve();
744 Handle(Geom_TrimmedCurve) aTrCurve = new Geom_TrimmedCurve(CurveSeq(i), FparSeq(i), LparSeq(i));
745 tab(i-1) = GeomConvert::CurveToBSplineCurve(aTrCurve);
746 tab(i-1)->Transform(LocSeq(i).Location().Transformation());
747 GeomConvert::C0BSplineToC1BSplineCurve(tab(i-1), Precision::Confusion());
748 if (LocSeq(i).Orientation() == TopAbs_REVERSED)
752 //char* name = new char[100];
753 //sprintf(name, "c%d", i);
754 //DrawTrSurf::Set(name, tab(i-1));
757 tabtolvertex(i-2) = TolSeq(i-1);
758 } // end for (i = 1; i <= nb_curve; i++)
759 tabtolvertex(nb_curve-1) = TolSeq(TolSeq.Length());
761 Standard_Boolean closed_flag = Standard_False;
762 Standard_Real closed_tolerance = 0.;
763 if (FirstVertex.IsSame(LastVertex) &&
764 GeomLProp::Continuity(tab(0), tab(nb_curve-1),
765 tab(0)->FirstParameter(),
766 tab(nb_curve-1)->LastParameter(),
767 Standard_False, Standard_False, LinTol, AngTol) >= GeomAbs_G1)
769 closed_flag = Standard_True ;
770 closed_tolerance = BRep_Tool::Tolerance(FirstVertex);
773 Handle(TColGeom_HArray1OfBSplineCurve) concatcurve; //array of the concatenated curves
774 Handle(TColStd_HArray1OfInteger) ArrayOfIndices; //array of the remining Vertex
775 GeomConvert::ConcatC1(tab,
780 closed_tolerance); //C1 concatenation
782 if (concatcurve->Length() > 1)
784 GeomConvert_CompCurveToBSplineCurve Concat(concatcurve->Value(concatcurve->Lower()));
786 for (i = concatcurve->Lower()+1; i <= concatcurve->Upper(); i++)
787 Concat.Add( concatcurve->Value(i), LinTol, Standard_True );
789 concatcurve->SetValue(concatcurve->Lower(), Concat.BSplineCurve());
792 ResEdge = BRepLib_MakeEdge(concatcurve->Value(concatcurve->Lower()),
793 FirstVertex, LastVertex);
797 if (CurveSeq(1)->IsInstance(STANDARD_TYPE(Geom_TrimmedCurve)))
798 CurveSeq(1) = (*((Handle(Geom_TrimmedCurve)*)&(CurveSeq(i))))->BasisCurve();
800 CurveSeq(1)->Transform(LocSeq(1).Location().Transformation());
801 ResEdge = BRepLib_MakeEdge(CurveSeq(1),
802 FirstVertex, LastVertex,
803 FparSeq(1), LparSeq(1));
809 else if (aType == EDGE_CURVE_LENGTH) {
810 GEOMImpl_IVector aVI (aFunction);
813 Handle(GEOM_Function) aRefCurve = aVI.GetPoint1();
814 if (aRefCurve.IsNull()) Standard_NullObject::Raise("Argument Curve is null");
815 TopoDS_Shape aRefShape1 = aRefCurve->GetValue();
816 if (aRefShape1.ShapeType() != TopAbs_EDGE) {
817 Standard_TypeMismatch::Raise
818 ("Edge On Curve creation aborted : curve shape is not an edge");
820 TopoDS_Edge aRefEdge = TopoDS::Edge(aRefShape1);
821 TopoDS_Vertex V1, V2;
822 TopExp::Vertices(aRefEdge, V1, V2, Standard_True);
825 TopoDS_Vertex aRefVertex;
826 Handle(GEOM_Function) aRefPoint = aVI.GetPoint2();
827 if (aRefPoint.IsNull()) {
831 TopoDS_Shape aRefShape2 = aRefPoint->GetValue();
832 if (aRefShape2.ShapeType() != TopAbs_VERTEX) {
833 Standard_TypeMismatch::Raise
834 ("Edge On Curve creation aborted : start point shape is not a vertex");
836 aRefVertex = TopoDS::Vertex(aRefShape2);
838 gp_Pnt aRefPnt = BRep_Tool::Pnt(aRefVertex);
841 Standard_Real aLength = aVI.GetParameter();
842 //Standard_Real aCurveLength = IntTools::Length(aRefEdge);
843 //if (aLength > aCurveLength) {
844 // Standard_ConstructionError::Raise
845 // ("Edge On Curve creation aborted : given length is greater than edges length");
847 if (fabs(aLength) < Precision::Confusion()) {
848 Standard_ConstructionError::Raise
849 ("Edge On Curve creation aborted : given length is smaller than Precision::Confusion()");
853 Standard_Real UFirst, ULast;
854 Handle(Geom_Curve) EdgeCurve = BRep_Tool::Curve(aRefEdge, UFirst, ULast);
855 Handle(Geom_Curve) ReOrientedCurve = EdgeCurve;
857 Standard_Real dU = ULast - UFirst;
858 Standard_Real par1 = UFirst + 0.1 * dU;
859 Standard_Real par2 = ULast - 0.1 * dU;
861 gp_Pnt P1 = EdgeCurve->Value(par1);
862 gp_Pnt P2 = EdgeCurve->Value(par2);
864 if (aRefPnt.SquareDistance(P2) < aRefPnt.SquareDistance(P1)) {
865 ReOrientedCurve = EdgeCurve->Reversed();
866 UFirst = EdgeCurve->ReversedParameter(ULast);
869 // Get the point by length
870 GeomAdaptor_Curve AdapCurve = GeomAdaptor_Curve(ReOrientedCurve);
871 GCPnts_AbscissaPoint anAbsPnt (AdapCurve, aLength, UFirst);
872 Standard_Real aParam = anAbsPnt.Parameter();
874 if (AdapCurve.IsClosed() && aLength < 0.0) {
875 Standard_Real aTmp = aParam;
880 BRepBuilderAPI_MakeEdge aME (ReOrientedCurve, UFirst, aParam);
882 aShape = aME.Shape();
887 if (aShape.IsNull()) return 0;
889 // Check shape validity
890 BRepCheck_Analyzer ana (aShape, false);
891 if (!ana.IsValid()) {
892 Standard_ConstructionError::Raise("Algorithm have produced an invalid shape result");
895 aFunction->SetValue(aShape);
897 log.SetTouched(Label());
903 //=======================================================================
904 //function : GEOMImpl_ShapeDriver_Type_
906 //=======================================================================
907 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ShapeDriver_Type_()
910 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
911 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
912 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
913 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
914 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
915 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
918 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
919 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ShapeDriver",
920 sizeof(GEOMImpl_ShapeDriver),
922 (Standard_Address)_Ancestors,
923 (Standard_Address)NULL);
928 //=======================================================================
929 //function : DownCast
931 //=======================================================================
932 const Handle(GEOMImpl_ShapeDriver) Handle(GEOMImpl_ShapeDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
934 Handle(GEOMImpl_ShapeDriver) _anOtherObject;
936 if (!AnObject.IsNull()) {
937 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ShapeDriver))) {
938 _anOtherObject = Handle(GEOMImpl_ShapeDriver)((Handle(GEOMImpl_ShapeDriver)&)AnObject);
942 return _anOtherObject;