Integration of OCCT 6.5.0 from SVN
[occt.git] / samples / mfc / occtdemo / Validate / Validate_Presentation.cpp
1 // Validate_Presentation.cpp: implementation of the Validate_Presentation class.
2 // Checking validity of shapes
3 //////////////////////////////////////////////////////////////////////
4
5 #include "stdafx.h"
6 #include "Validate_Presentation.h"
7
8 #include <BRep_Builder.hxx>
9 #include <BRepTools.hxx>
10 #include <BRepCheck_Analyzer.hxx>
11 #include <BRepCheck_ListOfStatus.hxx>
12 #include <BRepCheck_Result.hxx>
13 #include <BRepCheck_ListIteratorOfListOfStatus.hxx>
14 #include <TopAbs_ShapeEnum.hxx>
15 #include <TopTools_SequenceOfShape.hxx>
16 #include <TopTools_MapOfShape.hxx>
17 #include <TopExp_Explorer.hxx>
18
19
20 // Initialization of global variable with an instance of this class
21 OCCDemo_Presentation* OCCDemo_Presentation::Current = new Validate_Presentation;
22
23 // Initialization of array of samples
24 Standard_CString Validate_Presentation::myFileNames[] =
25 {
26   "wedge_ok.brep",
27   "wedge_bad.brep",
28   "shape2_bad.brep",
29   0
30 };
31
32 #ifdef WNT
33  #define EOL "\r\n"
34 #else
35  #define EOL "\n"
36 #endif
37
38 //////////////////////////////////////////////////////////////////////
39 // Construction/Destruction
40 //////////////////////////////////////////////////////////////////////
41
42 Validate_Presentation::Validate_Presentation()
43 {
44   FitMode=true;
45   for (myNbSamples = 0; myFileNames[myNbSamples]; myNbSamples++)
46     ;
47   setName ("Checking validity of shapes");
48 }
49
50 //////////////////////////////////////////////////////////////////////
51 // Sample execution
52 //////////////////////////////////////////////////////////////////////
53
54 void Validate_Presentation::DoSample()
55 {
56   getAISContext()->EraseAll();
57   if (myIndex >=0 && myIndex < myNbSamples)
58     sample (myFileNames[myIndex]);
59 }
60
61 //////////////////////////////////////////////////////////////////////
62 // Sample functions
63 //////////////////////////////////////////////////////////////////////
64
65 void Validate_Presentation::checkShape(const TopoDS_Shape& aShape)
66 {
67   setResultTitle ("Validate shape");
68   TCollection_AsciiString aText;
69
70   aText = aText +
71     "  // Input shape" EOL
72     "  TopoDS_Shape aShape;" EOL
73     "  // Initializing aShape" EOL
74     "  // ...." EOL EOL
75
76     "  // Checking" EOL
77     "  BRepCheck_Analyzer aChecker(aShape);" EOL
78     "  if (!aChecker.IsValid())" EOL
79     "  {" EOL
80     "    // Initialize sequence of subshapes" EOL
81     "    TopTools_SequenceOfShape aShapes;" EOL
82     "    TopTools_MapOfShape aMap;" EOL
83     "    TopAbs_ShapeEnum aTypes[] = {TopAbs_VERTEX, TopAbs_EDGE, " EOL
84     "      TopAbs_WIRE, TopAbs_FACE, TopAbs_SHELL};" EOL
85     "    TopExp_Explorer ex;" EOL
86     "    for (int i=0; i < 5; i++)" EOL
87     "      for (ex.Init(aShape, aTypes[i]); ex.More(); ex.Next())" EOL
88     "        if (!aMap.Contains(ex.Current()))" EOL
89     "        {" EOL
90     "          aShapes.Append(ex.Current());" EOL
91     "          aMap.Add(ex.Current());" EOL
92     "        }" EOL EOL
93
94     "    for (i=1; i <= aShapes.Length(); i++)" EOL
95     "    {" EOL
96     "      const TopoDS_Shape& aSubShape = aShapes(i);" EOL
97     "      Handle(BRepCheck_Result) aResult = aChecker.Result(aSubShape);" EOL
98     "      if (!aResult.IsNull())" EOL
99     "      {" EOL
100     "        BRepCheck_ListOfStatus aLstStatus;" EOL
101     "        // get statuses for a subshape" EOL
102     "        aLstStatus = aResult->Status();" EOL
103     "        // get statuses for a subshape in context" EOL
104     "        aResult->InitContextIterator();" EOL
105     "        while (aResult->MoreShapeInContext())" EOL
106     "        {" EOL
107     "          BRepCheck_ListOfStatus aLst1;" EOL
108     "          aLst1 = aResult->StatusOnShape();" EOL
109     "          aLstStatus.Append(aLst1);" EOL
110     "          aResult->NextShapeInContext();" EOL
111     "        }" EOL
112     "        BRepCheck_ListIteratorOfListOfStatus itl(aLstStatus);" EOL
113     "        for (; itl.More(); itl.Next())" EOL
114     "        {" EOL
115     "          BRepCheck_Status aStatus = itl.Value();" EOL
116     "          if (aStatus == BRepCheck_NoError)" EOL
117     "            continue;" EOL EOL
118
119     "          Standard_CString aError;" EOL
120     "          switch (aStatus)" EOL
121     "          {" EOL
122     "            // for vertices" EOL
123     "          case BRepCheck_InvalidPointOnCurve: aError = \"InvalidPointOnCurve\"; break;" EOL
124     "          case BRepCheck_InvalidPointOnCurveOnSurface: aError = \"InvalidPointOnCurveOnSurface\"; break;" EOL
125     "          case BRepCheck_InvalidPointOnSurface: aError = \"InvalidPointOnSurface\"; break;" EOL
126     "            // for edges" EOL
127     "          case BRepCheck_No3DCurve: aError = \"No3DCurve\"; break;" EOL
128     "          case BRepCheck_Multiple3DCurve: aError = \"Multiple3DCurve\"; break;" EOL
129     "          case BRepCheck_Invalid3DCurve: aError = \"Invalid3DCurve\"; break;" EOL
130     "          case BRepCheck_NoCurveOnSurface: aError = \"NoCurveOnSurface\"; break;" EOL
131     "          case BRepCheck_InvalidCurveOnSurface: aError = \"InvalidCurveOnSurface\"; break;" EOL
132     "          case BRepCheck_InvalidCurveOnClosedSurface: aError = \"InvalidCurveOnClosedSurface\"; break;" EOL
133     "          case BRepCheck_InvalidSameRangeFlag: aError = \"InvalidSameRangeFlag\"; break;" EOL
134     "          case BRepCheck_InvalidSameParameterFlag: aError = \"InvalidSameParameterFlag\"; break;" EOL
135     "          case BRepCheck_InvalidDegeneratedFlag: aError = \"InvalidDegeneratedFlag\"; break;" EOL
136     "          case BRepCheck_FreeEdge: aError = \"FreeEdge\"; break;" EOL
137     "          case BRepCheck_InvalidMultiConnexity: aError = \"InvalidMultiConnexity\"; break;" EOL
138     "          case BRepCheck_InvalidRange: aError = \"InvalidRange\"; break;" EOL
139     "            // for wires" EOL
140     "          case BRepCheck_EmptyWire: aError = \"EmptyWire\"; break;" EOL
141     "          case BRepCheck_RedundantEdge: aError = \"RedundantEdge\"; break;" EOL
142     "          case BRepCheck_SelfIntersectingWire: aError = \"SelfIntersectingWire\"; break;" EOL
143     "            // for faces" EOL
144     "          case BRepCheck_NoSurface: aError = \"NoSurface\"; break;" EOL
145     "          case BRepCheck_InvalidWire: aError = \"InvalidWire\"; break;" EOL
146     "          case BRepCheck_RedundantWire: aError = \"RedundantWire\"; break;" EOL
147     "          case BRepCheck_IntersectingWires: aError = \"IntersectingWires\"; break;" EOL
148     "          case BRepCheck_InvalidImbricationOfWires: aError = \"InvalidImbricationOfWires\"; break;" EOL
149     "            // for shells" EOL
150     "          case BRepCheck_EmptyShell: aError = \"EmptyShell\"; break;" EOL
151     "          case BRepCheck_RedundantFace: aError = \"RedundantFace\"; break;" EOL
152     "            // for shapes" EOL
153     "          case BRepCheck_UnorientableShape: aError = \"UnorientableShape\"; break;" EOL
154     "          case BRepCheck_NotClosed: aError = \"NotClosed\"; break;" EOL
155     "          case BRepCheck_NotConnected: aError = \"NotConnected\"; break;" EOL
156     "          case BRepCheck_SubshapeNotInShape: aError = \"SubshapeNotInShape\"; break;" EOL
157     "          case BRepCheck_BadOrientation: aError = \"BadOrientation\"; break;" EOL
158     "          case BRepCheck_BadOrientationOfSubshape: aError = \"BadOrientationOfSubshape\"; break;" EOL
159     "          case BRepCheck_InvalidToleranceValue: aError = \"InvalidToleranceValue\"; break;" EOL
160     "          default : aError = \"Undefined error\";" EOL
161     "          }" EOL
162     "        }" EOL
163     "      }" EOL
164     "    }" EOL
165     "  }" EOL
166     "  else" EOL
167     "  {" EOL
168     "    // aShape is a valid shape" EOL
169     "  }" EOL EOL
170
171     "//==========================================" EOL EOL;
172   setResultText (aText.ToCString());
173   getAISContext()->IsoOnPlane(Standard_False);
174   drawShape (aShape);
175   if (WAIT_A_SECOND) return;
176
177   // Checking
178   BRepCheck_Analyzer aChecker(aShape);
179   if (!aChecker.IsValid())
180   {
181     // Initialize sequence of subshapes
182     TopTools_SequenceOfShape aShapes;
183     TopTools_MapOfShape aMap;
184     TopAbs_ShapeEnum aTypes[] = {TopAbs_VERTEX, TopAbs_EDGE, 
185       TopAbs_WIRE, TopAbs_FACE, TopAbs_SHELL};
186     TopExp_Explorer ex;
187     for (int i=0; i < 5; i++)
188       for (ex.Init(aShape, aTypes[i]); ex.More(); ex.Next())
189         if (!aMap.Contains(ex.Current()))
190         {
191           aShapes.Append(ex.Current());
192           aMap.Add(ex.Current());
193         }
194
195     for (i=1; i <= aShapes.Length(); i++)
196     {
197       const TopoDS_Shape& aSubShape = aShapes(i);
198       Handle(BRepCheck_Result) aResult = aChecker.Result(aSubShape);
199       if (!aResult.IsNull())
200       {
201         BRepCheck_ListOfStatus aLstStatus;
202         // get statuses for a subshape
203         aLstStatus = aResult->Status();
204         // get statuses for a subshape in context
205         aResult->InitContextIterator();
206         while (aResult->MoreShapeInContext())
207         {
208           BRepCheck_ListOfStatus aLst1;
209           aLst1 = aResult->StatusOnShape();
210           aLstStatus.Append(aLst1);
211           aResult->NextShapeInContext();
212         }
213         BRepCheck_ListIteratorOfListOfStatus itl(aLstStatus);
214         for (; itl.More(); itl.Next())
215         {
216           BRepCheck_Status aStatus = itl.Value();
217           // use aStatus as you need
218           // ...
219
220           if (aStatus == BRepCheck_NoError)
221             continue;
222
223           TopAbs_ShapeEnum aType = aSubShape.ShapeType();
224           Standard_CString aTypeName;
225           switch (aType)
226           {
227           case TopAbs_SHELL: aTypeName = "SHELL"; break;
228           case TopAbs_FACE: aTypeName = "FACE"; break;
229           case TopAbs_WIRE: aTypeName = "WIRE"; break;
230           case TopAbs_EDGE: aTypeName = "EDGE"; break;
231           case TopAbs_VERTEX: aTypeName = "VERTEX"; break;
232           }
233
234           Standard_CString aError;
235           switch (aStatus)
236           {
237             // for vertices
238           case BRepCheck_InvalidPointOnCurve: aError = "InvalidPointOnCurve"; break;
239           case BRepCheck_InvalidPointOnCurveOnSurface: aError = "InvalidPointOnCurveOnSurface"; break;
240           case BRepCheck_InvalidPointOnSurface: aError = "InvalidPointOnSurface"; break;
241             // for edges
242           case BRepCheck_No3DCurve: aError = "No3DCurve"; break;
243           case BRepCheck_Multiple3DCurve: aError = "Multiple3DCurve"; break;
244           case BRepCheck_Invalid3DCurve: aError = "Invalid3DCurve"; break;
245           case BRepCheck_NoCurveOnSurface: aError = "NoCurveOnSurface"; break;
246           case BRepCheck_InvalidCurveOnSurface: aError = "InvalidCurveOnSurface"; break;
247           case BRepCheck_InvalidCurveOnClosedSurface: aError = "InvalidCurveOnClosedSurface"; break;
248           case BRepCheck_InvalidSameRangeFlag: aError = "InvalidSameRangeFlag"; break;
249           case BRepCheck_InvalidSameParameterFlag: aError = "InvalidSameParameterFlag"; break;
250           case BRepCheck_InvalidDegeneratedFlag: aError = "InvalidDegeneratedFlag"; break;
251           case BRepCheck_FreeEdge: aError = "FreeEdge"; break;
252           case BRepCheck_InvalidMultiConnexity: aError = "InvalidMultiConnexity"; break;
253           case BRepCheck_InvalidRange: aError = "InvalidRange"; break;
254             // for wires
255           case BRepCheck_EmptyWire: aError = "EmptyWire"; break;
256           case BRepCheck_RedundantEdge: aError = "RedundantEdge"; break;
257           case BRepCheck_SelfIntersectingWire: aError = "SelfIntersectingWire"; break;
258             // for faces
259           case BRepCheck_NoSurface: aError = "NoSurface"; break;
260           case BRepCheck_InvalidWire: aError = "InvalidWire"; break;
261           case BRepCheck_RedundantWire: aError = "RedundantWire"; break;
262           case BRepCheck_IntersectingWires: aError = "IntersectingWires"; break;
263           case BRepCheck_InvalidImbricationOfWires: aError = "InvalidImbricationOfWires"; break;
264             // for shells
265           case BRepCheck_EmptyShell: aError = "EmptyShell"; break;
266           case BRepCheck_RedundantFace: aError = "RedundantFace"; break;
267             // for shapes
268           case BRepCheck_UnorientableShape: aError = "UnorientableShape"; break;
269           case BRepCheck_NotClosed: aError = "NotClosed"; break;
270           case BRepCheck_NotConnected: aError = "NotConnected"; break;
271           case BRepCheck_SubshapeNotInShape: aError = "SubshapeNotInShape"; break;
272           case BRepCheck_BadOrientation: aError = "BadOrientation"; break;
273           case BRepCheck_BadOrientationOfSubshape: aError = "BadOrientationOfSubshape"; break;
274           case BRepCheck_InvalidToleranceValue: aError = "InvalidToleranceValue"; break;
275           default : aError = "Undefined error";
276           }
277
278           aText = aText + aTypeName + " : " + aError + EOL;
279           setResultText (aText.ToCString());
280           getAISContext()->IsoOnPlane(Standard_True);
281           Handle(AIS_InteractiveObject) obj = drawShape (aSubShape, Quantity_NOC_RED, Standard_False);
282           getAISContext()->SetDisplayMode(obj,AIS_WireFrame);
283           getAISContext()->Display(obj);
284           if (WAIT_A_SECOND) return;
285           getAISContext()->IsoOnPlane(Standard_False);
286           getAISContext()->Erase (obj);
287         }
288       }
289     }
290   }
291   else
292   {
293     aText = aText + "No errors in shape" + EOL;
294   }
295
296   setResultText (aText.ToCString());
297 }
298
299 void Validate_Presentation::sample(const Standard_CString aFileName)
300 {
301   TCollection_AsciiString aPath(GetDataDir());
302   aPath = aPath + "\\" + aFileName;
303
304   TopoDS_Shape aShape;
305   BRep_Builder aBld;
306   Standard_Boolean isRead = BRepTools::Read (aShape, aPath.ToCString(), aBld);
307   if (!isRead)
308   {
309     aPath += " was not found.  The sample can not be shown.";
310     setResultText(aPath.ToCString());
311     return;
312   }
313
314   checkShape (aShape);
315 }