sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    🛣️ Road Profile Builder | Generate roads, curbs and pavements easily Download

    Drawing Sphere with ruby

    Scheduled Pinned Locked Moved Newbie Forum
    sketchup
    2 Posts 2 Posters 1.0k Views 2 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.
    • K Offline
      kmk111890
      last edited by

      Hello. I've ever downloaded a ruby creates box automatically on this site. With this ruby, I would like to develop coding to create sphere shape too. I guessed adding radius was all task but didn't work. please fix errors.

      ` require 'sketchup.rb'
      def create_sphere

      @radius = 0.feet if not @radius # along Green Y axis

      prompts = ["radius", "R", "G", "B"]
      values = [@radius, @R, @G, @B]
      results = inputbox prompts, values, "Sphere"
      return if not results
      @radius, @R, @G, @B,= results

      model = Sketchup.active_model
      model.start_operation "Sphere"
      ents = Sketchup.active_model.definition.add

      center = [0, 0, 0]
      radius = @radius
      circle = ents.entities.add_circle center, [0, 0, 1], radius
      circle_face = ents.add_face circle

      path = ents.add_circle center, [0, 1, 0], radius + 1

      circle_face.followme path

      ents.erase_entities path

      circle_face.material=Sketchup::Color.new(@R, @G, @B)
      circle_face.material.alpha = 0.3

      status=model.place_component(ents)
      Sketchup.active_model.active_entities.add_instance(ents, trans)

      model.commit_operation
      end
      if( not file_loaded?("Sphere.rb") )
      utilities_menu = UI.menu("Plugins").add_submenu("Sphere")
      utilities_menu.add_item("Sphere") { create_sphere }
      end
      #-----------------------------------------------------------------------------
      file_loaded("Sphere.rb")`

      1 Reply Last reply Reply Quote 0
      • S Offline
        slbaumgartner
        last edited by

        See comments regarding various errors:

        ` require 'sketchup.rb'

        def create_sphere

        @radius = 0.feet if not @radius # along Green Y axis

        prompts = ["radius", "R", "G", "B"]
        values = [@radius, @R, @G, @B]
        results = inputbox prompts, values, "Sphere"
        return if not results
        @radius, @R, @G, @B,= results

        model = Sketchup.active_model # why keep switching between model and

        Sketchup.active_model?

        model.start_operation "Sphere"
        compdef = Sketchup.active_model.definitions.add # needed 's'

        multiple places show confusion between an Entity and its Entities collection

        ents = compdef.entities

        center = [0, 0, 0]
        radius = @radius
        circle = ents.add_circle center, [0, 0, 1], radius
        circle_face = ents.add_face circle

        assign these right away - the followme will destroy the original circle_face

        Color.new needs integers not strings

        circle_face.material=Sketchup::Color.new(@R.to_i, @G.to_i, @B.to_i)
        circle_face.material.alpha = 0.3

        path = ents.add_circle center, [0, 1, 0], radius + 1

        circle_face.followme path

        ents.erase_entities path

        place_component activates the Tool to manually place a

        ComponentInstance via GUI

        add_instance explicitly places one via Ruby

        You probably don't want to do both?

        #status=model.place_component(compdef)

        need to create trans before you can use it! This creates an identity,

        which adds the instance at the model origin

        trans=Geom::Transformation.new
        Sketchup.active_model.active_entities.add_instance(compdef, trans)

        model.commit_operation
        end

        if( not file_loaded?("Sphere.rb") )
        utilities_menu = UI.menu("Plugins").add_submenu("Sphere")
        utilities_menu.add_item("Sphere") { create_sphere }
        end
        #-----------------------------------------------------------------------------
        file_loaded("Sphere.rb")`

        Also, you should isolate your code in a module so that the method does not go into the global namespace.

        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