Skip to content

Commit

Permalink
Move COLLADA fix to dedicated module
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelTallet committed Apr 28, 2024
1 parent 075ac82 commit 0ad5f96
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 39 deletions.
24 changes: 23 additions & 1 deletion source/universal_importer/collada.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ module COLLADA
# Because SketchUp imports DAE files with generic materials names, e.g. `<auto>1`
#
# @param [Hash] materials_name Materials names indexed by texture path or color.
#
# @raise [ArgumentError]
def self.fix_materials_names(materials_names)

Expand All @@ -48,6 +47,29 @@ def self.fix_materials_names(materials_names)

end

# Fixes the double sided faces in a DAE file to import in SketchUp.
# @see https://github.com/SketchUp/api-issue-tracker/issues/414
#
# @param [String] dae_file_path Absolute path to the DAE file to fix.
# @raise [ArgumentError]
def self.fix_double_sided_faces(dae_file_path)

raise ArgumentError, 'dae_file_path must be a String' \
unless dae_file_path.is_a?(String)

dae_file_contents = File.read(dae_file_path)

# Thanks to Piotr Rachtan for this workaround.
faces_fix = '<extra><technique profile="GOOGLEEARTH">'
faces_fix += '<double_sided>1</double_sided>'
faces_fix += "</technique></extra>\n</profile_COMMON>"

dae_file_contents.gsub!('</profile_COMMON>', faces_fix)

File.write(dae_file_path, dae_file_contents)

end

end

end
22 changes: 2 additions & 20 deletions source/universal_importer/import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
require 'universal_importer/assimp'
require 'universal_importer/mtl'
require 'universal_importer/meshlab'
require 'universal_importer/collada'
require 'universal_importer/donate'

# Universal Importer plugin namespace.
Expand Down Expand Up @@ -112,7 +113,7 @@ def initialize()
end

convert_intermediate_to_final
fix_faces_in_final_dae
COLLADA.fix_double_sided_faces(@final_dae_file_path)
Sketchup.active_model.import(@final_dae_file_path)

# From now, SketchUp waits for user to place imported model as component.
Expand Down Expand Up @@ -404,25 +405,6 @@ def convert_intermediate_to_final

end

# Fixes double sided faces in final DAE file.
def fix_faces_in_final_dae

final_dae = File.read(@final_dae_file_path)

final_dae.insert(0, "<!-- File modified by Universal Importer plugin for SketchUp. -->\n")

# Thanks to Piotr Rachtan for workaround.
# @see https://github.com/SketchUp/api-issue-tracker/issues/414
faces_fix = '<extra><technique profile="GOOGLEEARTH">'
faces_fix += '<double_sided>1</double_sided>'
faces_fix += "</technique></extra>\n</profile_COMMON>"

final_dae.gsub!('</profile_COMMON>', faces_fix)

File.write(@final_dae_file_path, final_dae)

end

# Increments "imports.count" file.
def self.increment_counter

Expand Down
20 changes: 2 additions & 18 deletions source/universal_importer/poly_reduction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
require 'universal_importer/assimp'
require 'universal_importer/mtl'
require 'universal_importer/meshlab'
require 'universal_importer/collada'

# Universal Importer plugin namespace.
module UniversalImporter
Expand Down Expand Up @@ -67,7 +68,7 @@ def initialize
apply_polygon_reduction

convert_from_obj_to_dae
fix_faces_in_dae_import
COLLADA.fix_double_sided_faces(@dae_import_file_path)
convert_from_dae_to_skp

@completed = true
Expand Down Expand Up @@ -189,23 +190,6 @@ def convert_from_obj_to_dae
@dae_import_file_path = File.join(@temp_dir, 'import.dae')
end

# Fixes double sided faces in DAE file to import.
def fix_faces_in_dae_import
dae_import = File.read(@dae_import_file_path)

dae_import.insert(0, "<!-- File modified by Universal Importer plugin for SketchUp. -->\n")

# Thanks to Piotr Rachtan for workaround.
# @see https://github.com/SketchUp/api-issue-tracker/issues/414
faces_fix = '<extra><technique profile="GOOGLEEARTH">'
faces_fix += '<double_sided>1</double_sided>'
faces_fix += "</technique></extra>\n</profile_COMMON>"

dae_import.gsub!('</profile_COMMON>', faces_fix)

File.write(@dae_import_file_path, dae_import)
end

# Converts current SketchUp model to SKP format.
#
# @return [Boolean]
Expand Down

0 comments on commit 0ad5f96

Please sign in to comment.