b311480e |
1 | // Created on: 1995-03-06 |
2 | // Created by: Laurent PAINNOT |
3 | // Copyright (c) 1995-1999 Matra Datavision |
973c2be1 |
4 | // Copyright (c) 1999-2014 OPEN CASCADE SAS |
b311480e |
5 | // |
973c2be1 |
6 | // This file is part of Open CASCADE Technology software library. |
b311480e |
7 | // |
d5f74e42 |
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 |
973c2be1 |
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. |
b311480e |
13 | // |
973c2be1 |
14 | // Alternatively, this file may be used under the terms of Open CASCADE |
15 | // commercial license or contractual agreement. |
7fd59977 |
16 | |
42cf5bc1 |
17 | |
18 | #include <Draw_Appli.hxx> |
19 | #include <DrawTrSurf.hxx> |
42cf5bc1 |
20 | #include <DrawTrSurf_Triangulation.hxx> |
21 | #include <GeometryTest.hxx> |
5fe14d0f |
22 | #include <Poly.hxx> |
7fd59977 |
23 | #include <Poly_Array1OfTriangle.hxx> |
42cf5bc1 |
24 | #include <Poly_Polygon2D.hxx> |
25 | #include <Poly_Polygon3D.hxx> |
7fd59977 |
26 | #include <Poly_Triangle.hxx> |
42cf5bc1 |
27 | #include <Poly_Triangulation.hxx> |
7fd59977 |
28 | #include <TColgp_Array1OfPnt.hxx> |
29 | #include <TColgp_Array1OfPnt2d.hxx> |
7fd59977 |
30 | |
57c28b61 |
31 | #ifdef _WIN32 |
7fd59977 |
32 | Standard_IMPORT Draw_Viewer dout; |
33 | #endif |
34 | //======================================================================= |
35 | //function : polytr |
36 | //purpose : |
37 | //======================================================================= |
38 | |
39 | static Standard_Integer polytr(Draw_Interpretor& di, Standard_Integer n, const char** a) |
40 | { |
41 | if (n < 4) |
42 | return 1; |
43 | |
91322f44 |
44 | Standard_Integer nbNodes = Draw::Atoi(a[2]); |
45 | Standard_Integer nbTri = Draw::Atoi(a[3]); |
7fd59977 |
46 | |
47 | // read the nodes |
48 | Standard_Integer i, j = 4; |
49 | TColgp_Array1OfPnt Nodes(1, nbNodes); |
50 | |
51 | for (i = 1; i <= nbNodes; i++) { |
52 | if (j + 2 >= n) { |
53 | di << "Not enough nodes"; |
54 | return 1; |
55 | } |
91322f44 |
56 | Nodes(i).SetCoord(Draw::Atof(a[j]),Draw::Atof(a[j+1]),Draw::Atof(a[j+2])); |
7fd59977 |
57 | j += 3; |
58 | } |
59 | |
60 | // read the triangles |
61 | |
62 | Poly_Array1OfTriangle Triangles(1, nbTri); |
63 | for (i = 1; i <= nbTri; i++) { |
64 | if (j + 2 >= n) { |
65 | di << "Not enough triangles"; |
66 | return 1; |
67 | } |
91322f44 |
68 | Triangles(i).Set(Draw::Atoi(a[j]),Draw::Atoi(a[j+1]),Draw::Atoi(a[j+2])); |
7fd59977 |
69 | j += 3; |
70 | } |
71 | |
72 | Handle(Poly_Triangulation) T = new Poly_Triangulation(Nodes,Triangles); |
73 | |
74 | DrawTrSurf::Set(a[1],T); |
75 | |
76 | return 0;//wnt |
77 | } |
78 | |
79 | |
80 | //======================================================================= |
81 | //function : polygon3d |
82 | //purpose : |
83 | //======================================================================= |
84 | |
85 | static Standard_Integer polygon3d(Draw_Interpretor& di, Standard_Integer n, const char** a) |
86 | { |
87 | if (n < 4) |
88 | return 1; |
89 | |
91322f44 |
90 | Standard_Integer nbNodes = Draw::Atoi(a[2]); |
7fd59977 |
91 | |
92 | // read the nodes |
93 | Standard_Integer i, j = 3; |
94 | TColgp_Array1OfPnt Nodes(1, nbNodes); |
95 | |
96 | for (i = 1; i <= nbNodes; i++) { |
97 | if (j + 2 >= n) { |
98 | di << "Not enough nodes"; |
99 | return 1; |
100 | } |
91322f44 |
101 | Nodes(i).SetCoord(Draw::Atof(a[j]),Draw::Atof(a[j+1]),Draw::Atof(a[j+2])); |
7fd59977 |
102 | j += 3; |
103 | } |
104 | |
105 | Handle(Poly_Polygon3D) P3d = new Poly_Polygon3D(Nodes); |
106 | |
107 | DrawTrSurf::Set(a[1], P3d); |
108 | |
109 | return 0; |
110 | } |
111 | |
112 | //======================================================================= |
113 | //function : polygon2d |
114 | //purpose : |
115 | //======================================================================= |
116 | |
117 | static Standard_Integer polygon2d(Draw_Interpretor& di, Standard_Integer n, const char** a) |
118 | { |
119 | if (n < 4) |
120 | return 1; |
121 | |
91322f44 |
122 | Standard_Integer nbNodes = Draw::Atoi(a[2]); |
7fd59977 |
123 | |
124 | // read the nodes |
125 | Standard_Integer i, j = 3; |
126 | TColgp_Array1OfPnt2d Nodes(1, nbNodes); |
127 | |
128 | for (i = 1; i <= nbNodes; i++) { |
129 | if (j + 1 >= n) { |
130 | di << "Not enough nodes"; |
131 | return 1; |
132 | } |
91322f44 |
133 | Nodes(i).SetCoord(Draw::Atof(a[j]),Draw::Atof(a[j+1])); |
7fd59977 |
134 | j += 2; |
135 | } |
136 | |
137 | Handle(Poly_Polygon2D) P2d = new Poly_Polygon2D(Nodes); |
138 | |
139 | DrawTrSurf::Set(a[1], P2d); |
140 | |
141 | return 0; |
142 | } |
143 | |
144 | |
145 | //======================================================================= |
146 | //function : shnodes |
147 | //purpose : |
148 | //======================================================================= |
149 | |
150 | static Standard_Integer shnodes(Draw_Interpretor& , Standard_Integer n, const char** a) |
151 | { |
152 | if (n != 2) return 1; |
153 | Handle(DrawTrSurf_Triangulation) T |
154 | = Handle(DrawTrSurf_Triangulation)::DownCast(Draw::Get(a[1])); |
155 | |
156 | if (!T.IsNull()) { |
157 | Standard_Boolean SHOWNODES = T->ShowNodes(); |
158 | T->ShowNodes(!SHOWNODES); |
159 | } |
160 | |
161 | |
162 | |
163 | dout.RepaintAll(); |
164 | |
165 | return 0;//wnt |
166 | } |
167 | |
168 | //======================================================================= |
169 | //function : shtriangles |
170 | //purpose : |
171 | //======================================================================= |
172 | |
173 | static Standard_Integer shtriangles(Draw_Interpretor& , Standard_Integer n, const char** a) |
174 | { |
175 | if (n != 2) return 1; |
176 | |
177 | Handle(DrawTrSurf_Triangulation) T |
178 | = Handle(DrawTrSurf_Triangulation)::DownCast(Draw::Get(a[1])); |
179 | Standard_Boolean SHOWTRIANGLES = T->ShowTriangles(); |
180 | T->ShowTriangles(!SHOWTRIANGLES); |
181 | dout.RepaintAll(); |
182 | return 0;//wnt |
183 | } |
184 | |
5fe14d0f |
185 | //======================================================================= |
186 | //function : AddNode |
187 | //purpose : |
188 | //======================================================================= |
189 | template <typename Poly, typename Point, typename PointArr> |
190 | static inline void AddNode(const Handle(Poly)& thePolygon, |
191 | const Point& thePnt, |
192 | PointArr& theNodes) |
193 | { |
194 | for (Standard_Integer i = thePolygon->Nodes().Lower(); |
195 | i <= thePolygon->Nodes().Upper(); i++) |
196 | { |
197 | theNodes[i] = thePolygon->Nodes()[i]; |
198 | } |
199 | |
200 | theNodes.ChangeLast() = thePnt; |
201 | } |
202 | |
203 | //======================================================================= |
204 | //function : AddNode |
205 | //purpose : |
206 | //======================================================================= |
207 | static Standard_Integer AddNode(Draw_Interpretor& theDI, |
208 | Standard_Integer theNArg, |
209 | const char** theArgVal) |
210 | { |
211 | if (theNArg < 4) |
212 | { |
213 | theDI << "Not enough arguments\n"; |
214 | return 1; |
215 | } |
216 | |
217 | if (theNArg == 4) |
218 | { |
219 | Handle(Poly_Polygon2D) aPoly2d = DrawTrSurf::GetPolygon2D(theArgVal[1]); |
220 | TColgp_Array1OfPnt2d aNodes(aPoly2d->Nodes().Lower(), |
221 | aPoly2d->Nodes().Upper() + 1); |
222 | AddNode(aPoly2d, gp_Pnt2d(Draw::Atof(theArgVal[2]), |
223 | Draw::Atof(theArgVal[3])), aNodes); |
224 | aPoly2d.Nullify(); |
225 | aPoly2d = new Poly_Polygon2D(aNodes); |
226 | DrawTrSurf::Set(theArgVal[1], aPoly2d); |
227 | } |
228 | else |
229 | { |
230 | Handle(Poly_Polygon3D) aPoly3d = DrawTrSurf::GetPolygon3D(theArgVal[1]); |
231 | TColgp_Array1OfPnt aNodes(aPoly3d->Nodes().Lower(), |
232 | aPoly3d->Nodes().Upper() + 1); |
233 | AddNode(aPoly3d, gp_Pnt(Draw::Atof(theArgVal[2]), |
234 | Draw::Atof(theArgVal[3]), |
235 | Draw::Atof(theArgVal[4])), aNodes); |
236 | aPoly3d.Nullify(); |
237 | aPoly3d = new Poly_Polygon3D(aNodes); |
238 | DrawTrSurf::Set(theArgVal[1], aPoly3d); |
239 | } |
240 | |
241 | return 0; |
242 | } |
243 | |
244 | //======================================================================= |
245 | //function : PolygonProps |
246 | //purpose : |
247 | //======================================================================= |
248 | static Standard_Integer PolygonProps(Draw_Interpretor& theDI, |
249 | Standard_Integer theNArg, |
250 | const char** theArgVal) |
251 | { |
252 | if (theNArg < 2) |
253 | { |
254 | theDI << "Use: polygonprops polygon2d [-area val] [-perimeter val]\n"; |
255 | return 1; |
256 | } |
257 | |
258 | Handle(Poly_Polygon2D) aPoly2d = DrawTrSurf::GetPolygon2D(theArgVal[1]); |
259 | |
260 | Standard_Real anArea = 0.0, aPerimeter = 0.0; |
261 | Poly::PolygonProperties(aPoly2d->Nodes(), anArea, aPerimeter); |
262 | |
263 | theDI << "Area = " << anArea << "\n"; |
264 | theDI << "Perimeter = " << aPerimeter << "\n"; |
265 | |
266 | for (Standard_Integer i = 2; i < theNArg; i++) |
267 | { |
268 | if (!strcmp(theArgVal[i], "-area")) |
269 | { |
270 | Draw::Set(theArgVal[++i], anArea); |
271 | continue; |
272 | } |
273 | |
274 | if (!strcmp(theArgVal[i], "-perimeter")) |
275 | { |
276 | Draw::Set(theArgVal[++i], aPerimeter); |
277 | continue; |
278 | } |
279 | |
280 | theDI << "Error: Wrong option: \"" << theArgVal[i] << "\"\n"; |
281 | break; |
282 | } |
283 | |
284 | return 0; |
285 | } |
286 | |
7fd59977 |
287 | //======================================================================= |
288 | //function : PolyCommands |
289 | //purpose : |
290 | //======================================================================= |
291 | |
292 | void GeometryTest::PolyCommands(Draw_Interpretor& theCommands) |
293 | { |
294 | |
295 | const char* g = "Poly Commands"; |
296 | |
297 | theCommands.Add("polytr","polytr name nbnodes nbtri x1 y1 z1 ... n1 n2 n3 ...",__FILE__,polytr,g); |
298 | theCommands.Add("polygon3d","polygon3d name nbnodes x1 y1 z1 ...",__FILE__,polygon3d,g); |
299 | theCommands.Add("polygon2d","polygon2d name nbnodes x1 y1 ...",__FILE__,polygon2d,g); |
5fe14d0f |
300 | theCommands.Add("addpolygonnode","addpolygonnode polygon3d(2d) x y [z]",__FILE__, AddNode,g); |
301 | theCommands.Add("polygonprops","Computes area and perimeter of 2D-polygon. " |
302 | "Run \"polygonprops\" w/o any arguments to read help.\n", |
303 | __FILE__, PolygonProps,g); |
7fd59977 |
304 | theCommands.Add("shnodes","shnodes name", __FILE__,shnodes, g); |
305 | theCommands.Add("shtriangles","shtriangles name", __FILE__,shtriangles, g); |
306 | } |