# Observer for recognize object moved

**URL:** <https://forums.sketchup.com/t/observer-for-recognize-object-moved/57414>\
**Category:** Ruby API\
**Tags:** sketchup\
**Created:** [December 11, 2017, 7:51pm UTC](https://forums.sketchup.com/t/observer-for-recognize-object-moved/57414 "2017-12-11T19:51:52Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![d.trepiccione93](https://avatars.discourse-cdn.com/v4/letter/d/f07891/32.png) [@d.trepiccione93](https://forums.sketchup.com/u/d.trepiccione93)\
**Post date:** [December 11, 2017, 7:51pm UTC](https://forums.sketchup.com/t/observer-for-recognize-object-moved/57414/1 "2017-12-11T19:51:52Z")

</div>

Hy, I’am build a sketchup extension for a simulation of better router position. It’s a University project.  
I need an observer to recognize when a user moves the object. With this I used if I remove the object the observer answers me but if I move it gives me no results … Help me please!

```ruby
#this is my Observer    	
class Observer < Sketchup::EntityObserver
    		def onEraseEntity(entity)
            	puts("EntityObserver::onEraseEntity: #{entity.to_s}, is deleted? #{entity.deleted?}")
       	 	end  
       	 	def onChangeEntity(entity)
            	puts("EntityObserver::onChangeEntity: #{entity.to_s} #{entity.entityID}")
        	end		
	end

#this is the code that add Observer
def self.create_Ap(x,y,z,x_min,y_min)
	  model = Sketchup.active_model
	  model.start_operation('Create Ap', true)
	  group = model.active_entities.add_group
	  entities = group.entities
	  points = [
	    Geom::Point3d.new(x+x_min, y+y_min, z.m),
	    Geom::Point3d.new((x+(0.15.m)+x_min), y+y_min, z.m),
	    Geom::Point3d.new((x+(0.15.m)+x_min), (y+(0.1.m)+y_min), z.m),
	    Geom::Point3d.new(x+x_min,(y+(0.1.m))+y_min, z.m)
	  ]
	  face = entities.add_face(points)
	  face.pushpull(-0.02.m)
	  face.material = "Black"
	  face.back_material = "Black"
	  face.set_attribute "testdictionary", "test", 1
	  if $node == nil
	  	$node = Array.new
	  end
	  $node.push(face)
	  face.add_observer(Observer.new)
	  model.commit_operation
	end

```

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [December 11, 2017, 8:26pm UTC](https://forums.sketchup.com/t/observer-for-recognize-object-moved/57414/2 "2017-12-11T20:26:33Z")

</div>

Please read this on how to post code.

- [[How to] Colourize code on the forum?](https://forums.sketchup.com/t/how-to-colourize-code-on-the-forum-wiki/46189/)

Please use proper Ruby 2 space indentation.

Also, do not use $ global variables ! (Use module or class variables instead.) _See:_

> **[SketchUp Plugin Checklist](http://www.thomthom.net/thoughts/2013/02/sketchup-plugin-checklist/)**
>
> Here’s a simple overview of important items to remember when developing and distributing a SketchUp plugin. Where I have already written about the topic or other sources exists it’s bee…

---

<div class="post-metadata">

**Author:** ![d.trepiccione93](https://avatars.discourse-cdn.com/v4/letter/d/f07891/32.png) [@d.trepiccione93](https://forums.sketchup.com/u/d.trepiccione93)\
**Post date:** [December 11, 2017, 8:43pm UTC](https://forums.sketchup.com/t/observer-for-recognize-object-moved/57414/3 "2017-12-11T20:43:15Z")

</div>

Thanks. You know how solve my Observer problem?

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [December 11, 2017, 8:44pm UTC](https://forums.sketchup.com/t/observer-for-recognize-object-moved/57414/4 "2017-12-11T20:44:46Z")

</div>

> [@d.trepiccione93](#):
>
> You know how solve my Observer problem?

Now that it is readable I’ll look at it. May take a bit of time. (Still on 1st cup of coffee. ☕ )

---

<div class="post-metadata">

**Author:** ![eneroth3](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/eneroth3/32/1222_2.png) [@eneroth3](https://forums.sketchup.com/u/eneroth3)\
**Post date:** [December 11, 2017, 9:17pm UTC](https://forums.sketchup.com/t/observer-for-recognize-object-moved/57414/5 "2017-12-11T21:17:53Z")

</div>

Can you elaborate what the problem is? I ran the observer and didn’t see anything unexpected with it.

Also it’s quite hard to understand your code. Using 2 space indentation would help as we wouldn’t need to scroll sideways (or scroll away vertically from where we are just to get to the horizontal scroll bar). Using paranthesis around arguments makes it much easier to see what words are arguments to what method call. Lastly global variables should be avoided as they may clash with global variables defined by other plugins. You can wrap your code in a module and use a class variable instead.

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [December 11, 2017, 9:20pm UTC](https://forums.sketchup.com/t/observer-for-recognize-object-moved/57414/6 "2017-12-11T21:20:40Z")

</div>

> [@d.trepiccione93](#):
>
> I need an observer to recognize when a user moves the object.

**It works for me.** `onChangeEntity()` fires if you double-click into the group and move one of the BLACK face’s vertices with the **MoveTool** , or move the entire face with the **PushPullTool**.

Were you wanting to fire the observer if the user moved the entire group instance ?  
If so, you’ll need to attach the observer to the group reference also.

---

<div class="post-metadata">

**Author:** ![eneroth3](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/eneroth3/32/1222_2.png) [@eneroth3](https://forums.sketchup.com/u/eneroth3)\
**Post date:** [December 11, 2017, 9:28pm UTC](https://forums.sketchup.com/t/observer-for-recognize-object-moved/57414/7 "2017-12-11T21:28:58Z")

</div>

> [@DanRathbun](#):
>
> Were you wanting to fire the observer if the user moved the entire group instance ?
> 
> If so, you’ll need to attach the observer to the group reference also.

I simply attached the observer myself to a selected entity from the console rather than figuring in out what the long method did and make up values for its arguments. I think you found the problem!

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [December 11, 2017, 9:35pm UTC](https://forums.sketchup.com/t/observer-for-recognize-object-moved/57414/8 "2017-12-11T21:35:34Z")

</div>

Yes I had to cleanup the code, module wrap it, add default arguments and add a menuitem:

```ruby
module Author; end
module Author::SomePluginRenameMe

  extend self

  @@loaded ||= false

  #this is my Observer    	
  class Observer < Sketchup::EntityObserver
    def onEraseEntity(entity)
      puts("EntityObserver::onEraseEntity: #{entity.to_s}"<<
        ", is deleted? #{entity.deleted?}")
    end
    def onChangeEntity(entity)
      puts("EntityObserver::onChangeEntity: "<<
        "#{entity.to_s} #{entity.entityID}")
    end		
  end

  # This is the code that add Observer
  def create_Ap(
    x = 0,
    y = 0,
    z = 0,
    x_min = 1.m,
    y_min = 1.m
  )
    #
    model = Sketchup.active_model
    model.start_operation('Create Ap', true)
      #
      group = model.active_entities.add_group
      entities = group.entities
      points = [
        Geom::Point3d.new( x+x_min, y+y_min, z.m),
        Geom::Point3d.new( (x+(0.15.m)+x_min), y+y_min, z.m),
        Geom::Point3d.new( (x+(0.15.m)+x_min), (y+(0.1.m)+y_min), z.m),
        Geom::Point3d.new( x+x_min, (y+(0.1.m))+y_min, z.m)
      ]
      face = entities.add_face(points)
      face.pushpull(-0.02.m)
      face.material = "Black"
      face.back_material = "Black"
      face.set_attribute( "testdictionary", "test", 1 )
      if @node == nil
        @node = Array.new
      end
      @node.push(face)
      face.add_observer(Observer.new)
      group.add_observer(Observer.new)
      #
    model.commit_operation
    #
  end

  unless @@loaded # RUN ONCE CODE
    UI.menu('Plugins').add_item(
      Module.nesting[0].name.split('::').last
    ) { create_Ap() } # using default arguments
    @@loaded = true
  end

end

```

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [December 11, 2017, 9:40pm UTC](https://forums.sketchup.com/t/observer-for-recognize-object-moved/57414/9 "2017-12-11T21:40:56Z")

</div>

However, @d.trepiccione93, I would not create a new observer instance for every model object.  
Instead I’d create only one per model, and reuse it for all the model’s object (if possible.)  
But, I never use model level observers for more than one model. (That can “cross wires” and lead to problems and confuse the code.)

---

<div class="post-metadata">

**Author:** ![d.trepiccione93](https://avatars.discourse-cdn.com/v4/letter/d/f07891/32.png) [@d.trepiccione93](https://forums.sketchup.com/u/d.trepiccione93)\
**Post date:** [December 12, 2017, 12:00am UTC](https://forums.sketchup.com/t/observer-for-recognize-object-moved/57414/10 "2017-12-12T00:00:43Z")

</div>

Thank you so much. I will modify the code taking into account all the advice you gave me.  
Since we find ourselves, since these solids that I add to the model represent routers, if I wanted to import premade models instead of creating those ugly solids?

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [December 12, 2017, 12:09am UTC](https://forums.sketchup.com/t/observer-for-recognize-object-moved/57414/11 "2017-12-12T00:09:34Z")

</div>

> [@d.trepiccione93](#):
>
> Since we find ourselves, since these solids that I add to the model represent routers, if I wanted to import premade models instead of creating those ugly solids?

This question makes no sense in English.

Please begin a new topic (and explain better) …

* * *

… or perhaps look in the [**Extensions**](https://forums.sketchup.com/c/extensions) category.

> [@Eneroth Component Replacer](https://forums.sketchup.com/t/eneroth-component-replacer/46283):
>
> [[Eneroth Component Replacer: Chairs] ](https://www.youtube.com/watch?v=p5Q_ihFe0hM) Inspired by ThomThom’s Material Replacer and the native Paint Bucket Tool I have created a [Component Replacer plugin](http://extensions.sketchup.com/en/content/eneroth-component-replacer). The tool lets you pick up a component with an eye dropper and apply it in a paint-like fashion. In contrast to other component replacer plugins this one utilizes modifier keys to optionally replace all similar components in the same drawing context, or throughout the model. Imagine drawing a restaurant and wanting to switch the c…

> [@Cross Reference Organizer](https://forums.sketchup.com/t/cross-reference-organizer/46162/):
>
> New Plugin on the scene! Cross Reference Organizer is designed to bring SU closer to being a design environment in which team members can collaborate simultaneously. The plugin facilitates team members breaking a project into parts and and cross referencing each others’ models in their own workspace. It has a clean and simple UI that allows you to visually understand what in your model is cross referenced and how up to date it is. It solves a key problem we found when using the File/Import/R…

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/sketchup/original/3X/f/2/f2a121500b085ed60806607a82720b092d800bb3.png) [@system](https://forums.sketchup.com/u/system)\
**Post date:** [March 22, 2018, 8:49am UTC](https://forums.sketchup.com/t/observer-for-recognize-object-moved/57414/13 "2018-03-22T08:49:14Z")

</div>

Closed on March 22, 2018, 8:49am UTC

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.
