Get list of faces with associated corners
-
Hello,
I'm very new to scripting for SkechUp, and was wondering whether anybody could help point me in the right direction.
I need to create a script that exports all faces and their associated corner coordinates to a text file. This must be such a simple task, and I've looked a bit through the documentation, but can't figure out how I would ask SketchUp for that information... (edit: I.e. any functions I should check out..?)
Any pointers in the right direction would be greatly appreciated!
J
-
To learn to write files, you need to learn Standard Ruby.
Begin with my newbies guide, that will lead you to more info ...
-
What have you tried? And where are you stuck?
-
Thanks guys, I'll go through that tutorial and come back with some more specific questions. I realize my original post was a bit vague! I think I was hoping for a "oh it's only two lines of easy code"..
I've used Python a lot, so I reckon picking up Ruby should be fairly quick (I hope).
-
Well, when you say "all faces" - do you mean even those contained nested inside groups and components? Also, what output format would it be?
In order to traverse entities in SketchUp you look at the
Entities
collection class. This is anEnumerable
.To traverse the root context:
<span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each </span><span class="syntaxkeyword">{ |</span><span class="syntaxdefault">entity</span><span class="syntaxkeyword">|<br /> </span><span class="syntaxcomment"># ...<br /></span><span class="syntaxkeyword">} </span><span class="syntaxdefault"></span>
To traverse the active context (open group or component)
<span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each </span><span class="syntaxkeyword">{ |</span><span class="syntaxdefault">entity</span><span class="syntaxkeyword">|<br /> </span><span class="syntaxcomment"># ...<br /></span><span class="syntaxkeyword">} </span><span class="syntaxdefault"></span>
To traverse a collection of a specific type of entity:
<span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">grep</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Face</span><span class="syntaxkeyword">) { |</span><span class="syntaxdefault">face</span><span class="syntaxkeyword">|<br /> </span><span class="syntaxcomment"># Traverses all faces...<br /></span><span class="syntaxkeyword">} </span><span class="syntaxdefault"></span>
Note that if you want to process nested groups and components you need to look for
Sketchup::Group
andSketchup::ComponentInstance
entities and recursively dig into them. Beware that the coordinates you get from a vertex is local to it's parent context - so if you need the global position you need to take into account the total transformation of all the parent nodes.More info on SketchUp's instances and definitions:
http://www.thomthom.net/thoughts/2012/02/definitions-and-instances-in-sketchup/General info on SketchUp plugin development:
http://www.thomthom.net/thoughts/2012/01/golden-rules-of-sketchup-plugin-development/It is an easy task - but you need to be familiar with the API to know what classes and methods to use. Which is why it'd help if you mentioned what you've tried so far as it'd be easier to point you in the correct direction instead of just general all-covering answers.
-
@jodles said:
I've used Python a lot, so I reckon picking up Ruby should be fairly quick (I hope).
Yes.. you have a major advantage if your brain has already reached the "object oriented programming epiphany."
However.. two major differences to remember.
(1) Ruby uses block delimiters (or keywords,) rather than the fixed indentation of Python. (Also Ruby uses 2 space indentation by tradition, not 4 space; and never odd space indents, and please avoid outdenting.. it sucks!)
IE:{
...}
begin
...end
do
...end
def
...end
module
...end
class
...end
(2) Namespaces in Ruby are explicitly declared (more specifically opened for edit,) using the
module
orclass
blocks, not source file boundaries (as in Python.)The code for a
module
orclass
, may span any number of source files. Or in other words, any source file can open anymodule
orclass
namespace(s) and modify them dynamically during runtime (unless they are frozen.)Lastly.. be very aware that
include
,extend
,require
,load
andusing
... work different in Ruby, than in Python. -
Dan and thomthom, thank you so much! This is incredibly useful information on Ruby and where to start in the documentation. I'll have a go and report back if I get stuck!
thomthom: Yup, literally every face also nested within components and groups. Output is a simple text file.
-
Another thing to beware of: SketchUp stores ALL units in inches - internal. Always. It is only right when a unit is displayed in the UI that it is converted into the model units. So all coordinate data you get from SketchUp is in the Length class - which is a subclass of Float with extra methods to convert into model units for presentation. Something to beware of when you output the data.
More info on units in SketchUp: http://www.thomthom.net/thoughts/2012/08/dealing-with-units-in-sketchup/
Advertisement