外変「circlehatch」(網掛けハッチング)をアップします。
2本の境界線の間に小円(ドット)を配置する外変です。
台形や三角形の境界内にも配置できます。
(Geminiで作成)
境界線1をクリック
境界線2をクリック
[選択確定]で実行
興味のある方はどうぞ
【動作しない場合】
コピーしたコードを Gemini や ChatGPT などのAIに貼り付け、次の指示を出してください。直る場合があります。
AIへの指示:
「この Jw_cad 外部変形のスクリプトを Ruby として正しく動作するよう修復してください。HTMLエンティティ(' など)や全角スペースはすべて半角に修正し、文法エラーがない状態にしてください。ロジック(計算内容)は変更しないでください。」
+++++++++++++++++++++++++++++++++++++++++++
circlehatch.bat
@echo off
setlocal
goto EXE
REM circle hatch
REM #jww
REM #cd
REM #g0
REM #gn
REM #1ln 1st line
REM #2ln 2nd line
REM #hr
REM #e
:EXE
pushd "%~dp0" >nul
set "SCRIPT=%~dp0circlehatch.rb"
if exist "C:\Ruby33-x64\bin\ruby.exe" (
"C:\Ruby33-x64\bin\ruby.exe" -Ks "%SCRIPT%"
) else if exist "C:\Ruby32-x64\bin\ruby.exe" (
"C:\Ruby32-x64\bin\ruby.exe" -Ks "%SCRIPT%"
) else if exist "C:\Ruby31-x64\bin\ruby.exe" (
"C:\Ruby31-x64\bin\ruby.exe" -Ks "%SCRIPT%"
) else if exist "C:\Ruby30-x64\bin\ruby.exe" (
"C:\Ruby30-x64\bin\ruby.exe" -Ks "%SCRIPT%"
) else (
ruby -Ks "%SCRIPT%"
)
popd >nul
endlocal
exit /b 0
++++++++++++++++++++++++++++++++++++++++++++++
circlehatch.rb
# encoding: Windows-31J
# circlehatch.rb
Dir.chdir(File.dirname(__FILE__))
input_file = "jwc_temp.txt"
# --- 設定値 ---
RADIUS_MM = 0.25
INTERVAL_MM = 2.5
# --------------
scale = 1.0
selected_lines = []
if File.exist?(input_file)
src = File.read(input_file, encoding: "Windows-31J")
hs_vals = []
group = 0
src.each_line do |line|
l = line.strip
if l =~ /^hs\s+(.+)/
hs_vals = $1.split.map(&:to_f)
elsif l =~ /^lg([0-9a-f])/
group = $1.hex
scale = hs_vals[group] if hs_vals && hs_vals[group]
elsif l =~ /^[ \t]*(-?\d+\.?\d*)\s+(-?\d+\.?\d*)\s+(-?\d+\.?\d*)\s+(-?\d+\.?\d*)/
selected_lines << [$1.to_f, $2.to_f, $3.to_f, $4.to_f]
end
end
end
exit if selected_lines.size < 2
l1, l2 = selected_lines[-2], selected_lines[-1]
p1, p2 = [l1[0], l1[1]], [l1[2], l1[3]]
p3, p4 = [l2[0], l2[1]], [l2[2], l2[3]]
dist_p1_p3 = (p1[0]-p3[0])**2 + (p1[1]-p3[1])**2
dist_p1_p4 = (p1[0]-p4[0])**2 + (p1[1]-p4[1])**2
if dist_p1_p3 < dist_p1_p4
poly = [p1, p2, p4, p3]
else
poly = [p1, p2, p3, p4]
end
def inside?(px, py, poly)
ins = false; j = poly.size - 1
poly.size.times do |i|
if ((poly[i][1] > py) != (poly[j][1] > py)) &&
(px < (poly[j][0] - poly[i][0]) * (py - poly[i][1]) / (poly[j][1] - poly[i][1]) + poly[i][0])
ins = !ins
end
j = i
end
ins
end
half, step = RADIUS_MM * scale, INTERVAL_MM * scale
rad1, rad2 = 45.0 * Math::PI / 180.0, -45.0 * Math::PI / 180.0
u = [Math.cos(rad1) * step, Math.sin(rad1) * step]
v = [Math.cos(rad2) * step, Math.sin(rad2) * step]
xs, ys = poly.map{|p|p[0]}, poly.map{|p|p[1]}
xmin, xmax, ymin, ymax = xs.min, xs.max, ys.min, ys.max
cx_mid, cy_mid = (xmin + xmax) / 2.0, (ymin + ymax) / 2.0
num = (Math.sqrt((xmax-xmin)**2 + (ymax-ymin)**2) / step).ceil + 5
File.open(input_file, "w") do |file|
file.write "h#circle hatch\n"
(-num..num).each do |i|
(-num..num).each do |j|
px, py = cx_mid + i * u[0] + j * v[0], cy_mid + i * u[1] + j * v[1]
# 判定を4点に絞って高速化
pts = [[px, py - half], [px, py + half], [px - half, py], [px + half, py]]
if pts.all? { |pt| inside?(pt[0], pt[1], poly) }
file.write "ci %.6f %.6f %.6f\n" % [px, py, half]
end
end
end
file.write "hr\n"
end