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] [-full]\n";
45 di << "Compute properties of the shape\n";
46 di << "The epsilon, if given, defines relative precision of computation\n";
47 di << "The \"closed\" flag, if present, do computation only closed shells of the shape\n";
48 di << "The centroid coordinates will be put to DRAW variables x y z (if given)\n";
49 di << "All values are outputted with the full precision in the full mode.\n\n";
53 Standard_Boolean isFullMode = Standard_False;
54 if (n >= 2 && strcmp(a[n-1], "-full") == 0)
56 isFullMode = Standard_True;
60 TopoDS_Shape S = DBRep::Get(a[1]);
61 if (S.IsNull()) return 0;
65 Standard_Boolean onlyClosed = Standard_False;
66 Standard_Real eps = 1.0;
67 Standard_Boolean witheps = Standard_False;
68 if((n > 2 && *a[2]=='c') || (n > 3 && *a[3]=='c')) onlyClosed = Standard_True;
69 if(n > 2 && *a[2]!='c' && n != 5) {eps = Draw::Atof (a[2]); witheps = Standard_True;}
72 if (Abs(eps) < Precision::Angular()) return 2;
74 BRepGProp::LinearProperties(S,G);
75 else if (*a[0] == 's')
76 eps = BRepGProp::SurfaceProperties(S,G,eps);
78 eps = BRepGProp::VolumeProperties(S,G,eps,onlyClosed);
82 BRepGProp::LinearProperties(S,G);
83 else if (*a[0] == 's')
84 BRepGProp::SurfaceProperties(S,G);
86 BRepGProp::VolumeProperties(S,G,onlyClosed);
89 gp_Pnt P = G.CentreOfMass();
90 gp_Mat I = G.MatrixOfInertia();
93 Standard_Integer shift = n - 5;
94 Draw::Set(a[shift+2],P.X());
95 Draw::Set(a[shift+3],P.Y());
96 Draw::Set(a[shift+4],P.Z());
99 GProp_PrincipalProps Pr = G.PrincipalProperties();
100 Standard_Real Ix,Iy,Iz;
101 Pr.Moments(Ix,Iy,Iz);
105 Standard_SStream aSStream1;
107 aSStream1 << "Mass : " << setw(15) << G.Mass() << "\n\n";
108 if(witheps && *a[0] != 'l') aSStream1 << "Relative error of mass computation : " << setw(15) << eps << "\n\n";
110 aSStream1 << "Center of gravity : \n";
111 aSStream1 << "X = " << setw(15) << P.X() << "\n";
112 aSStream1 << "Y = " << setw(15) << P.Y() << "\n";
113 aSStream1 << "Z = " << setw(15) << P.Z() << "\n";
116 aSStream1 << "Matrix of Inertia : \n";
117 aSStream1 << setw(15) << I(1,1);
118 aSStream1 << " " << setw(15) << I(1,2);
119 aSStream1 << " " << setw(15) << I(1,3) << "\n";
120 aSStream1 << setw(15) << I(2,1);
121 aSStream1 << " " << setw(15) << I(2,2);
122 aSStream1 << " " << setw(15) << I(2,3) << "\n";
123 aSStream1 << setw(15) << I(3,1);
124 aSStream1 << " " << setw(15) << I(3,2);
125 aSStream1 << " " << setw(15) << I(3,3) << "\n";
130 Standard_SStream aSStream2;
131 aSStream2 << "Moments : \n";
132 aSStream2 << "IX = " << setw(15) << Ix << "\n";
133 aSStream2 << "IY = " << setw(15) << Iy << "\n";
134 aSStream2 << "IZ = " << setw(15) << Iz << "\n";
141 di << "\n\nMass : " << G.Mass() << "\n\n";
142 if (witheps && *a[0] != 'l')
144 di << "Relative error of mass computation : " << eps << "\n\n";
147 di << "Center of gravity : \n";
148 di << "X = " << P.X() << "\n";
149 di << "Y = " << P.Y() << "\n";
150 di << "Z = " << P.Z() << "\n\n";
152 di << "Matrix of Inertia :\n";
153 di << I(1,1) << " " << I(1,2) << " " << I(1,3) << "\n";
154 di << I(2,1) << " " << I(2,2) << " " << I(2,3) << "\n";
155 di << I(3,1) << " " << I(3,2) << " " << I(3,3) << "\n\n";
158 di << "IX = " << Ix << "\n";
159 di << "IY = " << Iy << "\n";
160 di << "IZ = " << Iz << "\n\n";
164 gp_Ax2 axes(P,Pr.ThirdAxisOfInertia(),Pr.FirstAxisOfInertia());
166 Handle(Draw_Axis3D) Dax = new Draw_Axis3D(axes,Draw_orange,30);
174 Standard_Integer vpropsgk(Draw_Interpretor& di, Standard_Integer n, const char** a)
177 di << "Use: " << a[0] << " shape epsilon closed span mode [x y z]\n";
178 di << "Compute properties of the shape\n";
179 di << "The epsilon defines relative precision of computation\n";
180 di << "The \"closed\" flag, if equal 1, causes computation only closed shells of the shape\n";
181 di << "The \"span\" flag, if equal 1, says that computation is performed on spans\n";
182 di << " This option makes effect only for BSpline surfaces.\n";
183 di << "mode can be 0 - only volume calculations\n";
184 di << " 1 - volume and gravity center\n";
185 di << " 2 - volume, gravity center and matrix of inertia\n";
186 di << "The centroid coordinates will be put to DRAW variables x y z (if given)\n\n";
190 if ( n > 2 && n < 6) {
191 di << "Wrong arguments\n";
195 TopoDS_Shape S = DBRep::Get(a[1]);
196 if (S.IsNull()) return 0;
200 Standard_Boolean onlyClosed = Standard_False;
201 Standard_Boolean isUseSpan = Standard_False;
202 Standard_Boolean CGFlag = Standard_False;
203 Standard_Boolean IFlag = Standard_False;
204 Standard_Real eps = 1.e-3;
205 //Standard_Real aDefaultTol = 1.e-3;
206 Standard_Integer mode = 0;
208 eps = Draw::Atof(a[2]);
209 mode = Draw::Atoi(a[3]);
210 if(mode > 0) onlyClosed = Standard_True;
211 mode = Draw::Atoi(a[4]);
212 if(mode > 0) isUseSpan = Standard_True;
214 mode = Draw::Atoi(a[5]);
215 if(mode == 1 || mode == 3) CGFlag = Standard_True;
216 if(mode == 2 || mode == 3) IFlag = Standard_True;
218 //OSD_Chronometer aChrono;
222 eps = BRepGProp::VolumePropertiesGK(S, G, eps, onlyClosed, isUseSpan, CGFlag, IFlag);
225 Standard_SStream aSStream0;
226 Standard_Integer anOutWidth = 24;
228 aSStream0.precision(15);
230 aSStream0 << "Mass : " << setw(anOutWidth) << G.Mass() << "\n\n";
231 aSStream0 << "Relative error of mass computation : " << setw(anOutWidth) << eps << "\n\n";
235 if(CGFlag || IFlag) {
236 Standard_SStream aSStream1;
237 gp_Pnt P = G.CentreOfMass();
239 Draw::Set(a[6],P.X());
242 Draw::Set(a[7],P.Y());
245 Draw::Set(a[8],P.Z());
248 aSStream1.precision(15);
249 aSStream1 << "Center of gravity : \n";
250 aSStream1 << "X = " << setw(anOutWidth) << P.X() << "\n";
251 aSStream1 << "Y = " << setw(anOutWidth) << P.Y() << "\n";
252 aSStream1 << "Z = " << setw(anOutWidth) << P.Z() << "\n";
256 gp_Mat I = G.MatrixOfInertia();
258 aSStream1 << "Matrix of Inertia : \n";
259 aSStream1 << setw(anOutWidth) << I(1,1);
260 aSStream1 << " " << setw(anOutWidth) << I(1,2);
261 aSStream1 << " " << setw(anOutWidth) << I(1,3) << "\n";
262 aSStream1 << setw(anOutWidth) << I(2,1);
263 aSStream1 << " " << setw(anOutWidth) << I(2,2);
264 aSStream1 << " " << setw(anOutWidth) << I(2,3) << "\n";
265 aSStream1 << setw(anOutWidth) << I(3,1);
266 aSStream1 << " " << setw(anOutWidth) << I(3,2);
267 aSStream1 << " " << setw(anOutWidth) << I(3,3) << "\n";
276 GProp_PrincipalProps Pr = G.PrincipalProperties();
278 Standard_Real Ix,Iy,Iz;
279 Pr.Moments(Ix,Iy,Iz);
280 gp_Pnt P = G.CentreOfMass();
282 Standard_SStream aSStream2;
284 aSStream2.precision(15);
285 aSStream2 << "Moments : \n";
286 aSStream2 << "IX = " << setw(anOutWidth) << Ix << "\n";
287 aSStream2 << "IY = " << setw(anOutWidth) << Iy << "\n";
288 aSStream2 << "IZ = " << setw(anOutWidth) << Iz << "\n";
294 gp_Ax2 axes(P,Pr.ThirdAxisOfInertia(),Pr.FirstAxisOfInertia());
296 Handle(Draw_Axis3D) Dax = new Draw_Axis3D(axes,Draw_orange,30);
303 //=======================================================================
304 //function : GPropCommands
306 //=======================================================================
308 void BRepTest::GPropCommands(Draw_Interpretor& theCommands)
310 static Standard_Boolean done = Standard_False;
312 done = Standard_True;
314 DBRep::BasicCommands(theCommands);
316 const char* g = "Global properties";
317 theCommands.Add("lprops",
318 "lprops name [x y z] [-full] : compute linear properties",
320 theCommands.Add("sprops", "sprops name [epsilon] [x y z] [-full] :\n"
321 " compute surfacic properties", __FILE__, props, g);
322 theCommands.Add("vprops", "vprops name [epsilon] [c[losed]] [x y z] [-full] :\n"
323 " compute volumic properties", __FILE__, props, g);
325 theCommands.Add("vpropsgk",
326 "vpropsgk name epsilon closed span mode [x y z] : compute volumic properties",