Let us know if any of the suggestions work.
Posts
-
RE: HTMLDialog vs WebDialog?
-
RE: [Code Snippets]
(No longer Maintained due to lack of donations.)
Platform Issues / Differences / Specifics
Platform Differences
-
Bugs / Issues
-
GUI
-
Misc.
-
Ruby
-
WebDialog
-
System / OS
-
[url=http://forums.sketchucation.com/viewtopic.php?f=180%26amp;t=24327:1a2mqsys]onKeyDown repeat parameter problem[/url:1a2mqsys]
Mac / OSX Specific
-
Bugs / Issues
-
[url=http://forums.sketchucation.com/viewtopic.php?f=180%26amp;t=35959:1a2mqsys]SU 8 Mac & strange content[/url:1a2mqsys] (memory leak?)
-
GUI
-
[url=http://forums.sketchucation.com/viewtopic.php?f=180%26amp;t=33888:1a2mqsys]Mac Toolbars Editable?[/url:1a2mqsys]
-
[url=http://forums.sketchucation.com/viewtopic.php?f=180%26amp;t=34002:1a2mqsys]My Apple Keyboard VK_KEYS[/url:1a2mqsys]
-
Misc.
-
Ruby
-
[url=http://forums.sketchucation.com/viewtopic.php?f=180%26amp;t=34219:1a2mqsys]Ruby Version for Sketchup 8.0 M1 on Mac ??[/url:1a2mqsys]
-
WebDialog
-
[url=http://forums.sketchucation.com/viewtopic.php?f=180%26amp;t=30127:1a2mqsys]JS loading problem under Mac OS X SU[/url:1a2mqsys]
-
[url=http://forums.sketchucation.com/viewtopic.php?f=180%26amp;t=29751:1a2mqsys]PC v MAC webdialog populate[/url:1a2mqsys]
-
[url=http://forums.sketchucation.com/viewtopic.php?f=180%26amp;t=26927:1a2mqsys]Sketchup use of WebKit[/url:1a2mqsys]
-
[url=http://forums.sketchucation.com/viewtopic.php?f=180%26amp;t=32415:1a2mqsys]Webdialog position not preserved across session on MAC[/url:1a2mqsys]
-
[url=http://forums.sketchucation.com/viewtopic.php?f=180%26amp;t=25865:1a2mqsys]WebDialogs are caching and not refreshing images[/url:1a2mqsys]
-
[url=http://forums.sketchucation.com/viewtopic.php?f=180%26amp;t=39842:1a2mqsys]WebDialog.set_html fails under Safari 5.0.6[/url:1a2mqsys]
-
System / OS
-
[url=http://forums.sketchucation.com/viewtopic.php?f=15%26amp;t=29330:1a2mqsys]Auto-running a Mac '.command' File from Sketchup?[/url:1a2mqsys]
-
[url=http://forums.sketchucation.com/viewtopic.php?f=180%26amp;t=30314:1a2mqsys]"Mac32API.so" ?[/url:1a2mqsys]
PC / Win Specific
-
Bugs / Issues
-
Files
-
[url=http://forums.sketchucation.com/viewtopic.php?f=180%26amp;t=43007:1a2mqsys][Code] PCFileTools[/url:1a2mqsys] - Support UTF8 (TIG)
-
GUI
-
[url=http://forums.sketchucation.com/viewtopic.php?f=180%26amp;t=29970:1a2mqsys]Windows: Moving Floating Toolbars[/url:1a2mqsys]
-
Misc.
-
Ruby
-
[url=http://forums.sketchucation.com/viewtopic.php?f=180%26amp;t=16574:1a2mqsys]Win32API vs DL lib[/url:1a2mqsys]
-
WebDialog
-
[url=http://forums.sketchucation.com/viewtopic.php?f=180%26amp;t=27594:1a2mqsys]Web dialog browser version[/url:1a2mqsys]
-
System / OS
-
[url=http://forums.sketchucation.com/viewtopic.php?f=180%26amp;t=36772:1a2mqsys]Determine OS bit version with ruby[/url:1a2mqsys]
-
[url=http://forums.sketchucation.com/viewtopic.php?f=180%26amp;t=18488:1a2mqsys]Sketchup registry key ?[/url:1a2mqsys]
(No longer Maintained due to lack of donations.)
COMMUNITY CONTENT
Moderators may edit post / add links at will. -
-
RE: [Code Snippets]
BY SUBJECT
(No longer Maintained due to lack of donations.)
Animation
- [url=http:http://forums.sketchucation.com/viewtopic.php?t=25119][Code] Animation controller[/url] (Jim)
Arcs & Curves
- [url=http:http://forums.sketchucation.com/viewtopic.php?t=19270][Code][Plugin]ArcCurve-set_segments.rb & changearcsegments[/url] ([url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=28846&p=322175#Author_TIG]TIG[/url])
Arrays
- (Element References) [Info] [url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=29975#p262832]This tripped me up (again) today[/url]
- [url=http:http://www.ruby-doc.org/core-1.8.6/Array.html]Core Methods of Array class[/url] (offsite)
- [url=http:http://www.ruby-doc.org/core-1.8.6/Enumerable.html]Methods Mixed in from module Enumerable[/url] (offsite)
- [url=http:https://developers.google.com/sketchup/docs/ourdoc/array]Methods Added by the SketchUp API[/url] (offsite)
Attributes & Dictionaries
[url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=30066][Info] Allowable Classes for "set_attribute"[/url] (Karen Walkerman)
C/C++ Ruby extensions & SketchUp plugins
- see: [url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=28846#p323876]subpost (below)[/url]
Camera
- [url=http:http://forums.sketchucation.com/viewtopic.php?t=25917][Code] Camera settings[/url] (chrisglasier)* [url=http:http://forums.sketchucation.com/viewtopic.php?t=26904][Code] Rotate Camera around Target[/url] (Jim)
Classes
-
[url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=38968]Checking for a group?[/url]
-
Misc:
-
[url=http:http://sketchucation.com/forums/viewtopic.php?f=180&t=48066&p=431794#p431794]"Why use a module instead of a class ?"[/url] (Dan Rathbun)
Communications
-
Serial / USB
-
[url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=11693&p=259509]Read Serial port or USB interface[/url]* Socket
-
[url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=31545]Interest in a Networking Sockets Workaround[/url]* [url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=33759]Clients <--> Server Communication (Sockets?)[/url]* [url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=36306&p=319776]Requiring Socket on Mac crashes SU[/url]
Colors
- [url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=23837][Code] Ruby Extension for Sketchup color integers[/url]
Component(s)
- [url=http:http://forums.sketchucation.com/viewtopic.php?t=18006][Code] ComponentInstance-add_entities v1.9[/url] (TIG)* [url=http:http://forums.sketchucation.com/viewtopic.php?t=17962][Code] ComponentDefinition-delete[/url] (TIG)* [url=http:http://forums.sketchucation.com/viewtopic.php?t=18028][Code] ComponentDefinition-delete (another version!)[/url] (AlexMozg)* [url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=38819]parent instance of a component[/url]
- [url=http:http://sketchucation.com/forums/viewtopic.php?f=180&t=49571&p=445964#p445858]Collecting definitions that ARE parents of component instances[/url] (TIG)
- [url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=44193][Code] intersect_with?() test method[/url] (Dan Rathbun)
Construction Lines & Points
Debugging
- [Info] [url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=34052]How to debug?[/url]
Dimension(s)
- [url=http:http://forums.sketchucation.com/viewtopic.php?t=21315][Code] entities.add_linear_dimension[/url] (TIG)
Edge(s)
- [url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=39016]Ruby How to Select All Connected[/url]
Entity(ies)
- [url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=38968]Checking for a group?[/url]* [url=http:http://forums.sketchucation.com/viewtopic.php?t=18538][Code] entity.real_typename[/url] (Matt666)
Execution & Error Trapping
- [url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=36167#p318387]Help with using begin, rescue, end[/url]* [url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=34885][Code] Error raising scriptlets for testing[/url] (Dan Rathbun)* [url=http:http://forums.sketchucation.com/viewtopic.php?t=19372][Code] Method Model.start_operation for SketchUp 5-6[/url] (AlexMozg)* [url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=25305]Optimization Tips[/url]
Face(s)
- [url=http:http://forums.sketchucation.com/viewtopic.php?t=3184][Code] orient_faces.rb[/url] (TIG)
- [url=http:http://sketchucation.com/forums/viewtopic.php?f=180&t=41211][Code] is an entity in a surface[/url]
File(s)
- [url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=36234#p319024]getting info from a text file[/url]* [url=http:http://forums.sketchucation.com/viewtopic.php?t=26185][Code] 3ds2obj.rb[/url] (Jim)* [url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=29787][Code] reading a CSV file[/url] (Dan Rathbun)* [url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=43007][Code] PCFileTools[/url] - Support UTF8 (TIG)
Group(s)
- [url=http:http://forums.sketchucation.com/viewtopic.php?t=19765][Code] Group.real_parent[/url] (thomthom)* [url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=38968]Checking for a group?[/url]* [url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=38925]Copy selection to new group?[/url]* [url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=44193][Code] intersect_with?() test method[/url] (Dan Rathbun)
- [url=http:http://sketchucation.com/forums/viewtopic.php?f=180&t=52014&view=unread#p470425][Info] Modifying geometry in freshly copied Groups (not unique)[/url] (Fredo6)
Image(s)
- [url=http:http://forums.sketchucation.com/viewtopic.php?t=21869][Code] Image: definition, transformation, transformation=[/url] (AlexMozg)* [url=http:http://forums.sketchucation.com/viewtopic.php?t=24876][Code] extract png thumbnail image from .skp file[/url] (Jim)
Layer(s)
- [Info] [url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=36702]Layer0 questions[/url] & answers (Al Hart/TIG)* [url=http:http://forums.sketchucation.com/viewtopic.php?t=20086][Code] layer.delete()[/url] (TIG)* [url=http:http://forums.sketchucation.com/viewtopic.php?t=21090][Code] layer-color= v1.2[/url] (TIG)* [url=http:http://forums.sketchucation.com/viewtopic.php?t=20881][Code] layers.purge_unused[/url] (chrisglasier)
Material(s)
- [url=http:http://forums.sketchucation.com/viewtopic.php?t=20085][Code] material.delete[/url] (TIG)* [url=http:http://forums.sketchucation.com/viewtopic.php?t=20112][Code] material.name= v1.2[/url] (TIG)
Math
- [url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=36722][Code] Parsing arithmetic formulas[/url] (Fredo6)
Modules
- [url=http:http://sketchucation.com/forums/viewtopic.php?f=180&t=48066&p=431794#p431794]"Why use a module instead of a class ?"[/url] (Dan Rathbun)
- [url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=46124&p=413623#p412101][Code] Private Mixin Module[/url] (Dan Rathbun)
Observer(s)
-
[Info] [url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=20676]State of Observers[/url] (ThomThom)* [url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=30793]State of Observers Redux — 12 September 2010[/url] (ThomThom)
-
[url=http:http://www.thomthom.net/software/sketchup/observers/][link] State of Google SketchUp's Observers[/url]* [url=http:http://forums.sketchucation.com/viewtopic.php?t=21659][Code] MetaObserver[/url] (Jim)
Pages & Scenes
Platform (Mac/OSX vs PC/Windows)
- [url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=28846&p=322176#p322176]Issues / Differences / Specifics[/url] (goto next post)
- [url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=34631][Code] FAQ: Detect if plugin is running on the Mac vs PC ?[/url]
Ruby
-
$LOAD_PATH
-
[url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=29412&p=257058][Code] Ruby LOAD PATHs script (Win32)[/url] (Dan Rathbun / Jim Foltz)* Object References
-
Array : [Info] [url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=29975#p262832]This tripped me up (again) today[/url]* [url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=25305]Optimization Tips[/url]
Selection
- [url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=38925]Copy selection to new group?[/url]
SketchupExtension
- [url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=39094][Code] SketchupExtension and rbs rubies[/url]
Strings
- [url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=36722][Code] Parsing arithmetic formulas[/url] (Fredo6)* [url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=35969#p316774]JSON in Ruby[/url]* [url=http:http://forums.sketchucation.com/viewtopic.php?t=20657][Code] UnicodeEx - (0.2.0a) Sketchup + Character Encoding[/url] (thomthom)* [url=http:http://forums.sketchucation.com/viewtopic.php?t=20289][Code] file_found?(path) and to_ascii+to_unicode.rb[/url] (TIG)* [url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=34975][Code] Split a long pathstring ~in half at the nearest '/'[/url] (various)
Styles
Surface(s)
- [url=http:http://sketchucation.com/forums/viewtopic.php?f=180&t=41211][Code] is an entity in a surface[/url]
Text & 3DText
Texture(s)
- [url=http:http://forums.sketchucation.com/viewtopic.php?t=23196][Code] texture_by_component.rb[/url] (honoluludesktop)* [url=http:http://forums.sketchucation.com/viewtopic.php?t=22719][Code] Real UV from UVHelper data[/url] (thomthom)
Timer(s)
- [url=http:http://forums.sketchucation.com/viewtopic.php?t=25178][Code] Timer class[/url] (MartinRinehart)
Tool(s)
- [url=http:http://forums.sketchucation.com/viewtopic.php?t=18124][Code] Tool ID Constants - Rev. 2[/url] (thomthom)
Transform(s)
- [url=http:http://forums.sketchucation.com/viewtopic.php?t=22639][Code] Transformation_Extensions[/url] (TIG)
User Interface
-
Command(s)
-
Instructor Helpfiles
-
[url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=35806#p315546][Code] Load Instructor v1.0.1[/url] (Niall Campbell)* Menu(s)
-
[url=http:http://sketchucation.com/forums/viewtopic.php?f=180&t=48350&p=433934#p433934]One Submenu for many plugins[/url] (Dan Rathbun)* Mouse
-
[url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=38651]Mouse Capture (Win32 API)[/url]* Toolbar(s)
-
WebDialog
-
[url=http:http://forums.sketchucation.com/viewtopic.php?t=23343][code] reset webdialog and toolbars .bat file[/url] (Jim)* [info] [url=http:http://forums.sketchucation.com/viewtopic.php?f=180&t=38940]Web Dialog not firing a JavaScript function...[/url]
(No longer Maintained due to lack of donations.)
COMMUNITY CONTENT
Moderators may edit post / add links at will. -
RE: DLL callback to ruby
You will need to do some studying:
- Read and bookmark this post:
[Info] C/C++ Ruby extensions & SketchUp plugins
- You will need to download the Ruby C source, as your DLL code must:
%(#404080)[include "ruby.h"]
- Read the old "Pick-Axe" book's chapter on writing a Ruby extension.
Programming Ruby: Extending Ruby
- Take note of the Ruby C-side functions:
rb_eval_string( *string_to_eval* )
*rb_funcall
... described in section "Evaluating Ruby Expressions in C"
- okay, a dedicated C side Ruby book is also now available:
-
RE: SketchUp Update Broke the Foundation Plugin
Or single object extension via mixin module (as shown above.)
unless bb.extend(BB_Volume).volume == 0.0 # do code here end
Or if only supporting Ruby 2.x+ SketchUp versions, you can use class refinements that are only active within your files.
See: http://ruby-doc.org/core-2.0.0/doc/syntax/refinements_rdoc.html -
RE: HTMLDialog vs WebDialog?
You missed the point John, he's doing neither. He is simply leaving the droplist and grabbing the window caption bar and moving the entire window. This is leaving the droplists behind (as if torn off) as shown in the screenshots above.
My attempt to fix it would detect when the mouse leaves the body of the html document, and blur the
<select>
element hopefully making it close it's droplist. -
RE: HTMLDialog vs WebDialog?
@garry k said:
Also I moved the form while the drop down list is open and now you can't tell which drop down I was in. The difference is IE closes the drop down when you click and drag the form - chrome does not.
Let us try some tests to see if we can get Chrome to close the
<select>
droplists.<body onmouseout="document.blur();" onmouseover="document.focus();">
I'm hoping this will cause the current active selection element to lose focus and have it's
blur
event fire.If this does not work, then perhaps keep track of the current object that has focus:
<!DOCTYPE html> <html> <head> <script> // Global variable; activeObject = document.body; previousObject = document.body; function setActive(obj) { activeObject = obj; } function clearActive(obj) { activeObject = document.body; previousObject = obj; } function blurAll() { if activeObject != document.body { activeObject.blur(); if activeObject != document.body { clearActive(activeObject); } } document.blur(); } function reFocus() { document.focus(); if previousObject != document.body { previousObject.focus(); // calls setActive(previousObject) previousObject = document.body; } } function CheckKey(e) { var code = e.keyCode ? e.keyCode ; e.which; if(code === 13) { alert("You press Enter key."); } } function SubstanceChangeHandler(val) { alert("New substance chosen; "+val); txtTest.value= val; } </script> </head> <body onmouseout="blurAll();" onmouseover="reFocus();"> <input name="txtTest" type="text" id="txtTest" onkeyup="CheckKey(event)"/> <select onchange="SubstanceChangeHandler(this.value);" onfocus="setActive(this);" onblur="clearActive(this);"> <optgroup label="Alkaline Metals"> <option>Lithium (Li)</option> <option>Sodium (Na)</option> <option>Potassium (K)</option> </optgroup> <optgroup label="Halogens"> <option>Fluorine (F)</option> <option>Chlorine (Cl)</option> <option>Bromine (Br)</option> </optgroup> </select> </body> </html>
If this scenario works, you can set the
onfocus
andonblur
handlers for all<select>
elements via an iteration loop after the page loads. -
RE: HTMLDialog vs WebDialog?
@garry k said:
Also the Enter key doesn't trap while you are inside a drop down list.
But when the user (in CEF) hits ENTER on an
<select><option>
, that changes the selection does it not ? And therefore it should fire the some event ?
I just tested in normal Chrome and the ENTER key works fine.
http://jsfiddle.net/wkpeq/73/<!DOCTYPE html> <html> <head> <script> function CheckKey(e) { var code = e.keyCode ? e.keyCode ; e.which; if(code === 13) { alert("You press Enter key."); } } function SubstanceChangeHandler(val) { alert("New substance chosen; "+val); txtTest.value= val; } </script> </head> <body> <input name="txtTest" type="text" id="txtTest" onkeyup="CheckKey(event)"/> <select onchange="SubstanceChangeHandler(this.value);"> <optgroup label="Alkaline Metals"> <option>Lithium (Li)</option> <option>Sodium (Na)</option> <option>Potassium (K)</option> </optgroup> <optgroup label="Halogens"> <option>Fluorine (F)</option> <option>Chlorine (Cl)</option> <option>Bromine (Br)</option> </optgroup> </select> </body> </html>
-
RE: SketchUp Update Broke the Foundation Plugin
I would also suggest avoiding naming objects "new" as this is the standard name for the class constructor method. This could confuse the Ruby interpreter under some situations, as it must decide whether you are trying to make a method call or creating a local reference of the same name.
The idea is to train yourself to avoid hard to find errors.
-
RE: SketchUp Update Broke the Foundation Plugin
@medeek said:
I've indicated the exact line where the error is happening below.
You cannot use bbCode bold tags inside a bbCode code block.
Try marking errors inside code using comments like:
erroneous code # <<<<<<<<<<--------------<<<<<<<< ERROR !
Or, bracketing:
` ####
THIS DOES NOT WORK
result = some_code()
a = b + result####`
Regarding your
NoMethodError
error. TheBoundingBox
class does not have (nor ever did,) an instance method namedvolume()
. So I doubt that it could work as intended on older versions.EDIT: However, it is possible you were relying upon a modification to the
BoundingBox
class made by some extension that you've not loaded in the latest version ? (Generally, you should not rely upon API modifications made by other extensions.)You also seem to have ignored the advice in the API docs concerning the
BoundingBox#intersect
method.You'd have to either define a singleton
volume()
method and attach to that single object:module BB_Volume def volume() height * width * depth end end bb_obj.extend(BB_Volume) unless bb_obj.volume == 0.0 # do code here end
OR .. just use the
Geom::BoundingBox#empty?
method:unless bb_obj.empty? # do code here end
-
RE: Pass array to a c/c++ ruby extension
Never could understand SWIG much. Cryptic interface language.
If you intended to write a library that needs interfaces in multiple scripting languages, then SWIG might lessen the workload. Or if you are wanting a Ruby interface for some library already written that has SWIG interface files, then again I can see using SWIG.But for an extension that will be written specifically for SketchUp, since it only uses Ruby, I cannot see much use for SWIG.
https://en.wikipedia.org/wiki/SWIG
-
RE: Delete a list of components by their definitions.
@tntdavid said:
For this reason the problem is strange and requires an investigation.
I agree. Sounds like a good learning experience for you.
I'll let you do the investigation, and fix it.@tntdavid said:
Thank you in advance for your help.
I've given you enough help for now.
Your turn to do some of the work.
-
RE: Multi-Point Tool
@medeek said:
What I would like to do is make it possible to hold down the control key or some other key while selecting the point to position the footing, thereby the allowing the user to select multiple points.
(As said also in the other forum...)
The convention for SketchUp is that the CTRL key acts as a toggle in and out of copy mode. (This is why the API defines the constants as
COPY_MODIFIER_KEY
andCOPY_MODIFIER_MASK
.Examine how the SelectionTool switches in and out of add or subtract mode, and the cursor changes to show + or - satellite icons. Or the MoveTool, ... etc.
Also see this other thread here at SCF:
http://sketchucation.com/forums/viewtopic.php?f=180%26amp;t=67837#p621579
You'll need to decide how to let the user tell the tool when they are done picking points.
Another convention is that the ESC key resets the tool to it's initial state, or cancels the current state. (See the
onCancel
callback.)The ENTER is another way, detected via a
onReturn
oronKeyUp
callback. -
RE: Pass array to a c/c++ ruby extension
@anton_s said:
Note: I did not check this code for typos, so it's up to you to fix it (if there are any).
I see one. The Ruby initiator function should begin:
void Init_some_lib() { // ...
where the name of the library file is exactly "some_lib" (with whatever the platform shared library extension is, ie ".so", ".dll", ".bundle". etc.)
-
RE: Pass array to a c/c++ ruby extension
Read this book for that kind of normal Ruby C extension stuff:
This is the Pragmatic Programmer's Guide to Extending Ruby (for v1.9.2)
by Dave Thomas.
(A companion to the good ol' "Pick Axe" Pragmatic Programmer's Guide): -
RE: Delete a list of components by their definitions.
# To test; # purge_comps('CUBE','TOTO','LOLA') # ... or; # del = ['CUBE','TOTO','LOLA'] # purge_comps(del) # # ver; 2.0 def purge_comps(*cnames) # return false if cnames.empty? cnames.flatten! # match = /#{cnames.join('|')}/ # model=Sketchup.active_model # collect matching definitions defstogo=model.definitions.find_all{|d| d.name =~ match } return 0 if defstogo.empty? # collect materials. matstogo=[] defstogo.each{|d| # first used by instances d.instances.each{|i| matstogo << i.material unless matstogo.include?(i.material) } # now used by its entities d.entities.each{|e| matstogo << e.material unless matstogo.include?(e.material) ( matstogo << e.back_material unless matstogo.include?(e.back_material) ) if e.is_a?(Sketchup;;Face) } } matstogo.compact! matstogo.uniq! # now check if materials used elsewhere defsNOTtogo=model.definitions.find_all{|d| ! d.name =~ match } # collect materials. matsNOTtogo=[] defsNOTtogo.each{|d| # first used by instances d.instances.each{|i| matsNOTtogo << i.material unless matsNOTtogo.include?(i.material) } # now used by its entities d.entities.each{|e| matsNOTtogo << e.material unless matsNOTtogo.include?(e.material) ( matsNOTtogo << e.back_material unless matsNOTtogo.include?(e.back_material) ) if e.is_a?(Sketchup;;Face) } } # now used by model entities model.entities.each{|e| matsNOTtogo << e.material unless matsNOTtogo.include?(e.material) ( matsNOTtogo << e.back_material unless matsNOTtogo.include?(e.back_material) ) if e.is_a?(Sketchup;;Face) } matsNOTtogo.compact! matsNOTtogo.uniq! # now reduce matstogo as necessary... matstogo.clone.each{|e| matstogo.delete(e) if matsNOTtogo.include?(e) } # defsgone = defstogo.size matsgone = matstogo.size # start and operation to ensure Garbage Collection works... model.start_operation('Purger', true) # now delete the selected entities defstogo.each{|d| d.entities.clear! } # now delete the unneeded materials matstogo.each{|m| model.materials.remove(m) } # clear the arrays of references to deleted objects; matstogo.clear defstogo.clear # commit model.commit_operation msg = "#{defsgone} definitions purged\n#{matsgone} materials purged" puts msg UI.messagebox(msg,MB_OK) return defsgone end
-
RE: Delete a list of components by their definitions.
@tntdavid said:
Your method works very well and offers new benefits.
It is TIG's method with a few extra "bells and whistles".
@tntdavid said:
I noticed a small problem:
How to avoid deleting the texture of 'CUBE', if it is used elsewhere in SketchUp?I'll look at it today a bit and see if there is an issue.
EDIT: Okay, YES I see the issue. The method does not check the model level entities collection for used materials (on primitive entity objects.)
-
RE: Delete a list of components by their definitions.
@tntdavid said:
Here is the TIG method, which works for several definitions:
Your implementation still has a few errors.
David, I say again, that I gave you the code for a complete corrected method above.
It defines a method namepurge_comps(*cnames)
-
RE: Delete a list of components by their definitions.
@tntdavid said:
@tig said:
What made you think you need to diverge from my guidance ?
I used a array to "match" because there will be several different definitions to delete.
TIG is showing you how to purge ONEdefinition and it's materials.
@tntdavid said:
Your code works perfectly for a single definition, ...
No it does not. because you made 3 edits that make it NOT work properly.
CORRECTION: Actually I found 4 errors that made it not delete all the components or materials.
The second use of the Regexp was! d.name =~ /#{name}/
(the regexp var ismatch
.)
matsNOTtogo=[]
was defined asmatsNOYtogo=[]
, resulting inNameError
The comparison of back_material was conditional upon inclusion of front material in two statements.@tntdavid said:
... what if we have "CUBE", "TOTO" and "LOLA" to delete?
First, learn how and why TIG's code works for ONE definition, then learn how to modify it for multiple definitions.
Then either you wrap what TIG showed you into a method, and call it 3 times from a loop:
for cname in ['CUBE','TOTO','LOLA'] # WHOOPS this not needed --> do |cname| purge_comp(cname) end
... or you build a multi-match regular expression (
Regexp
cnames = ['CUBE','TOTO','LOLA'] match = /#{cnames.join('|')}/
Test file (SU2016):
CubeLolaToto.skp
"Color B04" and "Color J08" are unused.
"Color K03", "Color M06" are used by primitive faces.
"Material 2" is the leader color.So here is a method with extra quirks:
- Returns false if array of cnames is empty.
- Short circuits and returns 0 if no definitions are found to delete.
- Reports both to console and message box number of definitions and materials purged.
- Returns number of definitions deleted.
# To test; # purge_comps('CUBE','TOTO','LOLA') # ... or; # del = ['CUBE','TOTO','LOLA'] # purge_comps(del) def purge_comps(*cnames) # return false if cnames.empty? cnames.flatten! # match = /#{cnames.join('|')}/ # model=Sketchup.active_model # collect matching definitions defstogo=model.definitions.find_all{|d| d.name =~ match } return 0 if defstogo.empty? # collect materials. matstogo=[] defstogo.each{|d| # first used by instances d.instances.each{|i| matstogo << i.material unless matstogo.include?(i.material) } # now used by its entities d.entities.each{|e| matstogo << e.material unless matstogo.include?(e.material) ( matstogo << e.back_material unless matstogo.include?(e.back_material) ) if e.is_a?(Sketchup;;Face) } } matstogo.compact! matstogo.uniq! # now check if materials used elsewhere defsNOTtogo=model.definitions.find_all{|d| ! d.name =~ match } # collect materials. matsNOTtogo=[] defsNOTtogo.each{|d| # first used by instances d.instances.each{|i| matsNOTtogo << i.material unless matsNOTtogo.include?(i.material) } # now used by its entities d.entities.each{|e| matsNOTtogo << e.material unless matsNOTtogo.include?(e.material) ( matsNOTtogo << e.back_material unless matsNOTtogo.include?(e.back_material) ) if e.is_a?(Sketchup;;Face) } } matsNOTtogo.compact! matsNOTtogo.uniq! # now reduce matstogo as necessary... matstogo.clone.each{|e| matstogo.delete(e) if matsNOTtogo.include?(e) } # defsgone = defstogo.size matsgone = matstogo.size # start and operation to ensure Garbage Collection works... model.start_operation('Purger', true) # now delete the selected entities defstogo.each{|d| d.entities.clear! } # now delete the unneeded materials matstogo.each{|m| model.materials.remove(m) } # clear the arrays of references to deleted objects; matstogo.clear defstogo.clear # commit model.commit_operation msg = "#{defsgone} definitions purged\n#{matsgone} materials purged" puts msg UI.messagebox(msg,MB_OK) return defsgone end
If you absolutely must, you can call
GC.start
to ensure Ruby garbage collection runs. -
RE: Delete a list of components by their definitions.
@sdmitch said:
You are trying to define the variable "cname" twice in the same statement. delete the do | cname |
MY BAD! I had a bad example (above.) [ Commented out that " do | cname |" part ]
@sdmitch said:
As usual Dan is right. You just need to wrap TIG's code with
["cube","toto","lola"].each{|name| > match = /#{name}/i > . > . > . > }
That will actually create a separate undo operation for each entity (and it's materials) and will be much slower.
@tntdavid said:
I have to move on from something obvious that I can not understand.
I gave you a complete working method at the bottom of that previous post !!!!!!
https://forums.sketchup.com/t/ruby-learning-resources-wikilists/22861
Get the old "Pick Axe" book and read it.