0024023: Revamp the OCCT Handle -- downcast (automatic)
[occt.git] / src / AIS / AIS_MultipleConnectedInteractive.cxx
1 // Created on: 1997-04-22
2 // Created by: Guest Design
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 <Standard_NotImplemented.hxx>
18
19 #include <AIS_MultipleConnectedInteractive.ixx>
20 #include <AIS_ConnectedInteractive.hxx>
21 #include <AIS_InteractiveContext.hxx>
22
23 #include <PrsMgr_ModedPresentation.hxx>
24 #include <PrsMgr_Presentation.hxx>
25 #include <SelectMgr_EntityOwner.hxx>
26 #include <Select3D_SensitiveEntity.hxx>
27 #include <TopLoc_Location.hxx>
28 #include <NCollection_DataMap.hxx>
29 #include <AIS_InteractiveObject.hxx>
30
31 namespace
32 {
33   //! SelectMgr_AssemblyEntityOwner replaces original owners in sensitive entities
34   //! copied from reference objects to AIS_MultipleConnectedInteractive in order to
35   //! redirect all selection queries to multiply connected (assembly).
36   class SelectMgr_AssemblyEntityOwner : public SelectMgr_EntityOwner
37   {
38
39   public:
40
41     // Copies another SelectMgr_EntityOwner.
42     SelectMgr_AssemblyEntityOwner (const Handle(SelectMgr_EntityOwner) theOwner,
43                                    SelectMgr_SelectableObject* theAssembly);
44
45     void SetAssembly (SelectMgr_SelectableObject* theAssembly)
46     {
47       myAssembly = theAssembly;
48     }
49
50     //! Selectable() method modified to return myAssembly.
51     virtual Handle(SelectMgr_SelectableObject) Selectable() const;
52
53     Standard_Boolean IsHilighted (const Handle(PrsMgr_PresentationManager)& PM,const Standard_Integer aMode) const;
54
55     void Hilight (const Handle(PrsMgr_PresentationManager)& PM,const Standard_Integer aMode);
56
57     void HilightWithColor (const Handle(PrsMgr_PresentationManager3d)& PM,
58                            const Quantity_NameOfColor aColor,
59                            const Standard_Integer aMode);
60
61     void Unhilight (const Handle(PrsMgr_PresentationManager)& PM, const Standard_Integer aMode);
62
63   private:
64
65     SelectMgr_SelectableObject* myAssembly;
66   };
67
68 }
69
70 //=======================================================================
71 //function : SelectMgr_AssemblyEntityOwner
72 //purpose  : 
73 //=======================================================================
74 SelectMgr_AssemblyEntityOwner::SelectMgr_AssemblyEntityOwner (const Handle(SelectMgr_EntityOwner) theOwner,
75                                                               SelectMgr_SelectableObject* theAssembly)
76 :
77   SelectMgr_EntityOwner (theOwner),
78   myAssembly (theAssembly)
79 {
80 }
81
82 //=======================================================================
83 //function : Selectable
84 //purpose  : 
85 //=======================================================================
86 Handle(SelectMgr_SelectableObject) SelectMgr_AssemblyEntityOwner::Selectable() const
87 {  
88   return myAssembly;
89 }
90
91 //=======================================================================
92 //function : IsHilighted
93 //purpose  : 
94 //=======================================================================
95 Standard_Boolean SelectMgr_AssemblyEntityOwner::IsHilighted (const Handle(PrsMgr_PresentationManager)& PM,
96                                                              const Standard_Integer aMode) const 
97 {
98   if (HasSelectable())
99   {
100    return PM->IsHighlighted (myAssembly, aMode);
101   }
102
103   return Standard_False;
104 }
105
106 //=======================================================================
107 //function : Hilight
108 //purpose  : 
109 //=======================================================================
110 void SelectMgr_AssemblyEntityOwner::Hilight (const Handle(PrsMgr_PresentationManager)& PM,
111                                              const Standard_Integer aMode)
112 {
113   if (HasSelectable())
114   {
115    PM->Highlight (myAssembly, aMode);
116   }
117 }
118
119 //=======================================================================
120 //function : HilightWithColor
121 //purpose  : 
122 //=======================================================================
123 void SelectMgr_AssemblyEntityOwner::HilightWithColor (const Handle(PrsMgr_PresentationManager3d)& PM,
124                                                       const Quantity_NameOfColor aColor,
125                                                       const Standard_Integer aMode)
126 {
127   if (HasSelectable())
128   {
129     if (IsAutoHilight())
130     {
131       PM->Color (myAssembly, aColor, aMode);
132     }
133     else
134     {
135       myAssembly->HilightOwnerWithColor (PM, aColor, this);
136     }
137   }
138 }
139
140 //=======================================================================
141 //function : Unhilight
142 //purpose  : 
143 //=======================================================================
144 void SelectMgr_AssemblyEntityOwner::Unhilight (const Handle(PrsMgr_PresentationManager)& PM,
145                                                const Standard_Integer aMode)
146 {
147   if (HasSelectable())
148   {
149     PM->Unhighlight (myAssembly, aMode);
150   }
151 }
152
153
154 //=======================================================================
155 //function : AIS_MultipleConnectedInteractive
156 //purpose  : 
157 //=======================================================================
158
159 AIS_MultipleConnectedInteractive::AIS_MultipleConnectedInteractive()
160   : AIS_InteractiveObject (PrsMgr_TOP_AllView)
161 {
162   myHasOwnPresentations = Standard_False;
163   myAssemblyOwner = NULL;
164   SetHilightMode (0);
165 }
166
167 //=======================================================================
168 //function : Type
169 //purpose  : 
170 //=======================================================================
171 AIS_KindOfInteractive AIS_MultipleConnectedInteractive::Type() const
172 {
173   return AIS_KOI_Object;
174 }
175
176 //=======================================================================
177 //function : Signature
178 //purpose  : 
179 //=======================================================================
180 Standard_Integer AIS_MultipleConnectedInteractive::Signature() const
181 {
182   return 1;
183 }
184
185 //=======================================================================
186 //function : Connect
187 //purpose  :
188 //=======================================================================
189 Handle(AIS_InteractiveObject) AIS_MultipleConnectedInteractive::Connect (const Handle(AIS_InteractiveObject)& theAnotherObj,
190                                                                          const gp_Trsf&                       theTransformation,
191                                                                          const Graphic3d_TransModeFlags&      theTrsfPersFlag,
192                                                                          const gp_Pnt&                        theTrsfPersPoint)
193 {
194   if (myAssemblyOwner.IsNull())
195     myAssemblyOwner = new SelectMgr_EntityOwner (this);
196
197   Handle(AIS_InteractiveObject) anObjectToAdd;
198
199   Handle(AIS_MultipleConnectedInteractive) aMultiConnected = Handle(AIS_MultipleConnectedInteractive)::DownCast (theAnotherObj);
200   if (!aMultiConnected.IsNull())
201   { 
202     Handle(AIS_MultipleConnectedInteractive) aNewMultiConnected = new AIS_MultipleConnectedInteractive();
203     aNewMultiConnected->myAssemblyOwner = myAssemblyOwner;
204     aNewMultiConnected->SetLocalTransformation (aMultiConnected->LocalTransformation());
205
206     // Perform deep copy of instance tree
207     for (PrsMgr_ListOfPresentableObjectsIter anIter (aMultiConnected->Children()); anIter.More(); anIter.Next())
208     {
209       Handle(AIS_InteractiveObject) anInteractive = Handle(AIS_InteractiveObject)::DownCast (anIter.Value());
210       if (anInteractive.IsNull())
211       {
212         continue;
213       }
214
215       aNewMultiConnected->Connect (anInteractive);     
216     }
217
218     anObjectToAdd = aNewMultiConnected;
219   }
220   else
221   {
222     Handle(AIS_ConnectedInteractive) aNewConnected = new AIS_ConnectedInteractive();
223     aNewConnected->Connect (theAnotherObj);
224     aNewConnected->SetLocalTransformation (theAnotherObj->LocalTransformation());
225
226     anObjectToAdd = aNewConnected;
227   }
228
229   anObjectToAdd->SetLocalTransformation (theTransformation);
230   if (theTrsfPersFlag != Graphic3d_TMF_None)
231   {
232     anObjectToAdd->SetTransformPersistence (theTrsfPersFlag, theTrsfPersPoint);
233   }
234   AddChild (anObjectToAdd);
235   return anObjectToAdd;
236 }
237
238 //=======================================================================
239 //function : Connect
240 //purpose  :
241 //=======================================================================
242 Handle(AIS_InteractiveObject) AIS_MultipleConnectedInteractive::Connect (const Handle(AIS_InteractiveObject)& theAnotherObj)
243 {
244   return Connect (theAnotherObj, theAnotherObj->LocalTransformation(),
245                   theAnotherObj->GetTransformPersistenceMode(),
246                   theAnotherObj->GetTransformPersistencePoint());
247 }
248
249 //=======================================================================
250 //function : Connect
251 //purpose  :
252 //=======================================================================
253 Handle(AIS_InteractiveObject) AIS_MultipleConnectedInteractive::Connect (const Handle(AIS_InteractiveObject)& theAnotherObj,
254                                                                          const gp_Trsf&                       theTransformation)
255 {
256   return Connect (theAnotherObj, theTransformation,
257                   theAnotherObj->GetTransformPersistenceMode(),
258                   theAnotherObj->GetTransformPersistencePoint());
259 }
260
261 //=======================================================================
262 //function : HasConnection
263 //purpose  : 
264 //=======================================================================
265 Standard_Boolean AIS_MultipleConnectedInteractive::HasConnection() const 
266 {
267   return (Children().Size() != 0);
268 }
269
270 //=======================================================================
271 //function : Disconnect
272 //purpose  : 
273 //=======================================================================
274
275 void AIS_MultipleConnectedInteractive::Disconnect(const Handle(AIS_InteractiveObject)& anotherIObj)
276 {
277   RemoveChild (anotherIObj);
278 }
279
280 //=======================================================================
281 //function : DisconnectAll
282 //purpose  : 
283 //=======================================================================
284
285 void AIS_MultipleConnectedInteractive::DisconnectAll()
286 {
287   Standard_Integer aNbItemsToRemove = Children().Size();
288   for (Standard_Integer anIter = 0; anIter < aNbItemsToRemove; ++anIter)
289   {
290     RemoveChild (Children().First());
291   }
292 }
293
294 //=======================================================================
295 //function : Compute
296 //purpose  :
297 //=======================================================================
298 void AIS_MultipleConnectedInteractive::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePrsMgr*/,
299                                                 const Handle(Prs3d_Presentation)&           /*thePrs*/,
300                                                 const Standard_Integer                      /*theMode*/)
301 {
302   for (PrsMgr_ListOfPresentableObjectsIter anIter (Children()); anIter.More(); anIter.Next())
303   {
304     Handle(AIS_InteractiveObject) aChild = Handle(AIS_InteractiveObject)::DownCast (anIter.Value());
305     if (aChild.IsNull())
306     {
307       continue;
308     }
309
310     if (!aChild->HasInteractiveContext())
311     {
312       aChild->SetContext (GetContext());
313     }
314   }
315 }
316
317 //=======================================================================
318 //function : Compute
319 //purpose  : 
320 //=======================================================================
321
322 void AIS_MultipleConnectedInteractive::Compute(const Handle(Prs3d_Projector)& aProjector,
323                                                const Handle(Prs3d_Presentation)& aPresentation)
324 {
325   PrsMgr_PresentableObject::Compute( aProjector , aPresentation ) ;
326 }
327
328 //=======================================================================
329 //function : Compute
330 //purpose  : 
331 //=======================================================================
332
333 void AIS_MultipleConnectedInteractive::Compute(const Handle(Prs3d_Projector)& aProjector,
334                                                const Handle(Geom_Transformation)& aTransformation,
335                                                const Handle(Prs3d_Presentation)& aPresentation)
336 {
337   PrsMgr_PresentableObject::Compute( aProjector , aTransformation , aPresentation ) ;
338 }
339
340 //=======================================================================
341 //function : AcceptShapeDecomposition
342 //purpose  : 
343 //=======================================================================
344 Standard_Boolean AIS_MultipleConnectedInteractive::AcceptShapeDecomposition() const 
345 {
346   for (PrsMgr_ListOfPresentableObjectsIter anIter (Children()); anIter.More(); anIter.Next())
347   {
348     Handle(AIS_InteractiveObject) aChild = Handle(AIS_InteractiveObject)::DownCast (anIter.Value());
349     if (aChild.IsNull())
350     {
351       continue;
352     }
353
354     if (aChild->AcceptShapeDecomposition())
355     {
356       return Standard_True;
357     }
358   }
359   return Standard_False;
360 }
361
362 //=======================================================================
363 //function : ComputeSelection
364 //purpose  : 
365 //=======================================================================
366 void AIS_MultipleConnectedInteractive::ComputeSelection (const Handle(SelectMgr_Selection)& /*theSelection*/,
367                                                          const Standard_Integer             theMode)
368 {
369   if (theMode != 0)
370   {
371     for (PrsMgr_ListOfPresentableObjectsIter anIter (Children()); anIter.More(); anIter.Next())
372     {
373       Handle(AIS_InteractiveObject) aChild = Handle(AIS_InteractiveObject)::DownCast (anIter.Value());
374       if (aChild.IsNull())
375       {
376         continue;
377       }
378
379       if (!aChild->HasSelection (theMode))
380       {
381         aChild->RecomputePrimitives (theMode);
382       }
383
384       Handle(SelectMgr_Selection) aSelection = new SelectMgr_Selection (theMode);
385       aChild->ComputeSelection (aSelection, theMode);
386     }
387   }
388 }