Yep, I've taken the native threads path. Got the thread running, got even an http server running in that thread. But now, GIL is killing me. Can't call ruby from that second native thread...
Posts made by SR20VET
-
RE: Ruby 2.0.0 Threads in SU | how to keep a subthread running?
-
RE: Ruby 2.0.0 Threads in SU | how to keep a subthread running?
To make sure the subthread is running, we have to put the main thread in a sleep state.
This is because Ruby does switch to another thread (if existing) when a thread goes into a sleep state.Now, is there a way to automatically put the main thread in a sleep state when the user is not interacting with SU? Once the user starts to interact (mouse movement, click, ...) the sleep state should be interrupted.
I'm not aware of such an event in SU.
For now this:
message_pump = UI.start_timer(0.1, true) do sleep(0.1) end
does help a bit, but ofcourse, this drastically affects SU performance. Even sleeping for 0.01 seconds does..
-
RE: Ruby 2.0.0 Threads in SU | how to keep a subthread running?
Ok tt_su, I might have to go the hard way then, and use native threads in a c++ extension.
-
RE: Ruby 2.0.0 Threads in SU | how to keep a subthread running?
I have the same issues with your latest code. When I simply enter the code, press ENTER, wait a couble of seconds and then type seconds, still not a proper amount of seconds.
Although it seems that, while I'm writing this post, the thread is running... while at the start of the thread I entered "seconds" a couple of times, still with about 10 seconds.
I'll try: seconds = 0; t = Thread.new(){ while true; seconds += 1; sleep(1) end }; t.run
and puts "seconds: #{seconds} and time: #{Time.now}"; a few times. Here is the result in the console, it does run, but not perfect:seconds = 0; t = Thread.new(){ while true; seconds += 1; sleep(1) end }; t.run
#<Thread:0x92a18d8 run>
puts "seconds: #{seconds} and time: #{Time.now}";
seconds: 1 and time: 2014-08-14 23:39:00 +0200
nil
puts "seconds: #{seconds} and time: #{Time.now}";
seconds: 7 and time: 2014-08-14 23:39:07 +0200
nil
puts "seconds: #{seconds} and time: #{Time.now}";
seconds: 14 and time: 2014-08-14 23:39:15 +0200
nil
puts "seconds: #{seconds} and time: #{Time.now}";
seconds: 25 and time: 2014-08-14 23:39:28 +0200
nil
puts "seconds: #{seconds} and time: #{Time.now}";
seconds: 34 and time: 2014-08-14 23:39:39 +0200
nil
puts "seconds: #{seconds} and time: #{Time.now}";
seconds: 43 and time: 2014-08-14 23:39:50 +0200
nil -
RE: Ruby 2.0.0 Threads in SU | how to keep a subthread running?
That's funny. If I close the console it does count indeed! But, when I press ENTER after entering the script, sit and wait for 15 seconds and then enter seconds, it does return 1 - 2
It look like the SU window has to have focus or a mouse hover.
-
Ruby 2.0.0 Threads in SU | how to keep a subthread running?
Hi SU,
I'm testing some multithreading in Ruby.
It seems like the main thread goes into a sleep state when the user is not interacting with Sketchup and/or no script is executing.Take a look at this:
seconds = 0; counter_thread = Thread.start do loop do seconds += 1; sleep(1); end#do end#do
If I just leave the ruby console for what it is after entering this script and don't interact with SU for some seconds, this new thread doesn't run. If I type
seconds
in the console it just says, 1 or 2. However, if I do react with SU, just move the mouse, change the camera position, ... the thread does run and the seconds gets updated.
Spawning a timer on the main thread also keeps the message pump running, but not at full "speed". After some time the seconds are incorrect.
I was wondering if there is some way to keep a subthread running, without the need to interact with SU?
-
RE: Post-its in SketchUp screen (DCExamples.skp)
draw2d and Chunky_png could accomplish the same result with some dynamic options.
-
RE: Rbs weird behaviour on modules
Ok, thanks for the workaround. You should expect that no mather you use rb's or rbs's, they behave the same way...
-
Rbs weird behaviour on modules
Today I ran into this weird behaviour when scrambling my scripts into an rbs.
Lets say I have the folowing script:
module MyModule1 def self.load_second_script Sketchup;;require 'second_script' end#def end#module
in seconds_script.rb(s) i have:
module MyModule1 module MyModule2 end#module end#module
next, I do:
MyModule1.load_second_script
While testing with rb, this eventually gives
MyModule1;;MyModule2
as a valid module.
With rbs this results inMyModule1;;MyModule1;;MyModule2
Has anyone noticed this before?
Regards
-
RE: Add items to submenu later on
@dan rathbun said:
Please (and I know I have keep scolded on this subject ...,) do not change Ruby Core classes and modules.
Test
IS a Ruby Core module (that ALSO has SketchUp API extensions added into it.)The general rule is, that there is NEVER any good reason for YOU to write code FOR YOUR OWN USE, that is not wrapped within YOUR OWN UNIQUE MODULE NAMESPACE.
So following that rule ... use
VET::Test
as your OWN custom test module.ALSO.. do not override the global method
test()
within theTOPLEVEL_BINDING
as it will override it for EVERYONE else's modules and classes.
Instead, if you wish to override thetest()
method, do it locally within one of YOUR OWN module or class namespaces.Don't worry. I use my own namespaces
Was for illustrating purposes. -
RE: Add items to submenu later on
@thomthom said:
Yea, you get a different ruby object each time. And it seem to go out of scope quickly.
They still exist in ObjectSpace though:
#ON STARTUP; menus = []; ObjectSpace.each_object Sketchup;;Menu do |obj|; menus << obj; end; menus #RESULT; [#<Sketchup;;Menu;0x99faea8>, #<Sketchup;;Menu;0xd419134>, #<Sketchup;;Menu;0xd41915c>, #<Sketchup;;Menu;0xd419274>, #<Sketchup;;Menu;0xd57f014>, #<Sketchup;;Menu;0xd57f6cc>, #<Sketchup;;Menu;0xd58219c>, #<Sketchup;;Menu;0xd583e70>, #<Sketchup;;Menu;0xd584474>, #<Sketchup;;Menu;0xd584c6c>, #<Sketchup;;Menu;0xd585a04>, #<Sketchup;;Menu;0xd585d74>, #<Sketchup;;Menu;0xd587494>, #<Sketchup;;Menu;0xd5879a8>, #<Sketchup;;Menu;0xd587a0c>, #<Sketchup;;Menu;0xd5881c8>, #<Sketchup;;Menu;0xd5881f0>, #<Sketchup;;Menu;0xd613da4>, #<Sketchup;;Menu;0xd62fd60>, #<Sketchup;;Menu;0xd62ff54>, #<Sketchup;;Menu;0xd632650>, #<Sketchup;;Menu;0xd6427f8>, #<Sketchup;;Menu;0xd6936bc>, #<Sketchup;;Menu;0xd695728>, #<Sketchup;;Menu;0xd737a3c>, #<Sketchup;;Menu;0xd7380a4>, #<Sketchup;;Menu;0x15ac140c>, #<Sketchup;;Menu;0x15b4de84>, #<Sketchup;;Menu;0x15b4deac>, #<Sketchup;;Menu;0x15b9ef64>, #<Sketchup;;Menu;0x15c87ebc>, #<Sketchup;;Menu;0x15c87f34>, #<Sketchup;;Menu;0x15cb6744>] #NAVIGATE TO PLUGINS MENU; plugins_menu = UI.menu('plugins') #RESULT #<Sketchup;;Menu;0x15a3004c> #SEARCH OBJECTSPACE AGAIN menus = []; ObjectSpace.each_object Sketchup;;Menu do |obj|; menus << obj; end; menus #RESULT [#<Sketchup;;Menu;0x99faea8>, #<Sketchup;;Menu;0xd419134>, #<Sketchup;;Menu;0xd41915c>, #<Sketchup;;Menu;0xd419274>, #<Sketchup;;Menu;0xd57f014>, #<Sketchup;;Menu;0xd57f6cc>, #<Sketchup;;Menu;0xd58219c>, #<Sketchup;;Menu;0xd583e70>, #<Sketchup;;Menu;0xd584474>, #<Sketchup;;Menu;0xd584c6c>, #<Sketchup;;Menu;0xd585a04>, #<Sketchup;;Menu;0xd585d74>, #<Sketchup;;Menu;0xd587494>, #<Sketchup;;Menu;0xd5879a8>, #<Sketchup;;Menu;0xd587a0c>, #<Sketchup;;Menu;0xd5881c8>, #<Sketchup;;Menu;0xd5881f0>, #<Sketchup;;Menu;0xd613da4>, #<Sketchup;;Menu;0xd62fd60>, #<Sketchup;;Menu;0xd62ff54>, #<Sketchup;;Menu;0xd632650>, #<Sketchup;;Menu;0xd6427f8>, #<Sketchup;;Menu;0xd6936bc>, #<Sketchup;;Menu;0xd695728>, #<Sketchup;;Menu;0xd737a3c>, #<Sketchup;;Menu;0xd7380a4>, [b]#<Sketchup;;Menu;0x15a3004c>[/b], #<Sketchup;;Menu;0x15ac140c>, #<Sketchup;;Menu;0x15b4de84>, #<Sketchup;;Menu;0x15b4deac>, #<Sketchup;;Menu;0x15b9ef64>, #<Sketchup;;Menu;0x15c87ebc>, #<Sketchup;;Menu;0x15c87f34>, #<Sketchup;;Menu;0x15cb6744>]
-
RE: Add items to submenu later on
@thomthom said:
Btw, nothing that will affect the result of the menu thing, but your sample code would make more sense as a Module:
<span class="syntaxdefault"><br />module Test<br /><br /> plugins_menu </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">menu</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'Plugins'</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> </span><span class="syntaxkeyword">@@</span><span class="syntaxdefault">my_menu </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> plugins_menu</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_submenu</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"mySubmenu"</span><span class="syntaxkeyword">)<br /><br /></span><span class="syntaxdefault"> def self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">my_menu<br /> </span><span class="syntaxkeyword">@@</span><span class="syntaxdefault">my_menu<br /> end</span><span class="syntaxcomment">#def<br /><br /></span><span class="syntaxdefault">end<br /></span>
When you don't create an instance of a class, use a module.
Ok. Is there also a reason why I shouldn't use Classes when I don't need instances? I mean does it affect something?
It also seems that:
u1 = UI.menu('Plugins'); u2 = UI.menu('Plugins'); u1==u2 ==> false
-
Add items to submenu later on
It seems that Sketchup doesn't 'remeber' its menu's...
For instance:
class Test def self.initialize plugins_menu = UI.menu('Plugins') @@my_menu = plugins_menu.add_submenu("mySubmenu") end#def def self.my_menu @@my_menu end#def end#class
From ruby console
my_item = Test.my_menu.add_item('myItem'){UI.messagebox "ok"}
Well, this doesn't work
I notices that every time i entered this in the console:
UI.menu("Plugins")
, the handler to the menu changes...
Is there any possibility to add items to a submenu long after the submenu has been created?
-
RE: Writing and reading Marshal data to attributes?
I was actually hoping to find a way to dump
Sketchup;;Material
to an attribute_dictionary
But the materials with a texture give me a headache -
RE: Writing and reading Marshal data to attributes?
I wonder if you ever found a solution on dumping Marshal'ed Sketchup Objects.
I also save my own Ruby instances as Marshaled data in an attributes dict and also use base 64 encode them. (actually just pack('m') but that's the same as base64 does# SAVING # create a dump object for each instance dumped_instances = [] @@instances.each do |inst| dumped_instances << [(Marshal.dump inst)].pack("m") end#do # save to the dictionary Sketchup.active_model.set_attribute "myDict", "mClassInstances", dumped_instances # LOADING # load all instances dumped_instances = Sketchup.active_model.get_attribute "myDict", "mClassInstances" if dumped_instances #create instances dumped_instances.each do |dump| instance = Marshal.load dump.unpack("m")[0] @@instances << instance end#do end#if
-
RE: Using win32ole on startup
@thomthom said:
For timer delay you should immediately stop the timer in it's block - because if a modal window appear within the block, like a messagebox or an error message (which you get when you start SU) will make the timer repeat until the modal window is closed.
<span class="syntaxdefault"><br />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">){<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"> </span><span class="syntaxcomment"># ...<br /></span><span class="syntaxkeyword">}<br /> </span><span class="syntaxdefault"></span>
Hi Thom and TIG, that's exactly how ì solved it. Noticed the fact that a UI.messagebox in the block will make the timer repeat too.
Never used UI.start_timer before but I can think of some nice implementations of it. -
Using win32ole on startup
Hello,
I have some difficulties on starting a WIN32OLE.new('Excel.Application') on startup of SketchUp... code I have is simple in test.rb:
require 'sketchup.rb' require 'win32ole.so' model_dict = Sketchup.active_model.attribute_dictionary "thisIsADict" xl = WIN32OLE.new('Excel.Application') if model_dict
The Error i get is:
uninitialized constant WIN32OLEI think this has something to do with me trying to start an ole connection directly on start-up. If I assign the code to a command, there is no problem...
But, the code needs to be executed directly when a specific .skp (with "thisIsADict" library) is opened...Any ideas how to solve this would be great.
Cheers!
-
Post-its in SketchUp screen (DCExamples.skp)
Hello. I was learning dynamic components today and downloaded DCExamples.skp on de SketchUp website. My attention didn't go to the dynamic components when I opened the file but to the post-it like dialogs which appear on the screen. See image in attachment.
I was just wondering if anyone knows how they are made? They must be part of the SKP-file. I've been looking for a while to make UI's that appear in the SketchUp window instead of in it's own window like webdialogs and default dialogs...
-
RE: New tool's "initialize" starts bf prev tool's "deactivate"
@thomthom said:
Do you stuff in the Tool's
activate
callback. That should trigger afterdeactivate
. Initialize triggers when you create the tool instance - not when it's activated. You can reuse a tool instance - whereactivate
is the one that will trigger when your tool activates.(How do you activate the tools btw?
model.select_tool
ormodel.tools.push_tool
?)Hi Thomthom. Man, such a stupid error of me. I did use the
initialize
method instead of theactivate
method.
Btw, I useselect_tool ToolName.new
Thanks for the answer, funny I didn't saw the error myself