by Matt666 » Thu Oct 09, 2008 4:35 pm
Hi all ! I try to understand and to use observer tools in ruby. I started a very, very, very simple script, but I'm already lost !! Here is the script : - Code: Select all
class ObsTest < Sketchup::EntitiesObserver def onElementAdded(e) puts e.typename end end
def observerTest Sketchup.active_model.active_entities.add_observer(ObsTest.new) end
I doooon't understand what's the problem !! Ruby console gives me this error : Error: #<ArgumentError: wrong number of arguments(2 for 1)>(eval):9:in `onElementAdded'
Can you help me please ??
-
Matt666
-
- Posts: 829
- Joined: Wed Dec 05, 2007 8:38 am
- Location: 48.1184, -1.675
- Name: Matt
-
by herodes » Thu Oct 09, 2008 6:17 pm
As it seems, the EntitiesObserver.OnElementAdded receives two arguments... The first argument is an Entities class (sort of like an array) that contains all the all the Entity objects in the model. The second argument is the Entity that was added and thus triggered the call to the Observer mechanism. Give the following a spin,... It should explain itself in the Ruby Console... - Code: Select all
class ObsTest < Sketchup::EntitiesObserver def onElementAdded(entities, last_entity_created) i=0 entities.each{|entity| puts "entity[#{i}]: #{entity.class}" i+=1 } puts "entities : #{entities.class}" puts "last_entity_created : #{last_entity_created.class}" end end
Sketchup.active_model.active_entities.add_observer(ObsTest.new)
-
herodes
-
- Posts: 31
- Joined: Tue Jan 29, 2008 7:40 pm
- Location: Rethymno, Crete, (Hellas)
- Name: Herodes
-
by Matt666 » Thu Oct 09, 2008 8:34 pm
Aaah thank you Herodes !!!
-
Matt666
-
- Posts: 829
- Joined: Wed Dec 05, 2007 8:38 am
- Location: 48.1184, -1.675
- Name: Matt
-
by Matt666 » Fri Oct 10, 2008 7:19 am
Hi ! This very (very) little script below works fine with entities created with native SketchUp tools... - Code: Select all
class EntitiesObsTest < Sketchup::EntitiesObserver def onElementAdded(ents,e) puts "Last entity created : #{e.class}" end end def observerTest Sketchup.active_model.entities.add_observer(EntitiesObsTest.new) end
But when I try to create some entities with mirror script, or create a line with ToolsOnSurface script, SU generate a bug splat !! Can you help me again, please ?
-
Matt666
-
- Posts: 829
- Joined: Wed Dec 05, 2007 8:38 am
- Location: 48.1184, -1.675
- Name: Matt
-
by herodes » Fri Oct 10, 2008 7:56 am
I would suggest trying something using the ModelObserver.OnTransactionEnd function. See it here
-
herodes
-
- Posts: 31
- Joined: Tue Jan 29, 2008 7:40 pm
- Location: Rethymno, Crete, (Hellas)
- Name: Herodes
-
by RickW » Fri Oct 10, 2008 8:39 am
You need to prevent the observer from doing anything when using a ruby to modify the geometry, otherwise you will get the bug splat every time (at least in my experience). Google is aware of the issue.
Herodes' suggestion may work as well (or in addition).
-
RickW
-
- Posts: 770
- Joined: Fri Nov 16, 2007 6:38 am
- Location: Wichita, KS
- Name: Rick Wilson
-
by Matt666 » Fri Oct 24, 2008 7:02 am
Google is aware of the issue.
Hi Rick ! Ok, that's not a code problem !! I would suggest trying something using the ModelObserver.OnTransactionEnd function. See it here
Hi Herodes ! Thank you for your help. But I don't understand how to use this observer... Do you have any example, please? Thank you !!
-
Matt666
-
- Posts: 829
- Joined: Wed Dec 05, 2007 8:38 am
- Location: 48.1184, -1.675
- Name: Matt
-
by Matt666 » Thu Apr 02, 2009 9:19 am
Hi! Sorry for the delay! And sorry for all these questions below I dig up this old post. Rick, you said observer must do nothing when entity is modified/created by ruby.  Is it the only solution? Google is aware of the issue.
And they found a work around? or else? Do you have some news? I am writing a ruby that push each entity in a layer by its typename. First it moves all existing entities, and next it uses an entity observer to move each new entity into its layer. Naturally (and sadly) it bugs with entities created by ruby... SU 7 has same bug.. Do you have any other solution, please?  I don't understand Herodes suggestion... What ModelObserver.OnTransactionEnd can do? PS: I've attached a code that shows the problem. Load it and enter Test.start_E in the console. Let console opened. Script puts each entity's class. It works great with native drawing tools. But It bugs with ruby drawing tools (like Fredo6's Tools on surface).
Please, register (free) to access all the attachments on the forums.
-
Matt666
-
- Posts: 829
- Joined: Wed Dec 05, 2007 8:38 am
- Location: 48.1184, -1.675
- Name: Matt
-
by thomthom » Thu Apr 02, 2009 9:47 am
Check out the new APi docs instead: http://code.google.com/intl/nb/apis/ske ... erver.htmlModelObserver.OnTransactionEnd doesn't exist as you see in the new docs. I also poked around in the class just to make sure it was missed from the docs, and this is the only events: - Code: Select all
onActivePathChanged onAfterComponentSaveAs onBeforeComponentSaveAs onDeleteModel onEraseAll onExplode onPlaceComponent onSaveModel onTransactionAbort onTransactionCommit onTransactionEmpty onTransactionRedo onTransactionStart onTransactionUndo
On a sidenote: I've come across a few observers that doesn't work. I've reported them in this thread: viewtopic.php?f=180&t=17047
-

