793e1ae0a6a6f1c18db8f4ce9963d3059c749f1c
[occt.git] / src / NIS / NIS_InteractiveObject.cxx
1 // Created on: 2007-07-07
2 // Created by: Alexander GRIGORIEV
3 // Copyright (c) 2007-2012 OPEN CASCADE SAS
4 //
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
9 //
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 //
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
19
20
21
22 #include <NIS_InteractiveObject.hxx>
23 #include <NIS_InteractiveContext.hxx>
24 #include <Standard_NullValue.hxx>
25
26 IMPLEMENT_STANDARD_HANDLE  (NIS_InteractiveObject, Standard_Transient)
27 IMPLEMENT_STANDARD_RTTIEXT (NIS_InteractiveObject, Standard_Transient)
28
29 //=======================================================================
30 //function : ~NIS_InteractiveObject()
31 //purpose  : Destructor
32 //=======================================================================
33
34 NIS_InteractiveObject::~NIS_InteractiveObject (  )
35 {
36   if (myID != 0 && myDrawer.IsNull() == Standard_False)
37     myDrawer->removeObject( this, Standard_False);
38 }
39
40 //=======================================================================
41 //function : SetDrawer
42 //purpose  : 
43 //=======================================================================
44
45 const Handle_NIS_Drawer& NIS_InteractiveObject::SetDrawer
46                                         (const Handle(NIS_Drawer)& theDrawer,
47                                          const Standard_Boolean    setUpdated)
48 {
49   NIS_InteractiveContext * aCtx = theDrawer->GetContext();
50   if (myDrawer.IsNull() == Standard_False)
51     if (aCtx == 0L)
52     {
53       aCtx = myDrawer->GetContext();
54       theDrawer->myCtx = aCtx;
55     }
56     else
57     {
58       Standard_NullValue_Raise_if
59         (myDrawer->GetContext() != 0L && aCtx != myDrawer->GetContext(),
60          "NIS_InteractiveObject::SetDrawer: different drawer context");
61     }
62
63   Standard_NullValue_Raise_if
64     (aCtx == 0L, "NIS_InteractiveObject::SetDrawer: NULL drawer context");
65   // Add (if necessary) the new drawer to the Context
66   if (theDrawer->myIniId == 0)
67     theDrawer->myIniId = myID;
68   const Handle(NIS_Drawer)& aDrawer = aCtx->myDrawers.Added (theDrawer);
69   if (myDrawer != aDrawer)
70   {
71     // Move the Object from the old drawer to the new one.
72     if (myDrawer.IsNull() == Standard_False)
73       myDrawer->removeObject(this, Standard_True);
74     myDrawer = aDrawer;
75
76     myDrawer->addObject(this, aCtx->myIsShareDrawList, Standard_True);
77   }
78   if (setUpdated)
79     myDrawer->SetUpdated (NIS_Drawer::Draw_Normal,
80                           NIS_Drawer::Draw_Top,
81                           NIS_Drawer::Draw_Transparent,
82                           NIS_Drawer::Draw_Hilighted);
83   return aDrawer;
84 }
85
86 //=======================================================================
87 //function : SetTransparency
88 //purpose  : 
89 //=======================================================================
90
91 void NIS_InteractiveObject::SetTransparency (const Standard_Real theValue)
92 {
93   Standard_Integer aValue =
94     static_cast<Standard_Integer> (theValue * MaxTransparency);
95   if (aValue != static_cast<Standard_Integer>(myTransparency))
96   {
97     if (aValue <= 0)
98       myTransparency = 0;
99     else if (aValue >= 1000)
100       myTransparency = 1000u;
101     else
102       myTransparency = static_cast<unsigned int> (aValue);
103
104     if (myDrawer.IsNull() == Standard_False && myID != 0) {
105       const Handle(NIS_Drawer) aDrawer = DefaultDrawer(0L);
106       aDrawer->Assign (GetDrawer());
107       aDrawer->myTransparency = Transparency();
108       SetDrawer (aDrawer, Standard_False);
109
110       NIS_InteractiveContext * aCtx = myDrawer->GetContext();
111       Standard_NullValue_Raise_if
112         (aCtx == 0L, "NIS_InteractiveObject::SetTransparency: "
113                      "NULL drawer context");
114       if (IsTransparent()) {
115         if (myDrawType == NIS_Drawer::Draw_Normal) {
116           aCtx->myMapObjects[NIS_Drawer::Draw_Transparent].Add(myID);
117           aCtx->myMapObjects[NIS_Drawer::Draw_Normal].Remove(myID);
118           myDrawType = NIS_Drawer::Draw_Transparent;
119         }
120         myDrawer->SetUpdated (NIS_Drawer::Draw_Normal);
121       } else {
122         if (myDrawType == NIS_Drawer::Draw_Transparent) {
123           aCtx->myMapObjects[NIS_Drawer::Draw_Normal].Add(myID);
124           aCtx->myMapObjects[NIS_Drawer::Draw_Transparent].Remove(myID);
125           myDrawType = NIS_Drawer::Draw_Normal;
126         }
127         myDrawer->SetUpdated (NIS_Drawer::Draw_Transparent);
128       }
129       setDrawerUpdate();
130     }
131   }
132 }
133
134 //=======================================================================
135 //function : GetBox
136 //purpose  : 
137 //=======================================================================
138
139 const Bnd_B3f& NIS_InteractiveObject::GetBox ()
140 {
141   if (myIsUpdateBox)
142   {
143     myIsUpdateBox = Standard_False;
144     computeBox();
145   }
146   return myBox;
147 }
148
149 //=======================================================================
150 //function : Clone
151 //purpose  : 
152 //=======================================================================
153
154 void NIS_InteractiveObject::Clone (const Handle_NCollection_BaseAllocator&,
155                                    Handle_NIS_InteractiveObject& theDest) const
156 {
157   if (theDest.IsNull() == Standard_False)
158   {
159     theDest->myID = 0;
160     theDest->myDrawer = myDrawer;
161     theDest->myDrawType = myDrawType;
162     theDest->myBaseType = myBaseType;
163     theDest->myIsHidden = myIsHidden;
164     theDest->myIsDynHilighted = myIsDynHilighted;
165     theDest->myIsUpdateBox = myIsUpdateBox;
166     theDest->myTransparency = myTransparency;
167     if (myIsUpdateBox == Standard_False)
168       theDest->myBox = myBox;
169     theDest->myAttributePtr = myAttributePtr;
170   }
171 }
172
173 //=======================================================================
174 //function : CloneWithID
175 //purpose  : 
176 //=======================================================================
177
178 void NIS_InteractiveObject::CloneWithID
179                         (const Handle_NCollection_BaseAllocator& theAlloc,
180                          Handle_NIS_InteractiveObject&           theDest)
181 {
182   Clone(theAlloc, theDest);
183   theDest->myID = myID;
184   myDrawer.Nullify();
185 }
186
187 //=======================================================================
188 //function : Intersect
189 //purpose  : 
190 //=======================================================================
191
192 Standard_Boolean NIS_InteractiveObject::Intersect (const Bnd_B3f&,
193                                                    const gp_Trsf&,
194                                                    const Standard_Boolean) const
195 {
196   return Standard_True;
197 }
198
199 //=======================================================================
200 //function : Intersect
201 //purpose  : 
202 //=======================================================================
203
204 Standard_Boolean NIS_InteractiveObject::Intersect
205                      (const NCollection_List<gp_XY>& ,
206                       const gp_Trsf&                 ,
207                       const Standard_Boolean         ) const
208 {
209   return Standard_True;
210 }
211
212 //=======================================================================
213 //function : IsSelectable
214 //purpose  : Query if the Object is selectable.
215 //=======================================================================
216
217 Standard_Boolean NIS_InteractiveObject::IsSelectable () const
218 {
219   Standard_Boolean aResult(Standard_False);
220   if (myDrawer.IsNull() == Standard_False)
221     aResult = myDrawer->GetContext()->IsSelectable (myID);
222   return aResult;
223 }
224
225 //=======================================================================
226 //function : SetSelectable
227 //purpose  : Set or change the selectable state of the Object.
228 //=======================================================================
229
230 void NIS_InteractiveObject::SetSelectable (const Standard_Boolean isSel) const
231 {
232   if (myDrawer.IsNull() == Standard_False) {
233     NIS_InteractiveContext * aCtx = myDrawer->GetContext();
234     if (isSel)
235       aCtx->myMapNonSelectableObjects.Remove (myID);
236     else {
237       aCtx->myMapNonSelectableObjects.Add (myID);
238       if (myDrawType == NIS_Drawer::Draw_Hilighted)
239       {
240         aCtx->myMapObjects[NIS_Drawer::Draw_Hilighted].Remove(myID);
241         aCtx->deselectObj (this, myID);
242       }
243     }
244   }
245 }