I am looking for a way in add a component into a model with a x, y location. I like this code for importing but can’t seem to find how to place the component to an x, y location without placing it by a mouse click.
model = Sketchup.active_model
component_def = model.definitions.load(path_to_component_file)
model.place_component(component_def)
That works great! Can I do the same thing with a file from a URL? I tried this code:
mod = Sketchup::active_model
url = "https://allanblock.com/sketchup/TestBox.skp"
cdef = mod.definitions.load_from_url(url)
return unless cdef # good practice
# could be "next unless cdef" inside a loop
point = Geom::Point3d::new( 10, 10, 0 )
cinst = mod.active_entities.add_instance(
cdef,
Geom::Transformation::new( point )
)
module Testing # <---<<< Change to your namespace
class LoadHandler
attr_accessor :error
def initialize(url = nil, transform = IDENTITY)
load(url, transform) if !url.nil?
end
def cancelled?
return false
end
def load(url, transform)
model = Sketchup.active_model
definitions = model.definitions
# Reset the url and success flag before each load attempt:
@url = url
@success = false
definition = definitions.load_from_url(url, self)
if @success && definition.is_a?(Sketchup::ComponentDefinition)
model.active_entities.add_instance(definition, transform)
end
end
# @param [String] error_message
def onFailure(error_message)
self.error = error_message
Sketchup::set_status_text('')
UI.messagebox("Error downloading from URL:\n #{@url}\n #{error_message}")
end
# @param [Float] percent
def onPercentChange(percent)
Sketchup::set_status_text("loading: #{percent.round}%")
end
def onSuccess
@success = true
Sketchup::set_status_text('')
puts("Download successful from URL:\n #{@url}\n")
end
end # class
end # module
… (scroll to see all code) …
EDITED:(2023-02-01) : Fixed, ie load_from_url() must return before placing component instance.
EDITED:(2023-02-02) : Simplified and changed so that an instance can be reused for multiple downloads.
To reuse a loader instance, create it without arguments so the @url attribute will be nil and the constructor will not call the #load() method.
Then your code can call the #load() instance method any number of times.
For example you might have a hash with filename keys each with a transform value and you want to batch load them:
# base_url is a string URI to where the files are located
# load_set is the hash with "filename" / transform pairs
loader = Testing::LoadHandler.new
load_set.each do |file, transform|
loader.load( File.join(base_url, "#{file}.skp"), transform )
end
loader = nil # Let garbage collection clean it up
oh I didn’t get it…
I tried to download the file via browser, the file is downloadable from the url…
I tried the code your code in ruby console, but it does not work.
Sadly I have no glue…I would appreciate your help Thanks