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
6 // This file is part of Open CASCADE Technology software library.
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.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
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>
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>
34 Standard_IMPORT Draw_Viewer dout;
36 //=======================================================================
39 //=======================================================================
41 static Standard_Integer polytr(Draw_Interpretor& di, Standard_Integer n, const char** a)
46 Standard_Integer nbNodes = Draw::Atoi(a[2]);
47 Standard_Integer nbTri = Draw::Atoi(a[3]);
50 Standard_Integer i, j = 4;
51 TColgp_Array1OfPnt Nodes(1, nbNodes);
53 for (i = 1; i <= nbNodes; i++) {
55 di << "Not enough nodes";
58 Nodes(i).SetCoord(Draw::Atof(a[j]),Draw::Atof(a[j+1]),Draw::Atof(a[j+2]));
64 Poly_Array1OfTriangle Triangles(1, nbTri);
65 for (i = 1; i <= nbTri; i++) {
67 di << "Not enough triangles";
70 Triangles(i).Set(Draw::Atoi(a[j]),Draw::Atoi(a[j+1]),Draw::Atoi(a[j+2]));
74 Handle(Poly_Triangulation) T = new Poly_Triangulation(Nodes,Triangles);
76 DrawTrSurf::Set(a[1],T);
82 //=======================================================================
83 //function : polygon3d
85 //=======================================================================
87 static Standard_Integer polygon3d(Draw_Interpretor& di, Standard_Integer n, const char** a)
92 Standard_Integer nbNodes = Draw::Atoi(a[2]);
95 Standard_Integer i, j = 3;
96 TColgp_Array1OfPnt Nodes(1, nbNodes);
98 for (i = 1; i <= nbNodes; i++) {
100 di << "Not enough nodes";
103 Nodes(i).SetCoord(Draw::Atof(a[j]),Draw::Atof(a[j+1]),Draw::Atof(a[j+2]));
107 Handle(Poly_Polygon3D) P3d = new Poly_Polygon3D(Nodes);
109 DrawTrSurf::Set(a[1], P3d);
114 //=======================================================================
115 //function : polygon2d
117 //=======================================================================
119 static Standard_Integer polygon2d(Draw_Interpretor& di, Standard_Integer n, const char** a)
124 Standard_Integer nbNodes = Draw::Atoi(a[2]);
127 Standard_Integer i, j = 3;
128 TColgp_Array1OfPnt2d Nodes(1, nbNodes);
130 for (i = 1; i <= nbNodes; i++) {
132 di << "Not enough nodes";
135 Nodes(i).SetCoord(Draw::Atof(a[j]),Draw::Atof(a[j+1]));
139 Handle(Poly_Polygon2D) P2d = new Poly_Polygon2D(Nodes);
141 DrawTrSurf::Set(a[1], P2d);
147 //=======================================================================
150 //=======================================================================
152 static Standard_Integer shnodes(Draw_Interpretor& , Standard_Integer n, const char** a)
154 if (n != 2) return 1;
155 Handle(DrawTrSurf_Triangulation) T
156 = Handle(DrawTrSurf_Triangulation)::DownCast(Draw::Get(a[1]));
159 Standard_Boolean SHOWNODES = T->ShowNodes();
160 T->ShowNodes(!SHOWNODES);
170 //=======================================================================
171 //function : shtriangles
173 //=======================================================================
175 static Standard_Integer shtriangles(Draw_Interpretor& , Standard_Integer n, const char** a)
177 if (n != 2) return 1;
179 Handle(DrawTrSurf_Triangulation) T
180 = Handle(DrawTrSurf_Triangulation)::DownCast(Draw::Get(a[1]));
181 Standard_Boolean SHOWTRIANGLES = T->ShowTriangles();
182 T->ShowTriangles(!SHOWTRIANGLES);
187 //=======================================================================
190 //=======================================================================
191 template <typename Poly, typename Point, typename PointArr>
192 static inline void AddNode(const Handle(Poly)& thePolygon,
196 for (Standard_Integer i = thePolygon->Nodes().Lower();
197 i <= thePolygon->Nodes().Upper(); i++)
199 theNodes[i] = thePolygon->Nodes()[i];
202 theNodes.ChangeLast() = thePnt;
205 //=======================================================================
208 //=======================================================================
209 static Standard_Integer AddNode(Draw_Interpretor& theDI,
210 Standard_Integer theNArg,
211 const char** theArgVal)
215 theDI << "Not enough arguments\n";
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);
227 aPoly2d = new Poly_Polygon2D(aNodes);
228 DrawTrSurf::Set(theArgVal[1], aPoly2d);
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);
239 aPoly3d = new Poly_Polygon3D(aNodes);
240 DrawTrSurf::Set(theArgVal[1], aPoly3d);
246 //=======================================================================
247 //function : PolygonProps
249 //=======================================================================
250 static Standard_Integer PolygonProps(Draw_Interpretor& theDI,
251 Standard_Integer theNArg,
252 const char** theArgVal)
256 theDI << "Use: polygonprops polygon2d [-area val] [-perimeter val]\n";
260 Handle(Poly_Polygon2D) aPoly2d = DrawTrSurf::GetPolygon2D(theArgVal[1]);
262 Standard_Real anArea = 0.0, aPerimeter = 0.0;
263 Poly::PolygonProperties(aPoly2d->Nodes(), anArea, aPerimeter);
265 theDI << "Area = " << anArea << "\n";
266 theDI << "Perimeter = " << aPerimeter << "\n";
268 for (Standard_Integer i = 2; i < theNArg; i++)
270 if (!strcmp(theArgVal[i], "-area"))
272 Draw::Set(theArgVal[++i], anArea);
276 if (!strcmp(theArgVal[i], "-perimeter"))
278 Draw::Set(theArgVal[++i], aPerimeter);
282 theDI << "Error: Wrong option: \"" << theArgVal[i] << "\"\n";
289 //=======================================================================
290 //function : PolyCommands
292 //=======================================================================
294 void GeometryTest::PolyCommands(Draw_Interpretor& theCommands)
297 const char* g = "Poly Commands";
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);