1 // Created on: 1997-01-08
2 // Created by: Robert COUBLANC
3 // Copyright (c) 1997-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
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.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 #include <AIS_ConnectedInteractive.hxx>
19 #include <AIS_InteractiveContext.hxx>
20 #include <AIS_Shape.hxx>
21 #include <BRepTools.hxx>
22 #include <NCollection_DataMap.hxx>
23 #include <Precision.hxx>
24 #include <Prs3d_Drawer.hxx>
25 #include <Prs3d_Presentation.hxx>
26 #include <Select3D_SensitiveEntity.hxx>
27 #include <SelectMgr_EntityOwner.hxx>
28 #include <SelectMgr_Selection.hxx>
29 #include <Standard_NotImplemented.hxx>
30 #include <Standard_ProgramError.hxx>
31 #include <Standard_Type.hxx>
32 #include <StdPrs_HLRPolyShape.hxx>
33 #include <StdPrs_WFShape.hxx>
34 #include <StdSelect.hxx>
35 #include <StdSelect_BRepOwner.hxx>
36 #include <TopAbs_ShapeEnum.hxx>
37 #include <TopoDS_Shape.hxx>
38 #include <TopTools_IndexedMapOfShape.hxx>
39 #include <TopTools_OrientedShapeMapHasher.hxx>
41 IMPLEMENT_STANDARD_RTTIEXT(AIS_ConnectedInteractive,AIS_InteractiveObject)
43 //=======================================================================
44 //function : AIS_ConnectedInteractive
46 //=======================================================================
47 AIS_ConnectedInteractive::AIS_ConnectedInteractive(const PrsMgr_TypeOfPresentation3d aTypeOfPresentation3d):
48 AIS_InteractiveObject(aTypeOfPresentation3d)
53 //=======================================================================
56 //=======================================================================
57 void AIS_ConnectedInteractive::connect (const Handle(AIS_InteractiveObject)& theAnotherObj,
58 const Handle(TopLoc_Datum3D)& theLocation)
60 if (myReference == theAnotherObj)
62 setLocalTransformation (theLocation);
66 Handle(AIS_ConnectedInteractive) aConnected = Handle(AIS_ConnectedInteractive)::DownCast (theAnotherObj);
67 if (!aConnected.IsNull())
69 myReference = aConnected->myReference;
71 else if (theAnotherObj->HasOwnPresentations())
73 myReference = theAnotherObj;
77 throw Standard_ProgramError("AIS_ConnectedInteractive::Connect() - object without own presentation can not be connected");
80 if (!myReference.IsNull())
82 if (myReference->HasInteractiveContext()
83 && myReference->GetContext()->DisplayStatus (myReference) != AIS_DS_None)
85 myReference.Nullify();
86 throw Standard_ProgramError("AIS_ConnectedInteractive::Connect() - connected object should NOT be displayed in context");
88 myTypeOfPresentation3d = myReference->TypeOfPresentation3d();
90 setLocalTransformation (theLocation);
93 //=======================================================================
94 //function : Disconnect
96 //=======================================================================
98 void AIS_ConnectedInteractive::Disconnect()
100 for (PrsMgr_Presentations::Iterator aPrsIter (myPresentations); aPrsIter.More(); aPrsIter.Next())
102 const Handle(PrsMgr_Presentation)& aPrs = aPrsIter.Value();
105 aPrs->DisconnectAll (Graphic3d_TOC_DESCENDANT);
109 //=======================================================================
112 //=======================================================================
113 void AIS_ConnectedInteractive::Compute (const Handle(PrsMgr_PresentationManager)& thePrsMgr,
114 const Handle(Prs3d_Presentation)& thePrs,
115 const Standard_Integer theMode)
119 thePrs->Clear (Standard_False);
120 thePrs->DisconnectAll (Graphic3d_TOC_DESCENDANT);
122 if (!myReference->HasInteractiveContext())
124 myReference->SetContext (GetContext());
126 thePrsMgr->Connect (this, myReference, theMode, theMode);
127 if (thePrsMgr->Presentation (myReference, theMode)->MustBeUpdated())
129 thePrsMgr->Update (myReference, theMode);
133 if (!thePrs.IsNull())
139 //=======================================================================
140 //function : computeHLR
142 //=======================================================================
143 void AIS_ConnectedInteractive::computeHLR (const Handle(Graphic3d_Camera)& theProjector,
144 const Handle(TopLoc_Datum3D)& theTransformation,
145 const Handle(Prs3d_Presentation)& thePresentation)
147 const bool hasTrsf = !theTransformation.IsNull()
148 && theTransformation->Form() != gp_Identity;
149 updateShape (!hasTrsf);
150 if (myShape.IsNull())
156 const TopLoc_Location& aLocation = myShape.Location();
157 TopoDS_Shape aShape = myShape.Located (TopLoc_Location (theTransformation->Trsf()) * aLocation);
158 AIS_Shape::computeHlrPresentation (theProjector, thePresentation, aShape, myDrawer);
162 AIS_Shape::computeHlrPresentation (theProjector, thePresentation, myShape, myDrawer);
166 //=======================================================================
167 //function : updateShape
169 //=======================================================================
170 void AIS_ConnectedInteractive::updateShape (const Standard_Boolean isWithLocation)
172 Handle(AIS_Shape) anAisShape = Handle(AIS_Shape)::DownCast (myReference);
173 if (anAisShape.IsNull())
178 TopoDS_Shape aShape = anAisShape->Shape();
190 myShape = aShape.Moved (TopLoc_Location (Transformation()));
194 //=======================================================================
195 //function : ComputeSelection
197 //=======================================================================
198 void AIS_ConnectedInteractive::ComputeSelection (const Handle(SelectMgr_Selection)& theSelection,
199 const Standard_Integer theMode)
201 if (!HasConnection())
206 if (theMode != 0 && myReference->AcceptShapeDecomposition())
208 computeSubShapeSelection (theSelection, theMode);
212 if (!myReference->HasSelection (theMode))
214 myReference->RecomputePrimitives (theMode);
217 const Handle(SelectMgr_Selection)& TheRefSel = myReference->Selection (theMode);
218 Handle(SelectMgr_EntityOwner) anOwner = new SelectMgr_EntityOwner (this);
220 TopLoc_Location aLocation (Transformation());
221 anOwner->SetLocation (aLocation);
223 if (TheRefSel->IsEmpty())
225 myReference->RecomputePrimitives (theMode);
228 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (TheRefSel->Entities()); aSelEntIter.More(); aSelEntIter.Next())
230 if (const Handle(Select3D_SensitiveEntity)& aSensitive = aSelEntIter.Value()->BaseSensitive())
232 // Get the copy of SE3D
233 if (Handle(Select3D_SensitiveEntity) aNewSensitive = aSensitive->GetConnected())
235 aNewSensitive->Set(anOwner);
236 theSelection->Add (aNewSensitive);
242 //=======================================================================
243 //function : ComputeSubShapeSelection
245 //=======================================================================
246 void AIS_ConnectedInteractive::computeSubShapeSelection (const Handle(SelectMgr_Selection)& theSelection,
247 const Standard_Integer theMode)
249 typedef NCollection_List<Handle(Select3D_SensitiveEntity)> SensitiveList;
250 typedef NCollection_DataMap<TopoDS_Shape, SensitiveList, TopTools_OrientedShapeMapHasher>
253 if (!myReference->HasSelection (theMode))
255 myReference->RecomputePrimitives (theMode);
258 const Handle(SelectMgr_Selection)& aRefSel = myReference->Selection (theMode);
259 if (aRefSel->IsEmpty() || aRefSel->UpdateStatus() == SelectMgr_TOU_Full)
261 myReference->RecomputePrimitives (theMode);
264 // Fill in the map of subshapes and corresponding sensitive entities associated with aMode
265 Shapes2EntitiesMap aShapes2EntitiesMap;
266 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (aRefSel->Entities()); aSelEntIter.More(); aSelEntIter.Next())
268 if (const Handle(Select3D_SensitiveEntity)& aSE = aSelEntIter.Value()->BaseSensitive())
270 if (Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast (aSE->OwnerId()))
272 const TopoDS_Shape& aSubShape = anOwner->Shape();
273 if(!aShapes2EntitiesMap.IsBound (aSubShape))
275 aShapes2EntitiesMap.Bind (aSubShape, SensitiveList());
277 aShapes2EntitiesMap (aSubShape).Append (aSE);
282 // Fill in selection from aShapes2EntitiesMap
283 for (Shapes2EntitiesMap::Iterator aMapIt (aShapes2EntitiesMap); aMapIt.More(); aMapIt.Next())
285 const SensitiveList& aSEList = aMapIt.Value();
286 Handle(StdSelect_BRepOwner) anOwner = new StdSelect_BRepOwner (aMapIt.Key(), this, aSEList.First()->OwnerId()->Priority(), Standard_True);
287 anOwner->SetLocation (Transformation());
288 for (SensitiveList::Iterator aListIt (aSEList); aListIt.More(); aListIt.Next())
290 if (Handle(Select3D_SensitiveEntity) aNewSE = aListIt.Value()->GetConnected())
292 aNewSE->Set (anOwner);
293 theSelection->Add (aNewSE);
298 StdSelect::SetDrawerForBRepOwner (theSelection, myDrawer);