Paste between instances and onLayerAdded issue
-
Hi guys
I've come across an annoying issue with layersObservers.
I have a layersObserver doing stuff when a new layer is added. The operations are wrapped in start_ and commit_operation, and I use a timer to avoid doing stuff right inside the observer (TT's advice), like this :<span class="syntaxdefault">module Test<br /><br /> class TEST_layersObserver </span><span class="syntaxkeyword"><</span><span class="syntaxdefault"> Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">LayersObserver<br /><br /> def onLayerAdded</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">layers</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> layer</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> timer </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">start_timer</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> false</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault"> UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">stop_timer</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">timer</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">start_operation</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Add layer"</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment">#some code<br /></span><span class="syntaxdefault"> Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">commit_operation<br /> </span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault"> end</span><span class="syntaxcomment">#def<br /></span><span class="syntaxdefault"> <br /> end</span><span class="syntaxcomment">#class<br /></span><span class="syntaxdefault"> <br /> Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">layers</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_observer</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">TEST_layersObserver</span><span class="syntaxkeyword">.new)<br /><br /></span><span class="syntaxdefault">end</span><span class="syntaxcomment">#module </span><span class="syntaxdefault"></span>
No issue when adding a new layer manually or via ruby, but when I try to copy some geometry from another instance of SU, being on a layer that is not already existing in the new file (The one where I want to paste), it fails.
When pasting, SU tries to add the non-existing layer (sometimes I even see it flashing in the layers window), and goes right back to the previous tool. No layer added, no geometry added.
It works fine without a timer, or without start/commit.You can try the snippet above, it fails. (If you have Layers Panel installed, disable it beforehand)
This is really frustrating...
-
Have you tried pasting with only one new layer?
I just checked in my code, and as you probably know, onLayerAdded is called for each new layer. This may be causing issues because you may have more than one start_operation beginning but not yet commited as the multiple calls to onLayerAdded are made.
I'd try puts statements inside onLayerAdded and also inside start_operation and see if that's the issue.
Greg
-
Layers can also be added when a user inserts a component from their library or the 3D-Warehouse.
-
FYI...
Test
is a standard Ruby module, into which the SU Dev Team added a set of undocumented test methods.Suggest you nest YOUR "Test" module inside YOUR toplevel namespace.
ie:
module JBB module Test # code end end
-
This happens with only one layer (Actually I haven't tested with multiple)
And of course, I test this in my own namespace, I added "Test" only to post the snippet here.
-
Nervermind, it works when I put the timer inside the start/commit_operation...
-
That operation should be transparent...
-
It is in my code. I just simplified everything to paste this snippet.
Advertisement