@thomthom said:
Except that you can't create modal webdialogs on Mac. A modal form on the Mac only makes it stay on top of SU's window - but not modal.
He knows, we know. That's really what he wants... it to stay on top.
@thomthom said:
Except that you can't create modal webdialogs on Mac. A modal form on the Mac only makes it stay on top of SU's window - but not modal.
He knows, we know. That's really what he wants... it to stay on top.
@driven said:
I'll globalise it, and do you have any objections to me posting this ruby for others, less interested in doing it themselves... I think really handy.
Well handy yes, but not all that 'special'. There are numerous plugins around that put help links on the menus. (One by Didier and TBD that will search all folders under Plugins and create links to any help file type, like .pdf, .txt, .chm, .hlp, .htm, etc... automatically.)
There is a problem with your version. You have the new constructor inside the command block without a conditional check to only create a new instance if it has not yet been done. Repeated clicking on the menu would create extra WebDialog objects, wasting space on the stack.
Either move the constructor statement before the 'helpmenu.add_item' line, or add
' get_dialog=false
' before it and change the constructor inside the {} block to:
' get_dialog = UI::WebDialog.new if not get_dialog
'
Probably the 2nd is best, so the WebDialog object doesn't get created unless the menuitem is actually clicked.
@driven said:
the next thing I've been attempting is to make this RubyCodeEditor to .show_modal.
This is something Alex can do when he gets the User Preferences options part done. He can add an option for Mac users to check a box and then put a conditional statement in the code.
Some thing like:
if RUBY_PLATFORM.include?('darwin') %(#F0F0F0)[__]OPTIONS['MacShowModal'] ? show_modal() : show() else # it's a PC %(#F0F0F0)[__]show() end #if
or you can just change the show to show_modal near he end of the code (for your copy, til Alex gets to releasing his next version.)
@driven said:
@Dan, thanks a lot for that, I went round in circles trying to get the pdf file to load using #{ENV['HOME']} but eventually just dragged the file in with out that there and it opens the doc on top of SU, but can I get it to stay on top while I use SU? I want to copy paste bits and bobs straight into ruby console.
No problem!
Don't worry about copyright. It's so simple and really just slightly modified version of the code snippet from the API. Assigning a copyright would be like tying to patent the chemical formula H2O.
Apparently the UI.openURL argument requires (on the Mac,) the protocol 'file://localhost' before the HOME variable. For a trully generic loadstring (if you were to change your username,) this should work as well (all one line):
UI.openURL("file://localhost#{ENV['HOME']}/Documents/Learning_rubies/TextBook.pdf")
URLs work slightly different on PC and Mac.
Anyway... it was a good learning exercise for you.
@driven said:
EDIT: I was digging around in systems library and came across this, can anyone shine a light on it's meaning and if it may be causing any of the many Mac ruby glitches that rear their heads.
Notes on building the stand-alone reader.
That looks like it refers to the C++ files for building the SKP Reader extension for a third-party application. So those 'issues' are C++ issues with the SKP Reader SDK, not Sketchup itself or the SU Ruby API.
I think the problem is the API is not right (or explaining things the way they work.)
Sketchup.find_support_file and Sketchup.find_support_files do not return the same format.
One return a pathname with the Rubyish forward slash as file separator.
The other returns a Win32 like pathname with escaped backslashes, ie 'C:\Program Files\Google' etc.
The example for WebDialog.set_file is showing:
dialog.set_file "c:\\mypage.html"
The only other thing I'd recommend is NOT using + to concat pathnames.
Use the Ruby function File.join whenever possible if forwardslash is to be the file separator.
@chris fullmer said:
Sketchup.select_tool(*toolclassobject*)
(in 3 different places.)
Chris of course, meant to say:
Sketchup.active_model.select_tool(*toolclassobject*)
I disagree with putting the argument Toolclass.new in the select_tool method without a symbol.
It can easily lead to newbie's creating multiple instances of the tool (when only 1 is ever needed.) And without a symbol (variable) pointing at the tool, you can't easily access it to call public methods.
What public methods? Well I do something a bit weird. I define the tool's UI::Command object INSIDE the tool itself, within the initialize method. ( So the block for the command is {Sketchup.active_model.select_tool(self)}.) I then create several public methods, one called cmd that returns the UI::Command object so any of it's methods can be called, such as .menu_text; also an add_to_toolbar( toolbar ) and an add_to_menu( menu ) method, so the tool can be added to any number of menus or toolbars, after the fact, using the proper menutext and toolbar button images. I also have a set of params for the .new call, that includes (optionally) a menu (defaults to "Plugins") and/or toolbar which the tool will add itself to (internally calling the previously mentioned methods.)
@chris fullmer said:
The
activate
method is called everytime the tool is instantiated. So it is a great place to put the first processing of your script - validation, tool process flow, etc.
Should read:
"The **initialize**
method is called ONCE whenthe tool is instantiated. So it is a great place to put the first processing of your script - validation, tool process flow, etc."
The **activate**
method is called everytime the tool is selected (from the menu or a toolbar button [via the Model.select_tool
method.]) This method usually sets the @tool_state variable to it's starting value (often 0,) loads the tool's cursor, and prompts the user to do something on the status bar.
Other tips on methods:
You can (like any class,) define whatever private methods you wish, in addition to the predefined callback methods, in order to keep your code readable and easier to debug.
A reset method often helps keep things clear (some people just call activate
internally instead, but I prefer a standalone reset
method.)
@remus said:
The original idea was to check lots of points in the bounding box of the object and see what proportion of them are in the volume, then take that proportion of the volume.
IF the object is box-like, it would be very easy.
pt=[1,2,3] bb=obj.bounds in_volume=bb.contains?(pt)
Unfortunately, not all volumes can be box-like and aligned with the x,y,z axes.
@dan rathbun said:
Anyhow.. an ini (or any settings file,) would give you complete freedom.
It doesn't need to be an ini format. It can be simply a ruby script that decalres a Settings Hash wrapped in your plugin namespace. ... that gets loaded by the first (if it exists,) and gets RE-written by the first file, when changes to the settings are made.
If Js is more comfy for you, the same technique can be used but in Js. A ruby method can write out a Js script that is a JS object holding settings, or an Array of settings, or global varibales, whatever.
The file just gets loaded with the webpage.. no complex parsing required.
If the user makes settings changes.. you send them to a ruby callback that overwrites the settings .js file.
Also if you wish to change CSS dynamically you'll need to assign ID attributes to the stylesheets (whether inline or loaded via LINK tag.)
@thomthom said:
@dan rathbun said:
Also.. XP Home has a Registry limit size (I'm constantly on the edge of it and often get the Registry Limit reached Warnings. If I'd known about it I would have paid the extra and bought XP Pro.)
Never knew that.
...how much stuff you got installed? I've installed lots of random stuff on my old xp box through up the years - never had that warning.
TOO much stuff! OpenOffice Suite, AutoCAD, Epson Scanner Software, Windows SDK, MS VisualStudio (w/ VB, C#, C++, SQL Server), Debugging Tools for Windows, Windows Support Tools, PowerToys for Windows XP, MS HTML Help Workshop, PCB123, AdobeReader, Paint.NET, NotePad++, SciTE, FamilyOrigins (Genealogy dBase), Google Sketchup 7.x, Google Earth. ..etc...
(And at one time 2 full versions of MS Flight Simulator; which I uninstalled.)
I believe I didn't get the errors until after I installed AutoCAD. I can't find the exact error message at a MS search, but did find an article dated 2007 that says the RSL (Registry Size Limit) "no longer applies" to Windows XP or Windows Server 2003. But I know I have got the error popup after that.
@Alex: Thinking more about this. You don't need need to worry at all regarding this issue. The RSL applies only (if it even does anymore,) to the System Hive of the Registry. Sketchup settings are saved in the User Hive, which does not have (or never had) any size limit.
@cjthompson said:
Do you know which version of Ruby you took the Win32api from?
Just as a head's up, so you'll know... Win32API.so is not distributed with Ruby 1.9.x and up. It is replaced by Win32API.rb, which is a translator script, that 'fools' old calls to the Win32API.so, by translating those calls into DL library calls.
It is supposed to work transparently, because most uses would load the so by simply:
require 'Win32API'
The require method looks for .rbfiles first, so it would load the new translator, instead of the .so, ... in a normal ruby install.
@thomthom said:
Cookies can be purged by the user or utilities that clean the browser data. Safest is to store it separately.
Agreed.
You can use Sketchup.write_default and Sketchup.read_default to save settings in the Registry (Win32) or plist files (Mac) but they have a few disadvantages.
Once you create a key, you cannot remove it, so they can become cluttered with old setting keys that are not used anymore.
And they can be only 1 level of heirarchy, even though the Windows Registry supports any numbers of levels. (It's a tree structered database.) Perhaps this is a limitation of the Mac plists and Google is enforcing it on both platforms for 'sameness'?
Also.. XP Home has a Registry limit size (I'm constantly on the edge of it and often get the Registry Limit reached Warnings. If I'd known about it I would have paid the extra and bought XP Pro.)
Anyhow.. an ini (or any settings file,) would give you complete freedom.
It doesn't need to be an ini format. It can be simply a ruby script that decalres a Settings Hash wrapped in your plugin namespace. Did you know that Module and Class definitions can be split up into multiple files? They can. The main file can be the functional part of the class definition, and the other can be just a Hash declaration inside the same class namespace, that gets loaded by the first (if it exists,) and gets RE-written by the first file, when changes to the settings are made.
Sketchup::load
The Sketchup::load method does NOT expose the wrap argument, so we can specify wrap=true for rbs scripts.
For some unknown reason, the Google team defeated, or just didn't pass the 2nd argument (wrap) on to the aliased standard load, when they overrode it to handle rbs decrypting.
Please fix this!
_
@jim said:
@jessejames said:
I noticed when i do obj.methods, and obj.private_methods, there is a lot of pollution in there. Names that obviously don't belong to that obj.
I wouldn't call it pollution. You're seeing all the methods that the object has. Some of them are inherited, some are class methods, others are instance methods. I suspect what you expected to see were only the instance methods of a class.
This is VERY important! For understanding Ruby,... but more so.. how the Sketchup Ruby API (and standard Pure-Ruby extensions (such as 'sketchup.rb' [as the best example,]) can be incorrectly implemented.
Ruby scripts... (if you don't tell them specifically what namespace to run in,) will run in (the global constant namespace,) TOPLEVEL_BINDING. (A Binding class object, is 'sort-of' an execution directive to a namespace.)
(1) TOPLEVEL_BINDING is preset to Object (uppercase.)
(2) Everything in Ruby is an object (lowercase,) meaning it is either an Object class, or a subclass (at some level of descendancy,) of Object class. [You may think of the lowercase word 'class' as 'type' if you come from a language where 'type' is more understandable. But recent revisions to Ruby are attempting to remove the word 'type' in favor of 'class.' Keep in mind that there is in Ruby a class Class, which is subclass of class Module, which is subclass of class Object.]
(3) The Sketchup Ruby Console operates in TOPLEVEL_BINDING. (Open the console and type: self <ENTERkey> , what do you see?
(4) Any script loaded runs in TOPLEVEL_BINDING.
(5) This means that most objects defined in TOPLEVEL_BINDING are in fact then defined WITHIN class Object. So if you define a method (without wrapping it with a Class or Module namespace, it is defined as a method of class Object. (Take a look at the 'sketchup.rb' script in the Tools folder and note the names of the methods defined there.)
(6) As every object in Ruby is a subclass of Object, every object will inherit (in some way,) those objects defined within class Object, including methods, constants, etc. IF you have incorrectly defined them there.
(7) The methods within the 'sketchup.rb' script were intended (for the most part,) to extend developer's use of the classes Module and Class. BUT they were not wrapped within the proper namespace(s) in the script, and so get defined as methods of Object, and so become inherited by ALL classes, such as the Numeric classes, the Sketchup::Entity subclasses, the Geom::subclasses, etc. where they will NEVER be used.
Open a new model, draw something simple a cube, select a Sketchup::Face object. Switch to the console, type (all 1 command):
**Sketchup.active_model.selection[0].private_methods(true).sort.join("\n")**
Look at the list, you will see that the Face object has inherited as private methods (which you will never call from outside the object,) all those methods defined in 'sketchup.rb'!
What kind of drain of memory will this have when you get to a model that has tens of thousands of Entities it it?
"How do you fix this?"
(a) ANSWER: 'sketchup.rb' MUST DIE! The methods within NEED to be moved (defined as an extension,) to the proper %(#BF0000)Module where they should have been added in the first place:
to module Sketchup:
file_loaded* file_loaded?* require_allto module UI:
add_separator_to_menu* inputbox (really a patch that needs to be made part of UI.inputbox.)
(b) Edit any script you use, that does not wrap it's code in a module namespace, especially method definitions with a module wrapper. Don't use any rbs (scrambled ruby) that you cannot wrap. (Contact the author and ask they wrap their code.)
(c) If you can.. use the load method with the wrap parameter set true, for menu command scripts, etc. when editing many many plugins is not an option. (The wrap argument for load will wrap the script in an anonymous namespace, and protect the global namespace from corruption.)
(d) Ask the Google Sketchup Team to fix the Sketchup::load method to expose the wrap argument, so we can specify wrap=true for rbs scripts. For some unknown reason, they defeated, or just didn't pass the 2nd argument (wrap) on to the aliased standard load, when they overrode it to handle rbs decrypting.
_
Just a note on personal preference.
If you use the META tag I gave you previously, the buttons (New, Open, Save, Quit) take on the look of the nice XP style buttons (rounded corners & which hilite on hover, etc.) They look just as they do in native dialogs.
The jQuery buttons you have used take up more space (especilally height-wise) and have the look of the old MS Frontpage styling, which I have always hated.
I prefer the native XP look.
I don't think it's an SU bug, it's probably a browser cache quirk.
I wonder if the image table was enclosed inside a FORM block, would the Js method %(#BF00BF)[form.reset]
cause the images to refresh?
Alex.. you did not change the Name of the DefaultSettings Key ('WebCon'), so your plugin is overwriting the values for Jim's standard WebConsole in the registry.
There is always the possibilty that some people may still wish to use Jim's "plain Jane" version from time to time, even with yours installed.
You should setup a unique Keyname for your defaults.
I think I see another problem. If an error occurs don't you need to abort the operation instead of committing it?
Try something like this:
add_action_callback("exec") do |webconsole_dialog, p|
v = webconsole_dialog.get_element_value('console').strip
# puts v # what's this for -- testing ??
r = nil
begin
Sketchup.active_model.start_operation "RubyEditor"
begin # evaluation
r = eval v
rescue => e
r = e
raise # pass to outer rescue clause
end # eval
rescue
Sketchup.active_model.abort_operation
else # only do if NO errors
Sketchup.active_model.commit_operation
ensure # always do this
r!=nil ? r = r.to_s ; r='nil'
p r
r.gsub!(/ /, " ")
r.gsub!(/\n/, "<br>")
r.gsub!(/'/, "’")
r.gsub!(/`/, "‘")
r.gsub!(/</, "<")
webconsole_dialog.execute_script("document.getElementById('results').innerHTML='#{r}'")
end
end # callback
@dan rathbun said:
(2) The puts method returns nil so you won't see anything if Jim's code stripped textstring "nil" out.
OK if a var evaluates to nil, and then you convert it to a String with .to_s, the result is an empty length String.
So line 73 needs a nil test, thus:
r!=nil ? r=r.to_s : r='nil'
(Don't just have "r" as the boolean expression, because false values would get set to 'nil' instead of 'false'.)