How to draw a ellipse useing ruby api?

i’d like to draw a ellipse in su using ruby, but it seems having no api to draw ellipse.

That’s right. You have to write your own procedure.
Just as the circle is made of edges, you have to calculate the points of the ellipse and connect them with edges.
Like others did:
https://sketchucation.com/pluginstore?listtype=1&author=0&category=0&search=ellipse&submit=%3F

Here, study this …

module Naive
  module Geom

    extend self

    def ellipse(center, normal, major_axis, minor_axis, numsegs)
      model = Sketchup.active_model
      model.start_operation('Ellipse',true)
      #
      ###
        #
        center = Geom::Point3d.new(center)
        normal = Geom::Vector3d.new(normal)
        ents  = model.active_entities
        grp   = ents.add_group
        gents = grp.entities
        gents.add_cpoint(center)
        gents.add_circle(center, Z_AXIS, major_axis/2.0, numsegs.to_i)
        t = Geom::Transformation.scaling(1.0, minor_axis.to_f/major_axis.to_f, 1.0)
        grp.transform!(t)
        unless normal == Z_AXIS
          rot = Geom::Transformation.rotation(
            center, Z_AXIS.cross(normal), Z_AXIS.angle_between(normal)
          )
          grp.transformation= grp.transformation * rot
        end
        #
      ###
      #
      model.commit_operation
      #
      return grp
    rescue => error
      model.abort_operation
      puts error.inspect
    end ### ellipse()

  end
end

Example call:

ellipse = Naive::Geom.ellipse([3,5,7], [1,1,1], 20, 13.2, 36)

You can explode the group afterwards.

1 Like

UPDATED above example to wrap within an undo operation.

For those who are wondering what @DanRathbun 's snippet does: it replicates in Ruby what you would do to draw an ellipse by hand in SketchUp. That is, it draws a circle, scales it along the minor axis, then rotates it to the specified normal. The scaling will produce non-uniform edge length in the ellipse, but it is much much more complicated math to create an ellipse with equal length edges!

4 Likes