534794a7fd8fcba79c85e78349fdb9d2ea97554c
[occt.git] / src / AIS / AIS_Dimension.hxx
1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2013 OPEN CASCADE SAS
3 //
4 // The content of this file is subject to the Open CASCADE Technology Public
5 // License Version 6.5 (the "License"). You may not use the content of this file
6 // except in compliance with the License. Please obtain a copy of the License
7 // at http://www.opencascade.org and read it completely before using this file.
8 //
9 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
10 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
11 //
12 // The Original Code and all software distributed under the License is
13 // distributed on an "AS IS" basis, without warranty of any kind, and the
14 // Initial Developer hereby disclaims all such warranties, including without
15 // limitation, any warranties of merchantability, fitness for a particular
16 // purpose or non-infringement. Please see the License for the specific terms
17 // and conditions governing the rights and limitations under the License.
18
19 #ifndef _AIS_Dimension_Headerfile
20 #define _AIS_Dimension_Headerfile
21
22 #include <AIS_DimensionDisplayMode.hxx>
23 #include <AIS_DimensionOwner.hxx>
24 #include <AIS_DisplaySpecialSymbol.hxx>
25 #include <AIS_InteractiveObject.hxx>
26 #include <AIS_KindOfInteractive.hxx>
27 #include <AIS_KindOfDimension.hxx>
28 #include <AIS_KindOfSurface.hxx>
29 #include <Bnd_Box.hxx>
30 #include <Geom_Curve.hxx>
31 #include <gp_Pln.hxx>
32 #include <Prs3d_ArrowAspect.hxx>
33 #include <Prs3d_DimensionAspect.hxx>
34 #include <Prs3d_LineAspect.hxx>
35 #include <Prs3d_Presentation.hxx>
36 #include <Prs3d_TextAspect.hxx>
37 #include <Select3D_ListOfSensitive.hxx>
38 #include <SelectMgr_EntityOwner.hxx>
39 #include <Standard.hxx>
40 #include <TCollection_ExtendedString.hxx>
41 #include <TColgp_HArray1OfPnt.hxx>
42 #include <TopoDS_Face.hxx>
43 #include <TopoDS_Shape.hxx>
44
45 DEFINE_STANDARD_HANDLE(AIS_Dimension, AIS_InteractiveObject)
46
47 class AIS_Dimension : public AIS_InteractiveObject
48 {
49 protected:
50
51   // Specifies supported at base level horizontal and vertical
52   // label positions for drawing extension lines and centered text.
53   enum LabelPosition
54   {
55     LabelPosition_None    = 0x00,
56
57     LabelPosition_Left    = 0x01,
58     LabelPosition_Right   = 0x02,
59     LabelPosition_HCenter = 0x04,
60     LabelPosition_HMask  = LabelPosition_Left | LabelPosition_Right | LabelPosition_HCenter,
61
62     LabelPosition_Above   = 0x10,
63     LabelPosition_Below   = 0x20,
64     LabelPosition_VCenter = 0x40,
65     LabelPosition_VMask  = LabelPosition_Above | LabelPosition_Below | LabelPosition_VCenter
66   };
67
68 public:
69
70   //! Constructor with default parameters values
71   Standard_EXPORT  AIS_Dimension();
72
73   //! Gets dimension value
74   Standard_EXPORT  Standard_Real GetValue() const;
75
76   //! Sets dimension value
77   //! Attention! This method is used ONLY to set custom value.
78   //! To set value internally, use <myValue>.
79   Standard_EXPORT  void SetCustomValue (const Standard_Real theValue);
80
81   //! Gets working plane.
82   Standard_EXPORT  const gp_Pln& GetWorkingPlane() const;
83
84   //! Sets working plane.
85   Standard_EXPORT  void SetWorkingPlane (const gp_Pln& thePlane);
86
87   Standard_EXPORT  void SetFirstPoint (const gp_Pnt& thePoint);
88
89   Standard_EXPORT  void SetSecondPoint (const gp_Pnt& thePoint);
90
91   Standard_EXPORT  void SetFirstShape (const TopoDS_Shape& theFirstShape);
92
93   Standard_EXPORT  void SetSecondShape (const TopoDS_Shape& theSecondShape);
94
95   //! Gets the dimension aspect from AIS object drawer.
96   //! Dimension aspect contains aspects of line, text and arrows for dimension presentation.
97   Standard_EXPORT   Handle(Prs3d_DimensionAspect) DimensionAspect() const;
98
99   //! Sets new length aspect in the interactive object drawer.
100   Standard_EXPORT   void SetDimensionAspect (const Handle(Prs3d_DimensionAspect)& theDimensionAspect);
101
102   //! Returns the kind of dimension
103   Standard_EXPORT  AIS_KindOfDimension KindOfDimension() const;
104
105   //! Returns the kind of interactive
106   Standard_EXPORT  virtual  AIS_KindOfInteractive Type() const;
107
108   //! Sets the kind of dimension
109   Standard_EXPORT  virtual void SetKindOfDimension (const AIS_KindOfDimension theKindOfDimension);
110
111   //! Returns true if the class of objects accepts the display mode theMode.
112   //! The interactive context can have a default mode of
113   //! representation for the set of Interactive Objects. This
114   //! mode may not be accepted by object
115   Standard_EXPORT   virtual  Standard_Boolean AcceptDisplayMode (const Standard_Integer theMode) const;
116
117   // Selection computing if it is needed here
118   Standard_EXPORT   virtual  void ComputeSelection (const Handle(SelectMgr_Selection)& theSelection,
119                                                     const Standard_Integer theMode);
120
121   //! Reset working plane to default.
122   Standard_EXPORT  void ResetWorkingPlane();
123
124   //! specifies dimension special symbol display options
125   Standard_EXPORT  void SetDisplaySpecialSymbol (const AIS_DisplaySpecialSymbol theDisplaySpecSymbol);
126
127   //! shows dimension special symbol display options
128   Standard_EXPORT  AIS_DisplaySpecialSymbol DisplaySpecialSymbol() const;
129
130   //! specifies special symbol
131   Standard_EXPORT  void SetSpecialSymbol (const Standard_ExtCharacter theSpecialSymbol);
132
133   //! returns special symbol
134   Standard_EXPORT  Standard_ExtCharacter SpecialSymbol() const;
135
136   //! shows if Units are to be displayed along with dimension value
137   Standard_EXPORT  Standard_Boolean IsUnitsDisplayed() const;
138
139   //! sets to display units along with the dimansion value or no
140   Standard_EXPORT  void MakeUnitsDisplayed (const Standard_Boolean toDisplayUnits);
141
142   //! returns the current type of units
143   Standard_EXPORT  TCollection_AsciiString UnitsQuantity() const;
144
145   //! sets the current type of units
146   Standard_EXPORT  void SetUnitsQuantity (const TCollection_AsciiString& theUnitsQuantity);
147
148   //! returns the current model units
149   Standard_EXPORT  TCollection_AsciiString ModelUnits() const;
150
151   //! sets the current model units
152   Standard_EXPORT  void SetModelUnits (const TCollection_AsciiString& theUnits);
153
154   //! returns the current display units
155   Standard_EXPORT  TCollection_AsciiString DisplayUnits() const;
156
157   //! sets the current display units
158   Standard_EXPORT  void SetDisplayUnits (const TCollection_AsciiString& theUnits);
159
160   //! Important! Only for 3d text </br>
161   //! 3d text is oriented relative to the attachment points order </br>
162   //! By default, text direction vector is oriented from the first attachment point </br>
163   //! to the second one. This method checks if text direction is to be default or </br>
164   //! should be reversed.
165   Standard_EXPORT  Standard_Boolean IsTextReversed() const;
166
167   //! Important! Only for 3d text
168   //! 3d text is oriented relative to the attachment points order </br>
169   //! By default, text direction vector is oriented from the first attachment point </br>
170   //! to the second one. This method sets value that shows if text direction </br>
171   //! should be reversed or not.
172   Standard_EXPORT  void MakeTextReversed (const Standard_Boolean isTextReversed);
173
174   //! Sets selection tolerance for text2d:
175   //! For 2d text selection detection sensitive point with tolerance is used
176   //! to change this tolerance use this method
177   //! Important! Only for 2d text
178   Standard_EXPORT  void SetSelToleranceForText2d (const Standard_Real theTol);
179
180   //! Returns selection tolerance for text2d:
181   //! For 2d text selection detection sensitive point with tolerance is used
182   //! Important! Only for 2d text
183   Standard_EXPORT  Standard_Real SelToleranceForText2d() const;
184
185   //! Sets flyout size for dimension.
186   Standard_EXPORT void SetFlyout (const Standard_Real theFlyout);
187
188   //! @return flyout size for dimension.
189   Standard_Real GetFlyout() const
190   {
191     return myFlyout;
192   }
193
194 public:
195
196   DEFINE_STANDARD_RTTI(AIS_Dimension)
197
198 protected:
199
200   Standard_EXPORT void getTextWidthAndString (Quantity_Length& theWidth,
201                                               TCollection_ExtendedString& theString) const;
202
203   Standard_EXPORT Standard_Real valueToDisplayUnits();
204
205   //! Reset working plane to default.
206   Standard_EXPORT void resetWorkingPlane (const gp_Pln& theNewDefaultPlane);
207
208   //! Count default plane 
209   Standard_EXPORT virtual void countDefaultPlane();
210
211   //! Computes dimension value in display units
212   Standard_EXPORT virtual void computeValue();
213
214   //! Performs drawing of 2d or 3d arrows on the working plane
215   Standard_EXPORT void drawArrow (const Handle(Prs3d_Presentation)& thePresentation,
216                                   const gp_Pnt& theLocation,
217                                   const gp_Dir& theDirection);
218
219   //! Performs drawing of 2d or 3d text on the working plane
220   //! @return text width relative to the dimension working plane. For 2d text this value will be zero.
221   Standard_EXPORT Standard_Real drawText (const Handle(Prs3d_Presentation)& thePresentation,
222                                           const gp_Dir& theTextDir,
223                                           const TCollection_ExtendedString theText,
224                                           const AIS_DimensionDisplayMode theMode,
225                                           const Standard_Integer theLabelPosition);
226
227   //! Performs computing of dimension linear extension with text
228   //! @param thePresentation [in] the presentation to fill with graphical primitives.
229   //! @param theExtensionSize [in] the size of extension line.
230   //! @param theExtensionStart [in] the point where extension line connects to dimension.
231   //! @param theExtensionDir [in] the direction of extension line.
232   //! @param theValueString [in] the string with value.
233   //! @param theMode [in] the display mode.
234   //! @param theLabelPosition [in] position flags for the text label.
235   Standard_EXPORT void drawExtension (const Handle(Prs3d_Presentation)& thePresentation,
236                                       const Standard_Real theExtensionSize,
237                                       const gp_Pnt& theExtensionStart,
238                                       const gp_Dir& theExtensionDir,
239                                       const TCollection_ExtendedString& theValueString,
240                                       const AIS_DimensionDisplayMode theMode,
241                                       const Standard_Integer theLabelPosition);
242
243   //! Performs computing of linear dimension (for length, diameter, radius and so on)
244   Standard_EXPORT void drawLinearDimension (const Handle(Prs3d_Presentation)& thePresentation,
245                                             const AIS_DimensionDisplayMode theMode,
246                                             const Standard_Boolean isOneSideDimension = Standard_False);
247
248   //! If it's possible computes circle from planar face
249   Standard_EXPORT  Standard_Boolean circleFromPlanarFace (const TopoDS_Face& theFace,
250                                                           Handle(Geom_Curve)& theCurve,
251                                                           gp_Pnt & theFirstPoint,
252                                                           gp_Pnt & theLastPoint);
253
254   //! Performs initialization of circle and points from given shape
255   //! (for radius, diameter and so on)
256   Standard_EXPORT  Standard_Boolean initCircularDimension (const TopoDS_Shape& theShape,
257                                                            gp_Circ& theCircle,
258                                                            gp_Pnt& theMiddleArcPoint,
259                                                            gp_Pnt& theOppositeDiameterPoint);
260   Standard_EXPORT Standard_Boolean isComputed() const;
261
262   Standard_EXPORT void setComputed (Standard_Boolean isComputed);
263
264   Standard_EXPORT gp_Pnt textPosition() const;
265
266   Standard_EXPORT void setTextPosition (const gp_Pnt thePosition);
267
268   Standard_EXPORT void resetGeom();
269
270   //! Fills sensitive entity for flyouts and adds it to the selection.
271   Standard_EXPORT virtual void computeFlyoutSelection (const Handle(SelectMgr_Selection)& theSelection,
272                                                        const Handle(AIS_DimensionOwner)& theOwner);
273
274 protected: //! @name Working plane properties
275
276   //! Dimension default plane
277   gp_Pln myDefaultPlane;
278
279   //! Shows if working plane is set custom
280   Standard_Boolean myIsWorkingPlaneCustom;
281
282 protected: //! @name Value properties
283
284   //! Dimension value which is displayed with dimension lines
285   Standard_Real myValue;
286
287   //! Shows if the value is set by user and is no need to count it automatically
288   Standard_Boolean myIsValueCustom;
289
290 protected: // !@name Units properties
291
292   //! The quantity of units for the value computation
293   TCollection_AsciiString myUnitsQuantity;
294
295   //! Units of the model
296   TCollection_AsciiString myModelUnits;
297
298   //! Units in which the displayed value will be converted
299   TCollection_AsciiString myDisplayUnits;
300
301   //! Determines if units is to be displayed along with the value
302   Standard_Boolean myToDisplayUnits;
303
304   //! Special symbol for some kind of dimensions (for diameter, radius and so on)
305   Standard_ExtCharacter mySpecialSymbol;
306
307   //! Special symbol display options
308   AIS_DisplaySpecialSymbol myDisplaySpecialSymbol;
309
310 protected: //! @name Geometry properties
311
312   //! Geometry of dimensions, needs for advanced selection
313   //! Geometry is computed in Compute() method and is used 
314   //! in ComputeSelection() method.
315   //! If it is computed successfully, myIsComputed = Standard_True.
316   //! to check computing result use IsComputed() method
317   struct DimensionGeom
318   {
319     //! Text position
320     gp_Pnt myTextPosition;
321
322     //! Text bounding box, stored for advanced selection
323     Bnd_Box myTextBndBox;
324
325     //! Sensitive point tolerance for 2d text selection
326     Standard_Real mySelToleranceForText2d;
327
328     //! For advanced dimension line selection
329     Select3D_ListOfSensitive mySensitiveSegments;
330
331     //! Shows if attachment points were computed
332     Standard_Boolean myIsComputed;
333
334   public: 
335
336     DimensionGeom () : myIsComputed (Standard_False) {}
337   };
338
339   //! Shows if text is inverted
340   Standard_Boolean myIsTextReversed;
341
342   //! Points that are base for dimension.
343   //! My first point of dimension attach (belongs to shape for which dimension is computed)
344   gp_Pnt myFirstPoint;
345
346   //! My second point of dimension attach (belongs to shape for which dimension is computed)
347   gp_Pnt mySecondPoint;
348
349   //! Shows if attach points are initialized correctly
350   Standard_Boolean myIsInitialized;
351
352   //! First shape (can be vertex, edge or face)
353   TopoDS_Shape myFirstShape;
354
355   //! Second shape (can be vertex, edge or face)
356   TopoDS_Shape mySecondShape;
357
358   //! Number of shapes
359   Standard_Integer myShapesNumber;
360
361   //! Defines flyout lines and direction
362   //! Flyout direction in the working plane.
363   //! Can be negative, or positive and is defined by the sign of myFlyout value.
364   //! The direction vector is counting using the working plane.
365   //! myFlyout value defined the size of flyout.
366   Standard_Real myFlyout;
367
368   //! Geometry of dimensions, needs for advanced selection
369   //! Geometry is computed in Compute() method and is used 
370   //! in ComputeSelection() method.
371   //! If it is computed successfully, myIsComputed = Standard_True.
372   //! to check computing result use IsComputed() method
373   DimensionGeom myGeom;
374
375 private:
376
377   //! Type of dimension
378   AIS_KindOfDimension myKindOfDimension;
379
380   //! Dimension working plane, is equal to <myDefaultPlane> if it can be computed automatically.
381   gp_Pln myWorkingPlane;
382 };
383 #endif