Class: Document

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

Constant Summary collapse

MODEL_TYPE =

Relationship schemas

'http://schemas.microsoft.com/3dmanufacturing/2013/01/3dmodel'
THUMBNAIL_TYPE =
'http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail'
TEXTURE_TYPE =
'http://schemas.microsoft.com/3dmanufacturing/2013/01/3dtexture'
'http://schemas.microsoft.com/3dmanufacturing/2013/01/printticket'
RELATIONSHIP_TYPES =

Relationship Type => Class validating relationship type

{
  MODEL_TYPE => {klass: 'Model3mf', collection: :models},
  THUMBNAIL_TYPE => {klass: 'Thumbnail3mf', collection: :thumbnails},
  TEXTURE_TYPE => {klass: 'Texture3mf', collection: :textures},
  PRINT_TICKET_TYPE => {}
}
TEXTURE_TYPES =
%w[image/jpeg image/png application/vnd.ms-package.3dmanufacturing-3dmodeltexture]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(zip_filename) ⇒ Document

Returns a new instance of Document.



28
29
30
31
32
33
34
35
36
37
# File 'lib/ruby3mf/document.rb', line 28

def initialize(zip_filename)
  self.models=[]
  self.thumbnails=[]
  self.textures=[]
  self.objects={}
  self.relationships={}
  self.types=[]
  self.parts=[]
  @zip_filename = zip_filename
end

Instance Attribute Details

#modelsObject

Returns the value of attribute models.



5
6
7
# File 'lib/ruby3mf/document.rb', line 5

def models
  @models
end

#objectsObject

Returns the value of attribute objects.



8
9
10
# File 'lib/ruby3mf/document.rb', line 8

def objects
  @objects
end

#partsObject

Returns the value of attribute parts.



9
10
11
# File 'lib/ruby3mf/document.rb', line 9

def parts
  @parts
end

#relationshipsObject

Returns the value of attribute relationships.



4
5
6
# File 'lib/ruby3mf/document.rb', line 4

def relationships
  @relationships
end

#texturesObject

Returns the value of attribute textures.



7
8
9
# File 'lib/ruby3mf/document.rb', line 7

def textures
  @textures
end

#thumbnailsObject

Returns the value of attribute thumbnails.



6
7
8
# File 'lib/ruby3mf/document.rb', line 6

def thumbnails
  @thumbnails
end

#typesObject

Returns the value of attribute types.



3
4
5
# File 'lib/ruby3mf/document.rb', line 3

def types
  @types
end

#zip_filenameObject

Returns the value of attribute zip_filename.



10
11
12
# File 'lib/ruby3mf/document.rb', line 10

def zip_filename
  @zip_filename
end

Class Method Details

.read(input_file) ⇒ Object



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
93
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/ruby3mf/document.rb', line 54

