Salome HOME
21854: Add persistent dimensions
[modules/geom.git] / src / GEOMGUI / GEOMGUI_DimensionProperty.h
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // File   : GEOMGUI_DimensionProperty.h
24 // Author : Anton POLETAEV, Open CASCADE S.A.S.
25 //
26
27 #ifndef GEOMGUI_DIMENSIONPROPERTY_H
28 #define GEOMGUI_DIMENSIONPROPERTY_H
29
30 // OCCT includes
31 #include <AIS_DiameterDimension.hxx>
32 #include <AIS_LengthDimension.hxx>
33 #include <AIS_AngleDimension.hxx>
34 #include <gp_Ax3.hxx>
35 #include <QVariant>
36 #include <QVector>
37 #include <QSharedPointer>
38
39 #include <string>
40 #include <vector>
41 #include <list>
42
43 class SalomeApp_Study;
44
45 /*!
46  * \brief Utility class to unpack/pack dimension presentations as object property of study.
47  *
48  * Dimension presentations are store in relative coordinate system (LCS).
49  * To ensure that dimension is bound to the equal shape irrespectively of its location
50  * transformation.
51  *
52  * The record is a list of qvariant, containing packed dimension properties and its attributes:
53  * (name);(is_visible);(dimension type);(dimension property list);
54  * 
55  * The following packing scheme is used to store dimension data:
56  * Length: (plane)[0-3] (flyout)[4] (text flags)[5-6] (arrow flag)[7] (p1)[8-10] (pnt2)[11-13]
57  * Diam:   (plane)[0-3] (flyout)[4] (text flags)[5-6] (arrow flag)[7] (circle loc, xdir, ydir, rad)[8-17]
58  * Angle:               (flyout)[0] (text flags)[1-2] (arrow flag)[3] (p1)[4-6] (p2)[7-9] (center)[10-12]
59  */
60 class Standard_EXPORT GEOMGUI_DimensionProperty
61 {
62 public:
63
64   /*!
65    * \brief Type of packed presentation.
66    */
67   enum DimensionType
68   {
69     DimensionType_Length,
70     DimensionType_Diameter,
71     DimensionType_Angle
72   };
73
74   /*!
75    * \brief Base class for pointer-optimized records storing.
76    */
77   struct Length;
78   struct Diameter;
79   struct Angle;
80   struct Record
81   {
82   public:
83     Record( const DimensionType theType ) 
84       : myType( theType )
85       {}
86
87     DimensionType Type() const
88     {
89       return myType;
90     }
91
92     Length*   AsLength()   { return static_cast<Length*>( this ); }
93     Diameter* AsDiameter() { return static_cast<Diameter*>( this ); }
94     Angle*    AsAngle()    { return static_cast<Angle*>( this ); }
95
96   private:
97     DimensionType myType;
98   };
99
100   /*!
101    * \brief Declaration of properties for length dimensions.
102    */
103   struct Standard_EXPORT Length : public Record
104   {
105     Length() :
106       Record( DimensionType_Length ),
107       FirstPoint( gp::Origin() ),
108       SecondPoint( gp::Origin() ),
109       Plane( gp::XOY() ),
110       Flyout( 0.0 ),
111       TextHPos( Prs3d_DTHP_Fit ),
112       TextVPos( Prs3d_DTVP_Center ),
113       ArrowPos( Prs3d_DAO_Fit )
114       {}
115
116     Length( const Length& theOther ) :
117       Record( DimensionType_Length ),
118       FirstPoint( theOther.FirstPoint ),
119       SecondPoint( theOther.SecondPoint ),
120       Plane( theOther.Plane ),
121       Flyout( theOther.Flyout ),
122       TextHPos( theOther.TextHPos ),
123       TextVPos( theOther.TextVPos ),
124       ArrowPos( theOther.ArrowPos )
125       {}
126
127     ~Length() {}
128
129     /*!
130      * \brief Inits property fields from the passed length object.
131      * \param theIO [in] the interactive presentation.
132      * \param theLCS [in] the local coordiante system of parent object.
133      */
134     void Init( const Handle(AIS_LengthDimension)& theIO, const gp_Ax3& theLCS );
135
136     /*!
137      * \brief Updates length object properties from the fields.
138      * \param theIO [in/out] the interactive presentation.
139      * \param theLCS [in] the local coordiante system of parent object.
140      */
141     void Update( Handle(AIS_LengthDimension)& theIO, const gp_Ax3& theLCS );
142
143     /*!
144      * \brief Overload comparsion.
145      */
146     bool operator == (const Length &theOther) const;
147     bool operator != (const Length &theOther) const { return !(operator == (theOther)); }
148
149     gp_Pnt FirstPoint;
150     gp_Pnt SecondPoint;
151     gp_Pln Plane;
152     double Flyout;
153     Prs3d_DimensionTextHorizontalPosition TextHPos;
154     Prs3d_DimensionTextVerticalPosition   TextVPos;
155     Prs3d_DimensionArrowOrientation       ArrowPos;
156   };
157
158   /*!
159    * \brief Declaration of properties for diameter dimensions.
160    */
161   struct Standard_EXPORT Diameter : public Record
162   {
163     Diameter() :
164       Record( DimensionType_Diameter ),
165       Plane( gp::XOY() ),
166       Flyout( 0.0 ),
167       TextHPos( Prs3d_DTHP_Fit ),
168       TextVPos( Prs3d_DTVP_Center ),
169       ArrowPos( Prs3d_DAO_Fit )
170       {}
171
172      Diameter( const Diameter& theOther ) :
173        Record( DimensionType_Diameter ),
174        Circle( theOther.Circle ),
175        Plane( theOther.Plane ),
176        Flyout( theOther.Flyout ),
177        TextHPos( theOther.TextHPos ),
178        TextVPos( theOther.TextVPos ),
179        ArrowPos( theOther.ArrowPos )
180        {}
181
182     ~Diameter() {}
183
184     /*!
185      * \brief Inits property fields from the passed length object.
186      * \param theIO [in] the interactive presentation.
187      * \param theLCS [in] the local coordiante system of parent object.
188      */
189     void Init( const Handle(AIS_DiameterDimension)& theIO, const gp_Ax3& theLCS );
190
191     /*!
192      * \brief Updates length object properties from the fields.
193      * \param theIO [in/out] the interactive presentation.
194      * \param theLCS [in] the local coordiante system of parent object.
195      */
196     void Update( Handle(AIS_DiameterDimension)& theIO, const gp_Ax3& theLCS );
197
198     /*!
199      * \brief Overload comparsion.
200      */
201     bool operator == (const Diameter &theOther) const;
202     bool operator != (const Diameter &theOther) const { return !(operator == (theOther)); }
203
204     gp_Circ Circle;
205     gp_Pln  Plane;
206     double Flyout;
207     Prs3d_DimensionTextHorizontalPosition TextHPos;
208     Prs3d_DimensionTextVerticalPosition   TextVPos;
209     Prs3d_DimensionArrowOrientation       ArrowPos;
210   };
211
212   /*!
213    * \brief Declaration of properties for angle dimensions.
214    */
215   struct Standard_EXPORT Angle : public Record
216   {
217     Angle() :
218       Record( DimensionType_Angle ),
219       FirstPoint( gp::Origin() ),
220       SecondPoint( gp::Origin() ),
221       CenterPoint( gp::Origin() ),
222       Flyout( 0.0 ),
223       TextHPos( Prs3d_DTHP_Fit ),
224       TextVPos( Prs3d_DTVP_Center ),
225       ArrowPos( Prs3d_DAO_Fit )
226       {}
227
228     Angle( const Angle& theOther ) :
229       Record( DimensionType_Angle ),
230       FirstPoint( theOther.FirstPoint ),
231       SecondPoint( theOther.SecondPoint ),
232       CenterPoint( theOther.CenterPoint ),
233       Flyout( theOther.Flyout ),
234       TextHPos( theOther.TextHPos ),
235       TextVPos( theOther.TextVPos ),
236       ArrowPos( theOther.ArrowPos )
237       {}
238
239     ~Angle() {}
240
241     /*!
242      * \brief Inits property fields from the passed length object.
243      * \param theIO [in] the interactive presentation.
244      * \param theLCS [in] the local coordiante system of parent object.
245      */
246     void Init( const Handle(AIS_AngleDimension)& theIO, const gp_Ax3& theLCS );
247
248     /*!
249      * \brief Updates length object properties from the fields.
250      * \param theIO [in/out] the interactive presentation.
251      * \param theLCS [in] the local coordiante system of parent object.
252      */
253     void Update( Handle(AIS_AngleDimension)& theIO, const gp_Ax3& theLCS );
254
255     /*!
256      * \brief Overload comparsion.
257      */
258     bool operator == (const Angle &theOther) const;
259     bool operator != (const Angle &theOther) const { return !(operator == (theOther)); }
260
261     gp_Pnt FirstPoint;
262     gp_Pnt SecondPoint;
263     gp_Pnt CenterPoint;
264     double Flyout;
265     Prs3d_DimensionTextHorizontalPosition TextHPos;
266     Prs3d_DimensionTextVerticalPosition   TextVPos;
267     Prs3d_DimensionArrowOrientation       ArrowPos;
268   };
269
270   typedef QSharedPointer<Record> RecordPtr;
271
272 public:
273
274   /*!
275    * \brief Constructor. Inits empty property.
276    */
277   GEOMGUI_DimensionProperty();
278
279   /*!
280    * \brief Copy constructor.
281    */
282   GEOMGUI_DimensionProperty( const GEOMGUI_DimensionProperty& theOther );
283
284    /*!
285    * \brief Constructor. Inits property from attribute.
286    */
287   GEOMGUI_DimensionProperty( SalomeApp_Study* theStudy, const std::string& theEntry );
288
289   /*!
290    * \brief Destructor.
291    */
292   ~GEOMGUI_DimensionProperty();
293
294   /*!
295    * \brief Overload QVariant cast operator.
296    */
297   operator QVariant();
298
299   /*!
300    * \brief Overload comparsion.
301    */
302   bool operator == (const GEOMGUI_DimensionProperty &theOther) const;
303
304   /*!
305    * \brief Overload comparsion.
306    */
307   bool operator != (const GEOMGUI_DimensionProperty &theOther) const
308   {
309     return !(operator == (theOther));
310   }
311
312 public:
313
314   /*!
315    * \brief Returns number of dimension records.
316    */
317   int GetNumber() const;
318
319   /*!
320    * \brief Adds new record for the passed interactive object. Convenience method.
321    * \param theIO [in] the interactive dimension to append as new record.
322    * \param theLCS [in] the local coordinate system of parent object.
323    */
324   void AddRecord( const Handle(AIS_Dimension)& theIO, const gp_Ax3& theLCS );
325
326   /*!
327    * \brief Adds new dimension record.
328    * \param theRecord [in] the record to add.
329    */
330   void AddRecord( const RecordPtr& theRecord );
331
332   /*!
333    * \brief Update dimension record data.
334    * \param theIndex [in] the index of the record.
335    * \param theIO [in] the interactive dimension to update properties.
336    * \param theLCS [in] the local coordinate system of parent object.
337    */
338   void SetRecord( const int theIndex,
339                   const Handle(AIS_Dimension)& theIO,
340                   const gp_Ax3& theLCS );
341
342   /*!
343    * \brief Update dimension record data.
344    * \param theIndex [in] the index of the dimension record.
345    * \param theRecord [in] the record to replace with.
346    */
347   void SetRecord( const int theIndex, const RecordPtr& theRecord );
348
349   /*!
350    * \brief Access record by index.
351    * \param theIndex [in] the index of the dimension record.
352    */
353   const RecordPtr& GetRecord( const int theIndex ) const;
354
355   /*!
356    * \brief Removes record by its index.
357    * \param theIndex [in] the index of dimension record.
358    */
359   void RemoveRecord( const int theIndex );
360
361   /*!
362    * \brief Clears property data.
363    */
364   void Clear();
365
366 public:
367
368   /*!
369    * \brief Returns visibility state of dimension record by its index.
370    *
371    * \param theIndex [in] the index of the dimension record.
372    */
373   bool IsVisible( const int theIndex ) const;
374
375   /*!
376    * \brief Changes visibility state of the dimension record.
377    *
378    * \param theIndex [in] the index of the dimension record.
379    * \param theIsVisible [in] the new visibility state.
380    */
381   void SetVisible( const int theIndex, const bool theIsVisible );
382
383   /*!
384    * \brief Returns name of dimension record by its index.
385    *
386    * \param theIndex [in] the index of the dimension record.
387    */
388   QString GetName( const int theIndex ) const;
389
390   /*!
391    * \brief Changes name of dimension record.
392    *
393    * \param theIndex [in] the index of the dimension record.
394    * \param theName [in] the new name.
395    */
396   void SetName( const int theIndex, const QString& theName );
397
398   /*!
399    * \brief Returns dimension type.
400    */
401   int GetType( const int theIndex ) const;
402
403 public:
404
405   /*!
406    * \brief Loads properties data from attribute "AttributeTableOfReal".
407    * \param theStudy [in] the study.
408    * \param theEntry [in] the entry of GEOM object to operate with.
409    */
410   void LoadFromAttribute( SalomeApp_Study* theStudy, const std::string& theEntry );
411
412   /*!
413    * \brief Saves properties data to attribute "AttributeTableOfReal".
414    * \param theStudy [in] the study.
415    * \param theEntry [in] the entry of GEOM object to operate with.
416    */
417   void SaveToAttribute( SalomeApp_Study* theStudy, const std::string& theEntry );
418
419 private:
420
421   /*!
422    * \brief Determines dimension type code.
423    */
424   int TypeFromIO( const Handle(AIS_Dimension)& theIO ) const;
425
426 private:
427
428   typedef QVector<bool>      VectorOfVisibility;
429   typedef QVector<QString>   VectorOfNames;
430   typedef QVector<RecordPtr> VectorOfRecords;
431
432 private:
433
434   VectorOfVisibility         myVisibility;
435   VectorOfNames              myNames;
436   VectorOfRecords            myRecords;
437 };
438
439 Q_DECLARE_METATYPE(GEOMGUI_DimensionProperty);
440
441 #endif