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!
-
You shouldn't be adding the extension.
require 'win32ole'
is the convention.Not sure if that's the source of your issues though.
But you might have other problems: trying to access
Sketchup.active_model
on startup. Not sure if everything is available when the Ruby engine is initialized. You might want to check that. -
Does the same code work if you run it after SketchUp has started?
-
Have you tried enclosing it and then waiting until things settle down?
require 'sketchup.rb' require 'win32ole.so' module SR20VET def SR20VET.xls() model = Sketchup.active_model until model.entities model_dict = model.attribute_dictionary("thisIsADict") if model_dict xl = WIN32OLE.new('Excel.Application') ### rest of code ### end end end#def end#module SR20VET.xls()
-
Won't that loop block the rest of SketchUp? Ruby doesn't let SketchUp "breathe" when it's working - that's why the UI white outs because not even the Windows messages are given any opportunity to be processed.
-
But presumably it'll only pause briefly until model.entities in returned ?
OR it could start a timer that'd run separately ?require 'sketchup.rb' require 'win32ole.so' module SR20VET def SR20VET.xls() UI.start_timer(1,false){ model = Sketchup.active_model until model.entities model_dict = model.attribute_dictionary("thisIsADict") if model_dict xl = WIN32OLE.new('Excel.Application') ### rest of code ### end end }#end timer end#def end#module SR20VET.xls()
-
@tig said:
But presumably it'll only pause briefly until model.entities in returned?
Pretty sure it won't, because the rest of SU can't continue until the loop is complete. You might have created an infinite loop. SketchUp's doesn't run in parallel with the ruby engine. It's sequential processing.
-
I think this'll work:
require 'sketchup.rb' require 'win32ole.so' module SR20VET def SR20VET.xls() timer=UI.start_timer(0,false){ UI.stop_timer(timer) model = Sketchup.active_model until model.entities; end model_dict = model.attribute_dictionary("thisIsADict") if model_dict xl = WIN32OLE.new('Excel.Application') ### rest of code ### end }#end timer end#def end#module SR20VET.xls()
-
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="syntaxcomment"># ...<br /></span><span class="syntaxkeyword">}<br /> </span><span class="syntaxdefault"></span>
-
Added to last post for avoidance of confusion...
-
Sorry to drag this off topic, but what does win32ole do? It looks like the code is interfacing with excel? Will win32ole read and write excel files? I'm looking around here:
But I don't see an explanatiojn of what it does.
-
OLE is an interface for communicating with Objects. Excel has implemented this interface so you can communicate with Excel.
Win32OLE is a library that allows you to use OLE interfaces within Ruby.
-
[Plugin Library] Win32API and Win32OLE so files
Online reference rdocs for these 2 libraries:
-
@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.
Advertisement