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 possibility to seed ifc models

Henriette Dinger8d397b964b9b
diff --git a/modules/bim_seeder/app/seeders/bim_seeder/demo_data/ifc_model_seeder.rb b/modules/bim_seeder/app/seeders/bim_seeder/demo_data/ifc_model_seeder.rb
new file mode 100644
index 00000000000..2f920022ac1
--- /dev/null
+++ b/modules/bim_seeder/app/seeders/bim_seeder/demo_data/ifc_model_seeder.rb
@@ -0,0 +1,104 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 the OpenProject GmbH
+#
+# 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 BimSeeder
+ module DemoData
+ class IfcModelSeeder < ::Seeder
+ attr_reader :project, :key
+
+ def initialize(project, key)
+ @project = project
+ @key = key
+ end
+
+ def seed_data!
+ models = project_data_for(key, 'ifc_models')
+ return unless models.present?
+
+ print ' ↳ Import IFC Models'
+
+ models.each do |model|
+ seed_model model
+ end
+ end
+
+ private
+
+ def seed_model(model)
+ user = User.admin.first
+
+ xkt_data = get_file model[:file] + '.xkt'
+ meta_data = get_file model[:file] + '.json'
+
+ if xkt_data.nil? || meta_data.nil?
+ print ' ↳ Missing converted data for ifc model'
+ else
+ create_model(model, user, xkt_data, meta_data)
+ end
+ end
+
+ def create_model(model, user, xkt_data, meta_data)
+ model_container = create_model_container project, user, model[:name]
+
+ add_ifc_model_attachment model_container, user, xkt_data, 'xkt'
+ add_ifc_model_attachment model_container, user, meta_data, 'metadata'
+ end
+
+ def create_model_container(project, user, title)
+ model_container = IFCModels::IFCModel.new
+ model_container.title = title
+ model_container.project = project
+ model_container.uploader = user
+ model_container.is_default = true
+
+ model_container.save!
+ model_container
+ end
+
+ def add_ifc_model_attachment(model_container, user, file, description)
+ attachment = Attachment.new(
+ container: model_container,
+ author: user,
+ file: file,
+ description: description
+ )
+ attachment.save!
+ end
+
+ def get_file(name)
+ path = 'modules/bim_seeder/files/ifc_models/'
+ return unless File.exist?(path + name)
+
+ File.new(File.join(Rails.root,
+ path,
+ name))
+ end
+ end
+ end
+end
diff --git a/modules/bim_seeder/lib/open_project/bim_seeder/patches/project_seeder_patch.rb b/modules/bim_seeder/lib/open_project/bim_seeder/patches/project_seeder_patch.rb
index 8e23e066ff7..b6377ece9c0 100644
--- a/modules/bim_seeder/lib/open_project/bim_seeder/patches/project_seeder_patch.rb
+++ b/modules/bim_seeder/lib/open_project/bim_seeder/patches/project_seeder_patch.rb
@@ -5,7 +5,10 @@ module OpenProject::BimSeeder::Patches::ProjectSeederPatch
module InstanceMethods
def project_data_seeders(project, key)
- [::BimSeeder::DemoData::BcfXmlSeeder.new(project, key)] + super(project, key)
+ [
+ ::BimSeeder::DemoData::BcfXmlSeeder.new(project, key),
+ ::BimSeeder::DemoData::IfcModelSeeder.new(project, key)
+ ] + super(project, key)
end
end
end
diff --git a/modules/bim_seeder/spec/seeders/demo_data_seeder_spec.rb b/modules/bim_seeder/spec/seeders/demo_data_seeder_spec.rb
index c2b138268d9..33d3a9080c2 100644
--- a/modules/bim_seeder/spec/seeders/demo_data_seeder_spec.rb
+++ b/modules/bim_seeder/spec/seeders/demo_data_seeder_spec.rb
@@ -64,6 +64,7 @@ describe 'seeds' do
expect(Status.count).to eq 4
expect(IssuePriority.count).to eq 4
expect(Project::Status.count).to eq 4
+ expect(IFCModels::IFCModel.count).to eq 0
ensure
ActionMailer::Base.perform_deliveries = perform_deliveries
end