as a beginner ruby for sketchup,I’m trying to develop an extension.
as the title,I want to select some groups or components and get the area of them ,I’ve check the ruby-api for sketchup ,there’s no example ,I think someone can help me here ,thanks so much!
Firstly you need to select group or component, then check it’s what you want… e.g.
group = Sketchup.active_model.active_entities.grep(Sketchup::Group)
Next get a list of its geometry that will have an area - e.g.
faces = group.entities.grep(Sketchup::Face) if group
Next get their area…
area=0.0
faces.each{|face| area+=face.area }
The area is always in sq", so you might want to apply a conversion factor to get it into something else - e.g. sq’ or m³
This doesn’t return a single group. You need to append .first
at the end of that.
group = Sketchup.active_model.active_entities.grep(Sketchup::Group).first
In most cases you want to present it in the current model’s units. Then you can use Sketchup.format_area
area = 0.0
faces.each { |face| area+=face.area }
puts Sketchup.format_area(area)
The above snippets do not take into account if the selected instances are scaled.
Try this …
def selected_area()
model = Sketchup.active_model
picks = model.selection
return 0.0 if picks.empty?
faces = picks.grep(Sketchup::Face)
area = faces.empty? ? 0.0 : area_total(faces)
insts = picks.grep(Sketchup::Group) + picks.grep(Sketchup::ComponentInstance)
insts.each do |inst|
ents = inst.definition.entities rescue inst.entities
faces = ents.grep(Sketchup::Face)
area += area_total(faces, inst.transformation)
end
area
end
if [].respond_to?(:sum) # Ruby 2.5+
def area_total(faces, t = IDENTITY)
faces.sum { |f| f.area(t) }
end
else # Ruby < 2.5
def area_total(faces, t = IDENTITY)
faces.inject(0.0) { |sum,f| sum + f.area(t) }
end
end
I’ve try the code,but it’s not work,as the picture
and,I don’t understand 【grep(sketchup::group)】【area = 0.0】,can u explain these words?
as a beginner without ruby-knowledge,it’s too hard to understand these grammar
thank u so much again!!
wow,tt god!
thank you for your help,but my question is too basic。
actually, I want to get some whole Opensource extensions code with explanation。then i can study it myself 。
Can you recommend relevant resources?thank u so much again
We have some tutorials and example on GitHub: GitHub - SketchUp/sketchup-ruby-api-tutorials: SketchUp Ruby API Tutorials and Examples
If you look at the tutorials
folder you find two tutorials where almost every single line is explained.
Then the examples
folder have more examples, less comments, but covers more topics.
It says this because that is the last thing that happened, meaning that area_total
is the name of a method that was just defined.
The web-based Ruby Console is misleading. It loads the code and does not actually run any methods unless you call them.
To get the area, you must call the method named selected_area()
.
Along with the above snippet … here is context menu item that you can right-click on the selection …
UI.add_context_menu_handler do |popup|
if !Sketchup.active_model.selection.empty?
popup.add_item("Area selected...") {
area = selected_area()
UI.messagebox("Area selected: #{Sketchup::format_area(area)}")
}
end
end
I created a pinned set of Ruby learning resources in this category:
OK, thanks a lot
perfect resources!!
thank you!!
Les us know if some things are unclear with those examples/tutorials. We aim to improve them, but we rely on feedback.