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_IndexedDataMapOfShapeListOfShape& anIndexedMapShellHoles,
144 TopTools_DataMapOfShapeInteger& theMapStatus)
146 TopTools_MapOfShape aMapHoles;
147 TopTools_DataMapOfShapeListOfShape aMapShellHoles;
148 for ( Standard_Integer i1 = 1; i1 <= aSeqShells.Length(); i1++ ) {
149 TopoDS_Shell aShell1 = TopoDS::Shell(aSeqShells.Value(i1));
150 TopTools_ListOfShape lshells;
151 aMapShellHoles.Bind(aShell1,lshells);
153 //Finds roots shells and hole shells.
154 for ( Standard_Integer i = 1; i <= aSeqShells.Length(); i++ ) {
155 TopoDS_Shell aShell1 = TopoDS::Shell(aSeqShells.Value(i));
156 TopExp_Explorer aExpEdges(aShell1,TopAbs_EDGE);
157 if(!BRep_Tool::IsClosed(aShell1) || !aExpEdges.More()) continue;
161 B.Add (solid,aShell1);
164 TopAbs_State infinstatus = TopAbs_UNKNOWN;
165 BRepClass3d_SolidClassifier bsc3d (solid);
166 Standard_Integer st = 0;
167 if(!theMapStatus.IsBound(aShell1)) {
169 bsc3d.PerformInfinitePoint(Precision::Confusion());
170 infinstatus = bsc3d.State();
172 if(infinstatus != TopAbs_UNKNOWN && infinstatus !=TopAbs_ON)
173 st = (infinstatus == TopAbs_IN ? 1 :2);
174 theMapStatus.Bind(aShell1,st);
179 st = theMapStatus.Find(aShell1);
181 infinstatus = (theMapStatus.Find(aShell1) == 1 ? TopAbs_IN : TopAbs_OUT);
184 for ( Standard_Integer j = 1; j <= aSeqShells.Length(); j++ ) {
186 TopoDS_Shape aShell2 = aSeqShells.Value(j);
187 if(!BRep_Tool::IsClosed(aShell2)) continue;
188 if(aMapHoles.Contains(aShell2)) continue;
189 if(aMapShellHoles.IsBound(aShell2)) {
190 Standard_Boolean isAnalysis = Standard_False;
191 const TopTools_ListOfShape& ls = aMapShellHoles.Find(aShell2);
192 for(TopTools_ListIteratorOfListOfShape li(ls); li.More() && !isAnalysis; li.Next())
193 isAnalysis = li.Value().IsSame(aShell1);
194 if(isAnalysis) continue;
196 TopAbs_State pointstatus = TopAbs_UNKNOWN;
197 Standard_Integer numon =0;
198 TopTools_IndexedMapOfShape amapVert;
199 for(TopExp_Explorer aExpVert(aShell2,TopAbs_VERTEX); aExpVert.More() && amapVert.Extent() < 10; aExpVert.Next())
200 amapVert.Add(aExpVert.Current());
201 for(Standard_Integer k = 1; k <= amapVert.Extent() &&
202 (pointstatus ==TopAbs_UNKNOWN || (pointstatus ==TopAbs_ON && numon < 3)); k++) {
203 TopoDS_Vertex aV = TopoDS::Vertex(amapVert.FindKey(k));
204 gp_Pnt aPf = BRep_Tool::Pnt(aV);
205 bsc3d.Perform(aPf,Precision::Confusion());
206 pointstatus =bsc3d.State();
207 if(pointstatus ==TopAbs_ON) numon++;
210 if(numon == 3 && pointstatus ==TopAbs_ON) {
211 #ifdef OCCT_DEBUG_GET_MIDDLE_POINT
213 GetMiddlePoint(aShell2,pmid);
214 bsc3d.Perform(pmid,Precision::Confusion());
216 pointstatus = /*(bsc3d.State() == TopAbs_IN ? TopAbs_IN :*/TopAbs_OUT;
218 if(pointstatus != infinstatus) {
219 aMapShellHoles.ChangeFind(aShell1).Append(aShell2);
220 if( aMapHoles.Contains(aShell2))
221 aMapHoles.Remove(aShell2);
222 else aMapHoles.Add(aShell2);
226 catch(Standard_Failure) {
228 cout << "Warning: ShapeFix_Solid::SolidFromShell: Exception: ";
229 Standard_Failure::Caught()->Print(cout); cout << endl;
234 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape aItShellHoles( aMapShellHoles);
235 for(; aItShellHoles.More();aItShellHoles.Next()) {
236 if(aMapHoles.Contains(aItShellHoles.Key())) continue;
237 const TopTools_ListOfShape& lHoles =aItShellHoles.Value();
238 if(lHoles.IsEmpty()) continue;
239 for(TopTools_ListIteratorOfListOfShape lItHoles(lHoles);lItHoles.More(); lItHoles.Next()) {
240 if(aMapHoles.Contains(lItHoles.Value())) {
241 const TopTools_ListOfShape& lUnHoles = aMapShellHoles.Find(lItHoles.Value());
242 for(TopTools_ListIteratorOfListOfShape lItUnHoles(lUnHoles);lItUnHoles.More(); lItUnHoles.Next())
243 aMapHoles.Remove(lItUnHoles.Value());
247 for(TopTools_MapIteratorOfMapOfShape aIterHoles(aMapHoles);aIterHoles.More(); aIterHoles.Next())
248 aMapShellHoles.UnBind (aIterHoles.Key());
250 for (Standard_Integer i = 1; i <= aSeqShells.Length(); ++i) {
251 const TopoDS_Shape& aShell1 = aSeqShells.Value (i);
252 if (aMapShellHoles.IsBound (aShell1)) {
253 const TopTools_ListOfShape& ls = aMapShellHoles.Find (aShell1);
254 anIndexedMapShellHoles.Add (aShell1, ls);
258 //=======================================================================
259 //function : CreateSolids
261 //=======================================================================
263 static Standard_Boolean CreateSolids(const TopoDS_Shape aShape,TopTools_IndexedMapOfShape& aMapSolids)
265 TopTools_SequenceOfShape aSeqShells;
266 Standard_Boolean isDone = Standard_False;
268 for(TopExp_Explorer aExpShell(aShape,TopAbs_SHELL); aExpShell.More(); aExpShell.Next()) {
269 aSeqShells.Append(aExpShell.Current());
271 TopTools_IndexedDataMapOfShapeListOfShape aMapShellHoles;
272 TopTools_DataMapOfShapeInteger aMapStatus;
273 CollectSolids(aSeqShells,aMapShellHoles,aMapStatus);
274 TopTools_IndexedDataMapOfShapeShape ShellSolid;
275 //Defines correct orientation of shells
276 for (Standard_Integer i = 1; i <= aMapShellHoles.Extent(); ++i) {
277 TopoDS_Shell aShell = TopoDS::Shell(aMapShellHoles.FindKey(i));
278 TopExp_Explorer aExpEdges(aShell,TopAbs_EDGE);
279 if(!BRep_Tool::IsClosed(aShell) || !aExpEdges.More()) {
280 ShellSolid.Add(aShell,aShell);
281 isDone = Standard_True;
285 TopAbs_State infinstatus = TopAbs_UNKNOWN;
287 B.MakeSolid (aSolid);
288 B.Add (aSolid,aShell);
289 if(aMapStatus.IsBound(aShell)) {
290 Standard_Integer st = aMapStatus.Find(aShell);
292 infinstatus = (aMapStatus.Find(aShell) == 1 ? TopAbs_IN : TopAbs_OUT);
299 BRepClass3d_SolidClassifier bsc3d (aSolid);
300 bsc3d.PerformInfinitePoint(Precision::Confusion());
301 infinstatus = bsc3d.State();
303 catch(Standard_Failure) {
305 cout << "Warning: ShapeFix_Solid::SolidFromShell: Exception: ";
306 Standard_Failure::Caught()->Print(cout); cout << endl;
308 ShellSolid.Add(aShell,aSolid);
312 if (infinstatus == TopAbs_IN) {
313 isDone = Standard_True;
315 TopoDS_Solid aTmpSolid;
316 B.MakeSolid (aTmpSolid);
317 B.Add (aTmpSolid,aShell);
321 const TopTools_ListOfShape& lHoles = aMapShellHoles (i);
322 for(TopTools_ListIteratorOfListOfShape lItHoles(lHoles); lItHoles.More();lItHoles.Next()) {
323 TopoDS_Shell aShellHole = TopoDS::Shell(lItHoles.Value());
324 if(aMapStatus.IsBound(aShellHole)) {
325 infinstatus = (aMapStatus.Find(aShellHole) == 1 ? TopAbs_IN : TopAbs_OUT);
330 B.Add (solid,aShellHole);
331 BRepClass3d_SolidClassifier bsc3dHol (solid);
332 bsc3dHol.PerformInfinitePoint(Precision::Confusion());
333 infinstatus = bsc3dHol.State();
335 if (infinstatus == TopAbs_OUT) {
336 aShellHole.Reverse();
337 isDone = Standard_True;
339 B.Add(aSolid,aShellHole);
341 ShellSolid.Add(aShell,aSolid);
343 //Creation of compsolid from shells containing shared faces.
344 TopTools_IndexedDataMapOfShapeListOfShape aMapFaceShells;
345 TopExp::MapShapesAndAncestors(aShape,TopAbs_FACE,TopAbs_SHELL,aMapFaceShells);
346 for(Standard_Integer i =1; i <= aMapFaceShells.Extent(); i++) {
347 const TopTools_ListOfShape& lshells = aMapFaceShells.FindFromIndex(i);
348 if(lshells.Extent() <2) continue;
349 TopoDS_CompSolid aCompSolid;
351 aB.MakeCompSolid(aCompSolid);
352 isDone = (aShape.ShapeType() != TopAbs_COMPSOLID || isDone);
353 for(TopTools_ListIteratorOfListOfShape lItSh(lshells);lItSh.More(); lItSh.Next()) {
354 if(ShellSolid.Contains(lItSh.Value())) {
355 for(TopExp_Explorer aExpSol(ShellSolid.FindFromKey(lItSh.Value()),TopAbs_SOLID);aExpSol.More(); aExpSol.Next())
356 aB.Add(aCompSolid,aExpSol.Current());
357 ShellSolid.ChangeFromKey(lItSh.Value()) = aCompSolid;
361 for(Standard_Integer kk =1 ; kk <= ShellSolid.Extent();kk++)
362 if(!aMapSolids.Contains(ShellSolid.FindFromIndex(kk)))
363 aMapSolids.Add(ShellSolid.FindFromIndex(kk));
364 isDone = (aMapSolids.Extent() >1 || isDone);
367 //=======================================================================
370 //=======================================================================
372 Standard_Boolean ShapeFix_Solid::Perform(const Handle(Message_ProgressIndicator)& theProgress)
375 Standard_Boolean status = Standard_False;
376 if ( Context().IsNull() )
377 SetContext ( new ShapeBuild_ReShape );
378 myFixShell->SetContext(Context());
380 Standard_Integer NbShells = 0;
381 TopoDS_Shape S = Context()->Apply ( myShape );
383 // Calculate number of underlying shells
384 Standard_Integer aNbShells = 0;
385 for ( TopExp_Explorer aExpSh(S, TopAbs_SHELL); aExpSh.More(); aExpSh.Next() )
388 // Start progress scope (no need to check if progress exists -- it is safe)
389 Message_ProgressSentry aPSentry(theProgress, "Fixing solid stage", 0, 2, 1);
391 if ( NeedFix(myFixShellMode) )
393 // Start progress scope (no need to check if progress exists -- it is safe)
394 Message_ProgressSentry aPSentry(theProgress, "Fixing shell", 0, aNbShells, 1);
396 // Fix shell by shell using ShapeFix_Shell tool
397 for ( TopExp_Explorer aExpSh(S, TopAbs_SHELL); aExpSh.More() && aPSentry.More(); aExpSh.Next(), aPSentry.Next() )
399 TopoDS_Shape sh = aExpSh.Current();
401 myFixShell->Init( TopoDS::Shell(sh) );
402 if ( myFixShell->Perform(theProgress) )
404 status = Standard_True;
405 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
407 NbShells+= myFixShell->NbShells();
410 // Halt algorithm in case of user's abort
411 if ( !aPSentry.More() )
412 return Standard_False;
416 NbShells = aNbShells;
419 // Switch to the second stage
423 TopoDS_Shape tmpShape = Context()->Apply(myShape);
424 TopExp_Explorer aExp(tmpShape,TopAbs_SHELL);
425 Standard_Boolean isClosed = Standard_False;
427 TopoDS_Shell aShtmp = TopoDS::Shell(aExp.Current());
428 ShapeAnalysis_FreeBounds sfb(aShtmp);
429 TopoDS_Compound aC1 = sfb.GetClosedWires();
430 TopoDS_Compound aC2 = sfb.GetOpenWires();
431 Standard_Integer numedge =0;
432 TopExp_Explorer aExp1(aC1,TopAbs_EDGE);
433 for( ; aExp1.More(); aExp1.Next())
435 for(aExp1.Init(aC2,TopAbs_EDGE) ; aExp1.More(); aExp1.Next())
437 isClosed = (!numedge);
438 aShtmp.Closed(isClosed);
441 if(isClosed || myCreateOpenSolidMode) {
442 TopoDS_Iterator itersh(tmpShape);
444 if(itersh.More() && itersh.Value().ShapeType() == TopAbs_SHELL)
445 aShell = TopoDS::Shell(itersh.Value());
446 if(!aShell.IsNull()) {
447 TopoDS_Solid aSol = SolidFromShell(aShell);
448 if(ShapeExtend::DecodeStatus(myStatus,ShapeExtend_DONE2)) {
449 SendWarning (Message_Msg ("FixAdvSolid.FixOrientation.MSG20"));// Orientaion of shell was corrected.
450 Context()->Replace(tmpShape,aSol);
454 mySolid = TopoDS::Solid(tmpShape);
457 TopoDS_Iterator aIt(tmpShape,Standard_False);
458 Context()->Replace(tmpShape,aIt.Value());
459 SendFail (Message_Msg ("FixAdvSolid.FixShell.MSG10")); // Solid can not be created from open shell.
463 TopoDS_Shape aResShape = Context()->Apply(myShape);
464 TopTools_SequenceOfShape aSeqShells;
465 TopTools_IndexedMapOfShape aMapSolids;
466 if(CreateSolids(aResShape,aMapSolids)) {
467 SendWarning (Message_Msg ("FixAdvSolid.FixOrientation.MSG20"));// Orientaion of shell was corrected..
468 if(aMapSolids.Extent() ==1) {
469 TopoDS_Shape aResSol = aMapSolids.FindKey(1);
470 if(aResShape.ShapeType() == TopAbs_SHELL && myCreateOpenSolidMode) {
474 B.Add (solid,aResSol);
479 if(aResSol.ShapeType() == TopAbs_SHELL)
480 SendFail (Message_Msg ("FixAdvSolid.FixShell.MSG10")); // Solid can not be created from open shell.
482 Context()->Replace(aResShape,mySolid);
484 else if(aMapSolids.Extent() >1) {
485 SendWarning (Message_Msg ("FixAdvSolid.FixOrientation.MSG30"));// Bad connected solid a few solids were created.
487 TopoDS_Compound aComp;
488 aB.MakeCompound(aComp);
489 Message_ProgressSentry aPSentry(theProgress, "Creating solid",
490 0, aMapSolids.Extent(), 1);
491 for(Standard_Integer i =1; (i <= aMapSolids.Extent()) && (aPSentry.More());
492 i++, aPSentry.Next())
494 TopoDS_Shape aResSh =aMapSolids.FindKey(i);
495 if(aResShape.ShapeType() == TopAbs_SHELL && myCreateOpenSolidMode) {
496 aResSh.Closed(Standard_False);
500 B.Add (solid,aResSh);
503 else if (aResShape.ShapeType() == TopAbs_SHELL)
504 SendFail(Message_Msg ("FixAdvSolid.FixShell.MSG10")); // Solid can not be created from open shell.
505 aB.Add(aComp,aResSh);
508 if ( !aPSentry.More() )
509 return Standard_False; // aborted execution
510 Context()->Replace(aResShape,aComp);
514 myShape = Context()->Apply(myShape);
517 //=======================================================================
520 //=======================================================================
522 TopoDS_Shape ShapeFix_Solid::Shape()
526 //=======================================================================
527 //function : SolidFromShell
529 //=======================================================================
531 TopoDS_Solid ShapeFix_Solid::SolidFromShell (const TopoDS_Shell& shell)
533 TopoDS_Shell sh = shell;
534 if (!sh.Free ()) sh.Free(Standard_True);
540 // Pas encore fini : il faut une bonne orientation
543 BRepClass3d_SolidClassifier bsc3d (solid);
544 Standard_Real t = Precision::Confusion(); // tolerance moyenne
545 bsc3d.PerformInfinitePoint(t);
547 if (bsc3d.State() == TopAbs_IN) {
548 // Ensuite, inverser C-A-D REPRENDRE LES SHELLS
549 // (l inversion du solide n est pas bien prise en compte)
551 if (!sh.Free ()) sh.Free(Standard_True);
553 B.MakeSolid (soli2); // on recommence
557 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE2 );
560 catch(Standard_Failure) {
562 cout << "Warning: ShapeFix_Solid::SolidFromShell: Exception: ";
563 Standard_Failure::Caught()->Print(cout); cout << endl;
570 //=======================================================================
573 //=======================================================================
575 Standard_Boolean ShapeFix_Solid::Status(const ShapeExtend_Status /*status*/) const
580 //=======================================================================
583 //=======================================================================
585 TopoDS_Shape ShapeFix_Solid::Solid() const
590 //=======================================================================
591 //function : SetMsgRegistrator
593 //=======================================================================
595 void ShapeFix_Solid::SetMsgRegistrator(const Handle(ShapeExtend_BasicMsgRegistrator)& msgreg)
597 ShapeFix_Root::SetMsgRegistrator ( msgreg );
598 myFixShell->SetMsgRegistrator ( msgreg );
601 //=======================================================================
602 //function : SetPrecision
604 //=======================================================================
606 void ShapeFix_Solid::SetPrecision (const Standard_Real preci)
608 ShapeFix_Root::SetPrecision ( preci );
609 myFixShell->SetPrecision ( preci );
612 //=======================================================================
613 //function : SetMinTolerance
615 //=======================================================================
617 void ShapeFix_Solid::SetMinTolerance (const Standard_Real mintol)
619 ShapeFix_Root::SetMinTolerance ( mintol );
620 myFixShell->SetMinTolerance ( mintol );
623 //=======================================================================
624 //function : SetMaxTolerance
626 //=======================================================================
628 void ShapeFix_Solid::SetMaxTolerance (const Standard_Real maxtol)
630 ShapeFix_Root::SetMaxTolerance ( maxtol );
631 myFixShell->SetMaxTolerance ( maxtol );