1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 #include <ShapeFix_Solid.ixx>
16 #include <Standard_ErrorHandler.hxx>
17 #include <Standard_Failure.hxx>
19 #include <BRep_Builder.hxx>
20 #include <BRepClass3d_SolidClassifier.hxx>
21 #include <Precision.hxx>
22 #include <TopoDS_Shape.hxx>
23 #include <ShapeBuild_ReShape.hxx>
24 #include <TopoDS_Iterator.hxx>
26 #include <ShapeExtend.hxx>
27 #include <ShapeAnalysis_Edge.hxx>
28 #include <ShapeAnalysis_Curve.hxx>
29 #include <TopoDS_Wire.hxx>
30 #include <ShapeExtend_WireData.hxx>
31 #include <TopTools_MapIteratorOfMapOfShape.hxx>
32 #include <TopoDS_Iterator.hxx>
33 #include <TopTools_MapOfShape.hxx>
34 #include <TopTools_DataMapOfShapeListOfShape.hxx>
35 #include <TopTools_SequenceOfShape.hxx>
36 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
38 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
40 #include <Bnd_Box2d.hxx>
41 #include <ShapeAnalysis.hxx>
42 #include <TopoDS_Edge.hxx>
43 #include <BRep_Tool.hxx>
44 #include <Geom_Surface.hxx>
45 #include <TopTools_ListOfShape.hxx>
46 #include <Precision.hxx>
48 #include <TopTools_ListIteratorOfListOfShape.hxx>
49 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
50 #include <TopTools_ListIteratorOfListOfShape.hxx>
51 #include <TopoDS_Solid.hxx>
52 #include <BRep_Builder.hxx>
53 #include <TopoDS_CompSolid.hxx>
54 #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
56 #include <BRep_Tool.hxx>
57 #include <TopTools_IndexedMapOfShape.hxx>
58 #include <TopTools_IndexedDataMapOfShapeShape.hxx>
59 #include <Message_Msg.hxx>
60 #include <Message_ProgressSentry.hxx>
61 #include <TopoDS_Vertex.hxx>
62 #include <TopTools_IndexedMapOfShape.hxx>
64 #include <TopTools_DataMapOfShapeInteger.hxx>
65 #include <Geom_Curve.hxx>
66 #include <ShapeAnalysis_FreeBounds.hxx>
68 //======================================================
69 //function : ShapeFix_Solid
71 //=======================================================================
73 ShapeFix_Solid::ShapeFix_Solid()
75 myStatus = ShapeExtend::EncodeStatus (ShapeExtend_OK);
77 myFixShell = new ShapeFix_Shell;
78 myCreateOpenSolidMode = Standard_False;
81 //=======================================================================
82 //function : ShapeFix_Solid
84 //=======================================================================
86 ShapeFix_Solid::ShapeFix_Solid(const TopoDS_Solid& solid)
88 myStatus = ShapeExtend::EncodeStatus (ShapeExtend_OK);
90 myFixShell = new ShapeFix_Shell;
91 myCreateOpenSolidMode = Standard_False;
95 //=======================================================================
98 //=======================================================================
100 void ShapeFix_Solid::Init(const TopoDS_Solid& solid)
103 //mySolid = TopoDS::Solid(shape.EmptyCopied());
105 // for( TopoDS_Iterator iter(solid); iter.More(); iter.Next())
106 // B.Add(mySolid,TopoDS::Shell(iter.Value()));
109 #ifdef OCCT_DEBUG_GET_MIDDLE_POINT
110 //=======================================================================
111 //function : GetMiddlePoint
113 //=======================================================================
114 static void GetMiddlePoint(const TopoDS_Shape& aShape, gp_Pnt& pmid)
116 TopExp_Explorer aExp(aShape,TopAbs_EDGE);
117 gp_XYZ center(0.0,0.0,0.0);
118 Standard_Integer numpoints =0;
119 for( ; aExp.More(); aExp.Next()) {
120 TopoDS_Edge e1 = TopoDS::Edge(aExp.Current());
122 Handle(Geom_Curve) c3d = BRep_Tool::Curve(e1,f,l);
124 for(Standard_Integer i =1 ; i <=5; i++) {
125 Standard_Real param = f+(l-f)/4*(i-1);
138 //=======================================================================
139 //function : CollectSolids
141 //=======================================================================
142 static void CollectSolids(const TopTools_SequenceOfShape& aSeqShells ,
143 TopTools_DataMapOfShapeListOfShape& aMapShellHoles,
144 TopTools_DataMapOfShapeInteger& theMapStatus)
146 TopTools_MapOfShape aMapHoles;
147 for ( Standard_Integer i1 = 1; i1 <= aSeqShells.Length(); i1++ ) {
148 TopoDS_Shell aShell1 = TopoDS::Shell(aSeqShells.Value(i1));
149 TopTools_ListOfShape lshells;
150 aMapShellHoles.Bind(aShell1,lshells);
152 //Finds roots shells and hole shells.
153 for ( Standard_Integer i = 1; i <= aSeqShells.Length(); i++ ) {
154 TopoDS_Shell aShell1 = TopoDS::Shell(aSeqShells.Value(i));
155 TopExp_Explorer aExpEdges(aShell1,TopAbs_EDGE);
156 if(!BRep_Tool::IsClosed(aShell1) || !aExpEdges.More()) continue;
160 B.Add (solid,aShell1);
163 TopAbs_State infinstatus = TopAbs_UNKNOWN;
164 BRepClass3d_SolidClassifier bsc3d (solid);
165 Standard_Integer st = 0;
166 if(!theMapStatus.IsBound(aShell1)) {
168 bsc3d.PerformInfinitePoint(Precision::Confusion());
169 infinstatus = bsc3d.State();
171 if(infinstatus != TopAbs_UNKNOWN && infinstatus !=TopAbs_ON)
172 st = (infinstatus == TopAbs_IN ? 1 :2);
173 theMapStatus.Bind(aShell1,st);
178 st = theMapStatus.Find(aShell1);
180 infinstatus = (theMapStatus.Find(aShell1) == 1 ? TopAbs_IN : TopAbs_OUT);
183 for ( Standard_Integer j = 1; j <= aSeqShells.Length(); j++ ) {
185 TopoDS_Shape aShell2 = aSeqShells.Value(j);
186 if(!BRep_Tool::IsClosed(aShell2)) continue;
187 if(aMapHoles.Contains(aShell2)) continue;
188 if(aMapShellHoles.IsBound(aShell2)) {
189 Standard_Boolean isAnalysis = Standard_False;
190 const TopTools_ListOfShape& ls = aMapShellHoles.Find(aShell2);
191 for(TopTools_ListIteratorOfListOfShape li(ls); li.More() && !isAnalysis; li.Next())
192 isAnalysis = li.Value().IsSame(aShell1);
193 if(isAnalysis) continue;
195 TopAbs_State pointstatus = TopAbs_UNKNOWN;
196 Standard_Integer numon =0;
197 TopTools_IndexedMapOfShape amapVert;
198 for(TopExp_Explorer aExpVert(aShell2,TopAbs_VERTEX); aExpVert.More() && amapVert.Extent() < 10; aExpVert.Next())
199 amapVert.Add(aExpVert.Current());
200 for(Standard_Integer k = 1; k <= amapVert.Extent() &&
201 (pointstatus ==TopAbs_UNKNOWN || (pointstatus ==TopAbs_ON && numon < 3)); k++) {
202 TopoDS_Vertex aV = TopoDS::Vertex(amapVert.FindKey(k));
203 gp_Pnt aPf = BRep_Tool::Pnt(aV);
204 bsc3d.Perform(aPf,Precision::Confusion());
205 pointstatus =bsc3d.State();
206 if(pointstatus ==TopAbs_ON) numon++;
209 if(numon == 3 && pointstatus ==TopAbs_ON) {
210 #ifdef OCCT_DEBUG_GET_MIDDLE_POINT
212 GetMiddlePoint(aShell2,pmid);
213 bsc3d.Perform(pmid,Precision::Confusion());
215 pointstatus = /*(bsc3d.State() == TopAbs_IN ? TopAbs_IN :*/TopAbs_OUT;
217 if(pointstatus != infinstatus) {
218 aMapShellHoles.ChangeFind(aShell1).Append(aShell2);
219 if( aMapHoles.Contains(aShell2))
220 aMapHoles.Remove(aShell2);
221 else aMapHoles.Add(aShell2);
225 catch(Standard_Failure) {
227 cout << "Warning: ShapeFix_Solid::SolidFromShell: Exception: ";
228 Standard_Failure::Caught()->Print(cout); cout << endl;
233 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape aItShellHoles( aMapShellHoles);
234 for(; aItShellHoles.More();aItShellHoles.Next()) {
235 if(aMapHoles.Contains(aItShellHoles.Key())) continue;
236 const TopTools_ListOfShape& lHoles =aItShellHoles.Value();
237 if(lHoles.IsEmpty()) continue;
238 for(TopTools_ListIteratorOfListOfShape lItHoles(lHoles);lItHoles.More(); lItHoles.Next()) {
239 if(aMapHoles.Contains(lItHoles.Value())) {
240 const TopTools_ListOfShape& lUnHoles = aMapShellHoles.Find(lItHoles.Value());
241 for(TopTools_ListIteratorOfListOfShape lItUnHoles(lUnHoles);lItUnHoles.More(); lItUnHoles.Next())
242 aMapHoles.Remove(lItUnHoles.Value());
246 for(TopTools_MapIteratorOfMapOfShape aIterHoles(aMapHoles);aIterHoles.More(); aIterHoles.Next())
247 aMapShellHoles.UnBind(aIterHoles.Key());
250 //=======================================================================
251 //function : CreateSolids
253 //=======================================================================
255 static Standard_Boolean CreateSolids(const TopoDS_Shape aShape,TopTools_IndexedMapOfShape& aMapSolids)
257 TopTools_SequenceOfShape aSeqShells;
258 Standard_Boolean isDone = Standard_False;
260 for(TopExp_Explorer aExpShell(aShape,TopAbs_SHELL); aExpShell.More(); aExpShell.Next()) {
261 aSeqShells.Append(aExpShell.Current());
263 TopTools_DataMapOfShapeListOfShape aMapShellHoles;
264 TopTools_DataMapOfShapeInteger aMapStatus;
265 CollectSolids(aSeqShells,aMapShellHoles,aMapStatus);
266 TopTools_IndexedDataMapOfShapeShape ShellSolid;
267 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape aItShellHoles( aMapShellHoles);
268 //Defines correct orientation of shells
269 for(; aItShellHoles.More();aItShellHoles.Next()) {
270 TopoDS_Shell aShell = TopoDS::Shell(aItShellHoles.Key());
271 TopExp_Explorer aExpEdges(aShell,TopAbs_EDGE);
272 if(!BRep_Tool::IsClosed(aShell) || !aExpEdges.More()) {
273 ShellSolid.Add(aShell,aShell);
274 isDone = Standard_True;
278 TopAbs_State infinstatus = TopAbs_UNKNOWN;
280 B.MakeSolid (aSolid);
281 B.Add (aSolid,aShell);
282 if(aMapStatus.IsBound(aShell)) {
283 Standard_Integer st = aMapStatus.Find(aShell);
285 infinstatus = (aMapStatus.Find(aShell) == 1 ? TopAbs_IN : TopAbs_OUT);
292 BRepClass3d_SolidClassifier bsc3d (aSolid);
293 bsc3d.PerformInfinitePoint(Precision::Confusion());
294 infinstatus = bsc3d.State();
296 catch(Standard_Failure) {
298 cout << "Warning: ShapeFix_Solid::SolidFromShell: Exception: ";
299 Standard_Failure::Caught()->Print(cout); cout << endl;
301 ShellSolid.Add(aShell,aSolid);
305 if (infinstatus == TopAbs_IN) {
306 isDone = Standard_True;
308 TopoDS_Solid aTmpSolid;
309 B.MakeSolid (aTmpSolid);
310 B.Add (aTmpSolid,aShell);
314 const TopTools_ListOfShape& lHoles = aItShellHoles.Value();
315 for(TopTools_ListIteratorOfListOfShape lItHoles(lHoles); lItHoles.More();lItHoles.Next()) {
316 TopoDS_Shell aShellHole = TopoDS::Shell(lItHoles.Value());
317 if(aMapStatus.IsBound(aShellHole)) {
318 infinstatus = (aMapStatus.Find(aShellHole) == 1 ? TopAbs_IN : TopAbs_OUT);
323 B.Add (solid,aShellHole);
324 BRepClass3d_SolidClassifier bsc3dHol (solid);
325 bsc3dHol.PerformInfinitePoint(Precision::Confusion());
326 infinstatus = bsc3dHol.State();
328 if (infinstatus == TopAbs_OUT) {
329 aShellHole.Reverse();
330 isDone = Standard_True;
332 B.Add(aSolid,aShellHole);
334 ShellSolid.Add(aShell,aSolid);
336 //Creation of compsolid from shells containing shared faces.
337 TopTools_IndexedDataMapOfShapeListOfShape aMapFaceShells;
338 TopExp::MapShapesAndAncestors(aShape,TopAbs_FACE,TopAbs_SHELL,aMapFaceShells);
339 for(Standard_Integer i =1; i <= aMapFaceShells.Extent(); i++) {
340 const TopTools_ListOfShape& lshells = aMapFaceShells.FindFromIndex(i);
341 if(lshells.Extent() <2) continue;
342 TopoDS_CompSolid aCompSolid;
344 aB.MakeCompSolid(aCompSolid);
345 isDone = (aShape.ShapeType() != TopAbs_COMPSOLID || isDone);
346 for(TopTools_ListIteratorOfListOfShape lItSh(lshells);lItSh.More(); lItSh.Next()) {
347 if(ShellSolid.Contains(lItSh.Value())) {
348 for(TopExp_Explorer aExpSol(ShellSolid.FindFromKey(lItSh.Value()),TopAbs_SOLID);aExpSol.More(); aExpSol.Next())
349 aB.Add(aCompSolid,aExpSol.Current());
350 ShellSolid.ChangeFromKey(lItSh.Value()) = aCompSolid;
354 for(Standard_Integer kk =1 ; kk <= ShellSolid.Extent();kk++)
355 if(!aMapSolids.Contains(ShellSolid.FindFromIndex(kk)))
356 aMapSolids.Add(ShellSolid.FindFromIndex(kk));
357 isDone = (aMapSolids.Extent() >1 || isDone);
360 //=======================================================================
363 //=======================================================================
365 Standard_Boolean ShapeFix_Solid::Perform(const Handle(Message_ProgressIndicator)& theProgress)
368 Standard_Boolean status = Standard_False;
369 if ( Context().IsNull() )
370 SetContext ( new ShapeBuild_ReShape );
371 myFixShell->SetContext(Context());
373 Standard_Integer NbShells = 0;
374 TopoDS_Shape S = Context()->Apply ( myShape );
376 // Calculate number of underlying shells
377 Standard_Integer aNbShells = 0;
378 for ( TopExp_Explorer aExpSh(S, TopAbs_SHELL); aExpSh.More(); aExpSh.Next() )
381 // Start progress scope (no need to check if progress exists -- it is safe)
382 Message_ProgressSentry aPSentry(theProgress, "Fixing solid stage", 0, 2, 1);
384 if ( NeedFix(myFixShellMode) )
386 // Start progress scope (no need to check if progress exists -- it is safe)
387 Message_ProgressSentry aPSentry(theProgress, "Fixing shell", 0, aNbShells, 1);
389 // Fix shell by shell using ShapeFix_Shell tool
390 for ( TopExp_Explorer aExpSh(S, TopAbs_SHELL); aExpSh.More() && aPSentry.More(); aExpSh.Next(), aPSentry.Next() )
392 TopoDS_Shape sh = aExpSh.Current();
394 myFixShell->Init( TopoDS::Shell(sh) );
395 if ( myFixShell->Perform(theProgress) )
397 status = Standard_True;
398 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
400 NbShells+= myFixShell->NbShells();
403 // Halt algorithm in case of user's abort
404 if ( !aPSentry.More() )
405 return Standard_False;
409 NbShells = aNbShells;
412 // Switch to the second stage
416 TopoDS_Shape tmpShape = Context()->Apply(myShape);
417 TopExp_Explorer aExp(tmpShape,TopAbs_SHELL);
418 Standard_Boolean isClosed = Standard_False;
420 TopoDS_Shell aShtmp = TopoDS::Shell(aExp.Current());
421 ShapeAnalysis_FreeBounds sfb(aShtmp);
422 TopoDS_Compound aC1 = sfb.GetClosedWires();
423 TopoDS_Compound aC2 = sfb.GetOpenWires();
424 Standard_Integer numedge =0;
425 TopExp_Explorer aExp1(aC1,TopAbs_EDGE);
426 for( ; aExp1.More(); aExp1.Next())
428 for(aExp1.Init(aC2,TopAbs_EDGE) ; aExp1.More(); aExp1.Next())
430 isClosed = (!numedge);
431 aShtmp.Closed(isClosed);
434 if(isClosed || myCreateOpenSolidMode) {
435 TopoDS_Iterator itersh(tmpShape);
437 if(itersh.More() && itersh.Value().ShapeType() == TopAbs_SHELL)
438 aShell = TopoDS::Shell(itersh.Value());
439 if(!aShell.IsNull()) {
440 TopoDS_Solid aSol = SolidFromShell(aShell);
441 if(ShapeExtend::DecodeStatus(myStatus,ShapeExtend_DONE2)) {
442 SendWarning (Message_Msg ("FixAdvSolid.FixOrientation.MSG20"));// Orientaion of shell was corrected.
443 Context()->Replace(tmpShape,aSol);
447 mySolid = TopoDS::Solid(tmpShape);
450 TopoDS_Iterator aIt(tmpShape,Standard_False);
451 Context()->Replace(tmpShape,aIt.Value());
452 SendFail (Message_Msg ("FixAdvSolid.FixShell.MSG10")); // Solid can not be created from open shell.
456 TopoDS_Shape aResShape = Context()->Apply(myShape);
457 TopTools_SequenceOfShape aSeqShells;
458 TopTools_IndexedMapOfShape aMapSolids;
459 if(CreateSolids(aResShape,aMapSolids)) {
460 SendWarning (Message_Msg ("FixAdvSolid.FixOrientation.MSG20"));// Orientaion of shell was corrected..
461 if(aMapSolids.Extent() ==1) {
462 TopoDS_Shape aResSol = aMapSolids.FindKey(1);
463 if(aResShape.ShapeType() == TopAbs_SHELL && myCreateOpenSolidMode) {
467 B.Add (solid,aResSol);
472 if(aResSol.ShapeType() == TopAbs_SHELL)
473 SendFail (Message_Msg ("FixAdvSolid.FixShell.MSG10")); // Solid can not be created from open shell.
475 Context()->Replace(aResShape,mySolid);
477 else if(aMapSolids.Extent() >1) {
478 SendWarning (Message_Msg ("FixAdvSolid.FixOrientation.MSG30"));// Bad connected solid a few solids were created.
480 TopoDS_Compound aComp;
481 aB.MakeCompound(aComp);
482 Message_ProgressSentry aPSentry(theProgress, "Creating solid",
483 0, aMapSolids.Extent(), 1);
484 for(Standard_Integer i =1; (i <= aMapSolids.Extent()) && (aPSentry.More());
485 i++, aPSentry.Next())
487 TopoDS_Shape aResSh =aMapSolids.FindKey(i);
488 if(aResShape.ShapeType() == TopAbs_SHELL && myCreateOpenSolidMode) {
489 aResSh.Closed(Standard_False);
493 B.Add (solid,aResSh);
496 else if (aResShape.ShapeType() == TopAbs_SHELL)
497 SendFail(Message_Msg ("FixAdvSolid.FixShell.MSG10")); // Solid can not be created from open shell.
498 aB.Add(aComp,aResSh);
501 if ( !aPSentry.More() )
502 return Standard_False; // aborted execution
503 Context()->Replace(aResShape,aComp);
507 myShape = Context()->Apply(myShape);
510 //=======================================================================
513 //=======================================================================
515 TopoDS_Shape ShapeFix_Solid::Shape()
519 //=======================================================================
520 //function : SolidFromShell
522 //=======================================================================
524 TopoDS_Solid ShapeFix_Solid::SolidFromShell (const TopoDS_Shell& shell)
526 TopoDS_Shell sh = shell;
527 if (!sh.Free ()) sh.Free(Standard_True);
533 // Pas encore fini : il faut une bonne orientation
536 BRepClass3d_SolidClassifier bsc3d (solid);
537 Standard_Real t = Precision::Confusion(); // tolerance moyenne
538 bsc3d.PerformInfinitePoint(t);
540 if (bsc3d.State() == TopAbs_IN) {
541 // Ensuite, inverser C-A-D REPRENDRE LES SHELLS
542 // (l inversion du solide n est pas bien prise en compte)
544 if (!sh.Free ()) sh.Free(Standard_True);
546 B.MakeSolid (soli2); // on recommence
550 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE2 );
553 catch(Standard_Failure) {
555 cout << "Warning: ShapeFix_Solid::SolidFromShell: Exception: ";
556 Standard_Failure::Caught()->Print(cout); cout << endl;
563 //=======================================================================
566 //=======================================================================
568 Standard_Boolean ShapeFix_Solid::Status(const ShapeExtend_Status /*status*/) const
573 //=======================================================================
576 //=======================================================================
578 TopoDS_Shape ShapeFix_Solid::Solid() const
583 //=======================================================================
584 //function : SetMsgRegistrator
586 //=======================================================================
588 void ShapeFix_Solid::SetMsgRegistrator(const Handle(ShapeExtend_BasicMsgRegistrator)& msgreg)
590 ShapeFix_Root::SetMsgRegistrator ( msgreg );
591 myFixShell->SetMsgRegistrator ( msgreg );
594 //=======================================================================
595 //function : SetPrecision
597 //=======================================================================
599 void ShapeFix_Solid::SetPrecision (const Standard_Real preci)
601 ShapeFix_Root::SetPrecision ( preci );
602 myFixShell->SetPrecision ( preci );
605 //=======================================================================
606 //function : SetMinTolerance
608 //=======================================================================
610 void ShapeFix_Solid::SetMinTolerance (const Standard_Real mintol)
612 ShapeFix_Root::SetMinTolerance ( mintol );
613 myFixShell->SetMinTolerance ( mintol );
616 //=======================================================================
617 //function : SetMaxTolerance
619 //=======================================================================
621 void ShapeFix_Solid::SetMaxTolerance (const Standard_Real maxtol)
623 ShapeFix_Root::SetMaxTolerance ( maxtol );
624 myFixShell->SetMaxTolerance ( maxtol );