# Reference to the GitHub script: https://github.com/modelcreate/infoworks-ruby-scripts/blob/master/scripts/open_data_import_centre/import_model.rb
# Import the necessary library for file utilities
require 'FileUtils'
# Get the current open network in InfoWorks
open_net = WSApplication.current_network
# Prompt the user to select a folder for importing files
shp_dir = WSApplication.folder_dialog 'Select a folder to import files', true
# The configuration (CFG) files are kept in the same directory as this Ruby script
cfg_dir = File.dirname(WSApplication.script_file)
# Define paths for the error file and the script file
err_file = shp_dir + '\errors.txt'
script_file = cfg_dir + '\odic_script.bas'
# Define the layers and their respective configuration and shape file paths
layers = {
"hydrant" => { "cfg" => cfg_dir + '\hydrant.cfg', "shp" => shp_dir + '\hydrants.shp'},
"valve" => { "cfg" => cfg_dir + '\valve.cfg', "shp" => shp_dir + '\valves.shp'},
"polygons" => { "cfg" => cfg_dir + '\polygons.cfg', "shp" => shp_dir + '\dma.shp' },
"pipe" => { "cfg" => cfg_dir + '\pipe.cfg', "shp" => shp_dir + '\mains.shp' }
}
# Iterate through each layer and import the data
layers.each do |layer, config|
puts "Importing IW layer: #{layer}"
# Skip the import if the shape file doesn't exist
if File.exist?(config["shp"]) == false
puts "Shape file not found for #{layer} layer - #{config["shp"]}"
next
end
# Define additional options for the import
options = {
"Error File" => err_file, # Location to save error logs
"Set Value Flag" => 'IG', # Import Flag
"Script File"=> script_file # Import Script (ICM .rb / WS .bas)
}
# Execute the import function
open_net.odic_import_ex(
'shp', # Source Type: Shapefile
config["cfg"], # Field mapping configuration
options, # Additional options in Ruby Hash format
layer, # InfoWorks Layer to import to
config["shp"] # Location of the shape file
)
# Append the error log with the completion status of each layer
File.write(err_file, "\n End of #{layer} import \n", File.size(err_file))
end
# Read and output the content of the error log to the console
File.readlines(err_file).each do |line|
puts line
end
# Print a completion message
puts "Finished Import"
No comments:
Post a Comment