0022048: Visualization, AIS_InteractiveContext - single object selection should alway...
[occt.git] / src / GeometryTest / GeometryTest_PolyCommands.cxx
1 // Created on: 1995-03-06
2 // Created by: Laurent PAINNOT
3 // Copyright (c) 1995-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
18 #include <Draw_Appli.hxx>
19 #include <DrawTrSurf.hxx>
20 #include <DrawTrSurf_Polygon2D.hxx>
21 #include <DrawTrSurf_Polygon3D.hxx>
22 #include <DrawTrSurf_Triangulation.hxx>
23 #include <GeometryTest.hxx>
24 #include <Poly_Array1OfTriangle.hxx>
25 #include <Poly_Polygon2D.hxx>
26 #include <Poly_Polygon3D.hxx>
27 #include <Poly_Triangle.hxx>
28 #include <Poly_Triangulation.hxx>
29 #include <TColgp_Array1OfPnt.hxx>
30 #include <TColgp_Array1OfPnt2d.hxx>
31
32 #ifdef _WIN32
33 Standard_IMPORT Draw_Viewer dout;
34 #endif
35 //=======================================================================
36 //function : polytr
37 //purpose  : 
38 //=======================================================================
39
40 static Standard_Integer polytr(Draw_Interpretor& di, Standard_Integer n, const char** a)
41 {
42   if (n < 4)
43     return 1;
44
45   Standard_Integer nbNodes = Draw::Atoi(a[2]);
46   Standard_Integer nbTri   = Draw::Atoi(a[3]);
47
48   // read the nodes
49   Standard_Integer i, j = 4;
50   TColgp_Array1OfPnt Nodes(1, nbNodes);
51
52   for (i = 1; i <= nbNodes; i++) {
53     if (j + 2 >= n) {
54       di << "Not enough nodes";
55       return 1;
56     }
57     Nodes(i).SetCoord(Draw::Atof(a[j]),Draw::Atof(a[j+1]),Draw::Atof(a[j+2]));
58     j += 3;
59   }
60
61   // read the triangles
62
63   Poly_Array1OfTriangle Triangles(1, nbTri);
64   for (i = 1; i <= nbTri; i++) {
65     if (j + 2 >= n) {
66       di << "Not enough triangles";
67       return 1;
68     }
69     Triangles(i).Set(Draw::Atoi(a[j]),Draw::Atoi(a[j+1]),Draw::Atoi(a[j+2]));
70     j += 3;
71   }
72
73   Handle(Poly_Triangulation) T = new Poly_Triangulation(Nodes,Triangles);
74
75   DrawTrSurf::Set(a[1],T);
76
77   return 0;//wnt
78 }
79
80
81 //=======================================================================
82 //function : polygon3d
83 //purpose  : 
84 //=======================================================================
85
86 static Standard_Integer polygon3d(Draw_Interpretor& di, Standard_Integer n, const char** a)
87 {
88   if (n < 4)
89     return 1;
90
91   Standard_Integer nbNodes = Draw::Atoi(a[2]);
92
93   // read the nodes
94   Standard_Integer i, j = 3;
95   TColgp_Array1OfPnt Nodes(1, nbNodes);
96
97   for (i = 1; i <= nbNodes; i++) {
98     if (j + 2 >= n) {
99       di << "Not enough nodes";
100       return 1;
101     }
102     Nodes(i).SetCoord(Draw::Atof(a[j]),Draw::Atof(a[j+1]),Draw::Atof(a[j+2]));
103     j += 3;
104   }
105
106   Handle(Poly_Polygon3D) P3d = new Poly_Polygon3D(Nodes);
107
108   DrawTrSurf::Set(a[1], P3d);
109
110   return 0;
111 }
112
113 //=======================================================================
114 //function : polygon2d
115 //purpose  : 
116 //=======================================================================
117
118 static Standard_Integer polygon2d(Draw_Interpretor& di, Standard_Integer n, const char** a)
119 {
120   if (n < 4)
121     return 1;
122
123   Standard_Integer nbNodes = Draw::Atoi(a[2]);
124
125   // read the nodes
126   Standard_Integer i, j = 3;
127   TColgp_Array1OfPnt2d Nodes(1, nbNodes);
128
129   for (i = 1; i <= nbNodes; i++) {
130     if (j + 1 >= n) {
131       di << "Not enough nodes";
132       return 1;
133     }
134     Nodes(i).SetCoord(Draw::Atof(a[j]),Draw::Atof(a[j+1]));
135     j += 2;
136   }
137
138   Handle(Poly_Polygon2D) P2d = new Poly_Polygon2D(Nodes);
139
140   DrawTrSurf::Set(a[1], P2d);
141
142   return 0;
143 }
144
145
146 //=======================================================================
147 //function : shnodes
148 //purpose  : 
149 //=======================================================================
150
151 static Standard_Integer shnodes(Draw_Interpretor& , Standard_Integer n, const char** a)
152 {
153   if (n != 2) return 1;
154   Handle(DrawTrSurf_Triangulation) T 
155     = Handle(DrawTrSurf_Triangulation)::DownCast(Draw::Get(a[1]));
156
157   if (!T.IsNull()) {
158     Standard_Boolean SHOWNODES = T->ShowNodes();
159     T->ShowNodes(!SHOWNODES);
160   }
161
162   
163
164   dout.RepaintAll();
165
166   return 0;//wnt
167 }
168
169 //=======================================================================
170 //function : shtriangles
171 //purpose  : 
172 //=======================================================================
173
174 static Standard_Integer shtriangles(Draw_Interpretor& , Standard_Integer n, const char** a)
175 {
176   if (n != 2) return 1;
177   
178   Handle(DrawTrSurf_Triangulation) T 
179     = Handle(DrawTrSurf_Triangulation)::DownCast(Draw::Get(a[1]));
180   Standard_Boolean SHOWTRIANGLES = T->ShowTriangles();
181   T->ShowTriangles(!SHOWTRIANGLES);
182   dout.RepaintAll();
183   return 0;//wnt
184 }
185
186 //=======================================================================
187 //function : PolyCommands
188 //purpose  : 
189 //=======================================================================
190
191 void GeometryTest::PolyCommands(Draw_Interpretor& theCommands)
192 {
193
194   const char* g = "Poly Commands";
195
196   theCommands.Add("polytr","polytr name nbnodes nbtri x1 y1 z1 ... n1 n2 n3 ...",__FILE__,polytr,g);
197   theCommands.Add("polygon3d","polygon3d name nbnodes x1 y1 z1  ...",__FILE__,polygon3d,g);
198   theCommands.Add("polygon2d","polygon2d name nbnodes x1 y1  ...",__FILE__,polygon2d,g);
199   theCommands.Add("shnodes","shnodes name", __FILE__,shnodes, g);
200   theCommands.Add("shtriangles","shtriangles name", __FILE__,shtriangles, g);
201 }