OCC22144 NIS performance and memory usage update
[occt.git] / src / NIS / NIS_InteractiveObject.cxx
1 // File:      NIS_InteractiveObject.cxx
2 // Created:   07.07.07 11:24
3 // Author:    Alexander GRIGORIEV
4 // Copyright: Open Cascade 2007
5
6
7 #include <NIS_InteractiveObject.hxx>
8 #include <NIS_InteractiveContext.hxx>
9 #include <Standard_NullValue.hxx>
10
11 IMPLEMENT_STANDARD_HANDLE  (NIS_InteractiveObject, Standard_Transient)
12 IMPLEMENT_STANDARD_RTTIEXT (NIS_InteractiveObject, Standard_Transient)
13
14 //=======================================================================
15 //function : ~NIS_InteractiveObject()
16 //purpose  : Destructor
17 //=======================================================================
18
19 NIS_InteractiveObject::~NIS_InteractiveObject (  )
20 {
21   if (myID != 0 && myDrawer.IsNull() == Standard_False)
22     myDrawer->removeObject( this, Standard_False);
23 }
24
25 //=======================================================================
26 //function : SetDrawer
27 //purpose  : 
28 //=======================================================================
29
30 const Handle_NIS_Drawer& NIS_InteractiveObject::SetDrawer
31                                         (const Handle(NIS_Drawer)& theDrawer,
32                                          const Standard_Boolean    setUpdated)
33 {
34   NIS_InteractiveContext * aCtx = theDrawer->GetContext();
35   if (myDrawer.IsNull() == Standard_False)
36     if (aCtx == 0L)
37     {
38       aCtx = myDrawer->GetContext();
39       theDrawer->myCtx = aCtx;
40     }
41     else
42     {
43       Standard_NullValue_Raise_if
44         (myDrawer->GetContext() != 0L && aCtx != myDrawer->GetContext(),
45          "NIS_InteractiveObject::SetDrawer: different drawer context");
46     }
47
48   Standard_NullValue_Raise_if
49     (aCtx == 0L, "NIS_InteractiveObject::SetDrawer: NULL drawer context");
50   // Add (if necessary) the new drawer to the Context
51   if (theDrawer->myIniId == 0)
52     theDrawer->myIniId = myID;
53   const Handle(NIS_Drawer)& aDrawer = aCtx->myDrawers.Added (theDrawer);
54   if (myDrawer != aDrawer)
55   {
56     // Move the Object from the old drawer to the new one.
57     if (myDrawer.IsNull() == Standard_False)
58       myDrawer->removeObject(this, Standard_True);
59     myDrawer = aDrawer;
60
61     myDrawer->addObject(this, aCtx->myIsShareDrawList, Standard_True);
62   }
63   if (setUpdated)
64     myDrawer->SetUpdated (NIS_Drawer::Draw_Normal,
65                           NIS_Drawer::Draw_Top,
66                           NIS_Drawer::Draw_Transparent,
67                           NIS_Drawer::Draw_Hilighted);
68   return aDrawer;
69 }
70
71 //=======================================================================
72 //function : SetTransparency
73 //purpose  : 
74 //=======================================================================
75
76 void NIS_InteractiveObject::SetTransparency (const Standard_Real theValue)
77 {
78   Standard_Integer aValue =
79     static_cast<Standard_Integer> (theValue * MaxTransparency);
80   if (aValue != static_cast<Standard_Integer>(myTransparency))
81   {
82     if (aValue <= 0)
83       myTransparency = 0;
84     else if (aValue >= 1000)
85       myTransparency = 1000u;
86     else
87       myTransparency = static_cast<unsigned int> (aValue);
88
89     if (myDrawer.IsNull() == Standard_False && myID != 0) {
90       const Handle(NIS_Drawer) aDrawer = DefaultDrawer(0L);
91       aDrawer->Assign (GetDrawer());
92       aDrawer->myTransparency = Transparency();
93       SetDrawer (aDrawer, Standard_False);
94
95       NIS_InteractiveContext * aCtx = myDrawer->GetContext();
96       Standard_NullValue_Raise_if
97         (aCtx == 0L, "NIS_InteractiveObject::SetTransparency: "
98                      "NULL drawer context");
99       if (IsTransparent()) {
100         if (myDrawType == NIS_Drawer::Draw_Normal) {
101           aCtx->myMapObjects[NIS_Drawer::Draw_Transparent].Add(myID);
102           aCtx->myMapObjects[NIS_Drawer::Draw_Normal].Remove(myID);
103           myDrawType = NIS_Drawer::Draw_Transparent;
104         }
105         myDrawer->SetUpdated (NIS_Drawer::Draw_Normal);
106       } else {
107         if (myDrawType == NIS_Drawer::Draw_Transparent) {
108           aCtx->myMapObjects[NIS_Drawer::Draw_Normal].Add(myID);
109           aCtx->myMapObjects[NIS_Drawer::Draw_Transparent].Remove(myID);
110           myDrawType = NIS_Drawer::Draw_Normal;
111         }
112         myDrawer->SetUpdated (NIS_Drawer::Draw_Transparent);
113       }
114       setDrawerUpdate();
115     }
116   }
117 }
118
119 //=======================================================================
120 //function : GetBox
121 //purpose  : 
122 //=======================================================================
123
124 const Bnd_B3f& NIS_InteractiveObject::GetBox ()
125 {
126   if (myIsUpdateBox)
127   {
128     myIsUpdateBox = Standard_False;
129     computeBox();
130   }
131   return myBox;
132 }
133
134 //=======================================================================
135 //function : Clone
136 //purpose  : 
137 //=======================================================================
138
139 void NIS_InteractiveObject::Clone (const Handle_NCollection_BaseAllocator&,
140                                    Handle_NIS_InteractiveObject& theDest) const
141 {
142   if (theDest.IsNull() == Standard_False)
143   {
144     theDest->myID = 0;
145     theDest->myDrawer = myDrawer;
146     theDest->myDrawType = myDrawType;
147     theDest->myBaseType = myBaseType;
148     theDest->myIsHidden = myIsHidden;
149     theDest->myIsDynHilighted = myIsDynHilighted;
150     theDest->myIsUpdateBox = myIsUpdateBox;
151     theDest->myTransparency = myTransparency;
152     if (myIsUpdateBox == Standard_False)
153       theDest->myBox = myBox;
154     theDest->myAttributePtr = myAttributePtr;
155   }
156 }
157
158 //=======================================================================
159 //function : CloneWithID
160 //purpose  : 
161 //=======================================================================
162
163 void NIS_InteractiveObject::CloneWithID
164                         (const Handle_NCollection_BaseAllocator& theAlloc,
165                          Handle_NIS_InteractiveObject&           theDest)
166 {
167   Clone(theAlloc, theDest);
168   theDest->myID = myID;
169   myDrawer.Nullify();
170 }
171
172 //=======================================================================
173 //function : Intersect
174 //purpose  : 
175 //=======================================================================
176
177 Standard_Boolean NIS_InteractiveObject::Intersect (const Bnd_B3f&,
178                                                    const gp_Trsf&,
179                                                    const Standard_Boolean) const
180 {
181   return Standard_True;
182 }
183
184 //=======================================================================
185 //function : Intersect
186 //purpose  : 
187 //=======================================================================
188
189 Standard_Boolean NIS_InteractiveObject::Intersect
190                      (const NCollection_List<gp_XY> &thePolygon,
191                       const gp_Trsf                 &theTrf,
192                       const Standard_Boolean         isFull) const
193 {
194   return Standard_True;
195 }
196
197 //=======================================================================
198 //function : IsSelectable
199 //purpose  : Query if the Object is selectable.
200 //=======================================================================
201
202 Standard_Boolean NIS_InteractiveObject::IsSelectable () const
203 {
204   Standard_Boolean aResult(Standard_False);
205   if (myDrawer.IsNull() == Standard_False)
206     aResult = myDrawer->GetContext()->IsSelectable (myID);
207   return aResult;
208 }
209
210 //=======================================================================
211 //function : SetSelectable
212 //purpose  : Set or change the selectable state of the Object.
213 //=======================================================================
214
215 void NIS_InteractiveObject::SetSelectable (const Standard_Boolean isSel) const
216 {
217   if (myDrawer.IsNull() == Standard_False) {
218     NIS_InteractiveContext * aCtx = myDrawer->GetContext();
219     if (isSel)
220       aCtx->myMapNonSelectableObjects.Remove (myID);
221     else {
222       aCtx->myMapNonSelectableObjects.Add (myID);
223       if (myDrawType == NIS_Drawer::Draw_Hilighted)
224       {
225         aCtx->myMapObjects[NIS_Drawer::Draw_Hilighted].Remove(myID);
226         aCtx->deselectObj (this, myID);
227       }
228     }
229   }
230 }