ツール管理へ戻る

v1 のソース

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>円形ワーク|切断シミュレーター</title>
<style>
:root {
  color-scheme: dark;
  --bg: #0a101c;
  --panel: #121d2e;
  --line: #293950;
  --text: #edf4ff;
  --muted: #9badc5;
  --cyan: #50dfef;
}
* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--text);
  background:
    radial-gradient(ellipse at top right, #163249 0, transparent 55%),
    var(--bg);
  line-height: 1.6;
}
main { max-width: 1220px; margin: auto; padding: 32px 18px 48px; }
.eyebrow {
  color: var(--cyan);
  letter-spacing: .18em;
  font-size: 12px;
  font-weight: 800;
}
h1 { font-size: clamp(25px, 4vw, 38px); margin: 6px 0; }
h2 { font-size: 18px; margin: 0 0 18px; }
p { margin: 8px 0; }
.sub { color: var(--muted); margin-bottom: 26px; }
.layout { display: grid; grid-template-columns: 330px 1fr; gap: 20px; }
.card {
  background: linear-gradient(145deg, #162337, #101a29);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 24px;
  box-shadow: 0 18px 45px #0003;
}
.fields { display: grid; gap: 15px; }
label { font-size: 14px; font-weight: 650; }
input, select, button { font: inherit; }
input, select {
  display: block;
  width: 100%;
  margin-top: 6px;
  background: #091321;
  color: var(--text);
  border: 1px solid #34465e;
  border-radius: 9px;
  padding: 11px 12px;
}
input:focus, select:focus, button:focus-visible {
  outline: 2px solid var(--cyan);
  outline-offset: 3px;
}
small, .note { color: var(--muted); font-size: 12px; }
small { display: block; margin-top: 5px; font-weight: 400; }
.fixed {
  border: 1px solid #285566;
  background: #0b303a;
  color: #a5f5ff;
  padding: 12px;
  border-radius: 10px;
  font-size: 14px;
}
button {
  border: none;
  border-radius: 10px;
  padding: 12px 16px;
  cursor: pointer;
  font-weight: 800;
}
.actions { display: flex; gap: 10px; margin-top: 22px; }
.primary {
  flex: 1;
  background: linear-gradient(120deg, #50dfef, #62a7ff);
  color: #071724;
}
.secondary { background: #26364d; color: var(--text); }
.metrics { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.metric {
  padding: 18px;
  border: 1px solid #30506a;
  border-radius: 13px;
  background: linear-gradient(130deg, #153449, #142439);
}
.metric span { font-size: 12px; color: var(--muted); }
.metric strong { display: block; font-size: clamp(23px, 3vw, 32px); }
.metric:first-child strong { color: var(--cyan); }
.figure { margin-top: 20px; }
canvas {
  display: block;
  width: 100%;
  height: 390px;
  background: #091321;
  border: 1px solid var(--line);
  border-radius: 14px;
}
.legend { display: flex; flex-wrap: wrap; gap: 16px; margin: 10px 0; }
.legend span { font-size: 12px; color: var(--muted); }
.dot { display: inline-block; width: 10px; height: 10px; margin-right: 6px; }
table { width: 100%; border-collapse: collapse; margin-top: 20px; font-size: 14px; }
th, td { padding: 11px 6px; border-bottom: 1px solid var(--line); text-align: left; }
th { color: var(--muted); font-size: 12px; }
td:last-child, th:last-child { text-align: right; }
.total { font-weight: 800; color: var(--cyan); }
.error {
  border: 1px solid #b15b65;
  background: #3c202b;
  padding: 14px;
  border-radius: 10px;
}
[hidden] { display: none !important; }
details { margin-top: 24px; }
summary { cursor: pointer; font-weight: 700; }
li { margin: 8px 0; }
@media(max-width: 850px) {
  .layout { grid-template-columns: 1fr; }
}
@media(max-width: 440px) {
  .card { padding: 17px; }
  .metrics { grid-template-columns: 1fr; }
  canvas { height: 320px; }
}
</style>
</head>
<body>
<main>
  <div class="eyebrow">CIRCULAR WORKPIECE / CUTTING SIMULATOR</div>
  <h1>円形ワークの切断シミュレーター</h1>
  <p class="sub">丸いワークのチップ配置と、戻り・移動を含む加工時間を可視化。</p>

  <div class="layout">
    <section class="card">
      <h2>加工条件</h2>
      <form id="form">
        <div class="fields">
          <label>ワーク直径(mm)
            <input id="diameter" type="number" min="0.001"
              step="any" value="100" required>
          </label>
          <label>外周の除外幅(mm)
            <input id="edge" type="number" min="0"
              step="any" value="0" required>
            <small>外周からこの幅を除いた円内に配置します。</small>
          </label>
          <label>チップ横寸法(mm)
            <input id="chipWidth" type="number" min="0.001"
              step="any" value="10" required>
          </label>
          <label>チップ縦寸法(mm)
            <input id="chipHeight" type="number" min="0.001"
              step="any" value="10" required>
          </label>
          <label>中心の合わせ方
            <select id="placement">
              <option value="auto">自動比較(4パターンで最多)</option>
              <option value="center">チップ中心をワーク中心に合わせる</option>
              <option value="cross">チップ境界の交点をワーク中心に合わせる</option>
            </select>
            <small>自動比較は、横・縦それぞれの半ピッチずれを比較します。</small>
          </label>
          <label>切断速度(mm/秒)
            <select id="speed"></select>
            <small>縦・横共通。初期値50は仮設定です。</small>
          </label>
          <div class="fixed">戻り速度 <strong>80 mm/秒 固定</strong></div>
          <label>同じ切断線を通す回数
            <input id="passes" type="number" min="1"
              step="1" value="1" required>
          </label>
          <label>次の切断位置への移動(秒/回)
            <input id="move" type="number" min="0"
              step="any" value="1" required>
          </label>
          <label>90度回転・位置合わせ(秒)
            <input id="rotate" type="number" min="0"
              step="any" value="30" required>
          </label>
        </div>
        <div class="actions">
          <button type="submit" class="primary">シミュレーション</button>
          <button type="button" id="reset" class="secondary">初期化</button>
        </div>
      </form>
    </section>

    <section class="card">
      <h2>シミュレーション結果</h2>
      <div id="error" class="error" role="alert" hidden></div>
      <div id="result" aria-live="polite">
        <div class="metrics">
          <div class="metric">
            <span>取得可能チップ数</span>
            <strong id="count">—</strong>
            <small id="yield"></small>
          </div>
          <div class="metric">
            <span>総加工時間</span>
            <strong id="duration">—</strong>
            <small id="seconds"></small>
          </div>
        </div>

        <div class="figure">
          <canvas id="canvas" role="img" aria-label="円形ワークのチップ配置図"></canvas>
          <div class="legend">
            <span><i class="dot" style="background:#50dfef"></i>取得チップ</span>
            <span><i class="dot" style="background:#ffad76"></i>切断線</span>
            <span><i class="dot" style="background:#65809e"></i>ワーク外周</span>
          </div>
          <p id="layoutName" class="note"></p>
          <p id="previewNote" class="note"></p>
        </div>

        <table>
          <thead><tr><th>配置・加工情報</th><th>結果</th></tr></thead>
          <tbody>
            <tr><td>配置可能領域の直径</td><td id="usable"></td></tr>
            <tr><td>チップ配置範囲(列 × 行)</td><td id="grid"></td></tr>
            <tr><td>縦/横の切断線数</td><td id="lines"></td></tr>
            <tr><td>総切断回数</td><td id="cuts"></td></tr>
            <tr><td>総切断距離</td><td id="distance"></td></tr>
            <tr><td>総戻り距離</td><td id="returnDistance"></td></tr>
            <tr><td>位置移動/回転回数</td><td id="moves"></td></tr>
          </tbody>
        </table>
        <table>
          <thead><tr><th>時間内訳</th><th>秒</th></tr></thead>
          <tbody>
            <tr><td>正味切断</td><td id="cutTime"></td></tr>
            <tr><td>戻り(最後の戻りを含む)</td><td id="returnTime"></td></tr>
            <tr><td>次の切断位置への移動</td><td id="moveTime"></td></tr>
            <tr><td>90度回転・位置合わせ</td><td id="rotateTime"></td></tr>
            <tr class="total"><td>総加工時間</td><td id="total"></td></tr>
          </tbody>
        </table>
      </div>

      <details open>
        <summary>計算条件・注意点</summary>
        <ul class="note">
          <li>対象は円形ワークからの矩形チップ切出しです。チップ自体は丸形ではありません。</li>
          <li>配置可能な円内に四隅すべてが収まるチップだけを数えます。外周の端数チップは含めません。</li>
          <li>自動比較は中心合わせ4パターンの比較です。任意の配置ずれまで探索する最適化ではありません。同数なら総切断距離が短い配置を選びます。</li>
          <li>取得チップの境界を通る縦・横の直線を切断します。重複する切断線は1本にまとめ、外周の不要部分も含む元の円の弦長を走行距離とします。</li>
          <li>縦方向を切断してから90度回転し、横方向を切断します。回転後もワークを保持し、元の円の弦長を走行できる前提です。</li>
          <li>切断は一方向のみ。毎回、同じ距離を80 mm/秒で戻ります。最後の切断後も戻り時間を加算します。</li>
          <li>同じ線の複数回切断では位置移動を加算しません。次の線への移動は方向ごとに「線数-1」回です。</li>
          <li>方向切替の位置合わせは回転時間に含め、通常の移動時間は重複加算しません。</li>
          <li>刃厚、切断溝、加減速、オーバーラン、刃の上下動、初期位置への移動、着脱時間は未計上です。実機条件によってチップ数・加工時間が変わります。</li>
          <li>外部通信・外部素材・自動保存・ログインは使用しません。入力はページを閉じると保持されません。</li>
        </ul>
      </details>
    </section>
  </div>
</main>

<script>
"use strict";

const $ = id => document.getElementById(id);
const canvas = $("canvas");
const ctx = canvas.getContext("2d");
const fmt = new Intl.NumberFormat("ja-JP", {
  maximumFractionDigits: 3
});
let state = null;

for (let n = 1; n <= 100; n++) {
  const option = document.createElement("option");
  option.value = String(n);
  option.textContent = String(n);
  $("speed").appendChild(option);
}
$("speed").value = "50";

function show(id, value) {
  $(id).textContent = value;
}
function f(value) {
  return fmt.format(value);
}
function read(id, label, min, integer = false) {
  const raw = $(id).value.trim();
  const value = Number(raw);
  if (!raw || !Number.isFinite(value) || value < min ||
      (integer && !Number.isSafeInteger(value))) {
    throw new Error(`${label}を正しい${integer ? "整数" : "数値"}で入力してください。`);
  }
  return value;
}
function timeLabel(seconds) {
  if (seconds < 60) return `${f(seconds)}秒`;
  const rounded = Math.round(seconds);
  const hours = Math.floor(rounded / 3600);
  const minutes = Math.floor(rounded % 3600 / 60);
  const rest = rounded % 60;
  return hours
    ? `${f(hours)}時間 ${minutes}分 ${rest}秒`
    : `${minutes}分 ${rest}秒`;
}
function chord(radius, position) {
  return 2 * Math.sqrt(Math.max(0, radius * radius - position * position));
}

function makeLayout(p, ox, oy) {
  const nx = Math.ceil(p.usableRadius / p.cw) + 1;
  const ny = Math.ceil(p.usableRadius / p.ch) + 1;
  const candidateCount = (2 * nx + 1) * (2 * ny + 1);

  // 極端に細かい入力によるブラウザの長時間占有を防ぎます。
  if (!Number.isFinite(candidateCount) || candidateCount > 300000) {
    throw new Error("配置が細かすぎます。チップ寸法を大きくして再計算してください。");
  }

  const cells = [];
  const xs = new Set();
  const ys = new Set();
  const columns = new Set();
  const rows = new Set();
  const tolerance = p.usableRadius * p.usableRadius * 1e-12;

  for (let i = -nx; i <= nx; i++) {
    const cx = (i + ox) * p.cw;
    const farX = Math.abs(cx) + p.cw / 2;
    if (farX > p.usableRadius) continue;

    for (let j = -ny; j <= ny; j++) {
      const cy = (j + oy) * p.ch;
      const farY = Math.abs(cy) + p.ch / 2;
      if (farX * farX + farY * farY >
          p.usableRadius * p.usableRadius + tolerance) continue;

      cells.push({ cx, cy });
      columns.add(i);
      rows.add(j);

      // 半ピッチ単位の整数キーで境界線の重複を排除します。
      xs.add(2 * i + 2 * ox - 1);
      xs.add(2 * i + 2 * ox + 1);
      ys.add(2 * j + 2 * oy - 1);
      ys.add(2 * j + 2 * oy + 1);
    }
  }

  const vertical = Array.from(xs, k => k * p.cw / 2)
    .sort((a, b) => a - b);
  const horizontal = Array.from(ys, k => k * p.ch / 2)
    .sort((a, b) => a - b);
  const distance =
    vertical.reduce((sum, x) => sum + chord(p.radius, x), 0) +
    horizontal.reduce((sum, y) => sum + chord(p.radius, y), 0);

  return {
    cells, vertical, horizontal, distance,
    columns: columns.size,
    rows: rows.size,
    name: `横:${ox === 0 ? "チップ中心" : "チップ境界"}合わせ / ` +
          `縦:${oy === 0 ? "チップ中心" : "チップ境界"}合わせ`
  };
}

function calculate() {
  try {
    const diameter = read("diameter", "ワーク直径", 0.001);
    const edge = read("edge", "外周の除外幅", 0);
    const cw = read("chipWidth", "チップ横寸法", 0.001);
    const ch = read("chipHeight", "チップ縦寸法", 0.001);
    const speed = read("speed", "切断速度", 1, true);
    const passes = read("passes", "切断回数", 1, true);
    const move = read("move", "移動時間", 0);
    const rotate = read("rotate", "回転時間", 0);
    const radius = diameter / 2;
    const usableRadius = radius - edge;

    if (speed > 100) throw new Error("切断速度は1~100で選択してください。");
    if (usableRadius <= 0) {
      throw new Error("外周の除外幅はワーク半径より小さくしてください。");
    }
    if (!Number.isFinite(radius * radius)) {
      throw new Error("ワーク寸法が大きすぎます。");
    }
    if (Math.hypot(cw, ch) > 2 * usableRadius * (1 + 1e-12)) {
      throw new Error("配置可能領域にチップが入りません。寸法を確認してください。");
    }

    const p = { diameter, radius, usableRadius, cw, ch };
    const mode = $("placement").value;
    const variants = mode === "center"
      ? [[0, 0]]
      : mode === "cross"
        ? [[0.5, 0.5]]
        : [[0, 0], [0.5, 0.5], [0, 0.5], [0.5, 0]];

    let best = null;
    for (const [ox, oy] of variants) {
      const candidate = makeLayout(p, ox, oy);
      if (!best ||
          candidate.cells.length > best.cells.length ||
          (candidate.cells.length === best.cells.length &&
           candidate.distance < best.distance)) {
        best = candidate;
      }
    }

    if (!best.cells.length) {
      throw new Error("指定の中心合わせではチップが入りません。自動比較をお試しください。");
    }

    const nv = best.vertical.length;
    const nh = best.horizontal.length;
    const cutCount = (nv + nh) * passes;
    const distance = best.distance * passes;
    const moves = Math.max(0, nv - 1) + Math.max(0, nh - 1);
    const rotations = nv > 0 && nh > 0 ? 1 : 0;
    const cutTime = distance / speed;
    const returnTime = distance / 80;
    const moveTime = moves * move;
    const rotateTime = rotations * rotate;
    const total = cutTime + returnTime + moveTime + rotateTime;

    if (!Number.isSafeInteger(cutCount) ||
        ![distance, total].every(Number.isFinite)) {
      throw new Error("計算値が大きすぎます。入力値を確認してください。");
    }

    const utilization = best.cells.length *
      (cw / radius) * (ch / radius) / Math.PI * 100;

    state = { ...p, ...best };
    show("count", `${f(best.cells.length)}個`);
    show("yield", `ワーク面積に対するチップ面積比 ${f(utilization)}%`);
    show("duration", timeLabel(total));
    show("seconds", `${f(total)}秒`);
    show("usable", `${f(usableRadius * 2)} mm`);
    show("grid", `${f(best.columns)}列 × ${f(best.rows)}行(外周は欠け配置)`);
    show("lines", `${f(nv)}本 / ${f(nh)}本`);
    show("cuts", `${f(cutCount)}回`);
    show("distance", `${f(distance)} mm`);
    show("returnDistance", `${f(distance)} mm`);
    show("moves", `${f(moves)}回 / ${rotations}回`);
    show("cutTime", f(cutTime));
    show("returnTime", f(returnTime));
    show("moveTime", f(moveTime));
    show("rotateTime", f(rotateTime));
    show("total", f(total));
    show("layoutName", best.name);

    $("error").hidden = true;
    $("result").hidden = false;
    draw();
  } catch (error) {
    state = null;
    show("error", error.message);
    $("error").hidden = false;
    $("result").hidden = true;
  }
}

function draw() {
  if (!state || !ctx) return;
  const s = state;
  const bounds = canvas.getBoundingClientRect();
  const width = Math.max(200, bounds.width);
  const height = bounds.height || 390;
  const ratio = window.devicePixelRatio || 1;

  canvas.width = Math.round(width * ratio);
  canvas.height = Math.round(height * ratio);
  ctx.setTransform(ratio, 0, 0, ratio, 0, 0);
  ctx.clearRect(0, 0, width, height);

  const centerX = width / 2;
  const centerY = height / 2;
  const screenRadius = Math.min(width, height) / 2 - 35;
  const scale = screenRadius / s.radius;
  const X = x => centerX + x * scale;
  const Y = y => centerY + y * scale;

  ctx.strokeStyle = "#16283c";
  ctx.lineWidth = 1;
  for (let x = 0; x < width; x += 24) {
    ctx.beginPath();
    ctx.moveTo(x, 0);
    ctx.lineTo(x, height);
    ctx.stroke();
  }
  for (let y = 0; y < height; y += 24) {
    ctx.beginPath();
    ctx.moveTo(0, y);
    ctx.lineTo(width, y);
    ctx.stroke();
  }

  ctx.beginPath();
  ctx.arc(centerX, centerY, screenRadius, 0, Math.PI * 2);
  ctx.fillStyle = "#182a40";
  ctx.fill();

  ctx.save();
  ctx.clip();

  const cellStride = Math.max(1, Math.ceil(s.cells.length / 12000));
  ctx.fillStyle = "#50dfef40";
  ctx.strokeStyle = "#50dfef9c";
  ctx.lineWidth = 0.6;
  for (let i = 0; i < s.cells.length; i += cellStride) {
    const cell = s.cells[i];
    const x = X(cell.cx - s.cw / 2);
    const y = Y(cell.cy - s.ch / 2);
    const w = s.cw * scale;
    const h = s.ch * scale;
    ctx.fillRect(x, y, w, h);
    ctx.strokeRect(x, y, w, h);
  }

  ctx.strokeStyle = "#ffad768f";
  ctx.lineWidth = 1;
  function drawLines(lines, vertical) {
    const stride = Math.max(1, Math.ceil(lines.length / 180));
    for (let i = 0; i < lines.length; i += stride) {
      const position = lines[i];
      const half = chord(s.radius, position) / 2;
      ctx.beginPath();
      if (vertical) {
        ctx.moveTo(X(position), Y(-half));
        ctx.lineTo(X(position), Y(half));
      } else {
        ctx.moveTo(X(-half), Y(position));
        ctx.lineTo(X(half), Y(position));
      }
      ctx.stroke();
    }
    return stride > 1;
  }
  const omittedV = drawLines(s.vertical, true);
  const omittedH = drawLines(s.horizontal, false);
  ctx.restore();

  if (s.usableRadius < s.radius) {
    ctx.beginPath();
    ctx.setLineDash([5, 5]);
    ctx.strokeStyle = "#8db4d7";
    ctx.arc(centerX, centerY, s.usableRadius * scale, 0, Math.PI * 2);
    ctx.stroke();
    ctx.setLineDash([]);
  }

  ctx.beginPath();
  ctx.arc(centerX, centerY, screenRadius, 0, Math.PI * 2);
  ctx.strokeStyle = "#8aacce";
  ctx.lineWidth = 2;
  ctx.stroke();

  ctx.strokeStyle = "#d7eaff";
  ctx.lineWidth = 1;
  ctx.beginPath();
  ctx.moveTo(centerX - 6, centerY);
  ctx.lineTo(centerX + 6, centerY);
  ctx.moveTo(centerX, centerY - 6);
  ctx.lineTo(centerX, centerY + 6);
  ctx.stroke();

  ctx.fillStyle = "#c3d9ef";
  ctx.font = "12px sans-serif";
  ctx.textAlign = "center";
  ctx.fillText(`直径 ${f(s.diameter)} mm`, centerX, height - 12);

  show("previewNote",
    cellStride > 1 || omittedV || omittedH
      ? "表示負荷を抑えるため図の一部を間引いています。計算は全チップ・全切断線を含みます。"
      : "円内に完全に収まるチップを表示。中心の十字はワーク中心です。"
  );
  canvas.setAttribute("aria-label",
    `直径${f(s.diameter)}mmの円形ワーク。` +
    `取得チップ${f(s.cells.length)}個。` +
    `縦切断線${f(s.vertical.length)}本、横切断線${f(s.horizontal.length)}本。`
  );
}

$("form").addEventListener("submit", event => {
  event.preventDefault();
  calculate();
});
$("reset").addEventListener("click", () => {
  $("form").reset();
  $("speed").value = "50";
  calculate();
});
window.addEventListener("resize", draw);
calculate();
</script>
</body>
</html>