Placing components within a model by x and y not the mouse

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)

Take a look at Entities#add_instance.

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 )
)

But get Error: Invalid component URL.


Does the file exist at the URL and is it publicly accessible ?

When I click on the above URL I get a 404 not found error.

This is in line with the error received from an abstract load handler class doing …

Testing::LoadHandler::new(
  "https://allanblock.com/sketchup/TestBox.skp", [10, 10, 0]
)
module Testing
  class LoadHandler
    attr_accessor :error

    def initialize(url, transform = IDENTITY)
      @url = url
      @model = Sketchup.active_model
      @transform = transform
      load(@url)
    end

    def load(url)
      @comp_def = @model.definitions.load_from_url(url, self)
    end

    # @param [Float] percent
    def onPercentChange(percent)
      Sketchup::set_status_text("loading: #{percent.round}%")
    end

    def cancelled?
      # You could, for example, show a messagebox after X seconds asking if the
      # user wants to cancel the download. If this method returns true, then
      # the download cancels.
      return false
    end

    def onSuccess
      Sketchup::set_status_text('')
      place_instance(@transform)
      UI.messagebox("Download successful from URL:\n #{@url}\n")
    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

    def place_instance(transform)
      @model.active_entities.add_instance(@comp_def,transform)
    end

  end # class
end # module

(scroll to see all code)

:nerd_face: