1 // Copyright (C) 2007-2014 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, or (at your option) any later version.
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_IIsoline.hxx>
26 #include <GEOMImpl_IShapes.hxx>
27 #include <GEOMImpl_IVector.hxx>
28 #include <GEOMImpl_Types.hxx>
29 #include <GEOMImpl_Block6Explorer.hxx>
31 #include <GEOM_Function.hxx>
32 #include <GEOMUtils_Hatcher.hxx>
35 #include <ShapeFix_Wire.hxx>
36 #include <ShapeFix_Edge.hxx>
37 #include <ShapeFix_Shape.hxx>
39 #include <BRep_Builder.hxx>
40 #include <BRep_Tool.hxx>
41 #include <BRepAdaptor_Curve.hxx>
42 #include <BRepAlgo_FaceRestrictor.hxx>
43 #include <BRepBuilderAPI_Copy.hxx>
44 #include <BRepBuilderAPI_Sewing.hxx>
45 #include <BRepBuilderAPI_MakeWire.hxx>
46 #include <BRepBuilderAPI_MakeEdge.hxx>
47 #include <BRepBuilderAPI_MakeSolid.hxx>
48 #include <BRepCheck.hxx>
49 #include <BRepCheck_Analyzer.hxx>
50 #include <BRepCheck_Shell.hxx>
51 #include <BRepClass3d_SolidClassifier.hxx>
52 #include <BRepLib.hxx>
53 #include <BRepLib_MakeEdge.hxx>
54 #include <BRepTools_WireExplorer.hxx>
56 #include <ShapeAnalysis.hxx>
57 #include <ShapeAnalysis_FreeBounds.hxx>
61 #include <TopExp_Explorer.hxx>
63 #include <TopoDS_Shape.hxx>
64 #include <TopoDS_Edge.hxx>
65 #include <TopoDS_Wire.hxx>
66 #include <TopoDS_Shell.hxx>
67 #include <TopoDS_Solid.hxx>
68 #include <TopoDS_Compound.hxx>
69 #include <TopoDS_Iterator.hxx>
71 #include <TopTools_MapOfShape.hxx>
72 #include <TopTools_HSequenceOfShape.hxx>
76 #include <GCPnts_AbscissaPoint.hxx>
78 #include <Geom_TrimmedCurve.hxx>
79 #include <Geom_Surface.hxx>
80 #include <GeomAbs_CurveType.hxx>
81 #include <GeomConvert_CompCurveToBSplineCurve.hxx>
82 #include <GeomConvert.hxx>
83 #include <GeomLProp.hxx>
85 #include <TColStd_SequenceOfReal.hxx>
86 #include <TColStd_HSequenceOfTransient.hxx>
87 #include <TColStd_Array1OfReal.hxx>
88 #include <TColGeom_SequenceOfCurve.hxx>
89 #include <TColGeom_Array1OfBSplineCurve.hxx>
90 #include <TColGeom_HArray1OfBSplineCurve.hxx>
92 #include <Precision.hxx>
94 #include <Standard_NullObject.hxx>
95 #include <Standard_TypeMismatch.hxx>
96 #include <Standard_ConstructionError.hxx>
98 //modified by NIZNHY-PKV Wed Dec 28 13:48:20 2011f
100 // void KeepEdgesOrder(const Handle(TopTools_HSequenceOfShape)& aEdges,
101 // const Handle(TopTools_HSequenceOfShape)& aWires);
102 //modified by NIZNHY-PKV Wed Dec 28 13:48:23 2011t
104 //=======================================================================
107 //=======================================================================
108 const Standard_GUID& GEOMImpl_ShapeDriver::GetID()
110 static Standard_GUID aShapeDriver("FF1BBB54-5D14-4df2-980B-3A668264EA16");
115 //=======================================================================
116 //function : GEOMImpl_ShapeDriver
118 //=======================================================================
119 GEOMImpl_ShapeDriver::GEOMImpl_ShapeDriver()
123 //=======================================================================
126 //=======================================================================
127 Standard_Integer GEOMImpl_ShapeDriver::Execute(TFunction_Logbook& log) const
129 if (Label().IsNull()) return 0;
130 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
132 GEOMImpl_IShapes aCI (aFunction);
133 Standard_Integer aType = aFunction->GetType();
136 TCollection_AsciiString aWarning;
137 TopAbs_ShapeEnum anExpectedType = TopAbs_SHAPE;
141 if (aType == WIRE_EDGES) {
142 anExpectedType = TopAbs_WIRE;
144 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
146 Standard_Real aTolerance = aCI.GetTolerance();
147 if (aTolerance < Precision::Confusion())
148 aTolerance = Precision::Confusion();
150 aShape = MakeWireFromEdges(aShapes, aTolerance);
152 else if (aType == FACE_WIRE) {
153 anExpectedType = TopAbs_FACE;
155 Handle(GEOM_Function) aRefBase = aCI.GetBase();
156 TopoDS_Shape aShapeBase = aRefBase->GetValue();
157 if (aShapeBase.IsNull()) Standard_NullObject::Raise("Argument Shape is null");
159 if (aShapeBase.ShapeType() == TopAbs_WIRE) {
160 W = TopoDS::Wire(aShapeBase);
161 // check the wire is closed
162 TopoDS_Vertex aV1, aV2;
163 TopExp::Vertices(W, aV1, aV2);
164 if ( !aV1.IsNull() && !aV2.IsNull() && aV1.IsSame(aV2) )
165 aShapeBase.Closed(true);
167 Standard_NullObject::Raise
168 ("Shape for face construction is not closed");
170 else if (aShapeBase.ShapeType() == TopAbs_EDGE && aShapeBase.Closed()) {
171 BRepBuilderAPI_MakeWire MW;
172 MW.Add(TopoDS::Edge(aShapeBase));
174 Standard_ConstructionError::Raise("Wire construction failed");
179 Standard_NullObject::Raise
180 ("Shape for face construction is neither a wire nor a closed edge");
182 aWarning = GEOMImpl_Block6Explorer::MakeFace(W, aCI.GetIsPlanar(), aShape);
183 if (aShape.IsNull()) {
184 Standard_ConstructionError::Raise("Face construction failed");
187 else if (aType == FACE_WIRES) {
188 anExpectedType = TopAbs_FACE;
190 // Try to build a face from a set of wires and edges
193 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
194 int nbshapes = aShapes->Length();
196 Standard_ConstructionError::Raise("No wires or edges given");
199 // 1. Extract all edges from the given arguments
200 TopTools_MapOfShape aMapEdges;
201 Handle(TopTools_HSequenceOfShape) aSeqEdgesIn = new TopTools_HSequenceOfShape;
203 for (ind = 1; ind <= nbshapes; ind++) {
204 Handle(GEOM_Function) aRefSh_i = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
205 TopoDS_Shape aSh_i = aRefSh_i->GetValue();
207 TopExp_Explorer anExpE_i (aSh_i, TopAbs_EDGE);
208 for (; anExpE_i.More(); anExpE_i.Next()) {
209 if (aMapEdges.Add(anExpE_i.Current())) {
210 aSeqEdgesIn->Append(anExpE_i.Current());
215 // 2. Connect edges to wires of maximum length
216 Handle(TopTools_HSequenceOfShape) aSeqWiresOut;
217 ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdgesIn, Precision::Confusion(),
218 /*shared*/Standard_False, aSeqWiresOut);
219 //modified by NIZNHY-PKV Wed Dec 28 13:46:55 2011f
220 //KeepEdgesOrder(aSeqEdgesIn, aSeqWiresOut);
221 //modified by NIZNHY-PKV Wed Dec 28 13:46:59 2011t
223 // 3. Separate closed wires
224 Handle(TopTools_HSequenceOfShape) aSeqClosedWires = new TopTools_HSequenceOfShape;
225 Handle(TopTools_HSequenceOfShape) aSeqOpenWires = new TopTools_HSequenceOfShape;
226 for (ind = 1; ind <= aSeqWiresOut->Length(); ind++) {
227 if (aSeqWiresOut->Value(ind).Closed())
228 aSeqClosedWires->Append(aSeqWiresOut->Value(ind));
230 aSeqOpenWires->Append(aSeqWiresOut->Value(ind));
233 if (aSeqClosedWires->Length() < 1) {
234 Standard_ConstructionError::Raise
235 ("There is no closed contour can be built from the given arguments");
238 // 4. Build a face / list of faces from all the obtained closed wires
242 TopoDS_Wire aW1 = TopoDS::Wire(aSeqClosedWires->Value(1));
243 aWarning = GEOMImpl_Block6Explorer::MakeFace(aW1, aCI.GetIsPlanar(), aFFace);
244 if (aFFace.IsNull()) {
245 Standard_ConstructionError::Raise("Face construction failed");
248 // 4.b. Add other wires
249 if (aSeqClosedWires->Length() == 1) {
254 BRep_Builder aBuilder;
255 aBuilder.MakeCompound(C);
256 BRepAlgo_FaceRestrictor FR;
258 TopAbs_Orientation OriF = aFFace.Orientation();
259 TopoDS_Shape aLocalS = aFFace.Oriented(TopAbs_FORWARD);
260 FR.Init(TopoDS::Face(aLocalS), Standard_False, Standard_True);
262 for (ind = 1; ind <= aSeqClosedWires->Length(); ind++) {
263 TopoDS_Wire aW = TopoDS::Wire(aSeqClosedWires->Value(ind));
272 for (; FR.More(); FR.Next()) {
273 aFace = FR.Current().Oriented(OriF);
274 aBuilder.Add(C, aFace);
285 // 5. Add all open wires to the result
286 if (aSeqOpenWires->Length() > 0) {
287 //Standard_ConstructionError::Raise("There are some open wires");
289 BRep_Builder aBuilder;
290 if (aSeqClosedWires->Length() == 1) {
291 aBuilder.MakeCompound(C);
292 aBuilder.Add(C, aShape);
295 C = TopoDS::Compound(aShape);
298 for (ind = 1; ind <= aSeqOpenWires->Length(); ind++) {
299 aBuilder.Add(C, aSeqOpenWires->Value(ind));
305 else if (aType == SHELL_FACES) {
306 anExpectedType = TopAbs_SHELL;
308 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
309 unsigned int ind, nbshapes = aShapes->Length();
312 BRepBuilderAPI_Sewing aSewing (Precision::Confusion()*10.0);
313 for (ind = 1; ind <= nbshapes; ind++) {
314 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
315 TopoDS_Shape aShape_i = aRefShape->GetValue();
316 if (aShape_i.IsNull()) {
317 Standard_NullObject::Raise("Face for shell construction is null");
319 aSewing.Add(aShape_i);
324 TopoDS_Shape sh = aSewing.SewedShape();
326 if (sh.ShapeType()==TopAbs_FACE && nbshapes==1) {
327 // case for creation of shell from one face - PAL12722 (skl 26.06.2006)
334 //TopExp_Explorer exp (aSewing.SewedShape(), TopAbs_SHELL);
335 TopExp_Explorer exp (sh, TopAbs_SHELL);
336 Standard_Integer ish = 0;
337 for (; exp.More(); exp.Next()) {
338 aShape = exp.Current();
343 // try the case of one face (Mantis issue 0021809)
344 TopExp_Explorer expF (sh, TopAbs_FACE);
345 Standard_Integer ifa = 0;
346 for (; expF.More(); expF.Next()) {
347 aShape = expF.Current();
358 aShape = aSewing.SewedShape();
364 else if (aType == SOLID_SHELLS) {
365 anExpectedType = TopAbs_SOLID;
367 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
368 unsigned int ind, nbshapes = aShapes->Length();
369 Standard_Integer ish = 0;
370 BRepBuilderAPI_MakeSolid aMkSolid;
373 for (ind = 1; ind <= nbshapes; ind++) {
374 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
375 TopoDS_Shape aShapeShell = aRefShape->GetValue();
376 if (aShapeShell.IsNull()) {
377 Standard_NullObject::Raise("Shell for solid construction is null");
379 if (aShapeShell.ShapeType() == TopAbs_COMPOUND) {
380 TopoDS_Iterator It (aShapeShell, Standard_True, Standard_True);
381 if (It.More()) aShapeShell = It.Value();
383 if (aShapeShell.ShapeType() == TopAbs_SHELL) {
384 aMkSolid.Add(TopoDS::Shell(aShapeShell));
388 if (ish == 0 || !aMkSolid.IsDone()) return 0;
390 TopoDS_Solid Sol = aMkSolid.Solid();
391 BRepClass3d_SolidClassifier SC (Sol);
392 SC.PerformInfinitePoint(Precision::Confusion());
393 if (SC.State() == TopAbs_IN)
394 aShape = Sol.Reversed();
398 else if (aType == COMPOUND_SHAPES) {
399 anExpectedType = TopAbs_COMPOUND;
401 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
402 unsigned int ind, nbshapes = aShapes->Length();
407 for (ind = 1; ind <= nbshapes; ind++) {
408 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
409 TopoDS_Shape aShape_i = aRefShape->GetValue();
410 if (aShape_i.IsNull()) {
411 Standard_NullObject::Raise("Shape for compound construction is null");
419 else if (aType == EDGE_WIRE) {
420 anExpectedType = TopAbs_EDGE;
422 Handle(GEOM_Function) aRefBase = aCI.GetBase();
423 TopoDS_Shape aWire = aRefBase->GetValue();
424 Standard_Real LinTol = aCI.GetTolerance();
425 Standard_Real AngTol = aCI.GetAngularTolerance();
426 if (aWire.IsNull()) Standard_NullObject::Raise("Argument Wire is null");
428 aShape = MakeEdgeFromWire(aWire, LinTol, AngTol);
430 else if (aType == EDGE_CURVE_LENGTH) {
431 anExpectedType = TopAbs_EDGE;
433 GEOMImpl_IVector aVI (aFunction);
436 Handle(GEOM_Function) aRefCurve = aVI.GetPoint1();
437 if (aRefCurve.IsNull()) Standard_NullObject::Raise("Argument Curve is null");
438 TopoDS_Shape aRefShape1 = aRefCurve->GetValue();
439 if (aRefShape1.ShapeType() != TopAbs_EDGE) {
440 Standard_TypeMismatch::Raise
441 ("Edge On Curve creation aborted : curve shape is not an edge");
443 TopoDS_Edge aRefEdge = TopoDS::Edge(aRefShape1);
444 TopoDS_Vertex V1, V2;
445 TopExp::Vertices(aRefEdge, V1, V2, Standard_True);
448 TopoDS_Vertex aRefVertex;
449 Handle(GEOM_Function) aRefPoint = aVI.GetPoint2();
450 if (aRefPoint.IsNull()) {
454 TopoDS_Shape aRefShape2 = aRefPoint->GetValue();
455 if (aRefShape2.ShapeType() != TopAbs_VERTEX) {
456 Standard_TypeMismatch::Raise
457 ("Edge On Curve creation aborted : start point shape is not a vertex");
459 aRefVertex = TopoDS::Vertex(aRefShape2);
461 gp_Pnt aRefPnt = BRep_Tool::Pnt(aRefVertex);
464 Standard_Real aLength = aVI.GetParameter();
465 //Standard_Real aCurveLength = IntTools::Length(aRefEdge);
466 //if (aLength > aCurveLength) {
467 // Standard_ConstructionError::Raise
468 // ("Edge On Curve creation aborted : given length is greater than edges length");
470 if (fabs(aLength) < Precision::Confusion()) {
471 Standard_ConstructionError::Raise
472 ("Edge On Curve creation aborted : given length is smaller than Precision::Confusion()");
476 Standard_Real UFirst, ULast;
477 Handle(Geom_Curve) EdgeCurve = BRep_Tool::Curve(aRefEdge, UFirst, ULast);
478 Handle(Geom_Curve) ReOrientedCurve = EdgeCurve;
480 Standard_Real dU = ULast - UFirst;
481 Standard_Real par1 = UFirst + 0.1 * dU;
482 Standard_Real par2 = ULast - 0.1 * dU;
484 gp_Pnt P1 = EdgeCurve->Value(par1);
485 gp_Pnt P2 = EdgeCurve->Value(par2);
487 if (aRefPnt.SquareDistance(P2) < aRefPnt.SquareDistance(P1)) {
488 ReOrientedCurve = EdgeCurve->Reversed();
489 UFirst = EdgeCurve->ReversedParameter(ULast);
492 // Get the point by length
493 GeomAdaptor_Curve AdapCurve = GeomAdaptor_Curve(ReOrientedCurve);
494 GCPnts_AbscissaPoint anAbsPnt (AdapCurve, aLength, UFirst);
495 Standard_Real aParam = anAbsPnt.Parameter();
497 if (AdapCurve.IsClosed() && aLength < 0.0) {
498 Standard_Real aTmp = aParam;
503 BRepBuilderAPI_MakeEdge aME (ReOrientedCurve, UFirst, aParam);
505 aShape = aME.Shape();
506 } else if (aType == SHAPE_ISOLINE) {
507 GEOMImpl_IIsoline aII (aFunction);
508 Handle(GEOM_Function) aRefFace = aII.GetFace();
509 TopoDS_Shape aShapeFace = aRefFace->GetValue();
511 if (aShapeFace.ShapeType() == TopAbs_FACE) {
512 TopoDS_Face aFace = TopoDS::Face(aShapeFace);
513 bool isUIso = aII.GetIsUIso();
514 Standard_Real aParam = aII.GetParameter();
515 Standard_Real U1,U2,V1,V2;
517 // Construct a real geometric parameter.
518 aFace.Orientation(TopAbs_FORWARD);
519 ShapeAnalysis::GetFaceUVBounds(aFace,U1,U2,V1,V2);
522 aParam = U1 + (U2 - U1)*aParam;
524 aParam = V1 + (V2 - V1)*aParam;
527 aShape = MakeIsoline(aFace, isUIso, aParam);
529 Standard_NullObject::Raise
530 ("Shape for isoline construction is not a face");
536 if (aShape.IsNull()) return 0;
538 // Check shape validity
539 BRepCheck_Analyzer ana (aShape, false);
540 if (!ana.IsValid()) {
541 //Standard_ConstructionError::Raise("Algorithm have produced an invalid shape result");
542 // For Mantis issue 0021772: EDF 2336 GEOM: Non valid face created from two circles
543 Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape (aShape);
545 aShape = aSfs->Shape();
548 // Check if the result shape type is compatible with the expected.
549 const TopAbs_ShapeEnum aShType = aShape.ShapeType();
551 if (anExpectedType != TopAbs_SHAPE && anExpectedType != aShType) {
552 Standard_ConstructionError::Raise("Result type check failed");
555 aFunction->SetValue(aShape);
557 log.SetTouched(Label());
559 if (!aWarning.IsEmpty())
560 Standard_Failure::Raise(aWarning.ToCString());
565 TopoDS_Wire GEOMImpl_ShapeDriver::MakeWireFromEdges(const Handle(TColStd_HSequenceOfTransient)& theEdgesFuncs,
566 const Standard_Real theTolerance)
574 for (unsigned int ind = 1; ind <= theEdgesFuncs->Length(); ind++) {
575 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(theEdgesFuncs->Value(ind));
576 TopoDS_Shape aShape_i = aRefShape->GetValue();
577 if (aShape_i.IsNull()) {
578 Standard_NullObject::Raise("Shape for wire construction is null");
580 if (aShape_i.ShapeType() == TopAbs_EDGE || aShape_i.ShapeType() == TopAbs_WIRE) {
581 TopExp_Explorer exp (aShape_i, TopAbs_EDGE);
582 for (; exp.More(); exp.Next())
583 B.Add(aWire, TopoDS::Edge(exp.Current()));
585 Standard_TypeMismatch::Raise
586 ("Shape for wire construction is neither an edge nor a wire");
591 Handle(ShapeFix_Wire) aFW = new ShapeFix_Wire;
595 if (aFW->StatusReorder(ShapeExtend_FAIL1)) {
596 Standard_ConstructionError::Raise("Wire construction failed: several loops detected");
598 else if (aFW->StatusReorder(ShapeExtend_FAIL)) {
599 Standard_ConstructionError::Raise("Wire construction failed");
604 // IMP 0019766: Building a Wire from unconnected edges by introducing a tolerance
605 aFW->ClosedWireMode() = Standard_False;
606 aFW->FixConnected(theTolerance);
607 if (aFW->StatusConnected(ShapeExtend_FAIL)) {
608 Standard_ConstructionError::Raise("Wire construction failed: cannot build connected wire");
611 if (aFW->StatusConnected(ShapeExtend_DONE3)) {
612 // Confused with <prec> but not Analyzer.Precision(), set the same
613 aFW->FixGapsByRangesMode() = Standard_True;
614 if (aFW->FixGaps3d()) {
615 Handle(ShapeExtend_WireData) sbwd = aFW->WireData();
616 Handle(ShapeFix_Edge) aFe = new ShapeFix_Edge;
617 for (Standard_Integer iedge = 1; iedge <= sbwd->NbEdges(); iedge++) {
618 TopoDS_Edge aEdge = TopoDS::Edge(sbwd->Edge(iedge));
619 aFe->FixVertexTolerance(aEdge);
620 aFe->FixSameParameter(aEdge);
623 else if (aFW->StatusGaps3d(ShapeExtend_FAIL)) {
624 Standard_ConstructionError::Raise("Wire construction failed: cannot fix 3d gaps");
627 aWire = aFW->WireAPIMake();
632 TopoDS_Edge GEOMImpl_ShapeDriver::MakeEdgeFromWire(const TopoDS_Shape& aWire,
633 const Standard_Real LinTol,
634 const Standard_Real AngTol)
638 BRepLib::BuildCurves3d(aWire);
639 Handle(ShapeFix_Shape) Fixer = new ShapeFix_Shape(aWire);
640 Fixer->SetPrecision(LinTol);
641 Fixer->SetMaxTolerance(LinTol);
643 TopoDS_Wire theWire = TopoDS::Wire(Fixer->Shape());
645 TColGeom_SequenceOfCurve CurveSeq;
646 TopTools_SequenceOfShape LocSeq;
647 TColStd_SequenceOfReal FparSeq;
648 TColStd_SequenceOfReal LparSeq;
649 TColStd_SequenceOfReal TolSeq;
650 GeomAbs_CurveType CurType;
651 TopoDS_Vertex FirstVertex, LastVertex;
653 BRepTools_WireExplorer wexp(theWire) ;
654 for (; wexp.More(); wexp.Next())
656 TopoDS_Edge anEdge = wexp.Current();
657 Standard_Real fpar, lpar;
658 TopLoc_Location aLoc;
659 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aLoc, fpar, lpar);
663 BRepAdaptor_Curve BAcurve(anEdge);
664 GeomAbs_CurveType aType = BAcurve.GetType();
666 Handle(Geom_Curve) aBasisCurve = BAcurve.Curve().Curve();
668 if (aBasisCurve->IsPeriodic())
669 ElCLib::AdjustPeriodic(aBasisCurve->FirstParameter(), aBasisCurve->LastParameter(),
670 Precision::PConfusion(), fpar, lpar);
672 if (CurveSeq.IsEmpty())
674 CurveSeq.Append(aCurve);
675 TopoDS_Shape aLocShape;
676 aLocShape.Location(aLoc);
677 aLocShape.Orientation(wexp.Orientation());
678 LocSeq.Append(aLocShape);
679 FparSeq.Append(fpar);
680 LparSeq.Append(lpar);
682 FirstVertex = wexp.CurrentVertex();
686 Standard_Boolean Done = Standard_False;
687 Standard_Real NewFpar, NewLpar;
688 GeomAdaptor_Curve GAprevcurve(CurveSeq.Last());
689 TopoDS_Vertex CurVertex = wexp.CurrentVertex();
690 TopoDS_Vertex CurFirstVer = TopExp::FirstVertex(anEdge);
691 TopAbs_Orientation ConnectByOrigin = (CurVertex.IsSame(CurFirstVer))? TopAbs_FORWARD : TopAbs_REVERSED;
692 if (aCurve == CurveSeq.Last())
696 if (aBasisCurve->IsPeriodic())
698 if (NewLpar < NewFpar)
699 NewLpar += aBasisCurve->Period();
700 if (ConnectByOrigin == TopAbs_FORWARD)
701 ElCLib::AdjustPeriodic(FparSeq.Last(),
702 FparSeq.Last() + aBasisCurve->Period(),
703 Precision::PConfusion(), NewFpar, NewLpar);
705 ElCLib::AdjustPeriodic(FparSeq.Last() - aBasisCurve->Period(),
707 Precision::PConfusion(), NewFpar, NewLpar);
709 Done = Standard_True;
711 else if (aType == CurType &&
712 aType != GeomAbs_BezierCurve &&
713 aType != GeomAbs_BSplineCurve &&
714 aType != GeomAbs_OtherCurve)
720 gp_Lin aLine = BAcurve.Line();
721 gp_Lin PrevLine = GAprevcurve.Line();
722 if (aLine.Contains(PrevLine.Location(), LinTol) &&
723 aLine.Direction().IsParallel(PrevLine.Direction(), AngTol))
725 gp_Pnt P1 = ElCLib::Value(fpar, aLine);
726 gp_Pnt P2 = ElCLib::Value(lpar, aLine);
727 NewFpar = ElCLib::Parameter(PrevLine, P1);
728 NewLpar = ElCLib::Parameter(PrevLine, P2);
729 if (NewLpar < NewFpar)
731 Standard_Real MemNewFpar = NewFpar;
733 NewLpar = MemNewFpar;
734 ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
736 Done = Standard_True;
742 gp_Circ aCircle = BAcurve.Circle();
743 gp_Circ PrevCircle = GAprevcurve.Circle();
744 if (aCircle.Location().Distance(PrevCircle.Location()) <= LinTol &&
745 Abs(aCircle.Radius() - PrevCircle.Radius()) <= LinTol &&
746 aCircle.Axis().IsParallel(PrevCircle.Axis(), AngTol))
748 if (aCircle.Axis().Direction() * PrevCircle.Axis().Direction() < 0.)
750 Standard_Real memfpar = fpar;
753 ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
755 gp_Pnt P1 = ElCLib::Value(fpar, aCircle);
756 gp_Pnt P2 = ElCLib::Value(lpar, aCircle);
757 NewFpar = ElCLib::Parameter(PrevCircle, P1);
758 NewLpar = ElCLib::Parameter(PrevCircle, P2);
759 if (NewLpar < NewFpar)
761 //Standard_Real MemNewFpar = NewFpar, MemNewLpar = NewLpar;
762 if (ConnectByOrigin == TopAbs_FORWARD)
763 ElCLib::AdjustPeriodic(FparSeq.Last(),
764 FparSeq.Last() + 2.*M_PI,
765 Precision::PConfusion(), NewFpar, NewLpar);
767 ElCLib::AdjustPeriodic(FparSeq.Last() - 2.*M_PI,
769 Precision::PConfusion(), NewFpar, NewLpar);
770 Done = Standard_True;
774 case GeomAbs_Ellipse:
776 gp_Elips anEllipse = BAcurve.Ellipse();
777 gp_Elips PrevEllipse = GAprevcurve.Ellipse();
778 if (anEllipse.Focus1().Distance(PrevEllipse.Focus1()) <= LinTol &&
779 anEllipse.Focus2().Distance(PrevEllipse.Focus2()) <= LinTol &&
780 Abs(anEllipse.MajorRadius() - PrevEllipse.MajorRadius()) <= LinTol &&
781 Abs(anEllipse.MinorRadius() - PrevEllipse.MinorRadius()) <= LinTol &&
782 anEllipse.Axis().IsParallel(PrevEllipse.Axis(), AngTol))
784 if (anEllipse.Axis().Direction() * PrevEllipse.Axis().Direction() < 0.)
786 Standard_Real memfpar = fpar;
789 ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
791 gp_Pnt P1 = ElCLib::Value(fpar, anEllipse);
792 gp_Pnt P2 = ElCLib::Value(lpar, anEllipse);
793 NewFpar = ElCLib::Parameter(PrevEllipse, P1);
794 NewLpar = ElCLib::Parameter(PrevEllipse, P2);
795 if (NewLpar < NewFpar)
797 if (ConnectByOrigin == TopAbs_FORWARD)
798 ElCLib::AdjustPeriodic(FparSeq.Last(),
799 FparSeq.Last() + 2.*M_PI,
800 Precision::PConfusion(), NewFpar, NewLpar);
802 ElCLib::AdjustPeriodic(FparSeq.Last() - 2.*M_PI,
804 Precision::PConfusion(), NewFpar, NewLpar);
805 Done = Standard_True;
809 case GeomAbs_Hyperbola:
811 gp_Hypr aHypr = BAcurve.Hyperbola();
812 gp_Hypr PrevHypr = GAprevcurve.Hyperbola();
813 if (aHypr.Focus1().Distance(PrevHypr.Focus1()) <= LinTol &&
814 aHypr.Focus2().Distance(PrevHypr.Focus2()) <= LinTol &&
815 Abs(aHypr.MajorRadius() - PrevHypr.MajorRadius()) <= LinTol &&
816 Abs(aHypr.MinorRadius() - PrevHypr.MinorRadius()) <= LinTol &&
817 aHypr.Axis().IsParallel(PrevHypr.Axis(), AngTol))
819 gp_Pnt P1 = ElCLib::Value(fpar, aHypr);
820 gp_Pnt P2 = ElCLib::Value(lpar, aHypr);
821 NewFpar = ElCLib::Parameter(PrevHypr, P1);
822 NewLpar = ElCLib::Parameter(PrevHypr, P2);
823 if (NewLpar < NewFpar)
825 Standard_Real MemNewFpar = NewFpar;
827 NewLpar = MemNewFpar;
828 ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
830 Done = Standard_True;
834 case GeomAbs_Parabola:
836 gp_Parab aParab = BAcurve.Parabola();
837 gp_Parab PrevParab = GAprevcurve.Parabola();
838 if (aParab.Location().Distance(PrevParab.Location()) <= LinTol &&
839 aParab.Focus().Distance(PrevParab.Focus()) <= LinTol &&
840 Abs(aParab.Focal() - PrevParab.Focal()) <= LinTol &&
841 aParab.Axis().IsParallel(PrevParab.Axis(), AngTol))
843 gp_Pnt P1 = ElCLib::Value(fpar, aParab);
844 gp_Pnt P2 = ElCLib::Value(lpar, aParab);
845 NewFpar = ElCLib::Parameter(PrevParab, P1);
846 NewLpar = ElCLib::Parameter(PrevParab, P2);
847 if (NewLpar < NewFpar)
849 Standard_Real MemNewFpar = NewFpar;
851 NewLpar = MemNewFpar;
852 ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
854 Done = Standard_True;
858 } //end of switch (aType)
859 } // end of else if (aType == CurType && ...
862 if (NewFpar < FparSeq.Last())
863 FparSeq(FparSeq.Length()) = NewFpar;
865 LparSeq(LparSeq.Length()) = NewLpar;
869 CurveSeq.Append(aCurve);
870 TopoDS_Shape aLocShape;
871 aLocShape.Location(aLoc);
872 aLocShape.Orientation(wexp.Orientation());
873 LocSeq.Append(aLocShape);
874 FparSeq.Append(fpar);
875 LparSeq.Append(lpar);
876 TolSeq.Append(BRep_Tool::Tolerance(CurVertex));
879 } // end of else (CurveSeq.IsEmpty()) -> not first time
880 } // end for (; wexp.More(); wexp.Next())
882 LastVertex = wexp.CurrentVertex();
883 TolSeq.Append(BRep_Tool::Tolerance(LastVertex));
885 FirstVertex.Orientation(TopAbs_FORWARD);
886 LastVertex.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");
955 Standard_Boolean isValidEndVtx = Standard_True;
958 // Check if closed curve is reordered.
959 Handle(Geom_Curve) aCurve = concatcurve->Value(concatcurve->Lower());
960 Standard_Real aFPar = aCurve->FirstParameter();
962 gp_Pnt aPntVtx = BRep_Tool::Pnt(FirstVertex);
963 Standard_Real aTolVtx = BRep_Tool::Tolerance(FirstVertex);
965 aCurve->D0(aFPar, aPFirst);
967 if (!aPFirst.IsEqual(aPntVtx, aTolVtx)) {
968 // The curve is reordered. Find the new first and last vertices.
969 TopTools_IndexedMapOfShape aMapVtx;
970 TopExp::MapShapes(theWire, TopAbs_VERTEX, aMapVtx);
972 const Standard_Integer aNbVtx = aMapVtx.Extent();
973 Standard_Integer iVtx;
975 for (iVtx = 1; iVtx <= aNbVtx; iVtx++) {
976 const TopoDS_Vertex aVtx = TopoDS::Vertex(aMapVtx.FindKey(iVtx));
977 const gp_Pnt aPnt = BRep_Tool::Pnt(aVtx);
978 const Standard_Real aTol = BRep_Tool::Tolerance(aVtx);
980 if (aPFirst.IsEqual(aPnt, aTol)) {
981 // The coinsident vertex is found.
984 FirstVertex.Orientation(TopAbs_FORWARD);
985 LastVertex.Orientation(TopAbs_REVERSED);
991 // It is necessary to create new vertices.
992 isValidEndVtx = Standard_False;
998 ResEdge = BRepLib_MakeEdge(concatcurve->Value(concatcurve->Lower()),
999 FirstVertex, LastVertex,
1000 concatcurve->Value(concatcurve->Lower())->FirstParameter(),
1001 concatcurve->Value(concatcurve->Lower())->LastParameter());
1003 ResEdge = BRepLib_MakeEdge(concatcurve->Value(concatcurve->Lower()),
1004 concatcurve->Value(concatcurve->Lower())->FirstParameter(),
1005 concatcurve->Value(concatcurve->Lower())->LastParameter());
1010 if (CurveSeq(1)->IsInstance(STANDARD_TYPE(Geom_TrimmedCurve)))
1011 CurveSeq(1) = (*((Handle(Geom_TrimmedCurve)*)&(CurveSeq(1))))->BasisCurve();
1013 Handle(Geom_Curve) aNewCurve =
1014 Handle(Geom_Curve)::DownCast(CurveSeq(1)->Copy());
1016 aNewCurve->Transform(LocSeq(1).Location().Transformation());
1018 if (LocSeq(1).Orientation() == TopAbs_REVERSED) {
1019 const TopoDS_Vertex aVtxTmp = FirstVertex;
1021 FirstVertex = LastVertex;
1022 LastVertex = aVtxTmp;
1023 FirstVertex.Orientation(TopAbs_FORWARD);
1024 LastVertex.Orientation(TopAbs_REVERSED);
1027 ResEdge = BRepLib_MakeEdge(aNewCurve,
1028 FirstVertex, LastVertex,
1029 FparSeq(1), LparSeq(1));
1031 if (LocSeq(1).Orientation() == TopAbs_REVERSED) {
1040 //=============================================================================
1042 * \brief Returns an isoline for a face.
1044 //=============================================================================
1046 TopoDS_Shape GEOMImpl_ShapeDriver::MakeIsoline
1047 (const TopoDS_Face &theFace,
1049 const double theParameter) const
1051 TopoDS_Shape aResult;
1052 GEOMUtils::Hatcher aHatcher(theFace);
1053 const GeomAbs_IsoType aType = (IsUIso ? GeomAbs_IsoU : GeomAbs_IsoV);
1055 aHatcher.Init(aType, theParameter);
1058 if (!aHatcher.IsDone()) {
1059 Standard_ConstructionError::Raise("MakeIsoline : Hatcher failure");
1062 const Handle(TColStd_HArray1OfInteger) &anIndices =
1063 (IsUIso ? aHatcher.GetUIndices() : aHatcher.GetVIndices());
1065 if (anIndices.IsNull()) {
1066 Standard_ConstructionError::Raise("MakeIsoline : Null hatching indices");
1069 const Standard_Integer anIsoInd = anIndices->Lower();
1070 const Standard_Integer aHatchingIndex = anIndices->Value(anIsoInd);
1072 if (aHatchingIndex == 0) {
1073 Standard_ConstructionError::Raise("MakeIsoline : Invalid hatching index");
1076 const Standard_Integer aNbDomains =
1077 aHatcher.GetNbDomains(aHatchingIndex);
1079 if (aNbDomains < 0) {
1080 Standard_ConstructionError::Raise("MakeIsoline : Invalid number of domains");
1083 // The hatching is performed successfully. Create the 3d Curve.
1084 Handle(Geom_Surface) aSurface = BRep_Tool::Surface(theFace);
1085 Handle(Geom_Curve) anIsoCurve = (IsUIso ?
1086 aSurface->UIso(theParameter) : aSurface->VIso(theParameter));
1087 Handle(Geom2d_Curve) aPIsoCurve =
1088 aHatcher.GetHatching(aHatchingIndex);
1089 const Standard_Real aTol = Precision::Confusion();
1090 Standard_Integer anIDom = 1;
1093 BRep_Builder aBuilder;
1094 Standard_Integer aNbEdges = 0;
1096 for (; anIDom <= aNbDomains; anIDom++) {
1097 if (aHatcher.GetDomain(aHatchingIndex, anIDom, aV1, aV2)) {
1098 // Check first and last parameters.
1099 if (!aHatcher.IsDomainInfinite(aHatchingIndex, anIDom)) {
1101 TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge(anIsoCurve, aV1, aV2);
1103 // Update it with a parametric curve on face.
1104 aBuilder.UpdateEdge(anEdge, aPIsoCurve, theFace, aTol);
1108 // Result is a compond.
1109 if (aNbEdges == 2) {
1110 // Create a new compound.
1111 TopoDS_Compound aCompound;
1113 aBuilder.MakeCompound(aCompound);
1114 aBuilder.Add(aCompound, aResult);
1115 aResult = aCompound;
1118 // Add an edge to the compound.
1119 aBuilder.Add(aResult, anEdge);
1121 // Result is the edge.
1128 if (aNbEdges == 0) {
1129 Standard_ConstructionError::Raise("MakeIsoline : Empty result");
1135 //================================================================================
1137 * \brief Returns a name of creation operation and names and values of creation parameters
1139 //================================================================================
1141 bool GEOMImpl_ShapeDriver::
1142 GetCreationInformation(std::string& theOperationName,
1143 std::vector<GEOM_Param>& theParams)
1145 if (Label().IsNull()) return 0;
1146 Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
1148 GEOMImpl_IShapes aCI( function );
1149 Standard_Integer aType = function->GetType();
1153 theOperationName = "WIRE";
1154 AddParam( theParams, "Wires/edges", aCI.GetShapes() );
1155 AddParam( theParams, "Tolerance", aCI.GetTolerance() );
1158 theOperationName = "FACE";
1159 AddParam( theParams, "Wire/edge", aCI.GetBase() );
1160 AddParam( theParams, "Is planar wanted", aCI.GetIsPlanar() );
1163 theOperationName = "FACE";
1164 AddParam( theParams, "Wires/edges", aCI.GetShapes() );
1165 AddParam( theParams, "Is planar wanted", aCI.GetIsPlanar() );
1168 theOperationName = "SHELL";
1169 AddParam( theParams, "Objects", aCI.GetShapes() );
1172 theOperationName = "SOLID";
1173 AddParam( theParams, "Objects", aCI.GetShapes() );
1175 case COMPOUND_SHAPES:
1176 theOperationName = "COMPOUND";
1177 AddParam( theParams, "Objects", aCI.GetShapes() );
1180 theOperationName = "EDGE";
1181 AddParam( theParams, "Wire", aCI.GetBase() );
1182 AddParam( theParams, "Linear Tolerance", aCI.GetTolerance() );
1183 AddParam( theParams, "Angular Tolerance", aCI.GetAngularTolerance() );
1185 case EDGE_CURVE_LENGTH:
1186 theOperationName = "EDGE";
1188 GEOMImpl_IVector aCI( function );
1189 AddParam( theParams, "Edge", aCI.GetPoint1() );
1190 AddParam( theParams, "Start point", aCI.GetPoint2() );
1191 AddParam( theParams, "Length", aCI.GetParameter() );
1194 case SHAPES_ON_SHAPE:
1196 theOperationName = "GetShapesOnShapeAsCompound";
1197 Handle(TColStd_HSequenceOfTransient) shapes = aCI.GetShapes();
1198 if ( !shapes.IsNull() && shapes->Length() > 0 )
1199 AddParam( theParams, "Check shape", shapes->Value(1) );
1200 if ( !shapes.IsNull() && shapes->Length() > 1 )
1201 AddParam( theParams, "Shape", shapes->Value(2) );
1202 AddParam( theParams, "Shape type", TopAbs_ShapeEnum( aCI.GetSubShapeType() ));
1203 AddParam( theParams, "State", TopAbs_State((int) aCI.GetTolerance() ));
1208 GEOMImpl_IIsoline aII (function);
1210 theOperationName = "ISOLINE";
1211 AddParam(theParams, "Face", aII.GetFace());
1212 AddParam(theParams, "Isoline type", (aII.GetIsUIso() ? "U" : "V"));
1213 AddParam(theParams, "Parameter", aII.GetParameter());
1223 IMPLEMENT_STANDARD_HANDLE (GEOMImpl_ShapeDriver,GEOM_BaseDriver);
1224 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_ShapeDriver,GEOM_BaseDriver);
1226 //modified by NIZNHY-PKV Wed Dec 28 13:48:31 2011f
1227 #include <TopoDS_Iterator.hxx>
1228 #include <TopTools_HSequenceOfShape.hxx>
1229 #include <ShapeAnalysis_FreeBounds.hxx>
1230 #include <TopTools_MapOfShape.hxx>
1231 #include <TopTools_MapOfOrientedShape.hxx>
1232 #include <BRep_Builder.hxx>
1233 #include <TopoDS_Wire.hxx>
1235 //=======================================================================
1236 //function : KeepEdgesOrder
1238 //=======================================================================
1240 void KeepEdgesOrder(const Handle(TopTools_HSequenceOfShape)& aEdges,
1241 const Handle(TopTools_HSequenceOfShape)& aWires)
1243 Standard_Integer aNbWires, aNbEdges;
1245 if (aEdges.IsNull()) {
1249 if (aWires.IsNull()) {
1253 aNbEdges=aEdges->Length();
1254 aNbWires=aWires->Length();
1255 if (!aNbEdges || !aNbWires) {
1259 Standard_Boolean bClosed;
1260 Standard_Integer i, j;
1262 TopoDS_Iterator aIt;
1264 TopTools_MapOfOrientedShape aMEx;
1266 for (i=1; i<=aNbWires; ++i) {
1267 const TopoDS_Shape& aWx=aWires->Value(i);
1270 aIt.Initialize (aWx);
1271 for (; aIt.More(); aIt.Next()) {
1272 const TopoDS_Shape& aEx=aIt.Value();
1277 for (j=1; j<=aNbEdges; ++j) {
1278 const TopoDS_Shape& aE=aEdges->Value(j);
1279 if (aMEx.Contains(aE)) {
1284 bClosed=aWx.Closed();
1285 aWy.Closed(bClosed);
1287 aWires->Append(aWy);
1288 }// for (i=1; i<=aNbWires; ++i) {
1290 aWires->Remove(1, aNbWires);
1293 //modified by NIZNHY-PKV Wed Dec 28 13:48:34 2011t