Sketchup crashed after Sketchup.active_model.close

Steps:

  1. open a sketchup file, select a group, s1 = Sketchup.active_model.selection.first
  2. open a new file, Sketchup.file_new
  3. copy s1 to new file: Sketchup.active_model.entities.add_instance(s1.definition, s1.transformation)
  4. save the file, Sketchup.active_model.save('some/path')
  5. close file, Sketchup.active_model.close

then, Sketchup crashed.

If use command + C to copy to the new file and save then close it, Sketchup won’t crash.
What are the differences between the two copy methods? How could I avoid the crash if I use API?

The API code is written so that it expects operations only upon the active model.
It is the API programmer’s responsibility to always write code that stays within this assumption.

Your crashing code example broke this rule. It attempted to add an instance of a component definition that did not belong to the model’s definition list that you were inserting it in.

Paradigm 1: (Something like this. – UNTESTED)

module KillerNova
  module SomePlugin
    extend self

    class PasteWatcher
      def onActivateModel(model) # <--<<< Mac ONLY
        Sketchup.send_action("paste:")
        Sketchup.remove_observer(self)
      end
    end

    def paste_into_new()
      return if Sketchup.active_model.selection.empty?
      Sketchup.send_action("copy:")
      Sketchup.add_observer(PasteWatcher.new)
      # change model by opening a new one:
      Sketchup.file_new
    end

  end
end

Paradigm 2:

Would be to save out the component as a skp file, then load it into the new model’s definition list, then place an instance at the desired transform.

1 Like

@tt_su Thomas, do you think this crash scenario should get a GitHub issue, even though it should not have been attempted. Ie … should #add_instance raise an exception if the definition argument does not belong to the entities receiver’s model ?