0031067: Visualization - Aspect_Window::DoResize() should be a non-const method
[occt.git] / src / AIS / AIS_ConnectedInteractive.cxx
1 // Created on: 1997-01-08
2 // Created by: Robert COUBLANC
3 // Copyright (c) 1997-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #include <AIS_ConnectedInteractive.hxx>
18
19 #include <AIS_InteractiveContext.hxx>
20 #include <AIS_Shape.hxx>
21 #include <BRepTools.hxx>
22 #include <NCollection_DataMap.hxx>
23 #include <Precision.hxx>
24 #include <Prs3d_Drawer.hxx>
25 #include <Prs3d_Projector.hxx>
26 #include <Select3D_SensitiveEntity.hxx>
27 #include <SelectMgr_EntityOwner.hxx>
28 #include <SelectMgr_Selection.hxx>
29 #include <Standard_NotImplemented.hxx>
30 #include <Standard_ProgramError.hxx>
31 #include <Standard_Type.hxx>
32 #include <StdPrs_HLRPolyShape.hxx>
33 #include <StdPrs_WFShape.hxx>
34 #include <StdSelect.hxx>
35 #include <StdSelect_BRepOwner.hxx>
36 #include <TopAbs_ShapeEnum.hxx>
37 #include <TopoDS_Shape.hxx>
38 #include <TopTools_IndexedMapOfShape.hxx>
39 #include <TopTools_OrientedShapeMapHasher.hxx>
40
41 IMPLEMENT_STANDARD_RTTIEXT(AIS_ConnectedInteractive,AIS_InteractiveObject)
42
43 //=======================================================================
44 //function : AIS_ConnectedInteractive
45 //purpose  : 
46 //=======================================================================
47 AIS_ConnectedInteractive::AIS_ConnectedInteractive(const PrsMgr_TypeOfPresentation3d aTypeOfPresentation3d):
48 AIS_InteractiveObject(aTypeOfPresentation3d)
49 {
50   //
51 }
52
53 //=======================================================================
54 //function : connect
55 //purpose  :
56 //=======================================================================
57 void AIS_ConnectedInteractive::connect (const Handle(AIS_InteractiveObject)& theAnotherObj,
58                                         const Handle(Geom_Transformation)&   theLocation)
59 {
60   if (myReference == theAnotherObj)
61   {
62     setLocalTransformation (theLocation);
63     return;
64   }
65
66   Handle(AIS_ConnectedInteractive) aConnected = Handle(AIS_ConnectedInteractive)::DownCast (theAnotherObj);
67   if (!aConnected.IsNull())
68   {
69     myReference = aConnected->myReference;
70   }
71   else if (theAnotherObj->HasOwnPresentations())
72   {
73     myReference = theAnotherObj;
74   }
75   else
76   {
77     throw Standard_ProgramError("AIS_ConnectedInteractive::Connect() - object without own presentation can not be connected");
78   }
79
80   if (!myReference.IsNull())
81   {
82     myTypeOfPresentation3d = myReference->TypeOfPresentation3d();
83   }
84   setLocalTransformation (theLocation);
85 }
86
87 //=======================================================================
88 //function : Disconnect
89 //purpose  :
90 //=======================================================================
91
92 void AIS_ConnectedInteractive::Disconnect()
93 {
94   for (PrsMgr_Presentations::Iterator aPrsIter (myPresentations); aPrsIter.More(); aPrsIter.Next())
95   {
96     const Handle(PrsMgr_Presentation)& aPrs = aPrsIter.Value();
97     if (!aPrs.IsNull())
98     {
99       aPrs->DisconnectAll (Graphic3d_TOC_DESCENDANT);
100     }
101   }
102 }
103 //=======================================================================
104 //function : Compute
105 //purpose  :
106 //=======================================================================
107 void AIS_ConnectedInteractive::Compute (const Handle(PrsMgr_PresentationManager3d)& thePrsMgr,
108                                         const Handle(Prs3d_Presentation)&           thePrs,
109                                         const Standard_Integer                      theMode)
110 {
111   if (HasConnection())
112   {
113     thePrs->Clear (Standard_False);
114     thePrs->DisconnectAll (Graphic3d_TOC_DESCENDANT);
115
116     if (!myReference->HasInteractiveContext())
117     {
118       myReference->SetContext (GetContext());
119     }
120     thePrsMgr->Connect (this, myReference, theMode, theMode);
121     if (thePrsMgr->Presentation (myReference, theMode)->MustBeUpdated())
122     {
123       thePrsMgr->Update (myReference, theMode);
124     }
125   }
126
127   if (!thePrs.IsNull())
128   {
129     thePrs->ReCompute();
130   }
131 }
132
133 //=======================================================================
134 //function : Compute
135 //purpose  :
136 //=======================================================================
137 void AIS_ConnectedInteractive::Compute (const Handle(Prs3d_Projector)& theProjector,
138                                         const Handle(Geom_Transformation)& theTransformation,
139                                         const Handle(Prs3d_Presentation)& thePresentation)
140 {
141   updateShape (Standard_False);
142   if (myShape.IsNull())
143   {
144     return;
145   }
146   const TopLoc_Location& aLocation = myShape.Location();
147   TopoDS_Shape aShape = myShape.Located (TopLoc_Location (theTransformation->Trsf()) * aLocation);
148   Compute (theProjector, thePresentation, aShape);
149 }
150
151 //=======================================================================
152 //function : Compute
153 //purpose  :
154 //=======================================================================
155 void AIS_ConnectedInteractive::Compute(const Handle(Prs3d_Projector)& aProjector, const Handle(Prs3d_Presentation)& aPresentation)
156 {
157   updateShape (Standard_True);
158   Compute (aProjector, aPresentation, myShape);
159 }
160
161 //=======================================================================
162 //function : Compute
163 //purpose  :
164 //=======================================================================
165 void AIS_ConnectedInteractive::Compute (const Handle(Prs3d_Projector)& theProjector,
166                                         const Handle(Prs3d_Presentation)& thePrs,
167                                         const TopoDS_Shape& theShape)
168 {
169   AIS_Shape::computeHlrPresentation (theProjector, thePrs, theShape, myDrawer);
170 }
171
172 //=======================================================================
173 //function : updateShape
174 //purpose  : 
175 //=======================================================================
176 void AIS_ConnectedInteractive::updateShape (const Standard_Boolean isWithLocation)
177 {
178   Handle(AIS_Shape) anAisShape = Handle(AIS_Shape)::DownCast (myReference);
179   if (anAisShape.IsNull())
180   {
181     return;
182   }
183  
184   TopoDS_Shape aShape = anAisShape->Shape();
185   if (aShape.IsNull())
186   {
187     return;
188   }
189
190   if(!isWithLocation)
191   {
192     myShape = aShape;
193   }
194   else
195   {
196     myShape = aShape.Moved (TopLoc_Location (Transformation()));
197   }
198 }
199
200 //=======================================================================
201 //function : ComputeSelection
202 //purpose  : 
203 //=======================================================================
204 void AIS_ConnectedInteractive::ComputeSelection (const Handle(SelectMgr_Selection)& theSelection, 
205                                                  const Standard_Integer theMode)
206 {
207   if (!HasConnection())
208   {
209     return;
210   }
211
212   if (theMode != 0 && myReference->AcceptShapeDecomposition())
213   {
214     computeSubShapeSelection (theSelection, theMode);
215     return;
216   }
217
218   if (!myReference->HasSelection (theMode))
219   {
220     myReference->RecomputePrimitives (theMode);
221   }
222
223   const Handle(SelectMgr_Selection)& TheRefSel = myReference->Selection (theMode);
224   Handle(SelectMgr_EntityOwner) anOwner = new SelectMgr_EntityOwner (this);
225
226   TopLoc_Location aLocation (Transformation());
227   anOwner->SetLocation (aLocation);
228
229   if (TheRefSel->IsEmpty())
230   {
231     myReference->RecomputePrimitives (theMode);
232   }
233
234   for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (TheRefSel->Entities()); aSelEntIter.More(); aSelEntIter.Next())
235   {
236     if (const Handle(Select3D_SensitiveEntity)& aSensitive = aSelEntIter.Value()->BaseSensitive())
237     {
238       // Get the copy of SE3D
239       if (Handle(Select3D_SensitiveEntity) aNewSensitive = aSensitive->GetConnected())
240       {
241         aNewSensitive->Set(anOwner);
242         theSelection->Add (aNewSensitive);
243       }
244     }
245   }
246 }
247
248 //=======================================================================
249 //function : ComputeSubShapeSelection 
250 //purpose  :
251 //=======================================================================
252 void AIS_ConnectedInteractive::computeSubShapeSelection (const Handle(SelectMgr_Selection)& theSelection, 
253                                                          const Standard_Integer theMode)
254 {
255   typedef NCollection_List<Handle(Select3D_SensitiveEntity)> SensitiveList;
256   typedef NCollection_DataMap<TopoDS_Shape, SensitiveList, TopTools_OrientedShapeMapHasher>
257     Shapes2EntitiesMap;
258
259   if (!myReference->HasSelection (theMode))
260   {
261     myReference->RecomputePrimitives (theMode);
262   }
263
264   const Handle(SelectMgr_Selection)& aRefSel = myReference->Selection (theMode);
265   if (aRefSel->IsEmpty() || aRefSel->UpdateStatus() == SelectMgr_TOU_Full)
266   {
267     myReference->RecomputePrimitives (theMode);
268   }
269
270   // Fill in the map of subshapes and corresponding sensitive entities associated with aMode
271   Shapes2EntitiesMap aShapes2EntitiesMap;
272   for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (aRefSel->Entities()); aSelEntIter.More(); aSelEntIter.Next())
273   {
274     if (const Handle(Select3D_SensitiveEntity)& aSE = aSelEntIter.Value()->BaseSensitive())
275     {
276       if (Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast (aSE->OwnerId()))
277       {
278         const TopoDS_Shape& aSubShape = anOwner->Shape();
279         if(!aShapes2EntitiesMap.IsBound (aSubShape))
280         {
281           aShapes2EntitiesMap.Bind (aSubShape, SensitiveList());
282         }
283         aShapes2EntitiesMap (aSubShape).Append (aSE);
284       }
285     }
286   }
287
288   // Fill in selection from aShapes2EntitiesMap
289   for (Shapes2EntitiesMap::Iterator aMapIt (aShapes2EntitiesMap); aMapIt.More(); aMapIt.Next())
290   {
291     const SensitiveList& aSEList = aMapIt.Value();
292     Handle(StdSelect_BRepOwner) anOwner = new StdSelect_BRepOwner (aMapIt.Key(), this, aSEList.First()->OwnerId()->Priority(), Standard_True);
293     anOwner->SetLocation (Transformation());
294     for (SensitiveList::Iterator aListIt (aSEList); aListIt.More(); aListIt.Next())
295     {
296       if (Handle(Select3D_SensitiveEntity) aNewSE = aListIt.Value()->GetConnected())
297       {
298         aNewSE->Set (anOwner);
299         theSelection->Add (aNewSE);
300       }
301     }
302   }
303
304   StdSelect::SetDrawerForBRepOwner (theSelection, myDrawer);  
305 }