1 // Created on: 2000-08-04
2 // Created by: Pavel TELKOV
3 // Copyright (c) 2000-2012 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
20 // OCC532 sln 24.07.2002. Add epsilon parameter to SetProps function
22 #include <XDEDRAW_Props.ixx>
25 #include <DDocStd.hxx>
27 #include <TCollection_AsciiString.hxx>
28 #include <TopoDS_Shape.hxx>
30 #include <GProp_GProps.hxx>
31 #include <BRepGProp.hxx>
33 #include <TDF_Tool.hxx>
34 #include <TDF_Label.hxx>
35 #include <TDF_LabelSequence.hxx>
36 #include <TDocStd_Document.hxx>
37 #include <TDataStd_Name.hxx>
39 #include <XCAFDoc_DocumentTool.hxx>
40 #include <XCAFDoc_ShapeTool.hxx>
41 #include <XCAFDoc_Area.hxx>
42 #include <XCAFDoc_Volume.hxx>
43 #include <XCAFDoc_Centroid.hxx>
44 #include <XCAFDoc_MaterialTool.hxx>
45 #include <XCAFDoc_Location.hxx>
46 #include <Precision.hxx>
47 #include <TNaming_NamedShape.hxx>
49 #include <Standard_ErrorHandler.hxx>
50 #include <Standard_Failure.hxx>
52 #include <BRepMesh.hxx>
53 #include <TColgp_Array1OfPnt.hxx>
54 #include <Poly_Triangulation.hxx>
55 #include <TopoDS_Compound.hxx>
56 #include <TopExp_Explorer.hxx>
57 #include <TopoDS_Face.hxx>
59 #include <BRep_Tool.hxx>
60 #include <BRepMesh_IncrementalMesh.hxx>
61 #include <TopoDS_Vertex.hxx>
62 #include <Standard_Stream.hxx>
64 #include <TColgp_Array1OfXYZ.hxx>
65 #include <TColStd_Array1OfReal.hxx>
66 #include <TDataStd_TreeNode.hxx>
67 #include <XCAFDoc.hxx>
68 #include <TCollection_HAsciiString.hxx>
71 // --------------------- VolumeFix Begin ---
73 //=======================================================================
76 //=======================================================================
77 static double TetraVol(gp_Pnt RefPoint, gp_Pnt Som1, gp_Pnt Som2, gp_Pnt Som3)
88 Line12=gp_Dir( gp_Vec(Som1, Som2));
89 gp_Vec v1(Som1, Som2);
90 gp_Vec v2(Som2, Som3);
92 Plane123=gp_Pln( Som1, gp_Dir( N ) );
94 catch(Standard_Failure) {return(0.);}
97 L1 = Som1.Distance(Som2);
98 L2 = gp_Lin(Som1, Line12).Distance(Som3);
99 L3 = Plane123.Distance(RefPoint);
101 curVolume = ((L1 * L2)/2) * (L3/3);
103 gp_Vec Rad(RefPoint, Som1);
112 //=======================================================================
113 //function : TetraCen
115 //=======================================================================
116 static gp_XYZ TetraCen(gp_Pnt RefPoint, gp_Pnt Som1, gp_Pnt Som2, gp_Pnt Som3)
118 gp_XYZ curCentr, plnPnt;
119 plnPnt = ( Som1.XYZ() + Som2.XYZ() + Som3.XYZ() )/3;
120 curCentr = plnPnt + (RefPoint.XYZ() - plnPnt)/4;
125 //=======================================================================
126 //function : CalculVolume
128 //=======================================================================
129 static Standard_Real CalculVolume(const TopoDS_Shape& So,
132 Standard_Boolean withForce,
133 Draw_Interpretor& di)
135 Standard_Real myVolume = 0, curVolume = 0;
136 gp_XYZ localCentroid(0,0,0), curCentroid(0,0,0);
137 Standard_Boolean haveVertex = Standard_False;
138 for (TopExp_Explorer ex(So,TopAbs_FACE) ; ex.More(); ex.Next())
140 TopoDS_Face F =TopoDS::Face(ex.Current());
143 for (TopExp_Explorer Vex(F, TopAbs_VERTEX); Vex.More(); Vex.Next() )
145 TopoDS_Vertex v = TopoDS::Vertex(Vex.Current());
146 if ( ! v.IsNull() ) {
147 aRefPoint = BRep_Tool::Pnt(v);
148 haveVertex = Standard_True;
153 Handle (Poly_Triangulation) facing = BRep_Tool::Triangulation(F,L);
154 if(facing.IsNull() || withForce)
156 // BRepMesh::Mesh(F, tol);
157 BRepMesh_IncrementalMesh MESH(F, tol);
159 facing = BRep_Tool::Triangulation(F,L);
162 TColgp_Array1OfPnt tab(1,(facing->NbNodes()));
163 tab = facing->Nodes();
164 Poly_Array1OfTriangle tri(1,facing->NbTriangles());
165 tri = facing->Triangles();
166 for (Standard_Integer i=1;i<=(facing->NbTriangles());i++)
169 Poly_Triangle trian = tri.Value(i);
170 Standard_Integer index1,index2,index3;//M,N;
171 if( F.Orientation() == TopAbs_REVERSED )
172 trian.Get(index1,index3,index2);
174 trian.Get(index1,index2,index3);
175 curVolume = TetraVol(aRefPoint, tab.Value(index1),
176 tab.Value(index2), tab.Value(index3));
177 myVolume += curVolume;
178 curCentroid = TetraCen(aRefPoint, tab.Value(index1),
179 tab.Value(index2), tab.Value(index3));
181 localCentroid = localCentroid + curCentroid*curVolume;
185 localCentroid = localCentroid * (1./ myVolume);
187 di << "Centroid:" << "\n";
188 di << "X=\t" << localCentroid.X() << "\n";
189 di << "Y=\t" << localCentroid.Y() << "\n";
190 di << "Z=\t" << localCentroid.Z() << "\n";
195 // --------------------- VolumeFix End ---
198 //=======================================================================
199 // Section: Work with val props
200 //=======================================================================
202 //=======================================================================
203 //function : SetProps
205 //=======================================================================
207 static Standard_Integer SetProps (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
210 di<<"Use: "<<argv[0]<<" DocName {Shape|Label} [epsilon = 0.001]"<<"\n";
213 Handle(TDocStd_Document) Doc;
214 DDocStd::GetDocument(argv[1], Doc);
215 if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
217 Standard_Real Vres, Ares;
220 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
222 if ( aLabel.IsNull() ) {
223 aShape= DBRep::Get(argv[2]);
224 if ( !aShape.IsNull() ) {
225 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
226 aLabel = STool->FindShape(aShape);
230 aShape = XCAFDoc_ShapeTool::GetShape ( aLabel );
232 if ( !aLabel.IsNull() ) {
236 if(argc > 3 ) anEps = atof(argv[3]);
240 BRepGProp::VolumeProperties(aShape,G,anEps,Standard_True);
242 Handle(XCAFDoc_Volume) aVolume = new XCAFDoc_Volume;
243 if (!aLabel.FindAttribute (XCAFDoc_Volume::GetID(), aVolume)) aLabel.AddAttribute(aVolume);
246 gp_Pnt aPoint = G.CentreOfMass();
247 Handle(XCAFDoc_Centroid) aCentroid = new XCAFDoc_Centroid;
248 if (!aLabel.FindAttribute (XCAFDoc_Centroid::GetID(), aCentroid)) aLabel.AddAttribute(aCentroid);
249 aCentroid->Set(aPoint);
251 BRepGProp::SurfaceProperties(aShape,G,anEps);
253 Handle(XCAFDoc_Area) aArea = new XCAFDoc_Area;
254 if (!aLabel.FindAttribute (XCAFDoc_Area::GetID(), aArea)) aLabel.AddAttribute(aArea);
257 di << argv[2] << ": Volume = " << Vres << ", Area = " << Ares <<
258 ", Centroid is (" << aPoint.X() << ", " << aPoint.Y() << ", " << aPoint.Z() << ")";
264 //=======================================================================
265 //function : SetVolume
267 //=======================================================================
269 static Standard_Integer SetVolume (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
272 di<<"Use: "<<argv[0]<<" DocName {Label|Shape} volume"<<"\n";
275 Handle(TDocStd_Document) Doc;
276 DDocStd::GetDocument(argv[1], Doc);
277 if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
279 Standard_Real res=0.;
282 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
283 if ( aLabel.IsNull() ) {
284 TopoDS_Shape aShape= DBRep::Get(argv[2]);
285 if ( !aShape.IsNull() ) {
286 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
287 aLabel = STool->FindShape(aShape);
290 if ( !aLabel.IsNull() ) {
292 Handle(XCAFDoc_Volume) aVolume = new XCAFDoc_Volume;
293 if (!aLabel.FindAttribute (XCAFDoc_Volume::GetID(), aVolume)) aLabel.AddAttribute(aVolume);
302 //=======================================================================
305 //=======================================================================
307 static Standard_Integer SetArea (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
310 di<<"Use: "<<argv[0]<<" DocName {Label|Shape} area"<<"\n";
313 Handle(TDocStd_Document) Doc;
314 DDocStd::GetDocument(argv[1], Doc);
315 if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
317 Standard_Real res=0.;
320 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
321 if ( aLabel.IsNull() ) {
322 TopoDS_Shape aShape= DBRep::Get(argv[2]);
323 if ( !aShape.IsNull() ) {
324 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
325 aLabel = STool->FindShape(aShape);
328 if ( !aLabel.IsNull() ) {
330 Handle(XCAFDoc_Area) aArea = new XCAFDoc_Area;
331 if (!aLabel.FindAttribute (XCAFDoc_Area::GetID(), aArea)) aLabel.AddAttribute(aArea);
339 //=======================================================================
340 //function : SetCentroid
342 //=======================================================================
344 static Standard_Integer SetCentroid (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
347 di<<"Use: "<<argv[0]<<" DocName {Label|Shape} x y z"<<"\n";
350 Handle(TDocStd_Document) Doc;
351 DDocStd::GetDocument(argv[1], Doc);
352 if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
357 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
358 if ( aLabel.IsNull() ) {
359 TopoDS_Shape aShape= DBRep::Get(argv[2]);
360 if ( !aShape.IsNull() ) {
361 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
362 aLabel = STool->FindShape(aShape);
365 if ( !aLabel.IsNull() ) {
366 aPoint.SetX(atof(argv[3]));
367 aPoint.SetY(atof(argv[4]));
368 aPoint.SetZ(atof(argv[5]));
369 Handle(XCAFDoc_Centroid) aCentroid = new XCAFDoc_Centroid;
370 if (!aLabel.FindAttribute (XCAFDoc_Centroid::GetID(), aCentroid)) aLabel.AddAttribute(aCentroid);
371 aCentroid->Set(aPoint);
372 di << atof(argv[3])<<" "<<atof(argv[4])<<" "<<atof(argv[5]);
378 //=======================================================================
379 //function : GetVolume
381 //=======================================================================
383 static Standard_Integer GetVolume (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
386 di<<"Use: "<<argv[0]<<" DocName {Shape|Label}"<<"\n";
389 Handle(TDocStd_Document) Doc;
390 DDocStd::GetDocument(argv[1], Doc);
391 if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
394 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
395 if ( aLabel.IsNull() ) {
396 TopoDS_Shape aShape= DBRep::Get(argv[2]);
397 if ( !aShape.IsNull() ) {
398 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
399 aLabel = STool->FindShape(aShape);
402 if ( !aLabel.IsNull() ) {
403 // Handle(XCAFDoc_Volume) aVolume = new (XCAFDoc_Volume);
404 // if (aLabel.FindAttribute (XCAFDoc_Volume::GetID(), aVolume)) di << aVolume->Get();
407 if(XCAFDoc_Volume::Get(aLabel, aVol))
414 //=======================================================================
417 //=======================================================================
419 static Standard_Integer GetArea (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
422 di<<"Use: "<<argv[0]<<" DocName {Shape|Label}"<<"\n";
425 Handle(TDocStd_Document) Doc;
426 DDocStd::GetDocument(argv[1], Doc);
427 if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
430 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
431 if ( aLabel.IsNull() ) {
432 TopoDS_Shape aShape= DBRep::Get(argv[2]);
433 if ( !aShape.IsNull() ) {
434 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
435 aLabel = STool->FindShape(aShape);
438 if ( !aLabel.IsNull() ) {
439 // Handle(XCAFDoc_Area) aArea = new (XCAFDoc_Area);
440 // if (aLabel.FindAttribute (XCAFDoc_Area::GetID(), aArea)) di << aArea->Get();
443 if(XCAFDoc_Area::Get(aLabel, anA))
450 //=======================================================================
451 //function : GetCentroid
453 //=======================================================================
455 static Standard_Integer GetCentroid (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
458 di<<"Use: "<<argv[0]<<" DocName {Shape|Label} "<<"\n";
461 Handle(TDocStd_Document) Doc;
462 DDocStd::GetDocument(argv[1], Doc);
463 if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
468 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
469 if ( aLabel.IsNull() ) {
470 TopoDS_Shape aShape= DBRep::Get(argv[2]);
471 if ( !aShape.IsNull() ) {
472 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
473 aLabel = STool->FindShape(aShape);
476 if ( !aLabel.IsNull() ) {
477 Handle(XCAFDoc_Centroid) aCentroid = new (XCAFDoc_Centroid);
478 if (aLabel.FindAttribute (XCAFDoc_Centroid::GetID(), aCentroid)) {
479 // aPoint = aCentroid->Get();
480 // di << aPoint.X()<<" "<<aPoint.Y()<<" "<<aPoint.Z();
482 if(XCAFDoc_Centroid::Get(aLabel, aPoint))
483 di << aPoint.X()<<" "<<aPoint.Y()<<" "<<aPoint.Z();
490 //=======================================================================
493 //=======================================================================
495 static inline Standard_Real doround (Standard_Real val, Standard_Real low)
497 return Abs ( val ) < low ? 0. : val;
501 //=======================================================================
502 //function : CheckProps
504 //=======================================================================
506 static Standard_Integer CheckProps (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
509 di << "Use: "<<argv[0]<<" DocName [ 0|deflection [Shape|Label] ]"<<"\n";
510 di << " If second argument is 0, standard CADCADE tool is used for" << "\n";
511 di << " computation of volume and CG." << "\n";
512 di << " If second argument is not 0, it is treated as deflection" << "\n";
513 di << " and computation is done by triangulations" << "\n";
514 di << " If the second argument is negative, meshing is forced" << "\n";
517 Handle(TDocStd_Document) Doc;
518 DDocStd::GetDocument(argv[1], Doc);
519 if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
520 Standard_Boolean withVolFix = Standard_False;
521 if ( argc >2 && atof(argv[2]) != 0 ) withVolFix = Standard_True;
522 Standard_Boolean wholeDoc = ( argc <4 );
523 TDF_LabelSequence seq;
526 TDF_Tool::Label(Doc->GetData(), argv[3], aLabel);
528 if ( aLabel.IsNull() ) {
529 aShape= DBRep::Get(argv[3]);
530 if ( !aShape.IsNull() ) {
531 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
532 aLabel = STool->FindShape(aShape);
535 if ( aLabel.IsNull() ) return 1;
536 seq.Append ( aLabel );
539 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
540 STool->GetShapes(seq);
543 di << "Label Area defect Volume defect dX dY dZ Name" << "\n";
545 for ( Standard_Integer i=1; i <= seq.Length(); i++ ) {
546 TDF_Label aLabel = seq(i);
548 // add instance labels to sequence to process them as well
549 if ( XCAFDoc_ShapeTool::IsAssembly ( aLabel ) ) {
550 TDF_LabelSequence comp;
551 XCAFDoc_ShapeTool::GetComponents ( aLabel, comp );
552 Standard_Integer m=i;
553 for ( Standard_Integer k=1; k <= comp.Length(); k++ ) {
554 TDF_Label lab = comp(k);
555 Handle(XCAFDoc_Volume) aVolume;
556 Handle(XCAFDoc_Area) aArea;
557 Handle(XCAFDoc_Centroid) aCentroid;
558 if ( ! lab.FindAttribute (XCAFDoc_Volume::GetID(), aVolume) &&
559 ! lab.FindAttribute (XCAFDoc_Area::GetID(), aArea) &&
560 ! lab.FindAttribute (XCAFDoc_Centroid::GetID(), aCentroid) ) continue;
561 seq.InsertAfter ( m++, lab );
565 TCollection_AsciiString str;
566 TDF_Tool::Entry ( aLabel, str );
567 //printf ( "%s%-12.12s", ( wholeDoc ? "" : "Label " ), str.ToCString() );
570 sprintf (string1, "%s%-12.12s", ( wholeDoc ? "" : "Label " ), str.ToCString() );
572 Handle(TDataStd_Name) N;
573 if ( aLabel.FindAttribute ( TDataStd_Name::GetID(), N ) && ! wholeDoc ) {
574 TCollection_AsciiString name(N->Get(), '?');
575 di << " \"" << name.ToCString() << "\"";
577 if ( ! wholeDoc ) di << "\n";
579 Handle(XCAFDoc_Volume) aVolume;
580 Handle(XCAFDoc_Area) aArea;
581 Handle(XCAFDoc_Centroid) aCentroid;
582 aLabel.FindAttribute (XCAFDoc_Volume::GetID(), aVolume);
583 aLabel.FindAttribute (XCAFDoc_Area::GetID(), aArea);
584 aLabel.FindAttribute (XCAFDoc_Centroid::GetID(), aCentroid);
587 TopoDS_Shape aShape = XCAFDoc_ShapeTool::GetShape ( aLabel );
588 if ( ! aArea.IsNull() ) {
591 BRepGProp::SurfaceProperties(aShape,G,0.001);
592 //printf ("%s%9.1f (%3d%%)%s", ( wholeDoc ? "" : " Area defect: " ),
593 // aArea->Get() - G.Mass(),
594 // (Standard_Integer)( Abs ( G.Mass() ) > 1e-10 ? 100. * ( aArea->Get() - G.Mass() ) / G.Mass() : 999. ),
595 // ( wholeDoc ? "" : "\n" ));
597 sprintf (string2, "%s%9.1f (%3d%%)%s", ( wholeDoc ? "" : " Area defect: " ),
598 aArea->Get() - G.Mass(),
599 (Standard_Integer)( Abs ( G.Mass() ) > 1e-10 ? 100. * ( aArea->Get() - G.Mass() ) / G.Mass() : 999. ),
600 ( wholeDoc ? "" : "\n" ));
603 catch (Standard_Failure) {
604 //printf ( "%-16.16s", "exception" );
606 sprintf (string3, "%-16.16s", "exception" );
610 else if ( wholeDoc ) {
611 //printf ( "%16.16s", "" );
613 sprintf (string4, "%16.16s", "" );
617 if ( ! aVolume.IsNull() || ! aCentroid.IsNull() ) {
620 // Added for check Volume. PTV 08 Nov 2000.
621 Standard_Real localVolume;
624 Standard_Real tol = atof(argv[2]);
625 Standard_Boolean withForce = Standard_False;
627 withForce = Standard_True;
630 localVolume = CalculVolume(aShape, pcg, tol, withForce, di);
633 BRepGProp::VolumeProperties(aShape,G,0.001,Standard_True);
634 localVolume = G.Mass();
635 pcg = G.CentreOfMass();
638 if ( ! aVolume.IsNull() ) {
639 //printf ("%s%9.1f (%3d%%)%s", ( wholeDoc ? "" : " Volume defect: " ),
640 // aVolume->Get() - localVolume,
641 // (Standard_Integer)( Abs ( localVolume ) > 1e-10 ? 100. * ( aVolume->Get() - localVolume ) / localVolume : 999. ),
642 // ( wholeDoc ? "" : "\n" ));
644 sprintf (string5, "%s%9.1f (%3d%%)%s", ( wholeDoc ? "" : " Volume defect: " ),
645 aVolume->Get() - localVolume,
646 (Standard_Integer)( Abs ( localVolume ) > 1e-10 ? 100. * ( aVolume->Get() - localVolume ) / localVolume : 999. ),
647 ( wholeDoc ? "" : "\n" ));
650 else if ( wholeDoc ) {
651 //printf ( "%16.16s", "" );
653 sprintf (string6, "%16.16s", "" );
657 if ( ! aCentroid.IsNull() ) {
658 gp_Pnt p = aCentroid->Get();
661 //printf ( " %7.2f %7.2f %7.2f",
662 // p.X() - pcg.X(), p.Y() - pcg.Y(), p.Z() - pcg.Z() );
663 sprintf (string7, " %7.2f %7.2f %7.2f",
664 p.X() - pcg.X(), p.Y() - pcg.Y(), p.Z() - pcg.Z() );
666 //printf ( " CG defect: dX=%.3f, dY=%.3f, dZ=%.3f\n",
667 // p.X() - pcg.X(), p.Y() - pcg.Y(), p.Z() - pcg.Z() );
668 sprintf (string7, " CG defect: dX=%.3f, dY=%.3f, dZ=%.3f\n",
669 p.X() - pcg.X(), p.Y() - pcg.Y(), p.Z() - pcg.Z() );
673 else if ( wholeDoc ) {
674 //printf ( "%24.24s", "" );
676 sprintf (string8, "%24.24s", "" );
680 catch (Standard_Failure) {
681 //printf ( "%40.40s", "exception" );
683 sprintf (string9, "%40.40s", "exception" );
688 di << Standard_Failure::Caught()->GetMessageString();
689 di<<" ** Skip"<<"\n";
693 else if ( wholeDoc ) {
694 //printf ( "%40.40s", "" );
696 sprintf (string10, "%40.40s", "" );
702 if ( ! N.IsNull() ) {
703 TCollection_AsciiString name(N->Get(),'?');
704 di << " \"" << name.ToCString() << "\"";
713 //=======================================================================
714 //function : ShapeVolume
716 //=======================================================================
718 static Standard_Integer ShapeVolume (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
721 di<<"Use: "<<argv[0]<<" Shape deflection "<<"\n";
724 TopoDS_Shape aShape = DBRep::Get(argv[1]);
725 if (aShape.IsNull()) return 1;
726 gp_Pnt aPoint(0,0,0);
727 Standard_Real localVolume;
728 Standard_Real tol = atof(argv[2]);
729 Standard_Boolean withForce = Standard_False;
731 withForce = Standard_True;
734 localVolume = CalculVolume(aShape, aPoint, tol, withForce, di);
735 //cout << "Volume : " << setw(15) << localVolume << "\n" << endl;
736 Standard_SStream aSStream;
737 aSStream << "Volume : " << setw(15) << localVolume << "\n";
743 //=======================================================================
744 //function : GetMassProps
745 //purpose : auxilary for ShapeMassProps
746 //=======================================================================
748 static Standard_Boolean GetMassProps(const TDF_Label& aLabel, gp_XYZ& theCenterGravity,
749 Standard_Real& theMassVal, const Standard_Real thetol)
751 Standard_Real aDensity = XCAFDoc_MaterialTool::GetDensityForShape(aLabel);
754 TopoDS_Shape aShape = XCAFDoc_ShapeTool::GetShape(aLabel);
756 BRepGProp::VolumeProperties(aShape,G,0.001,Standard_True);
757 Standard_Real localVolume = G.Mass();
758 theMassVal = aDensity*localVolume;
759 theCenterGravity = G.CentreOfMass().XYZ();
760 return Standard_True;
764 Handle(TNaming_NamedShape) NS;
765 if(aLabel.FindAttribute(TNaming_NamedShape::GetID(),NS)) {
766 //S = TNaming_Tool::GetShape(NS);
767 TopoDS_Shape aSh = NS->Get();
768 if(aSh.ShapeType()==TopAbs_SOLID) return Standard_False;
771 //TopoDS_Shape aSh = XCAFDoc_ShapeTool::GetShape(aLabel);
772 //if(aSh.ShapeType()==TopAbs_SOLID) return Standard_False;
774 Handle(TDataStd_TreeNode) Node;
775 if( aLabel.FindAttribute(XCAFDoc::ShapeRefGUID(),Node) && Node->HasFather() ) {
776 TDF_Label SubL = Node->Father()->Label();
777 if(GetMassProps(SubL,theCenterGravity,theMassVal,thetol)) {
778 Handle(XCAFDoc_Location) LocationAttribute;
779 if(aLabel.FindAttribute(XCAFDoc_Location::GetID(),LocationAttribute)) {
780 gp_XYZ tmp = LocationAttribute->Get().Transformation().TranslationPart();
781 theCenterGravity += tmp;
783 return Standard_True;
786 return Standard_False;
789 // calculate for components
790 TDF_LabelSequence comp;
791 XCAFDoc_ShapeTool::GetComponents ( aLabel, comp );
793 return Standard_False;
795 TColgp_Array1OfXYZ anArrCentres(1,comp.Length());
796 TColStd_Array1OfReal anArrMass(1,comp.Length());
798 Standard_Real aTotalMass =0.0;
799 Standard_Integer k=1;
800 for ( ; k <= comp.Length(); k++ ) {
801 TDF_Label lab = comp(k);
802 gp_XYZ aCenterGravity(0.0,0.0,0.0);
803 Standard_Real aMassVal =0.0;
804 if(GetMassProps(lab,aCenterGravity,aMassVal,thetol)) {
805 anArrCentres.SetValue(k,aCenterGravity);
806 anArrMass.SetValue(k,aMassVal);
807 aTotalMass += aMassVal;
811 Standard_Real x= 0.0, y =0.0, z= 0.0;
812 for( k=1; k <= anArrMass.Length(); k++) {
813 Standard_Real koef = anArrMass.Value(k)/aTotalMass;
814 x+= (anArrCentres.Value(k).X()*koef);
815 y+= (anArrCentres.Value(k).Y()*koef);
816 z+= (anArrCentres.Value(k).Z()*koef);
818 theMassVal = aTotalMass;
819 theCenterGravity.SetCoord(x,y,z);
823 return Standard_True;
827 //=======================================================================
828 //function : ShapeMassProps
830 //=======================================================================
832 static Standard_Integer ShapeMassProps (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
836 di << "Use: "<<argv[0]<<" DocName [deflection [Shape|Label] ]"<<"\n";
837 di << " If second argument is 0, standard CADCADE tool is used for" << "\n";
838 di << " computation of volume and CG." << "\n";
839 di << " If second argument is not 0, it is treated as deflection" << "\n";
840 di << " and computation is done by triangulations" << "\n";
841 di << " If the second argument is negative, meshing is forced" << "\n";
844 Handle(TDocStd_Document) Doc;
845 DDocStd::GetDocument(argv[1], Doc);
846 Standard_Real atol = Precision::Confusion();
848 atol = atof(argv[2]);
849 if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
850 Standard_Boolean wholeDoc = ( argc <4 );
851 TDF_LabelSequence seq;
854 TDF_Tool::Label(Doc->GetData(), argv[3], aLabel);
856 if ( aLabel.IsNull() ) {
857 aShape= DBRep::Get(argv[3]);
858 if ( !aShape.IsNull() ) {
859 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
860 aLabel = STool->FindShape(aShape);
863 if ( aLabel.IsNull() ) return 1;
864 seq.Append ( aLabel );
867 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
868 STool->GetShapes(seq);
871 // di << "Label Area defect Volume defect dX dY dZ Name" << "\n";
873 gp_XYZ aCenterGravity(0.0,0.0,0.0);
874 Standard_Real aMassVal =0.0;
875 for ( Standard_Integer i=1; i <= seq.Length(); i++ ) {
876 TDF_Label aLabel = seq(i);
877 GetMassProps(aLabel,aCenterGravity,aMassVal,atol);
878 // if(GetMassProps(aLabel,aCenterGravity,aMassVal,atol))
880 TCollection_AsciiString str;
881 TDF_Tool::Entry ( aLabel, str );
883 di<<"Shape from label : "<<str.ToCString()<<"\n";
884 di<<"Mass = "<<aMassVal<<"\n";
885 di<<"CenterOfGravity X = "<<aCenterGravity.X()<<",Y = "<<aCenterGravity.Y()<<",Z = "<<aCenterGravity.Z()<<"\n";
889 // di<<"For one component density is absent"<<"\n";
890 di<<"Shape from label : "<<str.ToCString()<<" not have a mass"<<"\n";
897 //=======================================================================
898 //function : SetMaterial
900 //=======================================================================
902 static Standard_Integer SetMaterial (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
905 di<<"Use: "<<argv[0]<<" Doc {Label|Shape} name density(g/cu sm) "<<"\n";
908 Handle(TDocStd_Document) Doc;
909 DDocStd::GetDocument(argv[1], Doc);
910 if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
913 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
915 Handle(XCAFDoc_MaterialTool) MatTool = XCAFDoc_DocumentTool::MaterialTool(Doc->Main());
917 MatTool->SetMaterial(aLabel, new TCollection_HAsciiString(argv[3]),
918 new TCollection_HAsciiString(""), atof(argv[4]),
919 new TCollection_HAsciiString("density measure"),
920 new TCollection_HAsciiString("POSITIVE_RATIO_MEASURE"));
926 //=======================================================================
927 //function : InitCommands
929 //=======================================================================
931 void XDEDRAW_Props::InitCommands(Draw_Interpretor& di)
934 static Standard_Boolean initactor = Standard_False;
935 if (initactor) return; initactor = Standard_True;
937 Standard_CString g = "XDE property's commands";
939 di.Add ("XSetVolume","DocName {Label|Shape} volume \t: Seting volume to shape",
940 __FILE__, SetVolume, g);
942 di.Add ("XGetVolume","DocName {Shape|Label} \t: Getting volume of shape",
943 __FILE__, GetVolume, g);
945 di.Add ("XSetArea","DocName {Label|Shape} area \t: Seting area to shape",
946 __FILE__, SetArea, g);
948 di.Add ("XGetArea","DocName {Shape|Label} \t: Getting area of shape",
949 __FILE__, GetArea, g);
951 di.Add ("XSetCentroid","DocName {Label|Shape} x y z \t: Seting centroid to shape",
952 __FILE__, SetCentroid, g);
954 di.Add ("XGetCentroid","DocName {Shape|Label} \t: Getting centroid of shape ",
955 __FILE__, GetCentroid, g);
957 di.Add ("XSetProps","DocName {Shape|Label} [epsilon = 0.001] \t: Compute properties for shape ",
958 __FILE__, SetProps, g);
960 di.Add ("XCheckProps","DocName [ 0|deflection [Shape|Label] ]\t: Check properties recorded for shape ",
961 __FILE__, CheckProps, g);
962 di.Add ("XShapeVolume","Shape \t: Calculating volume of shape",
963 __FILE__, ShapeVolume, g);
964 di.Add ("XShapeMassProps","XShapeMassProps DocName [deflection [Shape|Label] ]\t: Get mass value and center of gravity for shape ",
965 __FILE__,ShapeMassProps , g);
966 di.Add ("XSetMaterial","Doc {Label|Shape} name density(g/cu sm) \t: Set material to shape given by Label",
967 __FILE__, SetMaterial, g);