ebc578c92a2e64685f390b07bcca935342ecd431
[occt.git] / src / QAAMINO / QAAMINO.cxx
1 // Created on: 2002-07-18
2 // Created by: QA Admin
3 // Copyright (c) 2002-2012 OPEN CASCADE SAS
4 //
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
9 //
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 //
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
19
20
21
22 #include <QAAMINO.hxx>
23
24 #include <Draw_Interpretor.hxx>
25 #include <DBRep.hxx>
26 #include <DrawTrSurf.hxx>
27 #include <AIS_InteractiveContext.hxx>
28 #include <ViewerTest.hxx>
29 #include <AIS_Shape.hxx>
30 #include <TopoDS_Shape.hxx>
31 #include <TopoDS_Vertex.hxx>
32
33 #include <TopExp_Explorer.hxx>
34 #include <TopoDS_Face.hxx>
35 #include <TopoDS.hxx>
36 #include <BRepBndLib.hxx>
37 #include <gp_Pln.hxx>
38 #include <BRep_Tool.hxx>
39 #include <BRepAlgoAPI_Section.hxx>
40 #include <BRepAlgo_Section.hxx>
41 #include <Precision.hxx>
42 #include <Standard_ErrorHandler.hxx>
43
44 #include <stdio.h>
45
46 //=======================================================================
47 //function : OCC527 
48 //purpose  : 
49 //=======================================================================
50 static Standard_Integer OCC527(Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
51 {
52   try
53   {
54     OCC_CATCH_SIGNALS
55     // 1. Verify amount of arguments of the command
56     //if (argc < 2) { di << "OCC527 FAULTY. Use : OCC527 shape "; return 0;} 
57     if (argc < 2 || argc > 3) {
58       di << "Usage : " << argv[0] << " shape [BRepAlgoAPI/BRepAlgo = 1/0]" << "\n";
59       return 1;
60     }
61     Standard_Boolean IsBRepAlgoAPI = Standard_True;
62     if (argc == 3) {
63       Standard_Integer IsB = atoi(argv[2]);
64       if (IsB != 1) {
65         IsBRepAlgoAPI = Standard_False;
66 //#if ! defined(BRepAlgo_def04)
67 //      di << "Error: There is not BRepAlgo_Section class" << "\n";
68 //      return 1;
69 //#endif
70       }
71     }
72     
73     // 2. Get selected shape
74     TopoDS_Shape aShape = DBRep::Get(argv[1]);
75     if(aShape.IsNull()) { di << "OCC527 FAULTY. Entry shape is NULL"; return 0;} 
76
77     // 3. Explode entry shape on faces and build sections from Zmin to Zmax with step aStep
78     const Standard_Real Zmin = -40.228173882121, Zmax = 96.408126285268, aStep = 1.0;
79     char str[100]; str[0] = 0; sprintf(str,"Test range: [%f, %f] with step %f\n",Zmin,Zmax,aStep); di << str;
80     int nbf = 0;
81     TopExp_Explorer aExp1;
82     for (aExp1.Init(aShape,TopAbs_FACE); aExp1.More(); aExp1.Next())
83     {
84       // Process one face
85       str[0] = 0; sprintf(str,"Face #%d: \t",nbf++); di << str;
86       TopoDS_Face aFace = TopoDS::Face(aExp1.Current());
87       
88       // Build BndBox in order to avoid try of building section 
89       // if plane of the one does not intersect BndBox of the face
90       Bnd_Box aFaceBox;
91       BRepBndLib::Add(aFace,aFaceBox);
92       Standard_Real X1,X2,Y1,Y2,Z1,Z2;
93       aFaceBox.Get(X1,Y1,Z1,X2,Y2,Z2);
94
95       // Build sections from Zmin to Zmax with step aStep
96       Standard_Boolean wasBuilt = Standard_False;
97       double gmaxdist = 0.0, gzmax = Zmax;
98       for (double zcur = Zmax; zcur > Zmin; zcur -= aStep)
99       {
100         // If plane of the section does not intersect BndBox of the face do nothing
101         if(zcur < Z1 || zcur > Z2 ) continue;
102                 
103         // Build current section
104         gp_Pln pl(0,0,1,-zcur);
105 //#if ! defined(BRepAlgoAPI_def01)
106 //        BRepAlgoAPI_Section aSection(aFace,pl,Standard_False);
107 //#else
108 //        BRepAlgo_Section aSection(aFace,pl,Standard_False);
109 //#endif
110 //        aSection.Approximation(Standard_True);
111 //        aSection.Build();
112 //        // If section was built meassure distance between vertexes and plane of the one. Max distance is stored.
113 //        if (aSection.IsDone())
114         
115         Standard_Boolean IsDone;
116         TopoDS_Shape aResult;
117         if (IsBRepAlgoAPI) {
118           di << "BRepAlgoAPI_Section aSection(aFace,pl,Standard_False)" <<"\n";
119           BRepAlgoAPI_Section aSection(aFace,pl,Standard_False);
120           aSection.Approximation(Standard_True);
121           aSection.Build();
122           IsDone = aSection.IsDone();
123           aResult = aSection.Shape();
124         } else {
125           di << "BRepAlgo_Section aSection(aFace,pl,Standard_False)" <<"\n";
126           BRepAlgo_Section aSection(aFace,pl,Standard_False);
127           aSection.Approximation(Standard_True);
128           aSection.Build();
129           IsDone = aSection.IsDone();
130           aResult = aSection.Shape();
131         }
132
133         if (IsDone)
134           {
135 //          TopoDS_Shape aResult = aSection.Shape();
136             if (!aResult.IsNull())
137               {
138             double lmaxdist = 0.0;
139             double lmaxtoler = 0.0;
140             TopExp_Explorer aExp2;
141             for (aExp2.Init(aResult,TopAbs_VERTEX); aExp2.More(); aExp2.Next())
142             {
143               TopoDS_Vertex aV = TopoDS::Vertex(aExp2.Current());
144               Standard_Real  toler = BRep_Tool::Tolerance(aV);
145               double dist = pl.Distance(BRep_Tool::Pnt(aV));
146               if (dist > lmaxdist) lmaxdist = dist;
147               wasBuilt = Standard_True;
148       // If section was built check distance beetwen vertexes and plane of the one
149       str[0] =0;
150 //       if (wasBuilt) 
151 //       {
152 //         if(gmaxdist > Precision::Confusion())
153 //           sprintf(str,"Dist=%f, Param=%f FAULTY\n",gmaxdist,gzmax);
154 //         else
155 //           sprintf(str,"Dist=%f, Param=%f\n",gmaxdist,gzmax);
156         if(dist > toler)
157           sprintf(str,"Dist=%f, Toler=%f, Param=%f FAULTY\n",dist,toler,gzmax);
158         else
159           sprintf(str,"Dist=%f, Toler=%f, Param=%f\n",dist,toler,gzmax);
160 //       }
161 //       else sprintf(str,"No result\n");
162       di << str;
163             }
164             if (lmaxdist > gmaxdist)
165             {
166               gmaxdist = lmaxdist;
167               gzmax = zcur;
168             }
169           }
170         }
171       }
172     }
173   }
174   catch (Standard_Failure) {di << "OCC527 Exception \n" ;return 0;}
175   
176   return 0;
177 }
178
179 #include <StlMesh_Mesh.hxx>
180 #include <StlTransfer.hxx>
181 //=======================================================================
182 //function : OCC1048
183 //purpose  : 
184 //=======================================================================
185 static Standard_Integer OCC1048 (Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
186 {
187   // Verify amount of arguments of the command
188   if (argc < 2) { di << "Usage : " << argv[0] <<" shape"; return 1;}
189
190   TopoDS_Shape aShape = DBRep::Get(argv[1]);
191
192   Standard_Real theDeflection = 0.006;
193   Handle(StlMesh_Mesh) theStlMesh = new StlMesh_Mesh;
194   StlTransfer::BuildIncrementalMesh(aShape, theDeflection, theStlMesh);
195
196   Standard_Integer NBTRIANGLES = theStlMesh->NbTriangles();
197   di<<"Info: Number of triangles = "<<NBTRIANGLES<<"\n";
198
199   return 0;
200 }
201
202 void QAAMINO::Commands(Draw_Interpretor& theCommands) {
203   char *group = "QAAMINO";
204
205   //theCommands.Add("OCC527", "OCC527 shape", __FILE__, OCC527, group);  
206   theCommands.Add("OCC527", "OCC527 shape [BRepAlgoAPI/BRepAlgo = 1/0]", __FILE__, OCC527, group);  
207   theCommands.Add("OCC1048", "OCC1048 shape", __FILE__, OCC1048, group);  
208   return;
209 }