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.
18 #include <AIS_ConnectedInteractive.hxx>
19 #include <AIS_InteractiveContext.hxx>
20 #include <AIS_InteractiveObject.hxx>
21 #include <AIS_Shape.hxx>
22 #include <BRepTools.hxx>
23 #include <Geom_Transformation.hxx>
24 #include <gp_Trsf.hxx>
25 #include <NCollection_DataMap.hxx>
26 #include <Precision.hxx>
27 #include <Prs3d_Drawer.hxx>
28 #include <Prs3d_Presentation.hxx>
29 #include <Prs3d_Projector.hxx>
30 #include <PrsMgr_ModedPresentation.hxx>
31 #include <PrsMgr_Presentation.hxx>
32 #include <Select3D_SensitiveEntity.hxx>
33 #include <SelectMgr_EntityOwner.hxx>
34 #include <SelectMgr_Selection.hxx>
35 #include <Standard_NotImplemented.hxx>
36 #include <Standard_ProgramError.hxx>
37 #include <Standard_Type.hxx>
38 #include <StdPrs_HLRPolyShape.hxx>
39 #include <StdPrs_WFDeflectionShape.hxx>
40 #include <StdSelect.hxx>
41 #include <StdSelect_BRepOwner.hxx>
42 #include <TopAbs_ShapeEnum.hxx>
43 #include <TopoDS_Shape.hxx>
44 #include <TopTools_IndexedMapOfShape.hxx>
45 #include <TopTools_OrientedShapeMapHasher.hxx>
47 //=======================================================================
48 //function : AIS_ConnectedInteractive
50 //=======================================================================
51 AIS_ConnectedInteractive::AIS_ConnectedInteractive(const PrsMgr_TypeOfPresentation3d aTypeOfPresentation3d):
52 AIS_InteractiveObject(aTypeOfPresentation3d)
57 //=======================================================================
60 //=======================================================================
61 AIS_KindOfInteractive AIS_ConnectedInteractive::Type() const
63 return AIS_KOI_Object;
66 Standard_Integer AIS_ConnectedInteractive::Signature() const
72 //=======================================================================
75 //=======================================================================
76 void AIS_ConnectedInteractive::Connect (const Handle(AIS_InteractiveObject)& theAnotherObj)
78 if (myReference == theAnotherObj) return;
80 Handle(AIS_ConnectedInteractive) aConnected = Handle(AIS_ConnectedInteractive)::DownCast (theAnotherObj);
81 if (!aConnected.IsNull())
83 myReference = aConnected->myReference;
85 else if (theAnotherObj->HasOwnPresentations())
87 myReference = theAnotherObj;
91 Standard_ProgramError::Raise ("AIS_ConnectedInteractive::Connect() - object without own presentation can not be connected");
94 if (!myReference.IsNull())
96 myTypeOfPresentation3d = myReference->TypeOfPresentation3d();
100 //=======================================================================
103 //=======================================================================
104 void AIS_ConnectedInteractive::Connect (const Handle(AIS_InteractiveObject)& theAnotherObj,
105 const gp_Trsf& theLocation)
107 Connect (theAnotherObj);
109 SetLocalTransformation (theLocation);
113 //=======================================================================
114 //function : Disconnect
116 //=======================================================================
118 void AIS_ConnectedInteractive::Disconnect()
120 for(Standard_Integer aPrsIter = 1; aPrsIter <= myPresentations.Length(); ++aPrsIter)
122 const Handle(PrsMgr_Presentation)& aPrs = myPresentations (aPrsIter).Presentation();
125 aPrs->Presentation()->DisconnectAll (Graphic3d_TOC_DESCENDANT);
129 //=======================================================================
132 //=======================================================================
133 void AIS_ConnectedInteractive::Compute (const Handle(PrsMgr_PresentationManager3d)& thePrsMgr,
134 const Handle(Prs3d_Presentation)& thePrs,
135 const Standard_Integer theMode)
139 thePrs->Clear (Standard_False);
142 if (!myReference->HasInteractiveContext())
144 myReference->SetContext (GetContext());
146 thePrsMgr->Connect (this, myReference, theMode, theMode);
147 if (thePrsMgr->Presentation (myReference, theMode)->MustBeUpdated())
149 thePrsMgr->Update (myReference, theMode);
153 if (!thePrs.IsNull())
159 //=======================================================================
162 //=======================================================================
163 void AIS_ConnectedInteractive::Compute (const Handle(Prs3d_Projector)& theProjector,
164 const Handle(Geom_Transformation)& theTransformation,
165 const Handle(Prs3d_Presentation)& thePresentation)
167 updateShape (Standard_False);
168 if (myShape.IsNull())
172 const TopLoc_Location& aLocation = myShape.Location();
173 TopoDS_Shape aShape = myShape.Located (TopLoc_Location (theTransformation->Trsf()) * aLocation);
174 Compute (theProjector, thePresentation, aShape);
177 //=======================================================================
180 //=======================================================================
181 void AIS_ConnectedInteractive::Compute(const Handle(Prs3d_Projector)& aProjector, const Handle(Prs3d_Presentation)& aPresentation)
183 updateShape (Standard_True);
184 Compute (aProjector, aPresentation, myShape);
187 //=======================================================================
190 //=======================================================================
191 void AIS_ConnectedInteractive::Compute (const Handle(Prs3d_Projector)& theProjector,
192 const Handle(Prs3d_Presentation)& thePresentation,
193 const TopoDS_Shape& theShape)
195 if (myShape.IsNull())
200 switch (theShape.ShapeType())
206 thePresentation->SetDisplayPriority (4);
207 StdPrs_WFDeflectionShape::Add (thePresentation, theShape, myDrawer);
212 Handle(Prs3d_Drawer) aDefaultDrawer = GetContext()->DefaultDrawer();
213 if (aDefaultDrawer->DrawHiddenLine())
215 myDrawer->EnableDrawHiddenLine();
219 myDrawer->DisableDrawHiddenLine();
222 Aspect_TypeOfDeflection aPrevDeflection = aDefaultDrawer->TypeOfDeflection();
223 aDefaultDrawer->SetTypeOfDeflection(Aspect_TOD_RELATIVE);
225 // process HLRAngle and HLRDeviationCoefficient()
226 Standard_Real aPrevAngle = myDrawer->HLRAngle();
227 Standard_Real aNewAngle = aDefaultDrawer->HLRAngle();
228 if (myDrawer->IsAutoTriangulation() &&
229 Abs (aNewAngle - aPrevAngle) > Precision::Angular())
231 BRepTools::Clean (theShape);
234 myDrawer->SetHLRAngle (aNewAngle);
235 myDrawer->SetHLRDeviationCoefficient (aDefaultDrawer->HLRDeviationCoefficient());
236 StdPrs_HLRPolyShape::Add (thePresentation, theShape, myDrawer, theProjector);
237 aDefaultDrawer->SetTypeOfDeflection (aPrevDeflection);
242 //=======================================================================
243 //function : updateShape
245 //=======================================================================
246 void AIS_ConnectedInteractive::updateShape (const Standard_Boolean isWithLocation)
248 Handle(AIS_Shape) anAisShape = Handle(AIS_Shape)::DownCast (myReference);
249 if (anAisShape.IsNull())
254 TopoDS_Shape aShape = anAisShape->Shape();
266 myShape = aShape.Moved (TopLoc_Location (Transformation()));
270 //=======================================================================
271 //function : ComputeSelection
273 //=======================================================================
274 void AIS_ConnectedInteractive::ComputeSelection (const Handle(SelectMgr_Selection)& theSelection,
275 const Standard_Integer theMode)
277 if (!HasConnection())
282 if (theMode != 0 && myReference->AcceptShapeDecomposition())
284 computeSubShapeSelection (theSelection, theMode);
288 if (!myReference->HasSelection (theMode))
290 myReference->RecomputePrimitives (theMode);
293 const Handle(SelectMgr_Selection)& TheRefSel = myReference->Selection (theMode);
294 Handle(SelectMgr_EntityOwner) anOwner = new SelectMgr_EntityOwner (this);
295 Handle(Select3D_SensitiveEntity) aSensitive, aNewSensitive;
297 TopLoc_Location aLocation (Transformation());
298 anOwner->SetLocation (aLocation);
300 if (TheRefSel->IsEmpty())
302 myReference->RecomputePrimitives (theMode);
305 for (TheRefSel->Init(); TheRefSel->More(); TheRefSel->Next())
307 aSensitive = Handle(Select3D_SensitiveEntity)::DownCast (TheRefSel->Sensitive()->BaseSensitive());
308 if (!aSensitive.IsNull())
310 // Get the copy of SE3D
311 aNewSensitive = aSensitive->GetConnected();
313 aNewSensitive->Set(anOwner);
315 theSelection->Add (aNewSensitive);
320 //=======================================================================
321 //function : ComputeSubShapeSelection
323 //=======================================================================
324 void AIS_ConnectedInteractive::computeSubShapeSelection (const Handle(SelectMgr_Selection)& theSelection,
325 const Standard_Integer theMode)
327 typedef NCollection_List<Handle(Select3D_SensitiveEntity)> SensitiveList;
328 typedef NCollection_DataMap<TopoDS_Shape, SensitiveList, TopTools_OrientedShapeMapHasher>
331 if (!myReference->HasSelection (theMode))
332 myReference->RecomputePrimitives (theMode);
334 const Handle(SelectMgr_Selection)& aRefSel = myReference->Selection (theMode);
336 if (aRefSel->IsEmpty() || aRefSel->UpdateStatus() == SelectMgr_TOU_Full)
338 myReference->RecomputePrimitives (theMode);
341 Handle(StdSelect_BRepOwner) anOwner;
342 TopLoc_Location aDummyLoc;
344 Handle(Select3D_SensitiveEntity) aSE, aNewSE;
345 Shapes2EntitiesMap aShapes2EntitiesMap;
347 SensitiveList aSEList;
348 TopoDS_Shape aSubShape;
350 // Fill in the map of subshapes and corresponding
351 // sensitive entities associated with aMode
352 for (aRefSel->Init(); aRefSel->More(); aRefSel->Next())
354 aSE = Handle(Select3D_SensitiveEntity)::DownCast (aRefSel->Sensitive()->BaseSensitive());
357 anOwner = Handle(StdSelect_BRepOwner)::DownCast (aSE->OwnerId());
358 if(!anOwner.IsNull())
360 aSubShape = anOwner->Shape();
361 if(!aShapes2EntitiesMap.IsBound (aSubShape))
363 aShapes2EntitiesMap.Bind (aSubShape, aSEList);
365 aShapes2EntitiesMap (aSubShape).Append (aSE);
370 // Fill in selection from aShapes2EntitiesMap
371 for (Shapes2EntitiesMap::Iterator aMapIt (aShapes2EntitiesMap); aMapIt.More(); aMapIt.Next())
373 aSEList = aMapIt.Value();
374 anOwner = new StdSelect_BRepOwner (aMapIt.Key(),
376 aSEList.First()->OwnerId()->Priority(),
378 anOwner->SetLocation (Transformation());
380 for (SensitiveList::Iterator aListIt (aSEList); aListIt.More(); aListIt.Next())
382 aSE = aListIt.Value();
384 aNewSE = aSE->GetConnected();
385 aNewSE->Set (anOwner);
387 theSelection->Add (aNewSE);
391 StdSelect::SetDrawerForBRepOwner (theSelection, myDrawer);