1 // Copyright (C) 2007-2011 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
23 #include <GEOMImpl_ShapeDriver.hxx>
25 #include <GEOMImpl_IShapes.hxx>
26 #include <GEOMImpl_IVector.hxx>
27 #include <GEOMImpl_Types.hxx>
28 #include <GEOMImpl_Block6Explorer.hxx>
30 #include <GEOM_Function.hxx>
33 #include <ShapeFix_Wire.hxx>
34 #include <ShapeFix_Edge.hxx>
35 #include <ShapeFix_Shape.hxx>
37 #include <BRep_Builder.hxx>
38 #include <BRep_Tool.hxx>
39 #include <BRepAdaptor_Curve.hxx>
40 #include <BRepAlgo_FaceRestrictor.hxx>
41 #include <BRepBuilderAPI_Copy.hxx>
42 #include <BRepBuilderAPI_Sewing.hxx>
43 #include <BRepBuilderAPI_MakeWire.hxx>
44 #include <BRepBuilderAPI_MakeEdge.hxx>
45 #include <BRepCheck.hxx>
46 #include <BRepCheck_Analyzer.hxx>
47 #include <BRepCheck_Shell.hxx>
48 #include <BRepClass3d_SolidClassifier.hxx>
49 #include <BRepLib.hxx>
50 #include <BRepLib_MakeEdge.hxx>
51 #include <BRepTools_WireExplorer.hxx>
53 #include <ShapeAnalysis_FreeBounds.hxx>
57 #include <TopExp_Explorer.hxx>
59 #include <TopoDS_Shape.hxx>
60 #include <TopoDS_Edge.hxx>
61 #include <TopoDS_Wire.hxx>
62 #include <TopoDS_Shell.hxx>
63 #include <TopoDS_Solid.hxx>
64 #include <TopoDS_Compound.hxx>
65 #include <TopoDS_Iterator.hxx>
67 #include <TopTools_MapOfShape.hxx>
68 #include <TopTools_HSequenceOfShape.hxx>
72 #include <GCPnts_AbscissaPoint.hxx>
74 #include <Geom_TrimmedCurve.hxx>
75 #include <GeomAbs_CurveType.hxx>
76 #include <GeomConvert_CompCurveToBSplineCurve.hxx>
77 #include <GeomConvert.hxx>
78 #include <GeomLProp.hxx>
80 #include <TColStd_SequenceOfReal.hxx>
81 #include <TColStd_HSequenceOfTransient.hxx>
82 #include <TColStd_Array1OfReal.hxx>
83 #include <TColGeom_SequenceOfCurve.hxx>
84 #include <TColGeom_Array1OfBSplineCurve.hxx>
85 #include <TColGeom_HArray1OfBSplineCurve.hxx>
87 #include <Precision.hxx>
89 #include <Standard_NullObject.hxx>
90 #include <Standard_TypeMismatch.hxx>
91 #include <Standard_ConstructionError.hxx>
93 //=======================================================================
96 //=======================================================================
97 const Standard_GUID& GEOMImpl_ShapeDriver::GetID()
99 static Standard_GUID aShapeDriver("FF1BBB54-5D14-4df2-980B-3A668264EA16");
104 //=======================================================================
105 //function : GEOMImpl_ShapeDriver
107 //=======================================================================
108 GEOMImpl_ShapeDriver::GEOMImpl_ShapeDriver()
112 //=======================================================================
115 //=======================================================================
116 Standard_Integer GEOMImpl_ShapeDriver::Execute(TFunction_Logbook& log) const
118 if (Label().IsNull()) return 0;
119 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
121 GEOMImpl_IShapes aCI (aFunction);
122 Standard_Integer aType = aFunction->GetType();
127 if (aType == WIRE_EDGES) {
128 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
133 for (unsigned int ind = 1; ind <= aShapes->Length(); ind++) {
134 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
135 TopoDS_Shape aShape_i = aRefShape->GetValue();
136 if (aShape_i.IsNull()) {
137 Standard_NullObject::Raise("Shape for wire construction is null");
139 if (aShape_i.ShapeType() == TopAbs_EDGE || aShape_i.ShapeType() == TopAbs_WIRE) {
140 TopExp_Explorer exp (aShape_i, TopAbs_EDGE);
141 for (; exp.More(); exp.Next())
142 B.Add(aWire, TopoDS::Edge(exp.Current()));
144 Standard_TypeMismatch::Raise
145 ("Shape for wire construction is neither an edge nor a wire");
150 Handle(ShapeFix_Wire) aFW = new ShapeFix_Wire;
154 if (aFW->StatusReorder(ShapeExtend_FAIL1)) {
155 Standard_ConstructionError::Raise("Wire construction failed: several loops detected");
156 } else if (aFW->StatusReorder(ShapeExtend_FAIL)) {
157 Standard_ConstructionError::Raise("Wire construction failed");
161 // IMP 0019766: Building a Wire from unconnected edges by introducing a tolerance
162 Standard_Real aTolerance = aCI.GetTolerance();
163 if (aTolerance < Precision::Confusion())
164 aTolerance = Precision::Confusion();
166 aFW->ClosedWireMode() = Standard_False;
167 aFW->FixConnected(aTolerance);
168 if (aFW->StatusConnected(ShapeExtend_FAIL)) {
169 Standard_ConstructionError::Raise("Wire construction failed: cannot build connected wire");
172 if (aFW->StatusConnected(ShapeExtend_DONE3)) {
173 // Confused with <prec> but not Analyzer.Precision(), set the same
174 aFW->FixGapsByRangesMode() = Standard_True;
175 if (aFW->FixGaps3d()) {
176 Handle(ShapeExtend_WireData) sbwd = aFW->WireData();
177 Handle(ShapeFix_Edge) aFe = new ShapeFix_Edge;
178 for (Standard_Integer iedge = 1; iedge <= sbwd->NbEdges(); iedge++) {
179 TopoDS_Edge aEdge = TopoDS::Edge(sbwd->Edge(iedge));
180 aFe->FixVertexTolerance(aEdge);
181 aFe->FixSameParameter(aEdge);
184 else if (aFW->StatusGaps3d(ShapeExtend_FAIL)) {
185 Standard_ConstructionError::Raise("Wire construction failed: cannot fix 3d gaps");
188 aShape = aFW->WireAPIMake();
190 else if (aType == FACE_WIRE) {
191 Handle(GEOM_Function) aRefBase = aCI.GetBase();
192 TopoDS_Shape aShapeBase = aRefBase->GetValue();
193 if (aShapeBase.IsNull()) Standard_NullObject::Raise("Argument Shape is null");
195 if (aShapeBase.ShapeType() == TopAbs_WIRE) {
196 W = TopoDS::Wire(aShapeBase);
197 // check the wire is closed
198 TopoDS_Vertex aV1, aV2;
199 TopExp::Vertices(W, aV1, aV2);
200 if ( !aV1.IsNull() && !aV2.IsNull() && aV1.IsSame(aV2) )
201 aShapeBase.Closed(true);
203 Standard_NullObject::Raise
204 ("Shape for face construction is not closed");
206 else if (aShapeBase.ShapeType() == TopAbs_EDGE && aShapeBase.Closed()) {
207 BRepBuilderAPI_MakeWire MW;
208 MW.Add(TopoDS::Edge(aShapeBase));
210 Standard_ConstructionError::Raise("Wire construction failed");
215 Standard_NullObject::Raise
216 ("Shape for face construction is neither a wire nor a closed edge");
218 GEOMImpl_Block6Explorer::MakeFace(W, aCI.GetIsPlanar(), aShape);
219 if (aShape.IsNull()) {
220 Standard_ConstructionError::Raise("Face construction failed");
223 else if (aType == FACE_WIRES) {
224 // Try to build a face from a set of wires and edges
227 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
228 int nbshapes = aShapes->Length();
230 Standard_ConstructionError::Raise("No wires or edges given");
233 // 1. Extract all edges from the given arguments
234 TopTools_MapOfShape aMapEdges;
235 Handle(TopTools_HSequenceOfShape) aSeqEdgesIn = new TopTools_HSequenceOfShape;
237 for (ind = 1; ind <= nbshapes; ind++) {
238 Handle(GEOM_Function) aRefSh_i = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
239 TopoDS_Shape aSh_i = aRefSh_i->GetValue();
241 TopExp_Explorer anExpE_i (aSh_i, TopAbs_EDGE);
242 for (; anExpE_i.More(); anExpE_i.Next()) {
243 if (aMapEdges.Add(anExpE_i.Current())) {
244 aSeqEdgesIn->Append(anExpE_i.Current());
249 // 2. Connect edges to wires of maximum length
250 Handle(TopTools_HSequenceOfShape) aSeqWiresOut;
251 ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdgesIn, Precision::Confusion(),
252 /*shared*/Standard_False, aSeqWiresOut);
254 // 3. Separate closed wires
255 Handle(TopTools_HSequenceOfShape) aSeqClosedWires = new TopTools_HSequenceOfShape;
256 Handle(TopTools_HSequenceOfShape) aSeqOpenWires = new TopTools_HSequenceOfShape;
257 for (ind = 1; ind <= aSeqWiresOut->Length(); ind++) {
258 if (aSeqWiresOut->Value(ind).Closed())
259 aSeqClosedWires->Append(aSeqWiresOut->Value(ind));
261 aSeqOpenWires->Append(aSeqWiresOut->Value(ind));
264 if (aSeqClosedWires->Length() < 1) {
265 Standard_ConstructionError::Raise
266 ("There is no closed contour can be built from the given arguments");
269 // 4. Build a face / list of faces from all the obtained closed wires
273 TopoDS_Wire aW1 = TopoDS::Wire(aSeqClosedWires->Value(1));
274 GEOMImpl_Block6Explorer::MakeFace(aW1, aCI.GetIsPlanar(), aFFace);
275 if (aFFace.IsNull()) {
276 Standard_ConstructionError::Raise("Face construction failed");
279 // 4.b. Add other wires
280 if (aSeqClosedWires->Length() == 1) {
285 BRep_Builder aBuilder;
286 aBuilder.MakeCompound(C);
287 BRepAlgo_FaceRestrictor FR;
289 TopAbs_Orientation OriF = aFFace.Orientation();
290 TopoDS_Shape aLocalS = aFFace.Oriented(TopAbs_FORWARD);
291 FR.Init(TopoDS::Face(aLocalS), Standard_False, Standard_True);
293 for (ind = 1; ind <= aSeqClosedWires->Length(); ind++) {
294 TopoDS_Wire aW = TopoDS::Wire(aSeqClosedWires->Value(ind));
303 for (; FR.More(); FR.Next()) {
304 aFace = FR.Current().Oriented(OriF);
305 aBuilder.Add(C, aFace);
316 // 5. Add all open wires to the result
317 if (aSeqOpenWires->Length() > 0) {
318 //Standard_ConstructionError::Raise("There are some open wires");
320 BRep_Builder aBuilder;
321 if (aSeqClosedWires->Length() == 1) {
322 aBuilder.MakeCompound(C);
323 aBuilder.Add(C, aShape);
326 C = TopoDS::Compound(aShape);
329 for (ind = 1; ind <= aSeqOpenWires->Length(); ind++) {
330 aBuilder.Add(C, aSeqOpenWires->Value(ind));
336 else if (aType == SHELL_FACES) {
337 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
338 unsigned int ind, nbshapes = aShapes->Length();
341 BRepBuilderAPI_Sewing aSewing(Precision::Confusion()*10.0);
342 for (ind = 1; ind <= nbshapes; ind++) {
343 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
344 TopoDS_Shape aShape_i = aRefShape->GetValue();
345 if (aShape_i.IsNull()) {
346 Standard_NullObject::Raise("Face for shell construction is null");
348 aSewing.Add(aShape_i);
353 TopoDS_Shape sh = aSewing.SewedShape();
354 if( sh.ShapeType()==TopAbs_FACE && nbshapes==1 ) {
355 // case for creation of shell from one face - PAL12722 (skl 26.06.2006)
362 //TopExp_Explorer exp (aSewing.SewedShape(), TopAbs_SHELL);
363 TopExp_Explorer exp (sh, TopAbs_SHELL);
364 Standard_Integer ish = 0;
365 for (; exp.More(); exp.Next()) {
366 aShape = exp.Current();
371 aShape = aSewing.SewedShape();
375 else if (aType == SOLID_SHELL) {
376 Handle(GEOM_Function) aRefShell = aCI.GetBase();
377 TopoDS_Shape aShapeShell = aRefShell->GetValue();
378 if (!aShapeShell.IsNull() && aShapeShell.ShapeType() == TopAbs_COMPOUND) {
379 TopoDS_Iterator It (aShapeShell, Standard_True, Standard_True);
380 if (It.More()) aShapeShell = It.Value();
382 if (aShapeShell.IsNull() || aShapeShell.ShapeType() != TopAbs_SHELL) {
383 Standard_NullObject::Raise("Shape for solid construction is null or not a shell");
386 BRepCheck_Shell chkShell(TopoDS::Shell(aShapeShell));
387 if (chkShell.Closed() == BRepCheck_NotClosed) return 0;
391 B.Add(Sol, aShapeShell);
392 BRepClass3d_SolidClassifier SC (Sol);
393 SC.PerformInfinitePoint(Precision::Confusion());
394 if (SC.State() == TopAbs_IN) {
396 B.Add(Sol, aShapeShell.Reversed());
402 else if (aType == SOLID_SHELLS) {
403 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
404 unsigned int ind, nbshapes = aShapes->Length();
405 Standard_Integer ish = 0;
410 for (ind = 1; ind <= nbshapes; ind++) {
411 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
412 TopoDS_Shape aShapeShell = aRefShape->GetValue();
413 if (aShapeShell.IsNull()) {
414 Standard_NullObject::Raise("Shell for solid construction is null");
416 if (aShapeShell.ShapeType() == TopAbs_COMPOUND) {
417 TopoDS_Iterator It (aShapeShell, Standard_True, Standard_True);
418 if (It.More()) aShapeShell = It.Value();
420 if (aShapeShell.ShapeType() == TopAbs_SHELL) {
421 B.Add(Sol, aShapeShell);
425 if (ish == 0) return 0;
426 BRepClass3d_SolidClassifier SC (Sol);
427 SC.PerformInfinitePoint(Precision::Confusion());
428 if (SC.State() == TopAbs_IN)
429 aShape = Sol.Reversed();
433 else if (aType == COMPOUND_SHAPES) {
434 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
435 unsigned int ind, nbshapes = aShapes->Length();
440 for (ind = 1; ind <= nbshapes; ind++) {
441 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
442 TopoDS_Shape aShape_i = aRefShape->GetValue();
443 if (aShape_i.IsNull()) {
444 Standard_NullObject::Raise("Shape for compound construction is null");
452 else if (aType == REVERSE_ORIENTATION) {
453 Handle(GEOM_Function) aRefShape = aCI.GetBase();
454 TopoDS_Shape aShape_i = aRefShape->GetValue();
455 if (aShape_i.IsNull()) {
456 Standard_NullObject::Raise("Shape for reverse is null");
459 BRepBuilderAPI_Copy Copy(aShape_i);
460 if( Copy.IsDone() ) {
461 TopoDS_Shape tds = Copy.Shape();
463 Standard_ConstructionError::Raise("Orientation aborted : Can not reverse the shape");
466 if( tds.Orientation() == TopAbs_FORWARD)
467 tds.Orientation(TopAbs_REVERSED);
469 tds.Orientation(TopAbs_FORWARD);
474 else if (aType == EDGE_WIRE) {
475 Handle(GEOM_Function) aRefBase = aCI.GetBase();
476 TopoDS_Shape aWire = aRefBase->GetValue();
477 Standard_Real LinTol = aCI.GetTolerance();
478 Standard_Real AngTol = aCI.GetAngularTolerance();
479 if (aWire.IsNull()) Standard_NullObject::Raise("Argument Wire is null");
483 BRepLib::BuildCurves3d(aWire);
484 Handle(ShapeFix_Shape) Fixer = new ShapeFix_Shape(aWire);
485 Fixer->SetPrecision(LinTol);
486 Fixer->SetMaxTolerance(LinTol);
488 TopoDS_Wire theWire = TopoDS::Wire(Fixer->Shape());
490 TColGeom_SequenceOfCurve CurveSeq;
491 TopTools_SequenceOfShape LocSeq;
492 TColStd_SequenceOfReal FparSeq;
493 TColStd_SequenceOfReal LparSeq;
494 TColStd_SequenceOfReal TolSeq;
495 GeomAbs_CurveType CurType;
496 TopoDS_Vertex FirstVertex, LastVertex;
498 BRepTools_WireExplorer wexp(theWire) ;
499 for (; wexp.More(); wexp.Next())
501 TopoDS_Edge anEdge = wexp.Current();
502 Standard_Real fpar, lpar;
503 TopLoc_Location aLoc;
504 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aLoc, fpar, lpar);
508 BRepAdaptor_Curve BAcurve(anEdge);
509 GeomAbs_CurveType aType = BAcurve.GetType();
511 Handle(Geom_Curve) aBasisCurve = BAcurve.Curve().Curve();
513 if (aBasisCurve->IsPeriodic())
514 ElCLib::AdjustPeriodic(aBasisCurve->FirstParameter(), aBasisCurve->LastParameter(),
515 Precision::PConfusion(), fpar, lpar);
517 if (CurveSeq.IsEmpty())
519 CurveSeq.Append(aCurve);
520 TopoDS_Shape aLocShape;
521 aLocShape.Location(aLoc);
522 aLocShape.Orientation(wexp.Orientation());
523 LocSeq.Append(aLocShape);
524 FparSeq.Append(fpar);
525 LparSeq.Append(lpar);
527 FirstVertex = wexp.CurrentVertex();
531 Standard_Boolean Done = Standard_False;
532 Standard_Real NewFpar, NewLpar;
533 GeomAdaptor_Curve GAprevcurve(CurveSeq.Last());
534 TopoDS_Vertex CurVertex = wexp.CurrentVertex();
535 TopoDS_Vertex CurFirstVer = TopExp::FirstVertex(anEdge);
536 TopAbs_Orientation ConnectByOrigin = (CurVertex.IsSame(CurFirstVer))? TopAbs_FORWARD : TopAbs_REVERSED;
537 if (aCurve == CurveSeq.Last())
541 if (aBasisCurve->IsPeriodic())
543 if (NewLpar < NewFpar)
544 NewLpar += aBasisCurve->Period();
545 if (ConnectByOrigin == TopAbs_FORWARD)
546 ElCLib::AdjustPeriodic(FparSeq.Last(),
547 FparSeq.Last() + aBasisCurve->Period(),
548 Precision::PConfusion(), NewFpar, NewLpar);
550 ElCLib::AdjustPeriodic(FparSeq.Last() - aBasisCurve->Period(),
552 Precision::PConfusion(), NewFpar, NewLpar);
554 Done = Standard_True;
556 else if (aType == CurType &&
557 aType != GeomAbs_BezierCurve &&
558 aType != GeomAbs_BSplineCurve &&
559 aType != GeomAbs_OtherCurve)
565 gp_Lin aLine = BAcurve.Line();
566 gp_Lin PrevLine = GAprevcurve.Line();
567 if (aLine.Contains(PrevLine.Location(), LinTol) &&
568 aLine.Direction().IsParallel(PrevLine.Direction(), AngTol))
570 gp_Pnt P1 = ElCLib::Value(fpar, aLine);
571 gp_Pnt P2 = ElCLib::Value(lpar, aLine);
572 NewFpar = ElCLib::Parameter(PrevLine, P1);
573 NewLpar = ElCLib::Parameter(PrevLine, P2);
574 if (NewLpar < NewFpar)
576 Standard_Real MemNewFpar = NewFpar;
578 NewLpar = MemNewFpar;
579 ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
581 Done = Standard_True;
587 gp_Circ aCircle = BAcurve.Circle();
588 gp_Circ PrevCircle = GAprevcurve.Circle();
589 if (aCircle.Location().Distance(PrevCircle.Location()) <= LinTol &&
590 Abs(aCircle.Radius() - PrevCircle.Radius()) <= LinTol &&
591 aCircle.Axis().IsParallel(PrevCircle.Axis(), AngTol))
593 if (aCircle.Axis().Direction() * PrevCircle.Axis().Direction() < 0.)
595 Standard_Real memfpar = fpar;
598 ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
600 gp_Pnt P1 = ElCLib::Value(fpar, aCircle);
601 gp_Pnt P2 = ElCLib::Value(lpar, aCircle);
602 NewFpar = ElCLib::Parameter(PrevCircle, P1);
603 NewLpar = ElCLib::Parameter(PrevCircle, P2);
604 if (NewLpar < NewFpar)
606 //Standard_Real MemNewFpar = NewFpar, MemNewLpar = NewLpar;
607 if (ConnectByOrigin == TopAbs_FORWARD)
608 ElCLib::AdjustPeriodic(FparSeq.Last(),
609 FparSeq.Last() + 2.*PI,
610 Precision::PConfusion(), NewFpar, NewLpar);
612 ElCLib::AdjustPeriodic(FparSeq.Last() - 2.*PI,
614 Precision::PConfusion(), NewFpar, NewLpar);
615 Done = Standard_True;
619 case GeomAbs_Ellipse:
621 gp_Elips anEllipse = BAcurve.Ellipse();
622 gp_Elips PrevEllipse = GAprevcurve.Ellipse();
623 if (anEllipse.Focus1().Distance(PrevEllipse.Focus1()) <= LinTol &&
624 anEllipse.Focus2().Distance(PrevEllipse.Focus2()) <= LinTol &&
625 Abs(anEllipse.MajorRadius() - PrevEllipse.MajorRadius()) <= LinTol &&
626 Abs(anEllipse.MinorRadius() - PrevEllipse.MinorRadius()) <= LinTol &&
627 anEllipse.Axis().IsParallel(PrevEllipse.Axis(), AngTol))
629 if (anEllipse.Axis().Direction() * PrevEllipse.Axis().Direction() < 0.)
631 Standard_Real memfpar = fpar;
634 ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
636 gp_Pnt P1 = ElCLib::Value(fpar, anEllipse);
637 gp_Pnt P2 = ElCLib::Value(lpar, anEllipse);
638 NewFpar = ElCLib::Parameter(PrevEllipse, P1);
639 NewLpar = ElCLib::Parameter(PrevEllipse, P2);
640 if (NewLpar < NewFpar)
642 if (ConnectByOrigin == TopAbs_FORWARD)
643 ElCLib::AdjustPeriodic(FparSeq.Last(),
644 FparSeq.Last() + 2.*PI,
645 Precision::PConfusion(), NewFpar, NewLpar);
647 ElCLib::AdjustPeriodic(FparSeq.Last() - 2.*PI,
649 Precision::PConfusion(), NewFpar, NewLpar);
650 Done = Standard_True;
654 case GeomAbs_Hyperbola:
656 gp_Hypr aHypr = BAcurve.Hyperbola();
657 gp_Hypr PrevHypr = GAprevcurve.Hyperbola();
658 if (aHypr.Focus1().Distance(PrevHypr.Focus1()) <= LinTol &&
659 aHypr.Focus2().Distance(PrevHypr.Focus2()) <= LinTol &&
660 Abs(aHypr.MajorRadius() - PrevHypr.MajorRadius()) <= LinTol &&
661 Abs(aHypr.MinorRadius() - PrevHypr.MinorRadius()) <= LinTol &&
662 aHypr.Axis().IsParallel(PrevHypr.Axis(), AngTol))
664 gp_Pnt P1 = ElCLib::Value(fpar, aHypr);
665 gp_Pnt P2 = ElCLib::Value(lpar, aHypr);
666 NewFpar = ElCLib::Parameter(PrevHypr, P1);
667 NewLpar = ElCLib::Parameter(PrevHypr, P2);
668 if (NewLpar < NewFpar)
670 Standard_Real MemNewFpar = NewFpar;
672 NewLpar = MemNewFpar;
673 ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
675 Done = Standard_True;
679 case GeomAbs_Parabola:
681 gp_Parab aParab = BAcurve.Parabola();
682 gp_Parab PrevParab = GAprevcurve.Parabola();
683 if (aParab.Location().Distance(PrevParab.Location()) <= LinTol &&
684 aParab.Focus().Distance(PrevParab.Focus()) <= LinTol &&
685 Abs(aParab.Focal() - PrevParab.Focal()) <= LinTol &&
686 aParab.Axis().IsParallel(PrevParab.Axis(), AngTol))
688 gp_Pnt P1 = ElCLib::Value(fpar, aParab);
689 gp_Pnt P2 = ElCLib::Value(lpar, aParab);
690 NewFpar = ElCLib::Parameter(PrevParab, P1);
691 NewLpar = ElCLib::Parameter(PrevParab, P2);
692 if (NewLpar < NewFpar)
694 Standard_Real MemNewFpar = NewFpar;
696 NewLpar = MemNewFpar;
697 ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
699 Done = Standard_True;
703 } //end of switch (aType)
704 } // end of else if (aType == CurType && ...
707 if (NewFpar < FparSeq.Last())
708 FparSeq(FparSeq.Length()) = NewFpar;
710 LparSeq(LparSeq.Length()) = NewLpar;
714 CurveSeq.Append(aCurve);
715 TopoDS_Shape aLocShape;
716 aLocShape.Location(aLoc);
717 aLocShape.Orientation(wexp.Orientation());
718 LocSeq.Append(aLocShape);
719 FparSeq.Append(fpar);
720 LparSeq.Append(lpar);
721 TolSeq.Append(BRep_Tool::Tolerance(CurVertex));
724 } // end of else (CurveSeq.IsEmpty()) -> not first time
725 } // end for (; wexp.More(); wexp.Next())
727 LastVertex = wexp.CurrentVertex();
728 TolSeq.Append(BRep_Tool::Tolerance(LastVertex));
730 if (!CurveSeq.IsEmpty())
732 Standard_Integer nb_curve = CurveSeq.Length(); //number of curves
733 TColGeom_Array1OfBSplineCurve tab(0,nb_curve-1); //array of the curves
734 TColStd_Array1OfReal tabtolvertex(0,nb_curve-1); //(0,nb_curve-2); //array of the tolerances
740 for (i = 1; i <= nb_curve; i++)
742 if (CurveSeq(i)->IsInstance(STANDARD_TYPE(Geom_TrimmedCurve)))
743 CurveSeq(i) = (*((Handle(Geom_TrimmedCurve)*)&(CurveSeq(i))))->BasisCurve();
745 Handle(Geom_TrimmedCurve) aTrCurve = new Geom_TrimmedCurve(CurveSeq(i), FparSeq(i), LparSeq(i));
746 tab(i-1) = GeomConvert::CurveToBSplineCurve(aTrCurve);
747 tab(i-1)->Transform(LocSeq(i).Location().Transformation());
748 GeomConvert::C0BSplineToC1BSplineCurve(tab(i-1), Precision::Confusion());
749 if (LocSeq(i).Orientation() == TopAbs_REVERSED)
753 //char* name = new char[100];
754 //sprintf(name, "c%d", i);
755 //DrawTrSurf::Set(name, tab(i-1));
758 tabtolvertex(i-2) = TolSeq(i-1);
759 } // end for (i = 1; i <= nb_curve; i++)
760 tabtolvertex(nb_curve-1) = TolSeq(TolSeq.Length());
762 Standard_Boolean closed_flag = Standard_False;
763 Standard_Real closed_tolerance = 0.;
764 if (FirstVertex.IsSame(LastVertex) &&
765 GeomLProp::Continuity(tab(0), tab(nb_curve-1),
766 tab(0)->FirstParameter(),
767 tab(nb_curve-1)->LastParameter(),
768 Standard_False, Standard_False, LinTol, AngTol) >= GeomAbs_G1)
770 closed_flag = Standard_True ;
771 closed_tolerance = BRep_Tool::Tolerance(FirstVertex);
774 Handle(TColGeom_HArray1OfBSplineCurve) concatcurve; //array of the concatenated curves
775 Handle(TColStd_HArray1OfInteger) ArrayOfIndices; //array of the remining Vertex
776 GeomConvert::ConcatC1(tab,
781 closed_tolerance); //C1 concatenation
783 if (concatcurve->Length() > 1)
785 GeomConvert_CompCurveToBSplineCurve Concat(concatcurve->Value(concatcurve->Lower()));
787 for (i = concatcurve->Lower()+1; i <= concatcurve->Upper(); i++)
788 Concat.Add( concatcurve->Value(i), LinTol, Standard_True );
790 concatcurve->SetValue(concatcurve->Lower(), Concat.BSplineCurve());
792 // rnc : prevents the driver from building an edge without C1 continuity
793 if (concatcurve->Value(concatcurve->Lower())->Continuity()==GeomAbs_C0){
794 Standard_ConstructionError::Raise("Construction aborted : The given Wire has sharp bends between some Edges, no valid Edge can be built");
796 ResEdge = BRepLib_MakeEdge(concatcurve->Value(concatcurve->Lower()),
797 FirstVertex, LastVertex);
801 if (CurveSeq(1)->IsInstance(STANDARD_TYPE(Geom_TrimmedCurve)))
802 CurveSeq(1) = (*((Handle(Geom_TrimmedCurve)*)&(CurveSeq(i))))->BasisCurve();
804 CurveSeq(1)->Transform(LocSeq(1).Location().Transformation());
805 ResEdge = BRepLib_MakeEdge(CurveSeq(1),
806 FirstVertex, LastVertex,
807 FparSeq(1), LparSeq(1));
813 else if (aType == EDGE_CURVE_LENGTH) {
814 GEOMImpl_IVector aVI (aFunction);
817 Handle(GEOM_Function) aRefCurve = aVI.GetPoint1();
818 if (aRefCurve.IsNull()) Standard_NullObject::Raise("Argument Curve is null");
819 TopoDS_Shape aRefShape1 = aRefCurve->GetValue();
820 if (aRefShape1.ShapeType() != TopAbs_EDGE) {
821 Standard_TypeMismatch::Raise
822 ("Edge On Curve creation aborted : curve shape is not an edge");
824 TopoDS_Edge aRefEdge = TopoDS::Edge(aRefShape1);
825 TopoDS_Vertex V1, V2;
826 TopExp::Vertices(aRefEdge, V1, V2, Standard_True);
829 TopoDS_Vertex aRefVertex;
830 Handle(GEOM_Function) aRefPoint = aVI.GetPoint2();
831 if (aRefPoint.IsNull()) {
835 TopoDS_Shape aRefShape2 = aRefPoint->GetValue();
836 if (aRefShape2.ShapeType() != TopAbs_VERTEX) {
837 Standard_TypeMismatch::Raise
838 ("Edge On Curve creation aborted : start point shape is not a vertex");
840 aRefVertex = TopoDS::Vertex(aRefShape2);
842 gp_Pnt aRefPnt = BRep_Tool::Pnt(aRefVertex);
845 Standard_Real aLength = aVI.GetParameter();
846 //Standard_Real aCurveLength = IntTools::Length(aRefEdge);
847 //if (aLength > aCurveLength) {
848 // Standard_ConstructionError::Raise
849 // ("Edge On Curve creation aborted : given length is greater than edges length");
851 if (fabs(aLength) < Precision::Confusion()) {
852 Standard_ConstructionError::Raise
853 ("Edge On Curve creation aborted : given length is smaller than Precision::Confusion()");
857 Standard_Real UFirst, ULast;
858 Handle(Geom_Curve) EdgeCurve = BRep_Tool::Curve(aRefEdge, UFirst, ULast);
859 Handle(Geom_Curve) ReOrientedCurve = EdgeCurve;
861 Standard_Real dU = ULast - UFirst;
862 Standard_Real par1 = UFirst + 0.1 * dU;
863 Standard_Real par2 = ULast - 0.1 * dU;
865 gp_Pnt P1 = EdgeCurve->Value(par1);
866 gp_Pnt P2 = EdgeCurve->Value(par2);
868 if (aRefPnt.SquareDistance(P2) < aRefPnt.SquareDistance(P1)) {
869 ReOrientedCurve = EdgeCurve->Reversed();
870 UFirst = EdgeCurve->ReversedParameter(ULast);
873 // Get the point by length
874 GeomAdaptor_Curve AdapCurve = GeomAdaptor_Curve(ReOrientedCurve);
875 GCPnts_AbscissaPoint anAbsPnt (AdapCurve, aLength, UFirst);
876 Standard_Real aParam = anAbsPnt.Parameter();
878 if (AdapCurve.IsClosed() && aLength < 0.0) {
879 Standard_Real aTmp = aParam;
884 BRepBuilderAPI_MakeEdge aME (ReOrientedCurve, UFirst, aParam);
886 aShape = aME.Shape();
891 if (aShape.IsNull()) return 0;
893 // Check shape validity
894 BRepCheck_Analyzer ana (aShape, false);
895 if (!ana.IsValid()) {
896 //Standard_ConstructionError::Raise("Algorithm have produced an invalid shape result");
899 aFunction->SetValue(aShape);
901 log.SetTouched(Label());
907 //=======================================================================
908 //function : GEOMImpl_ShapeDriver_Type_
910 //=======================================================================
911 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ShapeDriver_Type_()
914 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
915 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
916 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
917 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
918 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
919 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
922 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
923 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ShapeDriver",
924 sizeof(GEOMImpl_ShapeDriver),
926 (Standard_Address)_Ancestors,
927 (Standard_Address)NULL);
932 //=======================================================================
933 //function : DownCast
935 //=======================================================================
936 const Handle(GEOMImpl_ShapeDriver) Handle(GEOMImpl_ShapeDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
938 Handle(GEOMImpl_ShapeDriver) _anOtherObject;
940 if (!AnObject.IsNull()) {
941 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ShapeDriver))) {
942 _anOtherObject = Handle(GEOMImpl_ShapeDriver)((Handle(GEOMImpl_ShapeDriver)&)AnObject);
946 return _anOtherObject;