[ Code ] Scarpino's SphereTool Sample
This edition is a combo Library & Mixin module.
ver 1.0.0
- initial posting (not tested)
- draw_group() method: fixed call to draw()
4th arg was passing a group object, instead of an entities object.
- Code: Select all
# ---------------------------------------------------------
# file : 'sphereutil.rb'
# by : Dan Rathbun
# ---------------------------------------------------------
# Example of combo Library/Mixin module, sphere utilities.
# based on draw code from SphereTool example...
# from "Automatic SketchUp" by Matthew Scarpino, pg 260.
# ---------------------------------------------------------
# Versions:
#
# 1.0.0 : 02-09-2012
#
# 1.1.0 : 02-10-2012
# ~ draw_group() method: fixed call to draw()
# 4th arg now: sgrp.entities, was: sgrp.
#
# ---------------------------------------------------------
require('sketchup.rb')
module Scarpino # <-- Author's namespace
module SphereUtil # <-- plugin namespace
### CONSTANTS
#
VERSION = '1.1.0'
# make the following methods work as library functions
# and as instance methods when this module is mixed-in.
module_function
# draw()
#
# Args:
# center, # [Geom::Point3d] The sphere's center point.
# size = 1, # if [Numeric] then the length of the radius,
# # if [Geom::Point3d] a point on the surface.
# segs = 24, # [Integer] number of segments used for surface.
# ents = Sketchup.active_model.active_entities, # [Entities]
# mod = Sketchup.active_model # [Sketchup::Model]
#
# Returns an array of entities belonging to the new sphere.
#
def draw(center, size=1, segs=24,
ents = Sketchup.active_model.active_entities,
mod = Sketchup.active_model
)
before = ents.to_a
segs = segs.to_i
# Draw the circles
begin
mod.start_operation('Draw Sphere')
#
if size.is_a?(Numeric)
rad = size
else
rad = center.distance( size )
end
circle = ents.add_circle( center.position, [1, 0, 0], rad, segs )
path = ents.add_circle( center.position, [0, 1, 0], rad + 1, segs )
circle_face = ents.add_face( circle )
# Extrude the sphere and erase the extrusion path
circle_face.followme( path )
ents.erase_entities( path )
#
mod.commit_operation
rescue
mod.abort_operation
return nil
else
before - ents.to_a
end
end
# draw_group()
#
# Args:
# center, # [Geom::Point3d] The sphere's center point.
# size = 1, # if [Numeric] then the length of the radius,
# # if [Geom::Point3d] a point on the surface.
# segs = 24, # [Integer] number of segments used for surface.
# ents = Sketchup.active_model.active_entities, # [Entities]
# mod = Sketchup.active_model
#
# Returns a new group object enclosing the new sphere,
# within the given entities context.
#
def draw_group(center, size=1, segs=24,
ents = Sketchup.active_model.active_entities,
mod = Sketchup.active_model
)
sgrp = ents.add_group
ret = draw(center,size,segs,sgrp.entities,mod)
if ( ret.nil? || ret==[] ) && sgrp.entities.length==0
sgrp.entities.clear!
ents.erase_entities( sgrp )
else
return sgrp
end
end
### RUN ONCE
#
unless file_loaded?(File.basename(__FILE__))
file_loaded(File.basename(__FILE__))
end
end # module SphereUtil
end # module Scarpino