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