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