ハッチ範囲の不具合を修正しました。他も何かあるかも知れません。
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:\Ruby34-x64\bin\ruby.exe" (
"C:\Ruby34-x64\bin\ruby.exe" -Ks "%SCRIPT%"
) else 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
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]]
v1 = [p2[0] - p1[0], p2[1] - p1[1]]
v2 = [p4[0] - p3[0], p4[1] - p3[1]]
dot_product = v1[0] * v2[0] + v1[1] * v2[1]
p3, p4 = p4, p3 if dot_product < 0
poly = [p1, p2, p4, p3]
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]
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