0031642: Visualization - crash in Graphic3d_Structure::SetVisual() on redisplaying...
[occt.git] / src / TPrsStd / TPrsStd_NamedShapeDriver.cxx
1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 // last modified by SRN 01/08/2000    
15
16 #include <AIS_InteractiveContext.hxx>
17 #include <AIS_InteractiveObject.hxx>
18 #include <AIS_Shape.hxx>
19 #include <Prs3d_Presentation.hxx>
20 #include <PrsMgr_Presentation.hxx>
21 #include <PrsMgr_PresentationManager.hxx>
22 #include <Standard_GUID.hxx>
23 #include <Standard_Type.hxx>
24 #include <TColStd_ListIteratorOfListOfInteger.hxx>
25 #include <TColStd_ListOfInteger.hxx>
26 #include <TDataStd.hxx>
27 #include <TDF_Label.hxx>
28 #include <TNaming_NamedShape.hxx>
29 #include <TNaming_Tool.hxx>
30 #include <TopLoc_Location.hxx>
31 #include <TPrsStd_DriverTable.hxx>
32 #include <TPrsStd_NamedShapeDriver.hxx>
33
34 IMPLEMENT_STANDARD_RTTIEXT(TPrsStd_NamedShapeDriver,TPrsStd_Driver)
35
36 #undef OPTIM_UPDATE  // If this variable is defined there will be done
37 //                      more otimized update of AIS_Shape. If an object was
38 //                      erased in the viewer and it's location was changed
39 //                      but topological data wasn't then when displayed only 
40 //                      the object's presentation will be moved to new location
41 //                      without recompute. The shape in AIS_Shape will 
42 //                      be the previous one with the old location.       
43 //                      NOTE! After selection of sub shapes of the object
44 //                      they will have THE OLD LOCATION and it has to be
45 //                      compared with location of AIS_Shape that will contain
46 //                      the right location of shape. 
47  
48
49 //=======================================================================
50 //function :
51 //purpose  : 
52 //=======================================================================
53 TPrsStd_NamedShapeDriver::TPrsStd_NamedShapeDriver()
54 {
55 }
56
57 //=======================================================================
58 //function :
59 //purpose  : 
60 //=======================================================================
61 Standard_Boolean TPrsStd_NamedShapeDriver::Update (const TDF_Label& aLabel,
62                                                   Handle(AIS_InteractiveObject)& AIS) 
63 {
64   Handle(TNaming_NamedShape) NS;
65
66   if( !aLabel.FindAttribute(TNaming_NamedShape::GetID(), NS) )  {
67     return Standard_False;  
68   }
69
70   //TopoDS_Shape S = TNaming_Tool::CurrentShape (NS);
71   TopoDS_Shape S = TNaming_Tool::GetShape (NS);
72   if(S.IsNull()){ 
73     return Standard_False; 
74   }  
75   TopLoc_Location L  = S.Location();
76
77   Handle(AIS_Shape) AISShape;
78   if (AIS.IsNull()) AISShape = new AIS_Shape(S);
79   else {
80     AISShape = Handle(AIS_Shape)::DownCast(AIS);
81     if (AISShape.IsNull()) {
82       AISShape = new AIS_Shape(S);
83     }
84     else {
85       TopoDS_Shape oldShape = AISShape->Shape();
86       if(oldShape != S) {
87         AISShape->ResetTransformation();
88
89 #ifdef OPTIM_UPDATE
90         Handle(AIS_InteractiveContext) ctx = AISShape->GetContext();
91         if(S.IsPartner(oldShape) && (!ctx.IsNull() && !ctx->IsDisplayed(AISShape))) {
92           if(L != oldShape.Location()) ctx->SetLocation(AISShape, L);
93         }
94         else {
95           AISShape->Set(S);
96           AISShape->UpdateSelection();
97           AISShape->SetToUpdate();        
98         }
99 #else
100         AISShape->Set(S);
101         AISShape->UpdateSelection();
102         AISShape->SetToUpdate();      
103 #endif
104       }
105     }
106
107     AISShape->SetInfiniteState(S.Infinite());
108   }
109   AIS = AISShape;
110   return Standard_True;
111  
112 }
113