def self.read(input_file)

  m = new(input_file)
  begin
    Log3mf.context 'zip' do |l|
      begin
        Zip.warn_invalid_date = false

        # check for the general purpose flag set - if so, warn that 3mf may not work on some systems
        File.open(input_file, "r") do |file|
          if file.read[6] == "\b"
            l.warning 'File format: this file may not open on all systems'
          end
        end

        Zip::File.open(input_file) do |zip_file|

          l.info 'Zip file is valid'

          # check for valid, absolute URI's for each path name

          zip_file.each do |part|
            l.context "part names /#{part.name}" do |l|
              unless part.name.end_with? '[Content_Types].xml'
                begin
                  u = URI part.name
                rescue ArgumentError, URI::InvalidURIError
                  l.fatal_error "This NEVER Happens! mdw 12Jan2017"
                  l.error :err_uri_bad
                  next
                end

                u.path.split('/').each do |segment|
                  l.error :err_uri_hidden_file if (segment.start_with? '.') && !(segment.end_with? '.rels')
                end
                m.parts << '/' + part.name unless part.directory?
              end
            end
          end

          l.context 'content types' do |l|
            content_type_match = zip_file.glob('\[Content_Types\].xml').first
            if content_type_match
              m.types = ContentTypes.parse(content_type_match)
              model_extension = m.types.key('application/vnd.ms-package.3dmanufacturing-3dmodel+xml')
              model_file = zip_file.glob("**/*.#{model_extension}").first
              l.error :no_3d_model, extension: model_extension if model_file.nil?
            else
              l.error 'Missing required file: [Content_Types].xml', page: 4
            end
          end

          l.context 'relationships' do |l|
            rel_file = zip_file.glob('_rels/.rels').first
            l.fatal_error :missing_dot_rels_file unless rel_file

            zip_file.glob('**/*.rels').each do |rel|
              m.relationships[rel.name] = Relationships.parse(rel)
            end
          end

          l.context "print tickets" do |l|
            print_ticket_types = m.relationships.flat_map { |k, v| v }.select { |rel| rel[:type] == PRINT_TICKET_TYPE }
            l.error :multiple_print_tickets if print_ticket_types.size > 1
          end

          l.context "relationship elements" do |l|
            m.relationships.each do |file_name, rels|
              rels.each do |rel|
                l.context rel[:target] do |l|

                  begin
                    u = URI rel[:target]
                  rescue URI::InvalidURIError
                    l.error :err_uri_bad
                    next
                  end

                  l.error :err_uri_relative_path unless u.to_s.start_with? '/'

                  target = rel[:target].gsub(/^\//, "")
                  l.error :err_uri_empty_segment if target.end_with? '/' or target.include? '//'
                  l.error :err_uri_relative_path if target.include? '/../'
                  relationship_file = zip_file.glob(target).first

                  if relationship_file
                    relationship_type = RELATIONSHIP_TYPES[rel[:type]]
                    if relationship_type.nil?
                      l.error :invalid_relationship_type, type: rel[:type]
                    else
                      unless relationship_type[:klass].nil?
                        m.send(relationship_type[:collection]) << {
                          rel_id: rel[:id],
                          target: rel[:target],
                          object: Object.const_get(relationship_type[:klass]).parse(m, relationship_file)
                        }
                      end
                    end
                  else
                    l.error "Relationship Target file #{rel[:target]} not found", page: 11
                  end
                end
              end
            end
          end

          validate_texture_parts(m, l)
        end

        return m
      rescue Zip::Error
        l.fatal_error 'File provided is not a valid ZIP archive', page: 9
      end
    end
  rescue Log3mf::FatalError
    #puts "HALTING PROCESSING DUE TO FATAL ERROR"
    return nil
  end
end

.validate_texture_parts(document, log) ⇒ Object

verify that each texture part in the 3MF is related to the model through a texture relationship in a rels file



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ruby3mf/document.rb', line 40

def self.validate_texture_parts(document, log)
  unless document.types.empty?
    document.parts.select { |part| TEXTURE_TYPES.include?(document.types[File.extname(part).strip.downcase[1..-1]]) }.each do |tfile|
      if document.textures.select { |f| f[:target] == tfile }.size == 0
        if document.thumbnails.select { |t| t[:target] == tfile }.size == 0
          log.context "part names" do |l|
            l.warning "#{tfile} appears to be a texture file but no rels file declares any relationship to the model", page: 13
          end
        end
      end
    end
  end
end

Instance Method Details

#contents_for(path) ⇒ Object



200
201
202
203
204
# File 'lib/ruby3mf/document.rb', line 200

def contents_for(path)
  Zip::File.open(zip_filename) do |zip_file|
    zip_file.glob(path).first.get_input_stream.read
  end
end

#write(output_file = nil) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/ruby3mf/document.rb', line 174

def write(output_file = nil)
  output_file = zip_filename if output_file.nil?

  Zip::File.open(zip_filename) do |input_zip_file|

    buffer = Zip::OutputStream.write_buffer do |out|
      input_zip_file.entries.each do |e|
        if e.directory?
          out.copy_raw_entry(e)
        else
          out.put_next_entry(e.name)
          if objects[e.name]
            out.write objects[e.name]
          else
            out.write e.get_input_stream.read
          end
        end
      end
    end

    File.open(output_file, 'wb') { |f| f.write(buffer.string) }

  end

end