0022898: IGES import fails in german environment
[occt.git] / src / BRepTest / BRepTest_GPropCommands.cxx
CommitLineData
b311480e 1// Created on: 1994-02-18
2// Created by: Remi LEQUETTE
3// Copyright (c) 1994-1999 Matra Datavision
4// Copyright (c) 1999-2012 OPEN CASCADE SAS
5//
6// The content of this file is subject to the Open CASCADE Technology Public
7// License Version 6.5 (the "License"). You may not use the content of this file
8// except in compliance with the License. Please obtain a copy of the License
9// at http://www.opencascade.org and read it completely before using this file.
10//
11// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13//
14// The Original Code and all software distributed under the License is
15// distributed on an "AS IS" basis, without warranty of any kind, and the
16// Initial Developer hereby disclaims all such warranties, including without
17// limitation, any warranties of merchantability, fitness for a particular
18// purpose or non-infringement. Please see the License for the specific terms
19// and conditions governing the rights and limitations under the License.
20
7fd59977 21
22#include <Standard_Stream.hxx>
23#include <BRepTest.hxx>
24#include <Draw_Interpretor.hxx>
25#include <Draw_Appli.hxx>
26#include <DBRep.hxx>
27#include <BRepGProp.hxx>
28#include <TopoDS_Shape.hxx>
29#include <GProp_PrincipalProps.hxx>
30#include <gp_Ax2.hxx>
31#include <gp_Ax3.hxx>
32
33#include <Draw_Axis3D.hxx>
34#include <Precision.hxx>
35#include <OSD_Chronometer.hxx>
36#include <Geom_Surface.hxx>
37#include <DrawTrSurf.hxx>
38#include <Geom_Plane.hxx>
39#include <gp_Pln.hxx>
40
41#ifdef WNT
42Standard_IMPORT Draw_Viewer dout;
43#endif
44
45
46Standard_Integer props(Draw_Interpretor& di, Standard_Integer n, const char** a)
47{
48 if (n < 2) {
49 di << "Use: " << a[0] << " shape [epsilon] [c[losed]] [x y z]" << "\n";
50 di << "Compute properties of the shape" << "\n";
51 di << "The epsilon, if given, defines relative precision of computation" << "\n";
52 di << "The \"closed\" flag, if present, do computation only closed shells of the shape" << "\n";
53 di << "The centroid coordinates will be put to DRAW variables x y z (if given)\n" << "\n";
54 return 1;
55 }
56
57 TopoDS_Shape S = DBRep::Get(a[1]);
58 if (S.IsNull()) return 0;
59
60 GProp_GProps G;
61
62 Standard_Boolean onlyClosed = Standard_False;
63 Standard_Real eps = 1.0;
64 Standard_Boolean witheps = Standard_False;
65 if(n > 2 && *a[2]=='c' || n > 3 && *a[3]=='c') onlyClosed = Standard_True;
91322f44 66 if(n > 2 && *a[2]!='c' && n != 5) {eps = Draw::Atof (a[2]); witheps = Standard_True;}
7fd59977 67
68 if (witheps){
69 if (Abs(eps) < Precision::Angular()) return 2;
70 if (*a[0] == 'l')
71 BRepGProp::LinearProperties(S,G);
72 else if (*a[0] == 's')
73 eps = BRepGProp::SurfaceProperties(S,G,eps);
74 else
75 eps = BRepGProp::VolumeProperties(S,G,eps,onlyClosed);
76 }
77 else {
78 if (*a[0] == 'l')
79 BRepGProp::LinearProperties(S,G);
80 else if (*a[0] == 's')
81 BRepGProp::SurfaceProperties(S,G);
82 else
83 BRepGProp::VolumeProperties(S,G,onlyClosed);
84 }
85
86 gp_Pnt P = G.CentreOfMass();
87 gp_Mat I = G.MatrixOfInertia();
88
89 if (n >= 5) {
90 Standard_Integer shift = n - 5;
91 Draw::Set(a[shift+2],P.X());
92 Draw::Set(a[shift+3],P.Y());
93 Draw::Set(a[shift+4],P.Z());
94 }
95
96 Standard_SStream aSStream1;
97 aSStream1 << "\n\n";
98 aSStream1 << "Mass : " << setw(15) << G.Mass() << "\n" << "\n";
99 if(witheps && *a[0] != 'l') aSStream1 << "Relative error of mass computation : " << setw(15) << eps << "\n" << "\n";
100
101 aSStream1 << "Center of gravity : \n";
102 aSStream1 << "X = " << setw(15) << P.X() << "\n";
103 aSStream1 << "Y = " << setw(15) << P.Y() << "\n";
104 aSStream1 << "Z = " << setw(15) << P.Z() << "\n";
105 aSStream1 << "\n";
106
107 aSStream1 << "Matrix of Inertia : \n";
108 aSStream1 << setw(15) << I(1,1);
109 aSStream1 << " " << setw(15) << I(1,2);
110 aSStream1 << " " << setw(15) << I(1,3) << "\n";
111 aSStream1 << setw(15) << I(2,1);
112 aSStream1 << " " << setw(15) << I(2,2);
113 aSStream1 << " " << setw(15) << I(2,3) << "\n";
114 aSStream1 << setw(15) << I(3,1);
115 aSStream1 << " " << setw(15) << I(3,2);
116 aSStream1 << " " << setw(15) << I(3,3) << "\n";
117 aSStream1 << "\n";
118 aSStream1 << ends;
119 di << aSStream1;
120
121 GProp_PrincipalProps Pr = G.PrincipalProperties();
122
123 Standard_Real Ix,Iy,Iz;
124 Pr.Moments(Ix,Iy,Iz);
125
126 Standard_SStream aSStream2;
127 aSStream2 << "Moments : \n";
128 aSStream2 << "IX = " << setw(15) << Ix << "\n";
129 aSStream2 << "IY = " << setw(15) << Iy << "\n";
130 aSStream2 << "IZ = " << setw(15) << Iz << "\n";
131 aSStream2 << "\n";
132 aSStream2 << ends;
133 di << aSStream2;
134
135 //if (n == 2) {
136 gp_Ax2 axes(P,Pr.ThirdAxisOfInertia(),Pr.FirstAxisOfInertia());
137
138 Handle(Draw_Axis3D) Dax = new Draw_Axis3D(axes,Draw_orange,30);
139 dout << Dax;
140 //}
141
142 return 0;
143}
144
145
146Standard_Integer vpropsgk(Draw_Interpretor& di, Standard_Integer n, const char** a)
147{
148 if (n < 2) {
149 di << "Use: " << a[0] << " shape epsilon closed span mode [x y z]" << "\n";
150 di << "Compute properties of the shape" << "\n";
151 di << "The epsilon defines relative precision of computation" << "\n";
152 di << "The \"closed\" flag, if equal 1, causes computation only closed shells of the shape" << "\n";
153 di << "The \"span\" flag, if equal 1, says that computation is performed on spans" << "\n";
154 di << " This option makes effect only for BSpline surfaces." << "\n";
155 di << "mode can be 0 - only volume calculations" << "\n";
156 di << " 1 - volume and gravity center" << "\n";
157 di << " 2 - volume, gravity center and matrix of inertia" << "\n";
158 di << "The centroid coordinates will be put to DRAW variables x y z (if given)\n" << "\n";
159 return 1;
160 }
161
162 if ( n > 2 && n < 6) {
163 di << "Wrong arguments" << "\n";
164 return 1;
165 }
166
167 TopoDS_Shape S = DBRep::Get(a[1]);
168 if (S.IsNull()) return 0;
169
170 GProp_GProps G;
171
172 Standard_Boolean onlyClosed = Standard_False;
173 Standard_Boolean isUseSpan = Standard_False;
174 Standard_Boolean CGFlag = Standard_False;
175 Standard_Boolean IFlag = Standard_False;
176 Standard_Real eps = 1.e-3;
177//Standard_Real aDefaultTol = 1.e-3;
178 Standard_Integer mode = 0;
179
91322f44 180 eps = Draw::Atof(a[2]);
181 mode = Draw::Atoi(a[3]);
7fd59977 182 if(mode > 0) onlyClosed = Standard_True;
91322f44 183 mode = Draw::Atoi(a[4]);
7fd59977 184 if(mode > 0) isUseSpan = Standard_True;
185
91322f44 186 mode = Draw::Atoi(a[5]);
7fd59977 187 if(mode == 1 || mode == 3) CGFlag = Standard_True;
188 if(mode == 2 || mode == 3) IFlag = Standard_True;
189
190 //OSD_Chronometer aChrono;
191
192 //aChrono.Reset();
193 //aChrono.Start();
194 eps = BRepGProp::VolumePropertiesGK(S, G, eps, onlyClosed, isUseSpan, CGFlag, IFlag);
195 //aChrono.Stop();
196
197 Standard_SStream aSStream0;
198 Standard_Integer anOutWidth = 24;
199
200 aSStream0.precision(15);
201 aSStream0 << "\n\n";
202 aSStream0 << "Mass : " << setw(anOutWidth) << G.Mass() << "\n" << "\n";
203 aSStream0 << "Relative error of mass computation : " << setw(anOutWidth) << eps << "\n" << "\n";
204 aSStream0 << ends;
205 di << aSStream0;
206
207 if(CGFlag || IFlag) {
208 Standard_SStream aSStream1;
209 gp_Pnt P = G.CentreOfMass();
210 if (n > 6) {
211 Draw::Set(a[6],P.X());
212 }
213 if (n > 7) {
214 Draw::Set(a[7],P.Y());
215 }
216 if (n > 8) {
217 Draw::Set(a[8],P.Z());
218 }
219
220 aSStream1.precision(15);
221 aSStream1 << "Center of gravity : \n";
222 aSStream1 << "X = " << setw(anOutWidth) << P.X() << "\n";
223 aSStream1 << "Y = " << setw(anOutWidth) << P.Y() << "\n";
224 aSStream1 << "Z = " << setw(anOutWidth) << P.Z() << "\n";
225 aSStream1 << "\n";
226
227 if(IFlag) {
228 gp_Mat I = G.MatrixOfInertia();
229
230 aSStream1 << "Matrix of Inertia : \n";
231 aSStream1 << setw(anOutWidth) << I(1,1);
232 aSStream1 << " " << setw(anOutWidth) << I(1,2);
233 aSStream1 << " " << setw(anOutWidth) << I(1,3) << "\n";
234 aSStream1 << setw(anOutWidth) << I(2,1);
235 aSStream1 << " " << setw(anOutWidth) << I(2,2);
236 aSStream1 << " " << setw(anOutWidth) << I(2,3) << "\n";
237 aSStream1 << setw(anOutWidth) << I(3,1);
238 aSStream1 << " " << setw(anOutWidth) << I(3,2);
239 aSStream1 << " " << setw(anOutWidth) << I(3,3) << "\n";
240 aSStream1 << "\n";
241 }
242 aSStream1 << ends;
243 di << aSStream1;
244 }
245
246 if(IFlag) {
247
248 GProp_PrincipalProps Pr = G.PrincipalProperties();
249
250 Standard_Real Ix,Iy,Iz;
251 Pr.Moments(Ix,Iy,Iz);
252 gp_Pnt P = G.CentreOfMass();
253
254 Standard_SStream aSStream2;
255
256 aSStream2.precision(15);
257 aSStream2 << "Moments : \n";
258 aSStream2 << "IX = " << setw(anOutWidth) << Ix << "\n";
259 aSStream2 << "IY = " << setw(anOutWidth) << Iy << "\n";
260 aSStream2 << "IZ = " << setw(anOutWidth) << Iz << "\n";
261 aSStream2 << "\n";
262 aSStream2 << "\n";
263 aSStream2 << ends;
264 di << aSStream2;
265
266 gp_Ax2 axes(P,Pr.ThirdAxisOfInertia(),Pr.FirstAxisOfInertia());
267
268 Handle(Draw_Axis3D) Dax = new Draw_Axis3D(axes,Draw_orange,30);
269 dout << Dax;
270 }
271 return 0;
272}
273
274
275//=======================================================================
276//function : GPropCommands
277//purpose :
278//=======================================================================
279
280void BRepTest::GPropCommands(Draw_Interpretor& theCommands)
281{
282 static Standard_Boolean done = Standard_False;
283 if (done) return;
284 done = Standard_True;
285
286 DBRep::BasicCommands(theCommands);
287
288 const char* g = "Global properties";
289 theCommands.Add("lprops",
290 "lprops name [epsilon] [x y z] : compute linear properties",
291 __FILE__,
292 props,
293 g);
294 theCommands.Add("sprops",
295 "sprops name [epsilon] [x y z] : compute surfacic properties",
296 __FILE__,
297 props,
298 g);
299 theCommands.Add("vprops",
300 "vprops name [epsilon] [c[losed]] [x y z] : compute volumic properties",
301 __FILE__,
302 props,
303 g);
304
305 theCommands.Add("vpropsgk",
306 "vpropsgk name epsilon closed span mode [x y z] : compute volumic properties",
307 __FILE__,
308 vpropsgk,
309 g);
310}