Skip to content

Instantly share code, notes, and snippets.

@merwaaan
Last active May 4, 2020 09:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save merwaaan/26a9d10d3f06b36858dbcb8c8cd14cd1 to your computer and use it in GitHub Desktop.
Save merwaaan/26a9d10d3f06b36858dbcb8c8cd14cd1 to your computer and use it in GitHub Desktop.
module Test
unless file_loaded?(__FILE__)
SKETCHUP_CONSOLE.show()
# We'll count how many time the observer is fired when we open/create a model
@@counter = 0
def self.counter
@@counter
end
def self.counter=(x)
@@counter = x
end
# Custom definitions observer
class CustomDefinitionsObserver < Sketchup::DefinitionsObserver
def onComponentAdded(definitions, definition)
Test.counter += 1
puts "definition #{definition.entityID} added to model #{definition.model.definitions.entityID}: #{Test.counter} times"
end
end
puts "attaching observer to the startup model #{Sketchup.active_model.definitions.entityID}"
Sketchup.active_model.definitions.add_observer(CustomDefinitionsObserver.new())
# Custom app observer to attach a definitions observer to each new model
class CustomAppObserver < Sketchup::AppObserver
def onOpenModel(model)
puts ""
puts "opening model #{model.definitions.entityID}"
puts "attaching observer to model #{model.definitions.entityID}"
Test.counter = 0
model.definitions.add_observer(CustomDefinitionsObserver.new())
end
def onNewModel(model)
puts ""
puts "new model #{model.definitions.entityID}"
puts "attaching observer to model #{model.definitions.entityID}"
Test.counter = 0
model.definitions.add_observer(CustomDefinitionsObserver.new())
end
end
Sketchup.add_observer(CustomAppObserver.new())
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment