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