1 // Copyright (c) 1999-2012 OPEN CASCADE SAS
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.
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.
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.
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>
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 )
47 : Graphic2d_Line( aGO ),
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 ) ),
62 gp_Pnt2d PntP( myX, myY );
63 gp_Vec2d theV1( gp_Pnt2d( myX, myY - myHeight/2 ), PntP );
64 theV1.Rotate( myAngle );
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 );
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() );
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);
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(),
102 theSz1 = myHeight * 2/5,
103 theSz2 = myHeight * 3/5;
105 switch ( aTolType ) {
106 case Prs2d_TOT_TAPER:
107 myHTol = new Prs2d_Taper( aGO, xTTol, yTTol, theSz2, myAngle );
109 case Prs2d_TOT_SYMTOTAL:
110 myHTol = new Prs2d_SymTotal( aGO, xTTol, yTTol, theSz2, myAngle );
112 case Prs2d_TOT_SYMCIRCULAR:
113 myHTol = new Prs2d_SymCircular( aGO, xTTol, yTTol, theSz2, myAngle );
115 case Prs2d_TOT_SYMMETRY:
116 myHTol = new Prs2d_Symmetry( aGO, xTTol, yTTol, theSz2, myAngle );
118 case Prs2d_TOT_CONCENTRIC:
119 myHTol = new Prs2d_Concentric( aGO, xTTol, yTTol, theSz2, myAngle );
121 case Prs2d_TOT_POSITION:
122 myHTol = new Prs2d_Position( aGO, xTTol, yTTol, theSz2, myAngle );
124 case Prs2d_TOT_ANGULARITY:
125 myHTol = new Prs2d_Angularity( aGO, xTTol, yTTol, theSz2, myAngle );
127 case Prs2d_TOT_PERPENDIC:
128 myHTol = new Prs2d_Perpendicular( aGO, xTTol, yTTol, theSz2, myAngle );
130 case Prs2d_TOT_PARALLELISM:
131 myHTol = new Prs2d_Parallelism( aGO, xTTol, yTTol, theSz1, myAngle );
133 case Prs2d_TOT_SURFACEPROF:
134 myHTol = new Prs2d_SurfProfile( aGO, xTTol, yTTol, theSz2, myAngle );
136 case Prs2d_TOT_LINEPROF:
137 myHTol = new Prs2d_LineProfile( aGO, xTTol, yTTol, theSz2, myAngle );
139 case Prs2d_TOT_CYLINDRIC:
140 myHTol = new Prs2d_Cylindric( aGO, xTTol, yTTol, theSz1, myAngle );
142 case Prs2d_TOT_CIRCULARITY:
143 myHTol = new Prs2d_Circularity( aGO, xTTol, yTTol, theSz2, myAngle );
145 case Prs2d_TOT_FLATNESS:
146 myHTol = new Prs2d_Flatness( aGO, xTTol, yTTol, theSz1, myAngle );
148 case Prs2d_TOT_STRAIGHTNESS:
149 myHTol = new Prs2d_Straightness( aGO, xTTol, yTTol, theSz2, myAngle );
158 void Prs2d_ToleranceFrame::Draw( const Handle(Graphic2d_Drawer)& aDrawer ) {
160 Standard_Boolean IsIn = Standard_False;
162 if ( ! myGOPtr->IsTransformed() )
163 IsIn = aDrawer->IsIn( myMinX, myMaxX, myMinY,myMaxY );
165 Standard_ShortReal minx, miny, maxx, maxy;
166 MinMax(minx,maxx,miny,maxy);
167 IsIn = aDrawer->IsIn( minx, maxx, miny, maxy );
172 DrawLineAttrib( aDrawer );
175 Standard_Real a1, b1;
177 TShort_Array1OfShortReal Xpoint( 1, 4 ), Ypoint( 1, 4 );
178 Xpoint.Assign( myXVert );
179 Ypoint.Assign( myYVert );
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 );
191 aDrawer->MapPolygonFromTo( Xpoint, Ypoint );
193 //------------additional text's cells
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 );
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 );
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 );
211 gp_Pnt2d Pnt1 = Pnt0.Translated( theVecV * myHeight );
212 gp_Pnt2d Pnt2 = Pnt0.Translated( theVecG * myHeight );
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 );
218 Standard_ShortReal tVecSize = myHeight;
219 Standard_ShortReal ws = 0.0, hs;
221 Standard_ShortReal X1=0., Y1=0., X2=0., Y2=0., X3, Y3, X4, Y4;
222 if ( myTolTxt.Length() ) {
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 );
234 aDrawer->MapTextFromTo( myTolTxt, X1, Y1, myAngle, 0.0, 0.0, Aspect_TOT_SOLID );
235 aDrawer->GetTextSize( myTolTxt, ws, hs );
236 tVecSize += ws + offS;
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 );
255 aDrawer->MapSegmentFromTo( X1, Y1, X2, Y2 );
258 if ( myTxt1.Length() ) {
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 );
269 aDrawer->MapTextFromTo( myTxt1, X1, Y1, myAngle, 0., 0.,Aspect_TOT_SOLID );
270 aDrawer->GetTextSize( myTxt1, ws, hs );
271 tVecSize += ws + offS;
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 );
290 aDrawer->MapSegmentFromTo( X1, Y1, X2, Y2 );
293 if ( myTxt2.Length() ) {
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 );
304 aDrawer->MapTextFromTo( myTxt2, X1, Y1, myAngle, 0., 0., Aspect_TOT_SOLID );
305 aDrawer->GetTextSize( myTxt2, ws, hs );
306 tVecSize += ws + offS;
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 );
325 aDrawer->MapSegmentFromTo( X1, Y1, X2, Y2 );
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 );
343 aDrawer->MapSegmentFromTo( X1, Y1, X3, Y3 );
344 aDrawer->MapSegmentFromTo( X2, Y2, X4, Y4 );
346 } // end if one of text strings isn't empty
349 if ( ! myHTol.IsNull() )
350 myHTol->Draw( aDrawer );
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;
364 void Prs2d_ToleranceFrame::SetHeight( const Standard_Real aHSize ) {
366 myHeight = Standard_ShortReal( aHSize );
369 Standard_Real Prs2d_ToleranceFrame::Height() const {
374 void Prs2d_ToleranceFrame::Save(Aspect_FStream& aFStream) const
378 const Handle(TColgp_HArray1OfPnt2d) Prs2d_ToleranceFrame::ArrayOfPnt2d( ) const
380 Handle(TColgp_HArray1OfPnt2d) points = new TColgp_HArray1OfPnt2d( 1, 4 );
382 for ( Standard_Integer i = 1; i <= 4; i++ )
384 points->SetValue( i, gp_Pnt2d( myXVert(i), myYVert(i) ) );
389 void Prs2d_ToleranceFrame::SetTolText( const Standard_CString aTolTxt )
391 myTolTxt = TCollection_ExtendedString(aTolTxt);
394 void Prs2d_ToleranceFrame::SetText1( const Standard_CString aTxt1 )
396 myTxt1 = TCollection_ExtendedString(aTxt1);
399 void Prs2d_ToleranceFrame::SetText2( const Standard_CString aTxt2 )
401 myTxt2 = TCollection_ExtendedString(aTxt2);
404 TCollection_ExtendedString Prs2d_ToleranceFrame::TolText( ) const
409 TCollection_ExtendedString Prs2d_ToleranceFrame::Text1() const
414 TCollection_ExtendedString Prs2d_ToleranceFrame::Text2() const
419 Standard_Real Prs2d_ToleranceFrame::TextScale( ) const