sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    [Code] Scarpino's SphereUtil Sample ver 1.1.0

    Scheduled Pinned Locked Moved Developers' Forum
    3 Posts 1 Posters 1.2k Views 1 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Dan RathbunD Offline
      Dan Rathbun
      last edited by Dan Rathbun

      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.

      # ---------------------------------------------------------
      # 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
      

      I'm not here much anymore.

      1 Reply Last reply Reply Quote 0
      • Dan RathbunD Offline
        Dan Rathbun
        last edited by

        @driven said:

        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:

        module Driven
          class PearlPathTool
        
            require('Scarpino/sphereutil.rb')
            include(Scarpino;;SphereUtil)
        
            # now your tool has the mixin's methods
        
          end#class
        end
        

        I'm not here much anymore.

        1 Reply Last reply Reply Quote 0
        • Dan RathbunD Offline
          Dan Rathbun
          last edited by

          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"

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Buy SketchPlus
          Buy SUbD
          Buy WrapR
          Buy eBook
          Buy Modelur
          Buy Vertex Tools
          Buy SketchCuisine
          Buy FormFonts

          Advertisement