[Code] Scarpino's SphereUtil Sample ver 1.1.0

[Code] Scarpino's SphereUtil Sample ver 1.1.0

Postby Dan Rathbun » Thu Feb 09, 2012 11:26 pm

Here's slightly modified sphere drawing code from:
[ Code ] Scarpino's SphereTool Sample

This edition is a combo Library & Mixin module.

ver 1.0.0
  • initial posting (not tested)
ver 1.1.0
  • 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
0
Last edited by Dan Rathbun on Sat Feb 11, 2012 6:48 pm, edited 3 times in total.
    I'm not here much anymore. But a PM will fire email notifications.
    User avatar
    Dan Rathbun 
    PluginStore Author
    PluginStore Author
     

    Re: [Code] Scarpino's SphereUtil Sample

    Postby Dan Rathbun » Thu Feb 09, 2012 11:38 pm

    driven wrote:With Mat's help, I attempted to have this one work along the user drawn axis, so you could e.g. create a string of pearls.

    Could you add or suggest how that could be done?


    I would suppose you would write a loop of some kind and call the lib methods x number of times, using the same radius, but incrementing the center point within the loop (2 * rad ?). The result would be a line of spheres.

    If you wished to write a Tool, where the user draws the path, you can mix in the methods using:

    Code: Select all
    module Driven
      class PearlPathTool

        require('Scarpino/sphereutil.rb')
        include(Scarpino::SphereUtil)

        # now your tool has the mixin's methods

      end#class
    end
    0
      I'm not here much anymore. But a PM will fire email notifications.
      User avatar
      Dan Rathbun 
      PluginStore Author
      PluginStore Author
       

      Re: [Code] Scarpino's SphereUtil Sample ver 1.1.0

      Postby Dan Rathbun » Fri Feb 10, 2012 9:41 am

      Fixed a small booboo (see the docheader in file.)

      @John.. perhaps a good example tool, to make a path, might be a modified edition of the "Examples/linetool.rb"
      0
        I'm not here much anymore. But a PM will fire email notifications.
        User avatar
        Dan Rathbun 
        PluginStore Author
        PluginStore Author
         

        SketchUcation One-Liner Adverts

        by Ad Machine » 5 minutes ago



        Ad Machine 
        Robot
         



         

        Return to Developers' Forum

        Who is online

        Users browsing this forum: No registered users and 5 guests

        Visit our sponsors: