Skip to content

Commit

Permalink
bugfix
Browse files Browse the repository at this point in the history
Now it works :)
  • Loading branch information
cshaa committed Feb 18, 2017
1 parent 0dbe197 commit 9f644fc
Showing 1 changed file with 38 additions and 35 deletions.
73 changes: 38 additions & 35 deletions m93a_osm_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,53 +16,56 @@
module M93A
module OSM_Importer

def m93a_osm_import()
class << self
def m93a_osm_import

model = Sketchup.active_model
imported = model.entities.add_group
model = Sketchup.active_model
imported = model.entities.add_group

# Check for georeferencing
unless model.georeferenced?
UI.messagebox("The model needs to be georeferenced!")
return
end
# Check for georeferencing
unless model.georeferenced?
UI.messagebox("The model needs to be georeferenced!")
return
end

# Start the operation
model.start_operation("Import OSM File",true)
# Start the operation
model.start_operation("Import OSM File",true)

# Ask user for the OSM file
base = UI.openpanel("Select OSM File", "~", "OSM Files|*.osm;||")
path = File.dirname(base) + '/' + File.basename(base,".*") + '.osm'
# Ask user for the OSM file
base = UI.openpanel("Select OSM File", "~", "OSM Files|*.osm;||")
path = File.dirname(base) + '/' + File.basename(base,".*") + '.osm'

# Parse the file
doc = Document.new File.new path
nodes = Hash.new
# Parse the file
doc = Document.new File.new path
nodes = Hash.new

# Read positions of nodes
doc.elements.each("osm/node") { |n|
id = n.attributes["id"]
lat = n.attributes["lat"].to_f
lon = n.attributes["lon"].to_f
nodes[id] = model.latlong_to_point [lat,lon]
}
# Read positions of nodes
doc.elements.each("osm/node") { |n|
id = n.attributes["id"]
lat = n.attributes["lat"].to_f
lon = n.attributes["lon"].to_f
# BEWARE! Longitude comes first!
nodes[id] = model.latlong_to_point [lon,lat]
}

# Add ways to the model
doc.elements.each("osm/way") { |w|
points = []
group = imported.entities.add_group
w.elements.each("nd") { |n|
points.push nodes[n.attributes["ref"]]
# Add ways to the model
doc.elements.each("osm/way") { |w|
points = []
group = imported.entities.add_group
w.elements.each("nd") { |n|
points.push nodes[n.attributes["ref"]]
}
group.entities.add_edges points
}
group.entities.add_edges points
}

# Commit the operation
model.commit_operation
end
# Commit the operation
model.commit_operation
end #m93a_osm_import
end #class


unless file_loaded?("m93a_osm_importer.rb")
UI.menu("PlugIns").add_item("Import OSM File (new)") { m93a_osm_import() }
UI.menu("PlugIns").add_item("Import OSM File") { m93a_osm_import }
file_loaded("m93a_osm_importer.rb")
end

Expand Down

0 comments on commit 9f644fc

Please sign in to comment.