summaryrefslogtreecommitdiff
path: root/src/XDEDRAW/XDEDRAW_Props.cxx
blob: 5f4053d591f334177ce5c9346e03ca5fc912494e (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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
// File:	XDEDRAW_Props.cxx
// Created:	Fri Aug  4 14:38:55 2000
// Author:	Pavel TELKOV
//		<ptv@zamox.nnov.matra-dtv.fr>
// OCC532 sln 24.07.2002. Add epsilon parameter to SetProps function

#include <XDEDRAW_Props.ixx>

#include <DBRep.hxx>
#include <DDocStd.hxx>

#include <TCollection_AsciiString.hxx>
#include <TopoDS_Shape.hxx>

#include <GProp_GProps.hxx>
#include <BRepGProp.hxx>

#include <TDF_Tool.hxx>
#include <TDF_Label.hxx>
#include <TDF_LabelSequence.hxx>
#include <TDocStd_Document.hxx>
#include <TDataStd_Name.hxx>

#include <XCAFDoc_DocumentTool.hxx>
#include <XCAFDoc_ShapeTool.hxx>
#include <XCAFDoc_Area.hxx>
#include <XCAFDoc_Volume.hxx>
#include <XCAFDoc_Centroid.hxx>
#include <XCAFDoc_MaterialTool.hxx>
#include <XCAFDoc_Location.hxx>
#include <Precision.hxx>
#include <TNaming_NamedShape.hxx>

#include <Standard_ErrorHandler.hxx>
#include <Standard_Failure.hxx>
#include <gp_Pln.hxx>
#include <BRepMesh.hxx>
#include <TColgp_Array1OfPnt.hxx>
#include <Poly_Triangulation.hxx>
#include <TopoDS_Compound.hxx>
#include <TopExp_Explorer.hxx>
#include <TopoDS_Face.hxx>
#include <TopoDS.hxx>
#include <BRep_Tool.hxx>
#include <BRepMesh_IncrementalMesh.hxx>
#include <TopoDS_Vertex.hxx>
#include <Standard_Stream.hxx>
#include <stdio.h>
#include <TColgp_Array1OfXYZ.hxx>
#include <TColStd_Array1OfReal.hxx>
#include <TDataStd_TreeNode.hxx>
#include <XCAFDoc.hxx>
#include <TCollection_HAsciiString.hxx>


// --------------------- VolumeFix Begin ---

//=======================================================================
//function : TetraVol
//purpose  : auxilary
//=======================================================================
static double TetraVol(gp_Pnt RefPoint, gp_Pnt Som1, gp_Pnt Som2, gp_Pnt Som3)
{
  double curVolume = 0;
  gp_Dir Line12;
  gp_Pln Plane123;
  gp_Vec N;
  
  {
    try
      {
      OCC_CATCH_SIGNALS
	Line12=gp_Dir( gp_Vec(Som1, Som2));
	gp_Vec v1(Som1, Som2);
	gp_Vec v2(Som2, Som3);
	N=v1^v2;
	Plane123=gp_Pln( Som1, gp_Dir( N ) );
      }
    catch(Standard_Failure) {return(0.);}
  }
  double L1, L2, L3;
  L1 = Som1.Distance(Som2);
  L2 = gp_Lin(Som1, Line12).Distance(Som3);
  L3 = Plane123.Distance(RefPoint);
  
  curVolume = ((L1 * L2)/2) * (L3/3);
  
  gp_Vec Rad(RefPoint, Som1);
  
  if( (Rad*N)>0 )
    return (curVolume);
  else
    return (-curVolume);
}


//=======================================================================
//function : TetraCen
//purpose  : auxilary
//=======================================================================
static gp_XYZ TetraCen(gp_Pnt RefPoint, gp_Pnt Som1, gp_Pnt Som2, gp_Pnt Som3)
{
  gp_XYZ curCentr, plnPnt;
  plnPnt = ( Som1.XYZ() + Som2.XYZ() + Som3.XYZ() )/3;
  curCentr = plnPnt + (RefPoint.XYZ() - plnPnt)/4;
  return curCentr;
}


//=======================================================================
//function : CalculVolume
//purpose  : auxilary
//=======================================================================
static Standard_Real CalculVolume(const TopoDS_Shape& So,
				  gp_Pnt&       aRefPoint,
				  Standard_Real       tol,
				  Standard_Boolean withForce,
				  Draw_Interpretor& di)
{
  Standard_Real myVolume = 0, curVolume = 0;
  gp_XYZ localCentroid(0,0,0), curCentroid(0,0,0);
  Standard_Boolean haveVertex = Standard_False;
  for (TopExp_Explorer ex(So,TopAbs_FACE) ; ex.More(); ex.Next())
    {
      TopoDS_Face F =TopoDS::Face(ex.Current());
      TopLoc_Location L;
      if ( ! haveVertex )
	for (TopExp_Explorer Vex(F, TopAbs_VERTEX); Vex.More(); Vex.Next() )
	  {
	    TopoDS_Vertex v = TopoDS::Vertex(Vex.Current());
	    if ( ! v.IsNull() ) {
	      aRefPoint = BRep_Tool::Pnt(v);
	      haveVertex = Standard_True;
	      break;
	    }
	  }
      
      Handle (Poly_Triangulation) facing = BRep_Tool::Triangulation(F,L);
      if(facing.IsNull() || withForce)
	{
//    BRepMesh::Mesh(F, tol);
	  BRepMesh_IncrementalMesh MESH(F, tol);
	  
	  facing = BRep_Tool::Triangulation(F,L);
	}

      TColgp_Array1OfPnt tab(1,(facing->NbNodes()));
      tab = facing->Nodes();
      Poly_Array1OfTriangle tri(1,facing->NbTriangles());
      tri = facing->Triangles();
      for (Standard_Integer i=1;i<=(facing->NbTriangles());i++)
	{
	  
	  Poly_Triangle trian = tri.Value(i);
	  Standard_Integer index1,index2,index3;//M,N;
	  if( F.Orientation() == TopAbs_REVERSED )
	    trian.Get(index1,index3,index2);
	  else
	    trian.Get(index1,index2,index3);
	  curVolume = TetraVol(aRefPoint, tab.Value(index1),
			       tab.Value(index2), tab.Value(index3));
	  myVolume += curVolume;
	  curCentroid = TetraCen(aRefPoint, tab.Value(index1),
				 tab.Value(index2), tab.Value(index3));
	  
	  localCentroid = localCentroid + curCentroid*curVolume;
	}
    }
  
  localCentroid = localCentroid * (1./ myVolume);
  
  di << "Centroid:" << "\n";
  di << "X=\t" << localCentroid.X() << "\n";
  di << "Y=\t" << localCentroid.Y() << "\n";
  di << "Z=\t" << localCentroid.Z() << "\n";
  return (myVolume);
}


// --------------------- VolumeFix End   ---


//=======================================================================
// Section: Work with val props
//=======================================================================

//=======================================================================
//function : SetProps
//purpose  : 
//=======================================================================

static Standard_Integer SetProps (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
{
  if (argc < 3) {
    di<<"Use: "<<argv[0]<<" DocName {Shape|Label} [epsilon = 0.001]"<<"\n";
    return 1;
  }
  Handle(TDocStd_Document) Doc;
  DDocStd::GetDocument(argv[1], Doc);
  if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
  
  Standard_Real Vres, Ares;

  TDF_Label aLabel;
  TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
  TopoDS_Shape aShape;
  if ( aLabel.IsNull() ) {
    aShape= DBRep::Get(argv[2]);
    if ( !aShape.IsNull() ) {
      Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
      aLabel = STool->FindShape(aShape);
    }
  }
  else {
    aShape = XCAFDoc_ShapeTool::GetShape ( aLabel );
  }
  if ( !aLabel.IsNull() ) {
    
    // retrieve epsilon
    Standard_Real anEps;
    if(argc > 3 ) anEps = atof(argv[3]);
    else anEps = 0.001;
    
    GProp_GProps G;
    BRepGProp::VolumeProperties(aShape,G,anEps,Standard_True);
    Vres = G.Mass();
    Handle(XCAFDoc_Volume) aVolume = new XCAFDoc_Volume;
    if (!aLabel.FindAttribute (XCAFDoc_Volume::GetID(), aVolume)) aLabel.AddAttribute(aVolume);
    aVolume->Set(Vres);
    
    gp_Pnt aPoint = G.CentreOfMass();
    Handle(XCAFDoc_Centroid) aCentroid = new XCAFDoc_Centroid;
    if (!aLabel.FindAttribute (XCAFDoc_Centroid::GetID(), aCentroid)) aLabel.AddAttribute(aCentroid);
    aCentroid->Set(aPoint);

    BRepGProp::SurfaceProperties(aShape,G,anEps);
    Ares = G.Mass();
    Handle(XCAFDoc_Area) aArea = new XCAFDoc_Area;
    if (!aLabel.FindAttribute (XCAFDoc_Area::GetID(), aArea)) aLabel.AddAttribute(aArea);
    aArea->Set(Ares);
    
    di << argv[2] << ": Volume = " << Vres << ", Area = " << Ares << 
      ", Centroid is (" << aPoint.X() << ", " << aPoint.Y() << ", " << aPoint.Z() << ")";
  }
  return 0;
}


//=======================================================================
//function : SetVolume
//purpose  : 
//=======================================================================

static Standard_Integer SetVolume (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
{
  if (argc!=4) {
    di<<"Use: "<<argv[0]<<" DocName {Label|Shape} volume"<<"\n";
    return 1;
  }
  Handle(TDocStd_Document) Doc;
  DDocStd::GetDocument(argv[1], Doc);
  if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
  
  Standard_Real res=0.;

  TDF_Label aLabel;
  TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
  if ( aLabel.IsNull() ) {
    TopoDS_Shape aShape= DBRep::Get(argv[2]);
    if ( !aShape.IsNull() ) {
      Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
      aLabel = STool->FindShape(aShape);
    }
  }
  if ( !aLabel.IsNull() ) {
    res = atof(argv[3]);
    Handle(XCAFDoc_Volume) aVolume = new XCAFDoc_Volume;
    if (!aLabel.FindAttribute (XCAFDoc_Volume::GetID(), aVolume)) aLabel.AddAttribute(aVolume);
    aVolume->Set(res);
  }
  
  di << res;
  return 0;
}


//=======================================================================
//function : SetArea
//purpose  : 
//=======================================================================

static Standard_Integer SetArea (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
{
  if (argc!=4) {
    di<<"Use: "<<argv[0]<<" DocName {Label|Shape} area"<<"\n";
    return 1;
  }
  Handle(TDocStd_Document) Doc;
  DDocStd::GetDocument(argv[1], Doc);
  if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
  
  Standard_Real res=0.;

  TDF_Label aLabel;
  TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
  if ( aLabel.IsNull() ) {
    TopoDS_Shape aShape= DBRep::Get(argv[2]);
    if ( !aShape.IsNull() ) {
      Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
      aLabel = STool->FindShape(aShape);
    }
  }
  if ( !aLabel.IsNull() ) {
    res = atof(argv[3]);
    Handle(XCAFDoc_Area) aArea = new XCAFDoc_Area;
    if (!aLabel.FindAttribute (XCAFDoc_Area::GetID(), aArea)) aLabel.AddAttribute(aArea);
    aArea->Set(res);
  }
  di << res;
  return 0;
}


//=======================================================================
//function : SetCentroid
//purpose  : 
//=======================================================================

static Standard_Integer SetCentroid (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
{
  if (argc!=6) {
    di<<"Use: "<<argv[0]<<" DocName {Label|Shape} x y z"<<"\n";
    return 1;
  }
  Handle(TDocStd_Document) Doc;
  DDocStd::GetDocument(argv[1], Doc);
  if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
  
  gp_Pnt aPoint;

  TDF_Label aLabel;
  TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
  if ( aLabel.IsNull() ) {
    TopoDS_Shape aShape= DBRep::Get(argv[2]);
    if ( !aShape.IsNull() ) {
      Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
      aLabel = STool->FindShape(aShape);
    }
  }
  if ( !aLabel.IsNull() ) {
    aPoint.SetX(atof(argv[3]));
    aPoint.SetY(atof(argv[4]));
    aPoint.SetZ(atof(argv[5]));
    Handle(XCAFDoc_Centroid) aCentroid = new XCAFDoc_Centroid;
    if (!aLabel.FindAttribute (XCAFDoc_Centroid::GetID(), aCentroid)) aLabel.AddAttribute(aCentroid);
    aCentroid->Set(aPoint);
    di << atof(argv[3])<<" "<<atof(argv[4])<<" "<<atof(argv[5]);
  }
  return 0;
}


//=======================================================================
//function : GetVolume
//purpose  : 
//=======================================================================

static Standard_Integer GetVolume (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
{
  if (argc!=3) {
    di<<"Use: "<<argv[0]<<" DocName {Shape|Label}"<<"\n";
    return 1;
  }
  Handle(TDocStd_Document) Doc;
  DDocStd::GetDocument(argv[1], Doc);
  if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
  
  TDF_Label aLabel;
  TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
  if ( aLabel.IsNull() ) {
    TopoDS_Shape aShape= DBRep::Get(argv[2]);
    if ( !aShape.IsNull() ) {
      Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
      aLabel = STool->FindShape(aShape);
    }
  }
  if ( !aLabel.IsNull() ) {
//    Handle(XCAFDoc_Volume) aVolume = new (XCAFDoc_Volume);
//    if (aLabel.FindAttribute (XCAFDoc_Volume::GetID(), aVolume)) di << aVolume->Get();
    // another case
    Standard_Real aVol;
    if(XCAFDoc_Volume::Get(aLabel, aVol))
      di << aVol;
  }
  return 0;
}


//=======================================================================
//function : GetArea
//purpose  : 
//=======================================================================

static Standard_Integer GetArea (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
{
  if (argc!=3) {
    di<<"Use: "<<argv[0]<<" DocName {Shape|Label}"<<"\n";
    return 1;
  }
  Handle(TDocStd_Document) Doc;
  DDocStd::GetDocument(argv[1], Doc);
  if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
  
  TDF_Label aLabel;
  TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
  if ( aLabel.IsNull() ) {
    TopoDS_Shape aShape= DBRep::Get(argv[2]);
    if ( !aShape.IsNull() ) {
      Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
      aLabel = STool->FindShape(aShape);
    }
  }
  if ( !aLabel.IsNull() ) {
//     Handle(XCAFDoc_Area) aArea = new (XCAFDoc_Area);
//     if (aLabel.FindAttribute (XCAFDoc_Area::GetID(), aArea)) di << aArea->Get();
    // another case
    Standard_Real anA;
    if(XCAFDoc_Area::Get(aLabel, anA))
      di << anA;
  }
  return 0;
}


//=======================================================================
//function : GetCentroid
//purpose  : 
//=======================================================================

static Standard_Integer GetCentroid (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
{
  if (argc!=3) {
    di<<"Use: "<<argv[0]<<" DocName {Shape|Label} "<<"\n";
    return 1;
  }
  Handle(TDocStd_Document) Doc;
  DDocStd::GetDocument(argv[1], Doc);
  if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
  
  gp_Pnt aPoint;
  
  TDF_Label aLabel;
  TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
  if ( aLabel.IsNull() ) {
    TopoDS_Shape aShape= DBRep::Get(argv[2]);
    if ( !aShape.IsNull() ) {
      Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
      aLabel = STool->FindShape(aShape);
    }
  }
  if ( !aLabel.IsNull() ) {
    Handle(XCAFDoc_Centroid) aCentroid = new (XCAFDoc_Centroid);
    if (aLabel.FindAttribute (XCAFDoc_Centroid::GetID(), aCentroid)) {
//       aPoint = aCentroid->Get();
//       di << aPoint.X()<<" "<<aPoint.Y()<<" "<<aPoint.Z();
      // another case
      if(XCAFDoc_Centroid::Get(aLabel, aPoint))
        di << aPoint.X()<<" "<<aPoint.Y()<<" "<<aPoint.Z();
    }
  }
  return 0;
}


//=======================================================================
//function : doround
//purpose  : auxilary
//=======================================================================

static inline Standard_Real doround (Standard_Real val, Standard_Real low)
{
  return Abs ( val ) < low ? 0. : val;
}


//=======================================================================
//function : CheckProps
//purpose  : 
//=======================================================================

static Standard_Integer CheckProps (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
{
  if (argc <2) {
    di << "Use: "<<argv[0]<<" DocName [ 0|deflection [Shape|Label] ]"<<"\n";
    di << "     If second argument is 0, standard CADCADE tool is used for" << "\n";
    di << "     computation of volume and CG." << "\n";
    di << "     If second argument is not 0, it is treated as deflection" << "\n";
    di << "     and computation is done by triangulations" << "\n";
    di << "     If the second argument is negative, meshing is forced" << "\n";
    return 1;
  }
  Handle(TDocStd_Document) Doc;
  DDocStd::GetDocument(argv[1], Doc);
  if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
  Standard_Boolean withVolFix = Standard_False;
  if ( argc >2 && atof(argv[2]) != 0 ) withVolFix = Standard_True;
  Standard_Boolean wholeDoc = ( argc <4 );
  TDF_LabelSequence seq;
  if ( ! wholeDoc ) {
    TDF_Label aLabel;
    TDF_Tool::Label(Doc->GetData(), argv[3], aLabel);
    TopoDS_Shape aShape;
    if ( aLabel.IsNull() ) {
      aShape= DBRep::Get(argv[3]);
      if ( !aShape.IsNull() ) {
	Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
	aLabel = STool->FindShape(aShape);
      }
    }
    if ( aLabel.IsNull() ) return 1;
    seq.Append ( aLabel );
  }
  else {
    Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
    STool->GetShapes(seq);
  }
  if ( wholeDoc ) {
    di << "Label            Area defect   Volume defect    dX      dY      dZ    Name" << "\n";
  }
  for ( Standard_Integer i=1; i <= seq.Length(); i++ ) {
    TDF_Label aLabel = seq(i);
    
    // add instance labels to sequence to process them as well
    if ( XCAFDoc_ShapeTool::IsAssembly ( aLabel ) ) {
      TDF_LabelSequence comp;
      XCAFDoc_ShapeTool::GetComponents ( aLabel, comp );
      Standard_Integer m=i;
      for ( Standard_Integer k=1; k <= comp.Length(); k++ ) {
	TDF_Label lab = comp(k);
	Handle(XCAFDoc_Volume) aVolume;
	Handle(XCAFDoc_Area) aArea;
	Handle(XCAFDoc_Centroid) aCentroid;
	if ( ! lab.FindAttribute (XCAFDoc_Volume::GetID(), aVolume) &&
	     ! lab.FindAttribute (XCAFDoc_Area::GetID(), aArea) &&
	     ! lab.FindAttribute (XCAFDoc_Centroid::GetID(), aCentroid) ) continue;
	seq.InsertAfter ( m++, lab );
      }
    }

    TCollection_AsciiString str;
    TDF_Tool::Entry ( aLabel, str );
    //printf ( "%s%-12.12s", ( wholeDoc ? "" : "Label " ), str.ToCString() );
    //fflush ( stdout );
    char string1[260];
    sprintf (string1, "%s%-12.12s", ( wholeDoc ? "" : "Label " ), str.ToCString() );
    di << string1;
    Handle(TDataStd_Name) N;
    if ( aLabel.FindAttribute ( TDataStd_Name::GetID(), N ) && ! wholeDoc ) {
      TCollection_AsciiString name(N->Get(), '?');
      di << " \"" << name.ToCString() << "\"";
    }
    if ( ! wholeDoc ) di << "\n";
    
    Handle(XCAFDoc_Volume) aVolume;
    Handle(XCAFDoc_Area) aArea;
    Handle(XCAFDoc_Centroid) aCentroid;
    aLabel.FindAttribute (XCAFDoc_Volume::GetID(), aVolume);
    aLabel.FindAttribute (XCAFDoc_Area::GetID(), aArea);
    aLabel.FindAttribute (XCAFDoc_Centroid::GetID(), aCentroid);
    GProp_GProps G;

    TopoDS_Shape aShape = XCAFDoc_ShapeTool::GetShape ( aLabel );
    if ( ! aArea.IsNull() ) {
      try { 
        OCC_CATCH_SIGNALS
	BRepGProp::SurfaceProperties(aShape,G,0.001);
	//printf ("%s%9.1f (%3d%%)%s", ( wholeDoc ? "" : "  Area defect:   " ),
	//	aArea->Get() - G.Mass(), 
	//	(Standard_Integer)( Abs ( G.Mass() ) > 1e-10 ? 100. * ( aArea->Get() - G.Mass() ) / G.Mass() : 999. ),
	//	( wholeDoc ? "" : "\n" ));
	char string2[260];
	sprintf (string2, "%s%9.1f (%3d%%)%s", ( wholeDoc ? "" : "  Area defect:   " ),
		 aArea->Get() - G.Mass(), 
		 (Standard_Integer)( Abs ( G.Mass() ) > 1e-10 ? 100. * ( aArea->Get() - G.Mass() ) / G.Mass() : 999. ),
		 ( wholeDoc ? "" : "\n" ));
	di << string2;
      }
      catch (Standard_Failure) {
	//printf ( "%-16.16s", "exception" );
	char string3[260];
	sprintf (string3, "%-16.16s", "exception" );
	di << string3;
      }
    }
    else if ( wholeDoc ) {
      //printf ( "%16.16s", "" );
      char string4[260];
      sprintf (string4, "%16.16s", "" );
      di << string4;
    }

    if ( ! aVolume.IsNull() || ! aCentroid.IsNull() ) {
      try {
        OCC_CATCH_SIGNALS
	// Added for check Volume. PTV 08 Nov 2000.	
	Standard_Real localVolume;
	gp_Pnt pcg(0,0,0);
	if ( withVolFix ) {
	  Standard_Real tol = atof(argv[2]);
	  Standard_Boolean withForce = Standard_False;
	  if ( tol < 0 ) {
	    withForce = Standard_True;
	    tol = -tol;
	  }
	  localVolume = CalculVolume(aShape, pcg, tol, withForce, di);
	}
	else {
	  BRepGProp::VolumeProperties(aShape,G,0.001,Standard_True);
	  localVolume = G.Mass();
	  pcg = G.CentreOfMass();
	}
	
	if ( ! aVolume.IsNull() ) {
	  //printf ("%s%9.1f (%3d%%)%s", ( wholeDoc ? "" : "  Volume defect: " ),
		//  aVolume->Get() - localVolume,
		//  (Standard_Integer)( Abs ( localVolume ) > 1e-10 ? 100. * ( aVolume->Get() - localVolume ) / localVolume : 999. ),
		//  ( wholeDoc ? "" : "\n" ));
	  char string5[260];
	  sprintf (string5, "%s%9.1f (%3d%%)%s", ( wholeDoc ? "" : "  Volume defect: " ),
		   aVolume->Get() - localVolume,
		   (Standard_Integer)( Abs ( localVolume ) > 1e-10 ? 100. * ( aVolume->Get() - localVolume ) / localVolume : 999. ),
		   ( wholeDoc ? "" : "\n" ));
	  di << string5;
	}
	else if ( wholeDoc ) {
	  //printf ( "%16.16s", "" );
	  char string6[260];
	  sprintf (string6, "%16.16s", "" );
	  di << string6;
	}

	if ( ! aCentroid.IsNull() ) {
	  gp_Pnt p = aCentroid->Get();
	  char string7[260];
	  if ( wholeDoc ) {
	    //printf ( " %7.2f %7.2f %7.2f", 
		//    p.X() - pcg.X(), p.Y() - pcg.Y(), p.Z() - pcg.Z() );
	    sprintf (string7, " %7.2f %7.2f %7.2f", 
		    p.X() - pcg.X(), p.Y() - pcg.Y(), p.Z() - pcg.Z() );
	  } else {
	    //printf ( "  CG defect: dX=%.3f, dY=%.3f, dZ=%.3f\n", 
		//    p.X() - pcg.X(), p.Y() - pcg.Y(), p.Z() - pcg.Z() );
	    sprintf (string7, "  CG defect: dX=%.3f, dY=%.3f, dZ=%.3f\n", 
		    p.X() - pcg.X(), p.Y() - pcg.Y(), p.Z() - pcg.Z() );
	  }
	  di << string7;
	}
	else if ( wholeDoc ) {
	  //printf ( "%24.24s", "" );
	  char string8[260];
	  sprintf (string8, "%24.24s", "" );
	  di << string8;
	}
      }
      catch (Standard_Failure) {
	//printf ( "%40.40s", "exception" );
	char string9[260];
	sprintf (string9, "%40.40s", "exception" );
	di << string9;
#ifdef DEB
	//fflush ( stdout );
	di << ": ";
	di << Standard_Failure::Caught()->GetMessageString();
	di<<" ** Skip"<<"\n";
#endif
      }
    }
    else if ( wholeDoc ) {
      //printf ( "%40.40s", "" );
      char string10[260];
      sprintf (string10, "%40.40s", "" );
      di << string10;
    }
    //fflush ( stdout );

    if ( wholeDoc ) {
      if ( ! N.IsNull() ) {
	TCollection_AsciiString name(N->Get(),'?');
	di << "  \"" << name.ToCString() << "\"";
      }
      di << "\n";
    }
  }
  return 0;
}


//=======================================================================
//function : ShapeVolume
//purpose  : 
//=======================================================================

static Standard_Integer ShapeVolume (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
{
  if (argc!=3) {
    di<<"Use: "<<argv[0]<<" Shape deflection "<<"\n";
    return 1;
  }
  TopoDS_Shape aShape = DBRep::Get(argv[1]);
  if (aShape.IsNull()) return 1;
  gp_Pnt aPoint(0,0,0);
  Standard_Real localVolume;
  Standard_Real tol = atof(argv[2]);
  Standard_Boolean withForce = Standard_False;
  if ( tol < 0 ) {
    withForce = Standard_True;
    tol = -tol;
  }
  localVolume = CalculVolume(aShape, aPoint, tol, withForce, di);
  //cout << "Volume : " << setw(15) << localVolume << "\n" << endl;
  Standard_SStream aSStream;
  aSStream << "Volume : " << setw(15) << localVolume << "\n";
  di << aSStream;
  return 0;
}


//=======================================================================
//function : GetMassProps
//purpose  : auxilary for ShapeMassProps
//=======================================================================

static Standard_Boolean GetMassProps(const TDF_Label& aLabel, gp_XYZ& theCenterGravity,
                                     Standard_Real& theMassVal, const Standard_Real thetol)
{
  Standard_Real aDensity = XCAFDoc_MaterialTool::GetDensityForShape(aLabel);

  if(aDensity >0) {
    TopoDS_Shape aShape = XCAFDoc_ShapeTool::GetShape(aLabel);
    GProp_GProps G;
    BRepGProp::VolumeProperties(aShape,G,0.001,Standard_True);
    Standard_Real localVolume = G.Mass();
    theMassVal = aDensity*localVolume;
    theCenterGravity = G.CentreOfMass().XYZ();
    return Standard_True;
  }

  if(aDensity==0) {
    Handle(TNaming_NamedShape) NS;
    if(aLabel.FindAttribute(TNaming_NamedShape::GetID(),NS)) {
      //S = TNaming_Tool::GetShape(NS);
      TopoDS_Shape aSh = NS->Get();
      if(aSh.ShapeType()==TopAbs_SOLID) return Standard_False;
    }

    //TopoDS_Shape aSh = XCAFDoc_ShapeTool::GetShape(aLabel);
    //if(aSh.ShapeType()==TopAbs_SOLID) return Standard_False;

    Handle(TDataStd_TreeNode) Node;
    if( aLabel.FindAttribute(XCAFDoc::ShapeRefGUID(),Node) && Node->HasFather() ) {
      TDF_Label SubL = Node->Father()->Label();
      if(GetMassProps(SubL,theCenterGravity,theMassVal,thetol)) {
        Handle(XCAFDoc_Location) LocationAttribute;
        if(aLabel.FindAttribute(XCAFDoc_Location::GetID(),LocationAttribute)) {
          gp_XYZ tmp = LocationAttribute->Get().Transformation().TranslationPart();
          theCenterGravity += tmp;
        }
        return Standard_True;
      }
      else
        return Standard_False;
    }
    else {
      // calculate for components
      TDF_LabelSequence comp;
      XCAFDoc_ShapeTool::GetComponents ( aLabel, comp );
      if(!comp.Length())
        return Standard_False;
  
      TColgp_Array1OfXYZ anArrCentres(1,comp.Length());
      TColStd_Array1OfReal anArrMass(1,comp.Length());
      anArrMass.Init(0.0);
      Standard_Real aTotalMass =0.0;
      Standard_Integer k=1;
      for ( ; k <= comp.Length(); k++ ) {
        TDF_Label lab = comp(k);
        gp_XYZ aCenterGravity(0.0,0.0,0.0);
        Standard_Real aMassVal =0.0;
        if(GetMassProps(lab,aCenterGravity,aMassVal,thetol)) {
          anArrCentres.SetValue(k,aCenterGravity);
          anArrMass.SetValue(k,aMassVal);
          aTotalMass += aMassVal;
        }
      }
      if(aTotalMass>0) {
        Standard_Real x= 0.0, y =0.0, z= 0.0;
        for( k=1; k <= anArrMass.Length(); k++) {
          Standard_Real koef = anArrMass.Value(k)/aTotalMass;
          x+= (anArrCentres.Value(k).X()*koef);
          y+= (anArrCentres.Value(k).Y()*koef);
          z+= (anArrCentres.Value(k).Z()*koef);
        }
        theMassVal = aTotalMass;
        theCenterGravity.SetCoord(x,y,z);
      }
    }
  }
  return Standard_True;
}


//=======================================================================
//function : ShapeMassProps
//purpose  : 
//=======================================================================

static Standard_Integer ShapeMassProps (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
{
  
  if (argc <2) {
    di << "Use: "<<argv[0]<<" DocName [deflection [Shape|Label] ]"<<"\n";
    di << "     If second argument is 0, standard CADCADE tool is used for" << "\n";
    di << "     computation of volume and CG." << "\n";
    di << "     If second argument is not 0, it is treated as deflection" << "\n";
    di << "     and computation is done by triangulations" << "\n";
    di << "     If the second argument is negative, meshing is forced" << "\n";
    return 1;
  }
  Handle(TDocStd_Document) Doc;
  DDocStd::GetDocument(argv[1], Doc);
  Standard_Real atol = Precision::Confusion();
  if(argc >2)
    atol  = atof(argv[2]);
  if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
  Standard_Boolean wholeDoc = ( argc <4 );
  TDF_LabelSequence seq;
  if ( ! wholeDoc ) {
    TDF_Label aLabel;
    TDF_Tool::Label(Doc->GetData(), argv[3], aLabel);
    TopoDS_Shape aShape;
    if ( aLabel.IsNull() ) {
      aShape= DBRep::Get(argv[3]);
      if ( !aShape.IsNull() ) {
	Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
	aLabel = STool->FindShape(aShape);
      }
    }
    if ( aLabel.IsNull() ) return 1;
    seq.Append ( aLabel );
  }
  else {
    Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
    STool->GetShapes(seq);
  }
  //if ( wholeDoc ) {
  //  di << "Label            Area defect   Volume defect    dX      dY      dZ    Name" << "\n";
 // }
  gp_XYZ aCenterGravity(0.0,0.0,0.0);
  Standard_Real aMassVal =0.0;
  for ( Standard_Integer i=1; i <= seq.Length(); i++ ) {
    TDF_Label aLabel = seq(i);
    GetMassProps(aLabel,aCenterGravity,aMassVal,atol);
//    if(GetMassProps(aLabel,aCenterGravity,aMassVal,atol))
//    {
      TCollection_AsciiString str;
      TDF_Tool::Entry ( aLabel, str );
    if(aMassVal>0) {
      di<<"Shape from label : "<<str.ToCString()<<"\n";
      di<<"Mass = "<<aMassVal<<"\n";
      di<<"CenterOfGravity X = "<<aCenterGravity.X()<<",Y = "<<aCenterGravity.Y()<<",Z = "<<aCenterGravity.Z()<<"\n";
      di<<"\n";
    }
    else {
//      di<<"For one component density is absent"<<"\n";
      di<<"Shape from label : "<<str.ToCString()<<" not have a mass"<<"\n";
    }
  }
  return 0;
}


//=======================================================================
//function : SetMaterial
//purpose  : 
//=======================================================================

static Standard_Integer SetMaterial (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
{
  if (argc<5) {
    di<<"Use: "<<argv[0]<<" Doc {Label|Shape} name density(g/cu sm) "<<"\n";
    return 1;
  }
  Handle(TDocStd_Document) Doc;   
  DDocStd::GetDocument(argv[1], Doc);
  if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
  
  TDF_Label aLabel;
  TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);

  Handle(XCAFDoc_MaterialTool) MatTool = XCAFDoc_DocumentTool::MaterialTool(Doc->Main());

  MatTool->SetMaterial(aLabel, new TCollection_HAsciiString(argv[3]),
                       new TCollection_HAsciiString(""), atof(argv[4]),
                       new TCollection_HAsciiString("density measure"),
                       new TCollection_HAsciiString("POSITIVE_RATIO_MEASURE"));

  return 0;
}


//=======================================================================
//function : InitCommands
//purpose  : 
//=======================================================================

void XDEDRAW_Props::InitCommands(Draw_Interpretor& di) 
{

  static Standard_Boolean initactor = Standard_False;
  if (initactor) return;  initactor = Standard_True;

  Standard_CString g = "XDE property's commands";
  
  di.Add ("XSetVolume","DocName {Label|Shape} volume \t: Seting volume to shape",
		   __FILE__, SetVolume, g);

  di.Add ("XGetVolume","DocName {Shape|Label} \t: Getting volume of shape",
		   __FILE__, GetVolume, g);

  di.Add ("XSetArea","DocName {Label|Shape} area \t: Seting area to shape",
		   __FILE__, SetArea, g);

  di.Add ("XGetArea","DocName {Shape|Label} \t: Getting area of shape",
		   __FILE__, GetArea, g);

  di.Add ("XSetCentroid","DocName  {Label|Shape} x y z \t: Seting centroid to shape",
		   __FILE__, SetCentroid, g);

  di.Add ("XGetCentroid","DocName {Shape|Label} \t: Getting centroid of shape ",
		   __FILE__, GetCentroid, g);

  di.Add ("XSetProps","DocName {Shape|Label} [epsilon = 0.001] \t: Compute properties for shape ",
		   __FILE__, SetProps, g);

  di.Add ("XCheckProps","DocName [ 0|deflection [Shape|Label] ]\t: Check properties recorded for shape ",
		   __FILE__, CheckProps, g);
  di.Add ("XShapeVolume","Shape \t: Calculating volume of shape",
		   __FILE__, ShapeVolume, g);
  di.Add ("XShapeMassProps","XShapeMassProps DocName [deflection [Shape|Label] ]\t: Get mass value and center of gravity for shape ",
		   __FILE__,ShapeMassProps , g);
  di.Add ("XSetMaterial","Doc {Label|Shape} name density(g/cu sm) \t: Set material to shape given by Label",
		   __FILE__, SetMaterial, g);

}