Run a ruby script every time a file is loaded?

Can it be done?

The reason? We have thousands of Sketchup files and, as we update our standards, things like layers, styles, and materials change. Rather than going through those thousands of files to weed out the old standards, I’d like to create a script that “checks” the standards of the file on open.

Sounds simple but I thing plugins\extensions are only loaded on Sketchup startup.

Thanks!

True … BUT … an extension can implement itself as an AppObserver that watches for model openings and new model creates.

Here is an example of an extension’s main file that attaches the extension submodule itself as an AppObserver object to the SketchUp application:

module HankBinkle
  module StandardsEnforcer

    extend self

    ### AppObserver Callbacks
    #

    def expectsStartupModelNotifications
      return true
    end

    def onOpenModel(model)
      enforce_standards(model)
    end    

    def onNewModel(model)
      enforce_standards(model)
    end    

    ### Extension Functionality
    #

    # Check that the model meets standards.
    # If not, make changes and save the model.
    def enforce_standards(model)
      # This method will likely call several other methods
      # to do various checking and/or correction tasks.
    end    

    ### Run Once at Startup block:
    #
    unless defined?(@loaded)
      # Attach this module as an AppObserver object:
      Sketchup.add_observer(self)
      # Set the @loaded flag:
      @loaded = true
    end

  end
end

(…scroll to see all code…)


In SketchUp embedded Ruby, code is event driven. It normally does not use sequential “scripts” that run and then get unloaded like you do from a system prompt.

(But yes it is possible to load functionality on demand. For example you could have code in a class and only define that class when you need it to operate upon a model, make an instance of it, use the instance to modify the model, dispose of the instance, and then afterward undefine the class object. But this is likely to be more trouble than it’s worth.)

2 Likes

hopefully you’ll have the old-to-new mapping so when layer changes (now tags), materials etc are altered, the underlying model objects don’t inadvertently get damaged or deleted. especially a potential issues if naming changes are also required since as components you may find conflicts occurring.

as a note from my experience. my templates update probably 1-2 times per year, but projects are left intact if they’re completed. projects in flight - i’ll typically update those - mostly in the LayOut stage unless i’ve done some major updates to my dynamic components (rarely necessary).

i do keep a spreadsheet with lists of each major revision layers, styles, baseline viewports, etc so i can see the evolution and if i need to reuse something can reliably know what i need to update.

2 Likes

Genius! You are always on the ball. :slight_smile: Thanks so much @DanRathbun

You’re right… I need to get into the event-driven frame of mind. So far, I have only written click and wait stuff. I forget that there is a robust observer to explore.

@glennmstanton out of curiosity? what field? I’m architecture.

The files I update\maintain are components in a library I have been developing for years. We use them daily on our projects.

I started using FlexTools Component Finder recently and it lets me “Overwrite with Selected” to the library as I work. I improve the library the minute I discover a component bug or make an improvement. It’s awesome!

It gave me the idea to extend it to other parts of our library, heck our templates, layers, naming conventions. Ideally I you could select something like a layer and say “rename globally” and everything in the library would be broadcast the updates. It’s sort of like a push version of Component Finder.

That sounded too ambitious but I WOULD settle for updating files as the are opened, hence the question herein.

Long story short… too late.

Anyway, I’ll give this a go between Formula one and drafting. Thanks.

1 Like

mainly architecture with some printed component work. most of my dynamic components (and even non-dynamic ones) are designed for creating 3D models which can also properly represent 2D views to create construction documents, and some 2D specific models used as typical construction details.

1 Like