jw_cad 外部変形 - (1289) luaでソリッド図形のH形鋼断面を描く -

外部変形は データのやり取りをテキストファイルで行うので プログラム言語は 自由に選ぶことができます。図形は機能的かつシンプルなため、数多くのユーザーに受け入れられています。

 

luaでソリッド図形のH形鋼断面を描く

:luaでソリッド図形のH形鋼断面を描く
@echo off
set s=%*
if defined s (
  echo ^%*> %~dpn0.txt
) else (
  if exist %~dpn0.txt (
    for /f "tokens=*" %%a in (%~dpn0.txt) do set s=%%a
  )
)
if not defined s set s=400 200 8 13 16

for /f "delims=:" %%n in ('findstr /n "^#!" %0') do (
  copy jwc_temp.txt myfiles > nul
  more +%%n %0 | lua > jwc_temp.txt
)
goto:eof

REM #jww
REM #1%d 位置を指示して下さい
REM #99#
REM #c H B T1 T2 R =
REM #e

#!この次の行からプログラムを書いてください
--ユーザー定義関数
function split(str, FS) -- http://lua-users.org/wiki/SplitJoin
  if FS == "" or FS == nil then FS = " " end
  local t = {}
  local fFS = "(.-)" .. FS
  local last_end = 1
  local s, e, cap = str:find(fFS, 1)
  while s do
    if s ~= 1 or cap ~= "" then
      table.insert(t,cap)
    end
    last_end = e+1
    s, e, cap = str:find(fFS, last_end)
  end
  if last_end <= #str then
    cap = str:sub(last_end)
    table.insert(t, cap)
  end
  return t
end

function join(a, FS) --配列を平滑化してスペーサーで連結した文字列を返す
  if type(a) ~= "table" then return a end
  if FS == nil then FS = " " end
  return table.concat(flatten(a), FS)
end

function flatten(a) --配列を平滑化する
  local i, j, k
  local c = {}
  for i in pairs(a) do
    if type(a[i]) == "table" then
      --while #a[i] == 1 do a[i] = a[i][1] end
      for j in pairs(a[i]) do
        if type(a[i][j]) == "table" then
          --while #a[i][j] == 1 do a[i][j] = a[i][j][1] end
          for k in pairs(a[i][j]) do
            table.insert(c, a[i][j][k])
          end
        else
          table.insert(c, a[i][j])
        end
      end
    else
      table.insert(c, a[i])
    end
  end
  return c
end

function sprintf(...)
  if #{...} == 0 then return end
  if #{...} == 1 then return ... end
  return string.format(...)
end

function printf(...)
  return print(sprintf(...))
end

function moveto(x, y) --点x を 相対距離 y [ a, b ] へ移動
  return y and ptcalc("+", x, y) or x
end

function ptcalc(p, x, y) --点データの数値演算(加減乗除)
  local zz
  zz = {x[1], x[2], y[1], y[2]}
  if p == "+" then
    return zz and {zz[1] + zz[3], zz[2] + zz[4]} or {0, 0}
  elseif p == "-" then
    return zz and {zz[1] - zz[3], zz[2] - zz[4]} or {0, 0}
  elseif p == "*" then
    return zz and {zz[1] * zz[3], zz[2] * zz[4]} or {0, 0}
  elseif p == "/" then
    return zz and {zz[1] / zz[3], zz[2] / zz[4]} or {0, 0}
  elseif p == "ave" then
    return zz and {(zz[1] + zz[3]) / 2, (zz[2] + zz[4]) / 2} or {0, 0}
  else
    return {0, 0}
  end
end

function rot(x, d)
  if d == nil then return x end
  local x1, y1 = x[1], x[2]
  local co = math.cos(d)
  local si = math.sin(d)
  if type(x) == "table" then
    if type(x[1]) == "table" then
      local i
      for i = 1, #x do x[i] = rot(x[i], d) end
      return x
    end
  end
  return {x1 * co - y1 * si, x1 * si + y1 * co}
end

function int(a) return a < 0 and math.ceil(a) or math.floor(a) end

function offset(w, h, pt, opt)
  if opt == nil then opt = 5 end
  return w * ( (9 - pt) % 3 - (9 - opt) % 3) / 2, h * (int( (9 - pt) / 3) - int( (9 - opt) / 3)) / 2
end

function jish(hp, h, b, tw, tf, r, d, pt)
  if d == nil then d = 0 end
  if pt == nil then pt = 5 end
  x0, y0 = offset(b, h, pt)

  red, g, blue = 0, 128, 128
  printf("lc10 %s", red + 256 * g + 256 * 256 * blue)
  print("pl")

  drad = math.rad(d)
  p = {
      {tw / 2    , h / 2 - tf - r*0},
      {tw / 2 + r, h / 2 - tf - r},
      {tw / 2 + r, h / 2 - tf},
      { b / 2    , h / 2 - tf},
      { b / 2    , h / 2}
      }
  u = #p
  pp = {}
  for j = 0, 3 do
    a = 90 * ( (1 - j) % 4)
    for i = 0, u - 1 do
      k = j % 2 == 0 and i + 1 or u - i
      x, y = p[k][1], p[k][2]
      if j % 3 > 0 then x = -x end
      if j > 1 then y = -y end
      pc = moveto(rot({x + x0, y + y0}, drad), hp)
      if k == 2 and r > 0 then
        printf("sc %s", join({pc, r, 1, drad, math.rad(a), math.pi / 2, -1}))
      end
      pp[i + j * u + 1] = pc
    end
  end

    printf("sl %s", join({pp[1], pp[10], pp[11], pp[20]}))
    printf("sl %s", join({pp[4], pp[5], pp[6], pp[7]}))
    printf("sl %s", join({pp[14], pp[15], pp[16], pp[17]}))
    print("#")
end

hp = {}
for line in io.lines("myfiles") do
  F = {}
  string.gsub(line, "%S+", function (x) table.insert(F, x) end)
  if line:match("^hk") then hk = F[2]*1 end
  if line:match("^hp([1-9])") then
    hp[line:match("^hp([1-9])")*1] = {F[2]*1, F[3]*1}
  end
end
s = split(os.getenv('s'))
h = s[1] and s[1]*1 or 400
b = s[2] and s[2]*1 or 200
t1 = s[3] and s[3]*1 or 8
t2 = s[4] and s[4]*1 or 13
r = s[5] and s[5]*1 or 0
for i = 1, #hp do
  jish(hp[i], h, b, t1, t2, r, hk)
end