# 生成示例 2D 尺寸图 PNG 与数据表 JPEG (中/英)
# 用法: powershell -NoProfile -ExecutionPolicy Bypass -File tools\gen-images.ps1
# 依赖: 仅 Windows 自带 System.Drawing (PowerShell 5.1)
Add-Type -AssemblyName System.Drawing

$docsDir = Join-Path (Join-Path $PSScriptRoot '..\onb-sc') 'docs'
$dimDir  = Join-Path $docsDir 'dim-drawing'
$dsDir   = Join-Path $docsDir 'datasheet'
New-Item -ItemType Directory -Force -Path $dimDir | Out-Null
New-Item -ItemType Directory -Force -Path $dsDir | Out-Null

$fontTitle = New-Object System.Drawing.Font('Microsoft YaHei', 26, [System.Drawing.FontStyle]::Bold)
$fontH2    = New-Object System.Drawing.Font('Microsoft YaHei', 17, [System.Drawing.FontStyle]::Bold)
$fontText  = New-Object System.Drawing.Font('Microsoft YaHei', 13)
$fontSmall = New-Object System.Drawing.Font('Microsoft YaHei', 11)
$penThin   = New-Object System.Drawing.Pen([System.Drawing.Color]::Black, 1)
$penLine   = New-Object System.Drawing.Pen([System.Drawing.Color]::Black, 2)
$penDash   = New-Object System.Drawing.Pen([System.Drawing.Color]::Black, 1)
$penDash.DashStyle = [System.Drawing.Drawing2D.DashStyle]::Dash
$brush     = [System.Drawing.Brushes]::Black
$fillGray  = New-Object System.Drawing.SolidBrush([System.Drawing.Color]::FromArgb(235, 235, 235))

function New-Canvas([int]$w, [int]$h) {
  $bmp = New-Object System.Drawing.Bitmap($w, $h)
  $g = [System.Drawing.Graphics]::FromImage($bmp)
  $g.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::AntiAlias
  $g.TextRenderingHint = [System.Drawing.Text.TextRenderingHint]::AntiAlias
  $g.Clear([System.Drawing.Color]::White)
  return ,@($bmp, $g)
}

# 尺寸线箭头 (dx,dy 为箭头指向)
function Draw-Arrow($g, [float]$x, [float]$y, [float]$dx, [float]$dy, [float]$size) {
  $px = -$dy; $py = $dx
  $p1 = New-Object System.Drawing.PointF(($x + $dx * $size), ($y + $dy * $size))
  $p2 = New-Object System.Drawing.PointF(($x + $px * $size * 0.45), ($y + $py * $size * 0.45))
  $p3 = New-Object System.Drawing.PointF(($x - $px * $size * 0.45), ($y - $py * $size * 0.45))
  $g.FillPolygon($brush, [System.Drawing.PointF[]]@($p1, $p2, $p3))
}

# 表格: rows 为 string[] 数组, cols 为每列宽度
function Draw-Table($g, [float]$x, [float]$y, $rows, [float[]]$cols, [float]$rowH) {
  $top = $y
  foreach ($r in $rows) {
    $xx = $x
    for ($i = 0; $i -lt $cols.Count; $i++) {
      $g.DrawRectangle($penThin, $xx, $top, $cols[$i], $rowH)
      $g.DrawString($r[$i], $fontText, $brush, ($xx + 8), ($top + 10))
      $xx += $cols[$i]
    }
    $top += $rowH
  }
}

