1 // Copyright (C) 2007-2013 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();
138 Standard_Real aTolerance = aCI.GetTolerance();
139 if (aTolerance < Precision::Confusion())
140 aTolerance = Precision::Confusion();
142 aShape = MakeWireFromEdges(aShapes, aTolerance);
144 else if (aType == FACE_WIRE) {
145 Handle(GEOM_Function) aRefBase = aCI.GetBase();
146 TopoDS_Shape aShapeBase = aRefBase->GetValue();
147 if (aShapeBase.IsNull()) Standard_NullObject::Raise("Argument Shape is null");
149 if (aShapeBase.ShapeType() == TopAbs_WIRE) {
150 W = TopoDS::Wire(aShapeBase);
151 // check the wire is closed
152 TopoDS_Vertex aV1, aV2;
153 TopExp::Vertices(W, aV1, aV2);
154 if ( !aV1.IsNull() && !aV2.IsNull() && aV1.IsSame(aV2) )
155 aShapeBase.Closed(true);
157 Standard_NullObject::Raise
158 ("Shape for face construction is not closed");
160 else if (aShapeBase.ShapeType() == TopAbs_EDGE && aShapeBase.Closed()) {
161 BRepBuilderAPI_MakeWire MW;
162 MW.Add(TopoDS::Edge(aShapeBase));
164 Standard_ConstructionError::Raise("Wire construction failed");
169 Standard_NullObject::Raise
170 ("Shape for face construction is neither a wire nor a closed edge");
172 aWarning = GEOMImpl_Block6Explorer::MakeFace(W, aCI.GetIsPlanar(), aShape);
173 if (aShape.IsNull()) {
174 Standard_ConstructionError::Raise("Face construction failed");
177 else if (aType == FACE_WIRES) {
178 // Try to build a face from a set of wires and edges
181 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
182 int nbshapes = aShapes->Length();
184 Standard_ConstructionError::Raise("No wires or edges given");
187 // 1. Extract all edges from the given arguments
188 TopTools_MapOfShape aMapEdges;
189 Handle(TopTools_HSequenceOfShape) aSeqEdgesIn = new TopTools_HSequenceOfShape;
191 for (ind = 1; ind <= nbshapes; ind++) {
192 Handle(GEOM_Function) aRefSh_i = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
193 TopoDS_Shape aSh_i = aRefSh_i->GetValue();
195 TopExp_Explorer anExpE_i (aSh_i, TopAbs_EDGE);
196 for (; anExpE_i.More(); anExpE_i.Next()) {
197 if (aMapEdges.Add(anExpE_i.Current())) {
198 aSeqEdgesIn->Append(anExpE_i.Current());
203 // 2. Connect edges to wires of maximum length
204 Handle(TopTools_HSequenceOfShape) aSeqWiresOut;
205 ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdgesIn, Precision::Confusion(),
206 /*shared*/Standard_False, aSeqWiresOut);
207 //modified by NIZNHY-PKV Wed Dec 28 13:46:55 2011f
208 //KeepEdgesOrder(aSeqEdgesIn, aSeqWiresOut);
209 //modified by NIZNHY-PKV Wed Dec 28 13:46:59 2011t
211 // 3. Separate closed wires
212 Handle(TopTools_HSequenceOfShape) aSeqClosedWires = new TopTools_HSequenceOfShape;
213 Handle(TopTools_HSequenceOfShape) aSeqOpenWires = new TopTools_HSequenceOfShape;
214 for (ind = 1; ind <= aSeqWiresOut->Length(); ind++) {
215 if (aSeqWiresOut->Value(ind).Closed())
216 aSeqClosedWires->Append(aSeqWiresOut->Value(ind));
218 aSeqOpenWires->Append(aSeqWiresOut->Value(ind));
221 if (aSeqClosedWires->Length() < 1) {
222 Standard_ConstructionError::Raise
223 ("There is no closed contour can be built from the given arguments");
226 // 4. Build a face / list of faces from all the obtained closed wires
230 TopoDS_Wire aW1 = TopoDS::Wire(aSeqClosedWires->Value(1));
231 aWarning = GEOMImpl_Block6Explorer::MakeFace(aW1, aCI.GetIsPlanar(), aFFace);
232 if (aFFace.IsNull()) {
233 Standard_ConstructionError::Raise("Face construction failed");
236 // 4.b. Add other wires
237 if (aSeqClosedWires->Length() == 1) {
242 BRep_Builder aBuilder;
243 aBuilder.MakeCompound(C);
244 BRepAlgo_FaceRestrictor FR;
246 TopAbs_Orientation OriF = aFFace.Orientation();
247 TopoDS_Shape aLocalS = aFFace.Oriented(TopAbs_FORWARD);
248 FR.Init(TopoDS::Face(aLocalS), Standard_False, Standard_True);
250 for (ind = 1; ind <= aSeqClosedWires->Length(); ind++) {
251 TopoDS_Wire aW = TopoDS::Wire(aSeqClosedWires->Value(ind));
260 for (; FR.More(); FR.Next()) {
261 aFace = FR.Current().Oriented(OriF);
262 aBuilder.Add(C, aFace);
273 // 5. Add all open wires to the result
274 if (aSeqOpenWires->Length() > 0) {
275 //Standard_ConstructionError::Raise("There are some open wires");
277 BRep_Builder aBuilder;
278 if (aSeqClosedWires->Length() == 1) {
279 aBuilder.MakeCompound(C);
280 aBuilder.Add(C, aShape);
283 C = TopoDS::Compound(aShape);
286 for (ind = 1; ind <= aSeqOpenWires->Length(); ind++) {
287 aBuilder.Add(C, aSeqOpenWires->Value(ind));
293 else if (aType == SHELL_FACES) {
294 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
295 unsigned int ind, nbshapes = aShapes->Length();
298 BRepBuilderAPI_Sewing aSewing (Precision::Confusion()*10.0);
299 for (ind = 1; ind <= nbshapes; ind++) {
300 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
301 TopoDS_Shape aShape_i = aRefShape->GetValue();
302 if (aShape_i.IsNull()) {
303 Standard_NullObject::Raise("Face for shell construction is null");
305 aSewing.Add(aShape_i);
310 TopoDS_Shape sh = aSewing.SewedShape();
312 if (sh.ShapeType()==TopAbs_FACE && nbshapes==1) {
313 // case for creation of shell from one face - PAL12722 (skl 26.06.2006)
320 //TopExp_Explorer exp (aSewing.SewedShape(), TopAbs_SHELL);
321 TopExp_Explorer exp (sh, TopAbs_SHELL);
322 Standard_Integer ish = 0;
323 for (; exp.More(); exp.Next()) {
324 aShape = exp.Current();
329 // try the case of one face (Mantis issue 0021809)
330 TopExp_Explorer expF (sh, TopAbs_FACE);
331 Standard_Integer ifa = 0;
332 for (; expF.More(); expF.Next()) {
333 aShape = expF.Current();
344 aShape = aSewing.SewedShape();
350 else if (aType == SOLID_SHELL) {
351 Handle(GEOM_Function) aRefShell = aCI.GetBase();
352 TopoDS_Shape aShapeShell = aRefShell->GetValue();
353 if (!aShapeShell.IsNull() && aShapeShell.ShapeType() == TopAbs_COMPOUND) {
354 TopoDS_Iterator It (aShapeShell, Standard_True, Standard_True);
355 if (It.More()) aShapeShell = It.Value();
357 if (aShapeShell.IsNull() || aShapeShell.ShapeType() != TopAbs_SHELL) {
358 Standard_NullObject::Raise("Shape for solid construction is null or not a shell");
361 BRepCheck_Shell chkShell(TopoDS::Shell(aShapeShell));
362 if (chkShell.Closed() == BRepCheck_NotClosed) return 0;
366 B.Add(Sol, aShapeShell);
367 BRepClass3d_SolidClassifier SC (Sol);
368 SC.PerformInfinitePoint(Precision::Confusion());
369 if (SC.State() == TopAbs_IN) {
371 B.Add(Sol, aShapeShell.Reversed());
377 else if (aType == SOLID_SHELLS) {
378 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
379 unsigned int ind, nbshapes = aShapes->Length();
380 Standard_Integer ish = 0;
385 for (ind = 1; ind <= nbshapes; ind++) {
386 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
387 TopoDS_Shape aShapeShell = aRefShape->GetValue();
388 if (aShapeShell.IsNull()) {
389 Standard_NullObject::Raise("Shell for solid construction is null");
391 if (aShapeShell.ShapeType() == TopAbs_COMPOUND) {
392 TopoDS_Iterator It (aShapeShell, Standard_True, Standard_True);
393 if (It.More()) aShapeShell = It.Value();
395 if (aShapeShell.ShapeType() == TopAbs_SHELL) {
396 B.Add(Sol, aShapeShell);
400 if (ish == 0) return 0;
401 BRepClass3d_SolidClassifier SC (Sol);
402 SC.PerformInfinitePoint(Precision::Confusion());
403 if (SC.State() == TopAbs_IN)
404 aShape = Sol.Reversed();
408 else if (aType == COMPOUND_SHAPES) {
409 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
410 unsigned int ind, nbshapes = aShapes->Length();
415 for (ind = 1; ind <= nbshapes; ind++) {
416 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
417 TopoDS_Shape aShape_i = aRefShape->GetValue();
418 if (aShape_i.IsNull()) {
419 Standard_NullObject::Raise("Shape for compound construction is null");
428 else if (aType == REVERSE_ORIENTATION) {
429 Handle(GEOM_Function) aRefShape = aCI.GetBase();
430 TopoDS_Shape aShape_i = aRefShape->GetValue();
431 if (aShape_i.IsNull()) {
432 Standard_NullObject::Raise("Shape for reverse is null");
435 BRepBuilderAPI_Copy Copy(aShape_i);
436 if( Copy.IsDone() ) {
437 TopoDS_Shape tds = Copy.Shape();
439 Standard_ConstructionError::Raise("Orientation aborted : Can not reverse the shape");
442 if( tds.Orientation() == TopAbs_FORWARD)
443 tds.Orientation(TopAbs_REVERSED);
445 tds.Orientation(TopAbs_FORWARD);
451 else if (aType == EDGE_WIRE) {
452 Handle(GEOM_Function) aRefBase = aCI.GetBase();
453 TopoDS_Shape aWire = aRefBase->GetValue();
454 Standard_Real LinTol = aCI.GetTolerance();
455 Standard_Real AngTol = aCI.GetAngularTolerance();
456 if (aWire.IsNull()) Standard_NullObject::Raise("Argument Wire is null");
458 aShape = MakeEdgeFromWire(aWire, LinTol, AngTol);
460 else if (aType == EDGE_CURVE_LENGTH) {
461 GEOMImpl_IVector aVI (aFunction);
464 Handle(GEOM_Function) aRefCurve = aVI.GetPoint1();
465 if (aRefCurve.IsNull()) Standard_NullObject::Raise("Argument Curve is null");
466 TopoDS_Shape aRefShape1 = aRefCurve->GetValue();
467 if (aRefShape1.ShapeType() != TopAbs_EDGE) {
468 Standard_TypeMismatch::Raise
469 ("Edge On Curve creation aborted : curve shape is not an edge");
471 TopoDS_Edge aRefEdge = TopoDS::Edge(aRefShape1);
472 TopoDS_Vertex V1, V2;
473 TopExp::Vertices(aRefEdge, V1, V2, Standard_True);
476 TopoDS_Vertex aRefVertex;
477 Handle(GEOM_Function) aRefPoint = aVI.GetPoint2();
478 if (aRefPoint.IsNull()) {
482 TopoDS_Shape aRefShape2 = aRefPoint->GetValue();
483 if (aRefShape2.ShapeType() != TopAbs_VERTEX) {
484 Standard_TypeMismatch::Raise
485 ("Edge On Curve creation aborted : start point shape is not a vertex");
487 aRefVertex = TopoDS::Vertex(aRefShape2);
489 gp_Pnt aRefPnt = BRep_Tool::Pnt(aRefVertex);
492 Standard_Real aLength = aVI.GetParameter();
493 //Standard_Real aCurveLength = IntTools::Length(aRefEdge);
494 //if (aLength > aCurveLength) {
495 // Standard_ConstructionError::Raise
496 // ("Edge On Curve creation aborted : given length is greater than edges length");
498 if (fabs(aLength) < Precision::Confusion()) {
499 Standard_ConstructionError::Raise
500 ("Edge On Curve creation aborted : given length is smaller than Precision::Confusion()");
504 Standard_Real UFirst, ULast;
505 Handle(Geom_Curve) EdgeCurve = BRep_Tool::Curve(aRefEdge, UFirst, ULast);
506 Handle(Geom_Curve) ReOrientedCurve = EdgeCurve;
508 Standard_Real dU = ULast - UFirst;
509 Standard_Real par1 = UFirst + 0.1 * dU;
510 Standard_Real par2 = ULast - 0.1 * dU;
512 gp_Pnt P1 = EdgeCurve->Value(par1);
513 gp_Pnt P2 = EdgeCurve->Value(par2);
515 if (aRefPnt.SquareDistance(P2) < aRefPnt.SquareDistance(P1)) {
516 ReOrientedCurve = EdgeCurve->Reversed();
517 UFirst = EdgeCurve->ReversedParameter(ULast);
520 // Get the point by length
521 GeomAdaptor_Curve AdapCurve = GeomAdaptor_Curve(ReOrientedCurve);
522 GCPnts_AbscissaPoint anAbsPnt (AdapCurve, aLength, UFirst);
523 Standard_Real aParam = anAbsPnt.Parameter();
525 if (AdapCurve.IsClosed() && aLength < 0.0) {
526 Standard_Real aTmp = aParam;
531 BRepBuilderAPI_MakeEdge aME (ReOrientedCurve, UFirst, aParam);
533 aShape = aME.Shape();
538 if (aShape.IsNull()) return 0;
540 // Check shape validity
541 BRepCheck_Analyzer ana (aShape, false);
542 if (!ana.IsValid()) {
543 //Standard_ConstructionError::Raise("Algorithm have produced an invalid shape result");
544 // For Mantis issue 0021772: EDF 2336 GEOM: Non valid face created from two circles
545 Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape (aShape);
547 aShape = aSfs->Shape();
550 aFunction->SetValue(aShape);
552 log.SetTouched(Label());
554 if (!aWarning.IsEmpty())
555 Standard_Failure::Raise(aWarning.ToCString());
560 TopoDS_Wire GEOMImpl_ShapeDriver::MakeWireFromEdges(const Handle(TColStd_HSequenceOfTransient)& theEdgesFuncs,
561 const Standard_Real theTolerance)
569 for (unsigned int ind = 1; ind <= theEdgesFuncs->Length(); ind++) {
570 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(theEdgesFuncs->Value(ind));
571 TopoDS_Shape aShape_i = aRefShape->GetValue();
572 if (aShape_i.IsNull()) {
573 Standard_NullObject::Raise("Shape for wire construction is null");
575 if (aShape_i.ShapeType() == TopAbs_EDGE || aShape_i.ShapeType() == TopAbs_WIRE) {
576 TopExp_Explorer exp (aShape_i, TopAbs_EDGE);
577 for (; exp.More(); exp.Next())
578 B.Add(aWire, TopoDS::Edge(exp.Current()));
580 Standard_TypeMismatch::Raise
581 ("Shape for wire construction is neither an edge nor a wire");
586 Handle(ShapeFix_Wire) aFW = new ShapeFix_Wire;
590 if (aFW->StatusReorder(ShapeExtend_FAIL1)) {
591 Standard_ConstructionError::Raise("Wire construction failed: several loops detected");
593 else if (aFW->StatusReorder(ShapeExtend_FAIL)) {
594 Standard_ConstructionError::Raise("Wire construction failed");
599 // IMP 0019766: Building a Wire from unconnected edges by introducing a tolerance
600 aFW->ClosedWireMode() = Standard_False;
601 aFW->FixConnected(theTolerance);
602 if (aFW->StatusConnected(ShapeExtend_FAIL)) {
603 Standard_ConstructionError::Raise("Wire construction failed: cannot build connected wire");
606 if (aFW->StatusConnected(ShapeExtend_DONE3)) {
607 // Confused with <prec> but not Analyzer.Precision(), set the same
608 aFW->FixGapsByRangesMode() = Standard_True;
609 if (aFW->FixGaps3d()) {
610 Handle(ShapeExtend_WireData) sbwd = aFW->WireData();
611 Handle(ShapeFix_Edge) aFe = new ShapeFix_Edge;
612 for (Standard_Integer iedge = 1; iedge <= sbwd->NbEdges(); iedge++) {
613 TopoDS_Edge aEdge = TopoDS::Edge(sbwd->Edge(iedge));
614 aFe->FixVertexTolerance(aEdge);
615 aFe->FixSameParameter(aEdge);
618 else if (aFW->StatusGaps3d(ShapeExtend_FAIL)) {
619 Standard_ConstructionError::Raise("Wire construction failed: cannot fix 3d gaps");
622 aWire = aFW->WireAPIMake();
627 TopoDS_Edge GEOMImpl_ShapeDriver::MakeEdgeFromWire(const TopoDS_Shape& aWire,
628 const Standard_Real LinTol,
629 const Standard_Real AngTol)
633 BRepLib::BuildCurves3d(aWire);
634 Handle(ShapeFix_Shape) Fixer = new ShapeFix_Shape(aWire);
635 Fixer->SetPrecision(LinTol);
636 Fixer->SetMaxTolerance(LinTol);
638 TopoDS_Wire theWire = TopoDS::Wire(Fixer->Shape());
640 TColGeom_SequenceOfCurve CurveSeq;
641 TopTools_SequenceOfShape LocSeq;
642 TColStd_SequenceOfReal FparSeq;
643 TColStd_SequenceOfReal LparSeq;
644 TColStd_SequenceOfReal TolSeq;
645 GeomAbs_CurveType CurType;
646 TopoDS_Vertex FirstVertex, LastVertex;
647 Standard_Boolean FinalReverse = Standard_False;
649 BRepTools_WireExplorer wexp(theWire) ;
650 for (; wexp.More(); wexp.Next())
652 TopoDS_Edge anEdge = wexp.Current();
653 Standard_Real fpar, lpar;
654 TopLoc_Location aLoc;
655 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aLoc, fpar, lpar);
659 BRepAdaptor_Curve BAcurve(anEdge);
660 GeomAbs_CurveType aType = BAcurve.GetType();
662 Handle(Geom_Curve) aBasisCurve = BAcurve.Curve().Curve();
664 if (aBasisCurve->IsPeriodic())
665 ElCLib::AdjustPeriodic(aBasisCurve->FirstParameter(), aBasisCurve->LastParameter(),
666 Precision::PConfusion(), fpar, lpar);
668 if (CurveSeq.IsEmpty())
670 CurveSeq.Append(aCurve);
671 TopoDS_Shape aLocShape;
672 aLocShape.Location(aLoc);
673 aLocShape.Orientation(wexp.Orientation());
674 LocSeq.Append(aLocShape);
675 FparSeq.Append(fpar);
676 LparSeq.Append(lpar);
678 FirstVertex = wexp.CurrentVertex();
679 if (anEdge.Orientation() == TopAbs_REVERSED)
680 FinalReverse = Standard_True;
684 Standard_Boolean Done = Standard_False;
685 Standard_Real NewFpar, NewLpar;
686 GeomAdaptor_Curve GAprevcurve(CurveSeq.Last());
687 TopoDS_Vertex CurVertex = wexp.CurrentVertex();
688 TopoDS_Vertex CurFirstVer = TopExp::FirstVertex(anEdge);
689 TopAbs_Orientation ConnectByOrigin = (CurVertex.IsSame(CurFirstVer))? TopAbs_FORWARD : TopAbs_REVERSED;
690 if (aCurve == CurveSeq.Last())
694 if (aBasisCurve->IsPeriodic())
696 if (NewLpar < NewFpar)
697 NewLpar += aBasisCurve->Period();
698 if (ConnectByOrigin == TopAbs_FORWARD)
699 ElCLib::AdjustPeriodic(FparSeq.Last(),
700 FparSeq.Last() + aBasisCurve->Period(),
701 Precision::PConfusion(), NewFpar, NewLpar);
703 ElCLib::AdjustPeriodic(FparSeq.Last() - aBasisCurve->Period(),
705 Precision::PConfusion(), NewFpar, NewLpar);
707 Done = Standard_True;
709 else if (aType == CurType &&
710 aType != GeomAbs_BezierCurve &&
711 aType != GeomAbs_BSplineCurve &&
712 aType != GeomAbs_OtherCurve)
718 gp_Lin aLine = BAcurve.Line();
719 gp_Lin PrevLine = GAprevcurve.Line();
720 if (aLine.Contains(PrevLine.Location(), LinTol) &&
721 aLine.Direction().IsParallel(PrevLine.Direction(), AngTol))
723 gp_Pnt P1 = ElCLib::Value(fpar, aLine);
724 gp_Pnt P2 = ElCLib::Value(lpar, aLine);
725 NewFpar = ElCLib::Parameter(PrevLine, P1);
726 NewLpar = ElCLib::Parameter(PrevLine, P2);
727 if (NewLpar < NewFpar)
729 Standard_Real MemNewFpar = NewFpar;
731 NewLpar = MemNewFpar;
732 ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
734 Done = Standard_True;
740 gp_Circ aCircle = BAcurve.Circle();
741 gp_Circ PrevCircle = GAprevcurve.Circle();
742 if (aCircle.Location().Distance(PrevCircle.Location()) <= LinTol &&
743 Abs(aCircle.Radius() - PrevCircle.Radius()) <= LinTol &&
744 aCircle.Axis().IsParallel(PrevCircle.Axis(), AngTol))
746 if (aCircle.Axis().Direction() * PrevCircle.Axis().Direction() < 0.)
748 Standard_Real memfpar = fpar;
751 ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
753 gp_Pnt P1 = ElCLib::Value(fpar, aCircle);
754 gp_Pnt P2 = ElCLib::Value(lpar, aCircle);
755 NewFpar = ElCLib::Parameter(PrevCircle, P1);
756 NewLpar = ElCLib::Parameter(PrevCircle, P2);
757 if (NewLpar < NewFpar)
759 //Standard_Real MemNewFpar = NewFpar, MemNewLpar = NewLpar;
760 if (ConnectByOrigin == TopAbs_FORWARD)
761 ElCLib::AdjustPeriodic(FparSeq.Last(),
762 FparSeq.Last() + 2.*M_PI,
763 Precision::PConfusion(), NewFpar, NewLpar);
765 ElCLib::AdjustPeriodic(FparSeq.Last() - 2.*M_PI,
767 Precision::PConfusion(), NewFpar, NewLpar);
768 Done = Standard_True;
772 case GeomAbs_Ellipse:
774 gp_Elips anEllipse = BAcurve.Ellipse();
775 gp_Elips PrevEllipse = GAprevcurve.Ellipse();
776 if (anEllipse.Focus1().Distance(PrevEllipse.Focus1()) <= LinTol &&
777 anEllipse.Focus2().Distance(PrevEllipse.Focus2()) <= LinTol &&
778 Abs(anEllipse.MajorRadius() - PrevEllipse.MajorRadius()) <= LinTol &&
779 Abs(anEllipse.MinorRadius() - PrevEllipse.MinorRadius()) <= LinTol &&
780 anEllipse.Axis().IsParallel(PrevEllipse.Axis(), AngTol))
782 if (anEllipse.Axis().Direction() * PrevEllipse.Axis().Direction() < 0.)
784 Standard_Real memfpar = fpar;
787 ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
789 gp_Pnt P1 = ElCLib::Value(fpar, anEllipse);
790 gp_Pnt P2 = ElCLib::Value(lpar, anEllipse);
791 NewFpar = ElCLib::Parameter(PrevEllipse, P1);
792 NewLpar = ElCLib::Parameter(PrevEllipse, P2);
793 if (NewLpar < NewFpar)
795 if (ConnectByOrigin == TopAbs_FORWARD)
796 ElCLib::AdjustPeriodic(FparSeq.Last(),
797 FparSeq.Last() + 2.*M_PI,
798 Precision::PConfusion(), NewFpar, NewLpar);
800 ElCLib::AdjustPeriodic(FparSeq.Last() - 2.*M_PI,
802 Precision::PConfusion(), NewFpar, NewLpar);
803 Done = Standard_True;
807 case GeomAbs_Hyperbola:
809 gp_Hypr aHypr = BAcurve.Hyperbola();
810 gp_Hypr PrevHypr = GAprevcurve.Hyperbola();
811 if (aHypr.Focus1().Distance(PrevHypr.Focus1()) <= LinTol &&
812 aHypr.Focus2().Distance(PrevHypr.Focus2()) <= LinTol &&
813 Abs(aHypr.MajorRadius() - PrevHypr.MajorRadius()) <= LinTol &&
814 Abs(aHypr.MinorRadius() - PrevHypr.MinorRadius()) <= LinTol &&
815 aHypr.Axis().IsParallel(PrevHypr.Axis(), AngTol))
817 gp_Pnt P1 = ElCLib::Value(fpar, aHypr);
818 gp_Pnt P2 = ElCLib::Value(lpar, aHypr);
819 NewFpar = ElCLib::Parameter(PrevHypr, P1);
820 NewLpar = ElCLib::Parameter(PrevHypr, P2);
821 if (NewLpar < NewFpar)
823 Standard_Real MemNewFpar = NewFpar;
825 NewLpar = MemNewFpar;
826 ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
828 Done = Standard_True;
832 case GeomAbs_Parabola:
834 gp_Parab aParab = BAcurve.Parabola();
835 gp_Parab PrevParab = GAprevcurve.Parabola();
836 if (aParab.Location().Distance(PrevParab.Location()) <= LinTol &&
837 aParab.Focus().Distance(PrevParab.Focus()) <= LinTol &&
838 Abs(aParab.Focal() - PrevParab.Focal()) <= LinTol &&
839 aParab.Axis().IsParallel(PrevParab.Axis(), AngTol))
841 gp_Pnt P1 = ElCLib::Value(fpar, aParab);
842 gp_Pnt P2 = ElCLib::Value(lpar, aParab);
843 NewFpar = ElCLib::Parameter(PrevParab, P1);
844 NewLpar = ElCLib::Parameter(PrevParab, P2);
845 if (NewLpar < NewFpar)
847 Standard_Real MemNewFpar = NewFpar;
849 NewLpar = MemNewFpar;
850 ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
852 Done = Standard_True;
856 } //end of switch (aType)
857 } // end of else if (aType == CurType && ...
860 if (NewFpar < FparSeq.Last())
861 FparSeq(FparSeq.Length()) = NewFpar;
863 LparSeq(LparSeq.Length()) = NewLpar;
867 CurveSeq.Append(aCurve);
868 TopoDS_Shape aLocShape;
869 aLocShape.Location(aLoc);
870 aLocShape.Orientation(wexp.Orientation());
871 LocSeq.Append(aLocShape);
872 FparSeq.Append(fpar);
873 LparSeq.Append(lpar);
874 TolSeq.Append(BRep_Tool::Tolerance(CurVertex));
877 } // end of else (CurveSeq.IsEmpty()) -> not first time
878 } // end for (; wexp.More(); wexp.Next())
880 LastVertex = wexp.CurrentVertex();
881 TolSeq.Append(BRep_Tool::Tolerance(LastVertex));
883 TopoDS_Vertex FirstVtx_final = (FinalReverse)? LastVertex : FirstVertex;
884 FirstVtx_final.Orientation(TopAbs_FORWARD);
885 TopoDS_Vertex LastVtx_final = (FinalReverse)? FirstVertex : LastVertex;
886 LastVtx_final.Orientation(TopAbs_REVERSED);
888 if (!CurveSeq.IsEmpty())
890 Standard_Integer nb_curve = CurveSeq.Length(); //number of curves
891 TColGeom_Array1OfBSplineCurve tab(0,nb_curve-1); //array of the curves
892 TColStd_Array1OfReal tabtolvertex(0,nb_curve-1); //(0,nb_curve-2); //array of the tolerances
898 for (i = 1; i <= nb_curve; i++)
900 if (CurveSeq(i)->IsInstance(STANDARD_TYPE(Geom_TrimmedCurve)))
901 CurveSeq(i) = (*((Handle(Geom_TrimmedCurve)*)&(CurveSeq(i))))->BasisCurve();
903 Handle(Geom_TrimmedCurve) aTrCurve = new Geom_TrimmedCurve(CurveSeq(i), FparSeq(i), LparSeq(i));
904 tab(i-1) = GeomConvert::CurveToBSplineCurve(aTrCurve);
905 tab(i-1)->Transform(LocSeq(i).Location().Transformation());
906 GeomConvert::C0BSplineToC1BSplineCurve(tab(i-1), Precision::Confusion());
907 if (LocSeq(i).Orientation() == TopAbs_REVERSED)
911 //char* name = new char[100];
912 //sprintf(name, "c%d", i);
913 //DrawTrSurf::Set(name, tab(i-1));
916 tabtolvertex(i-2) = TolSeq(i-1);
917 } // end for (i = 1; i <= nb_curve; i++)
918 tabtolvertex(nb_curve-1) = TolSeq(TolSeq.Length());
920 Standard_Boolean closed_flag = Standard_False;
921 Standard_Real closed_tolerance = 0.;
922 if (FirstVertex.IsSame(LastVertex) &&
923 GeomLProp::Continuity(tab(0), tab(nb_curve-1),
924 tab(0)->FirstParameter(),
925 tab(nb_curve-1)->LastParameter(),
926 Standard_False, Standard_False, LinTol, AngTol) >= GeomAbs_G1)
928 closed_flag = Standard_True ;
929 closed_tolerance = BRep_Tool::Tolerance(FirstVertex);
932 Handle(TColGeom_HArray1OfBSplineCurve) concatcurve; //array of the concatenated curves
933 Handle(TColStd_HArray1OfInteger) ArrayOfIndices; //array of the remining Vertex
934 GeomConvert::ConcatC1(tab,
939 closed_tolerance); //C1 concatenation
941 if (concatcurve->Length() > 1)
943 GeomConvert_CompCurveToBSplineCurve Concat(concatcurve->Value(concatcurve->Lower()));
945 for (i = concatcurve->Lower()+1; i <= concatcurve->Upper(); i++)
946 Concat.Add( concatcurve->Value(i), LinTol, Standard_True );
948 concatcurve->SetValue(concatcurve->Lower(), Concat.BSplineCurve());
950 // rnc : prevents the driver from building an edge without C1 continuity
951 if (concatcurve->Value(concatcurve->Lower())->Continuity()==GeomAbs_C0){
952 Standard_ConstructionError::Raise("Construction aborted : The given Wire has sharp bends between some Edges, no valid Edge can be built");
954 ResEdge = BRepLib_MakeEdge(concatcurve->Value(concatcurve->Lower()),
955 FirstVtx_final, LastVtx_final,
956 concatcurve->Value(concatcurve->Lower())->FirstParameter(),
957 concatcurve->Value(concatcurve->Lower())->LastParameter());
961 if (CurveSeq(1)->IsInstance(STANDARD_TYPE(Geom_TrimmedCurve)))
962 CurveSeq(1) = (*((Handle(Geom_TrimmedCurve)*)&(CurveSeq(1))))->BasisCurve();
964 CurveSeq(1)->Transform(LocSeq(1).Location().Transformation());
965 ResEdge = BRepLib_MakeEdge(CurveSeq(1),
966 FirstVtx_final, LastVtx_final,
967 FparSeq(1), LparSeq(1));
977 //================================================================================
979 * \brief Returns a name of creation operation and names and values of creation parameters
981 //================================================================================
983 bool GEOMImpl_ShapeDriver::
984 GetCreationInformation(std::string& theOperationName,
985 std::vector<GEOM_Param>& theParams)
987 if (Label().IsNull()) return 0;
988 Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
990 GEOMImpl_IShapes aCI( function );
991 Standard_Integer aType = function->GetType();
995 theOperationName = "WIRE";
996 AddParam( theParams, "Wires/edges", aCI.GetShapes() );
997 AddParam( theParams, "Tolerance", aCI.GetTolerance() );
1000 theOperationName = "FACE";
1001 AddParam( theParams, "Wire/edge", aCI.GetBase() );
1002 AddParam( theParams, "Is planar wanted", aCI.GetIsPlanar() );
1005 theOperationName = "FACE";
1006 AddParam( theParams, "Wires/edges", aCI.GetShapes() );
1007 AddParam( theParams, "Is planar wanted", aCI.GetIsPlanar() );
1010 theOperationName = "SHELL";
1011 AddParam( theParams, "Objects", aCI.GetShapes() );
1015 theOperationName = "SOLID";
1016 AddParam( theParams, "Objects", aCI.GetShapes() );
1018 case COMPOUND_SHAPES:
1019 theOperationName = "COMPOUND";
1020 AddParam( theParams, "Objects", aCI.GetShapes() );
1023 theOperationName = "EDGE";
1024 AddParam( theParams, "Wire", aCI.GetBase() );
1025 AddParam( theParams, "Linear Tolerance", aCI.GetTolerance() );
1026 AddParam( theParams, "Angular Tolerance", aCI.GetAngularTolerance() );
1028 case EDGE_CURVE_LENGTH:
1029 theOperationName = "EDGE";
1031 GEOMImpl_IVector aCI( function );
1032 AddParam( theParams, "Edge", aCI.GetPoint1() );
1033 AddParam( theParams, "Start point", aCI.GetPoint2() );
1034 AddParam( theParams, "Length", aCI.GetParameter() );
1037 case SHAPES_ON_SHAPE:
1039 theOperationName = "GetShapesOnShapeAsCompound";
1040 Handle(TColStd_HSequenceOfTransient) shapes = aCI.GetShapes();
1041 if ( !shapes.IsNull() && shapes->Length() > 0 )
1042 AddParam( theParams, "Check shape", shapes->Value(1) );
1043 if ( !shapes.IsNull() && shapes->Length() > 1 )
1044 AddParam( theParams, "Shape", shapes->Value(2) );
1045 AddParam( theParams, "Shape type", TopAbs_ShapeEnum( aCI.GetSubShapeType() ));
1046 AddParam( theParams, "State", TopAbs_State((int) aCI.GetTolerance() ));
1056 IMPLEMENT_STANDARD_HANDLE (GEOMImpl_ShapeDriver,GEOM_BaseDriver);
1057 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_ShapeDriver,GEOM_BaseDriver);
1059 //modified by NIZNHY-PKV Wed Dec 28 13:48:31 2011f
1060 #include <TopoDS_Iterator.hxx>
1061 #include <TopTools_HSequenceOfShape.hxx>
1062 #include <ShapeAnalysis_FreeBounds.hxx>
1063 #include <TopTools_MapOfShape.hxx>
1064 #include <TopTools_MapOfOrientedShape.hxx>
1065 #include <BRep_Builder.hxx>
1066 #include <TopoDS_Wire.hxx>
1068 //=======================================================================
1069 //function : KeepEdgesOrder
1071 //=======================================================================
1073 void KeepEdgesOrder(const Handle(TopTools_HSequenceOfShape)& aEdges,
1074 const Handle(TopTools_HSequenceOfShape)& aWires)
1076 Standard_Integer aNbWires, aNbEdges;
1078 if (aEdges.IsNull()) {
1082 if (aWires.IsNull()) {
1086 aNbEdges=aEdges->Length();
1087 aNbWires=aWires->Length();
1088 if (!aNbEdges || !aNbWires) {
1092 Standard_Boolean bClosed;
1093 Standard_Integer i, j;
1095 TopoDS_Iterator aIt;
1097 TopTools_MapOfOrientedShape aMEx;
1099 for (i=1; i<=aNbWires; ++i) {
1100 const TopoDS_Shape& aWx=aWires->Value(i);
1103 aIt.Initialize (aWx);
1104 for (; aIt.More(); aIt.Next()) {
1105 const TopoDS_Shape& aEx=aIt.Value();
1110 for (j=1; j<=aNbEdges; ++j) {
1111 const TopoDS_Shape& aE=aEdges->Value(j);
1112 if (aMEx.Contains(aE)) {
1117 bClosed=aWx.Closed();
1118 aWy.Closed(bClosed);
1120 aWires->Append(aWy);
1121 }// for (i=1; i<=aNbWires; ++i) {
1123 aWires->Remove(1, aNbWires);
1126 //modified by NIZNHY-PKV Wed Dec 28 13:48:34 2011t