For faster speed, change:
if element.typename == 'Group'
with:
if element.is_a?(Sketchup::Group)
Reason: Ruby string comparison is slow, but object identity is fast.
For faster speed, change:
if element.typename == 'Group'
with:
if element.is_a?(Sketchup::Group)
Reason: Ruby string comparison is slow, but object identity is fast.
@slb_jwm said:
In any email client I've used, ...
This is an online messaging board, not an email client. (It just happens to have an optional email echo feature.)
@cesaro36 said:
Thanks TIG
My intention is to highlight the face that the cursor is passing over.
You can do this easily by pushing the face's reference into the model selection set.
old_set = model.selection.to_a model.selection.clear model.selection.add(@ip.face)
@davesexcel said:
Thanks,
I think I understand the concept, from excel vba we are opening sketchup and passing on a variable that a plugin will recognize to run the plugin?
I think Jim explained it better and more detailed.
Local references go out of scope and are garbage collected when a method ends.
You need to have some persistent references within your plugin's scope (module or class) to accept the values. This also means you need to use persistent reference for the dialog itself (ie, @w not w.)
btn_ok = SKUI;;Button.new( 'OK' ) {|control|
@txt_box_value = txt_box.value
control.window.close
}
Angle snapping works for rotational tools or tool states.
Length snapping would work for linear tool states.
You would basically save the user's current angle snap settings, set 15 degrees and angle snap ON, do your thing, then restore the user's settings.
At the least you should honor the user's settings (perhaps as a starting point.)
Access via Sketchup::Model#options(), ie:
model = Sketchup.active_model
manager = model.options
manager.each {|key| puts "#{key.name}" }
#=> PageOptions
#=> UnitsOptions
#=> SlideshowOptions
#=> NamedOptions
#=> PrintOptions
opts = manager["UnitsOptions"]
#=> #<Sketchup;;OptionsProvider;0x00000009215148>
opts.each {|key,name| puts "#{key} = #{name}" }
#=> LengthPrecision = 3
#=> LengthFormat = 0
#=> LengthUnit = 0
#=> LengthSnapEnabled = true
#=> LengthSnapLength = 0.0001
#=> AnglePrecision = 1
#=> AngleSnapEnabled = true
#=> SnapAngle = 15.0
#=> SuppressUnitsDisplay = false
#=> ForceInchDisplay = false
so:
def save_angle_options(model=Sketchup;;active_model)
opts = model.options["UnitsOptions"]
opts["AngleSnapEnabled"], opts["SnapAngle"]
end
def restore_angle_options(snapping,angle,model=Sketchup;;active_model)
opts = model.options["UnitsOptions"]
opts["AngleSnapEnabled"]= snapping
opts["SnapAngle"]= angle
end
# In code elsewhere;
snapping, angle = save_angle_options()
# do your thing
restore_angle_options(snapping,angle)
Pls edit and remove code, then re-post properly indented code in a [ code ] ... [ /code ] block.
(Ie, no one will even attempt to read it.)
(This last question needs it 's own thread.)
.
FYI, in Ruby the environment is accessed via a hash-like
object referenced by the constant ENV.
Ref: http://ruby-doc.org/core-2.2.4/doc/globals_rdoc.html
Example in Ruby:
user = ENV["USERNAME"] || ENV["User"]
(The implication is that the names of the variables differ from Windows to Mac OSX. But the keys are case insensitive, so any of: "User", "USER" or "user" will access the value.)
If you wish to examine the hash at the console, use the prettyprint method (it should already be loaded.)
pp ENV
If you get an error, just call:
require "pp"
and try again.
VBS
WshEnvironment Object
[https://msdn.microsoft.com/en-us/library/6s7w15a0(v=vs.84).aspx](https://msdn.microsoft.com/en-us/library/6s7w15a0(v)
VBA Office
Environ function
https://msdn.microsoft.com/en-us/library/office/gg264486.aspx

@davesexcel said:
... but would like it to be able to get the info from the workbook that is already opened, not having to open the workbook to get the data. Is there a way to get the info from an active workbook?
Have you seen my example here in this forum ?
[Code] Geometry Creation from Excel worksheet data
It has a method that first checks to see if Excel is already open. And it is properly module wrapped (but you should change the name of the outer module at least.)
~
(1) Only write to paths that the user has write permissions on.
@davesexcel said:
# Puts in SketchUp install directory by default
This path is a binary program path and the normal user does not have write permissions there.
This is not the default path for Ruby when SketchUp v13+ is running. After SketchUp loads Ruby and it's API, it sets the working directory to the User's "Documents" directory.
SketchUp 2013 and higher:
Dir.pwd %(green)[#=> C:/Users/Dan/Documents]
SketchUp 8 and earlier just left the current working directory in SketchUp's program path (which was a bad thing.)
(2) In code when you are doing a write then read, you need to wait until the file is finished being written, and the OS file system advertises that it is "ready" and available. (ie, the old file handle is closed.)
The Ruby standard File class has some class methods to help you determine if paths are readable or writable by the current user, and whether the OS reports if the file is finished being written (ie, does it exist yet?)
def image_from_model(
write_path = 'S;/Stairs/Stair Pics/',
keys = {
;filename => 'write_image.jpeg',
;width => 500,
;height => 400,
;antialias => false,
;compression => 0.5,
;transparent => false
}
)
return false unless File.writable?(write_path)
image_path = File.join( write_path, keys[;filename] )
keys[;filename]= image_path
model = Sketchup.active_model
view = model.active_view
view.zoom_extents
Kernel.sleep(2.0) # wait for view to redraw
view.write_image(keys)
@tid = UI.start_timer(0.5,true) {
if File.exist?(image_path)
UI.stop_timer(@tid)
insert_to_excel(image_path)
end
}
end
def insert_to_excel()
xl = WIN32OLE.new('Excel.Application')
xl.visible=1
wb = xl.Workbooks.Open('C;\TestFolder\Test2.xlsx')
worksheet = wb.Worksheets('Sheet1')
worksheet2 = wb.Worksheets('Sheet2')
worksheet.Range('a1').Value=12
worksheet.Range('a1;b4').Value
#-----Pictures Insert Code---------------
pic = worksheet2.Pictures.Insert('S;/Stairs/Stair Pics/write_image.jpeg')
range = worksheet2.Range('A1;F15')
pic.ShapeRange.LockAspectRatio = false
pic.Top = range.Top
pic.Left = range.Left
pic.Width = range.Width
pic.Height = range.Height
# write done message to SketchUp console;
put "Finished inserting image in Excel."
end
David, basically the new site is under construction. The API documentation needs to be corrected first, and then the examples will grow.
I did a search in the MS Excel Dev forum on "+SaveAs +xlsm +UndefinedConversionError", and got 947 hits. (You can browse those yourself.)
At this point I would double-check that any macros in the Workbook do not have errors, that could give the file export routine issues.
@davesexcel said:
I thought 52 was the code to save as an .xlsm file, but I get an UndefinedConversionError.
> workbook.SaveAs("C;\TestFolder\TestAddImage.xlsm",52)
>
As a general rule, try to use the interface's constants rather than direct use of integers. (The integers could change in the future and break your code.)
Microsoft forums:
Excel for Developers:
https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=exceldev%26amp;filter=alltypes%26amp;sort=lastpostdesc
Open XML Format SDK
https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=oxmlsdk%26amp;filter=alltypes%26amp;sort=lastpostdesc
No Steve, he's fine. He uses the Ruby standard WIN32OLE library to connect SketchUp with Excel workbooks.
This library basically exposes the VBA object model of Excel to Ruby. SO he's attempting to make a OLE call to Excel's VBA "SaveAs" method.
This is the entry page for the Excel VBA object model:
https://msdn.microsoft.com/en-us/library/ff194068.aspx
This page coves the Workbook object:
https://msdn.microsoft.com/en-us/library/ff835568.aspx
This page is for the Workbook.SaveAs method:
https://msdn.microsoft.com/en-us/library/ff841185.aspx
This page is for the XlFileFormat Enumeration, whose members are to be used in the 2nd argument to Workbook.SaveAs:
https://msdn.microsoft.com/en-us/library/ff198017.aspx
The XLSM format came out in 2007.
But has it been superseded in newer Office versions ?
List of Microsoft Office filename extensions:
https://en.wikipedia.org/wiki/List_of_Microsoft_Office_filename_extensions
Office Open XML (Microsoft invented standard, and copyright holder.)
https://en.wikipedia.org/wiki/Office_Open_XML
Not to be confused with:
OpenOffice.org XML (obsolete, superseded by OpenDocument):
https://en.wikipedia.org/wiki/OpenOffice.org_XML
OpenDocument (The FREE international standard):
https://en.wikipedia.org/wiki/OpenDocument
I looked in my 2016 Plugins folder and found what I had working last June 2016.
It seems to work for me on SU2016.
Don't miss the updated styling in
http://sketchucation.com/forums/viewtopic.php?f=180%26amp;t=65078%26amp;view=unread#p597010
OH! I see your booboo: inputbox always returns an array (except when the user cancels it returns false. So always check the result boolean-wise.)
input = UI.inputbox prompts, defaults,list, "File Name" return unless input j = input[0]
You might try:
s = File.join('C:/TestFolder/Doit with Sketchup/', j)