Skip to content

Instantly share code, notes, and snippets.

@miura1729
Created March 7, 2012 08:40
Show Gist options
  • Save miura1729/1991993 to your computer and use it in GitHub Desktop.
Save miura1729/1991993 to your computer and use it in GitHub Desktop.
JW CAD版
# -*- coding: cp932 -*-
# Graphics Layer for JWCAD
require 'util'
require 'singleton'
module JWCAD
class JwCadDevice
JwcTempDir = "g:/KD/program/jww"
JwcZahyoDir = "c:/tmp"
JwcTempName = "foo.txt"
include Singleton
include MessageUtil
def initialize
@output_shapes = []
@output_lines = []
@current_dir = JwcTempDir
@current_fname = JwcTempName
@input_lines = nil
read_input
@scale = Geometry::Scale # デフォルトでは1/200の縮尺
set_page
end
def device
self
end
def set_page(scale = @scale)
@output_shapes.push JwShScale.new(scale)
end
def set_scale(scale)
@scale = scale
@output_shapes.push JwShScale.new(scale)
end
def select_layer(lname)
ca = { "Pipe" => 1,
"PipeLabel" => 2,
"Label" => 3}[lname]
if ca == nil then
ca = 0
end
sh = JwShSelectLayer.new(ca)
@output_shapes.push sh
end
def draw_back(x1, y1, width, height)
end
def draw_line(x1, y1, x2, y2)
sh = JwShLine.new(x1, y1, x2, y2)
@output_shapes.push sh
sh
end
def draw_oval(x1, y1, x2, y2)
x = (x1 + x2) / 2
y = (y1 + y2) / 2
r = (x1 - x2).abs
sh = JwShOval.new(x, y, r)
@output_shapes.push sh
sh
end
def set_line_attribute(sh, width, style, color)
sh.set_line_attribute(width, style, color)
end
def set_rect_attribute(rect, color)
end
def draw_text(x1, y1, text, angle, fontsize)
rad = (angle * Math::PI) / 180.0
dx = Math.cos(rad)
dy = Math.sin(rad)
sh = JwShText.new(x1, y1, dx, dy, text, fontsize)
@output_shapes.push sh
sh
end
def draw_rectangle(x1, y1, width, height)
JwShCommon.new
end
def draw_bezier(xy, degree, flag)
sh = JwShBezier.new(*xy)
@output_shapes.push sh
sh
end
def draw_triangle(x1, y1, x2, y2, x3, y3)
sh = JwShTriangle.new(x1, y1, x2, y2, x3, y3)
@output_shapes.push sh
sh
end
def draw_polyline(xy)
end
def draw_label(orgx, orgy, labx, laby, mess, width, height, wang)
swidth = width
sheight = height
tlabx = labx + swidth / 2
tlaby = laby
if wang != 0 then
tlabx = tlabx - swidth
end
lin = draw_line(orgx, orgy, labx, laby)
set_line_attribute(lin, 0.5, 1, 0)
sh = draw_text(tlabx, tlaby, mess, 0, 8 * FONT_SIZE)
end
def finish
@output_lines += flush
File.open(JwcTempDir + "/" + JwcTempName, "w") do |fp|
@output_lines.each do |lin|
fp.puts lin
end
end
end
def save_as(fn)
end
def quit
end
def flush
lines = []
@output_shapes.each do |obj|
obj.flush(lines)
end
@output_shapes = []
lines
end
def select_page(fname, scale = 200)
@output_lines += flush
@current_dir = JwcZahyoDir
@current_fname = fname + ".txt"
set_page(scale)
end
def close
lines = flush
File.open(@current_dir + "/" + @current_fname, "w") do |fp|
lines.each do |lin|
fp.puts lin
end
end
end
def read_input
@input_lines = File.readlines(JwcTempDir + "/" + JwcTempName)
end
end
class JwShCommon
# R + G * 256 + B * 65536
CTAB = {
0 => 0, # Black
1 => 65536, # Blue
2 => 1, # Red
3 => 65536 + 1, # Magenta
4 => 256, # Green
5 => 65536 + 256, # Cyan
6 => 256 + 1, # Yellow
7 => 65536 + 256 + 1 # White
}
def initialize
@width = nil
@pattern = nil
@color = nil
end
def set_line_attribute(width, style, color)
@width = width / 72 * 2540
@pattern = style
@color = color
end
def flush(buf)
buf.push("lw #{@width}") if @width
buf.push("lt #{@pattern}") if @pattern
buf.push("lc10 #{CTAB[@color]}") if @color
end
def to_metor(inch)
inch * 2.54 / 2.0
# inch
end
end
class JwShScale<JwShCommon
def initialize(scale)
super()
@scale = scale
end
def flush(buf)
super(buf)
buf.push "by #{@scale.to_i / 10}"
end
end
class JwShText<JwShCommon
def initialize(x1, y1, dx, dy, text, fontsize)
super()
@dx = to_metor(dx)
@dy = to_metor(dy)
t = text.split(/\n/)
@x1 = to_metor(x1) #- t[0].bytesize * fontsize / 2 * dx - fontsize * dy
@y1 = to_metor(y1) #- fontsize * dx- t[0].bytesize * fontsize / 2 * dy
@text = t
@maxsize = t.map {|txt| txt.bytesize}.max
@scale = 2
@fontsize = fontsize / @scale
end
def flush(buf)
super(buf)
siz = @fontsize / 3
buf.push "cn0 #{siz} #{siz} 0 0"
buf.push "cc 4"
sz = Math.sqrt(@dx * @dx + @dy * @dy)
dx = @dx / sz * 10
dy = @dy / sz * 10
x = @x1
y = @y1 - dx * siz * (@text.size / 2) * @scale
@text.each do |lt|
x0 = (x - (@maxsize - lt.bytesize) * @fontsize * 0.706 * @scale).to_i
buf.push "ch #{x0} #{y} #{@dx} #{@dy} \"#{lt}"
x = x + dy * siz * @scale
y = y + dx * siz * @scale
end
end
end
class JwShLine<JwShCommon
def initialize(x1, y1, x2, y2)
super()
@x1 = to_metor(x1)
@y1 = to_metor(y1)
@x2 = to_metor(x2)
@y2 = to_metor(y2)
end
def flush(buf)
super(buf)
buf.push "#{@x1} #{@y1} #{@x2} #{@y2}"
end
end
class JwShBezier<JwShCommon
def initialize(x0, y0, x1, y1, x2, y2, x3, y3)
super()
@x0 = to_metor(x0)
@y0 = to_metor(y0)
@x1 = to_metor(x1)
@y1 = to_metor(y1)
@x2 = to_metor(x2)
@y2 = to_metor(y2)
@x3 = to_metor(x3)
@y3 = to_metor(y3)
end
DIVNUM = 10
def flush(buf)
super(buf)
buf.push ""
(0...DIVNUM).each do |i|
s = i / DIVNUM.to_f
si = 1.0 - s
x = si*si*si*@x0 + 3*s*si*si*@x1 + 3*s*s*si*@x2 + s*s*s*@x3
y = si*si*si*@y0 + 3*s*si*si*@y1 + 3*s*s*si*@y2 + s*s*s*@y3
buf.push "#{x} #{y}"
end
end
end
class JwShOval<JwShCommon
def initialize(x, y, r)
super()
@x = to_metor(x)
@y = to_metor(y)
@r = r / 2
end
def flush(buf)
super(buf)
buf.push "ci #{@x} #{@y} #{@r}"
end
end
class JwShTriangle<JwShCommon
def initialize(x1, y1, x2, y2, x3, y3)
super()
@x1 = to_metor(x1)
@y1 = to_metor(y1)
@x2 = to_metor(x2)
@y2 = to_metor(y2)
@x3 = to_metor(x3)
@y3 = to_metor(y3)
end
def flush(buf)
super(buf)
buf.push "sl #{@x1} #{@y1} #{@x2} #{@y2} #{@x3} #{@y3}"
end
end
class JwShSelectLayer<JwShCommon
def initialize(layer)
super()
@layer = layer
end
def flush(buf)
super(buf)
buf.push "ly#{@layer}"
end
end
end # Module jwcad
if __FILE__ == $0 then
device = JWCAD::JwCadDevice.instance
sh = device.draw_line(0, 0, 100, 100)
device.set_line_attribute(sh, 0.25, 1, 1)
device.draw_oval(0, 0, 100, 100)
p device.close
end
@supreetmankad
Copy link

Hey, I was wondering how to access the API or is there any feature of JWCAD for file conversion from .JWW to DXF in the background without using the GUI options. The file conversion is called upon by a third party application like Windchill for preview of the part drawing.

@miura1729
Copy link
Author

I don't know such a API, sorry.
My program use the JW-CAD feature "外部変形” (external transformation) which is like a batch file.

@supreetmankad
Copy link

Ok. Thankyou for the reply.
I basically need to open a file which is in .jww format and convert it to either PDF or .DXF. Can i achieve this through the batch file script? im just trying to get the file converted for visualisation. Any help would be massively appreciated. Thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment