(require: angle at intersection of p1-2 and p1-4 must be obtuse)
(cw  traverse: p1,p2,p3,p4)
(ccw traverse: p1,p4,p3,p2)

(info: ihex: internal hexagon, cw/ccw dir, radius compensation)
o<ihex>  sub

#<toolno>   =  #1 (=1)
#<rpm>      =  #2 (=2000)
#<feedrate> =  #3 (=10)
#<dir>      =  #4 (=2 2conv | 3climb)
#<flatd>    =  #5 (distance across flats)
#<cutdepth> =  #6
#<zincr>    =  #7
#<zsafe>    =  #8 (=0.2)
#<zstart>   =  #9 (=0)
#<scale>    = #10 (=1)
#<rotate>   = #11 (=0)
#<xoff>     = #12 (=0)
#<yoff>     = #13 (=0)
#<spin_notify> = #14 (=0)
#<use_g43>     = #15 (=1)
#<h_for_g43>   = #16 (=0)
#<verbose>     = #17 (=0)

o<i0> if [#<scale> EQ 0]
        #<scale> = 1.0
        (debug, scale was 0, setting #<scale>)
o<i0> endif

o<loadtool> call [#<toolno>][#<use_g43>][#<h_for_g43>][#<verbose>]
#<tooldiam> =  [#5410 + .001]

o<dir> if [[#<dir> NE 2] AND [#<dir> NE 3]]
         (debug, ihex: direction must be 2 or 3 - EXITING)
         (print, ihex: direction must be 2 or 3 - EXITING)
         (AXIS,notify, ihex: direction must be 2 or 3 - EXITING)
         m2
o<dir> endif

#<f>      = [#<flatd>/2]
#<p>      = [#<f>/cos[30]]

#<pcos60> = [#<p> * cos[60]]
#<psin60> = [#<p> * sin[60]]

(p1 -> p2 -> is cw)
(apply scale,rotate, then offset to all input points)
#<x1>      = #<p>
#<y1>      = 0
o<move> call [#<x1>][#<y1>][#<rotate>][#<scale>][#<xoff>][#<yoff>]
#<x1>      = #<_move:x>
#<y1>      = #<_move:y>

#<x2>      = #<pcos60>
#<y2>      = [0 - #<psin60>]
o<move> call [#<x2>][#<y2>][#<rotate>][#<scale>][#<xoff>][#<yoff>]
#<x2>      = #<_move:x>
#<y2>      = #<_move:y>

#<x3>      = [0 - #<pcos60>]
#<y3>      = [0 - #<psin60>]
o<move> call [#<x3>][#<y3>][#<rotate>][#<scale>][#<xoff>][#<yoff>]
#<x3>      = #<_move:x>
#<y3>      = #<_move:y>

#<x4>      = [0 - #<p>]
#<y4>      = 0
o<move> call [#<x4>][#<y4>][#<rotate>][#<scale>][#<xoff>][#<yoff>]
#<x4>      = #<_move:x>
#<y4>      = #<_move:y>

#<x5>      = [0 -# <pcos60>]
#<y5>      = #<psin60>
o<move> call [#<x5>][#<y5>][#<rotate>][#<scale>][#<xoff>][#<yoff>]
#<x5>      = #<_move:x>
#<y5>      = #<_move:y>

#<x6>      = #<pcos60>
#<y6>      = #<psin60>
o<move> call [#<x6>][#<y6>][#<rotate>][#<scale>][#<xoff>][#<yoff>]
#<x6>      = #<_move:x>
#<y6>      = #<_move:y>

#<r>        = [#<tooldiam>/2]
#<cutdepth> = [0 - #<cutdepth>]
#<zincr>    = [0 - #<zincr>]

(get lengths and angles for connecting lines)
o<line> call [#<x1>][#<y1>][#<x2>][#<y2>]
#<phi12>    = #<_line:phi>
o<line> call [#<x2>][#<y2>][#<x3>][#<y3>]
#<len23>    = #<_line:len>
#<phi23>    = #<_line:phi>
#<cos23>    = #<_line:cos>
#<sin23>    = #<_line:sin>
o<line> call [#<x1>][#<y1>][#<x6>][#<y6>]
#<phi16>    = #<_line:phi>

o<line> call [#<x6>][#<y6>][#<x5>][#<y5>]
#<len65>    = #<_line:len>
#<phi65>    = #<_line:phi>
#<cos65>    = #<_line:cos>
#<sin65>    = #<_line:sin>

         g40 (cutter radius compensation off)
         f #<feedrate>
         s #<rpm> m3 (spindle cw)
o<if1>   if [#<spin_notify> GT 0]
o<spin>    call [#<rpm>]
o<if1>   endif
         g0 z#<zsafe>
         #<zcurrent> = #<zstart>
         #<pass>     = 1

o<wh010> while [#<zcurrent> GT #<cutdepth>]
              #<zcurrent> = [#<zcurrent> + #<zincr>]
o<wh020>   if [#<zcurrent> LT #<cutdepth>]
                #<zcurrent> = #<cutdepth>
o<wh020>   endif
o<wh030>   if [#<pass> GT 100]
             (debug, ihex: too many passes - EXITING)
             (print, ihex: too many passes - EXITING)
             (AXIS,notify, ihex: too many passes - EXITING)
             m2
o<wh030>   endif

o<dir00>   if [#<dir> EQ 2]
o<aaa10>     if [#<pass> EQ 1]
               (arc entry along line1-2 where corner 1 is obtuse)
               #<ax>  = [#<x1> + 1 * #<r> * cos[#<phi12>]] (entry)
               #<ay>  = [#<y1> + 1 * #<r> * sin[#<phi12>]]
               #<bx>  = [#<ax> + 2 * #<r> * sin[#<phi12>]] (preentry 1)
               #<by>  = [#<ay> - 2 * #<r> * cos[#<phi12>]]
               #<cx>  = [#<bx> + #<r> * cos[#<phi12>]]     (preentry 2)
               #<cy>  = [#<by> + #<r> * sin[#<phi12>]]
               #<vx>  = [0     - #<r> * sin[#<phi12>]]     (r vector)
               #<vy>  = [0     + #<r> * cos[#<phi12>]]
               g0  x #<cx> y #<cy> (preentry 1)
               g0  x #<bx> y #<by> (preentry 2)
/              g42 (cutter radius comp right of path)
               g2  x #<ax> y #<ay> i #<vx> j #<vy> (arc entry)
               #<xfinal_a> = [#<x2>]
               #<yfinal_a> = [#<y2>]
               (go along the 2-->3 line to exit)
               (angle between 1-->2 and 2-->3)
               (only necessary for acute angles)
               #<angle> = [180 + #<phi23> - #<phi12>]
               #<elen>  = [#<r> / ABS[TAN[#<angle>/2]]]
               #<k>     = [#<elen>/#<len23>]
               (print  angle=#<angle> elen=#<elen> k=#<k>)
o<aaa20>       if [#<k> GE 1]
                 (debug, ihex: can't get in final corner k=#<k> - EXITING)
                 (print, ihex: can't get in final corner k=#<k> - EXITING)
                 (AXIS,notify, ihex: can't get in final corner - EXITING)
                 m2
o<aaa20>       endif
               #<xfinal_b> = [#<x2> + #<k> * #<len23> * #<cos23>]
               #<yfinal_b> = [#<y2> + #<k> * #<len23> * #<sin23>]
               g1 z #<zstart> (plunge)
o<aaa10>     endif
             x #<x2> y #<y2> z#<zcurrent>
             x #<x3> y #<y3>
             x #<x4> y #<y4>
             x #<x5> y #<y5>
             x #<x6> y #<y6>
             x #<x1> y #<y1>
             #<pass> = [#<pass> + 1]
o<dir00>   else (dir EQ 3 CCW)
o<bbb10>     if [#<pass> EQ 1]
               (arc entry along line4-1 where corner 1 is obtuse)
               #<ax>  = [#<x1> + 1 * #<r> * cos[#<phi16>]]       (entry)
               #<ay>  = [#<y1> + 1 * #<r> * sin[#<phi16>]]
               #<bx>  = [#<ax> - 2 * #<r> * sin[#<phi16>]]   (preentry 1)
               #<by>  = [#<ay> + 2 * #<r> * cos[#<phi16>]]
               #<cx>  = [#<bx> + #<r> * cos[#<phi16>]]       (preentry 2)
               #<cy>  = [#<by> + #<r> * sin[#<phi16>]]
               #<vx>  = [0 + #<r> * sin[#<phi16>]]           (r vector)
               #<vy>  = [0 - #<r> * cos[#<phi16>]]
               g0  x #<cx> y #<cy> (preentry 1)
               g0  x #<bx> y #<by> (preentry 2)
/              g41 (cutter radius comp left of path)
               g3  x #<ax> y #<ay> i #<vx> j #<vy> (arc entry)
               #<xfinal_a> = [#<x6>]
               #<yfinal_a> = [#<y6>]
               (go along the 4-->3 line to exit)
               (angle between 4-->3 and 1-->4)
               (only necessary for acute angles)
               #<angle> = [180 - #<phi16> + #<phi65>]
               #<elen>  = [#<r> / ABS[TAN[#<angle>/2]]]
               #<k>     = [#<elen>/#<len65>]
o<bbb20>       if [#<k> GE 1]
                 (debug, ihex: can't get in corner k=#<k> - EXITING)
                 (print, ihex: can't get in corner k=#<k> - EXITING)
                 (AXIS,notify, ihex: can't get in corner - EXITING)
                 m2
o<bbb20>       endif
               #<xfinal_b> = [#<x6> + 1.01* #<k> * #<len65> * #<cos65>]
               #<yfinal_b> = [#<y6> + 1.01* #<k> * #<len65> * #<sin65>]
               g1 z #<zstart> (plunge)
o<bbb10>     endif
             x #<x6> y #<y6> z#<zcurrent>
             x #<x5> y #<y5>
             x #<x4> y #<y4>
             x #<x3> y #<y3>
             x #<x2> y #<y2>
             x #<x1> y #<y1>
             #<pass> = [#<pass> + 1]
o<dir00>   endif

o<wh010> endwhile

         g1 x #<xfinal_a> y #<yfinal_a>
         g1 x #<xfinal_b> y #<yfinal_b>

         g0 z #<zsafe>
         g40
o<ihex> endsub