thomthom
- Global Moderator
-
- Posts: 17630
- Joined: Tue Nov 13, 2007 12:47 pm
- Location: Trondheim, Norway
- Name: thomthom
- Operating system: Windows
- SketchUp version: 8
- License type: Pro
- SketchUp use: architecture
- Level of SketchUp: Advanced
-
by thomthom » Thu Apr 02, 2009 9:51 am
I think ModelObserver.onTransactionCommit is the event you want.
-

thomthom
- Global Moderator
-
- Posts: 17630
- Joined: Tue Nov 13, 2007 12:47 pm
- Location: Trondheim, Norway
- Name: thomthom
- Operating system: Windows
- SketchUp version: 8
- License type: Pro
- SketchUp use: architecture
- Level of SketchUp: Advanced
-
by Matt666 » Thu Apr 02, 2009 10:47 am
Hi thomthom! And thank you for your quisk answers! I have a matter with all onTransaction.... I don't know how to use them... The problem is I get a bug splat with because of entities created/modified by ruby. Why can these functions (onTransaction...) help me??? Therefore, The questions are : - Is these functions can stop bug splat with Test.start_E?
- Can I return class of an entity modified/created by ruby, qith entity-observers functions?
-
Matt666
-
- Posts: 829
- Joined: Wed Dec 05, 2007 8:38 am
- Location: 48.1184, -1.675
- Name: Matt
-
by thomthom » Thu Apr 02, 2009 11:27 am
You can use that to detect when a plugin starts and commits and operation. Holding of your own actions until the operation is complete. If I understand this right, that's what might be causing the problems.
-

thomthom
- Global Moderator
-
- Posts: 17630
- Joined: Tue Nov 13, 2007 12:47 pm
- Location: Trondheim, Norway
- Name: thomthom
- Operating system: Windows
- SketchUp version: 8
- License type: Pro
- SketchUp use: architecture
- Level of SketchUp: Advanced
-
by Matt666 » Thu Apr 02, 2009 1:18 pm
Ok, thank you Thomthom. I see how these functions could help me. I try with code below, and none of onTransaction... functions works... Is it normal... Am I stupid? I really really don't understand the matter... Can you help me thomthom, please??? Thank you!
Please, register (free) to access all the attachments on the forums.
-
Matt666
-
- Posts: 829
- Joined: Wed Dec 05, 2007 8:38 am
- Location: 48.1184, -1.675
- Name: Matt
-
by thomthom » Thu Apr 02, 2009 1:54 pm
I haven't used the transaction events myself yet. Can't give an answer from the top of my head. I'd have to poke around first. I hope they are all working. I thought they might be useful for a plugin I'm doing myself.
-

