1 // Copyright (C) 2007-2012 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 //modified by NIZNHY-PKV Wed Dec 28 13:48:20 2011f
95 // void KeepEdgesOrder(const Handle(TopTools_HSequenceOfShape)& aEdges,
96 // const Handle(TopTools_HSequenceOfShape)& aWires);
97 //modified by NIZNHY-PKV Wed Dec 28 13:48:23 2011t
99 //=======================================================================
102 //=======================================================================
103 const Standard_GUID& GEOMImpl_ShapeDriver::GetID()
105 static Standard_GUID aShapeDriver("FF1BBB54-5D14-4df2-980B-3A668264EA16");
110 //=======================================================================
111 //function : GEOMImpl_ShapeDriver
113 //=======================================================================
114 GEOMImpl_ShapeDriver::GEOMImpl_ShapeDriver()
118 //=======================================================================
121 //=======================================================================
122 Standard_Integer GEOMImpl_ShapeDriver::Execute(TFunction_Logbook& log) const
124 if (Label().IsNull()) return 0;
125 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
127 GEOMImpl_IShapes aCI (aFunction);
128 Standard_Integer aType = aFunction->GetType();
131 TCollection_AsciiString aWarning;
135 if (aType == WIRE_EDGES) {
136 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
141 for (unsigned int ind = 1; ind <= aShapes->Length(); ind++) {
142 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
143 TopoDS_Shape aShape_i = aRefShape->GetValue();
144 if (aShape_i.IsNull()) {
145 Standard_NullObject::Raise("Shape for wire construction is null");
147 if (aShape_i.ShapeType() == TopAbs_EDGE || aShape_i.ShapeType() == TopAbs_WIRE) {
148 TopExp_Explorer exp (aShape_i, TopAbs_EDGE);
149 for (; exp.More(); exp.Next())
150 B.Add(aWire, TopoDS::Edge(exp.Current()));
152 Standard_TypeMismatch::Raise
153 ("Shape for wire construction is neither an edge nor a wire");
158 Handle(ShapeFix_Wire) aFW = new ShapeFix_Wire;
162 if (aFW->StatusReorder(ShapeExtend_FAIL1)) {
163 Standard_ConstructionError::Raise("Wire construction failed: several loops detected");
164 } else if (aFW->StatusReorder(ShapeExtend_FAIL)) {
165 Standard_ConstructionError::Raise("Wire construction failed");
169 // IMP 0019766: Building a Wire from unconnected edges by introducing a tolerance
170 Standard_Real aTolerance = aCI.GetTolerance();
171 if (aTolerance < Precision::Confusion())
172 aTolerance = Precision::Confusion();
174 aFW->ClosedWireMode() = Standard_False;
175 aFW->FixConnected(aTolerance);
176 if (aFW->StatusConnected(ShapeExtend_FAIL)) {
177 Standard_ConstructionError::Raise("Wire construction failed: cannot build connected wire");
180 if (aFW->StatusConnected(ShapeExtend_DONE3)) {
181 // Confused with <prec> but not Analyzer.Precision(), set the same
182 aFW->FixGapsByRangesMode() = Standard_True;
183 if (aFW->FixGaps3d()) {
184 Handle(ShapeExtend_WireData) sbwd = aFW->WireData();
185 Handle(ShapeFix_Edge) aFe = new ShapeFix_Edge;
186 for (Standard_Integer iedge = 1; iedge <= sbwd->NbEdges(); iedge++) {
187 TopoDS_Edge aEdge = TopoDS::Edge(sbwd->Edge(iedge));
188 aFe->FixVertexTolerance(aEdge);
189 aFe->FixSameParameter(aEdge);
192 else if (aFW->StatusGaps3d(ShapeExtend_FAIL)) {
193 Standard_ConstructionError::Raise("Wire construction failed: cannot fix 3d gaps");
196 aShape = aFW->WireAPIMake();
198 else if (aType == FACE_WIRE) {
199 Handle(GEOM_Function) aRefBase = aCI.GetBase();
200 TopoDS_Shape aShapeBase = aRefBase->GetValue();
201 if (aShapeBase.IsNull()) Standard_NullObject::Raise("Argument Shape is null");
203 if (aShapeBase.ShapeType() == TopAbs_WIRE) {
204 W = TopoDS::Wire(aShapeBase);
205 // check the wire is closed
206 TopoDS_Vertex aV1, aV2;
207 TopExp::Vertices(W, aV1, aV2);
208 if ( !aV1.IsNull() && !aV2.IsNull() && aV1.IsSame(aV2) )
209 aShapeBase.Closed(true);
211 Standard_NullObject::Raise
212 ("Shape for face construction is not closed");
214 else if (aShapeBase.ShapeType() == TopAbs_EDGE && aShapeBase.Closed()) {
215 BRepBuilderAPI_MakeWire MW;
216 MW.Add(TopoDS::Edge(aShapeBase));
218 Standard_ConstructionError::Raise("Wire construction failed");
223 Standard_NullObject::Raise
224 ("Shape for face construction is neither a wire nor a closed edge");
226 aWarning = GEOMImpl_Block6Explorer::MakeFace(W, aCI.GetIsPlanar(), aShape);
227 if (aShape.IsNull()) {
228 Standard_ConstructionError::Raise("Face construction failed");
231 else if (aType == FACE_WIRES) {
232 // Try to build a face from a set of wires and edges
235 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
236 int nbshapes = aShapes->Length();
238 Standard_ConstructionError::Raise("No wires or edges given");
241 // 1. Extract all edges from the given arguments
242 TopTools_MapOfShape aMapEdges;
243 Handle(TopTools_HSequenceOfShape) aSeqEdgesIn = new TopTools_HSequenceOfShape;
245 for (ind = 1; ind <= nbshapes; ind++) {
246 Handle(GEOM_Function) aRefSh_i = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
247 TopoDS_Shape aSh_i = aRefSh_i->GetValue();
249 TopExp_Explorer anExpE_i (aSh_i, TopAbs_EDGE);
250 for (; anExpE_i.More(); anExpE_i.Next()) {
251 if (aMapEdges.Add(anExpE_i.Current())) {
252 aSeqEdgesIn->Append(anExpE_i.Current());
257 // 2. Connect edges to wires of maximum length
258 Handle(TopTools_HSequenceOfShape) aSeqWiresOut;
259 ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdgesIn, Precision::Confusion(),
260 /*shared*/Standard_False, aSeqWiresOut);
261 //modified by NIZNHY-PKV Wed Dec 28 13:46:55 2011f
262 //KeepEdgesOrder(aSeqEdgesIn, aSeqWiresOut);
263 //modified by NIZNHY-PKV Wed Dec 28 13:46:59 2011t
265 // 3. Separate closed wires
266 Handle(TopTools_HSequenceOfShape) aSeqClosedWires = new TopTools_HSequenceOfShape;
267 Handle(TopTools_HSequenceOfShape) aSeqOpenWires = new TopTools_HSequenceOfShape;
268 for (ind = 1; ind <= aSeqWiresOut->Length(); ind++) {
269 if (aSeqWiresOut->Value(ind).Closed())
270 aSeqClosedWires->Append(aSeqWiresOut->Value(ind));
272 aSeqOpenWires->Append(aSeqWiresOut->Value(ind));
275 if (aSeqClosedWires->Length() < 1) {
276 Standard_ConstructionError::Raise
277 ("There is no closed contour can be built from the given arguments");
280 // 4. Build a face / list of faces from all the obtained closed wires
284 TopoDS_Wire aW1 = TopoDS::Wire(aSeqClosedWires->Value(1));
285 aWarning = GEOMImpl_Block6Explorer::MakeFace(aW1, aCI.GetIsPlanar(), aFFace);
286 if (aFFace.IsNull()) {
287 Standard_ConstructionError::Raise("Face construction failed");
290 // 4.b. Add other wires
291 if (aSeqClosedWires->Length() == 1) {
296 BRep_Builder aBuilder;
297 aBuilder.MakeCompound(C);
298 BRepAlgo_FaceRestrictor FR;
300 TopAbs_Orientation OriF = aFFace.Orientation();
301 TopoDS_Shape aLocalS = aFFace.Oriented(TopAbs_FORWARD);
302 FR.Init(TopoDS::Face(aLocalS), Standard_False, Standard_True);
304 for (ind = 1; ind <= aSeqClosedWires->Length(); ind++) {
305 TopoDS_Wire aW = TopoDS::Wire(aSeqClosedWires->Value(ind));
314 for (; FR.More(); FR.Next()) {
315 aFace = FR.Current().Oriented(OriF);
316 aBuilder.Add(C, aFace);
327 // 5. Add all open wires to the result
328 if (aSeqOpenWires->Length() > 0) {
329 //Standard_ConstructionError::Raise("There are some open wires");
331 BRep_Builder aBuilder;
332 if (aSeqClosedWires->Length() == 1) {
333 aBuilder.MakeCompound(C);
334 aBuilder.Add(C, aShape);
337 C = TopoDS::Compound(aShape);
340 for (ind = 1; ind <= aSeqOpenWires->Length(); ind++) {
341 aBuilder.Add(C, aSeqOpenWires->Value(ind));
347 else if (aType == SHELL_FACES) {
348 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
349 unsigned int ind, nbshapes = aShapes->Length();
352 BRepBuilderAPI_Sewing aSewing(Precision::Confusion()*10.0);
353 for (ind = 1; ind <= nbshapes; ind++) {
354 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
355 TopoDS_Shape aShape_i = aRefShape->GetValue();
356 if (aShape_i.IsNull()) {
357 Standard_NullObject::Raise("Face for shell construction is null");
359 aSewing.Add(aShape_i);
364 TopoDS_Shape sh = aSewing.SewedShape();
365 if( sh.ShapeType()==TopAbs_FACE && nbshapes==1 ) {
366 // case for creation of shell from one face - PAL12722 (skl 26.06.2006)
373 //TopExp_Explorer exp (aSewing.SewedShape(), TopAbs_SHELL);
374 TopExp_Explorer exp (sh, TopAbs_SHELL);
375 Standard_Integer ish = 0;
376 for (; exp.More(); exp.Next()) {
377 aShape = exp.Current();
382 aShape = aSewing.SewedShape();
386 else if (aType == SOLID_SHELL) {
387 Handle(GEOM_Function) aRefShell = aCI.GetBase();
388 TopoDS_Shape aShapeShell = aRefShell->GetValue();
389 if (!aShapeShell.IsNull() && aShapeShell.ShapeType() == TopAbs_COMPOUND) {
390 TopoDS_Iterator It (aShapeShell, Standard_True, Standard_True);
391 if (It.More()) aShapeShell = It.Value();
393 if (aShapeShell.IsNull() || aShapeShell.ShapeType() != TopAbs_SHELL) {
394 Standard_NullObject::Raise("Shape for solid construction is null or not a shell");
397 BRepCheck_Shell chkShell(TopoDS::Shell(aShapeShell));
398 if (chkShell.Closed() == BRepCheck_NotClosed) return 0;
402 B.Add(Sol, aShapeShell);
403 BRepClass3d_SolidClassifier SC (Sol);
404 SC.PerformInfinitePoint(Precision::Confusion());
405 if (SC.State() == TopAbs_IN) {
407 B.Add(Sol, aShapeShell.Reversed());
413 else if (aType == SOLID_SHELLS) {
414 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
415 unsigned int ind, nbshapes = aShapes->Length();
416 Standard_Integer ish = 0;
421 for (ind = 1; ind <= nbshapes; ind++) {
422 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
423 TopoDS_Shape aShapeShell = aRefShape->GetValue();
424 if (aShapeShell.IsNull()) {
425 Standard_NullObject::Raise("Shell for solid construction is null");
427 if (aShapeShell.ShapeType() == TopAbs_COMPOUND) {
428 TopoDS_Iterator It (aShapeShell, Standard_True, Standard_True);
429 if (It.More()) aShapeShell = It.Value();
431 if (aShapeShell.ShapeType() == TopAbs_SHELL) {
432 B.Add(Sol, aShapeShell);
436 if (ish == 0) return 0;
437 BRepClass3d_SolidClassifier SC (Sol);
438 SC.PerformInfinitePoint(Precision::Confusion());
439 if (SC.State() == TopAbs_IN)
440 aShape = Sol.Reversed();
444 else if (aType == COMPOUND_SHAPES) {
445 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
446 unsigned int ind, nbshapes = aShapes->Length();
451 for (ind = 1; ind <= nbshapes; ind++) {
452 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
453 TopoDS_Shape aShape_i = aRefShape->GetValue();
454 if (aShape_i.IsNull()) {
455 Standard_NullObject::Raise("Shape for compound construction is null");
464 else if (aType == REVERSE_ORIENTATION) {
465 Handle(GEOM_Function) aRefShape = aCI.GetBase();
466 TopoDS_Shape aShape_i = aRefShape->GetValue();
467 if (aShape_i.IsNull()) {
468 Standard_NullObject::Raise("Shape for reverse is null");
471 BRepBuilderAPI_Copy Copy(aShape_i);
472 if( Copy.IsDone() ) {
473 TopoDS_Shape tds = Copy.Shape();
475 Standard_ConstructionError::Raise("Orientation aborted : Can not reverse the shape");
478 if( tds.Orientation() == TopAbs_FORWARD)
479 tds.Orientation(TopAbs_REVERSED);
481 tds.Orientation(TopAbs_FORWARD);
487 else if (aType == EDGE_WIRE) {
488 Handle(GEOM_Function) aRefBase = aCI.GetBase();
489 TopoDS_Shape aWire = aRefBase->GetValue();
490 Standard_Real LinTol = aCI.GetTolerance();
491 Standard_Real AngTol = aCI.GetAngularTolerance();
492 if (aWire.IsNull()) Standard_NullObject::Raise("Argument Wire is null");
496 BRepLib::BuildCurves3d(aWire);
497 Handle(ShapeFix_Shape) Fixer = new ShapeFix_Shape(aWire);
498 Fixer->SetPrecision(LinTol);
499 Fixer->SetMaxTolerance(LinTol);
501 TopoDS_Wire theWire = TopoDS::Wire(Fixer->Shape());
503 TColGeom_SequenceOfCurve CurveSeq;
504 TopTools_SequenceOfShape LocSeq;
505 TColStd_SequenceOfReal FparSeq;
506 TColStd_SequenceOfReal LparSeq;
507 TColStd_SequenceOfReal TolSeq;
508 GeomAbs_CurveType CurType;
509 TopoDS_Vertex FirstVertex, LastVertex;
510 Standard_Boolean FinalReverse = Standard_False;
512 BRepTools_WireExplorer wexp(theWire) ;
513 for (; wexp.More(); wexp.Next())
515 TopoDS_Edge anEdge = wexp.Current();
516 Standard_Real fpar, lpar;
517 TopLoc_Location aLoc;
518 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aLoc, fpar, lpar);
522 BRepAdaptor_Curve BAcurve(anEdge);
523 GeomAbs_CurveType aType = BAcurve.GetType();
525 Handle(Geom_Curve) aBasisCurve = BAcurve.Curve().Curve();
527 if (aBasisCurve->IsPeriodic())
528 ElCLib::AdjustPeriodic(aBasisCurve->FirstParameter(), aBasisCurve->LastParameter(),
529 Precision::PConfusion(), fpar, lpar);
531 if (CurveSeq.IsEmpty())
533 CurveSeq.Append(aCurve);
534 TopoDS_Shape aLocShape;
535 aLocShape.Location(aLoc);
536 aLocShape.Orientation(wexp.Orientation());
537 LocSeq.Append(aLocShape);
538 FparSeq.Append(fpar);
539 LparSeq.Append(lpar);
541 FirstVertex = wexp.CurrentVertex();
542 if (anEdge.Orientation() == TopAbs_REVERSED)
543 FinalReverse = Standard_True;
547 Standard_Boolean Done = Standard_False;
548 Standard_Real NewFpar, NewLpar;
549 GeomAdaptor_Curve GAprevcurve(CurveSeq.Last());
550 TopoDS_Vertex CurVertex = wexp.CurrentVertex();
551 TopoDS_Vertex CurFirstVer = TopExp::FirstVertex(anEdge);
552 TopAbs_Orientation ConnectByOrigin = (CurVertex.IsSame(CurFirstVer))? TopAbs_FORWARD : TopAbs_REVERSED;
553 if (aCurve == CurveSeq.Last())
557 if (aBasisCurve->IsPeriodic())
559 if (NewLpar < NewFpar)
560 NewLpar += aBasisCurve->Period();
561 if (ConnectByOrigin == TopAbs_FORWARD)
562 ElCLib::AdjustPeriodic(FparSeq.Last(),
563 FparSeq.Last() + aBasisCurve->Period(),
564 Precision::PConfusion(), NewFpar, NewLpar);
566 ElCLib::AdjustPeriodic(FparSeq.Last() - aBasisCurve->Period(),
568 Precision::PConfusion(), NewFpar, NewLpar);
570 Done = Standard_True;
572 else if (aType == CurType &&
573 aType != GeomAbs_BezierCurve &&
574 aType != GeomAbs_BSplineCurve &&
575 aType != GeomAbs_OtherCurve)
581 gp_Lin aLine = BAcurve.Line();
582 gp_Lin PrevLine = GAprevcurve.Line();
583 if (aLine.Contains(PrevLine.Location(), LinTol) &&
584 aLine.Direction().IsParallel(PrevLine.Direction(), AngTol))
586 gp_Pnt P1 = ElCLib::Value(fpar, aLine);
587 gp_Pnt P2 = ElCLib::Value(lpar, aLine);
588 NewFpar = ElCLib::Parameter(PrevLine, P1);
589 NewLpar = ElCLib::Parameter(PrevLine, P2);
590 if (NewLpar < NewFpar)
592 Standard_Real MemNewFpar = NewFpar;
594 NewLpar = MemNewFpar;
595 ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
597 Done = Standard_True;
603 gp_Circ aCircle = BAcurve.Circle();
604 gp_Circ PrevCircle = GAprevcurve.Circle();
605 if (aCircle.Location().Distance(PrevCircle.Location()) <= LinTol &&
606 Abs(aCircle.Radius() - PrevCircle.Radius()) <= LinTol &&
607 aCircle.Axis().IsParallel(PrevCircle.Axis(), AngTol))
609 if (aCircle.Axis().Direction() * PrevCircle.Axis().Direction() < 0.)
611 Standard_Real memfpar = fpar;
614 ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
616 gp_Pnt P1 = ElCLib::Value(fpar, aCircle);
617 gp_Pnt P2 = ElCLib::Value(lpar, aCircle);
618 NewFpar = ElCLib::Parameter(PrevCircle, P1);
619 NewLpar = ElCLib::Parameter(PrevCircle, P2);
620 if (NewLpar < NewFpar)
622 //Standard_Real MemNewFpar = NewFpar, MemNewLpar = NewLpar;
623 if (ConnectByOrigin == TopAbs_FORWARD)
624 ElCLib::AdjustPeriodic(FparSeq.Last(),
625 FparSeq.Last() + 2.*M_PI,
626 Precision::PConfusion(), NewFpar, NewLpar);
628 ElCLib::AdjustPeriodic(FparSeq.Last() - 2.*M_PI,
630 Precision::PConfusion(), NewFpar, NewLpar);
631 Done = Standard_True;
635 case GeomAbs_Ellipse:
637 gp_Elips anEllipse = BAcurve.Ellipse();
638 gp_Elips PrevEllipse = GAprevcurve.Ellipse();
639 if (anEllipse.Focus1().Distance(PrevEllipse.Focus1()) <= LinTol &&
640 anEllipse.Focus2().Distance(PrevEllipse.Focus2()) <= LinTol &&
641 Abs(anEllipse.MajorRadius() - PrevEllipse.MajorRadius()) <= LinTol &&
642 Abs(anEllipse.MinorRadius() - PrevEllipse.MinorRadius()) <= LinTol &&
643 anEllipse.Axis().IsParallel(PrevEllipse.Axis(), AngTol))
645 if (anEllipse.Axis().Direction() * PrevEllipse.Axis().Direction() < 0.)
647 Standard_Real memfpar = fpar;
650 ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
652 gp_Pnt P1 = ElCLib::Value(fpar, anEllipse);
653 gp_Pnt P2 = ElCLib::Value(lpar, anEllipse);
654 NewFpar = ElCLib::Parameter(PrevEllipse, P1);
655 NewLpar = ElCLib::Parameter(PrevEllipse, P2);
656 if (NewLpar < NewFpar)
658 if (ConnectByOrigin == TopAbs_FORWARD)
659 ElCLib::AdjustPeriodic(FparSeq.Last(),
660 FparSeq.Last() + 2.*M_PI,
661 Precision::PConfusion(), NewFpar, NewLpar);
663 ElCLib::AdjustPeriodic(FparSeq.Last() - 2.*M_PI,
665 Precision::PConfusion(), NewFpar, NewLpar);
666 Done = Standard_True;
670 case GeomAbs_Hyperbola:
672 gp_Hypr aHypr = BAcurve.Hyperbola();
673 gp_Hypr PrevHypr = GAprevcurve.Hyperbola();
674 if (aHypr.Focus1().Distance(PrevHypr.Focus1()) <= LinTol &&
675 aHypr.Focus2().Distance(PrevHypr.Focus2()) <= LinTol &&
676 Abs(aHypr.MajorRadius() - PrevHypr.MajorRadius()) <= LinTol &&
677 Abs(aHypr.MinorRadius() - PrevHypr.MinorRadius()) <= LinTol &&
678 aHypr.Axis().IsParallel(PrevHypr.Axis(), AngTol))
680 gp_Pnt P1 = ElCLib::Value(fpar, aHypr);
681 gp_Pnt P2 = ElCLib::Value(lpar, aHypr);
682 NewFpar = ElCLib::Parameter(PrevHypr, P1);
683 NewLpar = ElCLib::Parameter(PrevHypr, P2);
684 if (NewLpar < NewFpar)
686 Standard_Real MemNewFpar = NewFpar;
688 NewLpar = MemNewFpar;
689 ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
691 Done = Standard_True;
695 case GeomAbs_Parabola:
697 gp_Parab aParab = BAcurve.Parabola();
698 gp_Parab PrevParab = GAprevcurve.Parabola();
699 if (aParab.Location().Distance(PrevParab.Location()) <= LinTol &&
700 aParab.Focus().Distance(PrevParab.Focus()) <= LinTol &&
701 Abs(aParab.Focal() - PrevParab.Focal()) <= LinTol &&
702 aParab.Axis().IsParallel(PrevParab.Axis(), AngTol))
704 gp_Pnt P1 = ElCLib::Value(fpar, aParab);
705 gp_Pnt P2 = ElCLib::Value(lpar, aParab);
706 NewFpar = ElCLib::Parameter(PrevParab, P1);
707 NewLpar = ElCLib::Parameter(PrevParab, P2);
708 if (NewLpar < NewFpar)
710 Standard_Real MemNewFpar = NewFpar;
712 NewLpar = MemNewFpar;
713 ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
715 Done = Standard_True;
719 } //end of switch (aType)
720 } // end of else if (aType == CurType && ...
723 if (NewFpar < FparSeq.Last())
724 FparSeq(FparSeq.Length()) = NewFpar;
726 LparSeq(LparSeq.Length()) = NewLpar;
730 CurveSeq.Append(aCurve);
731 TopoDS_Shape aLocShape;
732 aLocShape.Location(aLoc);
733 aLocShape.Orientation(wexp.Orientation());
734 LocSeq.Append(aLocShape);
735 FparSeq.Append(fpar);
736 LparSeq.Append(lpar);
737 TolSeq.Append(BRep_Tool::Tolerance(CurVertex));
740 } // end of else (CurveSeq.IsEmpty()) -> not first time
741 } // end for (; wexp.More(); wexp.Next())
743 LastVertex = wexp.CurrentVertex();
744 TolSeq.Append(BRep_Tool::Tolerance(LastVertex));
746 TopoDS_Vertex FirstVtx_final = (FinalReverse)? LastVertex : FirstVertex;
747 FirstVtx_final.Orientation(TopAbs_FORWARD);
748 TopoDS_Vertex LastVtx_final = (FinalReverse)? FirstVertex : LastVertex;
749 LastVtx_final.Orientation(TopAbs_REVERSED);
751 if (!CurveSeq.IsEmpty())
753 Standard_Integer nb_curve = CurveSeq.Length(); //number of curves
754 TColGeom_Array1OfBSplineCurve tab(0,nb_curve-1); //array of the curves
755 TColStd_Array1OfReal tabtolvertex(0,nb_curve-1); //(0,nb_curve-2); //array of the tolerances
761 for (i = 1; i <= nb_curve; i++)
763 if (CurveSeq(i)->IsInstance(STANDARD_TYPE(Geom_TrimmedCurve)))
764 CurveSeq(i) = (*((Handle(Geom_TrimmedCurve)*)&(CurveSeq(i))))->BasisCurve();
766 Handle(Geom_TrimmedCurve) aTrCurve = new Geom_TrimmedCurve(CurveSeq(i), FparSeq(i), LparSeq(i));
767 tab(i-1) = GeomConvert::CurveToBSplineCurve(aTrCurve);
768 tab(i-1)->Transform(LocSeq(i).Location().Transformation());
769 GeomConvert::C0BSplineToC1BSplineCurve(tab(i-1), Precision::Confusion());
770 if (LocSeq(i).Orientation() == TopAbs_REVERSED)
774 //char* name = new char[100];
775 //sprintf(name, "c%d", i);
776 //DrawTrSurf::Set(name, tab(i-1));
779 tabtolvertex(i-2) = TolSeq(i-1);
780 } // end for (i = 1; i <= nb_curve; i++)
781 tabtolvertex(nb_curve-1) = TolSeq(TolSeq.Length());
783 Standard_Boolean closed_flag = Standard_False;
784 Standard_Real closed_tolerance = 0.;
785 if (FirstVertex.IsSame(LastVertex) &&
786 GeomLProp::Continuity(tab(0), tab(nb_curve-1),
787 tab(0)->FirstParameter(),
788 tab(nb_curve-1)->LastParameter(),
789 Standard_False, Standard_False, LinTol, AngTol) >= GeomAbs_G1)
791 closed_flag = Standard_True ;
792 closed_tolerance = BRep_Tool::Tolerance(FirstVertex);
795 Handle(TColGeom_HArray1OfBSplineCurve) concatcurve; //array of the concatenated curves
796 Handle(TColStd_HArray1OfInteger) ArrayOfIndices; //array of the remining Vertex
797 GeomConvert::ConcatC1(tab,
802 closed_tolerance); //C1 concatenation
804 if (concatcurve->Length() > 1)
806 GeomConvert_CompCurveToBSplineCurve Concat(concatcurve->Value(concatcurve->Lower()));
808 for (i = concatcurve->Lower()+1; i <= concatcurve->Upper(); i++)
809 Concat.Add( concatcurve->Value(i), LinTol, Standard_True );
811 concatcurve->SetValue(concatcurve->Lower(), Concat.BSplineCurve());
813 // rnc : prevents the driver from building an edge without C1 continuity
814 if (concatcurve->Value(concatcurve->Lower())->Continuity()==GeomAbs_C0){
815 Standard_ConstructionError::Raise("Construction aborted : The given Wire has sharp bends between some Edges, no valid Edge can be built");
817 ResEdge = BRepLib_MakeEdge(concatcurve->Value(concatcurve->Lower()),
818 FirstVtx_final, LastVtx_final,
819 concatcurve->Value(concatcurve->Lower())->FirstParameter(),
820 concatcurve->Value(concatcurve->Lower())->LastParameter());
824 if (CurveSeq(1)->IsInstance(STANDARD_TYPE(Geom_TrimmedCurve)))
825 CurveSeq(1) = (*((Handle(Geom_TrimmedCurve)*)&(CurveSeq(1))))->BasisCurve();
827 CurveSeq(1)->Transform(LocSeq(1).Location().Transformation());
828 ResEdge = BRepLib_MakeEdge(CurveSeq(1),
829 FirstVtx_final, LastVtx_final,
830 FparSeq(1), LparSeq(1));
839 else if (aType == EDGE_CURVE_LENGTH) {
840 GEOMImpl_IVector aVI (aFunction);
843 Handle(GEOM_Function) aRefCurve = aVI.GetPoint1();
844 if (aRefCurve.IsNull()) Standard_NullObject::Raise("Argument Curve is null");
845 TopoDS_Shape aRefShape1 = aRefCurve->GetValue();
846 if (aRefShape1.ShapeType() != TopAbs_EDGE) {
847 Standard_TypeMismatch::Raise
848 ("Edge On Curve creation aborted : curve shape is not an edge");
850 TopoDS_Edge aRefEdge = TopoDS::Edge(aRefShape1);
851 TopoDS_Vertex V1, V2;
852 TopExp::Vertices(aRefEdge, V1, V2, Standard_True);
855 TopoDS_Vertex aRefVertex;
856 Handle(GEOM_Function) aRefPoint = aVI.GetPoint2();
857 if (aRefPoint.IsNull()) {
861 TopoDS_Shape aRefShape2 = aRefPoint->GetValue();
862 if (aRefShape2.ShapeType() != TopAbs_VERTEX) {
863 Standard_TypeMismatch::Raise
864 ("Edge On Curve creation aborted : start point shape is not a vertex");
866 aRefVertex = TopoDS::Vertex(aRefShape2);
868 gp_Pnt aRefPnt = BRep_Tool::Pnt(aRefVertex);
871 Standard_Real aLength = aVI.GetParameter();
872 //Standard_Real aCurveLength = IntTools::Length(aRefEdge);
873 //if (aLength > aCurveLength) {
874 // Standard_ConstructionError::Raise
875 // ("Edge On Curve creation aborted : given length is greater than edges length");
877 if (fabs(aLength) < Precision::Confusion()) {
878 Standard_ConstructionError::Raise
879 ("Edge On Curve creation aborted : given length is smaller than Precision::Confusion()");
883 Standard_Real UFirst, ULast;
884 Handle(Geom_Curve) EdgeCurve = BRep_Tool::Curve(aRefEdge, UFirst, ULast);
885 Handle(Geom_Curve) ReOrientedCurve = EdgeCurve;
887 Standard_Real dU = ULast - UFirst;
888 Standard_Real par1 = UFirst + 0.1 * dU;
889 Standard_Real par2 = ULast - 0.1 * dU;
891 gp_Pnt P1 = EdgeCurve->Value(par1);
892 gp_Pnt P2 = EdgeCurve->Value(par2);
894 if (aRefPnt.SquareDistance(P2) < aRefPnt.SquareDistance(P1)) {
895 ReOrientedCurve = EdgeCurve->Reversed();
896 UFirst = EdgeCurve->ReversedParameter(ULast);
899 // Get the point by length
900 GeomAdaptor_Curve AdapCurve = GeomAdaptor_Curve(ReOrientedCurve);
901 GCPnts_AbscissaPoint anAbsPnt (AdapCurve, aLength, UFirst);
902 Standard_Real aParam = anAbsPnt.Parameter();
904 if (AdapCurve.IsClosed() && aLength < 0.0) {
905 Standard_Real aTmp = aParam;
910 BRepBuilderAPI_MakeEdge aME (ReOrientedCurve, UFirst, aParam);
912 aShape = aME.Shape();
917 if (aShape.IsNull()) return 0;
919 // Check shape validity
920 BRepCheck_Analyzer ana (aShape, false);
921 if (!ana.IsValid()) {
922 //Standard_ConstructionError::Raise("Algorithm have produced an invalid shape result");
925 aFunction->SetValue(aShape);
927 log.SetTouched(Label());
929 if (!aWarning.IsEmpty())
930 Standard_Failure::Raise(aWarning.ToCString());
936 //=======================================================================
937 //function : GEOMImpl_ShapeDriver_Type_
939 //=======================================================================
940 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ShapeDriver_Type_()
943 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
944 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
945 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
946 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
947 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
948 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
951 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
952 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ShapeDriver",
953 sizeof(GEOMImpl_ShapeDriver),
955 (Standard_Address)_Ancestors,
956 (Standard_Address)NULL);
961 //=======================================================================
962 //function : DownCast
964 //=======================================================================
965 const Handle(GEOMImpl_ShapeDriver) Handle(GEOMImpl_ShapeDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
967 Handle(GEOMImpl_ShapeDriver) _anOtherObject;
969 if (!AnObject.IsNull()) {
970 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ShapeDriver))) {
971 _anOtherObject = Handle(GEOMImpl_ShapeDriver)((Handle(GEOMImpl_ShapeDriver)&)AnObject);
975 return _anOtherObject;
978 //modified by NIZNHY-PKV Wed Dec 28 13:48:31 2011f
979 #include <TopoDS_Iterator.hxx>
980 #include <TopTools_HSequenceOfShape.hxx>
981 #include <ShapeAnalysis_FreeBounds.hxx>
982 #include <TopTools_MapOfShape.hxx>
983 #include <TopTools_MapOfOrientedShape.hxx>
984 #include <BRep_Builder.hxx>
985 #include <TopoDS_Wire.hxx>
987 //=======================================================================
988 //function : KeepEdgesOrder
990 //=======================================================================
992 void KeepEdgesOrder(const Handle(TopTools_HSequenceOfShape)& aEdges,
993 const Handle(TopTools_HSequenceOfShape)& aWires)
995 Standard_Integer aNbWires, aNbEdges;
997 if (aEdges.IsNull()) {
1001 if (aWires.IsNull()) {
1005 aNbEdges=aEdges->Length();
1006 aNbWires=aWires->Length();
1007 if (!aNbEdges || !aNbWires) {
1011 Standard_Boolean bClosed;
1012 Standard_Integer i, j;
1014 TopoDS_Iterator aIt;
1016 TopTools_MapOfOrientedShape aMEx;
1018 for (i=1; i<=aNbWires; ++i) {
1019 const TopoDS_Shape& aWx=aWires->Value(i);
1022 aIt.Initialize (aWx);
1023 for (; aIt.More(); aIt.Next()) {
1024 const TopoDS_Shape& aEx=aIt.Value();
1029 for (j=1; j<=aNbEdges; ++j) {
1030 const TopoDS_Shape& aE=aEdges->Value(j);
1031 if (aMEx.Contains(aE)) {
1036 bClosed=aWx.Closed();
1037 aWy.Closed(bClosed);
1039 aWires->Append(aWy);
1040 }// for (i=1; i<=aNbWires; ++i) {
1042 aWires->Remove(1, aNbWires);
1045 //modified by NIZNHY-PKV Wed Dec 28 13:48:34 2011t