@michaelwhksg said:
My next challenge will be the licensing issue.
Please start a new topic, or find one that is already on this subject.
@michaelwhksg said:
My next challenge will be the licensing issue.
Please start a new topic, or find one that is already on this subject.
They have been known to be slow. Perhaps it's the summer vacation season ?
Ah okay, 3 years and two months ago, Fredo was noticing these same issues in the following topic thread:
onKeyDown: getting the charcater value from key
In that thread, post # 9, I noted several other related topic threads, and the one that I just remembered by Jim Foltz, which was back in 2009.
flags on onKeyDown?
(Note that at this time Chris Fullmer was not yet a member of the SketchUp Team, and had not yet learned much programming.)
See other related threads:
Now, @Anton_S posted a Windows only method that calls Windows API C functions, in this post:
http://sketchucation.com/forums/viewtopic.php?f=180%26amp;t=57450%26amp;p=522915%26amp;hilit=keytest#p522915

@ittayd said:
All of that doesn't help me in understand the values of the flags parameter which in the case of onKeyDown/onKeyUp doesn't make sense to me.
You know, ... I've been involved with SKetchUp Ruby for like 10 years, and the docs are still very poor. Also there has never been a public API BugTracker, so after all this time I have lost track of all the bugs, which ones have been fixed, and which ones haven't.
But this thread has jogged my memory a bit. I seem to remember an old thread here, (perhaps started by Jim Foltz, in which we discussed funky issues with the key callbacks in the abstract Tool class. I think he even stick a test script in it named "KeyTest.rb" ? I think in it we all agreed that onKeyDown had bugs, and you had to rely more on onKeyUp.
I did a bit of testing (SU2016), and I do see that the repeat arg still is not correct in onKeyDown.
I'll see if I can find that old thread.
@hank said:
So it sounds like really what I am doing is saying
give me entities whose layer property is 'X'
Right?
YES.
And this is almost what you are doing in the nested iteration loops above.
Except that you are being inefficient. You are creating a new literal string in each loop, and doing SLOW string comparison in each loop. You should do fast object comparison.
Outside and before the loops, check the layers collection for a layer named "OPENING_PRIMITIVES", and get it's object reference in a local variable. Then use that reference for comparison inside the loop.
if defn_inst.layer == target_layer
Another thing you should do is not assume the entity subclass type inside the loop.
(You use the ref name defn_inst, but I will use obj in this example, because there are many different subclasses of entity in the entities collections.)
Always have a "bail out" conditional expression, thus:
next unless obj.is_a?(Sketchup::Group) || obj.is_a?(Sketchup::ComponentInstance)
Otherwise in the few statements a call upon the obj entities method will raise an exception for primitives (or any other entity subclass object that doesn't have this method.)
Another way of safeguarding in Ruby, is a conditional "bailout" that tests if an object can respond to a method name:
next unless obj.respond_to?(:definition)
When the loop's block tests these kind of conditionals and they evaluate true the loop will cycle from there immediately to the next iteration.
@ittayd said:
Typo. ctrl should be shift.
... okay, then, let us try again:
@ittayd said:
Hitting ctrl+shift as ctrl, keeping pressed and then shift:
onKeyDown prints 11101 on hitting ctrl, note again the flags mean both ctrl and shift are down
onKeyDown prints 101010 on hitting shift (when the ctrl is still down) which suggests it "forgets" the shift is down.
It seems you are making a fundamental mistake. In this example in the quote above, the detected KEY is the SHIFT key, and it is what is causing the onKeyDown() callback to fire, and it's keycode VK_SHIFT will be stored in the key argument.
So, then you do not detect IT within the flags bitfield, because you already know what the firing key is.
Instead, at this point in time, you check the flags bitfield for whatever other modifiers are associated with this press of the SHIFT key.
Here's an example, assuming you've defined boolean methods to test the modifiers (as I explained in my educational post above.)
case key
when VK_SHIFT
if flags_has_copy_modifier?
# CTRL is also pressed, act accordingly
elsif flags_has_alt_modifier?
# ALT is also pressed, act accordingly
else
# act is some other way
end
when VK_CONTROL
if flags_has_constrain_modifier?
# SHIFT is also pressed, act accordingly
elsif flags_has_alt_modifier?
# ALT is also pressed, act accordingly
else
# act is some other way
end
else
if flags_has_constrain_modifier?
# SHIFT is also pressed, act accordingly
elsif flags_has_copy_modifier?
# CTRL is also pressed, act accordingly
elsif flags_has_alt_modifier?
# ALT is also pressed, act accordingly
else
# act is some other way
end
end
@ittayd said:
onKeyDown prints 101010 on hitting shift (when the ctrl is still down) which suggests it "forgets" the ctrl is down.
Why do you say this, when it is clear that the 4th bit (for CTRL modifier) is set ?
@ittayd said:
I couldn't find code examples that use flags in onKeyDown and onKeyUp either
The API docs have junky examples. They've never been a good teaching tool.
@ittayd said:
Is there a way to utilize sketchup's own selection tool?
The only way is to attach a SelectionObserver, activate SketchUp's native SelectTool Sketchup.active_model.select_tool(nil), collect the object references, detach the observer, and activate your tool.
But trying to leverage native tools via code has been problematic in the past. So, this is why they finally added (SU2016) the two window pick methods to the PickHelper class.
ThomThom wrote a tutorial and created a graphic helper chart for using this class.
I’ve put together a visual guide to the PickHelper class in SketchUp’s Ruby API. When I first tried to use the PickHelper class I was confused over what the different methods did, and f…
Procrastinators Revolt! (www.thomthom.net)
Other than that, you best learn by reading the example extension by the SketchUp Team (EW) or others like ThomThom's plugins.
Add coming from all this would be the need to detect when the user changes the axes:
# encoding; UTF-8
#
# A simple Axes Observer to detect when a user changes axes.
module Author
class AxesSpy < Sketchup;;EntityObserver
@@instance ||= self;;new
def self;;attach( mod = Sketchup.active_model )
mod.axes.add_observer(@@instance)
end
def onNewModel(mod)
self;;attach(mod)
end
alias_method ;onOpenModel, ;onNewModel
def expectsStartupModelNotifications()
true
end
def onChangeEntity(ent)
puts "AxesSpy; #{ent.inspect} changed."
end
Sketchup.add_observer(@@instance)
end
end
Edit: Fixed alias_method call. args were swapped, in error.
(I always seem to get them backwards. I always think "alias this as that", but it is more like "newalias that of this".)

@pixero said:
I'm trying to understand how axes works and have a script where axes are set to a custom value. I create a face and a group and still the origin and axes of the group is the default origin and axes.
Why doesn't it respect the new working axes?
Only the native tools respect the custom Axes setting by default.
If they had suddenly changed the API so that ALL code creating geometry in model space used the custom axes, they'd have broken a multitude of extensions, causing havoc.
Another facet, is that the original model origin and axes actually remain unchanged.
So, anyway, they decided that coders themselves had to decide to honor the user's custom axes settings.
@ittayd said:
What is the meaning of the values of the flags parameter to the key callback methods in a tool?
Each bit in a bitfield represents a boolean switch for some feature.
In this case, is it whether a certain key's flag is sent to an onKey... callback.
To test whether whatever virtual keycode (that you got in the key argument,) is also associated with one of the "modifier" keys (or mouse buttons,) you test the flags bitfield against the desired bitmask constant (listed at the top of the Tools class documentation page,) using the bitwise AND ( &) method.
If the result is 0 then that modifier key was not associated with the key.
If the result is non- 0 (usually the value of the mask constant,) then that modifier key WAS associated with the key.
This is accomplished because the & operation ignores all bits in the bitfield that are unset (0) in the mask argument. (Only that bit that you are testing for is set to 1 in the mask.)
See wikipedia:
http://en.wikipedia.org/wiki/Mask_(computing)#Querying_the_status_of_a_bit
Was the SHIFT modifier associated with the key argument ?
flags & CONSTRAIN_MODIFIER_MASK != 0
Was the Alt/Option on Mac, CTRL on PC modifier associated with the key argument ?
flags & COPY_MODIFIER_MASK != 0
Was the Command on Mac, ALT on PC modifier associated with the key argument ?
flags & ALT_MODIFIER_MASK != 0
The mousekeys modifier constants are:
MK_ALT, MK_COMMAND, MK_CONTROL, MK_LBUTTON, MK_MBUTTON, MK_RBUTTON, MK_SHIFT
It may help you to wrap them up into methods with names that make sense to you:
ie:
def mask_has_shift?(flags)
flags & CONSTRAIN_MODIFIER_MASK != 0
end
... etc.
BTW, all the virtual key codes and modifier mask constants are global (the API defines them at the toplevel.)

You clearly asked the following question which I answered (not just for your benefit, but also for others who may be new to programming and/or the SKetchUp Ruby API.)
@ittayd said:
What is the meaning of the values of the flags parameter to the key callback methods in a tool?
I'm editing my previous post to insert this question, to make it clear what I am answering.
Re-reading your original post the second paragraph is not at all clear. Ie: Run-on sentences. Not stating what callback (onKeyUp/onKeyDown) you are referencing.
In order for us to see what you see, we'd need a code snippet to try to re-create the issue. (Meaning, that if you've already written the code to test this, share it. Don't make us write up code also.)
This API has been working for more than 10 years. There is no clamour from the developers at large that something with this is amiss.
And BTW, you do not state what SketchUp version you are testing.
@boom02 said:
Do you guys think this is the right way?
NO. The spacebar is pre-coded to activate the native SelectionTool. Many users are used to it this way, and do not want to change it. Those that have changed it, have done so for their own choice of command.
Secondly, you cannot affect how the built-in place_component works as it is a native tool, most especially not when using the repeat mode. (Ie, there really is no way to trap the spacebar and prevent it from bubbling-up to SketchUp's shortcut handler.)
The way it is designed, when not in repeat mode, is that once placed, the normal MoveTool is active, and it's hover rotate mode can be used. Just hover the MoveTool cursor over a the side of a component's bounding box, and 4 rotate handles will appear. Move to one of the handles and you'll see the cursor change to a protractor.
I myself, also kinda miss the ACAD spin before place feature.
@boom02 said:
... how can I get the key press from the user?
The best way is inside a custom tool class written in Ruby.
http://ruby.sketchup.com/Sketchup/Tool.html
And you'd need to create a menu item pointing at a method object that rotates the selected object. Then that menu command can be assigned any shortcut, by users according to their own desires.
A workaround we employ is to stick a temporary cpoint into the entities (at it's origin,) and then delete it later after we've loaded it up with geometry. The temp cpoint stops SketchUp's garbage collector from deleting the object.
You might have missed the fact that the virtual helper classes Point3d, Vector3d and Array, can be transformed.
So if you were not creating a group, you could do before actually creating the geometry:
points.map! {|pt| pt.transform!(tr) }
For a group, you can actually apply the transform to the group, before adding the geometry. (You might need to assign a temporary cpoint to prevent SketchUp from garbage collecting it, as is normal.)
This will prevent the users from seeing geometry appear in a strange place.
@hank said:
I wish there was a way to quickly say "give me everything on this layer and copy it to a temporary group."
Just FYI, SketchUp layers are not like CAD layers. (They are IMO mis-named.)
SketchUp layers are nothing more than behavioral display property sheets, that any entity can point at, in order to know how to "act" with regard to display behaviors.
In SketchUp layers do not have entity collections, so objects are not "on layers." So this is why you cannot quickly iterate a layer's entity collection, as it doesn't have one.
And yes, transformations of nested objects are a PITB.
(TIG, I know you said this, but apparently the OP didn't understand it well.)
I would prefer that Trimble updates the DC code to add all those missing math functions that Scott did not implement, (... perhaps because Google Docs Spreadsheets did not support them at that time ?)
And then I'd like them to be serious about implementing nifty new DC Functions that users request, in a timely manner !
There is a problem with licensing. You will likely NOT pass the extension review if you alter the DCFunctionsV1 class in your extension code.
The licensing tutorial was removed pending an overhaul. No amount of asking has resulted in any estimation of when the tutorial will be edited and reposted.
Julia Christina Eneroth has posted her own licensing tutorial with test code at GitHub:
https://github.com/Eneroth3/Sketchup-Api-Licensing-Test
@tig said:
You can set @obs = ... within a module, outside of any of its methods and that then persists thereafter.
This is true because, a module is an instance of class Module.
@hank said:
Have not gotten very advanced with Classes etc. so just a simple module.
The Class class is the direct child class of class Module. So classes inherit all of Module's functionality, and get a little bit of their own (ie, a few extra methods and the ability to have many instance copies of themselves, that can hold their own instance variable values.)