Skip to the content.

10. Events

Run Python automatically when something happens in SOLIDWORKS: a rebuild, a save, a new selection, a document switch. on(source, event, handler) subscribes; it is predefined in scripts.

```python live log = []

def rebuilt(): log.append(doc.GetTitle())

handle = on(doc, “rebuild”, rebuilt) # doc rebuilds -> rebuilt() runs doc.ForceRebuild3(False) print(log, handle) handle.remove()


Or as a decorator:

```python live
@on(doc, "save")
def saved():
    print("saved", doc.GetTitle())

off(saved)                                  # remove every handler that uses this function

Sources and event names

A source is any SOLIDWORKS object with events: sw (the application), documents (parts, assemblies, drawings), model views and objects from other API libraries such as motion studies. The event is either an API event name, exactly as in the API help (RegenPostNotify2, FileSavePostNotify …), or one of these aliases:

Alias Source API event
rebuild / before_rebuild document RegenPostNotify2 (or RegenPostNotify) / RegenNotify
save, save_as document FileSavePostNotify, FileSaveAsNotify2
selection, clear_selection document NewSelectionNotify, ClearSelectionsNotify
close document DestroyNotify2
dimension document DimensionChangeNotify
add_item, delete_item, rename_item document AddItemNotify, DeleteItemNotify, RenameItemNotify
config document ActiveConfigChangePostNotify
property document AddCustomPropertyNotify (or ChangeCustomPropertyNotify)
active_doc, open, new, close_doc sw ActiveDocChangeNotify, FileOpenPostNotify, FileNewNotify2, FileCloseNotify
idle sw OnIdleNotify

List everything a source supports:

```python live from swpy import events print(events.available(doc)[:12]) print(len(events.available(sw)), “application events”)


## Handlers

* A handler may take **the event's arguments** (see the API help for each event) or **none** - SwPy
  calls it the way it accepts:

  ```python live
  @on(sw, "new")
  def created(new_doc, doc_type, template):
      print("new document of type", doc_type)

  off(created)

Arguments that are SOLIDWORKS objects arrive as typed proxies.

Safety

Handlers can never take SOLIDWORKS down:

Keep handlers short - they run on the SOLIDWORKS UI thread, inside the operation that raised the event. idle fires very often: do as little as possible there.

Managing handlers

   
handle = on(source, event, fn) Subscribe; returns a Handler
handle.remove() / off(handle) Unsubscribe
off(fn) Remove all handlers using fn; returns how many
swpy.events.handlers() Active handlers (all, or handlers(session))
handle.calls, handle.errors, handle.active Statistics

```python live from swpy import events h = on(doc, “rebuild”, lambda: None) print(events.handlers()) h.remove()


## Example: keep a property in sync

```python
from swpy import events

def update_mass():
    props = doc.Extension.get_CustomPropertyManager("")
    props.Add3("MassKg", swconst.swCustomInfoType_e.swCustomInfoText,
               f"{model.mass['mass']:.3f}",
               swconst.swCustomPropertyAddOption_e.swCustomPropertyReplaceValue)

on(doc, "rebuild", update_mass)

Every rebuild now refreshes the MassKg property of this document, until it closes or you press Reset.