Class: Document

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

Constant Summary collapse

RELATIONSHIP_TYPES =

Relationship Type => Class validating relationship type

{
  'http://schemas.microsoft.com/3dmanufacturing/2013/01/3dmodel' => {klass: 'Model3mf', collection: :models},
  'http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail' => {klass: 'Thumbnail3mf', collection: :thumbnails},
  'http://schemas.microsoft.com/3dmanufacturing/2013/01/3dtexture' => {klass: 'Texture3mf', collection: :textures}
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(zip_filename) ⇒ Document

Returns a new instance of Document.



15
16
17
18
19
20
21
# File 'lib/ruby3mf/document.rb', line 15

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

Instance Attribute Details

#modelsObject

Returns the value of attribute models.



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

def models
  @models
end

#objectsObject

Returns the value of attribute objects.



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

def objects
  @objects
end

#texturesObject

Returns the value of attribute textures.



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

def textures
  @textures
end

#thumbnailsObject

Returns the value of attribute thumbnails.



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

def thumbnails
  @thumbnails
end

Class Method Details

.read(input_file) ⇒ Object



23
24
25
26
27
28
29
30
31
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
93
94
95
96
97
98
# File 'lib/ruby3mf/document.rb', line 23

def self.read(input_file)
  m=self.new(input_file)
  m.zip_filename = input_file    begin
    Log3mf.context "examining zip" do |l|
      begin
        Zip.warn_invalid_date = false
        Zip::File.open(input_file) do |zip_file|

          # puts "Zip contents:"
          # zip_file.each do |entry|
          #   puts entry
          # end

          l.info "Zip file is valid"

          l.context "content types" do |l|
            # 1. Get Content Types
            content_type_match = zip_file.glob('\[Content_Types\].xml').first
            if content_type_match
              @types = ContentTypes.parse(content_type_match)
            else
              l.error "Missing required file: [Content_Types].xml", page: 4
            end
          end

          l.context "relationships" do |l|
            # 2. Get Relationships
            # rel_folders = zip_file.glob('**/_rels')
            # l.fatal_error "Missing any _rels folder", page: 4 unless rel_folders.size>0

            # 2.1 Validate that the top level _rels/.rel file exists
            rel_file = zip_file.glob('_rels/.rels').first
            l.fatal_error "Missing required file _rels/.rels", page: 4 unless rel_file

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

          l.context "relationship elements" do |l|
            # 3. Validate all relationships
            @relationships.each do |rel|
              l.context rel[:target] do |l|
                target = rel[:target].gsub(/^\//, "")
                relationship_file = zip_file.glob(target).first

                if relationship_file
                  relationship_type = RELATIONSHIP_TYPES[rel[:type]]
                  if relationship_type.nil?
                    l.warning "Relationship file defines a type that is not used in a normal 3mf file: #{rel[:type]}. Ignoring relationship."
                  else
                    m.send(relationship_type[:collection]) << {
                      rel_id: rel[:id],
                      target: rel[:target],
                      object: Object.const_get(relationship_type[:klass]).parse(m, relationship_file, @relationships)
                    }
                  end
                else
                  l.error "Relationship Target file #{rel[:target]} not found", page: 11
                end
              end
            end
          end
        end
        return m
      rescue Zip::Error
        l.fatal_error 'File provided is not a valid ZIP archive', page: 9
        return nil
      end
    end
  rescue Log3mf::FatalError
    #puts "HALTING PROCESSING DUE TO FATAL ERROR"
    return nil
  end
end

Instance Method Details

#write(output_file = nil) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/ruby3mf/document.rb', line 100

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

  input_zip_file = Zip::File.open(@zip_filename)

  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

  input_zip_file.close

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