*** This script creates sectors based on selected walls.

// Parameter list begins
mov r96 0 // 0 - no boolean, 2 - join sectors, 4 - cut sectors
mov r97 1 // Taper relatively to bisectrices
mov r98 0 // Amount of taper in units
mov r99 256 // Amount to extrude (set negative to extrude into the sector)
// Parameter list ends
~~~ Default parameters:
~~~ mov r96 0 // 0 - no boolean, 2 - join sectors, 4 - cut sectors
~~~ mov r97 1 // Taper relatively to bisectrices
~~~ mov r98 0 // Amount of taper in units
~~~ mov r99 256 // Amount to extrude (set negative to extrude into the sector)
mov r20 0
getwallnumber r00
while r00 > 0
  dec r00
  getselwall r00 r01
  if r01 != 0
    getwall r00 x r01
    setpntb1x r20 r01
    getwall r00 y r01
    setpntb1y r20 r01
    inc r20
    getwall r00 point2 r02
    getwall r02 x r01
    setpntb1x r20 r01
    getwall r02 y r01
    setpntb1y r20 r01 // pntb2 will be used by sector splitting algorithm.
    inc r20
    if r97 != 0
      getwall r02 point2 r02
      getwall r02 x r01
      setpntb1x r20 r01
      getwall r02 y r01
      setpntb1y r20 r01 // Next vertex
      inc r20
      getprevwall r00 r02
      getwall r02 x r01
      setpntb1x r20 r01
      getwall r02 y r01
      setpntb1y r20 r01 // Previous vertex
      inc r20
    endif
  endif
continue
deselectwalls
if r96 != 0
  setselectionmode 6 // Figured sector modeling
  setfsmbooleanmode r96
  deselectsectors
endif
mov r00 0
while r00 < r20
  if r96 == 4  deselectsectors  endif
  settool 0
  getpntb1x r00 r01
  getpntb1y r00 r02
  sethitpoint r01 r02
  hitspace
  inc r00
  getpntb1x r00 r04
  getpntb1y r00 r05
  inc r00
  mov r16 r98 // Left taper amount
  mov r17 r98  neg r17 // Right taper amount
  if r97 != 0
    getpntb1x r00 r14
    getpntb1y r00 r15
    inc r00
    getpntb1x r00 r12
    getpntb1y r00 r13
    inc r00
    // Add bisection angle cotangent to the taper amount
    sub r12 r01     sub r13 r02
    neg r12         neg r13
    sub r14 r04     sub r15 r05
    mov r18 r04     mov r19 r05
    sub r18 r01     sub r19 r02
    
    atan2 r08 r12 r13
    atan2 r09 r18 r19
    atan2 r10 r14 r15
    sub r10 r09     sub r09 r08
    div r10 2       div r09
    cos r12 r09     cos r13 r10
    sin r09 r09     sin r10 r10
    mul r09 r99     mul r10 r99
    mov r14 r12     mov r15 r13
    if r09 > 0  neg r14  endif
    if r10 > 0  neg r15  endif
    sub r09 r14     sub r10 r15
    div r09 r12     div r10 r13
    sub r16 r09     add r17 r10
  endif
  mov r06 r04
  mov r07 r05
  sub r06 r01 // Wall vector X
  sub r07 r02 // Wall vector Y
  hypot r08 r06 r07
  mov r11 r08  div r11 2
  
  mov r09 r07  mov r10 r06  neg r10
  mul r09 r99  mul r10
  add r09 r11  add r10
  div r09 r08  div r10 // Extrusion direction
  
  mov r26 r06  mov r27 r07
  mul r06 r16  mul r07
  mul r26 r17  mul r27
  add r06 r11  add r07
  add r26 r11  add r27
  div r06 r08  div r07 // Taper direction
  div r26 r08  div r27
  
  add r01 r09  add r01 r06
  add r02 r10  add r02 r07
  sethitpoint r01 r02
  hitspace
  mov r01 r04
  mov r02 r05
  add r01 r09  add r01 r26
  add r02 r10  add r02 r27
  sethitpoint r01 r02
  hitspace
  sethitpoint r04 r05
  hitspace
  mov r03 0
  if r99 > 0  mov r03 1  endif
  if r96 != 0  mov r03 1  endif
  if r03 != 0
    getwallnumber r04
    gettoolvertx 0 r01
    gettoolverty 0 r02
    sethitpoint r01 r02
    hitspace // Close contour
    if r99 > 4
      if r96 == 0
        getwallnumber r05
        while r05 > r04
          dec r05
          bindvertex r05 4
        continue
      endif
    endif
  endif
continue
settool 0
setselectionmode 2