Drawing Sphere with ruby
-
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,= resultsmodel = Sketchup.active_model
model.start_operation "Sphere"
ents = Sketchup.active_model.definition.addcenter = [0, 0, 0]
radius = @radius
circle = ents.entities.add_circle center, [0, 0, 1], radius
circle_face = ents.add_face circlepath = 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.3status=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")` -
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,= resultsmodel = 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 circleassign 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.3path = 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
endif( 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.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better đź’—
Register LoginAdvertisement