# ---- 2D 尺寸图 (中英双语标注) ----
function Draw-DimDrawing([string]$outPath, [int]$bore, [int]$stroke, [string]$magnet, [string]$mount) {
  $c = New-Canvas 1000 1500
  $bmp = $c[0]; $g = $c[1]
  $s = 3.0                       # px/mm
  $cx = 500.0
  $yTop = 320.0                  # 前盖顶边
  $capW  = ($bore + 10) * $s
  $bodyW = ($bore + 4) * $s
  $rodD  = [Math]::Max($bore / 5.0, 3.0) * 2
  $rodW  = $rodD * $s
  $yFrontCap = $yTop + 10 * $s
  $yBody     = $yFrontCap + ($stroke + 60) * $s
  $yRearCap  = $yBody + 8 * $s
  $yRodTop   = $yTop - ($stroke + 10) * $s
  $yRodBot   = $yTop + 14 * $s
  $model = "SC$bore`x$stroke$magnet-$mount"
  $total = 2 * $stroke + 88

  $g.DrawString('欧霓博 OUNIBO · SC 系列标准气缸 尺寸图', $fontTitle, $brush, 80, 40)
  $g.DrawString('SC Series Standard Cylinder · Dimension Drawing', $fontText, $brush, 82, 92)
  $g.DrawString("型号 Model: $model", $fontH2, $brush, 82, 128)

  # 外形 (前盖/缸体/后盖/活塞杆)
  $g.FillRectangle($fillGray, ($cx - $capW / 2), $yTop, $capW, (10 * $s))
  $g.DrawRectangle($penLine, ($cx - $capW / 2), $yTop, $capW, (10 * $s))
  $g.FillRectangle($fillGray, ($cx - $bodyW / 2), $yFrontCap, $bodyW, (($stroke + 60) * $s))
  $g.DrawRectangle($penLine, ($cx - $bodyW / 2), $yFrontCap, $bodyW, (($stroke + 60) * $s))
  $g.FillRectangle($fillGray, ($cx - $capW / 2), $yBody, $capW, (8 * $s))
  $g.DrawRectangle($penLine, ($cx - $capW / 2), $yBody, $capW, (8 * $s))
  $g.FillRectangle($fillGray, ($cx - $rodW / 2), $yRodTop, $rodW, ($yRodBot - $yRodTop))
  $g.DrawRectangle($penLine, ($cx - $rodW / 2), $yRodTop, $rodW, ($yRodBot - $yRodTop))

  # 中心线
  $g.DrawLine($penDash, $cx, ($yRodTop - 20), $cx, ($yRearCap + 30))

  # 缸径尺寸线 (跨前盖)
  $dimY = $yFrontCap + 40
  $g.DrawLine($penDash, ($cx - $capW / 2), ($yTop + 4), ($cx - $capW / 2), $dimY)
  $g.DrawLine($penDash, ($cx + $capW / 2), ($yTop + 4), ($cx + $capW / 2), $dimY)
  $g.DrawLine($penThin, ($cx - $capW / 2), $dimY, ($cx + $capW / 2), $dimY)
  Draw-Arrow $g ($cx - $capW / 2) $dimY -1 0 9
  Draw-Arrow $g ($cx + $capW / 2) $dimY 1 0 9
  $g.DrawString("Ø$bore mm (缸径 Bore)", $fontH2, $brush, ($cx - 70), ($dimY + 10))

  # 行程尺寸线 (活塞杆伸出段, 右侧)
  $dimX = $cx + $capW / 2 + 80
  $g.DrawLine($penDash, ($cx + $capW / 2 + 4), $yTop, $dimX, $yTop)
  $g.DrawLine($penDash, ($cx + $rodW / 2 + 4), $yRodTop, $dimX, $yRodTop)
  $g.DrawLine($penThin, $dimX, $yRodTop, $dimX, $yTop)
  Draw-Arrow $g $dimX $yRodTop 0 1 9
  Draw-Arrow $g $dimX $yTop 0 -1 9
  $g.DrawString("$stroke mm (行程 Stroke)", $fontH2, $brush, ($dimX + 12), (($yTop + $yRodTop) / 2 - 8))

  # 总长尺寸线 (左侧)
  $dimX2 = $cx - $capW / 2 - 80
  $g.DrawLine($penDash, ($cx - $capW / 2 - 4), $yRodTop, $dimX2, $yRodTop)
  $g.DrawLine($penDash, ($cx - $capW / 2 - 4), $yRearCap, $dimX2, $yRearCap)
  $g.DrawLine($penThin, $dimX2, $yRodTop, $dimX2, $yRearCap)
  Draw-Arrow $g $dimX2 $yRodTop 0 1 9
  Draw-Arrow $g $dimX2 $yRearCap 0 -1 9
  $g.DrawString("总长 Total: $total mm", $fontH2, $brush, ($dimX2 - 180), (($yRodTop + $yRearCap) / 2 - 8))

  # 磁石标注
  if ($magnet -eq 'S') {
    $g.DrawString('磁石 S / Magnet', $fontH2, $brush, ($cx + $bodyW / 2 + 20), ($yFrontCap + 60))
    $g.DrawLine($penThin, ($cx + $bodyW / 2 + 6), ($yFrontCap + 66), ($cx + $bodyW / 2 + 18), ($yFrontCap + 66))
  }

  # 信息表
  $mountDesc = 'FA=前法兰 Front flange / LB=脚座 Foot / CB=中摆 Clevis'
  $tblRows = @()
  $tblRows += ,@('项目 Item', '值 Value')
  $tblRows += ,@('型号 Model', $model)
  $tblRows += ,@('缸径 Bore', "Ø$bore mm")
  $tblRows += ,@('行程 Stroke', "$stroke mm")
  $tblRows += ,@('磁石 Magnet', $(if ($magnet -eq 'S') { 'S 带磁石' } else { '无 None' }))
  $tblRows += ,@('安装 Mount', $mountDesc)
  Draw-Table $g 150 1120 $tblRows @(200.0, 500.0) 40

  $g.DrawString('示意图 · 自编示例数据，仅用于目录软件开发 / Sample drawing for development only', $fontSmall, $brush, 150, 1420)

  $bmp.Save($outPath, [System.Drawing.Imaging.ImageFormat]::Png)
  $g.Dispose(); $bmp.Dispose()
  Write-Output ("尺寸图: " + $outPath)
}

# ---- 数据表 JPEG (zh/en) ----
function Draw-Datasheet([string]$outPath, [string]$lang) {
  $c = New-Canvas 1190 1684
  $bmp = $c[0]; $g = $c[1]
  $zh = ($lang -eq 'zh')

  if ($zh) {
    $g.DrawString('欧霓博 OUNIBO 气动元件', $fontTitle, $brush, 70, 50)
    $g.DrawString('SC 系列标准气缸 · 数据表 Datasheet', $fontH2, $brush, 70, 100)
    $g.DrawString('型号编码 Order Code:  SC{Bore}x{Stroke}{Magnet}-{Mount}', $fontH2, $brush, 70, 150)
    $encRows = @()
    $encRows += ,@('段 Segment', '含义 Meaning', '取值 Values')
    $encRows += ,@('SC', '系列 Series', '固定 Fixed')
    $encRows += ,@('{Bore}', '缸径 Bore (mm)', '16 / 25 / 32 / 40')
    $encRows += ,@('{Stroke}', '行程 Stroke (mm)', '25 / 50 / 75 / 100')
    $encRows += ,@('{Magnet}', '磁石 Magnet', '空=无 None / S=带磁石')
    $encRows += ,@('{Mount}', '安装 Mount', 'FA=前法兰 / LB=脚座 / CB=中摆')
    Draw-Table $g 70 200 $encRows @(150.0, 240.0, 360.0) 44

    $g.DrawString('规格 Specifications', $fontH2, $brush, 70, 500)
    $specRows = @()
    $specRows += ,@('参数 Parameter', '取值 Values')
    $specRows += ,@('缸径 Bore (mm)', '16 / 25 / 32 / 40')
    $specRows += ,@('行程 Stroke (mm)', '25 / 50 / 75 / 100')
    $specRows += ,@('磁石 Magnet', '无 None / S')
    $specRows += ,@('安装 Mount', 'FA / LB / CB')
    Draw-Table $g 70 550 $specRows @(260.0, 400.0) 44

    $g.DrawString('选型约束 Selection Rules', $fontH2, $brush, 70, 780)
    $g.DrawString('R1  缸径 16 时，行程仅可选 25 / 50', $fontText, $brush, 90, 830)
    $g.DrawString('R2  磁石 S 仅缸径 >= 25 可选', $fontText, $brush, 90, 870)
    $g.DrawString('R3  中摆 CB 仅行程 <= 75 可选', $fontText, $brush, 90, 910)

    $g.DrawString('参考尺寸 Reference Dimensions', $fontH2, $brush, 70, 980)
    $dimRows = @()
    $dimRows += ,@('型号 Model', '缸径 Bore', '行程 Stroke', '总长 Overall (mm)', '缸体长 Body (mm)')
    $dimRows += ,@('SC16x25-FA', '16', '25', '138', '85')
    $dimRows += ,@('SC25x50S-FA', '25', '50', '188', '110')
    $dimRows += ,@('SC32x50S-LB', '32', '50', '188', '110')
    $dimRows += ,@('SC40x100S-LB', '40', '100', '288', '160')
    Draw-Table $g 70 1030 $dimRows @(210.0, 120.0, 130.0, 200.0, 190.0) 44

    $g.DrawString('注 Note: 本数据表为自编示例数据（虚构系列），仅用于目录软件开发测试，非真实产品数据。', $fontSmall, $brush, 70, 1290)
    $g.DrawString('生成工具: sample-data/tools/gen-images.ps1', $fontSmall, $brush, 70, 1320)
  } else {
    $g.DrawString('OUNIBO Pneumatic Components', $fontTitle, $brush, 70, 50)
    $g.DrawString('SC Series Standard Cylinder · Datasheet', $fontH2, $brush, 70, 100)
    $g.DrawString('Order Code:  SC{Bore}x{Stroke}{Magnet}-{Mount}', $fontH2, $brush, 70, 150)
    $encRows = @()
    $encRows += ,@('Segment', 'Meaning', 'Values')
    $encRows += ,@('SC', 'Series', 'Fixed')
    $encRows += ,@('{Bore}', 'Bore (mm)', '16 / 25 / 32 / 40')
    $encRows += ,@('{Stroke}', 'Stroke (mm)', '25 / 50 / 75 / 100')
    $encRows += ,@('{Magnet}', 'Magnet', 'blank=None / S=Magnet')
    $encRows += ,@('{Mount}', 'Mounting', 'FA=Front flange / LB=Foot / CB=Clevis')
    Draw-Table $g 70 200 $encRows @(150.0, 240.0, 360.0) 44

    $g.DrawString('Specifications', $fontH2, $brush, 70, 500)
    $specRows = @()
    $specRows += ,@('Parameter', 'Values')
    $specRows += ,@('Bore (mm)', '16 / 25 / 32 / 40')
    $specRows += ,@('Stroke (mm)', '25 / 50 / 75 / 100')
    $specRows += ,@('Magnet', 'None / S')
    $specRows += ,@('Mounting', 'FA / LB / CB')
    Draw-Table $g 70 550 $specRows @(260.0, 400.0) 44

    $g.DrawString('Selection Rules', $fontH2, $brush, 70, 780)
    $g.DrawString('R1  Bore 16: stroke limited to 25 / 50', $fontText, $brush, 90, 830)
    $g.DrawString('R2  Magnet S requires bore >= 25', $fontText, $brush, 90, 870)
    $g.DrawString('R3  Clevis CB requires stroke <= 75', $fontText, $brush, 90, 910)

    $g.DrawString('Reference Dimensions', $fontH2, $brush, 70, 980)
    $dimRows = @()
    $dimRows += ,@('Model', 'Bore', 'Stroke', 'Overall (mm)', 'Body (mm)')
    $dimRows += ,@('SC16x25-FA', '16', '25', '138', '85')
    $dimRows += ,@('SC25x50S-FA', '25', '50', '188', '110')
    $dimRows += ,@('SC32x50S-LB', '32', '50', '188', '110')
    $dimRows += ,@('SC40x100S-LB', '40', '100', '288', '160')
    Draw-Table $g 70 1030 $dimRows @(210.0, 120.0, 130.0, 200.0, 190.0) 44

    $g.DrawString('Note: This datasheet contains sample data (fictional series) for catalog software development only, not real product data.', $fontSmall, $brush, 70, 1290)
    $g.DrawString('Generated by: sample-data/tools/gen-images.ps1', $fontSmall, $brush, 70, 1320)
  }

  $bmp.Save($outPath, [System.Drawing.Imaging.ImageFormat]::Jpeg)
  $g.Dispose(); $bmp.Dispose()
  Write-Output ("数据表: " + $outPath)
}

# ---- 生成 ----
Draw-DimDrawing (Join-Path $dimDir 'SC32x50S-LB.png') 32 50 'S' 'LB'
Draw-DimDrawing (Join-Path $dimDir 'SC16x25-FA.png')   16 25 ''  'FA'
Draw-Datasheet  (Join-Path $dsDir 'SC_datasheet_zh.jpg') 'zh'
Draw-Datasheet  (Join-Path $dsDir 'SC_datasheet_en.jpg') 'en'
