1 // Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : GEOMAlgo_Builder_3.cxx
24 // Author : Peter KURNEV
26 #include <GEOMAlgo_Builder.hxx>
28 #include <TopAbs_State.hxx>
31 #include <TopoDS_Iterator.hxx>
32 #include <TopoDS_Solid.hxx>
33 #include <TopoDS_Shape.hxx>
34 #include <TopoDS_Face.hxx>
35 #include <TopoDS_Solid.hxx>
36 #include <TopoDS_Iterator.hxx>
37 #include <TopoDS_Shell.hxx>
38 #include <TopoDS_Compound.hxx>
41 #include <TopExp_Explorer.hxx>
43 #include <BRep_Builder.hxx>
44 #include <BRepTools.hxx>
45 #include <BRepClass3d_SolidClassifier.hxx>
47 #include <TopTools_ListOfShape.hxx>
48 #include <TopTools_IndexedMapOfShape.hxx>
49 #include <TopTools_ListIteratorOfListOfShape.hxx>
50 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
51 #include <TopTools_IndexedMapOfShape.hxx>
52 #include <TopTools_MapIteratorOfMapOfShape.hxx>
53 #include <TopTools_DataMapOfShapeShape.hxx>
54 #include <TopTools_DataMapOfShapeInteger.hxx>
55 #include <TopTools_DataMapIteratorOfDataMapOfShapeInteger.hxx>
56 #include <TopTools_MapOfShape.hxx>
58 #include <IntTools_Context.hxx>
60 #include <NMTDS_ShapesDataStructure.hxx>
61 #include <NMTTools_PaveFiller.hxx>
63 #include <GEOMAlgo_Tools3D.hxx>
64 #include <GEOMAlgo_BuilderSolid.hxx>
65 #include <GEOMAlgo_ShapeSet.hxx>
66 #include <GEOMAlgo_DataMapOfShapeShapeSet.hxx>
67 #include <GEOMAlgo_DataMapIteratorOfDataMapOfShapeShapeSet.hxx>
72 void OwnInternalShapes(const TopoDS_Shape& ,
73 TopTools_IndexedMapOfShape& );
75 //=======================================================================
76 //function : FillImagesSolids
78 //=======================================================================
79 void GEOMAlgo_Builder::FillImagesSolids()
87 //=======================================================================
88 //function : BuildDraftSolid
90 //=======================================================================
91 void GEOMAlgo_Builder::BuildDraftSolid (const TopoDS_Shape& theSolid,
92 TopoDS_Shape& theDraftSolid,
93 TopTools_ListOfShape& theLIF)
97 /*const NMTDS_ShapesDataStructure& aDS=**/myPaveFiller->DS();
98 NMTTools_PaveFiller* pPF=myPaveFiller;
99 IntTools_Context& aCtx= pPF->ChangeContext();
101 Standard_Boolean bToReverse;
102 Standard_Integer iFlag;
103 TopAbs_Orientation aOrF, aOrSh, aOrSd;
104 TopoDS_Iterator aIt1, aIt2;
105 TopTools_ListIteratorOfListOfShape aItS;
108 TopoDS_Shape aFSDx, aFx;
110 aOrSd=theSolid.Orientation();
111 theDraftSolid.Orientation(aOrSd);
113 aIt1.Initialize(theSolid);
114 for (; aIt1.More(); aIt1.Next()) {
115 const TopoDS_Shape& aSh=aIt1.Value();
116 if(aSh.ShapeType()!=TopAbs_SHELL) {
117 continue; // mb internal edges,vertices
120 aOrSh=aSh.Orientation();
122 aShD.Orientation(aOrSh);
125 aIt2.Initialize(aSh);
126 for (; aIt2.More(); aIt2.Next()) {
127 const TopoDS_Shape& aF=aIt2.Value();
128 aOrF=aF.Orientation();
130 if (myImages.HasImage(aF)) {
131 const TopTools_ListOfShape& aLSp=myImages.Image(aF);
132 aItS.Initialize(aLSp);
133 for (; aItS.More(); aItS.Next()) {
136 if (mySameDomainShapes.Contains(aFx)) {
137 aFSDx=mySameDomainShapes.FindFromKey(aFx);
139 if (aOrF==TopAbs_INTERNAL) {
140 aFSDx.Orientation(aOrF);
141 theLIF.Append(aFSDx);
144 bToReverse=GEOMAlgo_Tools3D::IsSplitToReverse(aFSDx, aF, aCtx);
150 aBB.Add(aShD, aFSDx);
152 }// if (mySameDomainShapes.Contains(aFx)) {
154 aFx.Orientation(aOrF);
155 if (aOrF==TopAbs_INTERNAL) {
164 } //if (myImages.HasImage(aF)) {
167 if (aOrF==TopAbs_INTERNAL) {
175 } //for (; aIt2.More(); aIt2.Next()) {
178 aBB.Add(theDraftSolid, aShD);
180 } //for (; aIt1.More(); aIt1.Next()) {
182 //=======================================================================
183 //function : FillIn3DParts
185 //=======================================================================
186 void GEOMAlgo_Builder::FillIn3DParts()
190 const NMTDS_ShapesDataStructure& aDS=*myPaveFiller->DS();
191 NMTTools_PaveFiller* pPF=myPaveFiller;
192 IntTools_Context& aCtx= pPF->ChangeContext();
194 Standard_Boolean bIsIN, bHasImage;
195 Standard_Integer aNbS, aNbSolids, i, j, aNbFaces, aNbFP, aNbFPx, aNbFIN, aNbLIF;
196 TopAbs_ShapeEnum aType;
198 TopTools_IndexedMapOfShape aMSolids, aMS, aMFaces, aMFIN;
199 TopTools_MapOfShape aMFDone;
200 TopTools_IndexedDataMapOfShapeListOfShape aMEF;
201 TopTools_ListIteratorOfListOfShape aItS;
202 TopoDS_Iterator aIt, aItF;
204 TopoDS_Solid aSolidSp;
207 myDraftSolids.Clear();
209 aNbS=aDS.NumberOfShapesOfTheObject();
210 for (i=1; i<=aNbS; ++i) {
211 const TopoDS_Shape& aS=aDS.Shape(i);
213 aType=aS.ShapeType();
214 if (aType==TopAbs_SOLID) {
215 // all solids from DS
218 else if (aType==TopAbs_FACE) {
219 // all faces (originals from DS or theirs images)
220 if (myImages.HasImage(aS)) {
221 const TopTools_ListOfShape& aLS=myImages.Image(aS);
222 aItS.Initialize(aLS);
223 for (; aItS.More(); aItS.Next()) {
224 const TopoDS_Shape& aFx=aItS.Value();
226 if (mySameDomainShapes.Contains(aFx)) {
227 const TopoDS_Shape& aFSDx=mySameDomainShapes.FindFromKey(aFx);
236 if (mySameDomainShapes.Contains(aS)) {
237 const TopoDS_Shape& aFSDx=mySameDomainShapes.FindFromKey(aS);
247 aNbFaces=aMFaces.Extent();
248 aNbSolids=aMSolids.Extent();
250 for (i=1; i<=aNbSolids; ++i) {
251 const TopoDS_Solid& aSolid=TopoDS::Solid(aMSolids(i));
256 aBB.MakeSolid(aSolidSp);
258 TopTools_ListOfShape aLIF;
260 BuildDraftSolid(aSolid, aSolidSp, aLIF);
261 aNbLIF=aLIF.Extent();
263 // 1 all faces/edges from aSolid [ aMS ]
264 bHasImage=Standard_False;
266 aIt.Initialize(aSolid);
267 for (; aIt.More(); aIt.Next()) {
268 const TopoDS_Shape& aShell=aIt.Value();
270 if (myImages.HasImage(aShell)) {
271 bHasImage=Standard_True;
273 const TopTools_ListOfShape& aLS=myImages.Image(aShell);
274 aItS.Initialize(aLS);
275 for (; aItS.More(); aItS.Next()) {
276 const TopoDS_Shape& aSx=aItS.Value();
278 TopExp::MapShapes(aSx, TopAbs_FACE, aMS);
279 TopExp::MapShapes(aSx, TopAbs_EDGE, aMS);
280 TopExp::MapShapesAndAncestors(aSx, TopAbs_EDGE, TopAbs_FACE, aMEF);
285 TopExp::MapShapes(aShell, TopAbs_FACE, aMS);
286 //modified by NIZNHY-PKV Fri Dec 03 11:18:45 2010f
287 TopExp::MapShapes(aShell, TopAbs_EDGE, aMS);
288 //modified by NIZNHY-PKV Fri Dec 03 11:18:51 2010t
289 TopExp::MapShapesAndAncestors(aShell, TopAbs_EDGE, TopAbs_FACE, aMEF);
293 // 2 all faces that are not from aSolid [ aLFP1 ]
294 Standard_Integer aNbEFP;
295 TopTools_IndexedDataMapOfShapeListOfShape aMEFP;
296 TopTools_ListIteratorOfListOfShape aItFP, aItEx;
297 TopTools_MapOfShape aMFence;
298 TopTools_ListOfShape aLFP1, aLFP2, aLFP, aLCBF, aLFIN, aLEx;//*pLFP,
300 // for all non-solid faces build EF map [ aMEFP ]
301 for (j=1; j<=aNbFaces; ++j) {
302 const TopoDS_Shape& aFace=aMFaces(j);
303 if (!aMS.Contains(aFace)) {
304 TopExp::MapShapesAndAncestors(aFace, TopAbs_EDGE, TopAbs_FACE, aMEFP);
308 // among all faces from aMEFP select these that have same edges
309 // with the solid (i.e aMEF). These faces will be treated first
310 // to prevent the usage of 3D classifier.
311 // The full list of faces to process is aLFP1.
312 aNbEFP=aMEFP.Extent();
313 for (j=1; j<=aNbEFP; ++j) {
314 const TopoDS_Shape& aE=aMEFP.FindKey(j);
316 if (aMEF.Contains(aE)) { // !!
317 const TopTools_ListOfShape& aLF=aMEFP(j);
318 aItFP.Initialize(aLF);
319 for (; aItFP.More(); aItFP.Next()) {
320 const TopoDS_Shape& aF=aItFP.Value();
321 if (aMFence.Add(aF)) {
331 aItEx.Initialize(aLEx);
332 for (; aItEx.More(); aItEx.Next()) {
333 const TopoDS_Shape& aE=aItEx.Value();
334 const TopTools_ListOfShape& aLF=aMEFP.FindFromKey(aE);
335 aItFP.Initialize(aLF);
336 for (; aItFP.More(); aItFP.Next()) {
337 const TopoDS_Shape& aF=aItFP.Value();
338 if (aMFence.Add(aF)) {
346 // 3 Process faces aLFP1
347 aNbFP=aLFP1.Extent();
348 aItFP.Initialize(aLFP1);
349 for (; aItFP.More(); aItFP.Next()) {
350 const TopoDS_Shape& aSP=aItFP.Value();
351 if (!aMFDone.Add(aSP)) {
356 // first face to process
357 aFP=TopoDS::Face(aSP);
358 bIsIN= GEOMAlgo_Tools3D::IsInternalFace(aFP, aSolidSp, aMEF, 1.e-14, aCtx);
359 aState=(bIsIN) ? TopAbs_IN : TopAbs_OUT;
361 // collect faces to process [ aFP is the first ]
364 aItS.Initialize(aLFP1);
365 for (; aItS.More(); aItS.Next()) {
366 const TopoDS_Shape& aSk=aItS.Value();
367 if (!aMFDone.Contains(aSk)) {
372 // Connexity Block that spreads from aFP the Bound
373 // or till the end of the block itself
375 GEOMAlgo_Tools3D::MakeConnexityBlock(aLFP, aMS, aLCBF);
377 // fill states for the Connexity Block
378 aItS.Initialize(aLCBF);
379 for (; aItS.More(); aItS.Next()) {
380 const TopoDS_Shape& aSx=aItS.Value();
382 if (aState==TopAbs_IN) {
387 aNbFPx=aMFDone.Extent();
391 }//for (; aItFP.More(); aItFP.Next())
393 // faces Inside aSolid
395 aNbFIN=aMFIN.Extent();
396 if (aNbFIN || aNbLIF) {
397 for (j=1; j<=aNbFIN; ++j) {
398 const TopoDS_Shape& aFIN=aMFIN(j);
402 aItS.Initialize(aLIF);
403 for (; aItS.More(); aItS.Next()) {
404 const TopoDS_Shape& aFIN=aItS.Value();
408 myInParts.Add(aSolid, aLFIN);
410 if (aNbFIN || bHasImage) {
411 myDraftSolids.Add(aSolid, aSolidSp);
413 }//for (i=1; i<=aNbSolids; ++i) { // next solid
415 //=======================================================================
416 //function : BuildSplitSolids
418 //=======================================================================
419 void GEOMAlgo_Builder::BuildSplitSolids()
423 const NMTDS_ShapesDataStructure& aDS=*myPaveFiller->DS();
424 NMTTools_PaveFiller* pPF=myPaveFiller;
425 IntTools_Context& aCtx= pPF->ChangeContext();
427 Standard_Integer i, aNbS, iErr;
428 TopExp_Explorer aExp;
429 TopTools_ListOfShape aSFS, aLSEmpty;
430 TopTools_MapOfShape aMFence;
431 TopTools_ListIteratorOfListOfShape aIt;
432 GEOMAlgo_BuilderSolid aSB;
433 GEOMAlgo_DataMapIteratorOfDataMapOfShapeShapeSet aItSS;
434 GEOMAlgo_DataMapOfShapeShapeSet aMSS;
435 GEOMAlgo_ShapeSet aSSi;
437 // 0. Find same domain solids for non-interferred solids
438 aNbS=aDS.NumberOfShapesOfTheObject();
439 for (i=1; i<=aNbS; ++i) {
440 const TopoDS_Shape& aS=aDS.Shape(i);
441 if (aS.ShapeType()!=TopAbs_SOLID) {
444 if (!aMFence.Add(aS)) {
447 if(myDraftSolids.Contains(aS)) {
452 aSSi.Add(aS, TopAbs_FACE);
455 } //for (i=1; i<=aNbS; ++i)
457 // 1. Build solids for interferred source solids
458 aSB.SetContext(aCtx);
459 aSB.ComputeInternalShapes(myComputeInternalShapes);
460 aNbS=myDraftSolids.Extent();
461 for (i=1; i<=aNbS; ++i) {
462 const TopoDS_Shape& aS =myDraftSolids.FindKey(i);
463 const TopoDS_Shape& aSD=myDraftSolids.FindFromIndex(i);
464 const TopTools_ListOfShape& aLFIN=
465 (myInParts.Contains(aS)) ? myInParts.FindFromKey(aS) : aLSEmpty;
467 // 1.1 Fill Shell Faces Set
470 aExp.Init(aSD, TopAbs_FACE);
471 for (; aExp.More(); aExp.Next()) {
472 const TopoDS_Shape& aF=aExp.Current();
476 aIt.Initialize(aLFIN);
477 for (; aIt.More(); aIt.Next()) {
478 TopoDS_Shape aF=aIt.Value();
480 aF.Orientation(TopAbs_FORWARD);
482 aF.Orientation(TopAbs_REVERSED);
486 Standard_Integer aNbSFS;
487 aNbSFS=aSFS.Extent();
490 // Check whether aSFS contains a subsets of faces
491 // of solids that have been already built.
492 // If yes, shrink aSFS by these subsets.
496 aItSS.Initialize(aMSS);
497 for (; aItSS.More(); aItSS.Next()) {
498 const TopoDS_Shape& aSR=aItSS.Key();
499 const GEOMAlgo_ShapeSet& aSSR=aItSS.Value();
500 if (aSSi.Contains(aSSR)) {
501 // the aSR is SD solid for aS
504 if(myImages.HasImage(aS)) {
505 myImages.Add(aS, aSR);
508 myImages.Bind(aS, aSR);
512 mySameDomainShapes.Add(aSR, aSR);
515 const TopTools_ListOfShape& aSFS1=aSSi.GetSet();
516 aNbSFS=aSFS1.Extent();
517 //modified by NIZNHY-PKV Wed Oct 27 09:53:15 2010f
521 //modified by NIZNHY-PKV Wed Oct 27 09:53:18 2010t
523 // 1.3 Build new solids
524 aSB.SetShapes(aSFS1);
526 iErr=aSB.ErrorStatus();
528 myErrorStatus=30; // SolidBuilder failed
532 const TopTools_ListOfShape& aLSR=aSB.Areas();
534 // 1.4 Collect resulting solids and theirs set of faces
535 aIt.Initialize(aLSR);
536 for (; aIt.More(); aIt.Next()) {
537 const TopoDS_Shape& aSR=aIt.Value();
540 aExp.Init(aSR, TopAbs_FACE);
541 for (; aExp.More(); aExp.Next()) {
542 const TopoDS_Shape& aF=aExp.Current();
545 aMSS.Bind(aSR, aSSi);
549 if (myImages.HasImage(aS)) {
550 myImages.Add(aS, aLSR);
553 myImages.Bind(aS, aLSR);
555 } // for (i=1; i<=aNbS; ++i) {
557 //=======================================================================
558 //function :FillInternalShapes
560 //=======================================================================
561 void GEOMAlgo_Builder::FillInternalShapes()
565 const NMTDS_ShapesDataStructure& aDS=*myPaveFiller->DS();
566 NMTTools_PaveFiller* pPF=myPaveFiller;
567 IntTools_Context& aCtx= pPF->ChangeContext();
569 //Standard_Boolean bHasImage;
570 Standard_Integer i, j, jT, aNbS, aNbSI, aNbSx, aNbSd;
571 TopAbs_ShapeEnum aType, aT[]={ TopAbs_VERTEX, TopAbs_EDGE };
573 TopTools_ListIteratorOfListOfShape aIt, aIt1;
574 TopTools_IndexedDataMapOfShapeListOfShape aMSx;
575 TopTools_IndexedMapOfShape aMx;
576 TopTools_MapOfShape aMSI, aMFence, aMSOr;
577 TopTools_MapIteratorOfMapOfShape aItM;
578 TopTools_ListOfShape aLSI, aLSd;
579 TopoDS_Iterator aItS;
582 // 1. Shapes to process
584 // 1.1 Shapes from pure arguments aMSI
585 // 1.1.1 vertex, edge
586 for (i=0; i<2; ++i) {
587 jT=(Standard_Integer)aT[i];
588 const TopTools_ListOfShape &aLS=myShapes1[jT];
590 for (; aIt.More(); aIt.Next()) {
591 const TopoDS_Shape& aS=aIt.Value();
592 if (aMFence.Add(aS)) {
599 jT=(Standard_Integer)TopAbs_WIRE;
600 const TopTools_ListOfShape &aLW=myShapes1[jT];
602 for (; aIt.More(); aIt.Next()) {
603 const TopoDS_Shape& aW=aIt.Value();
605 for (; aItS.More(); aItS.Next()) {
606 const TopoDS_Shape& aE=aItS.Value();
607 if (aMFence.Add(aE)) {
613 // 1.1.3 theirs images/sources
614 aIt1.Initialize(aLSI);
615 for (; aIt1.More(); aIt1.Next()) {
616 const TopoDS_Shape& aS=aIt1.Value();
617 if (myImages.HasImage(aS)) {
618 const TopTools_ListOfShape &aLSp=myImages.Image(aS);
619 aIt.Initialize(aLSp);
620 for (; aIt.More(); aIt.Next()) {
621 const TopoDS_Shape& aSI=aIt.Value();
632 // 2. Internal vertices, edges from source solids
636 aNbS=aDS.NumberOfShapesOfTheObject();
637 for (i=1; i<=aNbS; ++i) {
638 const TopoDS_Shape& aS=aDS.Shape(i);
639 aType=aS.ShapeType();
640 if (aType==TopAbs_SOLID) {
643 OwnInternalShapes(aS, aMx);
646 for (j=1; j<=aNbSx; ++j) {
647 const TopoDS_Shape& aSI=aMx(j);
648 if (myImages.HasImage(aSI)) {
649 const TopTools_ListOfShape &aLSp=myImages.Image(aSI);
650 aIt.Initialize(aLSp);
651 for (; aIt.More(); aIt.Next()) {
652 const TopoDS_Shape& aSp=aIt.Value();
661 // build aux map from splits of solids
662 if (myImages.HasImage(aS)) {
663 const TopTools_ListOfShape &aLSp=myImages.Image(aS);
664 aIt.Initialize(aLSp);
665 for (; aIt.More(); aIt.Next()) {
666 const TopoDS_Shape& aSp=aIt.Value();
667 if (aMFence.Add(aSp)) {
668 TopExp::MapShapesAndAncestors(aSp, TopAbs_VERTEX, TopAbs_EDGE, aMSx);
669 TopExp::MapShapesAndAncestors(aSp, TopAbs_VERTEX, TopAbs_FACE, aMSx);
670 TopExp::MapShapesAndAncestors(aSp, TopAbs_EDGE , TopAbs_FACE, aMSx);
676 if (aMFence.Add(aS)) {
677 TopExp::MapShapesAndAncestors(aS, TopAbs_VERTEX, TopAbs_EDGE, aMSx);
678 TopExp::MapShapesAndAncestors(aS, TopAbs_VERTEX, TopAbs_FACE, aMSx);
679 TopExp::MapShapesAndAncestors(aS, TopAbs_EDGE , TopAbs_FACE, aMSx);
684 }//if (aType==TopAbs_SOLID)
689 // 3. Some shapes of aMSI can be already tied with faces of
691 aItM.Initialize(aMSI);
692 for (; aItM.More(); aItM.Next()) {
693 const TopoDS_Shape& aSI=aItM.Key();
694 if (aMSx.Contains(aSI)) {
695 const TopTools_ListOfShape &aLSx=aMSx.FindFromKey(aSI);
709 // 5 Settle internal vertices and edges into solids
711 aIt.Initialize(aLSd);
712 for (; aIt.More(); aIt.Next()) {
713 TopoDS_Solid aSd=TopoDS::Solid(aIt.Value());
715 aItM.Initialize(aMSI);
716 for (; aItM.More(); aItM.Next()) {
717 TopoDS_Shape aSI=aItM.Key();
718 aSI.Orientation(TopAbs_INTERNAL);
720 aState=GEOMAlgo_Tools3D::ComputeStateByOnePoint(aSI, aSd, 1.e-11, aCtx);
721 if (aState==TopAbs_IN) {
723 if(aMSOr.Contains(aSd)) {
728 aItS.Initialize(aSd);
729 for (; aItS.More(); aItS.Next()) {
730 const TopoDS_Shape& aSh=aItS.Value();
736 myImages.Bind(aSd, aSdx);
745 } //if (aState==TopAbs_IN) {
746 }// for (; aItM.More(); aItM.Next()) {
747 }//for (; aIt1.More(); aIt1.Next()) {
749 //=======================================================================
750 //function : OwnInternalShapes
752 //=======================================================================
753 void OwnInternalShapes(const TopoDS_Shape& theS,
754 TopTools_IndexedMapOfShape& theMx)
758 aIt.Initialize(theS);
759 for (; aIt.More(); aIt.Next()) {
760 const TopoDS_Shape& aSx=aIt.Value();
761 if (aSx.ShapeType()!=TopAbs_SHELL) {
768 // 30 - SolidBuilder failed