1 // Created on: 1994-02-18
2 // Created by: Remi LEQUETTE
3 // Copyright (c) 1994-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.
17 #include <Standard_Stream.hxx>
18 #include <BRepTest.hxx>
19 #include <Draw_Interpretor.hxx>
20 #include <Draw_Appli.hxx>
22 #include <BRepGProp.hxx>
23 #include <TopoDS_Shape.hxx>
24 #include <GProp_PrincipalProps.hxx>
28 #include <Draw_Axis3D.hxx>
29 #include <Precision.hxx>
30 #include <OSD_Chronometer.hxx>
31 #include <Geom_Surface.hxx>
32 #include <DrawTrSurf.hxx>
33 #include <Geom_Plane.hxx>
37 Standard_IMPORT Draw_Viewer dout;
41 Standard_Integer props(Draw_Interpretor& di, Standard_Integer n, const char** a)
44 di << "Use: " << a[0] << " shape [epsilon] [c[losed]] [x y z] [-skip] [-full] [-tri]\n";
45 di << "Compute properties of the shape, exact geometry (curves, surfaces) or\n";
46 di << "some discrete data (polygons, triangulations) can be used for calculations\n";
47 di << "The epsilon, if given, defines relative precision of computation\n";
48 di << "The \"closed\" flag, if present, do computation only closed shells of the shape\n";
49 di << "The centroid coordinates will be put to DRAW variables x y z (if given)\n";
50 di << "Shared entities will be take in account only one time in the skip mode\n";
51 di << "All values are outputted with the full precision in the full mode.\n";
52 di << "Preferable source of geometry data are triangulations in case if it exists, if the -tri key is used.\n";
53 di << "If epsilon is given, exact geometry (curves, surfaces) are used for calculations independently of using key -tri\n\n";
57 Standard_Boolean UseTriangulation = Standard_False;
58 if (n >= 2 && strcmp(a[n - 1], "-tri") == 0)
60 UseTriangulation = Standard_True;
63 Standard_Boolean isFullMode = Standard_False;
64 if (n >= 2 && strcmp(a[n-1], "-full") == 0)
66 isFullMode = Standard_True;
69 Standard_Boolean SkipShared = Standard_False;
70 if (n >= 2 && strcmp(a[n-1], "-skip") == 0)
72 SkipShared = Standard_True;
76 TopoDS_Shape S = DBRep::Get(a[1]);
77 if (S.IsNull()) return 0;
81 Standard_Boolean onlyClosed = Standard_False;
82 Standard_Real eps = 1.0;
83 Standard_Boolean witheps = Standard_False;
84 if((n > 2 && *a[2]=='c') || (n > 3 && *a[3]=='c')) onlyClosed = Standard_True;
85 if(n > 2 && *a[2]!='c' && n != 5) {eps = Draw::Atof (a[2]); witheps = Standard_True;}
88 if (Abs(eps) < Precision::Angular()) return 2;
90 BRepGProp::LinearProperties(S,G,SkipShared);
91 else if (*a[0] == 's')
92 eps = BRepGProp::SurfaceProperties(S,G,eps,SkipShared);
94 eps = BRepGProp::VolumeProperties(S,G,eps,onlyClosed,SkipShared);
98 BRepGProp::LinearProperties(S, G, SkipShared, UseTriangulation);
99 else if (*a[0] == 's')
100 BRepGProp::SurfaceProperties(S, G, SkipShared, UseTriangulation);
102 BRepGProp::VolumeProperties(S,G,onlyClosed,SkipShared, UseTriangulation);
105 gp_Pnt P = G.CentreOfMass();
106 gp_Mat I = G.MatrixOfInertia();
109 Standard_Integer shift = n - 5;
110 Draw::Set(a[shift+2],P.X());
111 Draw::Set(a[shift+3],P.Y());
112 Draw::Set(a[shift+4],P.Z());
115 GProp_PrincipalProps Pr = G.PrincipalProperties();
116 Standard_Real Ix,Iy,Iz;
117 Pr.Moments(Ix,Iy,Iz);
121 Standard_SStream aSStream1;
123 aSStream1 << "Mass : " << std::setw(15) << G.Mass() << "\n\n";
124 if(witheps && *a[0] != 'l') aSStream1 << "Relative error of mass computation : " << std::setw(15) << eps << "\n\n";
126 aSStream1 << "Center of gravity : \n";
127 aSStream1 << "X = " << std::setw(15) << P.X() << "\n";
128 aSStream1 << "Y = " << std::setw(15) << P.Y() << "\n";
129 aSStream1 << "Z = " << std::setw(15) << P.Z() << "\n";
132 aSStream1 << "Matrix of Inertia : \n";
133 aSStream1 << std::setw(15) << I(1,1);
134 aSStream1 << " " << std::setw(15) << I(1,2);
135 aSStream1 << " " << std::setw(15) << I(1,3) << "\n";
136 aSStream1 << std::setw(15) << I(2,1);
137 aSStream1 << " " << std::setw(15) << I(2,2);
138 aSStream1 << " " << std::setw(15) << I(2,3) << "\n";
139 aSStream1 << std::setw(15) << I(3,1);
140 aSStream1 << " " << std::setw(15) << I(3,2);
141 aSStream1 << " " << std::setw(15) << I(3,3) << "\n";
143 aSStream1 << std::ends;
146 Standard_SStream aSStream2;
147 aSStream2 << "Moments : \n";
148 aSStream2 << "IX = " << std::setw(15) << Ix << "\n";
149 aSStream2 << "IY = " << std::setw(15) << Iy << "\n";
150 aSStream2 << "IZ = " << std::setw(15) << Iz << "\n";
152 aSStream2 << std::ends;
157 di << "\n\nMass : " << G.Mass() << "\n\n";
158 if (witheps && *a[0] != 'l')
160 di << "Relative error of mass computation : " << eps << "\n\n";
163 di << "Center of gravity : \n";
164 di << "X = " << P.X() << "\n";
165 di << "Y = " << P.Y() << "\n";
166 di << "Z = " << P.Z() << "\n\n";
168 di << "Matrix of Inertia :\n";
169 di << I(1,1) << " " << I(1,2) << " " << I(1,3) << "\n";
170 di << I(2,1) << " " << I(2,2) << " " << I(2,3) << "\n";
171 di << I(3,1) << " " << I(3,2) << " " << I(3,3) << "\n\n";
174 di << "IX = " << Ix << "\n";
175 di << "IY = " << Iy << "\n";
176 di << "IZ = " << Iz << "\n\n";
180 gp_Ax2 axes(P,Pr.ThirdAxisOfInertia(),Pr.FirstAxisOfInertia());
182 Handle(Draw_Axis3D) Dax = new Draw_Axis3D(axes,Draw_orange,30);
190 Standard_Integer vpropsgk(Draw_Interpretor& di, Standard_Integer n, const char** a)
193 di << "Use: " << a[0] << " shape epsilon closed span mode [x y z] [-skip]\n";
194 di << "Compute properties of the shape\n";
195 di << "The epsilon defines relative precision of computation\n";
196 di << "The \"closed\" flag, if equal 1, causes computation only closed shells of the shape\n";
197 di << "The \"span\" flag, if equal 1, says that computation is performed on spans\n";
198 di << " This option makes effect only for BSpline surfaces.\n";
199 di << "mode can be 0 - only volume calculations\n";
200 di << " 1 - volume and gravity center\n";
201 di << " 2 - volume, gravity center and matrix of inertia\n";
202 di << "The centroid coordinates will be put to DRAW variables x y z (if given)\n\n";
206 if ( n > 2 && n < 6) {
207 di << "Wrong arguments\n";
211 TopoDS_Shape S = DBRep::Get(a[1]);
212 if (S.IsNull()) return 0;
215 Standard_Boolean SkipShared = Standard_False;
216 if (n >= 2 && strcmp(a[n-1], "-skip") == 0)
218 SkipShared = Standard_True;
222 Standard_Boolean onlyClosed = Standard_False;
223 Standard_Boolean isUseSpan = Standard_False;
224 Standard_Boolean CGFlag = Standard_False;
225 Standard_Boolean IFlag = Standard_False;
226 Standard_Real eps = 1.e-3;
227 //Standard_Real aDefaultTol = 1.e-3;
228 Standard_Integer mode = 0;
230 eps = Draw::Atof(a[2]);
231 mode = Draw::Atoi(a[3]);
232 if(mode > 0) onlyClosed = Standard_True;
233 mode = Draw::Atoi(a[4]);
234 if(mode > 0) isUseSpan = Standard_True;
236 mode = Draw::Atoi(a[5]);
237 if(mode == 1 || mode == 3) CGFlag = Standard_True;
238 if(mode == 2 || mode == 3) IFlag = Standard_True;
240 //OSD_Chronometer aChrono;
244 eps = BRepGProp::VolumePropertiesGK(S, G, eps, onlyClosed, isUseSpan, CGFlag, IFlag, SkipShared);
247 Standard_SStream aSStream0;
248 Standard_Integer anOutWidth = 24;
250 aSStream0.precision(15);
252 aSStream0 << "Mass : " << std::setw(anOutWidth) << G.Mass() << "\n\n";
253 aSStream0 << "Relative error of mass computation : " << std::setw(anOutWidth) << eps << "\n\n";
254 aSStream0 << std::ends;
257 if(CGFlag || IFlag) {
258 Standard_SStream aSStream1;
259 gp_Pnt P = G.CentreOfMass();
261 Draw::Set(a[6],P.X());
264 Draw::Set(a[7],P.Y());
267 Draw::Set(a[8],P.Z());
270 aSStream1.precision(15);
271 aSStream1 << "Center of gravity : \n";
272 aSStream1 << "X = " << std::setw(anOutWidth) << P.X() << "\n";
273 aSStream1 << "Y = " << std::setw(anOutWidth) << P.Y() << "\n";
274 aSStream1 << "Z = " << std::setw(anOutWidth) << P.Z() << "\n";
278 gp_Mat I = G.MatrixOfInertia();
280 aSStream1 << "Matrix of Inertia : \n";
281 aSStream1 << std::setw(anOutWidth) << I(1,1);
282 aSStream1 << " " << std::setw(anOutWidth) << I(1,2);
283 aSStream1 << " " << std::setw(anOutWidth) << I(1,3) << "\n";
284 aSStream1 << std::setw(anOutWidth) << I(2,1);
285 aSStream1 << " " << std::setw(anOutWidth) << I(2,2);
286 aSStream1 << " " << std::setw(anOutWidth) << I(2,3) << "\n";
287 aSStream1 << std::setw(anOutWidth) << I(3,1);
288 aSStream1 << " " << std::setw(anOutWidth) << I(3,2);
289 aSStream1 << " " << std::setw(anOutWidth) << I(3,3) << "\n";
292 aSStream1 << std::ends;
298 GProp_PrincipalProps Pr = G.PrincipalProperties();
300 Standard_Real Ix,Iy,Iz;
301 Pr.Moments(Ix,Iy,Iz);
302 gp_Pnt P = G.CentreOfMass();
304 Standard_SStream aSStream2;
306 aSStream2.precision(15);
307 aSStream2 << "Moments : \n";
308 aSStream2 << "IX = " << std::setw(anOutWidth) << Ix << "\n";
309 aSStream2 << "IY = " << std::setw(anOutWidth) << Iy << "\n";
310 aSStream2 << "IZ = " << std::setw(anOutWidth) << Iz << "\n";
313 aSStream2 << std::ends;
316 gp_Ax2 axes(P,Pr.ThirdAxisOfInertia(),Pr.FirstAxisOfInertia());
318 Handle(Draw_Axis3D) Dax = new Draw_Axis3D(axes,Draw_orange,30);
326 //=======================================================================
327 //function : GPropCommands
329 //=======================================================================
331 void BRepTest::GPropCommands(Draw_Interpretor& theCommands)
333 static Standard_Boolean done = Standard_False;
335 done = Standard_True;
337 DBRep::BasicCommands(theCommands);
339 const char* g = "Global properties";
340 theCommands.Add("lprops",
341 "lprops name [x y z] [-skip] [-full] [-tri]: compute linear properties",
343 theCommands.Add("sprops", "sprops name [epsilon] [x y z] [-skip] [-full] [-tri]:\n"
344 " compute surfacic properties", __FILE__, props, g);
345 theCommands.Add("vprops", "vprops name [epsilon] [c[losed]] [x y z] [-skip] [-full] [-tri]:\n"
346 " compute volumic properties", __FILE__, props, g);
348 theCommands.Add("vpropsgk",
349 "vpropsgk name epsilon closed span mode [x y z] [-skip] : compute volumic properties",