thomthom
- Global Moderator
-
- Posts: 17630
- Joined: Tue Nov 13, 2007 12:47 pm
- Location: Trondheim, Norway
- Name: thomthom
- Operating system: Windows
- SketchUp version: 8
- License type: Pro
- SketchUp use: architecture
- Level of SketchUp: Advanced
-
by Matt666 » Fri Apr 03, 2009 8:32 am
Ok thank you thomthom! I'm trying with other ways, I'll tell you if they work... 
-
Matt666
-
- Posts: 829
- Joined: Wed Dec 05, 2007 8:38 am
- Location: 48.1184, -1.675
- Name: Matt
-
by HPW » Fri Apr 03, 2009 2:47 pm
Are the here metioned observers helpfull to get a OnImport event? I would be intrerested to start a houskeeping script whenever I import one of our Autocad.DWG (Making a layer purge/layer switching and building groups from hierarchy-info contained in blocknames)
-
HPW
-
- Posts: 126
- Joined: Sat Mar 28, 2009 9:19 am
- Location: Germany
by TIG » Fri Apr 03, 2009 3:45 pm
HPW wrote:Are the here metioned observers helpfull to get a OnImport event? I would be intrerested to start a houskeeping script whenever I import one of our Autocad.DWG (Making a layer purge/layer switching and building groups from hierarchy-info contained in blocknames)
Sounds like your rewriting parts of my XrefManager for me ?
TIG
-

TIG
- Global Moderator
-
- Posts: 13992
- Joined: Mon Nov 12, 2007 7:24 pm
- Location: Northumbria UK
- Name: TIG
- Operating system: Windows
- SketchUp version: 8
- License type: Pro
- SketchUp use: architecture
- Level of SketchUp: Advanced
by HPW » Fri Apr 03, 2009 8:11 pm
Sounds like your rewriting parts of my XrefManager for me ?
Nope, we have a block-structure of artikels with a master-slave relation ship build on EED-Data which gets lost during export/import. (As far as I know, or does Sketchup supports EED?) And it would make sense to rebuild the structure on a group-base. I found your nice ComponentReporter+.rb here and get the list of imported components. Now I have to start with ruby (My main language ist still autolisp) and have to parse the component-names to lists and get some group building code to work with the components which belong to the new group (which then must be named with the same name as the first entry). So still lots to learn! Hans-Peter
-
HPW
-
- Posts: 126
- Joined: Sat Mar 28, 2009 9:19 am
- Location: Germany
by TIG » Sat Apr 04, 2009 10:02 am
Almost anything in SUp can be given 'attributes' =~ 'extended data' ? You could export the blocks' xdata from AutoCAD using AutoLisp: putting it into a .csv file that'd list the block-name/instance/location/xdata etc... Then you could write a matching Ruby to use on it later... After importing the AutoCAD file, its various blocks will come in as components - they should keep the same names and relative positions etc. Run the Ruby on the corresponding .csv file and add the xdata into each matching component-definition/instance as attributes. If the blocks interact you need to write a Ruby that perhaps 'observes' instances and finds/reads/writes the appropriate corresponding attributes and takes the required actions etc etc... At least if you have AutoLisp experience exporting the xdata in a usable format for Ruby would be relatively straightforward...
TIG
-

TIG
- Global Moderator
-
- Posts: 13992
- Joined: Mon Nov 12, 2007 7:24 pm
- Location: Northumbria UK
- Name: TIG
- Operating system: Windows
- SketchUp version: 8
- License type: Pro
- SketchUp use: architecture
- Level of SketchUp: Advanced
by HPW » Sat Apr 04, 2009 12:46 pm
At least if you have AutoLisp experience exporting the xdata in a usable format for Ruby would be relatively straightforward...
Thanks for your thoughts about extending such interface. I had thought about this for myself, but I have to make steps one after another. So first I want to get the structure in place again and then I can thought about adding/transporting more Info. The AutoLisp experience is not the problem, since I am a nearly full-time autolisp-developer and I am really used to do anything with ExtendedEntityData. We have logical propertys and geometric propertys stored in EED and even small chunks of Lisp-code stored there, which gets fired under certain events. So I can imagine I could do similar things in the future with ruby.
-
HPW
-
- Posts: 126
- Joined: Sat Mar 28, 2009 9:19 am
- Location: Germany
Return to Developers' Forum
|