1 // Created on: 2011-10-20
2 // Created by: Roman KOZLOV
3 // Copyright (c) 2011-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 #include <AIS_Shape.hxx>
17 #include <BRepBndLib.hxx>
18 #include <IVtkOCC_SelectableObject.hxx>
19 #include <Select3D_SensitiveBox.hxx>
20 #include <SelectMgr_Selection.hxx>
21 #include <Standard_ErrorHandler.hxx>
22 #include <StdSelect_BRepOwner.hxx>
23 #include <StdSelect_BRepSelectionTool.hxx>
24 #include <TopoDS_Iterator.hxx>
26 // Handle implementation
29 //============================================================================
30 // Method: Constructor
31 // Purpose: Constructs a selectable object initalized by the given shape
32 //============================================================================
33 IVtkOCC_SelectableObject::IVtkOCC_SelectableObject (const IVtkOCC_Shape::Handle& theShape)
34 : SelectMgr_SelectableObject (PrsMgr_TOP_AllView),
37 if (!myShape.IsNull())
39 myShape->SetSelectableObject (this);
42 // Minor stuff - but it facilitates usage of OCCT selection
43 // classes dealing with deflection, see ComputeSelection() below
44 myOCCTDrawer = new Prs3d_Drawer();
47 //============================================================================
48 // Method: Constructor
49 // Purpose: Constructs uninitialized selectable object.
50 // setShape() should be called later.
51 //============================================================================
52 IVtkOCC_SelectableObject::IVtkOCC_SelectableObject()
53 : SelectMgr_SelectableObject (PrsMgr_TOP_AllView),
57 //============================================================================
59 // Purpose: Sets the selectable shape
60 //============================================================================
61 void IVtkOCC_SelectableObject::SetShape (const IVtkOCC_Shape::Handle& theShape)
66 myShape->SetSelectableObject (this);
69 // Shape has changed -> Clear all internal data
74 //============================================================================
75 // Method: ComputeSelection
76 // Purpose: Internal method, computes selection data for viewer selector
77 //============================================================================
78 void IVtkOCC_SelectableObject::ComputeSelection (const Handle(SelectMgr_Selection)& theSelection,
79 const Standard_Integer theMode)
86 TopoDS_Shape anOcctShape = myShape->GetShape();
88 if (anOcctShape.ShapeType() == TopAbs_COMPOUND)
90 TopoDS_Iterator anExplor (anOcctShape);
91 if (!anExplor.More()) // Shape empty -> go away
97 TopAbs_ShapeEnum aTypeOfSel = AIS_Shape::SelectionType (theMode);
99 Standard_Real aDeflection = myOCCTDrawer->MaximalChordialDeviation();
100 if (myOCCTDrawer->TypeOfDeflection() == Aspect_TOD_RELATIVE)
103 BRepBndLib::Add (anOcctShape, aBndBox);
104 if (!aBndBox.IsVoid())
106 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
107 aBndBox.Get (aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
108 aDeflection = Max (aXmax - aXmin, Max (aYmax - aYmin, aZmax - aZmin)) *
109 myOCCTDrawer->DeviationCoefficient();
113 // Assume the shape has been displayed already -> triangulation should exist
114 Standard_Boolean isAutoTriangulation = Standard_False;
119 StdSelect_BRepSelectionTool::Load (theSelection,
124 myOCCTDrawer->DeviationAngle(),
125 isAutoTriangulation);
127 catch (Standard_Failure)
131 Bnd_Box aBndBox = BoundingBox();
132 Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner (anOcctShape, this);
133 Handle(Select3D_SensitiveBox) aSensitiveBox =
134 new Select3D_SensitiveBox (aOwner, aBndBox);
135 theSelection->Add (aSensitiveBox);
140 //============================================================================
141 // Method: BoundingBox
143 //============================================================================
144 const Bnd_Box& IVtkOCC_SelectableObject::BoundingBox()
152 TopoDS_Shape anOcctShape = myShape->GetShape();
154 if (anOcctShape.ShapeType() == TopAbs_COMPOUND)
156 TopoDS_Iterator anExplor (anOcctShape);
157 if (!anExplor.More())
158 { // Shape empty -> nothing to do
164 if (myBndBox.IsVoid())
166 // Add only edges and vertices, in case of troubles this should work anyway
167 BRepBndLib::AddClose (anOcctShape, myBndBox);
173 //============================================================================
174 // Method: BoundingBox
176 //============================================================================
177 void IVtkOCC_SelectableObject::BoundingBox (Bnd_Box& theBndBox)
180 theBndBox = myBndBox;