Adding Cylinders to Sketch
-
I am trying to add cylinders to the top of a board programmatically.
I have attached the a graphic of the desired result, the circles would pushpull up 5"
Any help is greatly appreciated.
Corbin
The current script is as follows:
require 'sketchup' UI.menu("PlugIns").add_item("Draw board") { # Call our new method. draw_board } def draw_board # Get "handles" to our model and the Entities collection it contains. model = Sketchup.active_model entities = model.entities # Create a series of "points", each a 3-item array containing x, y, and z. pt1 = [0, 0, 0] pt2 = [72, 0, 0] pt3 = [72, 42, 0] pt4 = [0, 42, 0] # Call methods on the Entities collection to draw stuff. new_face = entities.add_face pt1, pt2, pt3, pt4 new_face.pushpull 0.75 normal_vector = Geom;;Vector3d.new(0,0,1) radius = 0.2 (1..10).each { |i| # Draw a circles center_point = Geom;;Point3d.new(1,i,0) edgearray = entities.add_circle center_point, normal_vector, radius first_edge = edgearray[0] arccurve = first_edge.curve } end
-
(1..10).each { |i| # Draw a circles center_point = Geom;;Point3d.new(1,i,0) edgearray = entities.add_circle center_point, normal_vector, radius first_edge = edgearray[0] # The face will be the one where the outer loop matches the edge array returned by add_circle face = first_edge.faces.select { |f| (f.outer_loop.edges & edgearray).length == edgearray.length }.first face.pushpull(10) }
-
That is what I needed, thank you ThomThom.
I am creating several thousand of these pegs in different locations on the board,I have noticed a real processing hit.
I read that it would be better to create these objects as components and then add them dynamically? I tried to do a basic example of a component instance and transformation(seems simple with a simple face, but confusing in cylinder)What am I missing?
model = Sketchup.active_model entities = model.entities # Create a series of "points", each a 3-item array containing x, y, and z. pt1 = [0, 0, 0] pt2 = [20, 0, 0] pt3 = [20, 20, 0] pt4 = [0, 20, 0] # Call methods on the Entities collection to draw stuff. new_face = entities.add_face pt1, pt2, pt3, pt4 new_face.pushpull 0.75 # Draw a circle on the ground plane around the origin. center_point = Geom;;Point3d.new(1,1,0) normal_vector = Geom;;Vector3d.new(0,0,1) radius = 0.3 entities = Sketchup.active_model.entities edgearray = entities.add_circle center_point, normal_vector, radius first_edge = edgearray[0] face = first_edge.faces.select { |f| (f.outer_loop.edges & edgearray).length == edgearray.length }.first group = Sketchup.active_model.entities.add_group face.pushpull 3 face = group.entities.addface face comp = group.to_component transformation = Geom;;Transformation.new([2,1,0]) componentinstance = entities.add_instance(comp.definition, transformation)
-
When adding groups components programmatically you got to think somewhat different from when you model.
When you model you first make the geometry and then group or make a component out of it.When you program you create the group or component first and then add the geometry directly inside it.
model = Sketchup.active_model entities = model.entities # Create a series of "points", each a 3-item array containing x, y, and z. pt1 = [0, 0, 0] pt2 = [20, 0, 0] pt3 = [20, 20, 0] pt4 = [0, 20, 0] # Call methods on the Entities collection to draw stuff. new_face = entities.add_face pt1, pt2, pt3, pt4 new_face.pushpull 0.75 # Draw a circle on the ground plane around the origin. center_point = Geom;;Point3d.new(1,1,0) normal_vector = Geom;;Vector3d.new(0,0,1) radius = 0.3 # Make a new component definition comp_def = model.definitions.add("Cylinders") # Generate the geometry. Note that component definitions has and Entities collection - as model does. edgearray = comp_def.entities.add_circle(center_point, normal_vector, radius) first_edge = edgearray[0] face = first_edge.faces.select { |f| (f.outer_loop.edges & edgearray).length == edgearray.length }.first face.pushpull(3) # Now add an instance of hte component to the model transformation = Geom;;Transformation.new([2,1,0]) componentinstance = entities.add_instance(comp_def, transformation)
Advertisement