0025180: Visualization - Homogeneous transformation API in TKV3d
[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 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 Handle(Geom_Transformation)& theTrsf,
191                                                                          const Handle(Graphic3d_TransformPers)& theTrsfPers)
192 {
193   if (myAssemblyOwner.IsNull())
194     myAssemblyOwner = new SelectMgr_EntityOwner (this);
195
196   Handle(AIS_InteractiveObject) anObjectToAdd;
197
198   Handle(AIS_MultipleConnectedInteractive) aMultiConnected = Handle(AIS_MultipleConnectedInteractive)::DownCast (theAnotherObj);
199   if (!aMultiConnected.IsNull())
200   { 
201     Handle(AIS_MultipleConnectedInteractive) aNewMultiConnected = new AIS_MultipleConnectedInteractive();
202     aNewMultiConnected->myAssemblyOwner = myAssemblyOwner;
203     aNewMultiConnected->SetLocalTransformation (aMultiConnected->LocalTransformationGeom());
204
205     // Perform deep copy of instance tree
206     for (PrsMgr_ListOfPresentableObjectsIter anIter (aMultiConnected->Children()); anIter.More(); anIter.Next())
207     {
208       Handle(AIS_InteractiveObject) anInteractive = Handle(AIS_InteractiveObject)::DownCast (anIter.Value());
209       if (anInteractive.IsNull())
210       {
211         continue;
212       }
213
214       aNewMultiConnected->Connect (anInteractive);     
215     }
216
217     anObjectToAdd = aNewMultiConnected;
218   }
219   else
220   {
221     Handle(AIS_ConnectedInteractive) aNewConnected = new AIS_ConnectedInteractive();
222     aNewConnected->Connect (theAnotherObj, theAnotherObj->LocalTransformationGeom());
223
224     anObjectToAdd = aNewConnected;
225   }
226
227   anObjectToAdd->SetLocalTransformation (theTrsf);
228   if (!theTrsfPers.IsNull())
229   {
230     anObjectToAdd->SetTransformPersistence (theTrsfPers);
231   }
232   AddChild (anObjectToAdd);
233   return anObjectToAdd;
234 }
235
236 //=======================================================================
237 //function : HasConnection
238 //purpose  : 
239 //=======================================================================
240 Standard_Boolean AIS_MultipleConnectedInteractive::HasConnection() const 
241 {
242   return (Children().Size() != 0);
243 }
244
245 //=======================================================================
246 //function : Disconnect
247 //purpose  : 
248 //=======================================================================
249
250 void AIS_MultipleConnectedInteractive::Disconnect(const Handle(AIS_InteractiveObject)& anotherIObj)
251 {
252   RemoveChild (anotherIObj);
253 }
254
255 //=======================================================================
256 //function : DisconnectAll
257 //purpose  : 
258 //=======================================================================
259
260 void AIS_MultipleConnectedInteractive::DisconnectAll()
261 {
262   Standard_Integer aNbItemsToRemove = Children().Size();
263   for (Standard_Integer anIter = 0; anIter < aNbItemsToRemove; ++anIter)
264   {
265     RemoveChild (Children().First());
266   }
267 }
268
269 //=======================================================================
270 //function : Compute
271 //purpose  :
272 //=======================================================================
273 void AIS_MultipleConnectedInteractive::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePrsMgr*/,
274                                                 const Handle(Prs3d_Presentation)&           /*thePrs*/,
275                                                 const Standard_Integer                      /*theMode*/)
276 {
277   for (PrsMgr_ListOfPresentableObjectsIter anIter (Children()); anIter.More(); anIter.Next())
278   {
279     Handle(AIS_InteractiveObject) aChild = Handle(AIS_InteractiveObject)::DownCast (anIter.Value());
280     if (aChild.IsNull())
281     {
282       continue;
283     }
284
285     if (!aChild->HasInteractiveContext())
286     {
287       aChild->SetContext (GetContext());
288     }
289   }
290 }
291
292 //=======================================================================
293 //function : Compute
294 //purpose  : 
295 //=======================================================================
296
297 void AIS_MultipleConnectedInteractive::Compute(const Handle(Prs3d_Projector)& aProjector,
298                                                const Handle(Prs3d_Presentation)& aPresentation)
299 {
300   PrsMgr_PresentableObject::Compute( aProjector , aPresentation ) ;
301 }
302
303 //=======================================================================
304 //function : Compute
305 //purpose  : 
306 //=======================================================================
307
308 void AIS_MultipleConnectedInteractive::Compute(const Handle(Prs3d_Projector)& aProjector,
309                                                const Handle(Geom_Transformation)& aTransformation,
310                                                const Handle(Prs3d_Presentation)& aPresentation)
311 {
312   PrsMgr_PresentableObject::Compute( aProjector , aTransformation , aPresentation ) ;
313 }
314
315 //=======================================================================
316 //function : AcceptShapeDecomposition
317 //purpose  : 
318 //=======================================================================
319 Standard_Boolean AIS_MultipleConnectedInteractive::AcceptShapeDecomposition() const 
320 {
321   for (PrsMgr_ListOfPresentableObjectsIter anIter (Children()); anIter.More(); anIter.Next())
322   {
323     Handle(AIS_InteractiveObject) aChild = Handle(AIS_InteractiveObject)::DownCast (anIter.Value());
324     if (aChild.IsNull())
325     {
326       continue;
327     }
328
329     if (aChild->AcceptShapeDecomposition())
330     {
331       return Standard_True;
332     }
333   }
334   return Standard_False;
335 }
336
337 //=======================================================================
338 //function : ComputeSelection
339 //purpose  : 
340 //=======================================================================
341 void AIS_MultipleConnectedInteractive::ComputeSelection (const Handle(SelectMgr_Selection)& /*theSelection*/,
342                                                          const Standard_Integer             theMode)
343 {
344   if (theMode != 0)
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->HasSelection (theMode))
355       {
356         aChild->RecomputePrimitives (theMode);
357       }
358
359       Handle(SelectMgr_Selection) aSelection = new SelectMgr_Selection (theMode);
360       aChild->ComputeSelection (aSelection, theMode);
361     }
362   }
363 }
364
365 //=======================================================================
366 //function : GlobalSelOwner
367 //purpose  :
368 //=======================================================================
369 Handle(SelectMgr_EntityOwner) AIS_MultipleConnectedInteractive::GlobalSelOwner() const
370 {
371   return myAssemblyOwner;
372 }
373
374 //=======================================================================
375 //function : HasSelection
376 //purpose  :
377 //=======================================================================
378 Standard_Boolean AIS_MultipleConnectedInteractive::HasSelection (const Standard_Integer theMode) const
379 {
380   for (PrsMgr_ListOfPresentableObjectsIter anIter (Children()); anIter.More(); anIter.Next())
381   {
382     Handle(AIS_InteractiveObject) aChild = Handle(AIS_InteractiveObject)::DownCast (anIter.Value());
383     if (aChild.IsNull())
384       continue;
385
386     if (!aChild->HasSelection (theMode))
387       return Standard_False;
388   }
389
390   return Standard_True;
391 }