1 // Created on: 1998-06-08
2 // Created by: Stephanie HUMEAU
3 // Copyright (c) 1998-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
18 #include <Adaptor3d_HCurve.hxx>
19 #include <Adaptor3d_Surface.hxx>
20 #include <Bnd_Box.hxx>
21 #include <BndLib_Add3dCurve.hxx>
22 #include <BndLib_AddSurface.hxx>
23 #include <BRep_Builder.hxx>
24 #include <BRep_Tool.hxx>
25 #include <BRepAdaptor_Curve.hxx>
26 #include <BRepAdaptor_Surface.hxx>
27 #include <BRepAlgo_DSAccess.hxx>
28 #include <BRepBuilderAPI_Sewing.hxx>
29 #include <BRepClass3d_SolidClassifier.hxx>
30 #include <BRepExtrema_DistShapeShape.hxx>
31 #include <BRepFill_DataMapOfShapeHArray2OfShape.hxx>
32 #include <BRepFill_Draft.hxx>
33 #include <BRepFill_DraftLaw.hxx>
34 #include <BRepFill_SectionLaw.hxx>
35 #include <BRepFill_ShapeLaw.hxx>
36 #include <BRepFill_Sweep.hxx>
37 #include <BRepLib_FindSurface.hxx>
38 #include <BRepLib_MakeEdge.hxx>
39 #include <BRepLib_MakeFace.hxx>
40 #include <BRepLib_MakeWire.hxx>
41 #include <BRepTools.hxx>
42 #include <Geom_Geometry.hxx>
43 #include <Geom_Line.hxx>
44 #include <Geom_Plane.hxx>
45 #include <Geom_RectangularTrimmedSurface.hxx>
46 #include <Geom_Surface.hxx>
47 #include <Geom_TrimmedCurve.hxx>
48 #include <GeomAdaptor_HSurface.hxx>
49 #include <GeomAdaptor_Surface.hxx>
50 #include <GeomFill_LocationDraft.hxx>
51 #include <GeomLProp_SLProps.hxx>
57 #include <gp_Trsf.hxx>
58 #include <Precision.hxx>
59 #include <Standard_NoSuchObject.hxx>
60 #include <StdFail_NotDone.hxx>
61 #include <TColgp_Array1OfPnt.hxx>
62 #include <TColStd_Array1OfReal.hxx>
65 #include <TopExp_Explorer.hxx>
67 #include <TopoDS_Edge.hxx>
68 #include <TopoDS_Iterator.hxx>
69 #include <TopoDS_Shape.hxx>
70 #include <TopoDS_Shell.hxx>
71 #include <TopoDS_Solid.hxx>
72 #include <TopoDS_Wire.hxx>
73 #include <TopTools_ListIteratorOfListOfShape.hxx>
74 #include <TopTools_ListOfShape.hxx>
77 #include <Geom_Circle.hxx>
80 #include <DrawTrSurf.hxx>
81 static Standard_Boolean Affich = 0;
84 //=======================================================================
87 //======================================================================
88 static void ComputeTrsf(const TopoDS_Wire& W,
93 // Calculate approximate barycenter
94 BRepTools_WireExplorer Exp(W);
95 // Class BRep_Tool without fields and without Constructor :
97 gp_XYZ Bary(0.,0.,0.);
100 for (nb=0; Exp.More(); Exp.Next()) {
101 // Bary += BT.Pnt(Exp.CurrentVertex()).XYZ();
102 Bary += BRep_Tool::Pnt(Exp.CurrentVertex()).XYZ();
107 // Calculate the Transformation
109 Tf.SetTransformation(N);
110 BRepAdaptor_Curve AC;
111 // BndLib_Add3dCurve BC;
113 // transformation to the wire
114 TopoDS_Wire TheW = W;
115 TopLoc_Location Loc(Tf);
121 for (Exp.Init(TheW); Exp.More(); Exp.Next()) {
122 AC.Initialize(Exp.Current());
123 // BC.Add(AC, 0.1, Box);
124 BndLib_Add3dCurve::Add(AC, 0.1, Box);
128 //=======================================================================
131 //======================================================================
132 static Standard_Real Longueur(const Bnd_Box& WBox,
137 // face of the box most remoted from the face input in
138 // the direction of skin
139 Standard_Real Xmin,Ymin,Zmin,Xmax,Ymax,Zmax,WZmin,WZmax,L;
142 WBox.Get(Xmin,Ymin,Zmin,Xmax,Ymax,Zmax);
146 SBox.Get(Xmin,Ymin,Zmin,Xmax,Ymax,Zmax);
147 P.SetCoord( (Xmin+Xmax)/2, (Ymin+Ymax)/2, Zmax);
150 // Skin in the wrong direction. Invert...
161 //=======================================================================
162 //function : GoodOrientation
163 //purpose : Check if the law is oriented to have an exterior skin
164 //======================================================================
165 static Standard_Boolean GoodOrientation(const Bnd_Box& B,
166 const Handle(BRepFill_LocationLaw)& Law,
169 Standard_Real f, l, r, t;
170 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
172 B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
173 gp_Pnt P1(aXmin, aYmin, aZmin), P2(aXmax, aYmax, aZmax);
176 Law->CurvilinearBounds(Law->NbLaw(), f, l);
179 Standard_Integer ii, Ind;
181 Standard_Integer Nb = (Standard_Integer) (4+(10*r));
183 // Standard_Integer Nb = 4+(10*r);
187 Nb++; // Number of points
189 TColgp_Array1OfPnt Pnts(1, Nb);
190 Handle(Adaptor3d_HCurve) AC;
191 gp_XYZ Bary(0.,0.,0.);
193 for (ii=1; ii<=Nb; ii++) {
194 Law->Parameter((ii-1)*r, Ind, t);
195 AC = Law->Law(Ind)->GetCurve();
197 Bary+= Pnts(ii).XYZ();
202 gp_Vec Normal(D.XYZ());
203 Standard_Real Angle = 0;
204 gp_Vec Ref(Centre, Pnts(1));
206 for (ii=2; ii<=Nb; ii++) {
207 gp_Vec R(Centre, Pnts(ii));
208 Angle += Ref.AngleWithRef(R, Normal);
215 //=======================================================================
216 //function : Constructeur
218 //======================================================================
219 BRepFill_Draft::BRepFill_Draft(const TopoDS_Shape& S,
221 const Standard_Real Angle)
226 mySections.Nullify();
228 switch (S.ShapeType()) {
231 myWire = TopoDS::Wire(S);
236 TopoDS_Iterator Exp (S);
237 myWire = TopoDS::Wire(Exp.Value());
242 TopTools_ListOfShape List;
243 TopTools_IndexedDataMapOfShapeListOfShape edgemap;
244 TopExp::MapShapesAndAncestors(S,TopAbs_EDGE,TopAbs_FACE,edgemap);
245 Standard_Integer iedge, nbf;
246 for (iedge = 1; iedge <= edgemap.Extent(); iedge++) {
247 const TopoDS_Edge& theEdge = TopoDS::Edge(edgemap.FindKey(iedge));
248 // skip degenerated edges
249 if (!BRep_Tool::Degenerated(theEdge)) {
250 nbf = edgemap(iedge).Extent();
251 if (nbf==1) List.Append(theEdge);
255 if( List.Extent()>0) {
258 BRepLib_WireError Err = MW.Error();
259 if (Err == BRepLib_WireDone) {
264 cout << "Error in MakeWire" << endl;
266 Standard_ConstructionError::Raise("BRepFill_Draft");
271 cout << "No Free Borders !" << endl;
273 Standard_ConstructionError::Raise("BRepFill_Draft");
278 Standard_ConstructionError::Raise("BRepFill_Draft");
281 // Attention to closed non declared wires !
282 if (!myWire.Closed()) {
283 TopoDS_Vertex Vf, Vl;
284 TopExp::Vertices(myWire, Vf, Vl);
285 if (Vf.IsSame(Vl)) myWire.Closed(Standard_True);
289 DBRep::Set("TheWire", myWire);
293 myAngle = Abs(Angle);
296 myDone = Standard_False;
303 //=======================================================================
304 //function :SetOptions
305 //purpose : Defines the style
306 //======================================================================
307 void BRepFill_Draft::SetOptions(const BRepFill_TransitionStyle Style,
308 const Standard_Real Min,
309 const Standard_Real Max)
316 //=======================================================================
319 //======================================================================
320 void BRepFill_Draft::SetDraft(const Standard_Boolean Internal)
322 IsInternal = Internal;
326 //=======================================================================
328 //purpose : calculate a surface of skinning
329 //======================================================================
330 void BRepFill_Draft::Perform(const Standard_Real LengthMax)
332 Handle(Geom_Surface) S;
334 Bnd_Box WBox;//, SBox;
338 ComputeTrsf(myWire, myDir, WBox, Trsf);
339 Init(S, LengthMax, WBox);
344 //=======================================================================
346 //purpose : calculate a surface of skinning
347 //======================================================================
348 void BRepFill_Draft::Perform(const Handle(Geom_Surface)& Surface,
349 const Standard_Boolean KeepInsideSurface)
356 ComputeTrsf(myWire, myDir, WBox, Trsf);
358 // box with bounds of the stop surface
359 Handle(Geom_Surface) Surf;
360 Surf = Handle(Geom_Surface)::DownCast(Surface->Transformed(Trsf));
361 GeomAdaptor_Surface S1 (Surf);
362 // BndLib_AddSurface AS;
363 // AS.Add(S1, 0.1, SBox);
364 BndLib_AddSurface::Add(S1, 0.1, SBox);
366 // calculate the maximum length of the rule.
367 L = Longueur(WBox, SBox, myDir, Pt);
368 L /= Abs(Cos(myAngle));
371 Init(Surface, L, WBox);
372 BuildShell(Surface, !KeepInsideSurface);
376 //================================================================
378 //purpose : calculate the surface of skinning, stopped by a shape
379 //================================================================
380 void BRepFill_Draft::Perform(const TopoDS_Shape& StopShape,
381 const Standard_Boolean KeepOutSide)
388 ComputeTrsf(myWire, myDir, WBox, Trsf);
390 // bounding box of the stop shape
391 Bnd_Box BSurf;//, TheBox;
392 Standard_Real Umin, Umax, Vmin, Vmax;
395 Handle(Geom_Surface) Surf;
397 // BndLib_AddSurface AS;
399 TopExp_Explorer Ex (StopShape, TopAbs_FACE);
402 while (Ex.More()) { // parse faces of the stop shape
403 // B.UVBounds(TopoDS::Face(Ex.Current()), Umin,Umax,Vmin,Vmax);
404 BRepTools::UVBounds(TopoDS::Face(Ex.Current()), Umin,Umax,Vmin,Vmax);
405 Surf = Handle(Geom_Surface)::DownCast(
406 // BT.Surface(TopoDS::Face(Ex.Current()))->Transformed(Trsf) );
407 BRep_Tool::Surface(TopoDS::Face(Ex.Current()))->Transformed(Trsf) );
408 GeomAdaptor_Surface S1 (Surf);
409 // bounding box of the current face
410 // AS.Add(S1, Umin, Umax, Vmin, Vmax, 0.1, BSurf);
411 BndLib_AddSurface::Add(S1, Umin, Umax, Vmin, Vmax, 0.1, BSurf);
412 SBox.Add(BSurf); // group boxes
416 // calculate the maximum length of the rule.
417 L = Longueur(WBox, SBox, myDir, Pt);
418 L /= Abs(Cos(myAngle));
422 Inv = Trsf.Inverted(); // inverted transformation
423 Pt.Transform(Inv); // coordinate in the absolute reference
424 Handle(Geom_Plane) Plan = new (Geom_Plane)(Pt, myDir);
425 Surf = new (Geom_RectangularTrimmedSurface) (Plan,-L, L, -L, L);
429 char* Temp = "ThePlan" ;
430 DrawTrSurf::Set(Temp, Surf);
431 // DrawTrSurf::Set("ThePlan", Surf);
435 // Sweeping and restriction
436 Init(Plan, L*1.01, WBox);
437 BuildShell(Surf, Standard_False);
438 Fuse(StopShape, KeepOutSide);
442 //=======================================================================
444 //purpose : Construction of laws.
445 //======================================================================
446 void BRepFill_Draft::Init(const Handle(Geom_Surface)& ,
447 const Standard_Real Length,
452 // law of positioning
453 Handle(GeomFill_LocationDraft) Loc
454 = new (GeomFill_LocationDraft) (myDir, myAngle);
455 myLoc = new (BRepFill_DraftLaw) (myWire, Loc);
457 B = GoodOrientation(Box, myLoc, myDir);
459 if (IsInternal ^ (!B) ) {
461 Loc->SetAngle(myAngle);
462 myLoc = new (BRepFill_DraftLaw) (myWire, Loc);
465 myLoc->CleanLaw(angmin); // Clean small discontinuities.
468 // generating line is straight and parallel to binormal.
470 gp_Vec D (0., 1., 0.);
472 // Control of the orientation
474 myLoc->Law(1)->GetDomain(f,l);
478 myLoc->Law(1)->D0( (f+l)/2, M, Bid);
479 gp_Dir BN(M.Column(2));
481 Standard_Real ang = myDir.Angle(BN);
482 if (ang > M_PI/2) D.Reverse();
483 Handle(Geom_Line) L = new (Geom_Line) (P, D);
485 Handle(Geom_Curve) TC = new (Geom_TrimmedCurve) (L, 0, Length);
490 TC = new (Geom_Circle) (gp::XOY(), Length);
494 BRepLib_MakeEdge ME(TC);
495 TopoDS_Edge EG = ME.Edge();
497 BRepLib_MakeWire MW(EG);
498 TopoDS_Wire G = MW.Wire();
500 mySec = new (BRepFill_ShapeLaw) (G, Standard_True);
504 //=======================================================================
505 //function : BuildShell
506 //purpose : Construction of the skinning surface
507 //======================================================================
508 void BRepFill_Draft::BuildShell(const Handle(Geom_Surface)& Surf,
509 const Standard_Boolean KeepOutSide)
511 // construction of the surface
512 BRepFill_Sweep Sweep(mySec, myLoc, Standard_True);
513 Sweep.SetTolerance(myTol);
514 Sweep.SetAngularControl(angmin, angmax);
515 TopTools_MapOfShape Dummy;
516 BRepFill_DataMapOfShapeHArray2OfShape Dummy2;
517 BRepFill_DataMapOfShapeHArray2OfShape Dummy3;
518 Sweep.Build(Dummy, Dummy2, Dummy3, myStyle, myCont);
519 if (Sweep.IsDone()) {
520 myShape = Sweep.Shape();
521 myShell = TopoDS::Shell(myShape);
522 myFaces = Sweep.SubShape();
523 mySections = Sweep.Sections();
524 myDone = Standard_True;
525 // Control of the orientation
526 Standard_Boolean out=Standard_True;
527 TopExp_Explorer ex(myShell,TopAbs_FACE);
529 F = TopoDS::Face(ex.Current());
530 BRepAdaptor_Surface SF(F);
534 u = SF.FirstUParameter();
535 v = SF.FirstVParameter();
538 if (F.Orientation() == TopAbs_REVERSED) V.Reverse();
539 if (V.Magnitude() > 1.e-10) {
540 out = myDir.Angle(V) > M_PI/2;
542 if (out == IsInternal) {
548 myDone = Standard_False;
552 if (!Surf.IsNull()) { // Add the face at end
554 // Waiting the use of traces & retriction in BRepFill_Sweep
556 BRepLib_MakeFace MkF;
557 MkF.Init(Surf, Standard_True, Precision::Confusion());
558 Fuse(MkF.Face(), KeepOutSide);
563 //=======================================================================
565 //purpose : Boolean operation between the skin and the
567 //======================================================================
568 Standard_Boolean BRepFill_Draft::Fuse(const TopoDS_Shape& StopShape,
569 const Standard_Boolean KeepOutSide)
572 Standard_Boolean issolid = Standard_False;
573 TopoDS_Solid Sol1, Sol2;
574 TopAbs_State State1 = TopAbs_OUT, State2 = TopAbs_OUT;
577 if (myShape.ShapeType()==TopAbs_SOLID) {
578 Sol1 = TopoDS::Solid(myShape);
579 issolid = Standard_True;
583 B.Add(Sol1, myShape); // shell => solid (for fusion)
587 switch (StopShape.ShapeType()) {
588 case TopAbs_COMPOUND :
590 TopoDS_Iterator It(StopShape);
591 return Fuse(It.Value(), KeepOutSide);
595 Sol2 = TopoDS::Solid(StopShape);
601 B.Add(Sol2, StopShape); // shell => solid (for fusion)
610 S.Closed (BRep_Tool::IsClosed (S));
612 B.Add(Sol2, S); // shell => solid (for fusion)
618 return Standard_False; // Impossible to do
622 BRepAlgo_DSAccess DSA;
623 DSA.Load(Sol1, Sol2);
624 DSA.Intersect(Sol1, Sol2); // intersection of 2 solids
626 // removal of edges corresponding to "unused" intersections
627 Standard_Integer NbPaquet;
630 TopTools_ListOfShape List;
631 List = DSA.GetSectionEdgeSet();// list of edges
633 NbPaquet = List.Extent();
637 cout << "No fusion" << endl;
638 DBRep::Set("DepPart", Sol1);
639 DBRep::Set("StopPart", Sol2);
641 return Standard_False;
645 // It is required to select packs.
646 TColStd_Array1OfReal Dist(1, NbPaquet);
647 TopTools_ListIteratorOfListOfShape it(List);
648 Standard_Real D, Dmin = 1.e10;
651 //Classify the packs by distance.
652 BRepExtrema_DistShapeShape Dist2;
653 Dist2.LoadS1( myWire );
654 for (ii=1; it.More();it.Next(),ii++){
655 Dist2.LoadS2( it.Value() );
657 if (Dist2.IsDone()) {
660 if (D < Dmin) Dmin = D;
666 // remove edges "farther" than Dmin
667 for (ii=1, it.Initialize(List); it.More();it.Next(), ii++){
668 if (Dist(ii) > Dmin) {
669 DSA.SuppressEdgeSet(it.Value());
673 DBRep::Set("KeepEdges", it.Value());
679 if (StopShape.ShapeType() != TopAbs_SOLID) {
680 // It is required to choose the state by the geometry
682 //(1) Return an edge of section
683 List = DSA.GetSectionEdgeSet();// list of edges
684 TopTools_ListIteratorOfListOfShape it(List);
685 TopoDS_Iterator iter(it.Value());
686 TopoDS_Edge E = TopoDS::Edge(iter.Value());
688 //(2) Return geometry on StopShape
689 // Class BRep_Tool without fields and without Constructor :
691 Handle(Geom_Surface) S;
692 Handle(Geom2d_Curve) C2d;
696 // BT.CurveOnSurface(E, C2d, S, L, f, l, 2);
697 BRep_Tool::CurveOnSurface(E, C2d, S, L, f, l, 2);
700 C2d->D0((f+l)/2,P2d);
701 GeomLProp_SLProps SP(S, P2d.X(), P2d.Y(), 1, 1.e-12);
702 if (! SP.IsNormalDefined()) {
703 C2d->D0((3*f+l)/4,P2d);
704 SP.SetParameters(P2d.X(), P2d.Y());
705 if ( !SP.IsNormalDefined()) {
706 C2d->D0((f+3*l)/4,P2d);
707 SP.SetParameters(P2d.X(), P2d.Y());
712 if (myDir.Angle(SP.Normal()) < M_PI/2) State1 = TopAbs_IN;
713 else State1 = TopAbs_OUT;
716 if (! KeepOutSide) { // Invert State2;
717 if (State2 == TopAbs_IN) State2 = TopAbs_OUT;
718 else State2 = TopAbs_IN;
721 //recalculate the final shape
722 TopoDS_Shape result = DSA.Merge(State1, State2);
724 if (issolid) myShape = result;
727 Exp.Init(result, TopAbs_SHELL);
728 if (Exp.More()) myShape = Exp.Current();
731 // Update the History
733 for (ii=1; ii<=myLoc->NbLaw(); ii++) {
734 const TopTools_ListOfShape& L = DSA.Modified(myFaces->Value(1,ii));
736 myFaces->SetValue(1, ii, L.First());
738 for (ii=1; ii<=myLoc->NbLaw()+1; ii++) {
739 const TopTools_ListOfShape& L = DSA.Modified(mySections->Value(1,ii));
741 mySections->SetValue(1, ii, L.First());
744 return Standard_True;
747 //=======================================================================
749 //purpose : Assemble the skin with the above face
750 //======================================================================
751 Standard_Boolean BRepFill_Draft::Sewing()
753 Standard_Boolean ToAss;
754 Standard_Boolean Ok = Standard_False;
755 ToAss = (myTop.ShapeType() != TopAbs_WIRE);
757 if ((!ToAss) || (!myDone)) return Standard_False;
759 // Assembly make a shell from the faces of the shape + the input shape
760 Handle(BRepBuilderAPI_Sewing) Ass = new BRepBuilderAPI_Sewing(5*myTol, Standard_True,
761 Standard_True, Standard_False);
764 ToAss = Standard_True;
767 Standard_Integer NbCE;
770 // Check if the assembly is real.
771 NbCE = Ass->NbContigousEdges();
775 res = Ass->SewedShape();
776 if ((res.ShapeType() == TopAbs_SHELL)||
777 (res.ShapeType() == TopAbs_SOLID)) {
781 else if (res.ShapeType() == TopAbs_COMPOUND) {
782 TopoDS_Iterator It(res);
785 if (!It.More()) {//Only one part => this is correct
793 // Update the History
795 for (ii=1; ii<=myLoc->NbLaw(); ii++) {
796 if (Ass->IsModified(myFaces->Value(1,ii)))
797 myFaces->SetValue(1, ii,
798 Ass->Modified(myFaces->Value(1,ii)));
800 for (ii=1; ii<=myLoc->NbLaw()+1; ii++) {
801 if (Ass->IsModified(mySections->Value(1,ii)))
802 mySections->SetValue(1, ii,
803 Ass->Modified(mySections->Value(1,ii)));
806 if (myShape.Closed()) { // Make a Solid
810 BS.Add(solid,TopoDS::Shell(myShape));
812 BRepClass3d_SolidClassifier SC(solid);
813 SC.PerformInfinitePoint(Precision::Confusion());
814 if ( SC.State() == TopAbs_IN) {
817 BS.Add(solid,TopoDS::Shell(myShape));
823 else cout << "Draft : No assembly !" << endl;
828 //=======================================================================
829 //function : Generated
830 //purpose : return a sub-part generated by sweeping
831 //======================================================================
832 const TopTools_ListOfShape&
833 BRepFill_Draft::Generated(const TopoDS_Shape& S)
840 for (ii=0; ii<=myLoc->NbLaw(); ii++)
841 if (E.IsSame(myLoc->Vertex(ii))) {
842 myGenerated.Append(mySections->Value(1, ii+1));
847 for (ii=1; ii<=myLoc->NbLaw(); ii++)
848 if (E.IsSame(myLoc->Edge(ii))) {
849 myGenerated.Append(myFaces->Value(1, ii));
857 //=======================================================================
859 //purpose : return the complete shape
860 //======================================================================
861 TopoDS_Shape BRepFill_Draft::Shape()const
866 //=====================================================================
868 //purpose : surface of skinning with the input face (=>shell)
869 //=====================================================================
870 TopoDS_Shell BRepFill_Draft::Shell()const
875 //=======================================================================
878 //======================================================================
879 Standard_Boolean BRepFill_Draft::IsDone()const