17f7b07530686c89946016d8c19de6ab8472ed2f
[occt.git] / src / AIS / AIS_InteractiveObject.hxx
1 // Created on: 1996-12-11
2 // Created by: Robert COUBLANC
3 // Copyright (c) 1996-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
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.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #ifndef _AIS_InteractiveObject_HeaderFile
18 #define _AIS_InteractiveObject_HeaderFile
19
20 #include <Aspect_TypeOfFacingModel.hxx>
21 #include <AIS_KindOfInteractive.hxx>
22 #include <Graphic3d_NameOfMaterial.hxx>
23 #include <PrsMgr_TypeOfPresentation3d.hxx>
24 #include <SelectMgr_SelectableObject.hxx>
25 #include <TColStd_ListOfInteger.hxx>
26 #include <TColStd_ListOfTransient.hxx>
27 #include <Quantity_Color.hxx>
28
29 class AIS_InteractiveContext;
30 class Graphic3d_MaterialAspect;
31 class Prs3d_Presentation;
32 class Prs3d_BasicAspect;
33 class Bnd_Box;
34
35 //! Defines a class of objects with display and selection services.
36 //! Entities which are visualized and selected are
37 //! Interactive Objects. You can make use of classes of
38 //! standard Interactive Objects for which all necessary
39 //! methods have already been programmed, or you can
40 //! implement your own classes of Interactive Objects.
41 //! Specific attributes of entities such as arrow aspect for
42 //! dimensions must be loaded in a Drawer. This Drawer
43 //! is then applied to the Interactive Object in view.
44 //! There are four types of Interactive Object in AIS: the
45 //! construction element or Datum, the Relation, which
46 //! includes both dimensions and constraints, the Object,
47 //! and finally, when the object is of an unknown type, the None type.
48 //! Inside these categories, a signature, or index,
49 //! provides the possibility of additional characterization.
50 //! By default, the Interactive Object has a None type
51 //! and a signature of 0. If you want to give a particular
52 //! type and signature to your interactive object, you must
53 //! redefine the methods, Signature and Type.
54 //! Warning
55 //! In the case of attribute methods, methods for
56 //! standard attributes are virtual. They must be
57 //! redefined   by the inheriting classes. Setcolor for a
58 //! point and Setcolor for a plane, for example, do not
59 //! affect the same attributes in the Drawer.
60 class AIS_InteractiveObject : public SelectMgr_SelectableObject
61 {
62   friend class AIS_InteractiveContext;
63   DEFINE_STANDARD_RTTIEXT(AIS_InteractiveObject, SelectMgr_SelectableObject)
64 public:
65
66   //! Returns the kind of Interactive Object:
67   //! -   None
68   //! -   Datum
69   //! -   Relation
70   //! -   Object
71   //! By default, the   interactive object has a None type.
72   //! Because specific shapes entail different behavior
73   //! according to their sub-shapes, you may need to
74   //! create a Local Context. This will allow you to
75   //! specify the additional characteristics which you
76   //! need to handle these shapes.
77   Standard_EXPORT virtual AIS_KindOfInteractive Type() const;
78   
79   //! Specifies additional characteristics of Interactive
80   //! Objects. A signature is, in fact, an index with integer
81   //! values assigned different properties.
82   //! This method is frequently used in conjuction with
83   //! Type to give a particular type and signature to an
84   //! Interactive Object. By default, the Interactive Object
85   //! has a None type and a signature of 0. Among the
86   //! datums, this signature is attributed to the shape
87   //! The remaining datums have the following default signatures:
88   //! -   Point                   signature 1
89   //! -   Axis                     signature 2
90   //! -   Trihedron                signature 3
91   //! -   PlaneTrihedron            signature 4
92   //! -   Line                     signature 5
93   //! -   Circle                  signature 6
94   //! -   Plane                   signature 7.
95   Standard_EXPORT virtual Standard_Integer Signature() const;
96   
97   //! Informs the graphic context that the interactive Object
98   //! may be decomposed into sub-shapes for dynamic selection.
99   //! The most used Interactive Object is AIS_Shape.
100   //! Activation methods for standard selection modes are
101   //! proposed in the Interactive Context. These include
102   //! selection by vertex or by edges. For datums with the
103   //! same behavior as AIS_Shape, such as vetices and
104   //! edges, we must redefine the virtual method so that
105   //! AcceptShapeDecomposition returns false.
106   //! Rule for selection :
107   //! Mode 0 :  Selection of  the interactive Object itself
108   //! Mode 1 :  Selection of vertices
109   //! Mode 2 :  Selection Of Edges
110   //! Mode 3 :  Selection Of Wires
111   //! Mode 4 :  Selection Of Faces ...
112   virtual Standard_Boolean AcceptShapeDecomposition() const { return Standard_False; }
113
114   //! change the current facing model apply on polygons for
115   //! SetColor(), SetTransparency(), SetMaterial() methods
116   //! default facing model is Aspect_TOFM_TWO_SIDE. This mean that attributes is
117   //! applying both on the front and back face.
118   Standard_EXPORT void SetCurrentFacingModel (const Aspect_TypeOfFacingModel aModel = Aspect_TOFM_BOTH_SIDE);
119   
120   //! Returns the current facing model which is in effect.
121   Standard_EXPORT Aspect_TypeOfFacingModel CurrentFacingModel() const;
122
123   //! Only the interactive object knowns which Drawer attribute is affected by the color, if any
124   //! (ex: for a wire,it's the wireaspect field of the drawer, but for a vertex, only the point aspect field is affected by the color).
125   //! WARNING : Do not forget to set the corresponding fields here (hasOwnColor and myDrawer->SetColor())
126   Standard_EXPORT virtual void SetColor (const Quantity_Color& theColor);
127
128   //! Removes color settings. Only the Interactive Object
129   //! knows which Drawer attribute is   affected by the color
130   //! setting. For a wire, for example, wire aspect is the
131   //! attribute affected. For a vertex, however, only point
132   //! aspect is affected by the color setting.
133   Standard_EXPORT virtual void UnsetColor();
134   
135   //! Allows you to provide the setting aValue for width.
136   //! Only the Interactive Object knows which Drawer
137   //! attribute is affected by the width setting.
138   Standard_EXPORT virtual void SetWidth (const Standard_Real aValue);
139   
140   Standard_EXPORT virtual void UnsetWidth();
141   
142   //! Returns true if the class of objects accepts the display mode aMode.
143   //! The interactive context can have a default mode of
144   //! representation for the set of Interactive Objects. This
145   //! mode may not be accepted by a given class of
146   //! objects. Consequently, this virtual method allowing us
147   //! to get information about the class in question must be implemented.
148   Standard_EXPORT virtual Standard_Boolean AcceptDisplayMode (const Standard_Integer aMode) const;
149   
150   //! Returns the default display mode. This method is to
151   //! be implemented when the main mode is not mode 0.
152   Standard_EXPORT virtual Standard_Integer DefaultDisplayMode() const;
153   
154   //! Updates the active presentation; if <AllModes> = Standard_True
155   //! all the presentations inside are recomputed.
156   //! IMPORTANT: It is preferable to call Redisplay method of
157   //! corresponding AIS_InteractiveContext instance for cases when it
158   //! is accessible. This method just redirects call to myCTXPtr,
159   //! so this class field must be up to date for proper result.
160   Standard_EXPORT void Redisplay (const Standard_Boolean AllModes = Standard_False);
161   
162   //! Sets the infinite state flag aFlage.
163   //! if   <aFlag>   = True  ,  the   interactiveObject  is
164   //! considered as infinite, i.e. its graphic presentations
165   //! are not taken in account for View FitAll...
166   Standard_EXPORT void SetInfiniteState (const Standard_Boolean aFlag = Standard_True);
167
168   //! Returns true if the interactive object is infinite. In this
169   //! case, its graphic presentations are not taken into
170   //! account in the fit-all view.
171   Standard_Boolean IsInfinite() const { return myInfiniteState; }
172
173   //! Indicates whether the Interactive Object has a pointer to an interactive context.
174   Standard_Boolean HasInteractiveContext() const { return myCTXPtr != NULL; }
175
176   //! Returns the context pointer to the interactive context.
177   Standard_EXPORT Handle(AIS_InteractiveContext) GetContext() const;
178   
179   //! Sets the interactive context aCtx and provides a link
180   //! to the default drawing tool or "Drawer" if there is none.
181   Standard_EXPORT virtual void SetContext (const Handle(AIS_InteractiveContext)& aCtx);
182   
183   //! Returns true if the object has an owner attributed to it.
184   //! The owner can be a shape for a set of sub-shapes or
185   //! a sub-shape for sub-shapes which it is composed of,
186   //! and takes the form of a transient.
187   Standard_EXPORT Standard_Boolean HasOwner() const;
188   
189   //! Returns the owner of the Interactive Object.
190   //! The owner can be a shape for a set of sub-shapes or
191   //! a sub-shape for sub-shapes which it is composed of,
192   //! and takes the form of a transient.
193   //! There are two types of owners:
194   //! -   Direct owners, decomposition shapes such as
195   //! edges, wires, and faces.
196   //! -   Users, presentable objects connecting to sensitive
197   //! primitives, or a shape which has been decomposed.
198   const Handle(Standard_Transient)& GetOwner() const { return myOwner; }
199
200   //! Allows you to attribute the owner theApplicativeEntity to
201   //! an Interactive Object. This can be a shape for a set of
202   //! sub-shapes or a sub-shape for sub-shapes which it
203   //! is composed of. The owner takes the form of a transient.
204   void SetOwner (const Handle(Standard_Transient)& theApplicativeEntity) { myOwner = theApplicativeEntity; }
205
206   //! Each Interactive Object has methods which allow us
207   //! to attribute an Owner to it in the form of a Transient.
208   //! This method removes the owner from the graphic entity.
209   Standard_EXPORT void ClearOwner();
210
211   //! Returns true if the Interactive Object has a display
212   //! mode setting. Otherwise, it is displayed in Neutral Point.
213   Standard_Boolean HasDisplayMode() const { return myDrawer->DisplayMode() != -1; }
214
215   //! Sets the display mode aMode for the interactive object.
216   //! An object can have its own temporary display mode,
217   //! which is different from that proposed by the interactive context.
218   //! The range of possibilities currently proposed is the following:
219   //! -   AIS_WireFrame
220   //! -   AIS_Shaded
221   //! This range can, however, be extended through the creation of new display modes.
222   Standard_EXPORT void SetDisplayMode (const Standard_Integer aMode);
223
224   //! Removes display mode settings from the interactive object.
225   void UnsetDisplayMode() { myDrawer->SetDisplayMode (-1); }
226
227   //! Returns the display mode setting of the Interactive Object.
228   //! The range of possibilities is the following:
229   //! -   AIS_WireFrame
230   //! -   AIS_Shaded
231   //! This range can, however, be extended through the
232   //! creation of new display modes.
233   Standard_Integer DisplayMode() const { return myDrawer->DisplayMode(); }
234
235   //! Returns true if the Interactive Object is in highlight mode.
236   Standard_Boolean HasHilightMode() const { return !myHilightDrawer.IsNull() && myHilightDrawer->DisplayMode() != -1; }
237
238   //! Returns highlight display mode.
239   //! This is obsolete method for backward compatibility - use ::HilightAttributes() and ::DynamicHilightAttributes() instead.
240   Standard_Integer HilightMode() const { return !myHilightDrawer.IsNull() ? myHilightDrawer->DisplayMode() : -1; }
241
242   //! Sets highlight display mode.
243   //! This is obsolete method for backward compatibility - use ::HilightAttributes() and ::DynamicHilightAttributes() instead.
244   void SetHilightMode (const Standard_Integer theMode)
245   {
246     if (myHilightDrawer.IsNull())
247     {
248       myHilightDrawer = new Prs3d_Drawer();
249       myHilightDrawer->Link (myDrawer);
250       myHilightDrawer->SetAutoTriangulation (Standard_False);
251       myHilightDrawer->SetColor (Quantity_NOC_GRAY80);
252       myHilightDrawer->SetZLayer(Graphic3d_ZLayerId_UNKNOWN);
253     }
254     if (myDynHilightDrawer.IsNull())
255     {
256       myDynHilightDrawer = new Prs3d_Drawer();
257       myDynHilightDrawer->Link (myDrawer);
258       myDynHilightDrawer->SetColor (Quantity_NOC_CYAN1);
259       myDynHilightDrawer->SetAutoTriangulation (Standard_False);
260       myDynHilightDrawer->SetZLayer(Graphic3d_ZLayerId_Top);
261     }
262     myHilightDrawer   ->SetDisplayMode (theMode);
263     myDynHilightDrawer->SetDisplayMode (theMode);
264   }
265
266   //! Unsets highlight display mode.
267   void UnsetHilightMode()
268   {
269     if (!myHilightDrawer.IsNull())
270     {
271       myHilightDrawer->SetDisplayMode (-1);
272     }
273     if (!myDynHilightDrawer.IsNull())
274     {
275       myDynHilightDrawer->SetDisplayMode (-1);
276     }
277   }
278
279   //! Returns true if the Interactive Object has color.
280   Standard_Boolean HasColor() const { return hasOwnColor; }
281
282   //! Returns the color setting of the Interactive Object.
283   virtual void Color (Quantity_Color& theColor) const { theColor = myDrawer->Color(); }
284
285   //! Returns true if the Interactive Object has width.
286   Standard_Boolean HasWidth() const { return myOwnWidth != 0.0; }
287
288   //! Returns the width setting of the Interactive Object.
289   Standard_Real Width() const { return myOwnWidth; }
290
291   //! Returns true if the Interactive Object has a setting for material.
292   Standard_Boolean HasMaterial() const { return hasOwnMaterial; }
293
294   //! Returns the current material setting.
295   //! This will be on of the following materials:
296   //! -   Brass
297   //! -   Bronze
298   //! -   Gold
299   //! -   Pewter
300   //! -   Silver
301   //! -   Stone.
302   Standard_EXPORT virtual Graphic3d_NameOfMaterial Material() const;
303
304   //! Sets the material aMat defining this display attribute
305   //! for the interactive object.
306   //! Material aspect determines shading aspect, color and
307   //! transparency of visible entities.
308   Standard_EXPORT virtual void SetMaterial (const Graphic3d_MaterialAspect& aName);
309   
310   //! Removes the setting for material.
311   Standard_EXPORT virtual void UnsetMaterial();
312   
313   //! Attributes a setting aValue for transparency.
314   //! The transparency value should be between 0.0 and 1.0.
315   //! At 0.0 an object will be totally opaque, and at 1.0, fully transparent.
316   //! Warning At a value of 1.0, there may be nothing visible.
317   Standard_EXPORT virtual void SetTransparency (const Standard_Real aValue = 0.6);
318   
319   //! Returns true if there is a transparency setting.
320   Standard_Boolean IsTransparent() const { return myDrawer->Transparency() > 0.005f; }
321
322   //! Returns the transparency setting.
323   //! This will be between 0.0 and 1.0.
324   //! At 0.0 an object will be totally opaque, and at 1.0, fully transparent.
325   Standard_EXPORT virtual Standard_Real Transparency() const;
326   
327   //! Removes the transparency setting. The object is opaque by default.
328   Standard_EXPORT virtual void UnsetTransparency();
329   
330   //! Clears settings provided by the drawing tool aDrawer.
331   Standard_EXPORT virtual void UnsetAttributes() Standard_OVERRIDE;
332
333   //! Returns TRUE when this object has a presentation
334   //! in the current DisplayMode()
335   Standard_EXPORT Standard_Boolean HasPresentation() const;
336   
337   //! Returns the current presentation of this object
338   //! according to the current DisplayMode()
339   Standard_EXPORT Handle(Prs3d_Presentation) Presentation() const;
340
341   //! Sets the graphic basic aspect to the current presentation.
342   Standard_EXPORT void SetAspect (const Handle(Prs3d_BasicAspect)& anAspect);
343   
344   //! Sets up polygon offsets for this object.
345   //! It modifies all existing presentations of <anObj> (if any),
346   //! so it is reasonable to call this method after <anObj> has been displayed.
347   //! Otherwise, Compute() method should pass Graphic3d_AspectFillArea3d
348   //! aspect from <myDrawer> to Graphic3d_Group to make polygon offsets work.
349   //!
350   //! <aMode> parameter can contain various combinations of
351   //! Aspect_PolygonOffsetMode enumeration elements (Aspect_POM_None means
352   //! that polygon offsets are not changed).
353   //! If <aMode> is different from Aspect_POM_Off and Aspect_POM_None, then <aFactor> and <aUnits>
354   //! arguments are used by graphic renderer to calculate a depth offset value:
355   //!
356   //! offset = <aFactor> * m + <aUnits> * r, where
357   //! m - maximum depth slope for the polygon currently being displayed,
358   //! r - minimum window coordinates depth resolution (implementation-specific).
359   //!
360   //! Deafult settings for OCC 3D viewer: mode = Aspect_POM_Fill, factor = 1., units = 0.
361   //!
362   //! Negative offset values move polygons closer to the viewport,
363   //! while positive values shift polygons away.
364   //! Consult OpenGL reference for details (glPolygonOffset function description).
365   //!
366   //! NOTE: This method has a side effect - it creates own shading aspect
367   //! if not yet created, so it is better to set up object material,
368   //! color, etc. first.
369   Standard_EXPORT virtual void SetPolygonOffsets (const Standard_Integer aMode, const Standard_ShortReal aFactor = 1.0, const Standard_ShortReal aUnits = 0.0);
370   
371   //! Returns Standard_True if <myDrawer> has non-null shading aspect
372   Standard_EXPORT virtual Standard_Boolean HasPolygonOffsets() const;
373   
374   //! Retrieves current polygon offsets settings from <myDrawer>.
375   Standard_EXPORT virtual void PolygonOffsets (Standard_Integer& aMode, Standard_ShortReal& aFactor, Standard_ShortReal& aUnits) const;
376   
377   //! Returns bounding box of object correspondingly to its current display mode.
378   Standard_EXPORT virtual void BoundingBox (Bnd_Box& theBndBox) Standard_OVERRIDE;
379
380   //! Enables or disables on-triangulation build of isolines according to the flag given.
381   Standard_EXPORT void SetIsoOnTriangulation (const Standard_Boolean theIsEnabled);
382
383   //! Synchronize presentation aspects after their modification.
384   //!
385   //! This method should be called after modifying primitive aspect properties (material, texture, shader)
386   //! so that modifications will take effect on already computed presentation groups (thus avoiding re-displaying the object).
387   Standard_EXPORT void SynchronizeAspects();
388
389 private:
390
391   Standard_EXPORT virtual Standard_Boolean RecomputeEveryPrs() const;
392
393   Standard_EXPORT void MustRecomputePrs (const Standard_Integer aMode) const;
394
395   Standard_EXPORT const TColStd_ListOfInteger& ListOfRecomputeModes() const;
396
397   Standard_EXPORT void SetRecomputeOk();
398
399 protected:
400
401   //! The TypeOfPresention3d means that the interactive object
402   //! may have a presentation dependant of the view of Display.
403   Standard_EXPORT AIS_InteractiveObject(const PrsMgr_TypeOfPresentation3d aTypeOfPresentation3d = PrsMgr_TOP_AllView);
404
405 private:
406
407   AIS_InteractiveContext* myCTXPtr;
408   Handle(Standard_Transient) myOwner;
409
410 protected:
411
412   TColStd_ListOfInteger myToRecomputeModes;
413   Standard_Real myOwnWidth;
414   Aspect_TypeOfFacingModel myCurrentFacingModel;
415   Standard_Boolean myInfiniteState;
416   Standard_Boolean hasOwnColor;
417   Standard_Boolean hasOwnMaterial;
418   Standard_Boolean myRecomputeEveryPrs;
419
420 };
421
422 DEFINE_STANDARD_HANDLE(AIS_InteractiveObject, SelectMgr_SelectableObject)
423
424 #endif // _AIS_InteractiveObject_HeaderFile