0e89802594fb295d783a93645ae6ecd52ae610f8
[occt.git] / src / BRepAlgo / BRepAlgo_1.cxx
1 // Created on: 1999-10-21
2 // Created by: Atelier CAS2000
3 // Copyright (c) 1999-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
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.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #include <BRepAlgo.ixx>
18
19 #include <BRepCheck.hxx>
20 #include <BRepCheck_Analyzer.hxx>
21 #include <BRepCheck_ListIteratorOfListOfStatus.hxx>
22 #include <BRepCheck_ListOfStatus.hxx>
23 #include <BRepCheck_Result.hxx>
24 #include <BRepCheck_Shell.hxx>
25 #include <BRepLib.hxx>
26 #include <BRep_Builder.hxx>
27 #include <BRep_Tool.hxx>
28 #include <BRepTools.hxx>
29 #include <Precision.hxx>
30 #include <TopExp_Explorer.hxx>
31 #include <TopTools_ListIteratorOfListOfShape.hxx>
32 #include <TopTools_MapOfShape.hxx>
33 #include <TopoDS.hxx>
34 #include <TopoDS_Face.hxx>
35 #include <TopoDS_Compound.hxx>
36 #include <gp.hxx>
37
38 //=======================================================================
39 //function : IsValid
40 //purpose  : 
41 //=======================================================================
42 Standard_Boolean BRepAlgo::IsValid(const TopoDS_Shape& S)
43 {
44   BRepCheck_Analyzer ana(S);
45   return ana.IsValid(); 
46 }
47
48 //=======================================================================
49 //function : IsValid
50 //purpose  : 
51 //=======================================================================
52   Standard_Boolean BRepAlgo::IsValid(const TopTools_ListOfShape& theArgs,
53                                      const TopoDS_Shape& theResult,
54                                      const Standard_Boolean closedSolid,
55                                      const Standard_Boolean GeomCtrl)
56 {
57   if (theResult.IsNull()) return Standard_True;
58   Standard_Boolean validate= Standard_False;
59
60   TopTools_MapOfShape allFaces;
61   TopExp_Explorer tEx;
62   TopTools_ListIteratorOfListOfShape itLOS;
63   for (itLOS.Initialize(theArgs);
64        itLOS.More(); itLOS.Next()) {
65     if (itLOS.Value().IsSame(theResult)) {
66       validate = Standard_True;
67       break;
68     }
69     for (tEx.Init(itLOS.Value(), TopAbs_FACE); tEx.More(); tEx.Next()) {
70       allFaces.Add(tEx.Current());
71     }
72   }
73
74   TopoDS_Compound toCheck;
75
76   if (allFaces.IsEmpty()) {
77     if (validate) return Standard_True;
78     BRepCheck_Analyzer ana(theResult, GeomCtrl);
79     if (!ana.IsValid()) return Standard_False;
80   }
81   else if (!validate) {
82     BRep_Builder bB;
83     TopoDS_Face curf;
84     for (tEx.Init(theResult, TopAbs_FACE); tEx.More(); tEx.Next()) {
85       curf=TopoDS::Face(tEx.Current());
86       if (!allFaces.Contains(curf)) {
87         if (toCheck.IsNull()) bB.MakeCompound(toCheck);
88         BRepTools::Update(curf);
89         bB.Add(toCheck, curf);
90       }
91     }
92     if (toCheck.IsNull()) {
93       validate = Standard_True;
94     }
95     else {
96       BRepCheck_Analyzer ana(toCheck, Standard_True);
97       if (!ana.IsValid()) {
98
99 // Check if the problem is not just BRepCheck_InvalidSameParameterFlag
100         BRepCheck_ListIteratorOfListOfStatus itl;
101         BRepCheck_Status sta;
102         for (tEx.Init(toCheck, TopAbs_FACE); tEx.More(); tEx.Next()) {
103           if  (!ana.Result(tEx.Current()).IsNull()) {
104             for (itl.Initialize(ana.Result(tEx.Current())->Status()); itl.More(); itl.Next()) {
105               sta=itl.Value();
106 // If a face is incorrect
107               if (sta != BRepCheck_NoError) {
108                 BRepCheck_ListIteratorOfListOfStatus ilt;
109                 TopExp_Explorer exp;
110                 for (exp.Init(tEx.Current(), TopAbs_EDGE); exp.More(); exp.Next()) {
111                   const Handle(BRepCheck_Result)& res = ana.Result(exp.Current());
112                   for (res->InitContextIterator(); res->MoreShapeInContext(); res->NextShapeInContext()) {
113                     if (res->ContextualShape().IsSame(tEx.Current())) {
114                       for (ilt.Initialize(res->StatusOnShape()); ilt.More(); ilt.Next()) {
115                         sta=ilt.Value();
116 // If an edge is BRepCheck_InvalidSameParameterFlag or BRepCheck_InvalidSameRangeFlag, it is forced
117                         if (sta == BRepCheck_InvalidSameParameterFlag ||
118                             sta == BRepCheck_InvalidSameRangeFlag) {
119                           bB.SameRange(TopoDS::Edge(exp.Current()), Standard_False);
120                           bB.SameParameter(TopoDS::Edge(exp.Current()), Standard_False);
121                           BRepLib::SameParameter(TopoDS::Edge(exp.Current()), 
122                                                  BRep_Tool::Tolerance(TopoDS::Edge(exp.Current())));
123                           break;
124                         }
125                       }
126                     }
127                   }
128                 }
129                 break;
130               }
131             }
132           }
133         }
134 // Remake control (there can be a problem of another type orb the one that cannot be corrected
135         ana.Init(toCheck, Standard_True);
136         if (!ana.IsValid()) return Standard_False;
137       }
138     }
139   }
140
141   Handle(BRepCheck_Shell) HR;
142   for (tEx.Init(theResult, TopAbs_SHELL); tEx.More(); tEx.Next()) {
143     if (HR.IsNull()) HR = new BRepCheck_Shell(TopoDS::Shell(tEx.Current()));
144     else                             HR->Init(tEx.Current());
145     if (HR->Status().First() != BRepCheck_NoError) return Standard_False;
146     if (HR->Orientation(Standard_False) != BRepCheck_NoError) return Standard_False;
147     if (closedSolid) {
148       if (HR->Closed() != BRepCheck_NoError) return Standard_False;
149     }
150   }
151
152   return Standard_True;
153 }
154
155
156 //=======================================================================
157 //function : IsTopologicallyValid
158 //purpose  : 
159 //=======================================================================
160   Standard_Boolean BRepAlgo::IsTopologicallyValid(const TopoDS_Shape& S)
161 {
162 //
163
164 // if (getenv("DONT_SWITCH_IS_VALID") != NULL) {
165 //   return Standard_True ; 
166 // }
167 // else {
168    BRepCheck_Analyzer ana(S,Standard_False);
169    return ana.IsValid(); 
170  
171 // }
172 }
173
174
175