microstation 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. data/.autotest +23 -0
  2. data/.gemtest +0 -0
  3. data/.rspec +2 -0
  4. data/Gemfile +17 -0
  5. data/History.txt +6 -0
  6. data/Manifest.txt +60 -0
  7. data/README.txt +75 -0
  8. data/Rakefile +30 -0
  9. data/bin/dgn2pdf +37 -0
  10. data/lib/microstation.rb +88 -0
  11. data/lib/microstation/app.rb +286 -0
  12. data/lib/microstation/attributes.rb +35 -0
  13. data/lib/microstation/cad_input_queue.rb +25 -0
  14. data/lib/microstation/configuration.rb +57 -0
  15. data/lib/microstation/dir.rb +252 -0
  16. data/lib/microstation/drawing.rb +189 -0
  17. data/lib/microstation/enumerator.rb +29 -0
  18. data/lib/microstation/ext/pathname.rb +25 -0
  19. data/lib/microstation/extensions/hash.rb +27 -0
  20. data/lib/microstation/pdf_support.rb +40 -0
  21. data/lib/microstation/properties.rb +57 -0
  22. data/lib/microstation/scan/color.rb +38 -0
  23. data/lib/microstation/scan/criteria.rb +85 -0
  24. data/lib/microstation/scan/klass.rb +43 -0
  25. data/lib/microstation/scan/level.rb +38 -0
  26. data/lib/microstation/scan/line_style.rb +45 -0
  27. data/lib/microstation/scan/line_weight.rb +33 -0
  28. data/lib/microstation/scan/subtype.rb +40 -0
  29. data/lib/microstation/scan/type.rb +109 -0
  30. data/lib/microstation/scanner.rb +24 -0
  31. data/lib/microstation/tag.rb +58 -0
  32. data/lib/microstation/tag_set.rb +280 -0
  33. data/lib/microstation/template.rb +84 -0
  34. data/lib/microstation/text.rb +54 -0
  35. data/lib/microstation/text_node.rb +74 -0
  36. data/lib/microstation/ts/attribute.rb +139 -0
  37. data/lib/microstation/ts/instance.rb +112 -0
  38. data/lib/microstation/types.rb +91 -0
  39. data/lib/microstation/wrap.rb +214 -0
  40. data/plot/pdf-bw.plt +164 -0
  41. data/plot/pdf.plt +163 -0
  42. data/plot/png.plt +383 -0
  43. data/plot/tiff.plt +384 -0
  44. data/plot/wmbw.tbl +66 -0
  45. data/plot/wmcolor.tbl +62 -0
  46. data/spec/app_spec.rb +267 -0
  47. data/spec/configuration_spec.rb +122 -0
  48. data/spec/drawing_spec.rb +247 -0
  49. data/spec/drawings/new_drawing.dgn +0 -0
  50. data/spec/drawings/test.dgn +0 -0
  51. data/spec/drawings/test1.dgn +0 -0
  52. data/spec/drawings/testfile.pdf +0 -0
  53. data/spec/enumerator_spec.rb +60 -0
  54. data/spec/microstation_spec.rb +36 -0
  55. data/spec/scanner_spec.rb +155 -0
  56. data/spec/spec_app.rb +11 -0
  57. data/spec/spec_helper.rb +31 -0
  58. data/spec/tag_set_spec.rb +123 -0
  59. data/spec/text_node_spec.rb +92 -0
  60. data/spec/text_spec.rb +62 -0
  61. metadata +241 -0
