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