1 // Created on: 1999-11-02
2 // Created by: Peter KURNEV
3 // Copyright (c) 1999-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.
17 #include <TopOpeBRepBuild_Tools.ixx>
19 #include <gp_Pnt2d.hxx>
23 #include <Adaptor3d_CurveOnSurface.hxx>
24 #include <Adaptor2d_HCurve2d.hxx>
26 #include <BRepTools.hxx>
27 #include <BRep_Tool.hxx>
28 #include <BRepAdaptor_Surface.hxx>
29 #include <BRepAdaptor_Curve.hxx>
31 #include <GeomAdaptor_Surface.hxx>
32 #include <GeomAdaptor_Curve.hxx>
34 #include <Geom_Curve.hxx>
35 #include <Geom_TrimmedCurve.hxx>
36 #include <Geom_Surface.hxx>
37 #include <Geom2d_Curve.hxx>
38 #include <GeomProjLib.hxx>
40 #include <Geom2dAPI_ProjectPointOnCurve.hxx>
41 #include <GeomAPI_ProjectPointOnCurve.hxx>
43 #include <TopOpeBRepTool_2d.hxx>
45 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
46 #include <TopTools_MapIteratorOfMapOfShape.hxx>
47 #include <TopTools_MapOfShape.hxx>
48 #include <TopTools_IndexedMapOfShape.hxx>
49 #include <TopTools_SequenceOfShape.hxx>
51 #include <TopoDS_Shape.hxx>
52 #include <TopoDS_Face.hxx>
53 #include <TopoDS_Edge.hxx>
56 #include <TopOpeBRepDS_DataStructure.hxx>
57 #include <TopOpeBRepDS_IndexedDataMapOfShapeWithState.hxx>
58 #include <TopOpeBRepDS_ShapeWithState.hxx>
59 #include <TopOpeBRepDS_DataMapOfShapeState.hxx>
60 #include <TopOpeBRepDS_DataMapIteratorOfDataMapOfShapeState.hxx>
62 #include <TopOpeBRepTool_CurveTool.hxx>
63 #include <TopOpeBRepTool_TOOL.hxx>
64 #include <TopLoc_Location.hxx>
66 #include <TopExp_Explorer.hxx>
68 #include <TCollection_AsciiString.hxx>
69 #include <BRep_Tool.hxx>
70 #include <BRepAdaptor_HSurface.hxx>
71 #include <GeomAdaptor_HCurve.hxx>
72 #include <ProjLib_ProjectedCurve.hxx>
73 #include <GeomAbs_CurveType.hxx>
74 #include <Geom2d_Line.hxx>
75 #include <Geom2d_Circle.hxx>
76 #include <Geom2d_Ellipse.hxx>
77 #include <Geom2d_Parabola.hxx>
78 #include <Geom2d_Hyperbola.hxx>
79 #include <Geom2d_TrimmedCurve.hxx>
80 #include <BRepTopAdaptor_FClass2d.hxx>
81 #include <TopOpeBRepBuild_CorrectFace2d.hxx>
82 #include <Precision.hxx>
85 //define parameter division number as 10*e^(-PI) = 0.43213918
86 const Standard_Real PAR_T = 0.43213918;
88 //=======================================================================
89 //function TopOpeBRepBuild_Tools::DumpMapOfShapeWithState
91 //=======================================================================
92 void TopOpeBRepBuild_Tools::DumpMapOfShapeWithState(const Standard_Integer iP,
93 const TopOpeBRepDS_IndexedDataMapOfShapeWithState&
96 static Standard_Integer cnt=0;
97 TCollection_AsciiString aFName1 ("/DEBUG/TOPOPE/"), postfix;
99 Standard_CString ShapeType [9] = {"COMPO", "COMPS", "SOLID", "SHELL", "FACE ", "WIRE ", "EDGE ", "VERTX"};
100 Standard_CString ShapeState[4] = {"IN ", "OUT", "ON ", "UNKNOWN"};
102 printf("\n\n********************************\n");
104 Standard_Integer i, n=aMapOfShapeWithState.Extent();
106 printf("* Object comparing with TOOL *\n");
107 postfix=TCollection_AsciiString("Obj");
111 printf("* Tool comparing with Object *\n");
112 postfix=TCollection_AsciiString("Tool");
116 printf("********************************\n");
117 printf("*** aMapOfShapeWithState.Extent()=%d\n", n);
118 printf(" C O N T E N T S\n");
120 TCollection_AsciiString aFName;
124 for (i=1; i<=n; i++) {
125 TCollection_AsciiString aI(i), aName;;
126 aName+=aFName; aName+=aI;
128 const TopoDS_Shape& aShape=aMapOfShapeWithState.FindKey(i);
129 const TopOpeBRepDS_ShapeWithState& aShapeWithState=
130 aMapOfShapeWithState.FindFromIndex(i);
132 BRepTools::Write (aShape, aName.ToCString());
134 TCollection_AsciiString ann;
135 ann+=postfix; ann+=aI;
137 printf("Key: %-8s , " , ann.ToCString());
138 printf("%s, ", ShapeType[aShape.ShapeType()]);
140 printf("State comp.with Tool=%s\n", ShapeState[aShapeWithState.State()]);
143 printf("State comp.with Obj =%s\n", ShapeState[aShapeWithState.State()]);
145 if (aShapeWithState.IsSplitted()) {
147 const TopTools_ListOfShape& aListOfShape=aShapeWithState.Part(TopAbs_IN);
148 TopTools_ListIteratorOfListOfShape anIt(aListOfShape);
149 for (;anIt.More(); anIt.Next()) {
150 const TopoDS_Shape& aS=anIt.Value();
152 TCollection_AsciiString cn(cnt), prefix("_S_"), sn;
153 sn+=aFName; sn+=prefix; sn+=cn;
154 BRepTools::Write (aS, sn.ToCString());
156 TCollection_AsciiString an;//=postfix+prefix+cn;
157 an+=postfix; an+=prefix; an+=cn;
158 printf(" -> Splitted Part IN : %s\n", an.ToCString());
162 const TopTools_ListOfShape& aListOfShapeOut=aShapeWithState.Part(TopAbs_OUT);
163 anIt.Initialize (aListOfShapeOut);
164 for (;anIt.More(); anIt.Next()) {
165 const TopoDS_Shape& aS=anIt.Value();
167 TCollection_AsciiString cn(cnt), prefix("_S_"), sn;//=aFName+prefix+cn;
168 sn+=aFName; sn+=prefix; sn+=cn;
169 BRepTools::Write (aS, sn.ToCString());
171 TCollection_AsciiString an;//=postfix+prefix+cn;
172 an+=postfix; an+=prefix; an+=cn;
173 printf(" -> Splitted Part OUT: %-s\n", an.ToCString());
177 const TopTools_ListOfShape& aListOfShapeOn=aShapeWithState.Part(TopAbs_ON);
178 anIt.Initialize (aListOfShapeOn);
179 for (;anIt.More(); anIt.Next()) {
180 const TopoDS_Shape& aS=anIt.Value();
182 TCollection_AsciiString cn(cnt), prefix("_S_"), sn;//=aFName+prefix+cn;
183 sn+=aFName; sn+=prefix; sn+=cn;
184 BRepTools::Write (aS, sn.ToCString());
186 TCollection_AsciiString an;//=postfix+prefix+cn;
187 an+=postfix; an+=prefix; an+=cn;
188 printf(" -> Splitted Part ON : %s\n", an.ToCString());
198 //=======================================================================
199 //function TopOpeBRepBuild_Tools::FindState
201 //=======================================================================
202 void TopOpeBRepBuild_Tools::FindState (const TopoDS_Shape& aSubsh,
203 const TopAbs_State aState,
204 const TopAbs_ShapeEnum aSubshEnum,
205 const TopTools_IndexedDataMapOfShapeListOfShape& aMapSubshAnc,
206 TopTools_MapOfShape& aMapProcessedSubsh,
207 TopOpeBRepDS_DataMapOfShapeState& aMapSS)
209 Standard_Integer i, nSub;
210 const TopTools_ListOfShape& aListOfShapes=aMapSubshAnc.FindFromKey(aSubsh);
211 TopTools_ListIteratorOfListOfShape anIt(aListOfShapes);
212 for (; anIt.More(); anIt.Next()) {
213 const TopoDS_Shape& aS=anIt.Value();
214 TopTools_IndexedMapOfShape aSubshMap;
215 TopExp::MapShapes (aS, aSubshEnum, aSubshMap);
216 nSub=aSubshMap.Extent();
217 for (i=1; i<=nSub; i++) {
218 const TopoDS_Shape& aSS=aSubshMap(i);
219 if (! aMapProcessedSubsh.Contains(aSS)) {
220 aMapProcessedSubsh.Add(aSS);
221 aMapSS.Bind (aSS, aState);
222 FindState (aSS, aState, aSubshEnum, aMapSubshAnc, aMapProcessedSubsh, aMapSS);
228 //=======================================================================
229 //function TopOpeBRepBuild_Tools::PropagateState
231 //=======================================================================
232 void TopOpeBRepBuild_Tools::PropagateState (const TopOpeBRepDS_DataMapOfShapeState& aSplShapesState,
233 const TopTools_IndexedMapOfShape& aShapesToRestMap,
234 const TopAbs_ShapeEnum aSubshEnum,// Vertex
235 const TopAbs_ShapeEnum aShapeEnum,//Edge
236 TopOpeBRepTool_ShapeClassifier& aShapeClassifier,
237 TopOpeBRepDS_IndexedDataMapOfShapeWithState& aMapOfShapeWithState,
238 const TopTools_MapOfShape& anAvoidSubshMap)
240 Standard_Integer j, nSub, nRest;
241 TopOpeBRepDS_DataMapOfShapeState aMapSS, aMapSS1;
244 TopOpeBRepDS_DataMapIteratorOfDataMapOfShapeState anItSS (aSplShapesState);
245 for (; anItSS.More(); anItSS.Next()) {
246 const TopoDS_Shape& aShape= anItSS.Key();
247 TopAbs_State aState = anItSS.Value();
248 TopTools_IndexedMapOfShape aSubshapes;
249 TopExp::MapShapes (aShape, aSubshEnum, aSubshapes);
250 nSub=aSubshapes.Extent();
251 for (j=1; j<=nSub; j++)
252 if (!anAvoidSubshMap.Contains(aSubshapes(j))) // MSV: enforce subshapes avoidance
253 aMapSS.Bind (aSubshapes(j), aState);
258 // 1. Build the Map of ShapesAndAncestors for ShapesToRest
259 TopTools_IndexedDataMapOfShapeListOfShape aMapSubshAnc;
260 nRest=aShapesToRestMap.Extent();
261 for (j=1; j<=nRest; j++)
262 TopExp::MapShapesAndAncestors(aShapesToRestMap(j), aSubshEnum, aShapeEnum, aMapSubshAnc);
264 // 2. Make Map Of all subshapes aMapSS
265 TopTools_MapOfShape aProcessedSubshapes;
266 anItSS.Initialize (aMapSS1);
267 for (; anItSS.More(); anItSS.Next()) {
268 const TopoDS_Shape& aSubsh = anItSS.Key();
269 TopAbs_State aState = anItSS.Value();
270 if (aMapSubshAnc.Contains (aSubsh)) {
271 aProcessedSubshapes.Add (aSubsh);
272 FindState (aSubsh, aState, aSubshEnum, aMapSubshAnc, aProcessedSubshapes, aMapSS);
276 // 3. Propagate the states on ShapesToRestMap
277 TopoDS_Shape aNullShape;
278 TopTools_MapOfShape aNonPassedShapes;
279 nRest=aShapesToRestMap.Extent();
280 for (j=1; j<=nRest; j++) {
281 const TopoDS_Shape& aS=aShapesToRestMap.FindKey(j);
282 TopTools_IndexedMapOfShape aSubshMap;
283 TopExp::MapShapes (aS, aSubshEnum, aSubshMap);
284 const TopoDS_Shape& aSubsh=aSubshMap(1);
285 if (aMapSS.IsBound(aSubsh)) {
286 TopAbs_State aState=aMapSS.Find(aSubsh);
288 if (aState==TopAbs_ON) {
289 aState=aShapeClassifier.StateShapeReference(aS, aNullShape);
291 // Add the Rest Shape to aMapOfShapeWithState
292 TopOpeBRepDS_ShapeWithState aShapeWithState;
293 aShapeWithState.SetState (aState);
294 aShapeWithState.SetIsSplitted (Standard_False);
295 aMapOfShapeWithState.Add (aS, aShapeWithState);
299 aNonPassedShapes.Add(aS);
303 // 4. Define the states for aNonPassedShapes
304 // (for faces themselves and for theirs Wires, Edges):
305 if (aNonPassedShapes.Extent()) {
306 // Build the Map of ShapesAndAncestors for aNonPassedShapes
307 aMapSubshAnc.Clear();
308 TopTools_MapIteratorOfMapOfShape aMapIt;
309 aMapIt.Initialize (aNonPassedShapes);
310 for (; aMapIt.More(); aMapIt.Next())
311 TopExp::MapShapesAndAncestors (aMapIt.Key(), aSubshEnum, aShapeEnum, aMapSubshAnc);
314 aMapIt.Initialize (aNonPassedShapes);
315 for (; aMapIt.More(); aMapIt.Next()) {
317 const TopoDS_Shape& aNonPassedShape=aMapIt.Key();
319 if (!aMapSS.IsBound(aNonPassedShape)) {
320 TopAbs_State aState = FindStateThroughVertex (aNonPassedShape, aShapeClassifier,
321 aMapOfShapeWithState,anAvoidSubshMap);
322 aMapSS.Bind (aNonPassedShape, aState);
325 TopTools_IndexedMapOfShape aTmpMap;
326 TopExp::MapShapes (aNonPassedShape, aSubshEnum, aTmpMap);
327 TopoDS_Shape aFirstSubsh;
328 for (j=1; j <= aTmpMap.Extent() && aFirstSubsh.IsNull(); j++)
329 if (!anAvoidSubshMap.Contains(aTmpMap(j)))
330 aFirstSubsh = aTmpMap(j);
331 if (aFirstSubsh.IsNull()) continue;
332 aMapSS.Bind (aFirstSubsh, aState);
334 // Propagation of aState for subshapes
335 TopTools_MapOfShape aMapProcessedSubsh;
336 if (aSubshEnum==TopAbs_EDGE)
337 FindState1 (aFirstSubsh, aState, aMapSubshAnc, aMapProcessedSubsh, aMapSS);
338 else // if (aSubshEnum==TopAbs_VERTEX)
339 FindState2 (aFirstSubsh, aState, aMapSubshAnc, aMapProcessedSubsh, aMapSS);
343 // Fill aShapeWithState
344 TopOpeBRepDS_ShapeWithState aShapeWithState;
345 aShapeWithState.SetIsSplitted (Standard_False);
346 TopOpeBRepDS_DataMapIteratorOfDataMapOfShapeState anII(aMapSS);
347 for (; anII.More(); anII.Next()) {
348 aShapeWithState.SetState (anII.Value());
349 if (anII.Key().ShapeType() != TopAbs_VERTEX)
350 aMapOfShapeWithState.Add (anII.Key(), aShapeWithState);
355 //=======================================================================
356 //function : TopOpeBRepBuild_Tools::FindState2
358 //=======================================================================
359 void TopOpeBRepBuild_Tools::FindState2 (const TopoDS_Shape& aSubsh,
360 const TopAbs_State aState,
361 const TopTools_IndexedDataMapOfShapeListOfShape& aMapSubshAnc,
362 TopTools_MapOfShape& aMapProcessedSubsh,
363 TopOpeBRepDS_DataMapOfShapeState& aMapSS)
365 Standard_Integer i, nSub;
366 const TopTools_ListOfShape& aListOfShapes=aMapSubshAnc.FindFromKey(aSubsh);
367 TopTools_ListIteratorOfListOfShape anIt(aListOfShapes);
368 for (; anIt.More(); anIt.Next()) {
370 const TopoDS_Shape& aShape=anIt.Value();
371 aMapSS.Bind (aShape, aState);
374 TopTools_IndexedMapOfShape aSubshMap;
375 TopExp::MapShapes (aShape, TopAbs_VERTEX, aSubshMap);
376 nSub=aSubshMap.Extent();
377 for (i=1; i<=nSub; i++) {
378 const TopoDS_Shape& aSS=aSubshMap(i);
379 if (! aMapProcessedSubsh.Contains(aSS)) {
380 aMapProcessedSubsh.Add(aSS);
381 aMapSS.Bind (aSS, aState);
382 FindState2 (aSS, aState, aMapSubshAnc, aMapProcessedSubsh, aMapSS);
388 //=======================================================================
389 //function :TopOpeBRepBuild_Tools::FindState1
391 //=======================================================================
392 void TopOpeBRepBuild_Tools::FindState1 (const TopoDS_Shape& aSubsh,
393 const TopAbs_State aState,
394 const TopTools_IndexedDataMapOfShapeListOfShape& aMapSubshAnc,
395 TopTools_MapOfShape& aMapProcessedSubsh,
396 TopOpeBRepDS_DataMapOfShapeState& aMapSS)
398 Standard_Integer i, nSub, j, nW;
399 const TopTools_ListOfShape& aListOfShapes=aMapSubshAnc.FindFromKey(aSubsh);
400 TopTools_ListIteratorOfListOfShape anIt(aListOfShapes);
401 for (; anIt.More(); anIt.Next()) {
403 const TopoDS_Shape& aShape=anIt.Value();
404 aMapSS.Bind (aShape, aState);
406 TopTools_IndexedMapOfShape aWireMap;
407 TopExp::MapShapes (aShape, TopAbs_WIRE, aWireMap);
408 nW=aWireMap.Extent();
409 for (j=1; j<=nW; j++) aMapSS.Bind (aWireMap(j), aState);
411 TopTools_IndexedMapOfShape aSubshMap;
412 TopExp::MapShapes (aShape, TopAbs_EDGE, aSubshMap);
413 nSub=aSubshMap.Extent();
414 for (i=1; i<=nSub; i++) {
415 const TopoDS_Shape& aSS=aSubshMap(i);
416 if (! aMapProcessedSubsh.Contains(aSS)) {
417 aMapProcessedSubsh.Add(aSS);
418 aMapSS.Bind (aSS, aState);
419 FindState1 (aSS, aState, aMapSubshAnc, aMapProcessedSubsh, aMapSS);
425 //=======================================================================
426 //function :TopOpeBRepBuild_Tools::FindStateThroughVertex
428 //=======================================================================
429 TopAbs_State TopOpeBRepBuild_Tools::FindStateThroughVertex (const TopoDS_Shape& aShape,
430 TopOpeBRepTool_ShapeClassifier& aShapeClassifier,
431 TopOpeBRepDS_IndexedDataMapOfShapeWithState& aMapOfShapeWithState,
432 const TopTools_MapOfShape& anAvoidSubshMap)
434 TopTools_IndexedMapOfShape aSubshMap;
435 TopExp::MapShapes (aShape, TopAbs_VERTEX, aSubshMap);
439 for (i=1; i <= aSubshMap.Extent() && aSubsh.IsNull(); i++)
440 if (!anAvoidSubshMap.Contains (aSubshMap(i)) )
441 aSubsh = aSubshMap(i);
442 if (aSubsh.IsNull()) {
445 TopExp::MapShapes (aShape, TopAbs_EDGE, aSubshMap);
446 for (i=1; i <= aSubshMap.Extent() && aSubsh.IsNull(); i++)
447 if (!anAvoidSubshMap.Contains (aSubshMap(i)) )
448 aSubsh = aSubshMap(i);
449 if (aSubsh.IsNull()) {
451 cout<<"FindStateThroughVertex: warning: all vertices are avoided"<<endl;
453 return TopAbs_UNKNOWN; // failure
457 TopoDS_Shape aNullShape;
458 TopAbs_State aState=aShapeClassifier.StateShapeReference(aSubsh, aNullShape);
459 TopOpeBRepDS_ShapeWithState aShapeWithState;
460 aShapeWithState.SetState (aState);
461 aShapeWithState.SetIsSplitted (Standard_False);
462 aMapOfShapeWithState.Add(aShape, aShapeWithState);
463 SpreadStateToChild (aShape, aState, aMapOfShapeWithState);
469 //=======================================================================
470 //function :TopOpeBRepBuild_Tools::SpreadStateToChild
472 //=======================================================================
473 void TopOpeBRepBuild_Tools::SpreadStateToChild (const TopoDS_Shape& aShape,
474 const TopAbs_State aState,
475 TopOpeBRepDS_IndexedDataMapOfShapeWithState& aMapOfShapeWithState)
477 TopTools_IndexedMapOfShape aChildMap;
478 TopExp::MapShapes (aShape, TopAbs_FACE, aChildMap);
479 TopExp::MapShapes (aShape, TopAbs_WIRE, aChildMap);
480 TopExp::MapShapes (aShape, TopAbs_EDGE, aChildMap);
482 TopOpeBRepDS_ShapeWithState aShapeWithState;
483 aShapeWithState.SetState (aState);
484 aShapeWithState.SetIsSplitted (Standard_False);
486 Standard_Integer i, n=aChildMap.Extent();
487 for (i=1; i<=n; i++) {
488 aMapOfShapeWithState.Add(aChildMap(i), aShapeWithState);
492 //=======================================================================
493 //function :TopOpeBRepBuild_Tools::PropagateStateForWires
495 //=======================================================================
496 void TopOpeBRepBuild_Tools::PropagateStateForWires(const TopTools_IndexedMapOfShape& aFacesToRestMap,
497 TopOpeBRepDS_IndexedDataMapOfShapeWithState&
498 aMapOfShapeWithState)
500 Standard_Integer i, j, nF, nW, k, nE;
502 nF=aFacesToRestMap.Extent();
503 for (i=1; i<=nF; i++) {
504 const TopoDS_Shape& aF=aFacesToRestMap(i);
505 if (aMapOfShapeWithState.Contains (aF)) {
506 const TopOpeBRepDS_ShapeWithState& aSWS=aMapOfShapeWithState.FindFromKey(aF);
507 TopAbs_State aSt=aSWS.State();
509 TopTools_IndexedMapOfShape aWireMap;
510 TopExp::MapShapes (aF, TopAbs_WIRE, aWireMap);
511 nW=aWireMap.Extent();
512 for (j=1; j<=nW; j++) {
513 const TopoDS_Shape& aW=aWireMap(j);
514 TopOpeBRepDS_ShapeWithState aWireSWS;
515 aWireSWS.SetState(aSt);
516 aWireSWS.SetIsSplitted (Standard_False);
517 aMapOfShapeWithState.Add(aW, aWireSWS);
519 TopTools_IndexedMapOfShape aEdgeMap;
520 TopExp::MapShapes (aW, TopAbs_EDGE, aEdgeMap);
521 nE=aEdgeMap.Extent();
522 for (k=1; k<=nE; k++) {
523 const TopoDS_Shape& aE=aEdgeMap(k);
524 if (!aMapOfShapeWithState.Contains (aE)) {
525 TopOpeBRepDS_ShapeWithState anEdgeSWS;
526 anEdgeSWS.SetState(aSt);
527 anEdgeSWS.SetIsSplitted (Standard_False);
528 aMapOfShapeWithState.Add(aE, anEdgeSWS);
536 //=======================================================================
537 //function :TopOpeBRepBuild_Tools:: GetNormalToFaceOnEdge
539 //=======================================================================
540 void TopOpeBRepBuild_Tools::GetNormalToFaceOnEdge (const TopoDS_Face& aFObj,
541 const TopoDS_Edge& anEdgeObj,
544 TopoDS_Edge aEd=anEdgeObj;
545 TopoDS_Face aFS=aFObj;
546 Standard_Real f2 = 0., l2 = 0., tolpc = 0., f = 0., l = 0., par = 0.;
547 Handle(Geom2d_Curve) C2D=FC2D_CurveOnSurface(aEd,aFS,f2,l2,tolpc, Standard_True);
549 BRepAdaptor_Curve aCA(aEd);
550 f=aCA.FirstParameter();
551 l=aCA.LastParameter();
552 par= f*PAR_T + (1 - PAR_T)*l;
555 C2D -> D0(par, aUV1);
559 BRepAdaptor_Surface aSA1(aFS);
560 aSA1.D1(aUV1.X(), aUV1.Y(), aP, aTg1, aTg2);
564 //=======================================================================
565 //function : TopOpeBRepBuild_Tools::GetNormalInNearestPoint
567 //=======================================================================
568 void TopOpeBRepBuild_Tools::GetNormalInNearestPoint(const TopoDS_Face& F,
569 const TopoDS_Edge& E,
572 Standard_Real f2 = 0., l2 = 0., tolpc = 0., par = 0.;
576 Handle(Geom2d_Curve) C2D = FC2D_CurveOnSurface(E, F, f2, l2, tolpc, Standard_True);
579 par = f2*PAR_T + (1 - PAR_T)*l2;
582 C2D -> D1(par, aP, aTangent);
584 Standard_Real Xnorm = -aTangent.Y();
585 Standard_Real Ynorm = aTangent.X();
587 Standard_Real step = TopOpeBRepTool_TOOL::minDUV(F); step *= 1e-2;
589 gp_Vec2d aPV(aP.X(), aP.Y());
590 gp_Dir2d aStepV(Xnorm, Ynorm);
591 gp_Vec2d aNorm2d = aPV + gp_Vec2d(step*aStepV);
593 Standard_Real newU = aNorm2d.X();
594 Standard_Real newV = aNorm2d.Y();
598 BRepAdaptor_Surface BS(F);
599 BS.D1(newU, newV, aP1, aTg1, aTg2);
602 gp_Pnt2d aP2d(newU, newV);
603 BRepTopAdaptor_FClass2d FC(F, Precision::PConfusion());
604 TopAbs_State aState = FC.Perform(aP2d);
606 //point out of face: try to go at another direction
607 if(aState == TopAbs_OUT) {
609 aNorm2d = aPV + gp_Vec2d(step*aStepV);
614 BS.D1(newU, newV, aP1, aTg1, aTg2);
616 //in principle, we must check again
617 // aP2d.SetX(newU); aP2d.SetY(newV);
618 // BRepClass_FaceClassifier FC(Fex, aP2d, 1e-7);
619 // TopAbs_State aState = FC.State();
626 //=======================================================================
627 //function : TopOpeBRepBuild_Tools::GetTangentToEdgeEdge
629 //=======================================================================
630 Standard_Boolean TopOpeBRepBuild_Tools::GetTangentToEdgeEdge (const TopoDS_Face& ,//aFObj,
631 const TopoDS_Edge& anEdgeObj,
632 const TopoDS_Edge& aOriEObj,
636 if (BRep_Tool::Degenerated(aOriEObj) ||
637 BRep_Tool::Degenerated(anEdgeObj)) {
638 return TopOpeBRepBuild_Tools::GetTangentToEdge (anEdgeObj, aTangent) ;
641 TopoDS_Edge aEd=anEdgeObj, aEOri = aOriEObj;
643 Standard_Real f = 0., l = 0., par = 0., parOri = 0.;
645 BRepAdaptor_Curve aCA(aEd);
646 BRepAdaptor_Curve aCAOri(aEOri);
648 f=aCA.FirstParameter();
649 l=aCA.LastParameter();
651 par= f*PAR_T + (1 - PAR_T)*l;
655 aCA.D1(par, aP, aTgPiece);
661 Handle (Geom_Curve) GCOri=aCAOri.Curve().Curve();
662 Handle (Geom_Curve) aCopyCurve = Handle(Geom_Curve)::DownCast(GCOri -> Copy());
664 const TopLoc_Location& aLoc = aEOri.Location();
665 gp_Trsf aTrsf = aLoc.Transformation();
666 aCopyCurve -> Transform(aTrsf);
668 GeomAPI_ProjectPointOnCurve aPP(aP, aCopyCurve, aCopyCurve->FirstParameter(), aCopyCurve->LastParameter());
670 // gp_Pnt aNP = aPP.NearestPoint();
672 parOri = aPP.LowerDistanceParameter();
674 aCopyCurve -> D1(parOri, aPOri, aTgOri);// aPOri must be equal aNP !
675 //printf(" aNP ={%lf, %lf, %lf}\n", aNP.X(), aNP.Y(), aNP.Z());
676 //printf(" aPOri={%lf, %lf, %lf}\n", aPOri.X(), aPOri.Y(), aPOri.Z());
677 if (aEd.Orientation() == TopAbs_REVERSED)
680 if(aTgOri*aTgPiece < 0.) {
682 return Standard_True;
684 return Standard_False;
688 //=======================================================================
689 //function : TopOpeBRepBuild_Tools::GetTangentToEdge
691 //=======================================================================
692 Standard_Boolean TopOpeBRepBuild_Tools::GetTangentToEdge (const TopoDS_Edge& anEdgeObj,
695 TopoDS_Edge aEd=anEdgeObj;
697 Standard_Real f = 0., l = 0., par = 0.;
699 BRepAdaptor_Curve aCA(aEd);
701 f=aCA.FirstParameter();
702 l=aCA.LastParameter();
704 par= f*PAR_T + (1 - PAR_T)*l;
706 aCA.D1(par, aP, aTangent);
708 return Standard_True;
712 //=======================================================================
713 //function : TopOpeBRepBuild_Tools::GetAdjacentFace
715 //=======================================================================
716 Standard_Boolean TopOpeBRepBuild_Tools::GetAdjacentFace (const TopoDS_Shape& aFaceObj,
717 const TopoDS_Shape& anEObj,
718 const TopTools_IndexedDataMapOfShapeListOfShape& anEdgeFaceMap,
719 TopoDS_Shape& anAdjFaceObj)
721 const TopTools_ListOfShape& aListOfAdjFaces=anEdgeFaceMap.FindFromKey(anEObj);
722 TopTools_ListIteratorOfListOfShape anIt(aListOfAdjFaces);
723 TopoDS_Shape anAdjShape;
724 for (; anIt.More(); anIt.Next()) {
725 if (anIt.Value()!=aFaceObj) {
726 anAdjShape=anIt.Value();
731 if (!anAdjShape.IsNull()) {
732 anAdjFaceObj=TopoDS::Face(anAdjShape);
733 return Standard_True;
736 return Standard_False;
740 //=======================================================================
741 //function : UpdatePCurves
743 //=======================================================================
744 void TopOpeBRepBuild_Tools::UpdatePCurves(const TopoDS_Wire& aWire,
745 const TopoDS_Face& fromFace,
746 const TopoDS_Face& toFace)
748 TopExp_Explorer aExp(aWire, TopAbs_EDGE);
750 for(; aExp.More(); aExp.Next()) {
751 TopoDS_Shape aEdge = aExp.Current();
752 UpdateEdgeOnFace(TopoDS::Edge(aEdge), fromFace, toFace);
756 //=======================================================================
757 //function : UpdateEdgeOnFace
759 //=======================================================================
760 void TopOpeBRepBuild_Tools::UpdateEdgeOnFace(const TopoDS_Edge& aEdgeToUpdate,
761 const TopoDS_Face& fromFace,
762 const TopoDS_Face& toFace)
766 Standard_Real tolE = BRep_Tool::Tolerance(TopoDS::Edge(aEdgeToUpdate));
767 Standard_Real f2 = 0.,l2 = 0.,tolpc = 0.;
768 Handle(Geom2d_Curve) C2D;
770 if(BRep_Tool::Degenerated(aEdgeToUpdate)) {
771 //we can not compute PCurve for Degenerated Edge
772 //so we take as it was on old face and after (in CorrectFace2D)
773 // we will adjust this PCurve
774 C2D = FC2D_CurveOnSurface(aEdgeToUpdate, fromFace,
775 f2, l2, tolpc, Standard_True);
776 Standard_Real tol = Max(tolE, tolpc);
777 Handle(Geom2d_Curve) C2Dn = Handle(Geom2d_Curve)::DownCast(C2D -> Copy());
778 Handle(Geom2d_TrimmedCurve) newC2D = new Geom2d_TrimmedCurve(C2Dn, f2, l2);
779 BB.UpdateEdge(aEdgeToUpdate ,newC2D, toFace , tol);
782 else { //not degenerated edge
784 if(BRep_Tool::IsClosed(aEdgeToUpdate, fromFace)) {
785 UpdateEdgeOnPeriodicalFace(aEdgeToUpdate, fromFace, toFace);
788 C2D = FC2D_CurveOnSurface(aEdgeToUpdate, toFace , f2, l2, tolpc, Standard_True);
789 Standard_Real tol = Max(tolE,tolpc);
790 BB.UpdateEdge(aEdgeToUpdate ,C2D, toFace , tol);
795 //=======================================================================
796 //function : UpdateEdgeOnPeriodicalFace
798 //=======================================================================
799 void TopOpeBRepBuild_Tools::UpdateEdgeOnPeriodicalFace(const TopoDS_Edge& aEdgeToUpdate,
800 const TopoDS_Face& fromFace,
801 const TopoDS_Face& toFace)
803 Standard_Boolean DiffOriented = Standard_False;
805 TopoDS_Edge newE = aEdgeToUpdate; //newE.Orientation(TopAbs_FORWARD);
806 TopoDS_Face fFace = fromFace; //fFace.Orientation(TopAbs_FORWARD);
807 TopoDS_Face tFace = toFace; //tFace.Orientation(TopAbs_FORWARD);
808 Standard_Real fc = 0., lc = 0.;
810 Handle(Geom2d_Curve) cc = BRep_Tool::CurveOnSurface(newE, tFace, fc, lc);
813 //cout << "Pcurves exist" << endl;
818 TopOpeBRepBuild_Tools::GetNormalToFaceOnEdge(TopoDS::Face(fromFace),
819 TopoDS::Edge(aEdgeToUpdate), aN1);
822 TopOpeBRepBuild_Tools::GetNormalToFaceOnEdge(TopoDS::Face(toFace),
823 TopoDS::Edge(aEdgeToUpdate), aN2);
826 DiffOriented = Standard_True;
828 Standard_Real tolE = BRep_Tool::Tolerance(newE);
829 Standard_Real f2 = 0., l2 = 0., tolpc = 0., tol = 0.;
832 Handle(Geom2d_Curve) C2D = FC2D_CurveOnSurface(newE,
834 l2, tolpc, Standard_True);
836 tol = Max(tolpc, tolE);
838 BRepAdaptor_Surface aBAS(fFace);
841 Standard_Real ff = 0., lf = 0., fr = 0., lr = 0.;
844 Handle(Geom2d_Curve) oldC2DFor = BRep_Tool::CurveOnSurface(newE, //FC2D_CurveOnSurface(newE,
846 lf);//, tolpc, Standard_True);
848 Handle(Geom2d_Curve) oldC2DRev = BRep_Tool::CurveOnSurface(newE, //FC2D_CurveOnSurface(newE,
850 lr);//, tolpc, Standard_True);
852 oldC2DFor -> D0(ff, aUVf);
853 oldC2DRev -> D0(fr, aUVr);
856 aTrV = gp_Vec2d(aUVf, aUVr);
858 aTrV = gp_Vec2d(aUVr, aUVf);
860 gp_Vec2d aux(gp_Pnt2d(0., 0.), gp_Pnt2d(1., 1.));
861 Standard_Real scalar = aux*aTrV;
862 Standard_Boolean dir = (scalar >= 0.) ? Standard_True : Standard_False;
864 //compute right order of pcurves
865 gp_Vec2d aYVec(gp_Pnt2d(0., 0.), gp_Pnt2d(0., 1.));
866 gp_Pnt2d aUVfv, aUVlv;
867 C2D -> D0(f2, aUVfv);
868 C2D -> D0(l2, aUVlv);
869 gp_Vec2d C2DVec(aUVfv, aUVlv);
871 Standard_Boolean firstOrder = Standard_True;
872 scalar = aYVec*C2DVec;
873 if(fabs(scalar) <= 1e-10) {// compute along X axe
874 gp_Vec2d aXVec(gp_Pnt2d(0., 0.), gp_Pnt2d(1., 0.));
875 scalar = aXVec*C2DVec;
876 firstOrder = (scalar >= 0.) ? Standard_True : Standard_False;
879 firstOrder = (scalar > 0.) ? Standard_False : Standard_True;
881 Handle(Geom2d_Curve) aTrC = Handle(Geom2d_Curve)::DownCast(C2D->Copy());
882 aTrC -> Translate(aTrV);
886 BB.UpdateEdge(aEdgeToUpdate, C2D, aTrC, toFace, tol);
888 BB.UpdateEdge(aEdgeToUpdate, aTrC, C2D, toFace, tol);
892 BB.UpdateEdge(aEdgeToUpdate, C2D, aTrC, toFace, tol);
894 BB.UpdateEdge(aEdgeToUpdate, aTrC, C2D, toFace, tol);
898 //=======================================================================
899 //function : TopOpeBRepBuild_Tools::IsDegEdgesTheSame
901 //=======================================================================
902 Standard_Boolean TopOpeBRepBuild_Tools::IsDegEdgesTheSame (const TopoDS_Shape& anE1,
903 const TopoDS_Shape& anE2)
905 TopTools_IndexedMapOfShape aVMap1, aVMap2;
906 TopExp::MapShapes(anE1, TopAbs_VERTEX, aVMap1);
907 TopExp::MapShapes(anE2, TopAbs_VERTEX, aVMap2);
909 if (!aVMap1.Extent() || !aVMap2.Extent())
910 return Standard_False;
912 if (aVMap1(1).IsSame(aVMap2(1)))
913 return Standard_True;
915 return Standard_False;
918 //=======================================================================
919 //function : NormalizeFace
920 //purpose : remove all INTERNAL and EXTERNAL edges from the face
921 //=======================================================================
922 void TopOpeBRepBuild_Tools::NormalizeFace(const TopoDS_Shape& oldFace,
923 TopoDS_Shape& corrFace)
928 TopoDS_Face aF1 = TopoDS::Face(oldFace),
931 aF1.Orientation(TopAbs_FORWARD);
933 Handle(Geom_Surface) Surf = BRep_Tool::Surface(aF1, Loc);
934 tolF1 = BRep_Tool::Tolerance(aF1);
936 BB.MakeFace(aNewFace, Surf, Loc, tolF1);
938 TopExp_Explorer aFExp(aF1, TopAbs_WIRE);
939 for (; aFExp.More(); aFExp.Next()) {
940 Standard_Integer NbGoodEdges = 0;
941 TopoDS_Shape aWire=aFExp.Current();
942 aWire.Orientation(TopAbs_FORWARD);
943 TopoDS_Wire aNewWire;
945 BB.MakeWire(aNewWire);
947 TopExp_Explorer aWExp(aWire, TopAbs_EDGE);
948 for (; aWExp.More(); aWExp.Next()) {
949 TopoDS_Shape anEdge=aWExp.Current();
951 if (anEdge.Orientation()==TopAbs_EXTERNAL ||
952 anEdge.Orientation()==TopAbs_INTERNAL)
955 BB.Add (aNewWire, TopoDS::Edge(anEdge));
958 //keep wire orientation
959 aNewWire.Orientation(aFExp.Current().Orientation());//aWire.Orientation());
961 if(NbGoodEdges) //we add new wire only if it contains at least one edge
962 BB.Add (aNewFace, aNewWire);
964 //keep face orientation
965 aNewFace.Orientation(oldFace.Orientation());
971 //=======================================================================
972 //function : CorrectFace2d
973 //purpose : adjust PCurves of periodical face in 2d
974 //=======================================================================
975 void TopOpeBRepBuild_Tools::CorrectFace2d (const TopoDS_Shape& oldFace,
976 TopoDS_Shape& corrFace,
977 const TopTools_IndexedMapOfOrientedShape& aSourceShapes,
978 TopTools_IndexedDataMapOfShapeShape& aMapOfCorrect2dEdges)
980 TopOpeBRepBuild_CorrectFace2d aCorrectFace2d(TopoDS::Face(oldFace),
982 aMapOfCorrect2dEdges);
985 aCorrectFace2d.Perform();