@@ -0,0 +1,84 @@
1
+ require 'erb'
2
+ require 'liquid'
3
+ require 'fileutils'
4
+ require File.join(File.dirname(__FILE__), 'extensions/hash')
5
+ module Microstation
6
+
7
+ class Template
8
+
9
+ def initialize(template,app = nil)
10
+ # @app = app || Microstation::App.new
11
+ @template = template
12
+ end
13
+
14
+ def close
15
+ @app.quit
16
+ end
17
+
18
+ def render(context,locals={}, output)
19
+ temp = Tempfile.new('working')
20
+ temp.close
21
+ __run__(context,locals,temp.path)
22
+ FileUtils.mv(temp,output)
23
+ puts "Printed drawing #{output}"
24
+ end
25
+
26
+ def add_binding(scope,locals={},&block)
27
+ tagsets = locals.delete(:tagsets)
28
+ nlocals = normalize_hash(locals)
29
+ nscope = normalize_hash(scope)
30
+ nlocals = locals.merge(nscope)
31
+ nlocals['yield'] = block.nil? ? '' : yield
32
+ nlocals['content'] = nlocals['yield']
33
+ [nlocals,tagsets]
34
+ end
35
+
36
+ def normalize_hash(scope)
37
+ scope = scope.to_h if scope.respond_to?(:to_h)
38
+ if scope.kind_of? Hash
39
+ scope = scope.map_k{|k| k.to_s}
40
+ end
41
+ scope
42
+ end
43
+
44
+ def update_tagset(drawing,name,values)
45
+ tagset = drawing.find_tagset(name)
46
+ tagset.update(values)
47
+ end
48
+
49
+ def add_binding_erb(object)
50
+ class << object
51
+ def get_binding
52
+ binding
53
+ end
54
+ end
55
+ end
56
+
57
+ def __run__(context,locals={},file)
58
+
59
+ Microstation.run do |app|
60
+ app.new_drawing(file,@template) do |drawing|
61
+ scope,tagsets = add_binding(context,locals)
62
+ tagsets.each do |tagset_name,values|
63
+ tagset = drawing.find_tagset(tagset_name.to_s).first
64
+ require 'pry'
65
+ binding.pry
66
+ tagset.update(values) if tagset
67
+ end
68
+ drawing.scan_text do |text|
69
+ # binding.pry if text =~ /usi_west|usi_east/
70
+ compiled = ::Liquid::Template.parse(text.to_s)
71
+ new_text = compiled.render(scope) rescue text #binding.pry
72
+ if new_text != text.to_s
73
+ text.replace(new_text)
74
+ end
75
+
76
+ end
77
+ end
78
+ #file
79
+ end
80
+ end
81
+
82
+ end
83
+
84
+ end
@@ -0,0 +1,54 @@
1
+ module Microstation
2
+
3
+ class Text < Element
4
+
5
+ def initialize(ole)
6
+ @ole_obj = ole
7
+ @original_text = @ole_obj.Text
8
+ end
9
+
10
+ # def microstation_id
11
+ # @ole_obj.Id || @ole_obj.ID64
12
+ # end
13
+ # def text?
14
+ # true
15
+ # end
16
+
17
+ # def text_node?
18
+ # false
19
+ # end
20
+
21
+ def to_s
22
+ @original_text.to_s
23
+ end
24
+
25
+ def method_missing2(meth,*args, &block)
26
+ if meth =~ /^[A-Z]/
27
+ @ole_obj.send(meth,*args)
28
+ else
29
+ dup = @original_text.dup
30
+ result = dup.send(meth,*args, &block)
31
+ _update(dup) unless dup == @original_text
32
+ result
33
+ end
34
+ end
35
+
36
+ def method_missing(meth,*args,&block)
37
+ dup = @original_text.dup
38
+ result = dup.send(meth,*args, &block)
39
+ _update(dup) unless dup == @original_text
40
+ result
41
+ end
42
+
43
+
44
+ def _update(text)
45
+ @ole_obj.Text = text
46
+ @original_text = text
47
+ @ole_obj.Redraw Microstation::MSD::MsdDrawingModeNormal
48
+ @ole_obj.Rewrite
49
+ @original_text = text
50
+ end
51
+
52
+ end
53
+
54
+ end
@@ -0,0 +1,74 @@
1
+ module Microstation
2
+
3
+ class TextNode < Element
4
+
5
+ attr_reader :original_text, :ole_obj
6
+
7
+ def initialize(ole)
8
+ @ole_obj = ole
9
+ @original_text = ole_to_ruby(ole)
10
+ end
11
+
12
+ def empty?
13
+ ole_obj.TextLinesCount == 0
14
+ end
15
+
16
+ def text?
17
+ false
18
+ end
19
+
20
+ def size
21
+ ole_obj.TextLinesCount
22
+ end
23
+
24
+
25
+ def ole_to_ruby(ole)
26
+ count = ole.TextLinesCount
27
+ # debugger if count > 0
28
+ str_array = []
29
+ 1.upto(count) do |i|
30
+ str_array << ole.TextLine(i)
31
+ end
32
+ str_array.join("\n")
33
+ end
34
+
35
+ def _update(text)
36
+ update_ole(text)
37
+ @original_text = text
38
+ end
39
+
40
+ def update_ole(text)
41
+ ole_obj.DeleteAllTextLines
42
+ text.each_line do |line|
43
+ ole_obj.AddTextLine(line)
44
+ end
45
+ ole_obj.Redraw Microstation::MSD::MsdDrawingModeNormal
46
+ ole_obj.Rewrite
47
+ end
48
+
49
+ def to_s
50
+ @original_text.to_s
51
+ end
52
+
53
+ def method_missing(meth,*args,&block)
54
+ dup = @original_text.dup
55
+ result = dup.send(meth,*args,&block)
56
+ _update(dup) unless dup == @original_text
57
+ result
58
+ end
59
+
60
+
61
+ def method_missing2(meth,*args,&block)
62
+ if meth.to_s =~ /^[A-Z]/
63
+ ole_obj.send(meth,*args)
64
+ else
65
+ dup = @original_text.dup
66
+ result = dup.send(meth,*args,&block)
67
+ _update(dup) unless dup == @original_text
68
+ result
69
+ end
70
+ end
71
+
72
+ end
73
+
74
+ end
@@ -0,0 +1,139 @@
1
+ module Microstation
2
+ module TS
3
+
4
+ class Attribute
5
+
6
+ attr_reader :ole_obj
7
+
8
+ # msdTagTypeCharacter 1 (&H1)
9
+ # msdTagTypeShortInteger 2 (&H2)
10
+ # msdTagTypeLongInteger 3 (&H3)
11
+ # msdTagTypeDouble 4 (&H4)
12
+ # msdTagTypeBinary 5 (&H5)
13
+
14
+ TYPES = {
15
+ 1 => String,
16
+ 2 => Integer,
17
+ 3 => Integer,
18
+ 4 => Float,
19
+ # 5 => Binary
20
+ }
21
+
22
+ RUBY_TO_MS = TYPES.invert
23
+
24
+ def self.tag_type(type)
25
+ if type.class == Symbol
26
+ ruby_type = case type
27
+ when :char
28
+ String
29
+ when :int
30
+ Integer
31
+ when :float
32
+ Float
33
+ else
34
+ :char
35
+ end
36
+ else
37
+ ruby_type = type
38
+ end
39
+
40
+ RUBY_TO_MS[ruby_type]
41
+ end
42
+
43
+ def att_type
44
+ TYPES[type]
45
+ end
46
+
47
+ def initialize(ole, options = {})
48
+ @ole_obj = ole
49
+ @definition = options[:definer]
50
+ end
51
+
52
+ def close
53
+ @ole_obj = nil
54
+ end
55
+
56
+ def tagset
57
+ @definition.tagset if definition
58
+ end
59
+
60
+ def name
61
+ @ole_obj.name
62
+ end
63
+
64
+ def name=(val)
65
+ @ole_obj.Name = val
66
+ end
67
+
68
+ def options_for_attribute
69
+ options = {}
70
+ options[:is_hidden] = true if hidden?
71
+ options[:prompt] = prompt if prompt
72
+ options[:default] = default_value
73
+ options[:readonly] = true if constant?
74
+ end
75
+
76
+ def to_s
77
+ "TagDefinition: #{name}"
78
+ end
79
+
80
+ def constant?
81
+ @ole_obj.IsConstant
82
+ end
83
+
84
+ def constant=(constant)
85
+ bool = constant ? true : false
86
+ @ole_obj.IsConstant = bool
87
+ end
88
+
89
+ def default
90
+ @ole_obj.DefaultValue
91
+ end
92
+
93
+ def default=(val)
94
+ @ole_obj.DefaultValue = val
95
+ end
96
+
97
+ def has_default?
98
+ !!default
99
+ end
100
+
101
+ def hidden?
102
+ @ole_obj.IsHidden
103
+ end
104
+
105
+ def hidden=(hidden)
106
+ bool = hidden ? true :false
107
+ @ole_obj.IsHidden = bool
108
+ end
109
+ def prompt
110
+ @ole_obj.Prompt
111
+ end
112
+
113
+ def prompt=(val)
114
+ @ole_obj.Prompt = val
115
+ end
116
+
117
+ def type
118
+ TYPES[@ole_obj.TagType]
119
+ end
120
+
121
+ def attrib_options
122
+ options = {}
123
+ options[:default] = default_value if has_default?
124
+ options[:readonly] = true if constant?
125
+ options
126
+ end
127
+
128
+ def tagset_name
129
+ @ole_obj.TagSetName
130
+ end
131
+
132
+ def ==(other)
133
+ @ole_obj.Name == other.ole_obj.Name && @ole_obj.TagSetName == other.ole_obj.TagSetName && @ole_obj.TagType == other.ole_obj.TagType
134
+ end
135
+
136
+ end
137
+
138
+ end
139
+ end
@@ -0,0 +1,112 @@
1
+ module Microstation
2
+ module TS
3
+
4
+ class Instance
5
+
6
+ include Enumerable
7
+
8
+ attr_reader :elements,:tagset
9
+
10
+ def initialize(ts, elements)
11
+ @tagset = ts
12
+ @elements = elements
13
+ end
14
+
15
+ def to_s
16
+ "TS:Instance #{tagset.name}"
17
+ end
18
+
19
+ def find_attribute(name)
20
+ @elements.find{|a| a.name == name.to_s}
21
+ end
22
+
23
+ def [](name)
24
+ find_attribute(name)
25
+ end
26
+
27
+ def attributes
28
+ @elements.map{|e| e.name}
29
+ end
30
+
31
+ def update_element(name,value)
32
+ find_attribute(name)._update(value)
33
+ end
34
+
35
+ def []=(name,value)
36
+ update_element(name,value)
37
+ end
38
+
39
+ def element_value(name)
40
+ find_attribute(name).value
41
+ end
42
+
43
+ def to_hash
44
+ result = {}
45
+ @elements.each do |ele|
46
+ result[ele.name] = ele.value unless (ele.value == "" || ele.value.nil?)
47
+ end
48
+ result
49
+ end
50
+
51
+ def pair(el)
52
+ [el.name, el.value]
53
+ end
54
+
55
+ def select
56
+ result = []
57
+ each do |el|
58
+ k,v = pair(el)
59
+ save = yield k,v
60
+ result << find_attribute(k) if save
61
+ end
62
+ self.class.new(tagset,result)
63
+ end
64
+
65
+ def find(&block)
66
+ select(&block).first
67
+ end
68
+
69
+ def each
70
+ @elements.each do |el|
71
+ yield el
72
+ end
73
+ end
74
+
75
+ def each_pair
76
+ @elements.each do |el|
77
+ yield el.name, el.value
78
+ end
79
+ end
80
+
81
+ def map_v
82
+ each_pair do |k,v|
83
+ new_v = yield v
84
+ update_element(k,new_v)
85
+ end
86
+ end
87
+
88
+ def update(value_hash)
89
+ value_hash = value_hash.map_kv{|k,v| [k.to_s,v.to_s] }
90
+ valid_atts = attributes & value_hash.keys
91
+ valid_atts.each do |att|
92
+ update_element(att,value_hash[att])
93
+ end
94
+ end
95
+
96
+ def method_missing(meth,*args,&block)
97
+ base = meth.to_s.sub("=", "")
98
+ if attributes.include?(base)
99
+ if meth.match /(=)/
100
+ update_element(base,*args)
101
+ else
102
+ element_value(base.to_s)
103
+ end
104
+ else
105
+ super(meth,*args,&block)
106
+ end
107
+ end
108
+
109
+ end
110
+
111
+ end
112
+ end