Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Add view converter service

Oliver Günther699e19965f57
diff --git a/app/controllers/admin_controller.rb b/app/controllers/admin_controller.rb
index 3d24a891000..bdb36707e7f 100644
--- a/app/controllers/admin_controller.rb
+++ b/app/controllers/admin_controller.rb
@@ -91,6 +91,10 @@ class AdminController < ApplicationController
@checklist += plaintext_extraction_checks
end
+ call_hook(:admin_information_checklist).each do |result|
+ @checklist += result
+ end
+
@storage_information = OpenProject::Storage.mount_information
end
diff --git a/modules/ifc_models/app/controllers/ifc_models/ifc_models_controller.rb b/modules/ifc_models/app/controllers/ifc_models/ifc_models_controller.rb
index a8c3fecb5eb..f28a20f124f 100644
--- a/modules/ifc_models/app/controllers/ifc_models/ifc_models_controller.rb
+++ b/modules/ifc_models/app/controllers/ifc_models/ifc_models_controller.rb
@@ -78,10 +78,7 @@ module ::IFCModels
def add_attachment
if (file = ifc_model_params['ifc_attachment']) && file && file.size.positive?
- @ifc_model.attachments.build(file: file,
- container: @ifc_model,
- description: 'IFC attachment',
- author: User.current)
+ @ifc_model.ifc_attachment = file
@ifc_model.title = file.original_filename
else
flash[:error] = t('ifc_models.could_not_save_file')
diff --git a/modules/ifc_models/app/models/ifc_models/ifc_model.rb b/modules/ifc_models/app/models/ifc_models/ifc_model.rb
index 5705267d1ff..fee25318289 100644
--- a/modules/ifc_models/app/models/ifc_models/ifc_model.rb
+++ b/modules/ifc_models/app/models/ifc_models/ifc_model.rb
@@ -5,21 +5,35 @@ module IFCModels
belongs_to :project
belongs_to :uploader, class_name: 'User', foreign_key: 'uploader_id'
- def ifc_attachment
- attachments.first
- end
+ %i(ifc xkt metadata).each do |name|
+ define_method "#{name}_attachment" do
+ get_attached_type(name)
+ end
+
+ define_method "#{name}_attachment=" do |file|
+ if name == :ifc
+ # Also delete xkt and metadata
+ delete_attachment :xkt
+ delete_attachment :metadata
+ end
- def xkt_attachment
- attachments.second
+ delete_attachment :ifc
+ attach_files('first' => { 'file' => file, 'description' => name })
+ end
end
- def delete_ifc_attachment
- delete_xkt_attachment
- ifc_attachment&.destroy
+ private
+
+ ##
+ # Delete the given named description
+ def get_attached_type(key)
+ attachments.find_by_description(key.to_s)
end
- def delete_xkt_attachment
- xkt_attachment&.destroy
+ ##
+ # Delete the given named description
+ def delete_attachment(key)
+ get_attached_type(key)&.destroy
end
end
end
diff --git a/modules/ifc_models/app/services/ifc_models/view_converter_service.rb b/modules/ifc_models/app/services/ifc_models/view_converter_service.rb
new file mode 100644
index 00000000000..6b805c5a2e7
--- /dev/null
+++ b/modules/ifc_models/app/services/ifc_models/view_converter_service.rb
@@ -0,0 +1,163 @@
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2017 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#+
+require 'open3'
+
+module IFCModels
+ class ViewConverterService
+
+ ##
+ # Check availability of the pipeline
+ def self.available?
+ available_commands.length == pipeline_commands.length
+ end
+
+ def self.pipeline_commands
+ %w[IfcConvert COLLADA2GLTF gltf2xkt xeokit-metadata]
+ end
+
+ def self.available_commands
+ @available ||= begin
+ pipeline_commands.select do |command|
+ _, status = Open3.capture2e('which', command)
+ status.exitstatus == 0
+ end
+ end
+ end
+
+ attr_reader :ifc_model, :errors
+
+ def initialize(ifc_model)
+ @errors = ActiveModel::Errors.new(self)
+ @ifc_model = ifc_model
+ end
+
+ def call
+ validate!
+
+ Dir.mktmpdir do |dir|
+ # Step 1: IfcConvert
+ Rails.logger.debug { "Converting #{ifc_model.inspect} to DAE"}
+ ifc_file = ifc_model.ifc_attachment.diskfile.path
+ collada_file = convert_to_collada(ifc_file, dir)
+
+ # Step 2: Collada2GLTF
+ Rails.logger.debug { "Converting #{ifc_model.inspect} to GLTF"}
+ gltf_file = convert_to_gltf(collada_file, dir)
+
+ # Step 3: Convert to XKT
+ Rails.logger.debug { "Converting #{ifc_model.inspect} to XKT"}
+ xkt_file = convert_to_xkt(gltf_file, dir)
+ ifc_model.xkt_attachment = File.new xkt_file
+
+ # Convert metadata
+ Rails.logger.debug { "Retrieving metadata of #{ifc_model.inspect}"}
+ metadata_file = convert_metadata(ifc_file, dir)
+ ifc_model.metadata_attachment = File.new metadata_file
+
+ if ifc_model.save
+ ServiceResult.new(success: true, result: ifc_model)
+ else
+ ServiceResult.new(success: false, errors: ifc_model.errors)
+ end
+ end
+ rescue StandardError => e
+ OpenProject.logger.error("Failed to convert IFC to XKT", exception: e)
+ ServiceResult.new(success: false).tap { |r| r.errors.add(:base, e.message) }
+ end
+
+ ##
+ # Call IfcConvert with an IFC file to output an identically-named
+ # DAE collada file.
+ #
+ # @param ifc_filepath {String} Path to the IFC model file
+ # @param target_dir {String} Path to the temporary output folder
+ def convert_to_collada(ifc_filepath, target_dir)
+ convert!(ifc_filepath, target_dir, 'dae') do |target_file|
+ Open3.capture2e('IfcConvert', '--use-element-guids', ifc_filepath, target_file)
+ end
+ end
+
+ ##
+ # Call COLLADA2GLTF with the converted DAE file.
+ #
+ # @param dae_filepath {String} Path to the converted DAE model file
+ # @param target_dir {String} Path to the temporary output folder
+ def convert_to_gltf(dae_filepath, target_dir)
+ convert!(dae_filepath, target_dir, 'gltf') do |target_file|
+ Open3.capture2e('COLLADA2GLTF', '-i', dae_filepath, '-o', target_file)
+ end
+ end
+
+ ##
+ # Call gltf2xkt with the converted gltf file.
+ #
+ # @param gltf_filepath {String} Path to the converted GLTF model file
+ # @param target_dir {String} Path to the temporary output folder
+ def convert_to_xkt(gltf_filepath, target_dir)
+ convert!(gltf_filepath, target_dir, 'xkt') do |target_file|
+ Open3.capture2e('gltf2xkt', '-s', gltf_filepath, '-o', target_file)
+ end
+ end
+
+ ##
+ # Call xeokit-metadata
+ #
+ # @param ifc_filepath {String} Path to the converted IFC model file
+ # @param target_dir {String} Path to the temporary output folder
+ def convert_metadata(ifc_filepath, target_dir)
+ convert!(ifc_filepath, target_dir, 'json') do |target_file|
+ Open3.capture2e('xeokit-metadata', ifc_filepath, target_file)
+ end
+ end
+
+ ##
+ # Build input filename and target filename
+ def convert!(source_file, target_dir, ext, &block)
+ filename = File.basename(source_file, '.*')
+ target_filename = "#{filename}.#{ext}"
+ target_file = File.join(target_dir, target_filename)
+
+ out, status = block.call target_file
+
+ if status.exitstatus != 0
+ raise "Failed to convert #{filename} to #{ext}: #{out}"
+ end
+
+ target_file
+ end
+
+ def validate!
+ unless self.class.available?
+ missing = self.class.pipeline_commands - self.class.available_commands
+ raise I18n.t('ifc_models.conversion.missing_commands', names: missing.join(", "))
+ end
+
+ true
+ end
+ end
+end
diff --git a/modules/ifc_models/config/locales/en.yml b/modules/ifc_models/config/locales/en.yml
index 7614fa47f91..cfd1fd36871 100644
--- a/modules/ifc_models/config/locales/en.yml
+++ b/modules/ifc_models/config/locales/en.yml
@@ -9,6 +9,11 @@ en:
label: 'Processing?'
in_progress: 'In progress'
completed: 'Completed'
+ conversion:
+ missing_commands: "The following IFC converter commands are missing on this system: %{names}"
project_module_ifc_models: "IFC models"
permission_view_ifc_models: "View IFC models"
permission_manage_ifc_models: "Import and manage IFC models"
+ extraction:
+ available:
+ ifc_convert: "IFC conversion pipeline available"
diff --git a/modules/ifc_models/lib/open_project/ifc_models/engine.rb b/modules/ifc_models/lib/open_project/ifc_models/engine.rb
index 26e77a9e568..852b61c3d18 100644
--- a/modules/ifc_models/lib/open_project/ifc_models/engine.rb
+++ b/modules/ifc_models/lib/open_project/ifc_models/engine.rb
@@ -62,6 +62,10 @@ module OpenProject::IFCModels
end
end
+ config.to_prepare do
+ require 'open_project/ifc_models/hooks'
+ end
+
patches %i[Project]
end
end
diff --git a/modules/ifc_models/lib/open_project/ifc_models/hooks.rb b/modules/ifc_models/lib/open_project/ifc_models/hooks.rb
new file mode 100644
index 00000000000..22c34a7a8dd
--- /dev/null
+++ b/modules/ifc_models/lib/open_project/ifc_models/hooks.rb
@@ -0,0 +1,41 @@
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2017 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#+
+
+module OpenProject::IFCModels::Hooks
+ class Hook < Redmine::Hook::Listener
+ include ActionView::Helpers::TagHelper
+ include ActionView::Context
+ include WorkPackagesHelper
+
+ def admin_information_checklist(*)
+ [
+ [:'extraction.available.ifc_convert', ::OpenProject::IFCModels::Converter.available?]
+ ]
+ end
+ end
+end