1 // Copyright (C) 2007-2019 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 \class GEOM_AISShape GEOM_AISShape.hxx
28 #include "GEOM_AISShape.hxx"
29 #include "GEOM_AISVector.hxx"
31 #include <GEOMUtils.hxx>
33 // Open CASCADE Includes
34 #include <AIS_InteractiveContext.hxx>
35 #include <BRepAdaptor_Surface.hxx>
36 #include <BRep_Tool.hxx>
37 #include <GCPnts_AbscissaPoint.hxx>
38 #include <GeomAdaptor_Curve.hxx>
39 #include <Graphic3d_ArrayOfPoints.hxx>
40 #include <Graphic3d_AspectFillArea3d.hxx>
41 #include <Graphic3d_AspectLine3d.hxx>
42 #include <Graphic3d_AspectMarker3d.hxx>
43 #include <Graphic3d_AspectText3d.hxx>
45 #include <Prs3d_Arrow.hxx>
46 #include <Prs3d_IsoAspect.hxx>
47 #include <Prs3d_ShadingAspect.hxx>
48 #include <SelectBasics_SensitiveEntity.hxx>
49 #include <SelectMgr_EntityOwner.hxx>
50 #include <SelectMgr_IndexedMapOfOwner.hxx>
51 #include <SelectMgr_Selection.hxx>
52 #include <StdPrs_ShadedShape.hxx>
53 #include <StdPrs_ToolTriangulatedShape.hxx>
54 #include <StdSelect_BRepOwner.hxx>
55 #include <StdSelect_DisplayMode.hxx>
56 #include <TColStd_IndexedMapOfInteger.hxx>
57 #include <TColStd_ListIteratorOfListOfInteger.hxx>
58 #include <TColStd_ListOfInteger.hxx>
60 #include <TopExp_Explorer.hxx>
61 #include <TopTools_IndexedMapOfShape.hxx>
63 #include <TopoDS_Edge.hxx>
64 #include <TopoDS_Shape.hxx>
65 #include <TopoDS_Vertex.hxx>
66 #include <V3d_View.hxx>
70 #include <Prs3d_VertexDrawMode.hxx>
71 #include <StdPrs_WFShape.hxx>
73 #include <Basics_OCCTVersion.hxx>
75 #include <SalomeApp_Tools.h>
76 #include <SUIT_Session.h>
77 #include <SUIT_ResourceMgr.h>
79 IMPLEMENT_STANDARD_RTTIEXT(GEOM_AISShape, SALOME_AISShape)
81 GEOM_AISShape::TopLevelDispMode GEOM_AISShape::myTopLevelDm = GEOM_AISShape::TopKeepCurrent;
82 Quantity_Color GEOM_AISShape::myTopLevelColor;
85 static void getEntityOwners( const Handle(AIS_InteractiveObject)& theObj,
86 const Handle(AIS_InteractiveContext)& theIC,
87 SelectMgr_IndexedMapOfOwner& theMap )
89 if ( theObj.IsNull() || theIC.IsNull() )
92 TColStd_ListOfInteger modes;
93 theIC->ActivatedModes( theObj, modes );
95 TColStd_ListIteratorOfListOfInteger itr( modes );
96 for (; itr.More(); itr.Next() ) {
98 if ( !theObj->HasSelection( m ) )
101 Handle(SelectMgr_Selection) sel = theObj->Selection( m );
102 const NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>& selected = sel->Entities();
103 for ( NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator selIter( selected );
104 selIter.More(); selIter.Next() ) {
105 const Handle(SelectMgr_SensitiveEntity) aHSenEntity = selIter.Value();
106 if( aHSenEntity.IsNull() )
109 Handle(SelectBasics_SensitiveEntity) entity = aHSenEntity->BaseSensitive();
110 if ( entity.IsNull() )
112 Handle(SelectMgr_EntityOwner) owner =
113 Handle(SelectMgr_EntityOwner)::DownCast(entity->OwnerId());
114 if ( !owner.IsNull() )
120 static void indicesToOwners( const TColStd_IndexedMapOfInteger& aIndexMap,
121 const TopoDS_Shape& aMainShape,
122 const SelectMgr_IndexedMapOfOwner& anAllMap,
123 SelectMgr_IndexedMapOfOwner& aToHiliteMap )
125 TopTools_IndexedMapOfShape aMapOfShapes;
126 TopExp::MapShapes(aMainShape, aMapOfShapes);
128 for ( Standard_Integer i = 1, n = anAllMap.Extent(); i <= n; i++ ) {
129 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(anAllMap( i ));
130 if ( anOwner.IsNull() || !anOwner->HasShape() )
133 const TopoDS_Shape& aSubShape = anOwner->Shape();
134 Standard_Integer aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
135 if ( !aSubShapeId || !aIndexMap.Contains( aSubShapeId ) )
138 if ( !aToHiliteMap.Contains( anOwner ) )
139 aToHiliteMap.Add( anOwner );
143 GEOM_AISShape::GEOM_AISShape(const TopoDS_Shape& shape,
144 const Standard_CString aName)
145 : SALOME_AISShape(shape),
147 myDisplayVectors(false),
148 myDisplayVertices(false),
149 myFieldDataType(GEOM::FDT_Double),
151 myFieldStepRangeMin(0),
152 myFieldStepRangeMax(0)
154 //rnv: Commented to avoid bug with local selection
155 //SetHilightMode( CustomHighlight ); // override setting the mode to 0 inside AIS_Shape constructor
157 myShadingColor = Quantity_Color( Quantity_NOC_GOLDENROD );
158 myPrevDisplayMode = 0;
160 myEdgesInShadingColor = Quantity_Color( Quantity_NOC_GOLDENROD );
162 myTopLevel = Standard_False;
163 Graphic3d_MaterialAspect aMatAspect;
164 if ( !HasMaterial() ) {
165 #if OCC_VERSION_LARGE >= 0x07040000
166 aMatAspect.SetAmbientColor( Quantity_Color (Graphic3d_Vec3( 0.5f ) ) );
167 aMatAspect.SetDiffuseColor( Quantity_Color (Graphic3d_Vec3( 0.5f ) ) );
168 aMatAspect.SetEmissiveColor( Quantity_Color (Graphic3d_Vec3( 0.5f ) ) );
169 aMatAspect.SetSpecularColor( Quantity_Color (Graphic3d_Vec3( 0.5f ) ) );
171 aMatAspect.SetAmbient( 0.5 );
172 aMatAspect.SetDiffuse( 0.5 );
173 aMatAspect.SetEmissive( 0.5 );
174 aMatAspect.SetSpecular( 0.5 );
176 aMatAspect.SetShininess(0.5 );
177 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(aMatAspect);
178 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(aMatAspect);
182 GEOM_AISShape::~GEOM_AISShape()
186 void GEOM_AISShape::setIO(const Handle(SALOME_InteractiveObject)& io)
191 Handle(SALOME_InteractiveObject) GEOM_AISShape::getIO()
193 Handle(SALOME_InteractiveObject) IO;
194 if ( !GetOwner().IsNull() )
195 IO = Handle(SALOME_InteractiveObject)::DownCast( GetOwner() );
199 Standard_Boolean GEOM_AISShape::hasIO()
201 return !getIO().IsNull();
204 void GEOM_AISShape::setName(const Standard_CString aName)
208 Handle(SALOME_InteractiveObject) IO = getIO();
213 Standard_CString GEOM_AISShape::getName()
215 return myName.ToCString();
218 Standard_Boolean GEOM_AISShape::AcceptDisplayMode(const Standard_Integer theMode) const
220 return theMode >= Wireframe && theMode <= CustomHighlight;
223 void GEOM_AISShape::Compute(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
224 const Handle(Prs3d_Presentation)& aPrs,
225 const Standard_Integer aMode)
227 if (IsInfinite()) aPrs->SetInfiniteState(Standard_True); //pas de prise en compte lors du FITALL
229 Handle(AIS_InteractiveContext) anIC = GetContext();
230 // AKL: use old behavior to avoid keeping object's wireframe
231 // if to change shape properties (for example: 'Clear Top Level State','Color', 'Isos')
232 // calling popup menu over(!) the shape in OCC viewer.
233 anIC->SetToHilightSelected( false );
235 bool anIsField = !myFieldStepData.isEmpty();
236 bool anIsColorField = anIsField && myFieldDataType != GEOM::FDT_String;
237 bool anIsTextField = anIsField && myFieldDataType == GEOM::FDT_String;
239 if (isShowVertices())
240 myDrawer->SetVertexDrawMode(Prs3d_VDM_All);
242 // StdSelect_DisplayMode d = (StdSelect_DisplayMode) aMode;
243 bool isTopLev = isTopLevel() && switchTopLevel();
246 // Begin 0023271: [CEA 1823] Deflection Coefficient not taken into account in wireframe
248 StdPrs_ToolTriangulatedShape::ClearOnOwnDeflectionChange (myshape, myDrawer, Standard_True);
250 // After this call if type of deflection is relative
251 // computed deflection coefficient is stored as absolute.
252 Prs3d::GetDeflection (myshape, myDrawer);
255 case CustomHighlight:
258 SetColor(topLevelColor());
259 Handle(Prs3d_LineAspect) anAspect = Attributes()->WireAspect();
260 anAspect->SetColor( topLevelColor() );
261 Attributes()->SetWireAspect( anAspect );
263 if( !isTopLev && anIsColorField && myFieldDimension == 1 )
264 drawField( aPrs, false, aMode == CustomHighlight );
266 StdPrs_WFShape::Add(aPrs,myshape,myDrawer);
271 shadingMode(aPresentationManager, aPrs, aMode);
274 case ShadingWithEdges:
276 myDrawer->SetFaceBoundaryDraw( Standard_True );
277 shadingMode(aPresentationManager, aPrs, Shading);
278 if( anIsColorField && myFieldDimension == 1 ) {
279 myDrawer->SetFaceBoundaryDraw( Standard_False );
286 shadingMode(aPresentationManager, aPrs, Shading);
292 const bool isVector = IsKind(STANDARD_TYPE(GEOM_AISVector));
293 TopExp_Explorer Exp ( myshape, TopAbs_EDGE );
294 for ( ; Exp.More(); Exp.Next() ) {
295 TopoDS_Vertex aV1, aV2;
296 TopoDS_Edge anEdgeE = TopoDS::Edge(Exp.Current());
298 // draw curve direction (issue 0021087)
299 anEdgeE.Orientation( TopAbs_FORWARD );
301 if ( anEdgeE.IsNull() ) continue;
303 TopExp::Vertices(anEdgeE, aV1, aV2);
304 gp_Pnt aP1 = BRep_Tool::Pnt(aV1);
305 gp_Pnt aP2 = BRep_Tool::Pnt(aV2);
309 Handle(Geom_Curve) C = BRep_Tool::Curve(anEdgeE,fp,lp);
311 if ( C.IsNull() ) continue;
313 if ( anEdgeE.Orientation() == TopAbs_FORWARD )
314 C->D1(lp, aP2, aDirVec);
316 C->D1(fp, aP1, aDirVec);
320 GeomAdaptor_Curve aAdC;
321 aAdC.Load(C, fp, lp);
322 Standard_Real aDist = GCPnts_AbscissaPoint::Length(aAdC, fp, lp);
324 if (aDist > gp::Resolution()) {
326 if ( anEdgeE.Orientation() == TopAbs_FORWARD )
331 Prs3d_Arrow::Draw(aPrs, aP2, aDir, M_PI/180.*5., aDist/10.);
336 // draw color field on vertices
337 if( anIsColorField && myFieldDimension == 0 && aMode != CustomHighlight )
342 drawField( aPrs, true );
347 // aPrs->ReCompute(); // for hidden line recomputation if necessary...
350 void GEOM_AISShape::SetShadingColor(const Quantity_Color &aCol)
352 myShadingColor = aCol;
355 void GEOM_AISShape::SetEdgesInShadingColor(const Quantity_Color &aCol)
357 myEdgesInShadingColor = aCol;
360 void GEOM_AISShape::SetLabelColor(const Quantity_Color &aCol)
365 void GEOM_AISShape::highlightSubShapes(const TColStd_IndexedMapOfInteger& aIndexMap,
366 const Standard_Boolean aHighlight )
368 Handle(AIS_InteractiveObject) anObj = this;
369 Handle(AIS_InteractiveContext) anIC = GetContext();
371 #if OCC_VERSION_LARGE <= 0x07030000
372 || !anIC->HasOpenedContext()
377 Standard_Boolean isAutoHilight = anIC->AutomaticHilight();
378 anIC->SetAutomaticHilight( false );
380 anIC->ClearSelected( false );
383 SelectMgr_IndexedMapOfOwner anAllMap, aToHiliteMap;
385 // Get entity owners for all activated selection modes
386 getEntityOwners( anObj, anIC, anAllMap );
388 // Convert <aIndexMap> into the map of owners to highlight/unhighlight
389 indicesToOwners( aIndexMap, Shape(), anAllMap, aToHiliteMap );
392 for ( Standard_Integer i = 1, n = aToHiliteMap.Extent(); i <= n; i++ )
393 anIC->AddOrRemoveSelected( aToHiliteMap( i ), false );
396 anIC->SetAutomaticHilight( isAutoHilight );
397 anIC->HilightSelected( false );
400 void GEOM_AISShape::SetDisplayVectors(bool isDisplayed)
402 myDisplayVectors = isDisplayed;
405 void GEOM_AISShape::SetDisplayVertices(bool isDisplayed)
407 myDisplayVertices = isDisplayed;
410 void GEOM_AISShape::SetDisplayName(bool isDisplayed)
412 myDisplayName = isDisplayed;
415 void GEOM_AISShape::shadingMode(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
416 const Handle(Prs3d_Presentation)& aPrs,
417 const Standard_Integer aMode)
419 myDrawer->ShadingAspect()->Aspect()->SetDistinguishOn();
421 Graphic3d_MaterialAspect aMatAspect(Graphic3d_NOM_PLASTIC);
422 aMatAspect.SetTransparency(Transparency());
423 Graphic3d_MaterialAspect currentFrontMaterial = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
424 Graphic3d_MaterialAspect currentBackMaterial = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
425 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial( isTopLevel() ? aMatAspect : currentFrontMaterial );
426 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial ( isTopLevel() ? aMatAspect : currentBackMaterial );
428 if( isTopLevel() && switchTopLevel() )
429 myDrawer->ShadingAspect()->SetColor( topLevelColor() );
431 if(myDrawer->ShadingAspect()->Aspect()->FrontMaterial().MaterialType( Graphic3d_MATERIAL_ASPECT ))
432 myDrawer->ShadingAspect()->SetColor(myShadingColor);
434 myDrawer->ShadingAspect()->SetColor(myDrawer->ShadingAspect()->Aspect()->FrontMaterial().AmbientColor());
437 bool anIsColorField = !myFieldStepData.isEmpty() && myFieldDataType != GEOM::FDT_String;
438 if( anIsColorField && ( myFieldDimension == 2 || myFieldDimension == 3 || myFieldDimension == -1 ) )
444 // PAL12113: AIS_Shape::Compute() works correctly with shapes containing no faces
445 //StdPrs_ShadedShape::Add(aPrs,myshape,myDrawer);
446 SALOME_AISShape::Compute(aPresentationManager, aPrs, aMode);
450 Standard_Boolean GEOM_AISShape::isTopLevel()
455 void GEOM_AISShape::setTopLevel(Standard_Boolean f)
459 myPrevDisplayMode = DisplayMode();
461 switch(topLevelDisplayMode()) {
462 case TopWireFrame : dm = Wireframe; break;
463 case TopShading : dm = Shading; break;
464 case TopShadingWithEdges : dm = ShadingWithEdges; break;
465 default : dm = myPrevDisplayMode; break;
470 SetDisplayMode(myPrevDisplayMode);
475 void GEOM_AISShape::setPrevDisplayMode(const Standard_Integer mode)
477 myPrevDisplayMode = mode;
480 Quantity_Color GEOM_AISShape::topLevelColor()
482 return myTopLevelColor;
485 void GEOM_AISShape::setTopLevelColor(const Quantity_Color c)
490 GEOM_AISShape::TopLevelDispMode GEOM_AISShape::topLevelDisplayMode()
495 void GEOM_AISShape::setTopLevelDisplayMode(const GEOM_AISShape::TopLevelDispMode dm)
500 Standard_Boolean GEOM_AISShape::switchTopLevel()
502 return myTopLevelDm != TopShowAdditionalWActor;
505 Standard_Boolean GEOM_AISShape::toActivate()
507 return ( myTopLevel && myTopLevelDm == TopShowAdditionalWActor ) ? false : true;
510 void GEOM_AISShape::setFieldStepInfo( const GEOM::field_data_type theFieldDataType,
511 const int theFieldDimension,
512 const QList<QVariant>& theFieldStepData,
513 const TCollection_AsciiString& theFieldStepName,
514 const double theFieldStepRangeMin,
515 const double theFieldStepRangeMax )
517 myFieldDataType = theFieldDataType;
518 myFieldDimension = theFieldDimension;
519 myFieldStepData = theFieldStepData;
520 myFieldStepName = theFieldStepName;
521 myFieldStepRangeMin = theFieldStepRangeMin;
522 myFieldStepRangeMax = theFieldStepRangeMax;
525 void GEOM_AISShape::getFieldStepInfo( GEOM::field_data_type& theFieldDataType,
526 int& theFieldDimension,
527 QList<QVariant>& theFieldStepData,
528 TCollection_AsciiString& theFieldStepName,
529 double& theFieldStepRangeMin,
530 double& theFieldStepRangeMax ) const
532 theFieldDataType = myFieldDataType;
533 theFieldDimension = myFieldDimension;
534 theFieldStepData = myFieldStepData;
535 theFieldStepName = myFieldStepName;
536 theFieldStepRangeMin = myFieldStepRangeMin;
537 theFieldStepRangeMax = myFieldStepRangeMax;
540 void GEOM_AISShape::drawField( const Handle(Prs3d_Presentation)& thePrs,
541 const bool theIsString,
542 const bool theIsHighlight )
544 if( myFieldStepData.isEmpty() )
547 QListIterator<QVariant> aFieldStepDataIter( myFieldStepData );
549 TopAbs_ShapeEnum aShapeType = TopAbs_SHAPE;
550 switch( myFieldDimension )
552 case 0: aShapeType = TopAbs_VERTEX; break;
553 case 1: aShapeType = TopAbs_EDGE; break;
554 case 2: aShapeType = TopAbs_FACE; break;
555 case 3: aShapeType = TopAbs_SOLID; break;
556 case -1: aShapeType = TopAbs_VERTEX; break;
559 TopTools_IndexedMapOfShape aShapeMap;
560 TopExp::MapShapes( myshape, aShapeMap );
562 TColStd_IndexedMapOfInteger anIndexMap;
564 TopExp_Explorer anExp;
565 for( anExp.Init( myshape, aShapeType ); anExp.More(); anExp.Next() )
567 TopoDS_Shape aSubShape = anExp.Current();
568 if( !aSubShape.IsNull() )
570 Standard_Integer aSubShapeIndex = aShapeMap.FindIndex( aSubShape );
571 if( anIndexMap.Contains( aSubShapeIndex ) )
574 anIndexMap.Add( aSubShapeIndex );
576 Handle(Graphic3d_Group) aGroup = Prs3d_Root::NewGroup( thePrs );
580 if( aFieldStepDataIter.hasNext() )
582 const QVariant& aVariant = aFieldStepDataIter.next();
584 aString = aVariant.toString();
586 aQColor = aVariant.value<QColor>();
594 if( computeMassCenter( aSubShape, aCenter ) )
596 Graphic3d_Vertex aVertex( aCenter.X(), aCenter.Y(), aCenter.Z() );
598 Handle(Graphic3d_AspectText3d) anAspectText3d = new Graphic3d_AspectText3d();
599 anAspectText3d->SetStyle( Aspect_TOST_ANNOTATION );
600 anAspectText3d->SetColor( myLabelColor );
601 aGroup->SetPrimitivesAspect( anAspectText3d );
603 aGroup->Text( aString.toUtf8().constData(), aVertex, 14 );
608 Quantity_Color aColor( aQColor.redF(), aQColor.greenF(), aQColor.blueF(), Quantity_TOC_RGB );
609 SetCustomColor( aSubShape, aColor );
610 if( myFieldDimension == 0 )
612 TopoDS_Vertex aVertexShape = TopoDS::Vertex( aSubShape );
613 if( !aVertexShape.IsNull() )
615 gp_Pnt aPnt = BRep_Tool::Pnt( aVertexShape );
617 Handle(Graphic3d_AspectMarker3d) anAspectMarker3d = new Graphic3d_AspectMarker3d();
618 anAspectMarker3d->SetColor( aColor );
619 anAspectMarker3d->SetType( Aspect_TOM_POINT );
620 anAspectMarker3d->SetScale( 10.0 );
621 aGroup->SetPrimitivesAspect( anAspectMarker3d );
623 Handle(Graphic3d_ArrayOfPoints) anArray = new Graphic3d_ArrayOfPoints( 1 );
624 anArray->AddVertex( aPnt.X(), aPnt.Y(), aPnt.Z() );
626 aGroup->AddPrimitiveArray( anArray );
629 else if( myFieldDimension == 1 )
631 myDrawer->WireAspect()->SetColor( aColor );
633 myDrawer->WireAspect()->SetWidth( myOwnWidth );
635 myDrawer->WireAspect()->SetWidth( myOwnWidth + 4 );
637 StdPrs_WFShape::Add( thePrs, aSubShape, myDrawer );
640 else if( myFieldDimension == 2 ||
641 myFieldDimension == 3 ||
642 myFieldDimension == -1 )
644 //Handle(Prs3d_ShadingAspect) anAspect = new Prs3d_ShadingAspect();
645 //anAspect->SetColor( aColor );
646 //myDrawer->SetShadingAspect( anAspect );
647 //StdPrs_ShadedShape::Add( thePrs, aSubShape, myDrawer );
653 SALOME_AISShape::Compute( Handle(PrsMgr_PresentationManager3d)(),
654 thePrs, AIS_Shaded );
657 void GEOM_AISShape::drawName( const Handle(Prs3d_Presentation)& thePrs )
659 Handle(Graphic3d_Group) aGroup = Prs3d_Root::NewGroup( thePrs );
661 gp_Ax3 anAx3 = GEOMUtils::GetPosition(myshape);
662 gp_Pnt aCenter = anAx3.Location();
664 Graphic3d_Vertex aVertex( aCenter.X(), aCenter.Y(), aCenter.Z() );
666 Handle(Graphic3d_AspectText3d) anAspectText3d = new Graphic3d_AspectText3d();
667 anAspectText3d->SetStyle( Aspect_TOST_ANNOTATION );
668 anAspectText3d->SetColor( myLabelColor );
669 aGroup->SetPrimitivesAspect( anAspectText3d );
671 const char* aName = getIO()->getName();
672 aGroup->Text( TCollection_ExtendedString( aName ), aVertex, 16 );
675 Standard_Boolean GEOM_AISShape::computeMassCenter( const TopoDS_Shape& theShape,
678 theCenter.SetCoord( 0,0,0 );
679 Standard_Integer aNbPoints = 0;
681 if ( theShape.ShapeType() == TopAbs_EDGE )
684 Handle(Geom_Curve) curve = BRep_Tool::Curve( TopoDS::Edge( theShape ), f, l );
685 if ( !curve.IsNull() )
687 theCenter = curve->Value( 0.5 * ( f + l ));
691 else if ( theShape.ShapeType() == TopAbs_FACE )
693 const TopoDS_Face& F = TopoDS::Face( theShape );
694 BRepAdaptor_Surface surface( F );
697 Handle(Poly_Triangulation) triangulation = BRep_Tool::Triangulation( F, L );
698 if ( !triangulation.IsNull() && triangulation->HasUVNodes() )
702 const TColgp_Array1OfPnt2d& uvArray = triangulation->UVNodes();
703 const Poly_Array1OfTriangle& trias = triangulation->Triangles();
704 Standard_Integer n1,n2,n3;
705 for ( int iT = trias.Lower(); iT <= trias.Upper(); ++iT )
707 trias( iT ).Get( n1,n2,n3 );
708 const gp_Pnt2d& uv1 = uvArray( n1 );
709 const gp_Pnt2d& uv2 = uvArray( n2 );
710 const gp_Pnt2d& uv3 = uvArray( n3 );
711 double a = 0.5 * sqrt(( uv1.X() - uv3.X() ) * ( uv2.Y() - uv1.Y() ) -
712 ( uv1.X() - uv2.X() ) * ( uv3.Y() - uv1.Y() ));
713 C += ( uv1.XY() + uv2.XY() + uv3.XY() ) / 3. * a;
716 if ( A > std::numeric_limits<double>::min() )
719 theCenter = surface.Value( C.X(), C.Y() );
723 if ( aNbPoints == 0 )
725 theCenter = surface.Value( 0.5 * ( surface.FirstUParameter() + surface.LastUParameter() ),
726 0.5 * ( surface.FirstVParameter() + surface.LastVParameter() ));
731 if ( aNbPoints == 0 )
733 TopExp_Explorer anExp;
734 for( anExp.Init( theShape, TopAbs_VERTEX ); anExp.More(); anExp.Next() )
736 TopoDS_Vertex aVertex = TopoDS::Vertex( anExp.Current() );
737 if( !aVertex.IsNull() )
739 gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
740 theCenter.ChangeCoord() += aPnt.XYZ();
747 theCenter.ChangeCoord() /= (Standard_Real) aNbPoints;