1 // Created on: 2000-08-04
2 // Created by: Pavel TELKOV
3 // Copyright (c) 2000-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 // OCC532 sln 24.07.2002. Add epsilon parameter to SetProps function
18 #include <BRep_Tool.hxx>
19 #include <BRepGProp.hxx>
20 #include <BRepMesh_IncrementalMesh.hxx>
22 #include <DDocStd.hxx>
25 #include <GProp_GProps.hxx>
26 #include <Poly_Triangulation.hxx>
27 #include <Precision.hxx>
28 #include <Standard_ErrorHandler.hxx>
29 #include <Standard_Failure.hxx>
30 #include <Standard_Stream.hxx>
31 #include <TColgp_Array1OfPnt.hxx>
32 #include <TColgp_Array1OfXYZ.hxx>
33 #include <TCollection_AsciiString.hxx>
34 #include <TCollection_HAsciiString.hxx>
35 #include <TColStd_Array1OfReal.hxx>
36 #include <TDataStd_Name.hxx>
37 #include <TDataStd_TreeNode.hxx>
38 #include <TDF_Label.hxx>
39 #include <TDF_LabelSequence.hxx>
40 #include <TDF_Tool.hxx>
41 #include <TDocStd_Document.hxx>
42 #include <TNaming_NamedShape.hxx>
43 #include <TopExp_Explorer.hxx>
45 #include <TopoDS_Compound.hxx>
46 #include <TopoDS_Face.hxx>
47 #include <TopoDS_Shape.hxx>
48 #include <TopoDS_Vertex.hxx>
49 #include <XCAFDoc.hxx>
50 #include <XCAFDoc_Area.hxx>
51 #include <XCAFDoc_Centroid.hxx>
52 #include <XCAFDoc_DocumentTool.hxx>
53 #include <XCAFDoc_Location.hxx>
54 #include <XCAFDoc_MaterialTool.hxx>
55 #include <XCAFDoc_ShapeTool.hxx>
56 #include <XCAFDoc_Volume.hxx>
57 #include <XDEDRAW_Props.hxx>
60 // --------------------- VolumeFix Begin ---
61 //=======================================================================
64 //=======================================================================
65 static double TetraVol(gp_Pnt RefPoint, gp_Pnt Som1, gp_Pnt Som2, gp_Pnt Som3)
76 Line12=gp_Dir( gp_Vec(Som1, Som2));
77 gp_Vec v1(Som1, Som2);
78 gp_Vec v2(Som2, Som3);
80 Plane123=gp_Pln( Som1, gp_Dir( N ) );
82 catch(Standard_Failure const&) {return(0.);}
85 L1 = Som1.Distance(Som2);
86 L2 = gp_Lin(Som1, Line12).Distance(Som3);
87 L3 = Plane123.Distance(RefPoint);
89 curVolume = ((L1 * L2)/2) * (L3/3);
91 gp_Vec Rad(RefPoint, Som1);
100 //=======================================================================
101 //function : TetraCen
103 //=======================================================================
104 static gp_XYZ TetraCen(gp_Pnt RefPoint, gp_Pnt Som1, gp_Pnt Som2, gp_Pnt Som3)
106 gp_XYZ curCentr, plnPnt;
107 plnPnt = ( Som1.XYZ() + Som2.XYZ() + Som3.XYZ() )/3;
108 curCentr = plnPnt + (RefPoint.XYZ() - plnPnt)/4;
113 //=======================================================================
114 //function : CalculVolume
116 //=======================================================================
117 static Standard_Real CalculVolume(const TopoDS_Shape& So,
120 Standard_Boolean withForce,
121 Draw_Interpretor& di)
123 Standard_Real myVolume = 0, curVolume = 0;
124 gp_XYZ localCentroid(0,0,0), curCentroid(0,0,0);
125 Standard_Boolean haveVertex = Standard_False;
126 for (TopExp_Explorer ex(So,TopAbs_FACE) ; ex.More(); ex.Next())
128 TopoDS_Face F =TopoDS::Face(ex.Current());
131 for (TopExp_Explorer Vex(F, TopAbs_VERTEX); Vex.More(); Vex.Next() )
133 TopoDS_Vertex v = TopoDS::Vertex(Vex.Current());
134 if ( ! v.IsNull() ) {
135 aRefPoint = BRep_Tool::Pnt(v);
136 haveVertex = Standard_True;
141 Handle (Poly_Triangulation) facing = BRep_Tool::Triangulation(F,L);
142 if(facing.IsNull() || withForce)
144 BRepMesh_IncrementalMesh MESH(F, tol);
146 facing = BRep_Tool::Triangulation(F,L);
149 TColgp_Array1OfPnt tab(1,(facing->NbNodes()));
150 tab = facing->Nodes();
151 Poly_Array1OfTriangle tri(1,facing->NbTriangles());
152 tri = facing->Triangles();
153 for (Standard_Integer i=1;i<=(facing->NbTriangles());i++)
156 Poly_Triangle trian = tri.Value(i);
157 Standard_Integer index1,index2,index3;//M,N;
158 if( F.Orientation() == TopAbs_REVERSED )
159 trian.Get(index1,index3,index2);
161 trian.Get(index1,index2,index3);
162 curVolume = TetraVol(aRefPoint, tab.Value(index1),
163 tab.Value(index2), tab.Value(index3));
164 myVolume += curVolume;
165 curCentroid = TetraCen(aRefPoint, tab.Value(index1),
166 tab.Value(index2), tab.Value(index3));
168 localCentroid = localCentroid + curCentroid*curVolume;
172 localCentroid = localCentroid * (1./ myVolume);
175 di << "X=\t" << localCentroid.X() << "\n";
176 di << "Y=\t" << localCentroid.Y() << "\n";
177 di << "Z=\t" << localCentroid.Z() << "\n";
182 // --------------------- VolumeFix End ---
185 //=======================================================================
186 // Section: Work with val props
187 //=======================================================================
189 //=======================================================================
190 //function : SetProps
192 //=======================================================================
194 static Standard_Integer SetProps (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
197 di<<"Use: "<<argv[0]<<" DocName {Shape|Label} [epsilon = 0.001]\n";
200 Handle(TDocStd_Document) Doc;
201 DDocStd::GetDocument(argv[1], Doc);
202 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
204 Standard_Real Vres, Ares;
207 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
209 if ( aLabel.IsNull() ) {
210 aShape= DBRep::Get(argv[2]);
211 if ( !aShape.IsNull() ) {
212 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
213 aLabel = STool->FindShape(aShape);
217 aShape = XCAFDoc_ShapeTool::GetShape ( aLabel );
219 if ( !aLabel.IsNull() ) {
223 if(argc > 3 ) anEps = Draw::Atof(argv[3]);
227 BRepGProp::VolumeProperties(aShape,G,anEps,Standard_True);
229 Handle(XCAFDoc_Volume) aVolume = new XCAFDoc_Volume;
230 if (!aLabel.FindAttribute (XCAFDoc_Volume::GetID(), aVolume)) aLabel.AddAttribute(aVolume);
233 gp_Pnt aPoint = G.CentreOfMass();
234 Handle(XCAFDoc_Centroid) aCentroid = new XCAFDoc_Centroid;
235 if (!aLabel.FindAttribute (XCAFDoc_Centroid::GetID(), aCentroid)) aLabel.AddAttribute(aCentroid);
236 aCentroid->Set(aPoint);
238 BRepGProp::SurfaceProperties(aShape,G,anEps);
240 Handle(XCAFDoc_Area) aArea = new XCAFDoc_Area;
241 if (!aLabel.FindAttribute (XCAFDoc_Area::GetID(), aArea)) aLabel.AddAttribute(aArea);
244 di << argv[2] << ": Volume = " << Vres << ", Area = " << Ares <<
245 ", Centroid is (" << aPoint.X() << ", " << aPoint.Y() << ", " << aPoint.Z() << ")";
251 //=======================================================================
252 //function : SetVolume
254 //=======================================================================
256 static Standard_Integer SetVolume (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
259 di<<"Use: "<<argv[0]<<" DocName {Label|Shape} volume\n";
262 Handle(TDocStd_Document) Doc;
263 DDocStd::GetDocument(argv[1], Doc);
264 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
266 Standard_Real res=0.;
269 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
270 if ( aLabel.IsNull() ) {
271 TopoDS_Shape aShape= DBRep::Get(argv[2]);
272 if ( !aShape.IsNull() ) {
273 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
274 aLabel = STool->FindShape(aShape);
277 if ( !aLabel.IsNull() ) {
278 res = Draw::Atof(argv[3]);
279 Handle(XCAFDoc_Volume) aVolume = new XCAFDoc_Volume;
280 if (!aLabel.FindAttribute (XCAFDoc_Volume::GetID(), aVolume)) aLabel.AddAttribute(aVolume);
289 //=======================================================================
292 //=======================================================================
294 static Standard_Integer SetArea (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
297 di<<"Use: "<<argv[0]<<" DocName {Label|Shape} area\n";
300 Handle(TDocStd_Document) Doc;
301 DDocStd::GetDocument(argv[1], Doc);
302 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
304 Standard_Real res=0.;
307 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
308 if ( aLabel.IsNull() ) {
309 TopoDS_Shape aShape= DBRep::Get(argv[2]);
310 if ( !aShape.IsNull() ) {
311 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
312 aLabel = STool->FindShape(aShape);
315 if ( !aLabel.IsNull() ) {
316 res = Draw::Atof(argv[3]);
317 Handle(XCAFDoc_Area) aArea = new XCAFDoc_Area;
318 if (!aLabel.FindAttribute (XCAFDoc_Area::GetID(), aArea)) aLabel.AddAttribute(aArea);
326 //=======================================================================
327 //function : SetCentroid
329 //=======================================================================
331 static Standard_Integer SetCentroid (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
334 di<<"Use: "<<argv[0]<<" DocName {Label|Shape} x y z\n";
337 Handle(TDocStd_Document) Doc;
338 DDocStd::GetDocument(argv[1], Doc);
339 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
344 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
345 if ( aLabel.IsNull() ) {
346 TopoDS_Shape aShape= DBRep::Get(argv[2]);
347 if ( !aShape.IsNull() ) {
348 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
349 aLabel = STool->FindShape(aShape);
352 if ( !aLabel.IsNull() ) {
353 aPoint.SetX(Draw::Atof(argv[3]));
354 aPoint.SetY(Draw::Atof(argv[4]));
355 aPoint.SetZ(Draw::Atof(argv[5]));
356 Handle(XCAFDoc_Centroid) aCentroid = new XCAFDoc_Centroid;
357 if (!aLabel.FindAttribute (XCAFDoc_Centroid::GetID(), aCentroid)) aLabel.AddAttribute(aCentroid);
358 aCentroid->Set(aPoint);
359 di << Draw::Atof(argv[3])<<" "<<Draw::Atof(argv[4])<<" "<<Draw::Atof(argv[5]);
365 //=======================================================================
366 //function : GetVolume
368 //=======================================================================
370 static Standard_Integer GetVolume (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
373 di<<"Use: "<<argv[0]<<" DocName {Shape|Label}\n";
376 Handle(TDocStd_Document) Doc;
377 DDocStd::GetDocument(argv[1], Doc);
378 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
381 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
382 if ( aLabel.IsNull() ) {
383 TopoDS_Shape aShape= DBRep::Get(argv[2]);
384 if ( !aShape.IsNull() ) {
385 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
386 aLabel = STool->FindShape(aShape);
389 if ( !aLabel.IsNull() ) {
390 // Handle(XCAFDoc_Volume) aVolume = new (XCAFDoc_Volume);
391 // if (aLabel.FindAttribute (XCAFDoc_Volume::GetID(), aVolume)) di << aVolume->Get();
394 if(XCAFDoc_Volume::Get(aLabel, aVol))
401 //=======================================================================
404 //=======================================================================
406 static Standard_Integer GetArea (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
409 di<<"Use: "<<argv[0]<<" DocName {Shape|Label}\n";
412 Handle(TDocStd_Document) Doc;
413 DDocStd::GetDocument(argv[1], Doc);
414 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
417 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
418 if ( aLabel.IsNull() ) {
419 TopoDS_Shape aShape= DBRep::Get(argv[2]);
420 if ( !aShape.IsNull() ) {
421 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
422 aLabel = STool->FindShape(aShape);
425 if ( !aLabel.IsNull() ) {
426 // Handle(XCAFDoc_Area) aArea = new (XCAFDoc_Area);
427 // if (aLabel.FindAttribute (XCAFDoc_Area::GetID(), aArea)) di << aArea->Get();
430 if(XCAFDoc_Area::Get(aLabel, anA))
437 //=======================================================================
438 //function : GetCentroid
440 //=======================================================================
442 static Standard_Integer GetCentroid (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
445 di<<"Use: "<<argv[0]<<" DocName {Shape|Label} \n";
448 Handle(TDocStd_Document) Doc;
449 DDocStd::GetDocument(argv[1], Doc);
450 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
455 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
456 if ( aLabel.IsNull() ) {
457 TopoDS_Shape aShape= DBRep::Get(argv[2]);
458 if ( !aShape.IsNull() ) {
459 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
460 aLabel = STool->FindShape(aShape);
463 if ( !aLabel.IsNull() ) {
464 Handle(XCAFDoc_Centroid) aCentroid = new (XCAFDoc_Centroid);
465 if (aLabel.FindAttribute (XCAFDoc_Centroid::GetID(), aCentroid)) {
466 // aPoint = aCentroid->Get();
467 // di << aPoint.X()<<" "<<aPoint.Y()<<" "<<aPoint.Z();
469 if(XCAFDoc_Centroid::Get(aLabel, aPoint))
470 di << aPoint.X()<<" "<<aPoint.Y()<<" "<<aPoint.Z();
476 //=======================================================================
477 //function : CheckProps
479 //=======================================================================
481 static Standard_Integer CheckProps (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
484 di << "Use: "<<argv[0]<<" DocName [ 0|deflection [Shape|Label] ]\n";
485 di << " If second argument is 0, standard CADCADE tool is used for\n";
486 di << " computation of volume and CG.\n";
487 di << " If second argument is not 0, it is treated as deflection\n";
488 di << " and computation is done by triangulations\n";
489 di << " If the second argument is negative, meshing is forced\n";
492 Handle(TDocStd_Document) Doc;
493 DDocStd::GetDocument(argv[1], Doc);
494 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
495 Standard_Boolean withVolFix = Standard_False;
496 if ( argc >2 && Draw::Atof(argv[2]) != 0 ) withVolFix = Standard_True;
497 Standard_Boolean wholeDoc = ( argc <4 );
498 TDF_LabelSequence seq;
501 TDF_Tool::Label(Doc->GetData(), argv[3], aLabel);
503 if ( aLabel.IsNull() ) {
504 aShape= DBRep::Get(argv[3]);
505 if ( !aShape.IsNull() ) {
506 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
507 aLabel = STool->FindShape(aShape);
510 if ( aLabel.IsNull() ) return 1;
511 seq.Append ( aLabel );
514 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
515 STool->GetShapes(seq);
518 di << "Label Area defect Volume defect dX dY dZ Name\n";
520 for ( Standard_Integer i=1; i <= seq.Length(); i++ ) {
521 TDF_Label aLabel = seq(i);
523 // add instance labels to sequence to process them as well
524 if ( XCAFDoc_ShapeTool::IsAssembly ( aLabel ) ) {
525 TDF_LabelSequence comp;
526 XCAFDoc_ShapeTool::GetComponents ( aLabel, comp );
527 Standard_Integer m=i;
528 for ( Standard_Integer k=1; k <= comp.Length(); k++ ) {
529 TDF_Label lab = comp(k);
530 Handle(XCAFDoc_Volume) aVolume;
531 Handle(XCAFDoc_Area) aArea;
532 Handle(XCAFDoc_Centroid) aCentroid;
533 if ( ! lab.FindAttribute (XCAFDoc_Volume::GetID(), aVolume) &&
534 ! lab.FindAttribute (XCAFDoc_Area::GetID(), aArea) &&
535 ! lab.FindAttribute (XCAFDoc_Centroid::GetID(), aCentroid) ) continue;
536 seq.InsertAfter ( m++, lab );
540 TCollection_AsciiString str;
541 TDF_Tool::Entry ( aLabel, str );
542 //printf ( "%s%-12.12s", ( wholeDoc ? "" : "Label " ), str.ToCString() );
545 Sprintf (string1, "%s%-12.12s", ( wholeDoc ? "" : "Label " ), str.ToCString() );
547 Handle(TDataStd_Name) N;
548 if ( aLabel.FindAttribute ( TDataStd_Name::GetID(), N ) && ! wholeDoc ) {
549 TCollection_AsciiString name(N->Get(), '?');
550 di << " \"" << name.ToCString() << "\"";
552 if ( ! wholeDoc ) di << "\n";
554 Handle(XCAFDoc_Volume) aVolume;
555 Handle(XCAFDoc_Area) aArea;
556 Handle(XCAFDoc_Centroid) aCentroid;
557 aLabel.FindAttribute (XCAFDoc_Volume::GetID(), aVolume);
558 aLabel.FindAttribute (XCAFDoc_Area::GetID(), aArea);
559 aLabel.FindAttribute (XCAFDoc_Centroid::GetID(), aCentroid);
562 TopoDS_Shape aShape = XCAFDoc_ShapeTool::GetShape ( aLabel );
563 if ( ! aArea.IsNull() ) {
566 BRepGProp::SurfaceProperties(aShape,G,0.001);
567 //printf ("%s%9.1f (%3d%%)%s", ( wholeDoc ? "" : " Area defect: " ),
568 // aArea->Get() - G.Mass(),
569 // (Standard_Integer)( Abs ( G.Mass() ) > 1e-10 ? 100. * ( aArea->Get() - G.Mass() ) / G.Mass() : 999. ),
570 // ( wholeDoc ? "" : "\n" ));
572 Sprintf (string2, "%s%9.1f (%3d%%)%s", ( wholeDoc ? "" : " Area defect: " ),
573 aArea->Get() - G.Mass(),
574 (Standard_Integer)( Abs ( G.Mass() ) > 1e-10 ? 100. * ( aArea->Get() - G.Mass() ) / G.Mass() : 999. ),
575 ( wholeDoc ? "" : "\n" ));
578 catch (Standard_Failure const&) {
579 //printf ( "%-16.16s", "exception" );
581 Sprintf (string3, "%-16.16s", "exception" );
585 else if ( wholeDoc ) {
586 //printf ( "%16.16s", "" );
588 Sprintf (string4, "%16.16s", "" );
592 if ( ! aVolume.IsNull() || ! aCentroid.IsNull() ) {
595 // Added for check Volume. PTV 08 Nov 2000.
596 Standard_Real localVolume;
599 Standard_Real tol = Draw::Atof(argv[2]);
600 Standard_Boolean withForce = Standard_False;
602 withForce = Standard_True;
605 localVolume = CalculVolume(aShape, pcg, tol, withForce, di);
608 BRepGProp::VolumeProperties(aShape,G,0.001,Standard_True);
609 localVolume = G.Mass();
610 pcg = G.CentreOfMass();
613 if ( ! aVolume.IsNull() ) {
614 //printf ("%s%9.1f (%3d%%)%s", ( wholeDoc ? "" : " Volume defect: " ),
615 // aVolume->Get() - localVolume,
616 // (Standard_Integer)( Abs ( localVolume ) > 1e-10 ? 100. * ( aVolume->Get() - localVolume ) / localVolume : 999. ),
617 // ( wholeDoc ? "" : "\n" ));
619 Sprintf (string5, "%s%9.1f (%3d%%)%s", ( wholeDoc ? "" : " Volume defect: " ),
620 aVolume->Get() - localVolume,
621 (Standard_Integer)( Abs ( localVolume ) > 1e-10 ? 100. * ( aVolume->Get() - localVolume ) / localVolume : 999. ),
622 ( wholeDoc ? "" : "\n" ));
625 else if ( wholeDoc ) {
626 //printf ( "%16.16s", "" );
628 Sprintf (string6, "%16.16s", "" );
632 if ( ! aCentroid.IsNull() ) {
633 gp_Pnt p = aCentroid->Get();
636 //printf ( " %7.2f %7.2f %7.2f",
637 // p.X() - pcg.X(), p.Y() - pcg.Y(), p.Z() - pcg.Z() );
638 Sprintf (string7, " %7.2f %7.2f %7.2f",
639 p.X() - pcg.X(), p.Y() - pcg.Y(), p.Z() - pcg.Z() );
641 //printf ( " CG defect: dX=%.3f, dY=%.3f, dZ=%.3f\n",
642 // p.X() - pcg.X(), p.Y() - pcg.Y(), p.Z() - pcg.Z() );
643 Sprintf (string7, " CG defect: dX=%.3f, dY=%.3f, dZ=%.3f\n",
644 p.X() - pcg.X(), p.Y() - pcg.Y(), p.Z() - pcg.Z() );
648 else if ( wholeDoc ) {
649 //printf ( "%24.24s", "" );
651 Sprintf (string8, "%24.24s", "" );
655 catch (Standard_Failure const& anException) {
656 //printf ( "%40.40s", "exception" );
658 Sprintf (string9, "%40.40s", "exception" );
663 di << anException.GetMessageString();
669 else if ( wholeDoc ) {
670 //printf ( "%40.40s", "" );
672 Sprintf (string10, "%40.40s", "" );
678 if ( ! N.IsNull() ) {
679 TCollection_AsciiString name(N->Get(),'?');
680 di << " \"" << name.ToCString() << "\"";
689 //=======================================================================
690 //function : ShapeVolume
692 //=======================================================================
694 static Standard_Integer ShapeVolume (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
697 di<<"Use: "<<argv[0]<<" Shape deflection \n";
700 TopoDS_Shape aShape = DBRep::Get(argv[1]);
701 if (aShape.IsNull()) return 1;
702 gp_Pnt aPoint(0,0,0);
703 Standard_Real localVolume;
704 Standard_Real tol = Draw::Atof(argv[2]);
705 Standard_Boolean withForce = Standard_False;
707 withForce = Standard_True;
710 localVolume = CalculVolume(aShape, aPoint, tol, withForce, di);
711 //std::cout << "Volume : " << std::setw(15) << localVolume << "\n" << std::endl;
712 Standard_SStream aSStream;
713 aSStream << "Volume : " << std::setw(15) << localVolume << "\n";
719 //=======================================================================
720 //function : GetMassProps
721 //purpose : auxilary for ShapeMassProps
722 //=======================================================================
724 static Standard_Boolean GetMassProps(const TDF_Label& aLabel, gp_XYZ& theCenterGravity,
725 Standard_Real& theMassVal, const Standard_Real thetol)
727 Standard_Real aDensity = XCAFDoc_MaterialTool::GetDensityForShape(aLabel);
730 TopoDS_Shape aShape = XCAFDoc_ShapeTool::GetShape(aLabel);
732 BRepGProp::VolumeProperties(aShape,G,0.001,Standard_True);
733 Standard_Real localVolume = G.Mass();
734 theMassVal = aDensity*localVolume;
735 theCenterGravity = G.CentreOfMass().XYZ();
736 return Standard_True;
740 Handle(TNaming_NamedShape) NS;
741 if(aLabel.FindAttribute(TNaming_NamedShape::GetID(),NS)) {
742 //S = TNaming_Tool::GetShape(NS);
743 TopoDS_Shape aSh = NS->Get();
744 if(aSh.ShapeType()==TopAbs_SOLID) return Standard_False;
747 //TopoDS_Shape aSh = XCAFDoc_ShapeTool::GetShape(aLabel);
748 //if(aSh.ShapeType()==TopAbs_SOLID) return Standard_False;
750 Handle(TDataStd_TreeNode) Node;
751 if( aLabel.FindAttribute(XCAFDoc::ShapeRefGUID(),Node) && Node->HasFather() ) {
752 TDF_Label SubL = Node->Father()->Label();
753 if(GetMassProps(SubL,theCenterGravity,theMassVal,thetol)) {
754 Handle(XCAFDoc_Location) LocationAttribute;
755 if(aLabel.FindAttribute(XCAFDoc_Location::GetID(),LocationAttribute)) {
756 gp_XYZ tmp = LocationAttribute->Get().Transformation().TranslationPart();
757 theCenterGravity += tmp;
759 return Standard_True;
762 return Standard_False;
765 // calculate for components
766 TDF_LabelSequence comp;
767 XCAFDoc_ShapeTool::GetComponents ( aLabel, comp );
769 return Standard_False;
771 TColgp_Array1OfXYZ anArrCentres(1,comp.Length());
772 TColStd_Array1OfReal anArrMass(1,comp.Length());
774 Standard_Real aTotalMass =0.0;
775 Standard_Integer k=1;
776 for ( ; k <= comp.Length(); k++ ) {
777 TDF_Label lab = comp(k);
778 gp_XYZ aCenterGravity(0.0,0.0,0.0);
779 Standard_Real aMassVal =0.0;
780 if(GetMassProps(lab,aCenterGravity,aMassVal,thetol)) {
781 anArrCentres.SetValue(k,aCenterGravity);
782 anArrMass.SetValue(k,aMassVal);
783 aTotalMass += aMassVal;
787 Standard_Real x= 0.0, y =0.0, z= 0.0;
788 for( k=1; k <= anArrMass.Length(); k++) {
789 Standard_Real koef = anArrMass.Value(k)/aTotalMass;
790 x+= (anArrCentres.Value(k).X()*koef);
791 y+= (anArrCentres.Value(k).Y()*koef);
792 z+= (anArrCentres.Value(k).Z()*koef);
794 theMassVal = aTotalMass;
795 theCenterGravity.SetCoord(x,y,z);
799 return Standard_True;
803 //=======================================================================
804 //function : ShapeMassProps
806 //=======================================================================
808 static Standard_Integer ShapeMassProps (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
812 di << "Use: "<<argv[0]<<" DocName [deflection [Shape|Label] ]\n";
813 di << " If second argument is 0, standard CADCADE tool is used for\n";
814 di << " computation of volume and CG.\n";
815 di << " If second argument is not 0, it is treated as deflection\n";
816 di << " and computation is done by triangulations\n";
817 di << " If the second argument is negative, meshing is forced\n";
820 Handle(TDocStd_Document) Doc;
821 DDocStd::GetDocument(argv[1], Doc);
822 Standard_Real atol = Precision::Confusion();
824 atol = Draw::Atof(argv[2]);
825 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
826 Standard_Boolean wholeDoc = ( argc <4 );
827 TDF_LabelSequence seq;
830 TDF_Tool::Label(Doc->GetData(), argv[3], aLabel);
832 if ( aLabel.IsNull() ) {
833 aShape= DBRep::Get(argv[3]);
834 if ( !aShape.IsNull() ) {
835 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
836 aLabel = STool->FindShape(aShape);
839 if ( aLabel.IsNull() ) return 1;
840 seq.Append ( aLabel );
843 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
844 STool->GetShapes(seq);
847 // di << "Label Area defect Volume defect dX dY dZ Name\n";
849 gp_XYZ aCenterGravity(0.0,0.0,0.0);
850 Standard_Real aMassVal =0.0;
851 for ( Standard_Integer i=1; i <= seq.Length(); i++ ) {
852 TDF_Label aLabel = seq(i);
853 GetMassProps(aLabel,aCenterGravity,aMassVal,atol);
854 // if(GetMassProps(aLabel,aCenterGravity,aMassVal,atol))
856 TCollection_AsciiString str;
857 TDF_Tool::Entry ( aLabel, str );
859 di<<"Shape from label : "<<str.ToCString()<<"\n";
860 di<<"Mass = "<<aMassVal<<"\n";
861 di<<"CenterOfGravity X = "<<aCenterGravity.X()<<",Y = "<<aCenterGravity.Y()<<",Z = "<<aCenterGravity.Z()<<"\n";
865 // di<<"For one component density is absent\n";
866 di<<"Shape from label : "<<str.ToCString()<<" not have a mass\n";
873 //=======================================================================
874 //function : SetMaterial
876 //=======================================================================
878 static Standard_Integer SetMaterial (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
881 di<<"Use: "<<argv[0]<<" Doc {Label|Shape} name density(g/cu sm) \n";
884 Handle(TDocStd_Document) Doc;
885 DDocStd::GetDocument(argv[1], Doc);
886 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
889 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
891 Handle(XCAFDoc_MaterialTool) MatTool = XCAFDoc_DocumentTool::MaterialTool(Doc->Main());
893 MatTool->SetMaterial(aLabel, new TCollection_HAsciiString(argv[3]),
894 new TCollection_HAsciiString(""), Draw::Atof(argv[4]),
895 new TCollection_HAsciiString("density measure"),
896 new TCollection_HAsciiString("POSITIVE_RATIO_MEASURE"));
901 static Standard_Integer GetValidationProps(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
904 di<<"Use: "<<argv[0]<<" Doc {Label|Shape}\n";
907 Handle(TDocStd_Document) Doc;
908 DDocStd::GetDocument(argv[1], Doc);
909 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
910 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
911 TDF_LabelSequence aLabels;
913 STool->GetShapes (aLabels);
917 TopoDS_Shape aShape = DBRep::Get(argv[2]);
920 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
924 aLabel = STool->FindShape(aShape);
926 if( !aLabel.IsNull())
927 aLabels.Append(aLabel);
937 Standard_Integer nbProps[3];
938 Standard_Integer j =0;
939 for( ; j <= Centroid; j++)
941 Standard_Integer i = 1;
942 for( ; i <= aLabels.Length() ; i++)
944 TDF_Label aLabel = aLabels(i);
946 Standard_Real aProp[2];
952 gp_Pnt aP(Precision::Infinite(), Precision::Infinite(), Precision::Infinite());
953 XCAFDoc_Volume::Get(aLabel, aProp[Vol]);
954 XCAFDoc_Area::Get(aLabel, aProp[Area]);
956 Handle(XCAFDoc_Centroid) aCentroid = new (XCAFDoc_Centroid);
957 if (aLabel.FindAttribute (XCAFDoc_Centroid::GetID(), aCentroid))
958 XCAFDoc_Centroid::Get(aLabel, aP);
960 if(aProp[Vol] > 0 || aProp[Area] > 0 || !Precision::IsInfinite(aP.X()) )
962 TCollection_AsciiString str;
963 TDF_Tool::Entry ( aLabel, str );
966 for(j = 0 ; j <= Area; j++)
970 di<<(j == Vol ? "; Volume - " : "; Area - ")<<aProp[j];
975 if( !Precision::IsInfinite(aP.X()) )
977 di<< "; Centroid - "<< aP.X()<<" "<<aP.Y()<<" "<<aP.Z();
983 di<<"========================================================="<<"\n";
984 di<< "Number of the validation properties : ";
985 for( i = Vol; i <= Centroid; i++)
986 di<< ( i == Vol ? "Volume" : (i == Area ? "Area" : "Centroid"))<<" : "<<nbProps[i]<<" ; ";
992 //=======================================================================
993 //function : InitCommands
995 //=======================================================================
997 void XDEDRAW_Props::InitCommands(Draw_Interpretor& di)
999 static Standard_Boolean initactor = Standard_False;
1004 initactor = Standard_True;
1006 Standard_CString g = "XDE property's commands";
1008 di.Add ("XSetVolume","DocName {Label|Shape} volume \t: Seting volume to shape",
1009 __FILE__, SetVolume, g);
1011 di.Add ("XGetVolume","DocName {Shape|Label} \t: Getting volume of shape",
1012 __FILE__, GetVolume, g);
1014 di.Add ("XSetArea","DocName {Label|Shape} area \t: Seting area to shape",
1015 __FILE__, SetArea, g);
1017 di.Add ("XGetArea","DocName {Shape|Label} \t: Getting area of shape",
1018 __FILE__, GetArea, g);
1020 di.Add ("XSetCentroid","DocName {Label|Shape} x y z \t: Seting centroid to shape",
1021 __FILE__, SetCentroid, g);
1023 di.Add ("XGetCentroid","DocName {Shape|Label} \t: Getting centroid of shape ",
1024 __FILE__, GetCentroid, g);
1026 di.Add ("XSetProps","DocName {Shape|Label} [epsilon = 0.001] \t: Compute properties for shape ",
1027 __FILE__, SetProps, g);
1029 di.Add ("XCheckProps","DocName [ 0|deflection [Shape|Label] ]\t: Check properties recorded for shape ",
1030 __FILE__, CheckProps, g);
1031 di.Add ("XShapeVolume","Shape \t: Calculating volume of shape",
1032 __FILE__, ShapeVolume, g);
1033 di.Add ("XShapeMassProps","XShapeMassProps DocName [deflection [Shape|Label] ]\t: Get mass value and center of gravity for shape ",
1034 __FILE__,ShapeMassProps , g);
1035 di.Add ("XSetMaterial","Doc {Label|Shape} name density(g/cu sm) \t: Set material to shape given by Label",
1036 __FILE__, SetMaterial, g);
1037 di.Add ("XGetValProps","Doc {Label|Shape}",__FILE__, GetValidationProps, g);