d867faa56e90a399e28d8acda098ed12188da1dd
[occt.git] / src / Prs2d / Prs2d_ToleranceFrame.cxx
1 // Copyright (c) 1999-2012 OPEN CASCADE SAS
2 //
3 // The content of this file is subject to the Open CASCADE Technology Public
4 // License Version 6.5 (the "License"). You may not use the content of this file
5 // except in compliance with the License. Please obtain a copy of the License
6 // at http://www.opencascade.org and read it completely before using this file.
7 //
8 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
10 //
11 // The Original Code and all software distributed under the License is
12 // distributed on an "AS IS" basis, without warranty of any kind, and the
13 // Initial Developer hereby disclaims all such warranties, including without
14 // limitation, any warranties of merchantability, fitness for a particular
15 // purpose or non-infringement. Please see the License for the specific terms
16 // and conditions governing the rights and limitations under the License.
17
18 #include <Prs2d_ToleranceFrame.ixx>
19 #include <Prs2d_Tolerance.hxx>
20 #include <Prs2d_Straightness.hxx>
21 #include <Prs2d_Flatness.hxx>
22 #include <Prs2d_Circularity.hxx>
23 #include <Prs2d_Cylindric.hxx>
24 #include <Prs2d_LineProfile.hxx>
25 #include <Prs2d_SurfProfile.hxx>
26 #include <Prs2d_Parallelism.hxx>
27 #include <Prs2d_Perpendicular.hxx>
28 #include <Prs2d_Angularity.hxx>
29 #include <Prs2d_Position.hxx>
30 #include <Prs2d_Concentric.hxx>
31 #include <Prs2d_Symmetry.hxx>
32 #include <Prs2d_SymCircular.hxx>
33 #include <Prs2d_SymTotal.hxx>
34 #include <Prs2d_Taper.hxx>
35
36 Prs2d_ToleranceFrame::Prs2d_ToleranceFrame( const Handle(Graphic2d_GraphicObject)& aGO,
37                                             const Quantity_Length aX,
38                                             const Quantity_Length aY,
39                                             const Quantity_Length aHeight,
40                                             const Prs2d_TypeOfTolerance aTolType,
41                                             const Standard_CString aTolTxt,
42                                             const Standard_CString aTxt1,
43                                             const Standard_CString aTxt2,
44                                             const Standard_Real anAngle,
45                                             const Standard_Real aScale )
46
47   : Graphic2d_Line( aGO ),
48    
49    myX( Standard_ShortReal( aX ) ),
50    myY( Standard_ShortReal( aY ) ),
51    myTolTxt( TCollection_ExtendedString(aTolTxt) ),
52    myTxt1( TCollection_ExtendedString(aTxt1) ),
53    myTxt2( TCollection_ExtendedString(aTxt2) ),
54    myHeight( Standard_ShortReal( aHeight ) ),
55    myAngle( Standard_ShortReal( anAngle ) ),
56    myScale( Standard_ShortReal( aScale ) ),
57    myXVert( 1, 4 ),
58    myYVert( 1, 4 )
59   
60 {
61
62     gp_Pnt2d PntP( myX, myY );
63     gp_Vec2d theV1( gp_Pnt2d( myX, myY - myHeight/2 ), PntP );
64     theV1.Rotate( myAngle );
65     theV1.Reverse();
66     gp_Pnt2d Pnt0 = PntP.Translated( theV1 );
67     gp_Vec2d theVecG( Pnt0, gp_Pnt2d( Pnt0.X() + 1.0, Pnt0.Y() ) );
68     theVecG.Rotate( myAngle );
69     gp_Vec2d theVecV = theVecG.Rotated( M_PI / 2 );
70     theVecG.Normalize();
71     theVecV.Normalize();
72
73     gp_Pnt2d Pnt1 = Pnt0.Translated( theVecV * myHeight );
74     myXVert(1) = Standard_ShortReal( Pnt0.X() );
75     myYVert(1) = Standard_ShortReal( Pnt0.Y() );
76     myXVert(2) = Standard_ShortReal( Pnt1.X() );
77     myYVert(2) = Standard_ShortReal( Pnt1.Y() );
78     gp_Pnt2d Pnt2 = Pnt0.Translated( theVecG * myHeight );
79     myXVert(4) = Standard_ShortReal( Pnt2.X() );
80     myYVert(4) = Standard_ShortReal( Pnt2.Y() );
81     Pnt2 = Pnt1.Translated( theVecG * myHeight );
82     myXVert(3) = Standard_ShortReal( Pnt2.X() );
83     myYVert(3) = Standard_ShortReal( Pnt2.Y() );
84     
85     myMinX      = myX;
86     myMinY      = myY;
87     myMaxX      = myX;
88     myMaxY      = myY;
89
90     for ( Standard_Integer i = 1; i <= 4; i++ ) {
91            if ( myXVert(i) < myMinX ) myMinX = myXVert(i);
92        if ( myYVert(i) < myMinY ) myMinY = myYVert(i);
93        if ( myXVert(i) > myMaxX ) myMaxX = myXVert(i);
94            if ( myYVert(i) > myMaxY ) myMaxY = myYVert(i);
95     }
96
97     gp_Vec2d VecTol( PntP, gp_Pnt2d( myX + myHeight/2, myY ) );
98     VecTol.Rotate( myAngle );
99     PntP.Translate( VecTol );
100     Standard_Real  xTTol   = PntP.X(),
101                    yTTol   = PntP.Y(),
102                    theSz1  = myHeight * 2/5,
103                    theSz2  = myHeight * 3/5;
104     
105     switch ( aTolType )  {
106         case Prs2d_TOT_TAPER:
107            myHTol = new Prs2d_Taper( aGO, xTTol, yTTol, theSz2, myAngle );
108         break;
109         case Prs2d_TOT_SYMTOTAL:
110            myHTol = new Prs2d_SymTotal( aGO, xTTol, yTTol, theSz2, myAngle  );
111         break;
112         case Prs2d_TOT_SYMCIRCULAR:
113            myHTol = new Prs2d_SymCircular( aGO, xTTol, yTTol, theSz2, myAngle  );
114         break;
115         case Prs2d_TOT_SYMMETRY:
116            myHTol = new Prs2d_Symmetry( aGO, xTTol, yTTol, theSz2, myAngle  );
117         break;
118         case Prs2d_TOT_CONCENTRIC:
119            myHTol = new Prs2d_Concentric( aGO, xTTol, yTTol, theSz2, myAngle  );
120         break;
121         case Prs2d_TOT_POSITION:
122            myHTol = new Prs2d_Position( aGO, xTTol, yTTol, theSz2, myAngle  );
123         break;
124         case Prs2d_TOT_ANGULARITY:
125            myHTol = new Prs2d_Angularity( aGO, xTTol, yTTol, theSz2, myAngle  );
126         break;
127         case Prs2d_TOT_PERPENDIC:
128            myHTol = new Prs2d_Perpendicular( aGO, xTTol, yTTol, theSz2, myAngle  );
129         break;
130         case Prs2d_TOT_PARALLELISM:
131            myHTol = new Prs2d_Parallelism( aGO, xTTol, yTTol, theSz1, myAngle  );
132         break;
133         case Prs2d_TOT_SURFACEPROF:
134            myHTol = new Prs2d_SurfProfile( aGO, xTTol, yTTol, theSz2, myAngle  );
135         break;
136         case Prs2d_TOT_LINEPROF:
137            myHTol = new Prs2d_LineProfile( aGO, xTTol, yTTol, theSz2, myAngle  );
138         break;
139         case Prs2d_TOT_CYLINDRIC:
140            myHTol = new Prs2d_Cylindric( aGO, xTTol, yTTol, theSz1, myAngle  );
141         break;
142         case Prs2d_TOT_CIRCULARITY:
143            myHTol = new Prs2d_Circularity( aGO, xTTol, yTTol, theSz2, myAngle  );
144         break;
145         case Prs2d_TOT_FLATNESS:
146            myHTol = new Prs2d_Flatness( aGO, xTTol, yTTol, theSz1, myAngle  );
147         break;
148         case Prs2d_TOT_STRAIGHTNESS:
149            myHTol = new Prs2d_Straightness( aGO, xTTol, yTTol, theSz2, myAngle  );
150         break;
151         case Prs2d_TOT_NONE:
152         default:
153         break;
154
155     }
156 }
157
158 void Prs2d_ToleranceFrame::Draw( const Handle(Graphic2d_Drawer)& aDrawer ) {
159
160    Standard_Boolean IsIn = Standard_False;
161
162    if ( ! myGOPtr->IsTransformed() )
163      IsIn = aDrawer->IsIn( myMinX, myMaxX, myMinY,myMaxY );
164    else {
165      Standard_ShortReal minx, miny, maxx, maxy;
166      MinMax(minx,maxx,miny,maxy);
167      IsIn = aDrawer->IsIn( minx, maxx, miny, maxy );
168    }
169
170    if ( IsIn ) {
171
172     DrawLineAttrib( aDrawer );
173
174     gp_GTrsf2d aTrsf; 
175     Standard_Real a1, b1;
176         
177     TShort_Array1OfShortReal Xpoint( 1, 4 ), Ypoint( 1, 4 );
178     Xpoint.Assign( myXVert );
179     Ypoint.Assign( myYVert );
180
181     if ( myGOPtr->IsTransformed() ) {
182       aTrsf = myGOPtr->Transform ();
183       for ( Standard_Integer j = 1; j <= 4; j++ ) {
184         a1 = Standard_Real( Xpoint(j) );
185         b1 = Standard_Real( Ypoint(j) );
186         aTrsf.Transforms( a1, b1 );
187         Xpoint(j) = Standard_ShortReal( a1 );
188         Ypoint(j) = Standard_ShortReal( b1 );
189       }
190     }
191     aDrawer->MapPolygonFromTo( Xpoint, Ypoint );
192     
193     //------------additional text's cells
194
195     if ( myTolTxt.Length() || myTxt1.Length() || myTxt1.Length() ) {
196       gp_Pnt2d PntP( myX, myY );
197       gp_Vec2d theV1( gp_Pnt2d( myX, myY - myHeight/2 ), PntP );
198       theV1.Rotate( myAngle );
199       theV1.Reverse();
200       gp_Pnt2d Pnt0 = PntP.Translated( theV1 ),
201                PntT = PntP.Translated( theV1 / 2 );
202       gp_Vec2d theVecT( PntT, gp_Pnt2d( PntT.X() + 1.0, PntT.Y() ) );
203       theVecT.Rotate( myAngle );
204       theVecT.Normalize();
205       gp_Vec2d theVecG( Pnt0, gp_Pnt2d( Pnt0.X() + 1.0, Pnt0.Y() ) );
206       theVecG.Rotate( myAngle );
207       gp_Vec2d theVecV = theVecG.Rotated( M_PI / 2 );
208       theVecG.Normalize();
209       theVecV.Normalize();
210
211       gp_Pnt2d Pnt1 = Pnt0.Translated( theVecV * myHeight );
212       gp_Pnt2d Pnt2 = Pnt0.Translated( theVecG * myHeight );
213     
214       const Standard_ShortReal offS = Standard_ShortReal( 5 );
215       aDrawer->SetTextAttrib( myColorIndex, 0, 0, Standard_ShortReal(myScale*aDrawer->Scale()), 
216                               Standard_ShortReal(myScale*aDrawer->Scale()), Standard_False );
217     
218       Standard_ShortReal tVecSize = myHeight;
219       Standard_ShortReal ws = 0.0, hs;
220       gp_Pnt2d tmpPntTxt;
221       Standard_ShortReal X1=0., Y1=0., X2=0., Y2=0., X3, Y3, X4, Y4;
222       if ( myTolTxt.Length() ) {
223          tVecSize += offS;
224          tmpPntTxt = PntT.Translated( theVecT * tVecSize );
225          X1 = Standard_ShortReal( tmpPntTxt.X() ); 
226          Y1 = Standard_ShortReal( tmpPntTxt.Y() );
227          if ( myGOPtr->IsTransformed () ) {
228            aTrsf = myGOPtr->Transform();
229            a1 = Standard_Real( X1 );
230            b1 = Standard_Real( Y1 );
231            aTrsf.Transforms( a1, b1 );
232            X1 = Standard_ShortReal( a1 ); Y1 = Standard_ShortReal( b1 );
233          }
234          aDrawer->MapTextFromTo( myTolTxt, X1, Y1, myAngle, 0.0, 0.0, Aspect_TOT_SOLID );    
235          aDrawer->GetTextSize( myTolTxt, ws, hs );
236          tVecSize += ws  + offS;
237
238          Pnt2 = Pnt0.Translated( theVecG * tVecSize );
239          X1 = Standard_ShortReal( Pnt2.X() );
240          Y1 = Standard_ShortReal( Pnt2.Y() );
241          Pnt2 = Pnt1.Translated( theVecG * tVecSize );
242          X2 = Standard_ShortReal( Pnt2.X() );
243          Y2 = Standard_ShortReal( Pnt2.Y() );
244          if ( myGOPtr->IsTransformed () ) {
245            aTrsf = myGOPtr->Transform();
246            a1 = Standard_Real( X1 );
247            b1 = Standard_Real( Y1 );
248            aTrsf.Transforms( a1, b1 );
249            X1 = Standard_ShortReal( a1 ); Y1 = Standard_ShortReal( b1 );
250            a1 = Standard_Real( X2 );
251            b1 = Standard_Real( Y2 );
252            aTrsf.Transforms( a1, b1 );
253            X2 = Standard_ShortReal( a1 ); Y2 = Standard_ShortReal( b1 );
254          }
255          aDrawer->MapSegmentFromTo( X1, Y1, X2, Y2 );
256        }
257
258     if ( myTxt1.Length() ) {
259         tVecSize += offS;
260         tmpPntTxt = PntT.Translated( theVecT * tVecSize );
261         X1 = Standard_ShortReal( tmpPntTxt.X() ); Y1 = Standard_ShortReal( tmpPntTxt.Y() );
262         if ( myGOPtr->IsTransformed () ) {
263            aTrsf = myGOPtr->Transform();
264            a1 = Standard_Real( X1 );
265            b1 = Standard_Real( Y1 );
266            aTrsf.Transforms( a1, b1 );
267            X1 = Standard_ShortReal( a1 ); Y1 = Standard_ShortReal( b1 );
268         }
269         aDrawer->MapTextFromTo( myTxt1, X1, Y1, myAngle, 0., 0.,Aspect_TOT_SOLID );    
270         aDrawer->GetTextSize( myTxt1, ws, hs );
271         tVecSize += ws + offS;
272
273         Pnt2 = Pnt0.Translated( theVecG * tVecSize );
274         X1 = Standard_ShortReal( Pnt2.X() );
275         Y1 = Standard_ShortReal( Pnt2.Y() );
276         Pnt2 = Pnt1.Translated( theVecG * tVecSize );
277         X2 = Standard_ShortReal( Pnt2.X() );
278         Y2 = Standard_ShortReal( Pnt2.Y() );
279         if ( myGOPtr->IsTransformed () ) {
280           aTrsf = myGOPtr->Transform();
281           a1 = Standard_Real( X1 );
282           b1 = Standard_Real( Y1 );
283           aTrsf.Transforms( a1, b1 );
284           X1 = Standard_ShortReal( a1 ); Y1 = Standard_ShortReal( b1 );
285           a1 = Standard_Real( X2 );
286           b1 = Standard_Real( Y2 );
287           aTrsf.Transforms( a1, b1 );
288           X2 = Standard_ShortReal( a1 ); Y2 = Standard_ShortReal( b1 );
289         }
290         aDrawer->MapSegmentFromTo( X1, Y1, X2, Y2 );
291     }
292
293     if ( myTxt2.Length() ) {
294         tVecSize += offS;
295         tmpPntTxt = PntT.Translated( theVecT * tVecSize );
296         X1 = Standard_ShortReal( tmpPntTxt.X() ); Y1 = Standard_ShortReal( tmpPntTxt.Y() );
297         if ( myGOPtr->IsTransformed () ) {
298            aTrsf = myGOPtr->Transform();
299            a1 = Standard_Real( X1 );
300            b1 = Standard_Real( Y1 );
301            aTrsf.Transforms( a1, b1 );
302            X1 = Standard_ShortReal( a1 ); Y1 = Standard_ShortReal( b1 );
303         }
304         aDrawer->MapTextFromTo( myTxt2, X1, Y1, myAngle, 0., 0., Aspect_TOT_SOLID );    
305         aDrawer->GetTextSize( myTxt2, ws, hs );
306         tVecSize += ws + offS;
307
308         Pnt2 = Pnt0.Translated( theVecG * tVecSize );
309         X1 = Standard_ShortReal( Pnt2.X() );
310         Y1 = Standard_ShortReal( Pnt2.Y() );
311         Pnt2 = Pnt1.Translated( theVecG * tVecSize );
312         X2 = Standard_ShortReal( Pnt2.X() );
313         Y2 = Standard_ShortReal( Pnt2.Y() );
314         if ( myGOPtr->IsTransformed () ) {
315           aTrsf = myGOPtr->Transform();
316           a1 = Standard_Real( X1 );
317           b1 = Standard_Real( Y1 );
318           aTrsf.Transforms( a1, b1 );
319           X1 = Standard_ShortReal( a1 ); Y1 = Standard_ShortReal( b1 );
320           a1 = Standard_Real( X2 );
321           b1 = Standard_Real( Y2 );
322           aTrsf.Transforms( a1, b1 );
323           X2 = Standard_ShortReal( a1 ); Y2 = Standard_ShortReal( b1 );
324         }
325         aDrawer->MapSegmentFromTo( X1, Y1, X2, Y2 );
326     }
327
328     X3 = Standard_ShortReal( Pnt0.X() );
329     Y3 = Standard_ShortReal( Pnt0.Y() );
330     X4 = Standard_ShortReal( Pnt1.X() );
331     Y4 = Standard_ShortReal( Pnt1.Y() );
332     if ( myGOPtr->IsTransformed () ) {
333       aTrsf = myGOPtr->Transform();
334       a1 = Standard_Real( X3 );
335       b1 = Standard_Real( Y3 );
336       aTrsf.Transforms( a1, b1 );
337       X3 = Standard_ShortReal( a1 ); Y3 = Standard_ShortReal( b1 );
338       a1 = Standard_Real( X4 );
339       b1 = Standard_Real( Y4 );
340       aTrsf.Transforms( a1, b1 );
341       X4 = Standard_ShortReal( a1 ); Y4 = Standard_ShortReal( b1 );
342     }
343     aDrawer->MapSegmentFromTo( X1, Y1, X3, Y3 );
344     aDrawer->MapSegmentFromTo( X2, Y2, X4, Y4 );
345     
346    } // end if one of text strings isn't empty
347  } // end if IsIn
348  
349  if ( ! myHTol.IsNull() )
350   myHTol->Draw( aDrawer );
351        
352 }
353
354 Standard_Boolean Prs2d_ToleranceFrame::Pick( 
355                         const Standard_ShortReal X,
356                         const Standard_ShortReal Y,
357                         const Standard_ShortReal aPrecision,
358                         const Handle(Graphic2d_Drawer)& /*aDrawer*/ ) {
359     if ( IsInMinMax( X, Y, aPrecision ) ) 
360         return Standard_True;
361     return Standard_False;
362 }
363
364 void Prs2d_ToleranceFrame::SetHeight( const Standard_Real aHSize ) {
365     
366   myHeight = Standard_ShortReal( aHSize ); 
367 }
368
369 Standard_Real Prs2d_ToleranceFrame::Height() const {
370     
371   return myHeight;    
372 }
373
374 void Prs2d_ToleranceFrame::Save(Aspect_FStream& aFStream) const
375 {
376 }
377
378 const Handle(TColgp_HArray1OfPnt2d)  Prs2d_ToleranceFrame::ArrayOfPnt2d( ) const
379 {
380    Handle(TColgp_HArray1OfPnt2d) points = new TColgp_HArray1OfPnt2d( 1, 4 );
381                 
382    for ( Standard_Integer i = 1; i <= 4; i++ ) 
383    {
384      points->SetValue( i, gp_Pnt2d( myXVert(i), myYVert(i) ) );
385    }   
386    return points;
387 }
388
389 void Prs2d_ToleranceFrame::SetTolText( const Standard_CString aTolTxt ) 
390 {
391   myTolTxt = TCollection_ExtendedString(aTolTxt);
392 }
393
394 void Prs2d_ToleranceFrame::SetText1( const Standard_CString aTxt1 ) 
395 {
396   myTxt1 = TCollection_ExtendedString(aTxt1);
397 }
398
399 void Prs2d_ToleranceFrame::SetText2( const Standard_CString aTxt2 ) 
400 {
401   myTxt2 = TCollection_ExtendedString(aTxt2);
402 }
403
404 TCollection_ExtendedString Prs2d_ToleranceFrame::TolText( ) const 
405 {
406   return myTolTxt;
407 }
408
409 TCollection_ExtendedString Prs2d_ToleranceFrame::Text1() const 
410 {
411   return myTxt1;
412 }
413
414 TCollection_ExtendedString Prs2d_ToleranceFrame::Text2() const 
415 {
416   return myTxt2;
417 }
418
419 Standard_Real Prs2d_ToleranceFrame::TextScale( ) const
420 {
421   return myScale;
422 }