Class: StlExtract

Inherits:
Object
  • Object
show all
Defined in:
lib/STLExtract.rb

Instance Method Summary collapse

Constructor Details

#initialize(file = nil) ⇒ StlExtract

Returns a new instance of StlExtract.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/STLExtract.rb', line 94

def initialize(file=nil)
	#Set our initial values to nil and false for error 
	@x_value = nil
	@y_value = nil
	@z_value = nil
	@volume = nil
	@repair = nil
	@error = false
	
	#Find Slic3r location
	@slic3r_path = File.expand_path('../../', __FILE__)
	if OS.windows?
		@slic3r_path = @slic3r_path + '/Slic3r/Win/slic3r-console.exe'
	elsif OS.mac?
		@slic3r_path = @slic3r_path + '/Slic3r/Mac/Slic3r.app/Contents/MacOS/slic3r'
	elsif OS.linux?
		@slic3r_path = @slic3r_path + '/Slic3r/Linux/bin/slic3r'
	else
		#RuntimeError
		raise "Could not detect platform from: "+RUBY_PLATFORM
	end		
	
	#Unix systems (Mac and Linux) require that file be executable
	if OS.unix?
		if File.chmod(0744,@slic3r_path) == 0
			#RuntimeError
			raise "Could not make Slic3r executable"
		end
	end	
	
	if file!=nil
		get_info(file)
	end
end

Instance Method Details

#errorObject



154
155
156
157
# File 'lib/STLExtract.rb', line 154

def error()
	#Return stored volume
	@error
end

#get_info(file) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/STLExtract.rb', line 32

def get_info(file)
	#Set @file to use later in repair
	@file = file

	#Set our initial values to nil and false for error
	@x_value = nil
	@y_value = nil
	@z_value = nil
	@volume = nil
	@repair = nil
	@error = false
	
	#Open slic3r with filename supplied
	io = IO.popen(@slic3r_path+" --info \""+file+"\" 2>&1")
	
	io.each do |s|
		#If output contains size, extract size information using regex
		if s.include? "size"
			sizeMatch = s.match( /x=(\d+\.\d+)[^y]+y=(\d+\.\d+)[^z]+z=(\d+\.\d+)/ )
			if sizeMatch!=nil 
				@x_value = sizeMatch.values_at( 1 )[0]
				@y_value = sizeMatch.values_at( 2 )[0]
				@z_value = sizeMatch.values_at( 3 )[0]
			end
		end
		#If output contains repair stats
		if s.include? "needed repair"
			sizeMatch = s.match( /needed repair:[^y]+(yes|no)/ )
			if sizeMatch!=nil 
				if sizeMatch.values_at( 1 )[0]=="yes"
					@repair = true
				else
					@repair = false
				end
			end
		end
		#If output contains volume, extract volume information using regex
		if s.include? "volume"
			volMatch = s.match( /volume\:[^\d]+(\d+\.\d+)/ )
			if volMatch!=nil 
				@volume = volMatch.values_at( 1 )[0]
			end			  
		end
		
		#Check Slic3r output for errors
		if s.include? "Input file must have .stl, .obj or .amf(.xml) extension"
			@error = "Filetype not STL, OBJ or AMF "+file
		end
		if s.include? "Failed to open"
			@error = "Could not find: "+file
		end 
		if s.include? "The file"
			@error = "Corrupted file: "+file
		end
	end
	
	#Check that all required data is extracted
	if ((@x_value==nil || @y_value==nil || @z_value==nil || @volume==nil || @repair==nil ) && @error == false)
		@error = "Could not extract all data"
	end	
end

#repairObject



149
150
151
152
# File 'lib/STLExtract.rb', line 149

def repair()
	#Return stored volume
	@repair
end

#repair_file(file = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/STLExtract.rb', line 5

def repair_file(file=nil)
	@error = false
	
	if file==nil
		#Check for file to repair in local @file
		if @file!=nil
			#Use file found in @file to repair
			file = @file
		else
			#No file provided, give error
			@error = "No file provided to repair"
		end
	else
		#Use file provided to function in repair
	end
	
	#If no errors, repair file
	if @error == false
		io = IO.popen(@slic3r_path+" --repair \""+file+"\" 2>&1")
		io.each do |s|
			if s.include? "No such file"
				@error = "Incorrect filename"
			end
		end
	end
end

#volumeObject



144
145
146
147
# File 'lib/STLExtract.rb', line 144

def volume()
	#Return stored volume
	@volume
end

#xObject



129
130
131
132
# File 'lib/STLExtract.rb', line 129

def x()
	#Return stored x value
	@x_value
end

#yObject



134
135
136
137
# File 'lib/STLExtract.rb', line 134

def y()
	#Return stored y value
	@y_value
end

#zObject



139
140
141
142
# File 'lib/STLExtract.rb', line 139

def z()
	#Return stored z value
	@z_value
end