home *** CD-ROM | disk | FTP | other *** search
/ PC & Mediji 1997 January / PCM_9701.iso / programi / minicad / minicad.1 / SPURGEAR.MPC < prev    next >
Encoding:
Text File  |  1996-04-30  |  5.5 KB  |  222 lines

  1. Procedure SpurGear;
  2. {
  3. (Windows version)
  4. ⌐1996, Diehl Graphsoft, Inc.
  5. Developed by Tom Urie
  6.  
  7. This procedure draws a spur gear.
  8. }
  9. LABEL 20,98,99;
  10. CONST
  11.     f1=0.3966;  {These five constants are used to modify the six points}
  12.     fx1=0.935;    {that define the tooth profile so that the bezier curve}
  13.     fy1=0.5;      {approximates an involute curve.}
  14.     fx2=1.105;
  15.     fy2=1.25;
  16. VAR
  17.     Alpha,A1,Beta,PDia:REAL;
  18.     a,b,OD,RD,DPitch,r: REAL;
  19.     x0,y0,x0t,y0t : REAL;
  20.     x,y,xt,yt : ARRAY[1..6] OF REAL;
  21.     Theta1,Theta2 : REAL;
  22.     j,k,NTeeth:INTEGER;
  23.     Curves,Abort:BOOLEAN;
  24.  
  25. Procedure GearDialog;
  26. {
  27. This procedure defines the dialog box.
  28. }
  29. VAR
  30.     Width,x1,y1,x2,y2,px1,px2,px3,px4,py1,py2 : INTEGER;
  31.  
  32. Procedure AlignScr(Width:INTEGER; VAR x1,x2:INTEGER);
  33. VAR
  34.     scrx1,scry1,scrx2,scry2:INTEGER;
  35. BEGIN
  36.     GetScreen(scrx1,scry1,scrx2,scry2);
  37.     x1:=((scrx1+scrx2) div 2)-(Width div 2);
  38.     x2:=x1+Width; 
  39. END;
  40.  
  41. Procedure MakeDialog;
  42. CONST
  43.     y1=100;
  44.     scnh=180; scnw=280;
  45.     h=30;
  46. BEGIN
  47.     AlignScr(scnw,x1,x2);
  48.     y2:=y1+scnh;
  49.     px1:=scnw-160;
  50.     px2:=scnw-100;
  51.     px3:=scnw-80;
  52.     px4:=scnw-20;
  53.     py1:=scnh-40;
  54.     py2:=scnh-20;
  55.  
  56.     BeginDialog(1,1,x1,y1,x2,y2);
  57.         AddButton('OK',1,1,px1,py1,px2,py2);
  58.         AddButton('Cancel',2,1,px3,py1,px4,py2);
  59.         AddField('No. of Teeth:',4,1,20,45-h,145,60-h);
  60.         AddField('0',5,2,150,45-h,245,60-h);
  61.         AddField('Pitch Diameter:',6,1,20,70-h,145,85-h);
  62.         AddField('0',7,2,150,70-h,245,85-h);
  63.         AddField('Diametral Pitch:',8,1,20,95-h,145,110-h);
  64.         AddField('0',9,2,150,95-h,245,110-h);
  65.         AddButton('Curved Teeth',10,2,20,120-h,135,135-h);
  66.     EndDialog;
  67. END;
  68.  
  69. BEGIN
  70.     MakeDialog;
  71. END;
  72.  
  73. Procedure GetInfo;
  74. {
  75. This procedure displays the dialog box and retrieves the information.
  76. }
  77. VAR
  78.     Done,Go,Select10,OK : BOOLEAN;
  79.     Item : INTEGER;
  80. BEGIN
  81.     Done:=FALSE;
  82.     Abort:=FALSE;
  83.     NTeeth:=24;
  84.     PDia:=3;
  85.     DPitch:=8;
  86.     GetDialog(1);
  87.     SetTitle('Spur Gears');
  88.     Curves:=FALSE;
  89.     SetField(5,Num2Str(0,NTeeth));
  90.     SetField(7,Num2Str(4,PDia));
  91.     SetField(9,Num2Str(2,DPitch));
  92.     SelField(5);
  93.     Select10:=FALSE;
  94.     REPEAT
  95.         DialogEvent(Item);
  96.         IF Item=1 THEN
  97.             Done:=TRUE;
  98.         IF Item=2 then BEGIN
  99.             Done:=TRUE;
  100.             Abort:=TRUE;
  101.         END;
  102.         IF Item=5 THEN BEGIN
  103.             OK:=ValidNumStr(getfield(5),NTeeth);
  104.             IF NOT OK THEN BEGIN
  105.                 Sysbeep;
  106.                 SetField(5,Num2Str(0,NTeeth));
  107.                 SelField(5);
  108.             END;
  109.             IF DPitch <> 0 THEN
  110.                 PDia:=NTeeth/DPitch;
  111.             SetField(7,Num2Str(4,PDia));
  112.         END;    
  113.         IF Item=7 THEN BEGIN
  114.             Go:=ValidNumStr(getfield(7),PDia);
  115.             IF PDia <> 0 THEN
  116.                 DPitch:=NTeeth/PDia;
  117.             SetField(9,num2str(2,DPitch));
  118.         END;
  119.         IF Item=9 THEN BEGIN
  120.             Go:=ValidNumStr(GetField(9),DPitch);
  121.             IF DPitch <> 0 THEN
  122.                 PDia:=NTeeth/DPitch;
  123.             SetField(7,Num2Str(4,PDia));
  124.         END;
  125.         IF Item=10 THEN BEGIN
  126.             SetItem(10, NOT ItemSel(10));
  127.             Curves:= ItemSel(10);
  128.         END;
  129.     UNTIL DONE;
  130.     ClrDialog;
  131. END;
  132.  
  133. {
  134. Main program.
  135. }
  136. BEGIN
  137. {
  138. Display the dialog box and get the info.
  139. }
  140.     GearDialog;
  141.     SetCursor(ArrowC);
  142.     GetInfo;
  143.     IF Abort THEN GOTO 99;
  144.     DSelectAll;
  145. {
  146. Get location of the gear.
  147. }
  148.     GetPt(x0,y0);
  149. {
  150. Calculate gear parameters.
  151. }
  152.     a:=1/DPitch;                        {Addendum}
  153.     b:=1.157/DPitch;        {Dedendum}
  154.     OD:=PDia+2*a;                {Outside diameter}
  155.     RD:=PDia-2*b;              {Root diameter}
  156.     r:=1.5*(b-a);                    {Root fillet}
  157.     Alpha:=Deg2Rad(360/NTeeth);        {Angle between teeth}
  158.     Beta:=Alpha/4;
  159.     y0:=y0+RD/2;  {Point on the root diameter at Alpha=0}
  160. {
  161. Calculate the six points (x[1],y[1]...x[6],y[6]) that define the tooth profile.
  162. }
  163.     x[1]:=-PDia*Sin(Beta)/2;
  164.     Theta1:=ArcSin(2*x[1]/RD);
  165.     y[1]:=-RD*(1-Cos(Theta1))/2;
  166.     x[2]:=x[1];
  167.     y[2]:=(PDia*cos(Beta)-RD)/2;
  168.     x[3]:=-f1*OD*Sin(Beta)/2;
  169.     Theta2:=ArcSin(x[3]/PDia);
  170.     y[3]:=(OD*Cos(Theta2)-RD)/2;
  171.     IF Curves THEN BEGIN
  172.         x[1]:=fx1*x[1];
  173.         y[1]:=fy1*y[1];
  174.         x[2]:=fx2*x[2];
  175.         y[2]:=fy2*y[2];
  176.         y[3]:=Sqrt((OD/2)^2-x[3]^2)-RD/2;
  177.     END;
  178.     x[4]:=-x[3]; y[4]:=y[3];
  179.     x[5]:=-x[2]; y[5]:=y[2];
  180.     x[6]:=-x[1]; y[6]:=y[1];
  181.     A1:=Alpha;
  182.     ClosePoly;
  183.     Absolute;
  184. {
  185. Draw the gear.
  186. }
  187.     BeginPoly;
  188.     FOR k:= 1 TO NTeeth DO BEGIN
  189.         A1:=A1-Alpha;
  190. {
  191. Calculate the coordinates of the point on the root diameter at angle A1.
  192. }
  193.         x0t:=x0+RD*sin(-A1)/2;
  194.         y0t:=y0-RD*(1-cos(-A1))/2;
  195. {
  196. Transpose the 6 defining points of the tooth profile to angle A1.
  197. }
  198.         FOR j:=1 TO 6 DO BEGIN
  199.             xt[j]:=x[j]*Cos(A1)-y[j]*Sin(A1);
  200.             yt[j]:=x[j]*Sin(A1)+y[j]*Cos(A1);
  201. {
  202. Draw straight teeth.
  203. }
  204.             IF NOT Curves THEN
  205.                 LineTo((x0t+xt[j]),(y0t+yt[j]));
  206.         END;
  207.         IF NOT Curves THEN GOTO 98;
  208. {
  209. Draw curved teeth.
  210. }
  211.         ArcTo((x0t+xt[1]),(y0t+yt[1]),r);
  212.         CurveTo((x0t+xt[2]),(y0t+yt[2]));
  213.         LineTo((x0t+xt[3]),(y0t+yt[3]));
  214.         LineTo((x0t+xt[4]),(y0t+yt[4]));
  215.         CurveTo((x0t+xt[5]),(y0t+yt[5]));
  216.         ArcTo((x0t+xt[6]),(y0t+yt[6]),r);
  217.     98:END;
  218.     EndPoly;
  219. 99:END;
  220.  
  221. Run(SpurGear);
  222.