summaryrefslogtreecommitdiff
path: root/src/BRepTest/BRepTest_GPropCommands.cxx
blob: 40da043e016e12bf122d46de5ed148e98cfdcc16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// File:	DBRep_8.cxx
// Created:	Fri Feb 18 17:24:52 1994
// Author:	Remi LEQUETTE
//		<rle@nonox>

#include <Standard_Stream.hxx>
#include <BRepTest.hxx>
#include <Draw_Interpretor.hxx>
#include <Draw_Appli.hxx>
#include <DBRep.hxx>
#include <BRepGProp.hxx>
#include <TopoDS_Shape.hxx>
#include <GProp_PrincipalProps.hxx>
#include <gp_Ax2.hxx>
#include <gp_Ax3.hxx>

#include <Draw_Axis3D.hxx>
#include <Precision.hxx>
#include <OSD_Chronometer.hxx>
#include <Geom_Surface.hxx>
#include <DrawTrSurf.hxx>
#include <Geom_Plane.hxx>
#include <gp_Pln.hxx>

#ifdef WNT
Standard_IMPORT Draw_Viewer dout;
#endif


Standard_Integer props(Draw_Interpretor& di, Standard_Integer n, const char** a)
{
  if (n < 2) {
    di << "Use: " << a[0] << " shape [epsilon] [c[losed]] [x y z]" << "\n";
    di << "Compute properties of the shape" << "\n";
    di << "The epsilon, if given, defines relative precision of computation" << "\n";
    di << "The \"closed\" flag, if present, do computation only closed shells of the shape" << "\n";
    di << "The centroid coordinates will be put to DRAW variables x y z (if given)\n" << "\n";
    return 1;
  }

  TopoDS_Shape S = DBRep::Get(a[1]);
  if (S.IsNull()) return 0;

  GProp_GProps G;

  Standard_Boolean onlyClosed = Standard_False;
  Standard_Real eps = 1.0;
  Standard_Boolean witheps = Standard_False;
  if((n > 2 && *a[2]=='c') || (n > 3 && *a[3]=='c')) onlyClosed = Standard_True;
  if(n > 2 && *a[2]!='c' && n != 5) {eps = atof (a[2]); witheps = Standard_True;}

  if (witheps){
    if (Abs(eps) < Precision::Angular()) return 2;
    if (*a[0] == 'l')
      BRepGProp::LinearProperties(S,G);
    else if (*a[0] == 's')
      eps = BRepGProp::SurfaceProperties(S,G,eps);
    else 
      eps = BRepGProp::VolumeProperties(S,G,eps,onlyClosed);
  }
  else {
    if (*a[0] == 'l')
      BRepGProp::LinearProperties(S,G);
    else if (*a[0] == 's')
      BRepGProp::SurfaceProperties(S,G);
    else 
      BRepGProp::VolumeProperties(S,G,onlyClosed);
  }
  
  gp_Pnt P = G.CentreOfMass();
  gp_Mat I = G.MatrixOfInertia();

  if (n >= 5) {
    Standard_Integer shift =  n - 5;
    Draw::Set(a[shift+2],P.X());
    Draw::Set(a[shift+3],P.Y());
    Draw::Set(a[shift+4],P.Z());
  }
	    
  Standard_SStream aSStream1;
  aSStream1 << "\n\n";
  aSStream1 << "Mass : " << setw(15) << G.Mass() << "\n" << "\n";
  if(witheps && *a[0] != 'l') aSStream1 << "Relative error of mass computation : " <<  setw(15) << eps <<  "\n" << "\n";
  
  aSStream1 << "Center of gravity : \n";
  aSStream1 << "X = " << setw(15) << P.X() << "\n";
  aSStream1 << "Y = " << setw(15) << P.Y() << "\n";
  aSStream1 << "Z = " << setw(15) << P.Z() << "\n";
  aSStream1 << "\n";
  
  aSStream1 << "Matrix of Inertia : \n";
  aSStream1 << setw(15) << I(1,1);
  aSStream1 << " " << setw(15) << I(1,2);
  aSStream1 << " " << setw(15) << I(1,3) << "\n";
  aSStream1 << setw(15) << I(2,1);
  aSStream1 << " " << setw(15) << I(2,2);
  aSStream1 << " " << setw(15) << I(2,3) << "\n";
  aSStream1 << setw(15) << I(3,1);
  aSStream1 << " " << setw(15) << I(3,2);
  aSStream1 << " " << setw(15) << I(3,3) << "\n";
  aSStream1 << "\n";
  aSStream1 << ends;
  di << aSStream1;

  GProp_PrincipalProps Pr = G.PrincipalProperties();

  Standard_Real Ix,Iy,Iz;
  Pr.Moments(Ix,Iy,Iz);
  
  Standard_SStream aSStream2;
  aSStream2 << "Moments : \n";
  aSStream2 << "IX = " << setw(15) << Ix << "\n";
  aSStream2 << "IY = " << setw(15) << Iy << "\n";
  aSStream2 << "IZ = " << setw(15) << Iz << "\n";
  aSStream2 << "\n";
  aSStream2 << ends;
  di << aSStream2;

  //if (n == 2) {  
    gp_Ax2 axes(P,Pr.ThirdAxisOfInertia(),Pr.FirstAxisOfInertia());
    
    Handle(Draw_Axis3D) Dax = new Draw_Axis3D(axes,Draw_orange,30);
    dout << Dax;
  //}

  return 0;
}


