Can the Ruby API monitor the person’s actions in drawing faces with the drawing device? Every time the person attracts a floor get the coordinate info of that floor.
I discovered the related code, however I don’t know management MyEntitiesObserver to run or cease. I’d admire any recommendation, thanks!
class MyEntitiesObserver < Sketchup::EntitiesObserver
def onElementAdded(entities, entity)
places "onElementAdded: #{entity}"
if entity.typename == "Face"
p entity.persistent_id
finish
finish
finish
# Connect the observer
Sketchup.active_model.entities.add_observer(MyEntitiesObserver.new)
See the instance right here
Entities #remove_observer-instance_method
By the way in which:entity.typename == "Face"
comparability is sluggish, use entity.is_a?(Sketchup::Face)
as a substitute.
1 Like
You’ll doubtless additionally want to connect a ToolsObserver
to the mannequin’s toolstack expecting the change to the Line device.
To do this, you want a ModelObserver
that additionally watches for modifications to the energetic edit path, so you’ll be able to watch the active_entities
.
To do this you want an AppObserver
that watches the applying for when fashions are created or opened.
AppObserver
sees mannequin load or create —> attaches new hybrid ModelObserver
—> attaches itself to it’s Instruments
assortment —> which is able to begin watching the active_entities
when the Line Software is in use.
This instance makes use of the extension submodule because the AppObserver
object, since there’s solely one software and solely one AppObserver
will ever be wanted. (Ie, … we do not ever want an occasion of a category object for an AppObserver
object.)
Right here is an instance … (to view – click on to increase) …
# encoding: UTF-8
#
# Instance extension module as an observer.
module Instance # <<---<<< change to YOUR UNIQUE namespace module
module SomeExtension # <<---<<< change to desired title
lengthen self # ... self refers back to the submodule right here
# A hybrid observer that watches the mannequin, the toolstack and when
# the Line device is energetic additionally the watches the energetic entities.
class MyHybridObserver
# NOTE: Outdoors occasion strategies, self refers back to the class.
# Inside cases strategies, self refers back to the occasion when
# the strategies are literally executed.
# SketchUp's observer queue will maintain the reference to this observer.
def initialize(mannequin)
@mannequin = mannequin
@watching = nil
# Connect itself to the mannequin object:
@mannequin.add_observer(self)
# Connect itself to the mannequin's toolstack:
@mannequin.instruments.add_observer(self)
#
### ... connect this observer or different observers,
### to the mannequin's collections right here ...
#
finish
def onActivePathChanged(mannequin)
if @watching
# Solely watch the energetic entities context:
@watching.remove_observer(self)
@watching = mannequin.active_entities
@watching.add_observer(self)
finish
#
finish
def onActiveToolChanged(instruments, tool_name, tool_id)
case tool_id
when 21020 # Sketch (Edge/Line) device
@watching = @mannequin.active_entities
@watching.add_observer(self)
when 10508, 10509, 10520, 10523, 10525 # Digital camera instruments
# Do not change @watching, simply return:
return
else # another device
if @watching
@watching.remove_observer(self)
@watching = false
finish
finish
finish
def onElementAdded(entities, entity)
if entity.is_a?(Sketchup::Face)
# Do one thing with: entity.vertices
finish
finish
finish # observer class
### APPOBSERVER CALLBACKS
#
def expectsStartupModelNotifications
true
finish
def onNewModel(mannequin)
# Create a mannequin particular spy that attaches itself to the mannequin:
MyHybridObserver.new(mannequin)
finish
def onOpenModel(mannequin)
# Create a mannequin particular spy that attaches itself to the mannequin:
MyHybridObserver.new(mannequin)
finish
### RUN ONCE AT STARTUP
#
if !outlined?(@loaded)
#
### outline GUI objects right here ...
#
# Connect this submodule as an AppObserver to SketchUp:
::Sketchup.add_observer(self)
# Mark this submodule as loaded:
@loaded = true
finish
finish # extension submodule
finish # namespace module
… or obtain the entire file … face_watcher.rb (2.7 KB)
Thanks in your steerage and recommendation