microstation 0.8.3 → 0.8.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../spec_helper'
4
+
5
+ describe 'points by rectangle' do
6
+ include Minitest::Hooks
7
+
8
+ before(:all) do
9
+ @app = Microstation::App.new(visible: true)
10
+ config_app(@app)
11
+ end
12
+
13
+ after(:all) do
14
+ delete_current_drawing
15
+ @app.quit rescue nil
16
+ end
17
+
18
+ let(:app) { @app }
19
+
20
+ it 'should be a function on app' do
21
+ skip
22
+ require 'pry'; binding.pry
23
+ drawing = app.new_drawing('temp.dgn')
24
+ pts = app.get_points_by_rectangle
25
+ puts pts.value
26
+ end
27
+
28
+ describe '#points_by_line' do
29
+ it 'should not error' do
30
+ skip
31
+ drawing = app.new_drawing('temp.dgn')
32
+ require 'pry'; binding.pry
33
+ pts = app.get_points_by_line
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,159 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../spec_helper'
4
+
5
+ describe 'Lets start the Microstation once' do
6
+ include Minitest::Hooks
7
+ before(:all) do
8
+ @app = Microstation::App.new
9
+ config_app(@app)
10
+ end
11
+
12
+ after(:all) do
13
+ @app.quit rescue nil
14
+ end
15
+
16
+ describe 'a new drawing' do
17
+ let(:drawing) { new_drawing('temp.dgn', seedfile: 'seed2d') }
18
+
19
+ describe 'creating a new tag set' do
20
+ let(:tagsets) { drawing.tagsets }
21
+
22
+ it 'should have no initial tagsets' do
23
+ _(tagsets.size).must_equal 0
24
+ end
25
+
26
+ it 'should create a tagset' do
27
+ ts = tagsets.create('test')
28
+ _(ts).must_be_instance_of Microstation::TagSet
29
+ end
30
+
31
+ it 'should be the same as a found tagset' do
32
+ ts = tagsets.create('test')
33
+ _(tagsets['test']).must_equal ts
34
+ end
35
+
36
+ it 'should allow you to remove a tagset' do
37
+ tagsets.create('test')
38
+ _(tagsets.size).must_equal 1
39
+ tagsets.remove('test')
40
+ _(tagsets.size).must_equal 0
41
+ end
42
+ end
43
+
44
+ describe 'A TagSet' do
45
+ let(:drawing) { new_drawing('temp.dgn', seedfile: 'seed2d') }
46
+
47
+ describe '#create_tagset' do
48
+ it 'should allow you to create' do
49
+ ts = drawing.create_tagset('faatitle')
50
+ _(ts).must_be_instance_of Microstation::TagSet
51
+ end
52
+
53
+ it 'should yield to block if block_given' do
54
+ drawing.create_tagset('faatitle') do |ts|
55
+ _(ts).must_be_instance_of Microstation::TagSet
56
+ end
57
+ end
58
+
59
+ describe 'given a TagSet' do
60
+ let(:tagset) { drawing.create_tagset('faatitle') }
61
+
62
+ it 'should initially have no attributes' do
63
+ _(tagset.attributes).must_be_empty
64
+ end
65
+
66
+ it '#add_attribute adds TS::Attribute' do
67
+ _(tagset.attributes).must_be_empty
68
+ td = tagset.add_attribute('title', String, prompt: 'My title')
69
+ _(td.prompt).must_equal('My title')
70
+ _(tagset.attributes.size).must_equal 1
71
+ _(td).must_be_instance_of Microstation::TS::Attribute
72
+ end
73
+
74
+ it 'should exist and can be found in drawing' do
75
+ tagset_local = tagset
76
+ ts = drawing.find_tagset('faatitle')
77
+ _(ts.name).must_equal tagset_local.name
78
+ end
79
+ end
80
+
81
+ describe 'given a tagset with tag defintions' do
82
+ let(:tagset) { drawing.create_tagset('faatitle') }
83
+
84
+ before do
85
+ tagset.add_attribute('title', String)
86
+ tagset.add_attribute('city', String)
87
+ end
88
+
89
+ describe '#attributes' do
90
+ it 'should have the correct size' do
91
+ _(tagset.attributes.size).must_equal 2
92
+ end
93
+
94
+ it '#attributes should be Array of TS::Attributes' do
95
+ tagset.attributes.each do |ta|
96
+ _(ta).must_be_instance_of Microstation::TS::Attribute
97
+ end
98
+ end
99
+ end
100
+
101
+ describe '#attribute_names' do
102
+ it 'returns an array of the correct names' do
103
+ _(tagset.attribute_names).must_equal %w[title city]
104
+ end
105
+ end
106
+
107
+ describe '#[]' do
108
+ it 'should allow you to retrieve a tagset definition that exists' do
109
+ _(tagset['title']).must_be_instance_of Microstation::TS::Attribute
110
+ end
111
+
112
+ it "returns nil if attribute doesn't exist" do
113
+ _(tagset['bogus']).must_be_nil
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end
119
+ end
120
+
121
+ describe 'a drawing with tagsets placed in drawing' do
122
+ let(:drawing) { open_existing_drawing('drawing_with_block.dgn') }
123
+ let(:tagsets) { drawing.tagsets }
124
+ let(:tset) { tagsets.first }
125
+
126
+ it 'has tagsets' do
127
+ _(tagsets).wont_be_empty
128
+ end
129
+
130
+ it 'has the correct tagsets' do
131
+ _(tagsets.size).must_equal 1
132
+ _(tset.name).must_equal 'electrical_panel_42'
133
+ end
134
+
135
+ it 'has tagset instances' do
136
+ _(tset.instances.size).must_equal 1
137
+ tagset_instance = tset.instances.first
138
+ _(tagset_instance.name).must_equal 'electrical_panel_42'
139
+ _(tagset_instance).must_be_instance_of Microstation::TS::Instance
140
+ end
141
+ end
142
+
143
+ describe 'a drawing with 3 instances of same tagset in drawing' do
144
+ let(:drawing) { open_existing_drawing('drawing_with_3_block.dgn') }
145
+ let(:tagsets) { drawing.tagsets }
146
+ let(:tset) { tagsets.first }
147
+
148
+ it 'has the correct number of tagsets' do
149
+ _(tagsets.size).must_equal 1
150
+ _(tagsets.names).must_equal %w[electrical_panel_42]
151
+ _(tset.name).must_equal 'electrical_panel_42'
152
+ end
153
+
154
+ it 'has the correct instances' do
155
+ _(tset.instances.size).must_equal 3
156
+ _(tset.instances.map(&:name)).must_equal %w[electrical_panel_42 electrical_panel_42 electrical_panel_42]
157
+ end
158
+ end
159
+ end
@@ -0,0 +1,159 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../spec_helper'
4
+ require 'fileutils'
5
+ require 'digest/md5'
6
+
7
+ require 'minitest/spec'
8
+ require 'minitest/autorun'
9
+
10
+ require 'microstation/template'
11
+
12
+ module TestHelper
13
+ def digest_for_path(file)
14
+ Digest::MD5.hexdigest(File.read(file))
15
+ end
16
+
17
+ def file_same?(p, p2)
18
+ digest_path(p) == digest_path(p2)
19
+ end
20
+ end
21
+
22
+ describe Microstation::Template do
23
+ include TestHelper
24
+
25
+ describe 'initialization' do
26
+ it 'needs a drawing argument' do
27
+ assert_raises { Microstation::Template.new }
28
+ end
29
+
30
+ it 'requires a valid and existing microstation file' do
31
+ %w[draw.dgn file://adoc.doc file://anexcel.xls file://autocad.dwg].each do |ftype|
32
+ assert_raises { Microstation::Template.new(ftype) }
33
+ end
34
+ end
35
+
36
+ describe 'given a good dgn file and file exists' do
37
+ let(:file) { 'file.dgn' }
38
+
39
+ it 'does not error' do
40
+ mock = Minitest::Mock.new
41
+ mock.expect(:file?, true)
42
+ File.stub :file?, true do
43
+ Microstation::Template.new(file)
44
+ end
45
+ end
46
+
47
+ it 'sets the template' do
48
+ mock = Minitest::Mock.new
49
+ mock.expect(:file?, true)
50
+ File.stub :file?, true do
51
+ temp = Microstation::Template.new(file)
52
+ _(temp.template).must_equal(file)
53
+ end
54
+ end
55
+ end
56
+ end
57
+
58
+ describe 'a drawing with tags' do
59
+ before do
60
+ @dgn_name = 'drawing_with_block.dgn'
61
+ @dgn_file = drawing_path('drawing_with_block.dgn')
62
+ @template = Microstation::Template.new(@dgn_file)
63
+ end
64
+
65
+ subject { @template }
66
+
67
+ describe '.render' do
68
+ let(:dgn_name) { @dgn_name }
69
+ let(:template) { @template }
70
+ let(:dgn_file) { @dgn_file }
71
+ let(:copied_file) { drawing_path('copied_file.dgn') }
72
+ let(:output_file) { output_path(dgn_name) }
73
+ let(:changed_name) { 'changed_name.dgn' }
74
+ let(:locals) do
75
+ { 'a1' => 'change 1', 'a2' => 'change 2',
76
+ 'a3' => 'c3', 'a4' => 'c4' }
77
+ end
78
+
79
+ before do
80
+ FileUtils.rm(copied_file) if File.exist? copied_file
81
+ FileUtils.cp(dgn_file, copied_file)
82
+ FileUtils.rm(output_file) if File.exist? output_file
83
+ FileUtils.rm(output_path(changed_name)) if File.exist?(output_path(changed_name))
84
+ end
85
+
86
+ after do
87
+ FileUtils.rm(output_file) if File.exist? output_file
88
+ FileUtils.rm(output_path(changed_name)) if File.exist?(output_path(changed_name))
89
+ end
90
+
91
+ it "it doesn't error" do
92
+ template.render(output_dir: OUTPUT_DIR)
93
+ end
94
+
95
+ describe 'when locals or tagsets not input' do
96
+ it 'does nothing unless either locals or tagsets' do
97
+ template.render(output_dir: OUTPUT_DIR)
98
+ _(File.file?(output_file)).must_equal false
99
+ end
100
+ end
101
+
102
+ describe 'when locals or tagset is set' do
103
+ it 'creates a file' do
104
+ template.render(output_dir: OUTPUT_DIR, locals: locals)
105
+ _(File.file?(output_file)).must_equal true
106
+ end
107
+
108
+ it 'changes text' do
109
+ template.render(output_dir: OUTPUT_DIR,
110
+ locals: { 'a1' => 'change 1', 'a2' => 'change 2', 'a3' => 'c3', 'a4' => 'c4' })
111
+ text = Microstation.get_text(output_file)
112
+ ['text1 change 1', 'text2 change 2', 'text3 c3', "node1 c4\nnode1 ", "node2 \nnode2 ",
113
+ 'text a1 again change 1'].each do |t|
114
+ _(text).must_include(t)
115
+ end
116
+ end
117
+
118
+ it "doesn't change input file" do
119
+ template.render(output_dir: OUTPUT_DIR, locals: locals)
120
+ _(FileUtils.compare_file(dgn_file, copied_file)).must_equal true
121
+ end
122
+
123
+ it 'changes the output' do
124
+ template.render(output_dir: OUTPUT_DIR, locals: locals)
125
+ _(FileUtils.compare_file(output_file, copied_file)).must_equal false
126
+ end
127
+ end
128
+
129
+ describe 'a drawing with multiple blocks of the same block' do
130
+ # before :all do
131
+ # @dgn_file = drawing_path('drawing_with_block.dgn')
132
+ # @template = Microstation::Template.new(@dgn_file)
133
+ # end
134
+
135
+ let(:filename) { 'drawing_with_3_block.dgn' }
136
+ let(:drawing_file) { drawing_path(filename) }
137
+ let(:template) { Microstation::Template.new(drawing_file) }
138
+ let(:output_file) { output_path(filename) }
139
+
140
+ before do
141
+ FileUtils.rm(output_file) if File.exist? output_file
142
+ end
143
+
144
+ it 'updates the drawing_blocks' do
145
+ panel = { 'brk_1_service' => 'OUTLETS',
146
+ 'brk_2_service' => 'AIR CONDITIONER',
147
+ 'microstation_id' => 324 }
148
+ template.render(output_dir: OUTPUT_DIR, tagsets: [{ 'electrical_panel_42' => panel }])
149
+ Microstation.run do |app|
150
+ drawing = app.open_drawing(output_file)
151
+ ts = drawing.find_tagset_instance_by_name_and_id('electrical_panel_42', 324)
152
+ _(ts.brk_2_service).must_equal('AIR CONDITIONER')
153
+ end
154
+ end
155
+ end
156
+
157
+ end
158
+ end
159
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../spec_helper'
4
+
5
+ describe 'Microstation::TextNode' do
6
+ include Minitest::Hooks
7
+ before(:all) do
8
+ @app = Microstation::App.new
9
+ config_app(@app)
10
+ end
11
+
12
+ after(:all) do
13
+ @app.quit rescue nil
14
+ end
15
+
16
+ describe 'given a TextNode' do
17
+ let(:app) { @app }
18
+ let(:scanner) { app.create_scanner(:text, &:include_text_nodes) }
19
+ let(:drawing) { open_existing_drawing('drawing_with_text.dgn') }
20
+ let(:text_node) { drawing.scan_model(scanner).find { |t| !t.empty? } }
21
+
22
+ it 'should forward Capitalized methods to @ole_obj' do
23
+ ole = Minitest::Mock.new
24
+ ole.expect(:Type, 'A ole type')
25
+ text_node.stub(:ole_obj, ole) do
26
+ _(text_node.Type).must_equal 'A ole type'
27
+ end
28
+ ole.verify
29
+ end
30
+
31
+ describe 'ruby string methods' do
32
+ describe "calling with methods that don't change the string" do
33
+ it 'should not call the update method' do
34
+ skip
35
+ ole = Minitest::Mock.new
36
+ mock.expect(:update, true)
37
+ text_node.stub(:upcase, true)
38
+
39
+ ole = Minitest::Mock.new
40
+ text_node.reverse
41
+ text_node.downcase
42
+ end
43
+
44
+ it 'should give the correct value' do
45
+ value = text_node.to_s
46
+ _(text_node.reverse.to_s).must_equal(value.reverse)
47
+ end
48
+
49
+ it 'should retain the same value' do
50
+ value = text_node.to_s
51
+ result = text_node.reverse
52
+ _(text_node.to_s).must_equal(value)
53
+ _(text_node.to_s).wont_equal result
54
+ end
55
+ end
56
+
57
+ describe 'calling with methods that change the string' do
58
+ it 'should change the value' do
59
+ value = text_node.to_s
60
+ result = text_node.reverse!
61
+ _(text_node.to_s).must_equal(result)
62
+ _(text_node.to_s).wont_equal(value)
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../spec_helper'
4
+
5
+ describe 'Microstation:Text and Microstation::TextNode' do
6
+ include Minitest::Hooks
7
+ before(:all) do
8
+ @app = Microstation::App.new
9
+ config_app(@app)
10
+ end
11
+ after(:all) do
12
+ @app.quit rescue nil
13
+ end
14
+
15
+ describe 'given a text node initialized by a scan' do
16
+ let(:drawing) { open_existing_drawing('drawing_with_text.dgn') }
17
+ let(:criteria) { @app.create_scanner(&:include_textual) }
18
+
19
+ before do
20
+ text_array = drawing.scan_model(criteria).to_a
21
+ @text = text_array.find { |t| t.instance_of?(Microstation::Text) }
22
+ end
23
+
24
+ let(:text) { @text }
25
+
26
+ it 'should be an instance of text' do
27
+ _(text).must_be_instance_of Microstation::Text
28
+ end
29
+
30
+ it 'should forward all the Microstation commands to ole_obj' do
31
+ _(text.IsTextElement).must_equal true
32
+ end
33
+
34
+ it 'should update the drawing if the text is changed' do
35
+ id = text.microstation_id
36
+ val = text.to_s
37
+ text.reverse!
38
+ drawing_text = drawing.find_by_id(id)
39
+ _(drawing_text.to_s).must_equal(val.reverse)
40
+ end
41
+ end
42
+ end