0022646: Error in algorithms BRepOffsetAPI_ThruSections and BRepOffsetAPI_MakePipeShe...
[occt.git] / src / BRepFill / BRepFill_CompatibleWires.cxx
CommitLineData
7fd59977 1// File: BRepFill_CompatibleWires.cxx
2// Created: Thu Jul 2 16:47:25 1998
3// Author: Joelle CHAUVET
4// <jct@sgi64>
5
6
7#include <BRepFill_CompatibleWires.ixx>
8
9#include <BRepAdaptor_Curve.hxx>
10
11#include <BRepFill.hxx>
12#include <BRepLib.hxx>
13#include <BRepLib_FindSurface.hxx>
14#include <BRepLib_MakeWire.hxx>
15#include <BRepLib_MakeEdge.hxx>
d7325741 16#include <BRepCheck_Wire.hxx>
7fd59977 17#include <BRepExtrema_DistShapeShape.hxx>
18#include <Bnd_Box.hxx>
19#include <BRepBndLib.hxx>
20#include <BRep_Builder.hxx>
21#include <BRep_Tool.hxx>
22#include <BRepTools_WireExplorer.hxx>
23#include <BRepLProp.hxx>
24#include <BRepGProp.hxx>
25#include <GProp_GProps.hxx>
26#include <GProp_PrincipalProps.hxx>
27#include <Geom_Surface.hxx>
28#include <Geom_Plane.hxx>
29#include <Precision.hxx>
30#include <TopAbs.hxx>
31#include <TopExp.hxx>
32#include <TopExp_Explorer.hxx>
33#include <TopoDS.hxx>
34#include <TopoDS_Wire.hxx>
35#include <TopTools_ListIteratorOfListOfShape.hxx>
36#include <TopTools_DataMapOfShapeListOfShape.hxx>
37#include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
38#include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
39#include <TopTools_HSequenceOfShape.hxx>
40#include <TopTools_SequenceOfShape.hxx>
41#include <TopTools_ListOfShape.hxx>
42
43#include <gp.hxx>
44#include <gp_Vec.hxx>
45#include <gp_Ax2.hxx>
46#include <gp_Pln.hxx>
47#include <gp_Circ.hxx>
48#include <gp_Elips.hxx>
49#include <TColgp_HArray1OfVec.hxx>
50#include <TColgp_HArray1OfPnt.hxx>
51
52#include <TColStd_Array1OfInteger.hxx>
53#include <TColStd_Array1OfReal.hxx>
54#include <TColStd_SequenceOfReal.hxx>
55
56
57static void EdgesFromVertex (const TopoDS_Wire& W,
58 const TopoDS_Vertex& V,
59 TopoDS_Edge& E1,
60 TopoDS_Edge& E2)
61{
62 TopTools_IndexedDataMapOfShapeListOfShape Map;
63 TopExp::MapShapesAndAncestors(W,TopAbs_VERTEX,TopAbs_EDGE,Map);
64
65 const TopTools_ListOfShape& List = Map.FindFromKey(V);
66 TopoDS_Edge e1 = TopoDS::Edge(List.First());
67 TopoDS_Edge e2 = TopoDS::Edge(List. Last());
68
69 BRepTools_WireExplorer anExp;
70 Standard_Integer I1=0, I2=0, NE=0;
71
72 for(anExp.Init(W); anExp.More(); anExp.Next()) {
73 NE++;
74 const TopoDS_Edge& ECur = anExp.Current();
75 if (e1.IsSame(ECur)) {
76 I1 = NE;
77 }
78 if (e2.IsSame(ECur)) {
79 I2 = NE;
80 }
81 }
82
83 if (Abs(I2-I1)==1) {
84 // numeros consecutifs
85 if (I2==I1+1) {
86 E1 = e1;
87 E2 = e2;
88 }
89 else {
90 E1 = e2;
91 E2 = e1;
92 }
93 }
94 else {
95 // numeros non consecutifs sur un wire ferme
96 if (I1==1&&I2==NE) {
97 E1 = e2;
98 E2 = e1;
99 }
100 else {
101 E1 = e1;
102 E2 = e2;
103 }
104 }
105}
106
107
108static void SeqOfVertices (const TopoDS_Wire& W,
109 TopTools_SequenceOfShape& S)
110{
111 S.Clear();
112 Standard_Integer jj, cpt = 0;
113 TopExp_Explorer PE;
114 for (PE.Init(W,TopAbs_VERTEX); PE.More(); PE.Next()) {
115 cpt++;
116 Standard_Boolean trouve=Standard_False;
117 for (jj=1;jj<=S.Length() && (!trouve);jj++) {
118 if (S.Value(jj).IsSame(PE.Current())) trouve = Standard_True;
119 }
120 if (!trouve) S.Append(PE.Current());
121 }
122}
123
124
125static Standard_Boolean PlaneOfWire (const TopoDS_Wire& W, gp_Pln& P)
126{
127 Standard_Boolean isplane = Standard_True;
128 BRepLib_FindSurface findPlanarSurf;
129 Handle(Geom_Surface) S;
130 TopLoc_Location L;
131
132 GProp_GProps GP;
133 BRepGProp::LinearProperties(W,GP);
134 gp_Pnt Bary = GP.CentreOfMass();
135
136// blindage pour les cas particuliers : 1 seule edge cercle ou ellipse
137// sur un wire ferme !
138 Standard_Integer nbEdges = 0;
139 BRepTools_WireExplorer anExp;
140 anExp.Init(W);
141 Standard_Boolean wClosed = W.Closed();
142 if (!wClosed) {
143 // on regarde quand meme si les vertex sont les memes.
144 TopoDS_Vertex V1, V2;
145 TopExp::Vertices(W,V1,V2);
146 if ( V1.IsSame(V2)) wClosed = Standard_True;
147 }
148 TopoDS_Edge Edge = TopoDS::Edge(anExp.Current());
149 Standard_Real first, last;
150 TopLoc_Location loc;
151 Handle(Geom_Curve) curv;
152 curv = BRep_Tool::Curve(Edge, loc, first, last);
153 curv =
154 Handle(Geom_Curve)::DownCast(curv->Transformed(loc.Transformation()));
155 if (wClosed) {
156 GeomAdaptor_Curve AdC;
157 AdC.Load(curv);
158 for(; anExp.More(); anExp.Next()) {
159 nbEdges ++;
160 }
161 if ( nbEdges==1 && AdC.GetType() == GeomAbs_Circle ) {
162 Bary = AdC.Circle().Location();
163 }
164 if ( nbEdges==1 && AdC.GetType() == GeomAbs_Ellipse ) {
165 Bary = AdC.Ellipse().Location();
166 }
167 }
168
169 findPlanarSurf.Init(W, -1, Standard_True);
170 if ( findPlanarSurf.Found()) {
171 S = findPlanarSurf.Surface();
172 L = findPlanarSurf.Location();
173 if (!L.IsIdentity()) S = Handle(Geom_Surface)::
174 DownCast(S->Transformed(L.Transformation()));
175 P = (Handle(Geom_Plane)::DownCast(S))->Pln();
176 P.SetLocation(Bary);
177 }
178 else {
179 // wire non plan !
180 GProp_PrincipalProps Pp = GP.PrincipalProperties();
181 gp_Vec Vec;
182 Standard_Real R1, R2, R3,Tol = Precision::Confusion();
183 Pp.RadiusOfGyration(R1,R2,R3);
184 Standard_Real RMax = Max(Max(R1,R2),R3);
185 if ( ( Abs(RMax-R1)<Tol && Abs(RMax-R2)<Tol )
186 || ( Abs(RMax-R1)<Tol && Abs(RMax-R3)<Tol )
187 || ( Abs(RMax-R2)<Tol && Abs(RMax-R3)<Tol ) )
188 isplane = Standard_False;
189 else {
190 if (R1>=R2 && R1>=R3) {
191 Vec = Pp.FirstAxisOfInertia();
192 }
193 else if (R2>=R1 && R2>=R3) {
194 Vec = Pp.SecondAxisOfInertia();
195 }
196 else if (R3>=R1 && R3>=R2) {
197 Vec = Pp.ThirdAxisOfInertia();
198 }
199 gp_Dir NDir(Vec);
200 if (R3<=R2 && R3<=R1) {
201 Vec = Pp.ThirdAxisOfInertia();
202 }
203 else if (R2<=R1 && R2<=R3) {
204 Vec = Pp.SecondAxisOfInertia();
205 }
206 else if (R1<=R2 && R1<=R3) {
207 Vec = Pp.FirstAxisOfInertia();
208 }
209 gp_Dir XDir(Vec);
210 gp_Ax3 repere(Bary,NDir,XDir);
211 Geom_Plane GPlan(repere);
212 P = GPlan.Pln();
213 }
214 }
215
216 return isplane;
217
218}
219
220
221static void WireContinuity (const TopoDS_Wire& W,
222 GeomAbs_Shape& contW)
223{
224 contW = GeomAbs_CN;
225 GeomAbs_Shape cont;
226 Standard_Boolean IsDegenerated = Standard_False;
227
228 BRepTools_WireExplorer anExp;
229 Standard_Integer nbEdges=0;
230 Handle(TopTools_HSequenceOfShape) Edges = new TopTools_HSequenceOfShape();
231 for(anExp.Init(W); anExp.More(); anExp.Next()) {
232 nbEdges++;
233 Edges->Append(anExp.Current());
234 if (BRep_Tool::Degenerated(anExp.Current())) IsDegenerated = Standard_True;
235 }
236
237 if (!IsDegenerated) {
238
239 Standard_Boolean testconti = Standard_True;
240
241 for (Standard_Integer j=1;j<=nbEdges;j++) {
242
243 TopoDS_Edge Edge1, Edge2;
244
245 if (j == nbEdges) {
246 Edge1 = TopoDS::Edge (Edges->Value(nbEdges));
247 Edge2 = TopoDS::Edge (Edges->Value(1));
248 }
249 else {
250 Edge1 = TopoDS::Edge (Edges->Value(j));
251 Edge2 = TopoDS::Edge (Edges->Value(j+1));
252 }
253
254 TopoDS_Vertex V1,V2,Vbid;
255 TopExp::Vertices(Edge1,Vbid,V1,Standard_True);
256 TopExp::Vertices(Edge2,V2,Vbid,Standard_True);
257 Standard_Real U1 = BRep_Tool::Parameter(V1,Edge1);
258 Standard_Real U2 = BRep_Tool::Parameter(V2,Edge2);
259 BRepAdaptor_Curve Curve1(Edge1);
260 BRepAdaptor_Curve Curve2(Edge2);
261 Standard_Real Eps = BRep_Tool::Tolerance(V2) + BRep_Tool::Tolerance(V1);
262
263 if(j == nbEdges)
264 testconti = Curve1.Value(U1).IsEqual(Curve2.Value(U2), Eps);
265
266 if(testconti) {
267 cont = BRepLProp::Continuity(Curve1,Curve2,U1,U2,
268 Eps, Precision::Angular());
269 if (cont <= contW) contW = cont;
270 }
271 }
272 }
273
274}
275
276static void TrimEdge (const TopoDS_Edge& CurrentEdge,
277 const TColStd_SequenceOfReal& CutValues,
278 const Standard_Real t0, const Standard_Real t1,
279 const Standard_Boolean SeqOrder,
280 TopTools_SequenceOfShape& S)
281
282{
283 S.Clear();
284 Standard_Integer j, ndec=CutValues.Length();
285 Standard_Real first,last,m0,m1;
286 Handle(Geom_Curve) C = BRep_Tool::Curve(CurrentEdge,first,last);
287
288 TopoDS_Vertex Vf,Vl,Vbid,V0,V1;
289 TopAbs_Orientation CurrentOrient = CurrentEdge.Orientation();
290 TopExp::Vertices(CurrentEdge,Vf,Vl);
291 Vbid.Nullify();
292
293 if (SeqOrder) {
294 // de first vers last
295 m0 = first;
296 V0 = Vf;
297 for (j=1; j<=ndec; j++) {
298 // morceau d'edge
299 m1 = (CutValues.Value(j)-t0)*(last-first)/(t1-t0)+first;
300 TopoDS_Edge CutE = BRepLib_MakeEdge(C,V0,Vbid,m0,m1);
301 CutE.Orientation(CurrentOrient);
302 S.Append(CutE);
303 m0 = m1;
304 V0 = TopExp::LastVertex(CutE);
305 if (j==ndec) {
306 // dernier morceau
307 TopoDS_Edge LastE = BRepLib_MakeEdge(C,V0,Vl,m0,last);
308 LastE.Orientation(CurrentOrient);
309 S.Append(LastE);
310 }
311 }
312 }
313 else {
314 // de last vers first
315 m1 = last;
316 V1 = Vl;
317 for (j=ndec; j>=1; j--) {
318 // morceau d'edge
319 m0 = (CutValues.Value(j)-t0)*(last-first)/(t1-t0)+first;
320 TopoDS_Edge CutE = BRepLib_MakeEdge(C,Vbid,V1,m0,m1);
321 CutE.Orientation(CurrentOrient);
322 S.Append(CutE);
323 m1 = m0;
324 V1 = TopExp::FirstVertex(CutE);
325 if (j==1) {
326 // dernier morceau
327 TopoDS_Edge LastE = BRepLib_MakeEdge(C,Vf,V1,first,m1);
328 LastE.Orientation(CurrentOrient);
329 S.Append(LastE);
330 }
331 }
332 }
333}
334
335
336
337static Standard_Boolean SearchRoot (const TopoDS_Vertex& V,
338 const TopTools_DataMapOfShapeListOfShape& Map,
339 TopoDS_Vertex& VRoot)
340{
341 Standard_Boolean trouve = Standard_False;
342 VRoot.Nullify();
343 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape it;
344 for (it.Initialize(Map); it.More(); it.Next()) {
345 const TopTools_ListOfShape & List = it.Value();
346 TopTools_ListIteratorOfListOfShape itL;
347 Standard_Boolean ilyest = Standard_False;
348 for (itL.Initialize(List); itL.More(); itL.Next()) {
349 TopoDS_Vertex Vcur = TopoDS::Vertex(itL.Value());
350 if (Vcur.IsSame(V)) {
351 ilyest = Standard_True;
352 }
353 if (ilyest) break;
354 }
355 if (ilyest) {
356 trouve = Standard_True;
357 VRoot = TopoDS::Vertex(it.Key());
358 }
359 if (trouve) break;
360 }
361 return trouve;
362}
363
364static Standard_Boolean SearchVertex (const TopTools_ListOfShape& List,
365 const TopoDS_Wire& W,
366 TopoDS_Vertex& VonW)
367{
368 Standard_Boolean trouve = Standard_False;
369 VonW.Nullify();
370 TopTools_SequenceOfShape SeqV;
371 SeqOfVertices(W,SeqV);
372 for (Standard_Integer ii=1;ii<=SeqV.Length();ii++) {
373 TopoDS_Vertex Vi = TopoDS::Vertex(SeqV.Value(ii));
374 TopTools_ListIteratorOfListOfShape itL;
375 Standard_Boolean ilyest = Standard_False;
376 for (itL.Initialize(List); itL.More(); itL.Next()) {
377 TopoDS_Vertex Vcur = TopoDS::Vertex(itL.Value());
378 if (Vcur.IsSame(Vi)) {
379 ilyest = Standard_True;
380 }
381 if (ilyest) break;
382 }
383 if (ilyest) {
384 trouve = Standard_True;
385 VonW = Vi;
386 }
387 if (trouve) break;
388 }
389 return trouve;
390}
391
392
393static Standard_Boolean EdgeIntersectOnWire (const gp_Pnt& P1,
394 const gp_Pnt& P2,
395 Standard_Real percent,
396 const TopTools_DataMapOfShapeListOfShape& Map,
397 const TopoDS_Wire& W,
398 TopoDS_Vertex& Vsol,
399 TopoDS_Wire& newW)
400{
401
402 BRepTools_WireExplorer anExp;
403
404 // construction de l'edge d'intersection
405 Standard_Boolean NewVertex = Standard_False;
406 gp_Lin droite(P1,gp_Dir(gp_Vec(P1,P2)));
407 // ATTENTION : en toute rigueur, il faudrait construire une demi-droite
408 // mais il y a un bug dans BRepExtrema_DistShapeShape
409 // on se contente de 100 * la distance entre P1 et P2
410 // en esperant que ce soit suffisant jusqu'a ce que le bug
411 // soit corrige
412 // Standard_Real dernierparam = Precision::Infinite();
413 // ATTENTION : le retour !!
414 // 100 c'est mieux que 10 mais quelquefois c'est trop !
415 // finalement, rien ne vaut une bonne boite d'encombrement
416 // Standard_Real dernierparam = 100 * P1.Distance(P2);
417 Bnd_Box B;
418 BRepBndLib::Add(W,B);
419 Standard_Real x1,x2,y1,y2,z1,z2;
420 B.Get(x1,y1,z1,x2,y2,z2);
421 gp_Pnt BP1(x1,y1,z1), BP2(x2,y2,z2);
422 Standard_Real diag = BP1.Distance(BP2);
423 Standard_Real dernierparam = diag;
424 BRepLib_MakeEdge ME(droite,0.,dernierparam);
425 TopoDS_Edge ECur = BRepLib_MakeEdge(droite,0.,P1.Distance(P2));
426
427 // calcul de l'intersection par BRepExtrema (point de distance mini)
428 BRepExtrema_DistShapeShape DSS(ME.Edge(),W);
429 if (DSS.IsDone()) {
430 // on choisit la solution la plus proche de P2
431 Standard_Integer isol = 1;
432 Standard_Real dss = P2.Distance(DSS.PointOnShape2(isol));
433 for (Standard_Integer iss=2; iss<=DSS.NbSolution(); iss++) {
434 if (dss>P2.Distance(DSS.PointOnShape2(iss))) {
435 dss = P2.Distance(DSS.PointOnShape2(iss));
436 isol = iss;
437 }
438 }
439#ifdef DEB
440 gp_Pnt Psol =
441#endif
442 DSS.PointOnShape2(isol);
443 // la solution est-elle un nouveau vertex ?
444 NewVertex = (DSS.SupportTypeShape2(isol) != BRepExtrema_IsVertex);
445 if (NewVertex) {
446 TopoDS_Shape aLocalShape = DSS.SupportOnShape2(isol);
447 TopoDS_Edge E = TopoDS::Edge(aLocalShape);
448// TopoDS_Edge E = TopoDS::Edge(DSS.SupportOnShape2(isol));
449 Standard_Real tol = Precision::PConfusion();
450 Standard_Real first,last,param;
451 BRep_Tool::Range(E,first,last);
452 tol = Max(tol,percent*Abs(last-first));
453 DSS.ParOnEdgeS2(isol,param);
454 if (Abs(first-param)<tol) {
455 NewVertex = Standard_False;
456 Vsol = TopExp::FirstVertex(E);
457 }
458 else if (Abs(last-param)<tol) {
459 NewVertex = Standard_False;
460 Vsol = TopExp::LastVertex(E);
461 }
462 // verif
463 if (!NewVertex) {
464 TopoDS_Vertex VRoot;
465 if (SearchRoot(Vsol,Map,VRoot)) NewVertex = Standard_True;
466 }
467 }
468 else {
469 TopoDS_Shape aLocalShape = DSS.SupportOnShape2(isol);
470 Vsol = TopoDS::Vertex(aLocalShape);
471// Vsol = TopoDS::Vertex(DSS.SupportOnShape2(isol));
472 }
473
474 // il faut decouper l'edge
475 if (NewVertex) {
476 TopoDS_Shape aLocalShape = DSS.SupportOnShape2(isol);
477 TopoDS_Edge E = TopoDS::Edge(aLocalShape);
478// TopoDS_Edge E = TopoDS::Edge(DSS.SupportOnShape2(isol));
479#ifdef DEB
480 Standard_Real tol =
481#endif
482 Precision::PConfusion();
483 Standard_Real first,last,param;
484 DSS.ParOnEdgeS2(isol,param);
485 BRep_Tool::Range(E,first,last);
486 BRepLib_MakeWire MW;
487 for (anExp.Init(W); anExp.More(); anExp.Next()) {
488 if (E.IsSame(anExp.Current())) {
489 Standard_Boolean SO
490 = (anExp.CurrentVertex().IsSame(TopExp::FirstVertex(E)));
491 TopTools_SequenceOfShape SE;
492 SE.Clear();
493 TColStd_SequenceOfReal SR;
494 SR.Clear();
495 SR.Append(param);
496 TrimEdge(E,SR,first,last,SO,SE);
497 TopoDS_Vertex VV1,VV2;
498 TopExp::Vertices(TopoDS::Edge(SE.Value(1)),VV1,VV2);
499 if (TopExp::FirstVertex(E).IsSame(VV1)
500 || TopExp::LastVertex(E).IsSame(VV1)) {
501 Vsol = VV2;
502 }
503 if (TopExp::FirstVertex(E).IsSame(VV2)
504 || TopExp::LastVertex(E).IsSame(VV2)) {
505 Vsol = VV1;
506 }
507 for (Standard_Integer k=1; k<=SE.Length(); k++) {
508 MW.Add(TopoDS::Edge(SE.Value(k)));
509 }
510 }
511 else {
512 MW.Add(anExp.Current());
513 }
514 }
515 newW = MW.Wire();
516 }
517 else {
518 newW = W;
519 }
520
521
522 }
523
524 return NewVertex;
525
526}
527
528
529static void Transform (const Standard_Boolean WithRotation,
530 const gp_Pnt& P,
531 const gp_Pnt& Pos1,
532 const gp_Vec& Ax1,
533 const gp_Pnt& Pos2,
534 const gp_Vec& Ax2,
535 gp_Pnt& Pnew)
536{
537
538 Pnew = P.Translated (Pos1,Pos2);
539 gp_Vec axe1 = Ax1, axe2 = Ax2;
540 if (!axe1.IsParallel(axe2,1.e-4)) {
541 gp_Vec Vtrans(Pos1,Pos2),Vsign;
542 Standard_Real alpha,beta,sign=1;
543 alpha = Vtrans.Dot(axe1);
544 beta = Vtrans.Dot(axe2);
545 if (alpha<-1.e-7) axe1 *=-1;
546 if (beta<1.e-7) axe2 *=-1;
547 alpha = Vtrans.Dot(axe1);
548 beta = Vtrans.Dot(axe2);
549 gp_Vec norm2 = axe1 ^ axe2;
550 Vsign.SetLinearForm(Vtrans.Dot(axe1),axe2,-Vtrans.Dot(axe2),axe1);
551 alpha = Vsign.Dot(axe1);
552 beta = Vsign.Dot(axe2);
553 Standard_Boolean pasnul = (Abs(alpha)>1.e-4 && Abs(beta)>1.e-4);
554 if ( alpha*beta>0.0 && pasnul ) sign=-1;
555 gp_Ax1 Norm(Pos2,norm2);
556 Standard_Real ang = axe1.AngleWithRef(axe2,norm2);
557 if (!WithRotation) {
558 if (ang>PI/2) ang = ang - PI;
559 if (ang<-PI/2) ang = ang + PI;
560 }
561 ang *= sign;
562 Pnew = Pnew.Rotated (Norm,ang);
563 }
564}
565
566static void BuildConnectedEdges(const TopoDS_Wire& aWire,
567 const TopoDS_Edge& StartEdge,
568 const TopoDS_Vertex& StartVertex,
569 TopTools_ListOfShape& ConnectedEdges)
570{
571 TopTools_IndexedDataMapOfShapeListOfShape MapVE;
572 TopExp::MapShapesAndAncestors(aWire, TopAbs_VERTEX, TopAbs_EDGE, MapVE);
573 TopoDS_Edge CurEdge = StartEdge;
574 TopoDS_Vertex CurVertex = StartVertex;
575 TopoDS_Vertex Origin, V1, V2;
576 TopExp::Vertices(StartEdge, V1, V2);
577 Origin = (V1.IsSame(StartVertex))? V2 : V1;
578
579 for (;;)
580 {
581 TopTools_ListIteratorOfListOfShape itE( MapVE.FindFromKey(CurVertex) );
582 for (; itE.More(); itE.Next())
583 {
584 TopoDS_Edge anEdge = TopoDS::Edge(itE.Value());
585 if (!anEdge.IsSame(CurEdge))
586 {
587 ConnectedEdges.Append(anEdge);
588 TopExp::Vertices(anEdge, V1, V2);
589 CurVertex = (V1.IsSame(CurVertex))? V2 : V1;
590 CurEdge = anEdge;
591 break;
592 }
593 }
594 if (CurVertex.IsSame(Origin))
595 break;
596 }
597}
598
599//=======================================================================
600//function : BRepFill_CompatibleWires
601//purpose :
602//=======================================================================
603
604BRepFill_CompatibleWires::BRepFill_CompatibleWires()
605:myIsDone(Standard_False)
606{
607}
608
609
610//=======================================================================
611//function : BRepFill_CompatibleWires
612//purpose :
613//=======================================================================
614
615BRepFill_CompatibleWires::BRepFill_CompatibleWires(const TopTools_SequenceOfShape& Sections)
616{
617 Init(Sections);
618}
619
620
621//=======================================================================
622//function : Init
623//purpose :
624//=======================================================================
625
626void BRepFill_CompatibleWires::Init(const TopTools_SequenceOfShape& Sections)
627{
628 myInit = Sections;
629 myWork = Sections;
630 myPercent = 0.01;
631 myIsDone = Standard_False;
632 myMap.Clear();
633
634}
635
636
637//=======================================================================
638//function : SetPercent
639//purpose :
640//=======================================================================
641
642void BRepFill_CompatibleWires::SetPercent(const Standard_Real Percent)
643{
644 if (0.<Percent && Percent<1.) myPercent = Percent;
645
646}
647
648
649//=======================================================================
650//function : IsDone
651//purpose :
652//=======================================================================
653
654Standard_Boolean BRepFill_CompatibleWires::IsDone() const
655{
656 return myIsDone;
657}
658
659
660//=======================================================================
661//function : Shape
662//purpose :
663//=======================================================================
664
665const TopTools_SequenceOfShape& BRepFill_CompatibleWires::Shape() const
666{
667 return myWork;
668}
669
670
671//=======================================================================
672//function : GeneratedShapes
673//purpose :
674//=======================================================================
675
676const TopTools_ListOfShape& BRepFill_CompatibleWires::GeneratedShapes
677(const TopoDS_Edge& SubSection) const
678{
679
680 if (myMap.IsBound(SubSection)) {
681 return myMap(SubSection);
682 }
683 else {
684 static TopTools_ListOfShape Empty;
685 return Empty;
686 }
687}
688
689
690//=======================================================================
691//function : Perform
692//purpose :
693//=======================================================================
694
695void BRepFill_CompatibleWires::Perform (const Standard_Boolean WithRotation)
696{
697 // compute origin and orientation on wires to avoid twisted results
698 // and update wires to have same number of edges
699
700 // determination de report:
701 // si le nombre d'elements est identique et si les wires ont des discontinuites
702 // en tangence, on n'effectue pas le report par abscisse curviligne, ni
703 Standard_Integer nbSects = myWork.Length(), i;
704 BRepTools_WireExplorer anExp;
705 Standard_Integer nbmax=0, nbmin=0;
706 TColStd_Array1OfInteger nbEdges(1,nbSects);
707 Standard_Boolean report;
708 GeomAbs_Shape contS=GeomAbs_CN;
709 GeomAbs_Shape cont;
710 for (i=1; i<=nbSects; i++) {
711 TopoDS_Shape aLocalShape = myWork(i).Oriented(TopAbs_FORWARD);
712 myWork(i) = TopoDS::Wire(aLocalShape);
713// myWork(i) = TopoDS::Wire(myWork(i).Oriented(TopAbs_FORWARD));
714 TopoDS_Wire W = TopoDS::Wire(myWork(i));
715 WireContinuity(W,cont);
716 if (cont<contS) contS=cont;
717 nbEdges(i) = 0;
718 for(anExp.Init(W); anExp.More(); anExp.Next() ) nbEdges(i)++;
719 if (i==1) nbmin = nbEdges(i);
720 if (nbmax<nbEdges(i)) nbmax = nbEdges(i);
721 if (nbmin>nbEdges(i)) nbmin = nbEdges(i);
722 }
723 // si on n'a pas le meme nombre d'elements ou si tous les wires sont au moins
724 // C1, on effectue le report par abscisse curviligne des decoupes sinon, on se
725 // fait un report vertex / Vertex
726 report = (nbmax != nbmin || contS >= GeomAbs_C1 );
727
728 // initialisation de la map
729 Standard_Integer nbE = 0;
730 TopTools_ListOfShape Empty;
731 for (i=1; i<=nbSects; i++) {
732 TopoDS_Wire W = TopoDS::Wire(myWork(i));
733 for(anExp.Init(W); anExp.More(); anExp.Next() ) {
734 TopoDS_Edge E = TopoDS::Edge(anExp.Current());
735 myMap.Bind(E,Empty);
736 myMap(E).Append(E);
737 nbE++;
738 }
739 }
740
741 // sections ouvertes / sections fermees
742 // initialisation de myDegen1, myDegen2
743 Standard_Integer ideb=1, ifin=myWork.Length();
744 // on regarde si le premier wire est ponctuel
745 myDegen1 = Standard_True;
746 for(anExp.Init(TopoDS::Wire(myWork(ideb))); anExp.More(); anExp.Next()) {
747 myDegen1 = myDegen1 && (BRep_Tool::Degenerated(anExp.Current()));
748 }
749 if (myDegen1) ideb++;
750 // on regarde si le dernier wire est ponctuel
751 myDegen2 = Standard_True;
752 for(anExp.Init(TopoDS::Wire(myWork(ifin))); anExp.More(); anExp.Next()) {
753 myDegen2 = myDegen2 && (BRep_Tool::Degenerated(anExp.Current()));
754 }
755 if (myDegen2) ifin--;
756
757 Standard_Boolean wClosed, allClosed = Standard_True, allOpen = Standard_True;
758 for (i=ideb; i<=ifin; i++) {
759 wClosed = myWork(i).Closed();
760 if (!wClosed) {
761 // on regarde quand meme si les vertex sont les memes.
762 TopoDS_Vertex V1, V2;
763 TopExp::Vertices(TopoDS::Wire(myWork(i)),V1,V2);
764 if ( V1.IsSame(V2)) wClosed = Standard_True;
765 }
766 allClosed = (allClosed && wClosed);
767 allOpen = (allOpen && !wClosed);
768 }
769
770 if (allClosed) {
771 // Toutes les sections sont fermees
772 if (report) {
773 // same number of elements
774 SameNumberByPolarMethod(WithRotation);
775 }
776 else {
777 // origine
778 ComputeOrigin(Standard_False);
779 }
780 myIsDone = Standard_True;
781 }
782 else if (allOpen) {
783 // Toutes les sections sont ouvertes
784 // origine
785 SearchOrigin();
786 // same number of elements
787 if (report) {
788 SameNumberByACR(report);
789 }
790 myIsDone = Standard_True;
791 }
792 else {
793 // Il y a des sections ouvertes et des sections fermees :
794 // on ne traite pas
795 Standard_DomainError::Raise("Sections must be all closed or all open");
796 }
797
798}
799
800
801
802
803//=======================================================================
804//function : Generated
805//purpose :
806//=======================================================================
807
808const TopTools_DataMapOfShapeListOfShape& BRepFill_CompatibleWires::Generated() const
809{
810 return myMap;
811}
812
813
814//=======================================================================
815//function : SameNumberByPolarMethod
816//purpose :
817//=======================================================================
818
819void BRepFill_CompatibleWires::
820 SameNumberByPolarMethod(const Standard_Boolean WithRotation)
821{
822
823 // initialisation
824 Standard_Integer NbSects=myWork.Length();
825 BRepTools_WireExplorer anExp;
826 TopoDS_Vertex V1, V2;
827
828 Standard_Boolean allClosed = Standard_True;
829 Standard_Integer i,ii,ideb=1,ifin=NbSects;
830 for (i=1; i<=NbSects; i++) {
d7325741
J
831 Handle(BRepCheck_Wire) Checker = new BRepCheck_Wire(TopoDS::Wire(myWork(i)));
832 allClosed = (allClosed && (Checker->Closed() == BRepCheck_NoError));
833 //allClosed = (allClosed && myWork(i).Closed());
7fd59977 834 }
835 if (!allClosed)
836 Standard_NoSuchObject::Raise("BRepFill_CompatibleWires::SameNumberByPolarMethod : the wires must be closed");
837
838 // Nombre max de decoupes possibles
839 Standard_Integer NbMaxV = 0;
840 for (i=1; i<=NbSects; i++) {
841 for(anExp.Init(TopoDS::Wire(myWork(i))); anExp.More(); anExp.Next()) {
842 NbMaxV++;
843 }
844 }
845
846 // sections ponctuelles, sections bouclantes ?
847 if (myDegen1) ideb++;
848 if (myDegen2) ifin--;
849 Standard_Boolean vClosed = (!myDegen1) && (!myDegen2)
850 && (myWork(ideb).IsSame(myWork(ifin)));
851
852 // construction des tableaux de plans des wires
853 gp_Pln P;
854 Handle(TColgp_HArray1OfPnt) Pos
855 = new (TColgp_HArray1OfPnt) (1,NbSects);
856 Handle(TColgp_HArray1OfVec) Axe
857 = new (TColgp_HArray1OfVec) (1,NbSects);
858 for (i=ideb;i<=ifin;i++) {
859 if (PlaneOfWire(TopoDS::Wire(myWork(i)),P)) {
860 Pos->SetValue(i,P.Location());
861 Axe->SetValue(i,gp_Vec(P.Axis().Direction()));
862 }
863 }
864 TopTools_SequenceOfShape SeqV;
865 if (myDegen1) {
866 SeqOfVertices(TopoDS::Wire(myWork(1)),SeqV);
867 Pos->SetValue(1,BRep_Tool::Pnt(TopoDS::Vertex(SeqV.Value(1))));
868 Axe->SetValue(1,Axe->Value(ideb));
869 }
870 if (myDegen2) {
871 SeqOfVertices(TopoDS::Wire(myWork(NbSects)),SeqV);
872 Pos->SetValue(NbSects,BRep_Tool::Pnt(TopoDS::Vertex(SeqV.Value(1))));
873 Axe->SetValue(NbSects,Axe->Value(ifin));
874 }
875
876 // construction de RMap, map des reports du wire i vers le wire i-1
877 TopTools_DataMapOfShapeListOfShape RMap;
878 RMap.Clear();
879
880 // boucle sur i
881 for (i=ifin; i>ideb; i--) {
882
883 const TopoDS_Wire& wire1 = TopoDS::Wire(myWork(i));
884
885 // sequence des vertex du premier wire
886 SeqOfVertices(wire1,SeqV);
887 if (SeqV.Length()>NbMaxV)
888 Standard_NoSuchObject::Raise("BRepFill::SameNumberByPolarMethod failed");
889
890 // extremite du premier wire
891 V1 = TopoDS::Vertex(SeqV.Value(1));
892 // wire precedent
893#ifdef DEB
894 const TopoDS_Wire& wire2 =
895#endif
896 TopoDS::Wire(myWork(i-1));
897 // boucle sur les vertex de wire1
898 for (ii=1;ii<=SeqV.Length();ii++) {
899
900 TopoDS_Vertex Vi = TopoDS::Vertex(SeqV.Value(ii));
901
902 // init de RMap pour Vi
903 TopTools_ListOfShape Init;
904 Init.Clear();
905 RMap.Bind(Vi,Init);
906
907 // il faut chercher l'intersection Vi - wire2
908 gp_Pnt Pi = BRep_Tool::Pnt(Vi);
909
910 // on ramene Pi dans le plan courant
911 gp_Pnt Pnew;
912 Transform(WithRotation,Pi,
913 Pos->Value(i),Axe->Value(i),
914 Pos->Value(i-1),Axe->Value(i-1),Pnew);
915
916 // calcul de l'intersection
917 TopoDS_Shape Support;
918 Standard_Boolean NewVertex;
919 TopoDS_Vertex Vsol;
920 TopoDS_Wire newwire;
921 if (Pnew.Distance(Pos->Value(i-1))>Precision::Confusion()) {
922 Standard_Real percent = myPercent;
923 NewVertex = EdgeIntersectOnWire(Pos->Value(i-1),Pnew,percent,
924 RMap,TopoDS::Wire(myWork(i-1)),
925 Vsol,newwire);
926 if (NewVertex) myWork(i-1) = newwire;
927 RMap(Vi).Append(Vsol);
928 }
929
930 } // boucle sur ii
931 } // boucle sur i
932
933 // initialisation de MapVLV, map des correspondances vertex - liste de vertex
934 TopTools_DataMapOfShapeListOfShape MapVLV;
935 SeqOfVertices(TopoDS::Wire(myWork(ideb)),SeqV);
936 Standard_Integer SizeMap = SeqV.Length();
937 MapVLV.Clear();
938 for (ii=1;ii<=SizeMap;ii++) {
939 TopoDS_Vertex Vi = TopoDS::Vertex(SeqV.Value(ii));
940 TopTools_ListOfShape Init;
941 Init.Clear();
942 Init.Append(Vi);
943 MapVLV.Bind(Vi,Init);
944 Standard_Integer NbV = 1;
945 TopoDS_Vertex V0,V1;
946 V0 = Vi;
947 Standard_Boolean tantque = SearchRoot(V0,RMap,V1);
948 while (tantque) {
949 MapVLV(Vi).Append(V1);
950 NbV++;
951 // test sur NbV necessaire pour les sections bouclantes
952 if (V1.IsSame(Vi) || NbV >= myWork.Length()) {
953 tantque = Standard_False;
954 }
955 else {
956 V0 = V1;
957 tantque = SearchRoot(V0,RMap,V1);
958 }
959 }
960 }
961
962 // boucle sur i
963 for (i=ideb; i<ifin; i++) {
964
965 const TopoDS_Wire& wire1 = TopoDS::Wire(myWork(i));
966
967 // sequence des vertex du premier wire
968 SeqOfVertices(wire1,SeqV);
969 if ( SeqV.Length()>NbMaxV || SeqV.Length()>SizeMap )
970 Standard_NoSuchObject::Raise("BRepFill::SameNumberByPolarMethod failed");
971
972 // extremite du premier wire
973 V1 = TopoDS::Vertex(SeqV.Value(1));
974
975 // wire suivant
976 const TopoDS_Wire& wire2 = TopoDS::Wire(myWork(i+1));
977
978 // boucle sur les vertex de wire1
979 for (ii=1;ii<=SeqV.Length();ii++) {
980
981 TopoDS_Vertex Vi = TopoDS::Vertex(SeqV.Value(ii));
982 TopoDS_Vertex VRoot;
983 VRoot.Nullify();
984 Standard_Boolean intersect = Standard_True;
985 if (SearchRoot(Vi,MapVLV,VRoot)) {
986 const TopTools_ListOfShape& LVi = MapVLV(VRoot);
987 TopoDS_Vertex VonW;
988 VonW.Nullify();
989 intersect = (!SearchVertex(LVi,wire2,VonW));
990 }
991
992 if (intersect) {
993 // il faut chercher l'intersection Vi - wire2
994 gp_Pnt Pi = BRep_Tool::Pnt(Vi);
995
996 // on ramene Pi dans le plan courant
997 gp_Pnt Pnew;
998 Transform(WithRotation,Pi,
999 Pos->Value(i),Axe->Value(i),
1000 Pos->Value(i+1),Axe->Value(i+1),Pnew);
1001
1002 // calcul de l'intersection
1003 TopoDS_Shape Support;
1004 Standard_Boolean NewVertex;
1005 TopoDS_Vertex Vsol;
1006 TopoDS_Wire newwire;
1007 if (Pnew.Distance(Pos->Value(i+1))>Precision::Confusion()) {
1008 Standard_Real percent = myPercent;
1009 NewVertex = EdgeIntersectOnWire(Pos->Value(i+1),Pnew,percent,
1010 MapVLV,TopoDS::Wire(myWork(i+1)),
1011 Vsol,newwire);
1012 MapVLV(VRoot).Append(Vsol);
1013 if (NewVertex) myWork(i+1) = newwire;
1014 }
1015
1016 }
1017 } // boucle sur ii
1018 } // boucle sur i
1019
1020 // mise en ordre des wires en suivant MapVLV
1021 TopoDS_Wire wire = TopoDS::Wire(myWork(ideb));
1022
1023 // sauf le dernier si les sections sont bouclantes
1024 Standard_Integer ibout = ifin;
1025 if (vClosed) ibout--;
1026
1027 for ( i=ideb+1; i<=ibout; i++) {
1028
1029 BRepLib_MakeWire MW;
1030
1031 anExp.Init(wire);
1032 TopoDS_Edge ECur = anExp.Current();
1033 TopoDS_Vertex VF,VL;
1034 TopExp::Vertices(ECur,VF,VL,Standard_True);
1035 Standard_Real U1 = BRep_Tool::Parameter(VF,ECur);
1036 Standard_Real U2 = BRep_Tool::Parameter(VL,ECur);
1037 BRepAdaptor_Curve Curve(ECur);
1038 gp_Pnt PPs = Curve.Value(0.1*(U1+9*U2));
1039 TopTools_ListIteratorOfListOfShape itF(MapVLV(VF)),itL(MapVLV(VL));
1040 Standard_Integer rang = ideb;
1041 while (rang < i) {
1042 itF.Next();
1043 itL.Next();
1044 rang++;
1045 }
1046 TopoDS_Vertex V1 = TopoDS::Vertex(itF.Value()), V2 = TopoDS::Vertex(itL.Value());
1047 TopoDS_Edge Esol;
1048 Standard_Real scalmax=0.;
1049 TopoDS_Iterator itW( myWork(i) );
1050
1051 for(; itW.More(); itW.Next())
1052 {
1053 TopoDS_Edge E = TopoDS::Edge(itW.Value());
1054 TopoDS_Vertex VVF,VVL;
1055 TopExp::Vertices(E,VVF,VVL,Standard_True);
1056
1057 // tri des edges candidates
1058 Standard_Real scal1,scal2;
1059 if ( (V1.IsSame(VVF)&&V2.IsSame(VVL)) || (V2.IsSame(VVF)&&V1.IsSame(VVL)) ) {
1060 Standard_Real U1 = BRep_Tool::Parameter(VVF,E);
1061 Standard_Real U2 = BRep_Tool::Parameter(VVL,E);
1062 BRepAdaptor_Curve Curve(E);
1063 gp_Pnt PP1 = Curve.Value(0.1*(U1+9*U2));
1064 gp_Pnt PP2 = Curve.Value(0.1*(9*U1+U2));
1065
1066 for (rang=i;rang>ideb;rang--) {
1067 Transform(WithRotation, PP1,
1068 Pos->Value(rang), Axe->Value(rang),
1069 Pos->Value(rang-1), Axe->Value(rang-1), PP1);
1070 Transform(WithRotation, PP2,
1071 Pos->Value(rang), Axe->Value(rang),
1072 Pos->Value(rang-1), Axe->Value(rang-1), PP2);
1073 }
1074 gp_Vec Ns(Pos->Value(ideb),PPs);
1075 Ns = Ns.Normalized();
1076 gp_Vec N1(Pos->Value(ideb),PP1);
1077 N1 = N1.Normalized();
1078 gp_Vec N2(Pos->Value(ideb),PP2);
1079 N2 = N2.Normalized();
1080 scal1 = N1.Dot(Ns);
1081 if (scal1>scalmax) {
1082 scalmax = scal1;
1083 Esol = E;
1084 }
1085 scal2 = N2.Dot(Ns);
1086 if (scal2>scalmax) {
1087 scalmax = scal2;
1088 TopoDS_Shape aLocalShape = E.Reversed();
1089 Esol = TopoDS::Edge(aLocalShape);
1090 }
1091 }
1092 } //end of for(; itW.More(); itW.Next())
1093 MW.Add(Esol);
1094
1095 TopTools_ListOfShape ConnectedEdges;
1096 BuildConnectedEdges( TopoDS::Wire(myWork(i)), Esol, V2, ConnectedEdges );
1097
1098 TopTools_ListIteratorOfListOfShape itCE(ConnectedEdges);
1099 for(; anExp.More(), itCE.More(); anExp.Next(), itCE.Next())
1100 {
1101 ECur = anExp.Current();
1102 TopExp::Vertices(ECur,VF,VL,Standard_True);
1103 U1 = BRep_Tool::Parameter(VF,ECur);
1104 U2 = BRep_Tool::Parameter(VL,ECur);
1105 Curve.Initialize(ECur);
1106 PPs = Curve.Value(0.1*(U1+9*U2));
1107
1108 TopoDS_Edge E = TopoDS::Edge(itCE.Value());
1109 TopoDS_Vertex VVF,VVL;
1110 TopExp::Vertices(E,VVF,VVL,Standard_True);
1111
1112 // tri des edges candidates
1113 Standard_Real scal1,scal2;
1114 U1 = BRep_Tool::Parameter(VVF,E);
1115 U2 = BRep_Tool::Parameter(VVL,E);
1116 Curve.Initialize(E);
1117 gp_Pnt PP1 = Curve.Value(0.1*(U1+9*U2));
1118 gp_Pnt PP2 = Curve.Value(0.1*(9*U1+U2));
1119
1120 for (rang=i;rang>ideb;rang--) {
1121 Transform(WithRotation, PP1,
1122 Pos->Value(rang), Axe->Value(rang),
1123 Pos->Value(rang-1), Axe->Value(rang-1), PP1);
1124 Transform(WithRotation, PP2,
1125 Pos->Value(rang), Axe->Value(rang),
1126 Pos->Value(rang-1), Axe->Value(rang-1), PP2);
1127 }
1128 gp_Vec Ns(Pos->Value(ideb),PPs);
1129 Ns = Ns.Normalized();
1130 gp_Vec N1(Pos->Value(ideb),PP1);
1131 N1 = N1.Normalized();
1132 gp_Vec N2(Pos->Value(ideb),PP2);
1133 N2 = N2.Normalized();
1134 scal1 = N1.Dot(Ns);
1135 scal2 = N2.Dot(Ns);
1136 if (scal2>scal1)
1137 E.Reverse();
1138 MW.Add(E);
1139 }
1140 myWork(i) = MW.Wire();
1141 }
1142
1143 // sections bouclantes ?
1144 if (vClosed) myWork(myWork.Length()) = myWork(1);
1145
1146 // verification du nombre d'edges pour debug
1147 Standard_Integer nbmax=0, nbmin=0;
1148 for ( i=ideb; i<=ifin; i++) {
1149 Standard_Integer nbEdges=0;
1150 for(anExp.Init(TopoDS::Wire(myWork(i))); anExp.More(); anExp.Next()) {
1151 nbEdges++;
1152 }
1153 if (i==ideb) nbmin = nbEdges;
1154 if (nbmax<nbEdges) nbmax = nbEdges;
1155 if (nbmin>nbEdges) nbmin = nbEdges;
1156 }
1157 if (nbmin!=nbmax) {
1158 Standard_NoSuchObject::Raise("BRepFill_CompatibleWires::SameNumberByPolarMethod failed");
1159 }
1160
1161}
1162
1163//=======================================================================
1164//function : SameNumberByACR
1165//purpose :
1166//=======================================================================
1167
1168void BRepFill_CompatibleWires::SameNumberByACR(const Standard_Boolean report)
1169{
1170 // find the dimension
1171 Standard_Integer ideb=1, ifin=myWork.Length();
1172 BRepTools_WireExplorer anExp;
1173
1174 // sections ponctuelles, sections bouclantes ?
1175 if (myDegen1) ideb++;
1176 if (myDegen2) ifin--;
1177 Standard_Boolean vClosed = (!myDegen1) && (!myDegen2)
1178 && (myWork(ideb).IsSame(myWork(ifin)));
1179
1180 Standard_Integer nbSects = myWork.Length(), i;
1181 Standard_Integer nbmax=0, nbmin=0;
1182 TColStd_Array1OfInteger nbEdges(1,nbSects);
1183 for (i=1; i<=nbSects; i++) {
1184 nbEdges(i) = 0;
1185 for(anExp.Init(TopoDS::Wire(myWork(i))); anExp.More(); anExp.Next()) {
1186 nbEdges(i)++;
1187 }
1188 if (i==1) nbmin = nbEdges(i);
1189 if (nbmax<nbEdges(i)) nbmax = nbEdges(i);
1190 if (nbmin>nbEdges(i)) nbmin = nbEdges(i);
1191 }
1192
1193 if (nbmax>1) {
1194 // plusieurs edges
1195
1196 if (report || nbmin<nbmax) {
1197 // insertion des decoupes
1198 Standard_Integer nbdec=(nbmax-1)*nbSects+1;
1199 Standard_Real tol = 0.01;
1200 TColStd_Array1OfReal dec(1,nbdec);
1201 dec.Init(0);
1202 dec(2)=1;
1203 // calcul du tableau des decoupes
1204 Standard_Integer j,k,l;
1205 for (i=1; i<=nbSects; i++) {
1206 // wire courant
1207 const TopoDS_Wire& wire1 = TopoDS::Wire(myWork(i));
1208 Standard_Integer nbE = 0;
1209 for(anExp.Init(wire1); anExp.More(); anExp.Next()) {
1210 nbE++;
1211 }
1212 // longueur et ACR du wire
1213 TColStd_Array1OfReal ACR(0,nbE);
1214 ACR.Init(0);
1215 BRepFill::ComputeACR(wire1, ACR);
1216 // insertion des ACR du wire dans le tableau des decoupes
1217 for (j=1; j<ACR.Length()-1; j++) {
1218 k=1;
1219 while (dec(k)<ACR(j)) {
1220 k++;
1221 if (k>nbdec) break;
1222 }
1223 if (dec(k-1)+tol<ACR(j)&& ACR(j)+tol<dec(k)) {
1224 for (l=nbdec-1;l>=k;l--) {
1225 dec(l+1)=dec(l);
1226 }
1227 dec(k) = ACR(j);
1228 }
1229 }
1230 }
1231
1232 // tableau effectif des decoupes
1233 k=1;
1234 while (dec(k)<1) {
1235 k++;
1236 if (k>nbdec) break;
1237 }
1238 nbdec = k-1;
1239 TColStd_Array1OfReal dec2(1,nbdec);
1240 for (k=1;k<=nbdec;k++) {
1241 dec2(k) = dec(k);
1242 }
1243
1244 // insertion des decoupes dans chaque wire
1245 for (i=1; i<=nbSects; i++) {
1246 const TopoDS_Wire& oldwire = TopoDS::Wire(myWork(i));
1247 TopoDS_Wire newwire = BRepFill::InsertACR(oldwire, dec2, tol);
1248 BRepTools_WireExplorer anExp1,anExp2;
1249 anExp1.Init(oldwire);
1250 anExp2.Init(newwire);
1251 for (;anExp1.More();anExp1.Next()) {
1252 const TopoDS_Edge& Ecur = anExp1.Current();
1253 if (!Ecur.IsSame(TopoDS::Edge(anExp2.Current()))) {
1254 TopTools_ListOfShape LE;
1255 LE.Clear();
1256 gp_Pnt P1,P2;
1257 const TopoDS_Vertex& V1 = anExp1.CurrentVertex();
1258 TopoDS_Vertex VF,VR;
1259 TopExp::Vertices(Ecur,VF,VR,Standard_True);
1260 if (V1.IsSame(VF)) P1 = BRep_Tool::Pnt(VR);
1261 if (V1.IsSame(VR)) P1 = BRep_Tool::Pnt(VF);
1262 TopoDS_Vertex V2 = anExp2.CurrentVertex();
1263 TopExp::Vertices(TopoDS::Edge(anExp2.Current()),
1264 VF,VR,Standard_True);
1265 if (V2.IsSame(VF)) P2 = BRep_Tool::Pnt(VR);
1266 if (V2.IsSame(VR)) P2 = BRep_Tool::Pnt(VF);
1267 while (P1.Distance(P2)>1.e-3) {
1268 LE.Append(anExp2.Current());
1269 anExp2.Next();
1270 V2 = anExp2.CurrentVertex();
1271 TopExp::Vertices(TopoDS::Edge(anExp2.Current()),
1272 VF,VR,Standard_True);
1273 if (V2.IsSame(VF)) P2 = BRep_Tool::Pnt(VR);
1274 if (V2.IsSame(VR)) P2 = BRep_Tool::Pnt(VF);
1275 if (P1.Distance(P2)<=1.e-3) {
1276 LE.Append(anExp2.Current());
1277 anExp2.Next();
1278 }
1279 }
1280
1281 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itmap;
1282 //TopTools_ListIteratorOfListOfShape itlist;
1283 TopoDS_Edge Ancestor;
1284 Standard_Integer nbedge, nblist=0;
1285 Standard_Boolean found = Standard_False;
1286
1287 for (itmap.Initialize(myMap);itmap.More()&&(!found);itmap.Next()) {
1288 nblist++;
1289 TopTools_ListIteratorOfListOfShape itlist(itmap.Value());
1290 nbedge = 0;
1291 while (itlist.More()&&(!found)) {
1292 nbedge++;
1293 TopoDS_Edge ECur = TopoDS::Edge(itlist.Value());
1294
1295 if (Ecur.IsSame(ECur)) {
1296 Ancestor = TopoDS::Edge(itmap.Key());
1297 found = Standard_True;
1298 myMap(Ancestor).InsertBefore(LE,itlist);
1299 myMap(Ancestor).Remove(itlist);
1300 }
1301 if (itlist.More()) itlist.Next();
1302 }
1303
1304 }
1305
1306 }
1307 else {
1308 anExp2.Next();
1309 }
1310
1311 }
1312 myWork(i) = newwire;
1313 }
1314
1315 }
1316 }
1317
1318 // sections bouclantes ?
1319 if (vClosed) myWork(myWork.Length()) = myWork(1);
1320
1321 // verification du nombre d'edges pour debug
1322 nbmax = 0;
1323 for (i=ideb; i<=ifin; i++) {
1324 nbEdges(i) = 0;
1325 for(anExp.Init(TopoDS::Wire(myWork(i))); anExp.More(); anExp.Next()) {
1326 nbEdges(i)++;
1327 }
1328 if (i==ideb) nbmin = nbEdges(i);
1329 if (nbmax<nbEdges(i)) nbmax = nbEdges(i);
1330 if (nbmin>nbEdges(i)) nbmin = nbEdges(i);
1331 }
1332 if (nbmax!=nbmin)
1333 Standard_NoSuchObject::Raise("BRepFill_CompatibleWires::SameNumberByACR failed");
1334}
1335
1336//=======================================================================
1337//function : ComputeOrigin
1338//purpose :
1339//=======================================================================
1340
1341void BRepFill_CompatibleWires::ComputeOrigin(const Standard_Boolean polar )
1342{
1343 // reorganize the wires respecting orientation and origin
1344
1345 TopoDS_Vertex Vdeb, Vfin;
1346 gp_Pnt Pdeb, Psuiv, PPs;
1347
1348 BRepTools_WireExplorer anExp;
1349
1350 Standard_Boolean wClosed, allClosed = Standard_True;
1351
1352 Standard_Integer NbSects = myWork.Length();
1353 Standard_Integer i, ideb=1,ifin=NbSects;
1354
1355 // sections ponctuelles, sections bouclantes
1356 if (myDegen1) ideb++;
1357 if (myDegen2) ifin--;
1358 Standard_Boolean vClosed = (!myDegen1) && (!myDegen2)
1359 && (myWork(ideb).IsSame(myWork(ifin)));
1360
1361
1362 for (i=ideb; i<=ifin; i++) {
1363 wClosed = myWork(i).Closed();
1364 if (!wClosed) {
1365 // on regarde quand meme si les vertex sont les memes.
1366 TopoDS_Vertex V1, V2;
1367 TopExp::Vertices(TopoDS::Wire(myWork(i)),V1,V2);
1368 if ( V1.IsSame(V2)) wClosed = Standard_True;
1369 }
1370 allClosed = (allClosed && wClosed);
1371 }
1372/*
1373 for (i=ideb; i<=ifin; i++) {
1374 allClosed = (allClosed && myWork(i).Closed());
1375 }
1376*/
1377 if (!allClosed)
1378 Standard_NoSuchObject::Raise("BRepFill_CompatibleWires::ComputeOrigin : the wires must be closed");
1379
1380/*
1381 // Nombre max de decoupes possibles
1382 Standard_Integer NbMaxV = 0;
1383 for (i=1; i<=NbSects; i++) {
1384 for(anExp.Init(TopoDS::Wire(myWork(i))); anExp.More(); anExp.Next()) {
1385 NbMaxV++;
1386 }
1387 }
1388
1389 // construction des tableaux de plans des wires
1390 gp_Pln P;
1391 Handle(TColgp_HArray1OfPnt) Pos
1392 = new (TColgp_HArray1OfPnt) (1,NbSects);
1393 Handle(TColgp_HArray1OfVec) Axe
1394 = new (TColgp_HArray1OfVec) (1,NbSects);
1395 for (i=ideb;i<=ifin;i++) {
1396 if (PlaneOfWire(TopoDS::Wire(myWork(i)),P)) {
1397 Pos->SetValue(i,P.Location());
1398 Axe->SetValue(i,gp_Vec(P.Axis().Direction()));
1399 }
1400 }
1401 TopTools_SequenceOfShape SeqV;
1402 if (myDegen1) {
1403 SeqOfVertices(TopoDS::Wire(myWork(1)),SeqV);
1404 Pos->SetValue(1,BRep_Tool::Pnt(TopoDS::Vertex(SeqV.Value(1))));
1405 Axe->SetValue(1,Axe->Value(ideb));
1406 }
1407 if (myDegen2) {
1408 SeqOfVertices(TopoDS::Wire(myWork(NbSects)),SeqV);
1409 Pos->SetValue(NbSects,BRep_Tool::Pnt(TopoDS::Vertex(SeqV.Value(1))));
1410 Axe->SetValue(NbSects,Axe->Value(ifin));
1411 }
1412*/
1413
1414 //Consider that all wires have same number of edges (polar==Standard_False)
1415 TopTools_SequenceOfShape PrevSeq;
1416 Standard_Integer theLength = 0;
1417 const TopoDS_Wire& wire = TopoDS::Wire( myWork(ideb) );
1418 for (anExp.Init(wire); anExp.More(); anExp.Next())
1419 {
1420 PrevSeq.Append(anExp.CurrentVertex());
1421 theLength++;
1422 }
1423
1424 for (i = ideb+1; i <= ifin; i++)
1425 {
1426 const TopoDS_Wire& wire = TopoDS::Wire(myWork(i));
1427 TopoDS_Wire newwire;
1428 BRep_Builder BB;
1429 BB.MakeWire(newwire);
1430
1431 TopTools_SequenceOfShape SeqVertices, SeqEdges;
1432 for (anExp.Init(wire); anExp.More(); anExp.Next())
1433 {
1434 SeqVertices.Append( anExp.CurrentVertex() );
1435 SeqEdges.Append( anExp.Current() );
1436 }
1437
1438 Standard_Real MinSumDist = Precision::Infinite();
1439 Standard_Integer jmin, j, k, n;
1440 Standard_Boolean forward;
1441 if (i == myWork.Length() && myDegen2)
1442 {
1443 // derniere section ponctuelle
1444 jmin = 1;
1445 forward = Standard_True;
1446 }
1447 else
1448 for (j = 1; j <= theLength; j++)
1449 {
1450 //Forward
1451 Standard_Real SumDist = 0.;
1452 for (k = j, n = 1; k <= theLength; k++, n++)
1453 {
1454 const TopoDS_Vertex& Vprev = TopoDS::Vertex( PrevSeq(n) );
1455 gp_Pnt Pprev = BRep_Tool::Pnt(Vprev);
1456 const TopoDS_Vertex& V = TopoDS::Vertex( SeqVertices(k) );
1457 gp_Pnt P = BRep_Tool::Pnt(V);
1458 SumDist += Pprev.Distance(P);
1459 }
1460 for (k = 1; k < j; k++, n++)
1461 {
1462 const TopoDS_Vertex& Vprev = TopoDS::Vertex( PrevSeq(n) );
1463 gp_Pnt Pprev = BRep_Tool::Pnt(Vprev);
1464 const TopoDS_Vertex& V = TopoDS::Vertex( SeqVertices(k) );
1465 gp_Pnt P = BRep_Tool::Pnt(V);
1466 SumDist += Pprev.Distance(P);
1467 }
1468 if (SumDist < MinSumDist)
1469 {
1470 MinSumDist = SumDist;
1471 jmin = j;
1472 forward = Standard_True;
1473 }
1474
1475 //Backward
1476 SumDist = 0.;
1477 for (k = j, n = 1; k >= 1; k--, n++)
1478 {
1479 const TopoDS_Vertex& Vprev = TopoDS::Vertex( PrevSeq(n) );
1480 gp_Pnt Pprev = BRep_Tool::Pnt(Vprev);
1481 const TopoDS_Vertex& V = TopoDS::Vertex( SeqVertices(k) );
1482 gp_Pnt P = BRep_Tool::Pnt(V);
1483 SumDist += Pprev.Distance(P);
1484 }
1485 for (k = theLength; k > j; k--, n++)
1486 {
1487 const TopoDS_Vertex& Vprev = TopoDS::Vertex( PrevSeq(n) );
1488 gp_Pnt Pprev = BRep_Tool::Pnt(Vprev);
1489 const TopoDS_Vertex& V = TopoDS::Vertex( SeqVertices(k) );
1490 gp_Pnt P = BRep_Tool::Pnt(V);
1491 SumDist += Pprev.Distance(P);
1492 }
1493 if (SumDist < MinSumDist)
1494 {
1495 MinSumDist = SumDist;
1496 jmin = j;
1497 forward = Standard_False;
1498 }
1499 }
1500
1501 PrevSeq.Clear();
1502 if (forward)
1503 {
1504 for (j = jmin; j <= theLength; j++)
1505 {
1506 BB.Add( newwire, TopoDS::Edge(SeqEdges(j)) );
1507 PrevSeq.Append( SeqVertices(j) );
1508 }
1509 for (j = 1; j < jmin; j++)
1510 {
1511 BB.Add( newwire, TopoDS::Edge(SeqEdges(j)) );
1512 PrevSeq.Append( SeqVertices(j) );
1513 }
1514 }
1515 else
1516 {
1517 for (j = jmin-1; j >= 1; j--)
1518 {
1519 TopoDS_Shape aLocalShape = SeqEdges(j).Reversed();
1520 BB.Add( newwire, TopoDS::Edge(aLocalShape) );
1521 //PrevSeq.Append( SeqVertices(j) );
1522 }
1523 for (j = theLength; j >= jmin; j--)
1524 {
1525 TopoDS_Shape aLocalShape = SeqEdges(j).Reversed();
1526 BB.Add( newwire, TopoDS::Edge(aLocalShape) );
1527 //PrevSeq.Append( SeqVertices(j) );
1528 }
1529 for (j = jmin; j >= 1; j--)
1530 PrevSeq.Append( SeqVertices(j) );
1531 for (j = theLength; j > jmin; j--)
1532 PrevSeq.Append( SeqVertices(j) );
1533 }
1534
1535 newwire.Closed( Standard_True );
1536 newwire.Orientation( TopAbs_FORWARD );
1537 myWork(i) = newwire;
1538 }
1539
1540/*
1541 for ( i=ideb; i<=myWork.Length(); i++) {
1542
1543 const TopoDS_Wire& wire = TopoDS::Wire(myWork(i));
1544
1545 Standard_Integer nbEdges=0;
1546 for(anExp.Init(TopoDS::Wire(myWork(i))); anExp.More(); anExp.Next())
1547 nbEdges++;
1548 TopExp::Vertices(wire,Vdeb,Vfin);
1549 Standard_Boolean wClosed = wire.Closed();
1550 if (!wClosed) {
1551 // on regarde quand meme si les vertex sont les memes.
1552 if ( Vdeb.IsSame(Vfin)) wClosed = Standard_True;
1553 }
1554
1555
1556 TopoDS_Vertex Vsuiv, VF, VR;
1557 TopoDS_Wire newwire;
1558 BRep_Builder BW;
1559 BW.MakeWire(newwire);
1560 if (i==ideb) {
1561 anExp.Init(wire);
1562 const TopoDS_Edge Ecur = TopoDS::Edge(anExp.Current());
1563 TopExp::Vertices(Ecur,VF,VR);
1564 if (Vdeb.IsSame(VF)) Vsuiv=VR;
1565 else if (Vdeb.IsSame(VR)) Vsuiv=VF;
1566 else {
1567 // par defaut on prend l'origine sur cette arete
1568 if (VR.IsSame(TopoDS::Vertex(anExp.CurrentVertex()))) {
1569 Vdeb = VR;
1570 Vsuiv = VF;
1571 }
1572 else {
1573 Vdeb = VF;
1574 Vsuiv = VR;
1575 }
1576 }
1577 Pdeb=BRep_Tool::Pnt(Vdeb);
1578 Psuiv=BRep_Tool::Pnt(Vsuiv);
1579 Standard_Real U1 = BRep_Tool::Parameter(Vdeb,Ecur);
1580 Standard_Real U2 = BRep_Tool::Parameter(Vsuiv,Ecur);
1581 BRepAdaptor_Curve Curve(Ecur);
1582 PPs = Curve.Value(0.25*(U1+3*U2));
1583 myWork(ideb) = wire;
1584 }
1585 else {
1586 // on ramene Pdeb, Psuiv et PPs dans le plan courant
1587 gp_Pnt Pnew,Pnext,PPn;
1588 Transform(Standard_True,Pdeb,Pos->Value(i-1),Axe->Value(i-1),
1589 Pos->Value(i),Axe->Value(i),Pnew);
1590 Transform(Standard_True,Psuiv,Pos->Value(i-1),Axe->Value(i-1),
1591 Pos->Value(i),Axe->Value(i),Pnext);
1592 Transform(Standard_True,PPs,Pos->Value(i-1),Axe->Value(i-1),
1593 Pos->Value(i),Axe->Value(i),PPn);
1594
1595 Standard_Real distmini,dist;
1596 Standard_Integer rang=0,rangdeb=0;
1597 TopoDS_Vertex Vmini;
1598 gp_Pnt Pmini,P1,P2;
1599 SeqOfVertices(wire,SeqV);
1600 if (SeqV.Length()>NbMaxV)
1601 Standard_NoSuchObject::Raise("BRepFill::ComputeOrigin failed");
1602 if (!polar) {
1603 // choix du vertex le plus proche comme origine
1604 distmini = Precision::Infinite();
1605 for (Standard_Integer ii=1;ii<=SeqV.Length();ii++) {
1606 P1 = BRep_Tool::Pnt(TopoDS::Vertex(SeqV.Value(ii)));
1607 dist = P1.Distance(Pnew);
1608 if (dist<distmini) {
1609 distmini = dist;
1610 Vmini = TopoDS::Vertex(SeqV.Value(ii));
1611 }
1612 }
1613 if (!Vmini.IsNull()) Pmini = BRep_Tool::Pnt(Vmini);
1614 }
1615 else {
1616
1617 // recherche du vertex correspondant a la projection conique
1618 Standard_Real angmin, angV, eta = Precision::Angular();
1619 TopoDS_Vertex Vopti;
1620 angmin = PI/2;
1621 distmini = Precision::Infinite();
1622 gp_Dir dir0(gp_Vec(Pnew,P.Location()));
1623 for (Standard_Integer ii=1;ii<=SeqV.Length();ii++) {
1624 P1 = BRep_Tool::Pnt(TopoDS::Vertex(SeqV.Value(ii)));
1625 dist = Pnew.Distance(P1);
1626 if (dist<Precision::Confusion()) {
1627 angV = 0.0;
1628 }
1629 else {
1630 gp_Dir dir1(gp_Vec(Pnew,P1));
1631 angV = dir1.Angle(dir0);
1632 }
1633 if (angV>PI/2) angV = PI - angV;
1634 if (angmin>angV+eta) {
1635 distmini = dist;
1636 angmin = angV;
1637 Vopti = TopoDS::Vertex(SeqV.Value(ii));
1638 }
1639 else if (Abs(angmin-angV)<eta) {
1640 if (dist<distmini) {
1641 distmini = dist;
1642 angmin = angV;
1643 Vopti = TopoDS::Vertex(SeqV.Value(ii));
1644 }
1645 }
1646 }
1647 gp_Pnt Popti;
1648 if (!Vopti.IsNull()) Popti = BRep_Tool::Pnt(Vopti);
1649 Vmini = Vopti;
1650
1651 }
1652
1653 distmini = Precision::Infinite();
1654 for (anExp.Init(wire); anExp.More(); anExp.Next()) {
1655 TopoDS_Edge Ecur = anExp.Current();
1656 TopoDS_Vertex Vcur = anExp.CurrentVertex();
1657 TopExp::Vertices(Ecur,VF,VR);
1658 if (VF.IsSame(Vmini)) {
1659 P1 = BRep_Tool::Pnt(VR);
1660 dist = P1.Distance(Pnext);
1661 if (dist<=distmini) {
1662 distmini = dist;
1663 Vsuiv = VR;
1664 }
1665 }
1666 if (VR.IsSame(Vmini)) {
1667 P1 = BRep_Tool::Pnt(VF);
1668 dist = P1.Distance(Pnext);
1669 if (dist<distmini) {
1670 distmini = dist;
1671 Vsuiv = VF;
1672 }
1673 }
1674 }
1675
1676 // choix du sens de parcours en fonction de Pnext
1677 Standard_Boolean parcours = Standard_False;
1678 if (i==myWork.Length() && myDegen2) {
1679 // derniere section ponctuelle
1680 rangdeb = 1;
1681 parcours = Standard_True;
1682 }
1683 else {
1684 // cas general
1685 gp_Pnt Pbout = Pnext;
1686 TopoDS_Edge E1,E2;
1687 TopoDS_Vertex V1,V2;
1688 EdgesFromVertex(wire,Vmini,E1,E2);
1689
1690 TopExp::Vertices(E1,V1,V2,Standard_True);
1691#ifndef DEB
1692 Standard_Real U1=0, U2=0;
1693#else
1694 Standard_Real U1, U2;
1695#endif
1696 if (Vmini.IsSame(V1)) {
1697 P1 = BRep_Tool::Pnt(V2);
1698 U1 = 0.25*(BRep_Tool::Parameter(V1,E1)+3*BRep_Tool::Parameter(V2,E1));
1699 }
1700 if (Vmini.IsSame(V2)) {
1701 P1 = BRep_Tool::Pnt(V1);
1702 U1 = 0.25*(3*BRep_Tool::Parameter(V1,E1)+BRep_Tool::Parameter(V2,E1));
1703 }
1704
1705 TopExp::Vertices(E2,V1,V2,Standard_True);
1706 if (Vmini.IsSame(V1)) {
1707 P2 = BRep_Tool::Pnt(V2);
1708 U2 = 0.25*(BRep_Tool::Parameter(V1,E2)+3*BRep_Tool::Parameter(V2,E2));
1709 }
1710 if (Vmini.IsSame(V2)) {
1711 P2 = BRep_Tool::Pnt(V1);
1712 U2 = 0.25*(3*BRep_Tool::Parameter(V1,E2)+BRep_Tool::Parameter(V2,E2));
1713 }
1714
1715 if (Abs(Pbout.Distance(P1)-Pbout.Distance(P2))<Precision::Confusion()) {
1716 // cas limite ; on se decale un peu
1717 Pbout = PPn;
1718 BRepAdaptor_Curve Curve1(E1);
1719 P1 = Curve1.Value(U1);
1720 BRepAdaptor_Curve Curve2(E2);
1721 P2 = Curve2.Value(U2);
1722 }
1723
1724 // calcul de rangdeb
1725 rangdeb = 0;
1726 if (Pbout.Distance(P1)<Pbout.Distance(P2)){
1727 // P1 est plus proche; parcours = False
1728 parcours = Standard_False;
1729 rang = 0;
1730 for (anExp.Init(wire); anExp.More(); anExp.Next()) {
1731 rang++;
1732 TopoDS_Edge Ecur = anExp.Current();
1733 if (E1.IsSame(Ecur)) {
1734 rangdeb = rang;
1735 }
1736 }
1737 BRepAdaptor_Curve Curve(E1);
1738 PPs = Curve.Value(U1);
1739 }
1740 else {
1741 // P2 est plus proche; parcours = True
1742 parcours = Standard_True;
1743 rang = 0;
1744 for (anExp.Init(wire); anExp.More(); anExp.Next()) {
1745 rang++;
1746 TopoDS_Edge Ecur = anExp.Current();
1747 if (E2.IsSame(Ecur)) {
1748 rangdeb = rang;
1749 }
1750 }
1751 BRepAdaptor_Curve Curve(E2);
1752 PPs = Curve.Value(U2);
1753 }
1754 }
1755
1756 // reconstruction du wire a partir de rangdeb
1757 TopTools_SequenceOfShape SeqEdges;
1758 SeqEdges.Clear();
1759 for (anExp.Init(TopoDS::Wire(wire)); anExp.More(); anExp.Next()) {
1760 SeqEdges.Append(anExp.Current());
1761 }
1762 if (parcours) {
1763 for (rang=rangdeb;rang<=nbEdges;rang++) {
1764 BW.Add(newwire,TopoDS::Edge(SeqEdges.Value(rang)));
1765 }
1766 for (rang=1;rang<rangdeb;rang++) {
1767 BW.Add(newwire,TopoDS::Edge(SeqEdges.Value(rang)));
1768 }
1769 }
1770 else {
1771 for (rang=rangdeb;rang>=1;rang--) {
1772 TopoDS_Shape aLocalShape = SeqEdges.Value(rang).Reversed();
1773 BW.Add(newwire,TopoDS::Edge(aLocalShape));
1774// BW.Add(newwire,TopoDS::Edge(SeqEdges.Value(rang).Reversed()));
1775 }
1776 for (rang=nbEdges;rang>rangdeb;rang--) {
1777 TopoDS_Shape aLocalShape = SeqEdges.Value(rang).Reversed();
1778 BW.Add(newwire,TopoDS::Edge(aLocalShape));
1779// BW.Add(newwire,TopoDS::Edge(SeqEdges.Value(rang).Reversed()));
1780 }
1781 }
1782
1783 myWork(i) = newwire.Oriented(TopAbs_FORWARD);
1784
1785 // on passe au wire suivant
1786 if (!Vmini.IsNull()) Pdeb=BRep_Tool::Pnt(Vmini);
1787 if (!Vsuiv.IsNull()) Psuiv=BRep_Tool::Pnt(Vsuiv);
1788 }
1789 }
1790*/
1791
1792 // sections bouclantes ?
1793 if (vClosed) myWork(myWork.Length()) = myWork(1);
1794}
1795
1796//=======================================================================
1797//function : SearchOrigin
1798//purpose :
1799//=======================================================================
1800
1801void BRepFill_CompatibleWires::SearchOrigin()
1802{
1803 // reorganize the open wires respecting orientation and origin
1804
1805 gp_Pln P0,P;
1806
1807 TopoDS_Vertex Vdeb, Vfin;
1808 gp_Pnt Pdeb, Pfin;//,Psuiv;
1809
1810 BRepTools_WireExplorer anExp;
1811
1812 Standard_Boolean allOpen = Standard_True;
1813 Standard_Integer ideb=1, ifin=myWork.Length();
1814 if (myDegen1) ideb++;
1815 if (myDegen2) ifin--;
1816 Standard_Boolean vClosed = (!myDegen1) && (!myDegen2)
1817 && (myWork(ideb).IsSame(myWork(ifin)));
1818
1819// for (Standard_Integer i=ideb; i<=ifin; i++) {
1820 Standard_Integer i;
1821 for (i=ideb; i<=ifin; i++) {
1822 allOpen = (allOpen && !myWork(i).Closed());
1823 }
1824 if (!allOpen)
1825 Standard_NoSuchObject::Raise("BRepFill_CompatibleWires::SearchOrigin : the wires must be open");
1826
1827 // init
1828
1829 TopoDS_Wire wire1 = TopoDS::Wire(myWork(ideb));
1830 wire1.Orientation(TopAbs_FORWARD);
1831 TopExp::Vertices(wire1,Vdeb,Vfin);
1832 Pdeb = BRep_Tool::Pnt(Vdeb);
1833 Pfin = BRep_Tool::Pnt(Vfin);
1834 Standard_Boolean isline0 = (!PlaneOfWire(wire1,P0)), isline;
1835 myWork(ideb) = wire1;
1836 //OCC86
1837 anExp.Init(wire1);
1838 TopoDS_Edge E0 = anExp.Current(), E;
1839
1840 for ( i=ideb+1; i<=ifin; i++) {
1841
1842 TopoDS_Wire wire = TopoDS::Wire(myWork(i));
1843 wire.Orientation(TopAbs_FORWARD);
1844
1845 TopTools_SequenceOfShape SeqEdges;
1846 SeqEdges.Clear();
1847 Standard_Integer nbEdges=0;
1848 //OCC86 for(anExp.Init(wire); anExp.More(); anExp.Next()) {
1849 for(anExp.Init(wire), E = anExp.Current(); anExp.More(); anExp.Next()) {
1850 SeqEdges.Append(anExp.Current());
1851 nbEdges++;
1852 }
1853 TopExp::Vertices(wire,Vdeb,Vfin);
1854 isline = (!PlaneOfWire(wire,P));
1855
1856 TopoDS_Vertex Vmini;
1857 TopoDS_Wire newwire;
1858 BRep_Builder BW;
1859 BW.MakeWire(newwire);
1860 Standard_Boolean parcours = Standard_True;
1861
1862 if (isline0 || isline) {
1863
1864 // cas particulier des segments de droite
1865 gp_Pnt P1 = BRep_Tool::Pnt(Vdeb),
1866 P2 = BRep_Tool::Pnt(Vfin);
1867 Standard_Real dist1, dist2;
1868 dist1 = Pdeb.Distance(P1)+Pfin.Distance(P2);
1869 dist2 = Pdeb.Distance(P2)+Pfin.Distance(P1);
1870 parcours = (dist2>=dist1);
1871 }
1872
1873 else {
1874 //OCC86
1875 gp_Pnt P1 = BRep_Tool::Pnt(Vdeb), P1o = Pdeb,
1876 P2 = BRep_Tool::Pnt(Vfin), P2o = Pfin;
1877/* // on ramene Pdeb dans le plan courant
1878 gp_Pnt Pnew = Pdeb.Translated (P0.Location(),P.Location());
1879 gp_Ax1 A0 = P0.Axis();
1880 gp_Ax1 A1 = P.Axis();
1881
1882 if (!A0.IsParallel(A1,1.e-4)) {
1883 gp_Vec vec1(A0.Direction()), vec2(A1.Direction()),
1884 norm = vec1 ^ vec2;
1885 gp_Ax1 Norm(P.Location(),norm);
1886 Standard_Real ang = vec1.AngleWithRef(vec2,norm);
1887 if (ang > PI/2.0)
1888 ang = PI - ang;
1889 if (ang < -PI/2.0)
1890 ang = -PI - ang;
1891 if (Abs(ang-PI/2.0)<Precision::Angular()) {
1892 // cas d'ambiguite
1893 gp_Vec Vtrans(P0.Location(),P.Location()),Vsign;
1894 Standard_Real alpha,beta,sign=1;
1895 Vsign.SetLinearForm(Vtrans.Dot(vec1),vec2,-Vtrans.Dot(vec2),vec1);
1896 alpha = Vsign.Dot(vec1);
1897 beta = Vsign.Dot(vec2);
1898 Standard_Boolean pasnul = (Abs(alpha)>1.e-4 && Abs(beta)>1.e-4);
1899 if ( alpha*beta>0.0 && pasnul ) sign=-1;
1900 ang *= sign;
1901 }
1902 Pnew = Pnew.Rotated (Norm,ang);
1903 }
1904 // choix entre Vdeb et Vfin
1905 Standard_Real dist = Pnew.Distance(P1);
1906 parcours = (dist<Pnew.Distance(P2));
1907*/
1908 if(P1.IsEqual(P2,Precision::Confusion()) || P1o.IsEqual(P2o,Precision::Confusion())){
1909 BRepAdaptor_Curve Curve0(E0), Curve(E);
1910 Curve0.D0(Curve0.FirstParameter() + Precision::Confusion(), P2o);
1911 Curve.D0(Curve.FirstParameter() + Precision::Confusion(), P2);
1912 };
1913 gp_Vec VDebFin0(P1o,P2o), VDebFin(P1,P2);
1914 Standard_Real AStraight = VDebFin0.Angle(VDebFin);
1915 parcours = (AStraight < PI/2.0? Standard_True: Standard_False);
1916 }
1917
1918 // reconstruction du wire
1919 Standard_Integer rang;
1920 if (parcours) {
1921 for (rang=1;rang<=nbEdges;rang++) {
1922 TopoDS_Shape alocalshape = SeqEdges.Value(rang);
1923 BW.Add(newwire,TopoDS::Edge(alocalshape));
1924// BW.Add(newwire,TopoDS::Edge(SeqEdges.Value(rang)));
1925 }
1926 }
1927 else {
1928 for (rang=nbEdges;rang>=1;rang--) {
1929 TopoDS_Shape alocalshape = SeqEdges.Value(rang).Reversed();
1930 BW.Add(newwire,TopoDS::Edge(alocalshape));
1931// BW.Add(newwire,TopoDS::Edge(SeqEdges.Value(rang).Reversed()));
1932 }
1933 }
1934
1935 // orientation du wire
1936 newwire.Oriented(TopAbs_FORWARD);
1937 myWork(i) = newwire;
1938
1939 // on passe au wire suivant
1940 if (parcours) {
1941 Pdeb = BRep_Tool::Pnt(Vdeb);
1942 Pfin = BRep_Tool::Pnt(Vfin);
1943 }
1944 else {
1945 Pfin = BRep_Tool::Pnt(Vdeb);
1946 Pdeb = BRep_Tool::Pnt(Vfin);
1947 }
1948 P0 = P;
1949 isline0 = isline;
1950 //OCC86
1951 E0 = E;
1952 }
1953
1954 // sections bouclantes ?
1955 if (vClosed) myWork(myWork.Length()) = myWork(1);
1956}
1957
1958
1959
1960
1961
1962
1963
1964