1 // Created on: 1998-06-08
2 // Created by: Stephanie HUMEAU
3 // Copyright (c) 1998-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
23 #include <BRepFill_Draft.ixx>
25 #include <BRepFill_DraftLaw.hxx>
26 #include <BRepFill_ShapeLaw.hxx>
27 #include <BRepFill_Sweep.hxx>
29 #include <BndLib_Add3dCurve.hxx>
30 #include <BndLib_AddSurface.hxx>
31 #include <Bnd_Box.hxx>
34 #include <gp_Trsf.hxx>
38 #include <TColgp_Array1OfPnt.hxx>
40 #include <GeomAdaptor_Surface.hxx>
41 #include <BRepAdaptor_Surface.hxx>
42 #include <Adaptor3d_HCurve.hxx>
44 #include <GeomLProp_SLProps.hxx>
45 #include <Geom_Surface.hxx>
46 #include <Geom_Line.hxx>
47 #include <Geom_TrimmedCurve.hxx>
48 #include <Geom_Geometry.hxx>
49 #include <Geom_Plane.hxx>
50 #include <Geom_RectangularTrimmedSurface.hxx>
52 #include <GeomAdaptor_HSurface.hxx>
53 #include <Adaptor3d_Surface.hxx>
54 #include <BRepAdaptor_Curve.hxx>
56 #include <GeomFill_LocationDraft.hxx>
59 #include <TopoDS_Edge.hxx>
60 #include <TopoDS_Wire.hxx>
61 #include <TopoDS_Shell.hxx>
62 #include <TopoDS_Solid.hxx>
63 #include <TopoDS_Iterator.hxx>
64 #include <TopExp_Explorer.hxx>
67 #include <BRepLib_MakeWire.hxx>
68 #include <BRepLib_MakeEdge.hxx>
69 #include <BRepLib_MakeFace.hxx>
70 #include <BRepLib_FindSurface.hxx>
71 #include <BRep_Builder.hxx>
73 #include <BRep_Tool.hxx>
74 #include <BRepTools.hxx>
75 #include <BRepAlgo_DSAccess.hxx>
76 #include <BRepBuilderAPI_Sewing.hxx>
77 #include <BRepClass3d_SolidClassifier.hxx>
79 #include <TopTools_ListIteratorOfListOfShape.hxx>
80 #include <TopTools_ListOfShape.hxx>
81 #include <BRepExtrema_DistShapeShape.hxx>
83 #include <Precision.hxx>
84 #include <TColStd_Array1OfReal.hxx>
86 #include <Standard_NoSuchObject.hxx>
87 #include <StdFail_NotDone.hxx>
90 #include <Geom_Circle.hxx>
93 #include <DrawTrSurf.hxx>
94 static Standard_Boolean Affich = 0;
97 //=======================================================================
100 //======================================================================
101 static void ComputeTrsf(const TopoDS_Wire& W,
106 // Calculate approximate barycenter
107 BRepTools_WireExplorer Exp(W);
108 // Class BRep_Tool without fields and without Constructor :
110 gp_XYZ Bary(0.,0.,0.);
113 for (nb=0; Exp.More(); Exp.Next()) {
114 // Bary += BT.Pnt(Exp.CurrentVertex()).XYZ();
115 Bary += BRep_Tool::Pnt(Exp.CurrentVertex()).XYZ();
120 // Calculate the Transformation
122 Tf.SetTransformation(N);
123 BRepAdaptor_Curve AC;
124 // BndLib_Add3dCurve BC;
126 // transformation to the wire
127 TopoDS_Wire TheW = W;
128 TopLoc_Location Loc(Tf);
134 for (Exp.Init(TheW); Exp.More(); Exp.Next()) {
135 AC.Initialize(Exp.Current());
136 // BC.Add(AC, 0.1, Box);
137 BndLib_Add3dCurve::Add(AC, 0.1, Box);
141 //=======================================================================
144 //======================================================================
145 static Standard_Real Longueur(const Bnd_Box& WBox,
150 // face of the box most remoted from the face input in
151 // the direction of skin
152 Standard_Real Xmin,Ymin,Zmin,Xmax,Ymax,Zmax,WZmin,WZmax,L;
155 WBox.Get(Xmin,Ymin,Zmin,Xmax,Ymax,Zmax);
159 SBox.Get(Xmin,Ymin,Zmin,Xmax,Ymax,Zmax);
160 P.SetCoord( (Xmin+Xmax)/2, (Ymin+Ymax)/2, Zmax);
163 // Skin in the wrong direction. Invert...
174 //=======================================================================
175 //function : GoodOrientation
176 //purpose : Check if the law is oriented to have an exterior skin
177 //======================================================================
178 static Standard_Boolean GoodOrientation(const Bnd_Box& B,
179 const Handle(BRepFill_LocationLaw)& Law,
182 Standard_Real f, l, r, t;
183 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
185 B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
186 gp_Pnt P1(aXmin, aYmin, aZmin), P2(aXmax, aYmax, aZmax);
189 Law->CurvilinearBounds(Law->NbLaw(), f, l);
192 Standard_Integer ii, Ind;
194 Standard_Integer Nb = (Standard_Integer) (4+(10*r));
196 // Standard_Integer Nb = 4+(10*r);
200 Nb++; // Number of points
202 TColgp_Array1OfPnt Pnts(1, Nb);
203 Handle(Adaptor3d_HCurve) AC;
204 gp_XYZ Bary(0.,0.,0.);
206 for (ii=1; ii<=Nb; ii++) {
207 Law->Parameter((ii-1)*r, Ind, t);
208 AC = Law->Law(Ind)->GetCurve();
210 Bary+= Pnts(ii).XYZ();
215 gp_Vec Normal(D.XYZ());
216 Standard_Real Angle = 0;
217 gp_Vec Ref(Centre, Pnts(1));
219 for (ii=2; ii<=Nb; ii++) {
220 gp_Vec R(Centre, Pnts(ii));
221 Angle += Ref.AngleWithRef(R, Normal);
228 //=======================================================================
229 //function : Constructeur
231 //======================================================================
232 BRepFill_Draft::BRepFill_Draft(const TopoDS_Shape& S,
234 const Standard_Real Angle)
239 mySections.Nullify();
241 switch (S.ShapeType()) {
244 myWire = TopoDS::Wire(S);
249 TopoDS_Iterator Exp (S);
250 myWire = TopoDS::Wire(Exp.Value());
255 TopTools_ListOfShape List;
256 TopTools_IndexedDataMapOfShapeListOfShape edgemap;
257 TopExp::MapShapesAndAncestors(S,TopAbs_EDGE,TopAbs_FACE,edgemap);
258 Standard_Integer iedge, nbf;
259 for (iedge = 1; iedge <= edgemap.Extent(); iedge++) {
260 const TopoDS_Edge& theEdge = TopoDS::Edge(edgemap.FindKey(iedge));
261 // skip degenerated edges
262 if (!BRep_Tool::Degenerated(theEdge)) {
263 nbf = edgemap(iedge).Extent();
264 if (nbf==1) List.Append(theEdge);
268 if( List.Extent()>0) {
271 BRepLib_WireError Err = MW.Error();
272 if (Err == BRepLib_WireDone) {
277 cout << "Error in MakeWire" << endl;
279 Standard_ConstructionError::Raise("BRepFill_Draft");
284 cout << "No Free Borders !" << endl;
286 Standard_ConstructionError::Raise("BRepFill_Draft");
291 Standard_ConstructionError::Raise("BRepFill_Draft");
294 // Attention to closed non declared wires !
295 if (!myWire.Closed()) {
296 TopoDS_Vertex Vf, Vl;
297 TopExp::Vertices(myWire, Vf, Vl);
298 if (Vf.IsSame(Vl)) myWire.Closed(Standard_True);
302 DBRep::Set("TheWire", myWire);
306 myAngle = Abs(Angle);
309 myDone = Standard_False;
316 //=======================================================================
317 //function :SetOptions
318 //purpose : Defines the style
319 //======================================================================
320 void BRepFill_Draft::SetOptions(const BRepFill_TransitionStyle Style,
321 const Standard_Real Min,
322 const Standard_Real Max)
329 //=======================================================================
332 //======================================================================
333 void BRepFill_Draft::SetDraft(const Standard_Boolean Internal)
335 IsInternal = Internal;
339 //=======================================================================
341 //purpose : calculate a surface of skinning
342 //======================================================================
343 void BRepFill_Draft::Perform(const Standard_Real LengthMax)
345 Handle(Geom_Surface) S;
347 Bnd_Box WBox;//, SBox;
351 ComputeTrsf(myWire, myDir, WBox, Trsf);
352 Init(S, LengthMax, WBox);
357 //=======================================================================
359 //purpose : calculate a surface of skinning
360 //======================================================================
361 void BRepFill_Draft::Perform(const Handle(Geom_Surface)& Surface,
362 const Standard_Boolean KeepInsideSurface)
369 ComputeTrsf(myWire, myDir, WBox, Trsf);
371 // box with bounds of the stop surface
372 Handle(Geom_Surface) Surf;
373 Surf = Handle(Geom_Surface)::DownCast(Surface->Transformed(Trsf));
374 GeomAdaptor_Surface S1 (Surf);
375 // BndLib_AddSurface AS;
376 // AS.Add(S1, 0.1, SBox);
377 BndLib_AddSurface::Add(S1, 0.1, SBox);
379 // calculate the maximum length of the rule.
380 L = Longueur(WBox, SBox, myDir, Pt);
381 L /= Abs(Cos(myAngle));
384 Init(Surface, L, WBox);
385 BuildShell(Surface, !KeepInsideSurface);
389 //================================================================
391 //purpose : calculate the surface of skinning, stopped by a shape
392 //================================================================
393 void BRepFill_Draft::Perform(const TopoDS_Shape& StopShape,
394 const Standard_Boolean KeepOutSide)
401 ComputeTrsf(myWire, myDir, WBox, Trsf);
403 // bounding box of the stop shape
404 Bnd_Box BSurf;//, TheBox;
405 Standard_Real Umin, Umax, Vmin, Vmax;
408 Handle(Geom_Surface) Surf;
410 // BndLib_AddSurface AS;
412 TopExp_Explorer Ex (StopShape, TopAbs_FACE);
415 while (Ex.More()) { // parse faces of the stop shape
416 // B.UVBounds(TopoDS::Face(Ex.Current()), Umin,Umax,Vmin,Vmax);
417 BRepTools::UVBounds(TopoDS::Face(Ex.Current()), Umin,Umax,Vmin,Vmax);
418 Surf = Handle(Geom_Surface)::DownCast(
419 // BT.Surface(TopoDS::Face(Ex.Current()))->Transformed(Trsf) );
420 BRep_Tool::Surface(TopoDS::Face(Ex.Current()))->Transformed(Trsf) );
421 GeomAdaptor_Surface S1 (Surf);
422 // bounding box of the current face
423 // AS.Add(S1, Umin, Umax, Vmin, Vmax, 0.1, BSurf);
424 BndLib_AddSurface::Add(S1, Umin, Umax, Vmin, Vmax, 0.1, BSurf);
425 SBox.Add(BSurf); // group boxes
429 // calculate the maximum length of the rule.
430 L = Longueur(WBox, SBox, myDir, Pt);
431 L /= Abs(Cos(myAngle));
435 Inv = Trsf.Inverted(); // inverted transformation
436 Pt.Transform(Inv); // coordinate in the absolute reference
437 Handle(Geom_Plane) Plan = new (Geom_Plane)(Pt, myDir);
438 Surf = new (Geom_RectangularTrimmedSurface) (Plan,-L, L, -L, L);
442 char* Temp = "ThePlan" ;
443 DrawTrSurf::Set(Temp, Surf);
444 // DrawTrSurf::Set("ThePlan", Surf);
448 // Sweeping and restriction
449 Init(Plan, L*1.01, WBox);
450 BuildShell(Surf, Standard_False);
451 Fuse(StopShape, KeepOutSide);
455 //=======================================================================
457 //purpose : Construction of laws.
458 //======================================================================
459 void BRepFill_Draft::Init(const Handle(Geom_Surface)& ,
460 const Standard_Real Length,
465 // law of positioning
466 Handle(GeomFill_LocationDraft) Loc
467 = new (GeomFill_LocationDraft) (myDir, myAngle);
468 myLoc = new (BRepFill_DraftLaw) (myWire, Loc);
470 B = GoodOrientation(Box, myLoc, myDir);
472 if (IsInternal ^ (!B) ) {
474 Loc->SetAngle(myAngle);
475 myLoc = new (BRepFill_DraftLaw) (myWire, Loc);
478 myLoc->CleanLaw(angmin); // Clean small discontinuities.
481 // generating line is straight and parallel to binormal.
483 gp_Vec D (0., 1., 0.);
485 // Control of the orientation
487 myLoc->Law(1)->GetDomain(f,l);
491 myLoc->Law(1)->D0( (f+l)/2, M, Bid);
492 gp_Dir BN(M.Column(2));
494 Standard_Real ang = myDir.Angle(BN);
495 if (ang > M_PI/2) D.Reverse();
496 Handle(Geom_Line) L = new (Geom_Line) (P, D);
498 Handle(Geom_Curve) TC = new (Geom_TrimmedCurve) (L, 0, Length);
503 TC = new (Geom_Circle) (gp::XOY(), Length);
507 BRepLib_MakeEdge ME(TC);
508 TopoDS_Edge EG = ME.Edge();
510 BRepLib_MakeWire MW(EG);
511 TopoDS_Wire G = MW.Wire();
513 mySec = new (BRepFill_ShapeLaw) (G, Standard_True);
517 //=======================================================================
518 //function : BuildShell
519 //purpose : Construction of the skinning surface
520 //======================================================================
521 void BRepFill_Draft::BuildShell(const Handle(Geom_Surface)& Surf,
522 const Standard_Boolean KeepOutSide)
524 // construction of the surface
525 BRepFill_Sweep Sweep(mySec, myLoc, Standard_True);
526 Sweep.SetTolerance(myTol);
527 Sweep.SetAngularControl(angmin, angmax);
528 Sweep.Build(myStyle, GeomFill_Location, myCont);
529 if (Sweep.IsDone()) {
530 myShape = Sweep.Shape();
531 myShell = TopoDS::Shell(myShape);
532 myFaces = Sweep.SubShape();
533 mySections = Sweep.Sections();
534 myDone = Standard_True;
535 // Control of the orientation
536 Standard_Boolean out=Standard_True;
537 TopExp_Explorer ex(myShell,TopAbs_FACE);
539 F = TopoDS::Face(ex.Current());
540 BRepAdaptor_Surface SF(F);
544 u = SF.FirstUParameter();
545 v = SF.FirstVParameter();
548 if (F.Orientation() == TopAbs_REVERSED) V.Reverse();
549 if (V.Magnitude() > 1.e-10) {
550 out = myDir.Angle(V) > M_PI/2;
552 if (out == IsInternal) {
558 myDone = Standard_False;
562 if (!Surf.IsNull()) { // Add the face at end
564 // Waiting the use of traces & retriction in BRepFill_Sweep
566 BRepLib_MakeFace MkF;
567 MkF.Init(Surf, Standard_True, Precision::Confusion());
568 Fuse(MkF.Face(), KeepOutSide);
573 //=======================================================================
575 //purpose : Boolean operation between the skin and the
577 //======================================================================
578 Standard_Boolean BRepFill_Draft::Fuse(const TopoDS_Shape& StopShape,
579 const Standard_Boolean KeepOutSide)
582 Standard_Boolean issolid = Standard_False;
583 TopoDS_Solid Sol1, Sol2;
584 TopAbs_State State1 = TopAbs_OUT, State2 = TopAbs_OUT;
587 if (myShape.ShapeType()==TopAbs_SOLID) {
588 Sol1 = TopoDS::Solid(myShape);
589 issolid = Standard_True;
593 B.Add(Sol1, myShape); // shell => solid (for fusion)
597 switch (StopShape.ShapeType()) {
598 case TopAbs_COMPOUND :
600 TopoDS_Iterator It(StopShape);
601 return Fuse(It.Value(), KeepOutSide);
605 Sol2 = TopoDS::Solid(StopShape);
611 B.Add(Sol2, StopShape); // shell => solid (for fusion)
621 B.Add(Sol2, S); // shell => solid (for fusion)
627 return Standard_False; // Impossible to do
631 BRepAlgo_DSAccess DSA;
632 DSA.Load(Sol1, Sol2);
633 DSA.Intersect(Sol1, Sol2); // intersection of 2 solids
635 // removal of edges corresponding to "unused" intersections
636 Standard_Integer NbPaquet;
639 TopTools_ListOfShape List;
640 List = DSA.GetSectionEdgeSet();// list of edges
642 NbPaquet = List.Extent();
646 cout << "No fusion" << endl;
647 DBRep::Set("DepPart", Sol1);
648 DBRep::Set("StopPart", Sol2);
650 return Standard_False;
654 // It is required to select packs.
655 TColStd_Array1OfReal Dist(1, NbPaquet);
656 TopTools_ListIteratorOfListOfShape it(List);
657 Standard_Real D, Dmin = 1.e10;
660 //Classify the packs by distance.
661 BRepExtrema_DistShapeShape Dist2;
662 Dist2.LoadS1( myWire );
663 for (ii=1; it.More();it.Next(),ii++){
664 Dist2.LoadS2( it.Value() );
666 if (Dist2.IsDone()) {
669 if (D < Dmin) Dmin = D;
675 // remove edges "farther" than Dmin
676 for (ii=1, it.Initialize(List); it.More();it.Next(), ii++){
677 if (Dist(ii) > Dmin) {
678 DSA.SuppressEdgeSet(it.Value());
682 DBRep::Set("KeepEdges", it.Value());
688 if (StopShape.ShapeType() != TopAbs_SOLID) {
689 // It is required to choose the state by the geometry
691 //(1) Return an edge of section
692 List = DSA.GetSectionEdgeSet();// list of edges
693 TopTools_ListIteratorOfListOfShape it(List);
694 TopoDS_Iterator iter(it.Value());
695 TopoDS_Edge E = TopoDS::Edge(iter.Value());
697 //(2) Return geometry on StopShape
698 // Class BRep_Tool without fields and without Constructor :
700 Handle(Geom_Surface) S;
701 Handle(Geom2d_Curve) C2d;
705 // BT.CurveOnSurface(E, C2d, S, L, f, l, 2);
706 BRep_Tool::CurveOnSurface(E, C2d, S, L, f, l, 2);
709 C2d->D0((f+l)/2,P2d);
710 GeomLProp_SLProps SP(S, P2d.X(), P2d.Y(), 1, 1.e-12);
711 if (! SP.IsNormalDefined()) {
712 C2d->D0((3*f+l)/4,P2d);
713 SP.SetParameters(P2d.X(), P2d.Y());
714 if ( !SP.IsNormalDefined()) {
715 C2d->D0((f+3*l)/4,P2d);
716 SP.SetParameters(P2d.X(), P2d.Y());
721 if (myDir.Angle(SP.Normal()) < M_PI/2) State1 = TopAbs_IN;
722 else State1 = TopAbs_OUT;
725 if (! KeepOutSide) { // Invert State2;
726 if (State2 == TopAbs_IN) State2 = TopAbs_OUT;
727 else State2 = TopAbs_IN;
730 //recalculate the final shape
731 TopoDS_Shape result = DSA.Merge(State1, State2);
733 if (issolid) myShape = result;
736 Exp.Init(result, TopAbs_SHELL);
737 if (Exp.More()) myShape = Exp.Current();
740 // Update the History
742 for (ii=1; ii<=myLoc->NbLaw(); ii++) {
743 const TopTools_ListOfShape& L = DSA.Modified(myFaces->Value(1,ii));
745 myFaces->SetValue(1, ii, L.First());
747 for (ii=1; ii<=myLoc->NbLaw()+1; ii++) {
748 const TopTools_ListOfShape& L = DSA.Modified(mySections->Value(1,ii));
750 mySections->SetValue(1, ii, L.First());
753 return Standard_True;
756 //=======================================================================
758 //purpose : Assemble the skin with the above face
759 //======================================================================
760 Standard_Boolean BRepFill_Draft::Sewing()
762 Standard_Boolean ToAss;
763 Standard_Boolean Ok = Standard_False;
764 ToAss = (myTop.ShapeType() != TopAbs_WIRE);
766 if ((!ToAss) || (!myDone)) return Standard_False;
768 // Assembly make a shell from the faces of the shape + the input shape
769 Handle(BRepBuilderAPI_Sewing) Ass = new BRepBuilderAPI_Sewing(5*myTol, Standard_True,
770 Standard_True, Standard_False);
773 ToAss = Standard_True;
776 Standard_Integer NbCE;
779 // Check if the assembly is real.
780 NbCE = Ass->NbContigousEdges();
784 res = Ass->SewedShape();
785 if ((res.ShapeType() == TopAbs_SHELL)||
786 (res.ShapeType() == TopAbs_SOLID)) {
790 else if (res.ShapeType() == TopAbs_COMPOUND) {
791 TopoDS_Iterator It(res);
794 if (!It.More()) {//Only one part => this is correct
802 // Update the History
804 for (ii=1; ii<=myLoc->NbLaw(); ii++) {
805 if (Ass->IsModified(myFaces->Value(1,ii)))
806 myFaces->SetValue(1, ii,
807 Ass->Modified(myFaces->Value(1,ii)));
809 for (ii=1; ii<=myLoc->NbLaw()+1; ii++) {
810 if (Ass->IsModified(mySections->Value(1,ii)))
811 mySections->SetValue(1, ii,
812 Ass->Modified(mySections->Value(1,ii)));
815 if (myShape.Closed()) { // Make a Solid
819 BS.Add(solid,TopoDS::Shell(myShape));
821 BRepClass3d_SolidClassifier SC(solid);
822 SC.PerformInfinitePoint(Precision::Confusion());
823 if ( SC.State() == TopAbs_IN) {
826 BS.Add(solid,TopoDS::Shell(myShape));
832 else cout << "Draft : No assembly !" << endl;
837 //=======================================================================
838 //function : Generated
839 //purpose : return a sub-part generated by sweeping
840 //======================================================================
841 const TopTools_ListOfShape&
842 BRepFill_Draft::Generated(const TopoDS_Shape& S)
849 for (ii=0; ii<=myLoc->NbLaw(); ii++)
850 if (E.IsSame(myLoc->Vertex(ii))) {
851 myGenerated.Append(mySections->Value(1, ii+1));
856 for (ii=1; ii<=myLoc->NbLaw(); ii++)
857 if (E.IsSame(myLoc->Edge(ii))) {
858 myGenerated.Append(myFaces->Value(1, ii));
866 //=======================================================================
868 //purpose : return the complete shape
869 //======================================================================
870 TopoDS_Shape BRepFill_Draft::Shape()const
875 //=====================================================================
877 //purpose : surface of skinning with the input face (=>shell)
878 //=====================================================================
879 TopoDS_Shell BRepFill_Draft::Shell()const
884 //=======================================================================
887 //======================================================================
888 Standard_Boolean BRepFill_Draft::IsDone()const