Standard_Integer vpropsgk(Draw_Interpretor& di, Standard_Integer n, const char** a)
{
  if (n < 2) {
    di << "Use: " << a[0] << " shape epsilon closed span mode [x y z]" << "\n";
    di << "Compute properties of the shape" << "\n";
    di << "The epsilon defines relative precision of computation" << "\n";
    di << "The \"closed\" flag, if equal 1, causes computation only closed shells of the shape" << "\n";
    di << "The \"span\" flag, if equal 1, says that computation is performed on spans" << "\n";
    di << "      This option makes effect only for BSpline surfaces." << "\n";
    di << "mode can be 0 - only volume calculations" << "\n";
    di << "            1 - volume and gravity center" << "\n";
    di << "            2 - volume, gravity center and matrix of inertia" << "\n";
    di << "The centroid coordinates will be put to DRAW variables x y z (if given)\n" << "\n";
    return 1;
  }

  if ( n > 2 && n < 6) {
    di << "Wrong arguments" << "\n";
    return 1;
  }

  TopoDS_Shape S = DBRep::Get(a[1]);
  if (S.IsNull()) return 0;

  GProp_GProps G;

  Standard_Boolean onlyClosed  = Standard_False;
  Standard_Boolean isUseSpan   = Standard_False;
  Standard_Boolean CGFlag = Standard_False;
  Standard_Boolean IFlag = Standard_False;
  Standard_Real    eps         = 1.e-3;
//Standard_Real    aDefaultTol = 1.e-3;
  Standard_Integer mode = 0;

  eps = atof(a[2]);
  mode = atoi(a[3]);
  if(mode > 0) onlyClosed = Standard_True;
  mode = atoi(a[4]);
  if(mode > 0) isUseSpan = Standard_True;

  mode = atoi(a[5]);
  if(mode == 1 || mode == 3) CGFlag = Standard_True;
  if(mode == 2 || mode == 3) IFlag = Standard_True;

  //OSD_Chronometer aChrono;

  //aChrono.Reset();
  //aChrono.Start();
  eps = BRepGProp::VolumePropertiesGK(S, G, eps, onlyClosed, isUseSpan, CGFlag, IFlag);
  //aChrono.Stop();

  Standard_SStream aSStream0;
  Standard_Integer anOutWidth = 24;

  aSStream0.precision(15);
  aSStream0 << "\n\n";
  aSStream0 << "Mass : " << setw(anOutWidth) << G.Mass() << "\n" << "\n";
  aSStream0 << "Relative error of mass computation : " <<  setw(anOutWidth) << eps <<  "\n" << "\n";
  aSStream0 << ends;
  di << aSStream0;

  if(CGFlag || IFlag) {
    Standard_SStream aSStream1;
    gp_Pnt P = G.CentreOfMass();
    if (n > 6) {
      Draw::Set(a[6],P.X());
    }
    if (n > 7) {
      Draw::Set(a[7],P.Y());
    }
    if (n > 8) {
      Draw::Set(a[8],P.Z());
    }
  
    aSStream1.precision(15);
    aSStream1 << "Center of gravity : \n";
    aSStream1 << "X = " << setw(anOutWidth) << P.X() << "\n";
    aSStream1 << "Y = " << setw(anOutWidth) << P.Y() << "\n";
    aSStream1 << "Z = " << setw(anOutWidth) << P.Z() << "\n";
    aSStream1 << "\n";
 
    if(IFlag) {
      gp_Mat I = G.MatrixOfInertia();

      aSStream1 << "Matrix of Inertia : \n";
      aSStream1 << setw(anOutWidth) << I(1,1);
      aSStream1 << " " << setw(anOutWidth) << I(1,2);
      aSStream1 << " " << setw(anOutWidth) << I(1,3) << "\n";
      aSStream1 << setw(anOutWidth) << I(2,1);
      aSStream1 << " " << setw(anOutWidth) << I(2,2);
      aSStream1 << " " << setw(anOutWidth) << I(2,3) << "\n";
      aSStream1 << setw(anOutWidth) << I(3,1);
      aSStream1 << " " << setw(anOutWidth) << I(3,2);
      aSStream1 << " " << setw(anOutWidth) << I(3,3) << "\n";
      aSStream1 << "\n";
    }
    aSStream1 << ends;
    di << aSStream1;
  }

  if(IFlag) {

    GProp_PrincipalProps Pr = G.PrincipalProperties();

    Standard_Real Ix,Iy,Iz;
    Pr.Moments(Ix,Iy,Iz);
    gp_Pnt P = G.CentreOfMass();
  
    Standard_SStream aSStream2;

    aSStream2.precision(15);
    aSStream2 << "Moments : \n";
    aSStream2 << "IX = " << setw(anOutWidth) << Ix << "\n";
    aSStream2 << "IY = " << setw(anOutWidth) << Iy << "\n";
    aSStream2 << "IZ = " << setw(anOutWidth) << Iz << "\n";
    aSStream2 << "\n";
    aSStream2 << "\n";
    aSStream2 << ends;
    di << aSStream2;

    gp_Ax2 axes(P,Pr.ThirdAxisOfInertia(),Pr.FirstAxisOfInertia());
    
    Handle(Draw_Axis3D) Dax = new Draw_Axis3D(axes,Draw_orange,30);
    dout << Dax;
  }
  return 0;
}


//=======================================================================
//function : GPropCommands
//purpose  : 
//=======================================================================

void  BRepTest::GPropCommands(Draw_Interpretor& theCommands)
{
  static Standard_Boolean done = Standard_False;
  if (done) return;
  done = Standard_True;

  DBRep::BasicCommands(theCommands);

  const char* g = "Global properties";
  theCommands.Add("lprops",
		  "lprops name [epsilon] [x y z] : compute linear properties",
		  __FILE__,
		  props,
		  g);
  theCommands.Add("sprops",
		  "sprops name [epsilon] [x y z] : compute surfacic properties",
		  __FILE__,
		  props,
		  g);
  theCommands.Add("vprops",
		  "vprops name [epsilon] [c[losed]] [x y z] : compute volumic properties",
		  __FILE__,
		  props,
		  g);

  theCommands.Add("vpropsgk",
		  "vpropsgk name epsilon closed span mode [x y z] : compute volumic properties",
		  __FILE__,
		  vpropsgk,
		  g);
}