@DanRathbun, thanks for the workaround. This was helpful and now I have got it to work
…I still have a lot to do around it but I got the concept. (camera settings, update page, hidden layers, etc., I’ll leave them for other posts if needed)
Furthermore, I had one major error which contributed to my lot’s of questions. That was a wrong Layout::Document
as I was grabbing an empty LO doc with this: doc = Layout::Document.new
rather than the one which included the entities
already added by doc.add_entity
.
One thing is still connected here. How to grab the .skp file to create the Viewport? It seems that it matters if I save the file before sending to LO. If I do model.save
it does exactly the same as I hit the “save button” in the UI of SU? Otherwise it creates the viewport from the last saved version, if I got it right.
The bounds are also ok now, and I keep all units in inches. I’ll need several “utility” methods to create a proper bounds with scaled object. They will need to be in 1:50 or 1:100 or something similar, based on the building size.
I paste my code below which works ok, except the hidden layers.
module Kisfali
module Scaffolding
module LayoutDrawings
extend self
def create_layout_doc
model_path = Sketchup.active_model.path
if model_path.empty?
UI.messagebox("File not saved. Please save your file and try again.", MB_OK)
return nil
else
name = model_path.split('/')[-1].split('.skp')[0]
path = model_path.split('/')[0..-2].join('/')
@filename = File.join(path, "#{name}.layout")
end
template = "#{PLUGIN_ROOT}/20-009 Trebex template.layout"
doc = Layout::Document.new(template)
status = doc.save(@filename)
puts status
end
def prep_model
model = Sketchup.active_model
entities = model.active_entities
pages = model.pages
arr_of_pages = pages.to_a
if pages.length != 0
arr_of_pages.each{|page| pages.erase(page)}
end
views = ["Front", "Right", "Back","Left"]
views.each {|view|
pages.add view
}
pages.selected_page = model.pages[0]
end
def turn_off_spaces #layer in model
model = Sketchup.active_model
layers = model.layers
layers.each {|layer|
if layer.name.include? "Space"
layer.visible = false
end
}
end
def set_camera
turn_off_spaces
model = Sketchup.active_model
pages = model.pages
puts "scene length: #{pages.length}"
page = pages["Front"]
page.use_hidden_layers = true
puts "page layers count: #{page.layers.length}"
status = page.update
puts "Front page status: #{status}"
model_center = model.bounds.center
puts "center x: #{model_center.x.to_m}"
# Create a camera from scratch with an "eye" position in
# x, y, z coordinates, a "target" position that
# defines what to look at, and an "up" vector.
eye = [model_center.x, -30.m, model_center.z]
target = [model_center.x,model_center.y.to_m,model_center.z]
up = [0,0,1]
my_camera = Sketchup::Camera.new eye, target, up
my_camera.perspective = false
# Get a handle to the current view and change its camera.
view = Sketchup.active_model.active_view
view.camera = my_camera
status = page.update(33) #camera + visible layers
end
def add_model
doc = Layout::Document.open(@filename)
layers = doc.layers
new_layer = layers.add("Scaffold", shared=false)
layers.active = new_layer
first_layer = layers.first
this_page = doc.pages.first
page_info = doc.page_info
# Get the paper height and width and set the output resolution
width = page_info.width
height = page_info.height
bounds = Geom::Bounds2d.new(1,1,24,15)
model_file = @filename.split('.layout')[0]
viewport = Layout::SketchUpModel.new("#{model_file}.skp", bounds)
# Set the viewport scene index to 1 more than the model scene:
# 0 is the auto-added scene by LO as Last Saved SU View
viewport.current_scene = 1 # that's the Front scene
viewport.view = Layout::SketchUpModel::FRONT_VIEW
doc.add_entity(viewport, new_layer, this_page)
viewport.render_mode = Layout::SketchUpModel::VECTOR_RENDER
viewport.render if viewport.render_needed?
doc.save
return doc
end
def export_layout(doc)
# Export pages one through three at high quality, compressing jpeg images
# at 0.75 compression quality (valid range is 0.0 - 1.0). Note that the
# first page of a {Layout::Document} is index 0.
options = {
start_page: 0,
end_page: 1,
output_resolution: Layout::PageInfo::RESOLUTION_HIGH,
compress_images: true,
compress_quality: 0.75
}
export_path = @filename.split('.layout')[0] + '.pdf'
puts export_path
status = doc.export(export_path, options)
end
def generate_drawings
model = Sketchup.active_model
model.start_operation("Generating 2D drawings", true)
create_layout_doc
prep_model
set_camera
doc = add_model
export_layout(doc)
model.commit_operation
end
end
end
end
And here is how it looks: