• Login
sketchucation logo sketchucation
  • Login
ℹ️ GoFundMe | Our friend Gus Robatto needs some help in a challenging time Learn More

Help with Observers

Scheduled Pinned Locked Moved Developers' Forum
7 Posts 3 Posters 1.1k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    Didier Bur
    last edited by 19 Dec 2007, 09:02

    Hi all,
    Anyone knows how to use observers, or have an example code file ?
    There's quite NOTHING usable in the help pages(always the same content, no arguments given, no syntax, no return values...), and very few posts about that in the GGroups.
    I wonder why Google provides new classes with such a lack of documentation ❓

    DB

    1 Reply Last reply Reply Quote 0
    • T Offline
      TIG Moderator
      last edited by 19 Dec 2007, 10:27

      Here's some example code:

      ###
      class FaceWatch < Sketchup;;EntityObserver
       def onChangeEntity(entity)
         UI.messagebox(entity.to_s+"; Face Changed!")
       end#def
       def onEraseEntity(entity)
         UI.messagebox(entity.to_s+"; Face Erased!")
       end#def
      end#class
      pts=[[0,0,0],[100,0,0],[0,100,0]]
      face=Sketchup.active_model.entities.add_face(pts)
      face.add_observer(FaceWatch.new)
      ###
      

      Clearly the messages are only to show what it's doing - you can add other actions instead, such as affecting some other related objects ("attributed" together)... E.G. A window has an observer, you move the window and its associated reveal faces move too: the reveal faces each have an observer and if you move one the others move with it (like it's a group that's not a group !) AND their associated window moves too...

      TIG

      1 Reply Last reply Reply Quote 0
      • D Offline
        Didier Bur
        last edited by 19 Dec 2007, 11:20

        Thank TIG for the reply.
        One more question 😳 : how to associate reveals faces to the component instance, since there is no persistent entityID which can be set as an attribute to the component instance ?

        DB

        1 Reply Last reply Reply Quote 0
        • A Offline
          azuby
          last edited by 19 Dec 2007, 14:43

          Also have a look for the Sketchup::AppObserver - it's a good point to start with for understanding how they are working. You write your own class, which inherits from the Sketchup::AppObserver. Sketchup::AppObserver provides some empty methods, which will be called, when

          • a new model gets opend
          • a model gets loaded
          • on program exit

          These methods do nothing by default. But in your class you can overwrite (redefine) them. Because objects of your class also are Sketchup::AppObserver, they will be notified on changes. So i.e. a model gets opend, all Sketchup::AppObservers get the message "model opend" (their method for that is called by Sketchup).

          You need to register your Sketchup::AppObserver object to Sketchup with the line

          Sketchup.add_observer <MY_OBSERVERCLASS>.new # for simple constructors
          

          That's all.

          azuby

          *error initus :: Blocks | CurrentDate | d/Code | extensionmanager | FFlipper | HideEdges | MeasuredArea | ModelHistory | PluginsHelp | PronButton | SAWSO | SCP | SU²CATT

          Bad English? PM me, correct me. :smile:**

          1 Reply Last reply Reply Quote 0
          • D Offline
            Didier Bur
            last edited by 19 Dec 2007, 20:17

            Thanks all for your help, but I have the feeling that what I want to achieve will not be easy... 😒

            DB

            1 Reply Last reply Reply Quote 0
            • T Offline
              TIG Moderator
              last edited by 19 Dec 2007, 21:23

              @didier bur said:

              Thank TIG for the reply.
              One more question 😳 : how to associate reveals faces to the component instance, since there is no persistent entityID which can be set as an attribute to the component instance ?

              You can give the compo'instance an attribute and match this to the reveal's faces attributes, if you move it (observer kicks in)... it looks for any faces (reveals) with matching attributes and moves them too...
              Conversely if one of the reveal faces moves it too has attributes linking it back to the other related reveal faces and the compo'instance, by their matching attributes, and it moves them to suit too...
              Have an attribute for all related things... say it's called 'Cutter' and give it a random number key '1234567' when they are first connected by the tool.
              The window has the attribute 'Cutter' set to '1234567' and when the observer does its stuff the faces in the model are scanned for attribute 'Cutter' and IF its set to '1234567' it's a hit and they are moved to suit. This way all reveal faces and compo'instance are connected across sessions.
              You could also have a checksum in the key to check that there are still the original number of faces etc e.g. 4 faces >>> [4,1234567], when moving something IF the face count <4 then remove all of the face and compo'instance attributes so they're disconnected for good... OR try to make the missing face(s) and refix to suit ? - rather like erasing the wall face that the window is in... making a new window won't cut the face unless it's reglue'd (see my script fot this bit)...
              The 'Cutter' attribute can also containe the base face linked with an attribute if this helps heal or reglue, move reveals later on etc...

              TIG

              1 Reply Last reply Reply Quote 0
              • D Offline
                Didier Bur
                last edited by 20 Dec 2007, 09:37

                Aaaah TIG it's good to have your brain beside me... 😄
                Thanks (and merry Christmas) !

                DB

                1 Reply Last reply Reply Quote 0
                • 1 / 1
                1 / 1
                • First post
                  1/7
                  Last post
                Buy SketchPlus
                Buy SUbD
                Buy WrapR
                Buy eBook
                Buy Modelur
                Buy Vertex Tools
                Buy SketchCuisine
                Buy FormFonts

                Advertisement