0027869: Visualization - AIS_ConnectedInteractive does not support exact HLR mode
[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 <PrsMgr_ModedPresentation.hxx>
27 #include <Select3D_SensitiveEntity.hxx>
28 #include <SelectMgr_EntityOwner.hxx>
29 #include <SelectMgr_Selection.hxx>
30 #include <Standard_NotImplemented.hxx>
31 #include <Standard_ProgramError.hxx>
32 #include <Standard_Type.hxx>
33 #include <StdPrs_HLRPolyShape.hxx>
34 #include <StdPrs_WFShape.hxx>
35 #include <StdSelect.hxx>
36 #include <StdSelect_BRepOwner.hxx>
37 #include <TopAbs_ShapeEnum.hxx>
38 #include <TopoDS_Shape.hxx>
39 #include <TopTools_IndexedMapOfShape.hxx>
40 #include <TopTools_OrientedShapeMapHasher.hxx>
41
42 IMPLEMENT_STANDARD_RTTIEXT(AIS_ConnectedInteractive,AIS_InteractiveObject)
43
44 //=======================================================================
45 //function : AIS_ConnectedInteractive
46 //purpose  : 
47 //=======================================================================
48 AIS_ConnectedInteractive::AIS_ConnectedInteractive(const PrsMgr_TypeOfPresentation3d aTypeOfPresentation3d):
49 AIS_InteractiveObject(aTypeOfPresentation3d)
50 {
51   //
52 }
53
54 //=======================================================================
55 //function : connect
56 //purpose  :
57 //=======================================================================
58 void AIS_ConnectedInteractive::connect (const Handle(AIS_InteractiveObject)& theAnotherObj,
59                                         const Handle(Geom_Transformation)&   theLocation)
60 {
61   if (myReference == theAnotherObj)
62   {
63     setLocalTransformation (theLocation);
64     return;
65   }
66
67   Handle(AIS_ConnectedInteractive) aConnected = Handle(AIS_ConnectedInteractive)::DownCast (theAnotherObj);
68   if (!aConnected.IsNull())
69   {
70     myReference = aConnected->myReference;
71   }
72   else if (theAnotherObj->HasOwnPresentations())
73   {
74     myReference = theAnotherObj;
75   }
76   else
77   {
78     throw Standard_ProgramError("AIS_ConnectedInteractive::Connect() - object without own presentation can not be connected");
79   }
80
81   if (!myReference.IsNull())
82   {
83     myTypeOfPresentation3d = myReference->TypeOfPresentation3d();
84   }
85   setLocalTransformation (theLocation);
86 }
87
88 //=======================================================================
89 //function : Disconnect
90 //purpose  :
91 //=======================================================================
92
93 void AIS_ConnectedInteractive::Disconnect()
94 {
95   for(Standard_Integer aPrsIter = 1; aPrsIter <= myPresentations.Length(); ++aPrsIter)
96   {
97     const Handle(PrsMgr_Presentation)& aPrs = myPresentations (aPrsIter).Presentation();
98     if (!aPrs.IsNull())
99     {
100       aPrs->Presentation()->DisconnectAll (Graphic3d_TOC_DESCENDANT);
101     }
102   }
103 }
104 //=======================================================================
105 //function : Compute
106 //purpose  :
107 //=======================================================================
108 void AIS_ConnectedInteractive::Compute (const Handle(PrsMgr_PresentationManager3d)& thePrsMgr,
109                                         const Handle(Prs3d_Presentation)&           thePrs,
110                                         const Standard_Integer                      theMode)
111 {
112   if (HasConnection())
113   {
114     thePrs->Clear (Standard_False);
115     thePrs->RemoveAll();
116
117     if (!myReference->HasInteractiveContext())
118     {
119       myReference->SetContext (GetContext());
120     }
121     thePrsMgr->Connect (this, myReference, theMode, theMode);
122     if (thePrsMgr->Presentation (myReference, theMode)->MustBeUpdated())
123     {
124       thePrsMgr->Update (myReference, theMode);
125     }
126   }
127
128   if (!thePrs.IsNull())
129   {
130     thePrs->ReCompute();
131   }
132 }
133
134 //=======================================================================
135 //function : Compute
136 //purpose  :
137 //=======================================================================
138 void AIS_ConnectedInteractive::Compute (const Handle(Prs3d_Projector)& theProjector,
139                                         const Handle(Geom_Transformation)& theTransformation,
140                                         const Handle(Prs3d_Presentation)& thePresentation)
141 {
142   updateShape (Standard_False);
143   if (myShape.IsNull())
144   {
145     return;
146   }
147   const TopLoc_Location& aLocation = myShape.Location();
148   TopoDS_Shape aShape = myShape.Located (TopLoc_Location (theTransformation->Trsf()) * aLocation);
149   Compute (theProjector, thePresentation, aShape);
150 }
151
152 //=======================================================================
153 //function : Compute
154 //purpose  :
155 //=======================================================================
156 void AIS_ConnectedInteractive::Compute(const Handle(Prs3d_Projector)& aProjector, const Handle(Prs3d_Presentation)& aPresentation)
157 {
158   updateShape (Standard_True);
159   Compute (aProjector, aPresentation, myShape);
160 }
161
162 //=======================================================================
163 //function : Compute
164 //purpose  :
165 //=======================================================================
166 void AIS_ConnectedInteractive::Compute (const Handle(Prs3d_Projector)& theProjector,
167                                         const Handle(Prs3d_Presentation)& thePrs,
168                                         const TopoDS_Shape& theShape)
169 {
170   AIS_Shape::computeHlrPresentation (theProjector, thePrs, theShape, myDrawer);
171 }
172
173 //=======================================================================
174 //function : updateShape
175 //purpose  : 
176 //=======================================================================
177 void AIS_ConnectedInteractive::updateShape (const Standard_Boolean isWithLocation)
178 {
179   Handle(AIS_Shape) anAisShape = Handle(AIS_Shape)::DownCast (myReference);
180   if (anAisShape.IsNull())
181   {
182     return;
183   }
184  
185   TopoDS_Shape aShape = anAisShape->Shape();
186   if (aShape.IsNull())
187   {
188     return;
189   }
190
191   if(!isWithLocation)
192   {
193     myShape = aShape;
194   }
195   else
196   {
197     myShape = aShape.Moved (TopLoc_Location (Transformation()));
198   }
199 }
200
201 //=======================================================================
202 //function : ComputeSelection
203 //purpose  : 
204 //=======================================================================
205 void AIS_ConnectedInteractive::ComputeSelection (const Handle(SelectMgr_Selection)& theSelection, 
206                                                  const Standard_Integer theMode)
207 {
208   if (!HasConnection())
209   {
210     return;
211   }
212
213   if (theMode != 0 && myReference->AcceptShapeDecomposition())
214   {
215     computeSubShapeSelection (theSelection, theMode);
216     return;
217   }
218
219   if (!myReference->HasSelection (theMode))
220   {
221     myReference->RecomputePrimitives (theMode);
222   }
223
224   const Handle(SelectMgr_Selection)& TheRefSel = myReference->Selection (theMode);
225   Handle(SelectMgr_EntityOwner) anOwner = new SelectMgr_EntityOwner (this);
226   Handle(Select3D_SensitiveEntity) aSensitive, aNewSensitive;
227
228   TopLoc_Location aLocation (Transformation());
229   anOwner->SetLocation (aLocation);
230
231   if (TheRefSel->IsEmpty())
232   {
233     myReference->RecomputePrimitives (theMode);
234   }
235
236   for (TheRefSel->Init(); TheRefSel->More(); TheRefSel->Next())
237   {
238     aSensitive = Handle(Select3D_SensitiveEntity)::DownCast (TheRefSel->Sensitive()->BaseSensitive());
239     if (!aSensitive.IsNull())
240     {
241       // Get the copy of SE3D
242       aNewSensitive = aSensitive->GetConnected();
243
244       aNewSensitive->Set(anOwner);
245
246       theSelection->Add (aNewSensitive);
247     }
248   }
249 }
250
251 //=======================================================================
252 //function : ComputeSubShapeSelection 
253 //purpose  :
254 //=======================================================================
255 void AIS_ConnectedInteractive::computeSubShapeSelection (const Handle(SelectMgr_Selection)& theSelection, 
256                                                          const Standard_Integer theMode)
257 {
258   typedef NCollection_List<Handle(Select3D_SensitiveEntity)> SensitiveList;
259   typedef NCollection_DataMap<TopoDS_Shape, SensitiveList, TopTools_OrientedShapeMapHasher>
260     Shapes2EntitiesMap;
261
262   if (!myReference->HasSelection (theMode))
263     myReference->RecomputePrimitives (theMode);
264    
265   const Handle(SelectMgr_Selection)& aRefSel = myReference->Selection (theMode);
266
267   if (aRefSel->IsEmpty() || aRefSel->UpdateStatus() == SelectMgr_TOU_Full)
268   {
269     myReference->RecomputePrimitives (theMode);
270   }
271   
272   Handle(StdSelect_BRepOwner) anOwner;
273   TopLoc_Location aDummyLoc;
274
275   Handle(Select3D_SensitiveEntity) aSE, aNewSE;
276   Shapes2EntitiesMap aShapes2EntitiesMap;
277
278   SensitiveList aSEList;
279   TopoDS_Shape aSubShape;
280
281   // Fill in the map of subshapes and corresponding 
282   // sensitive entities associated with aMode 
283   for (aRefSel->Init(); aRefSel->More(); aRefSel->Next())
284   {
285     aSE = Handle(Select3D_SensitiveEntity)::DownCast (aRefSel->Sensitive()->BaseSensitive());
286     if(!aSE.IsNull())
287     {
288       anOwner = Handle(StdSelect_BRepOwner)::DownCast (aSE->OwnerId());
289       if(!anOwner.IsNull())
290       {
291         aSubShape = anOwner->Shape(); 
292         if(!aShapes2EntitiesMap.IsBound (aSubShape))
293         {
294           aShapes2EntitiesMap.Bind (aSubShape, aSEList);
295         }
296         aShapes2EntitiesMap (aSubShape).Append (aSE);
297       }
298     }
299   }
300
301   // Fill in selection from aShapes2EntitiesMap
302   for (Shapes2EntitiesMap::Iterator aMapIt (aShapes2EntitiesMap); aMapIt.More(); aMapIt.Next())
303   {
304     aSEList = aMapIt.Value();
305     anOwner = new StdSelect_BRepOwner (aMapIt.Key(), 
306                                        this, 
307                                        aSEList.First()->OwnerId()->Priority(), 
308                                        Standard_True);
309     anOwner->SetLocation (Transformation());
310
311     for (SensitiveList::Iterator aListIt (aSEList); aListIt.More(); aListIt.Next())
312     {
313       aSE = aListIt.Value();
314
315       aNewSE = aSE->GetConnected();
316       aNewSE->Set (anOwner);
317
318       theSelection->Add (aNewSE);
319     }
320   }
321
322   StdSelect::SetDrawerForBRepOwner (theSelection, myDrawer);  
323 }