A basic ruby plugin question about spheres

Hello,

I have been working with computers for a while, but new to Ruby! I am in awe at the potential ruby and Sketchup bring!

Anyway, as I set out to develop what (I hope) will be some useful tools, I encountered a problem. I am trying to create a sphere, by creating a circle, a face, and having the face “followme” the circle. I started off by offsetting the face from the origin a distance equal to the circle’s radius, creating a torus (it "followme"s the circle nicely). When I start to reduce this offset distance the torus starts to become more sphere-like until, when the distance is something like radius/5, Sketchup blows up and a bug is generated. The code for the successful torus is as follows:

def draw_sphere
  UI.messagebox "in draw_sphere2"
  
  model = Sketchup.active_model
  entities = model.active_entities
  
  radius = 10

  centerpoint1 = Geom::Point3d.new( 0, 0, 0 )
  vector1 = Geom::Vector3d.new( 0,1,0 )
  vector1n = vector1.normalize!
  circle1 = entities.add_circle centerpoint1, vector1n, radius

  centerpoint2 = Geom::Point3d.new( radius, 0, 0 )
  vector2 = Geom::Vector3d.new( 0,0,1 )
  vector2n = vector2.normalize!
  circle2 = entities.add_circle centerpoint2, vector2n, radius
  
  face2 = entities.add_face circle2
  face2.followme( circle1 )   
end

I imagine this demonstrates some basic ignorance on my part about how this works, but I would like to know how to resolve it. In order to run this, I added an item to my UI.menu(“Plugins”), which seems to work well.

Thank you,
Matthew

If you are creating a sphere, try using half a circle as the face.

When you use geometry that extends past the axis of revolution, it folds in on itself and can create interesting (usually unexpected or undesired) results.

I can reproduce this crash on my Mac. I sent in the BugSplat with reference to this topic. Did you also? Although it is possible that the manipulation you are trying to do is mathematically illegal (such as ending with a divide by zero), the actual crash is due to an illegal address deep in the SketchUp code for followme. In other words, SketchUp failed to trap a bad operation and crashed as a result.

seems like it’s a small geometry for unit size thing…
with model units set at meters, it survives…

john

Changing the model units doesn’t affect the internal unit - only the UI presentation. If small edges/faces is a problem one has to scale up the actual units of the geometry.

your totally right, I see now why didn’t it blow up for my test?

radius = 0.5 works…

centerpoint2 = Geom::Point3d.new( radius/20, 0, 0 )

goes pop

that’s a very strange place to apply a variable?

john

Here’s something even stranger: if I comment out the followme (so that the circle path and face are drawn) and then do the follow me via the GUI, it does not crash. But the identical values when allowed to run followme within the ruby cause a BugSplat. This suggests to me that there is a cleanup being done in the GUI that hasn’t had a chance yet in the Ruby, and without this cleanup the internals of followme fail - quite possibly due to small geometry, but it still should have been trapped instead of causing a SEGV!

Could be Integer division which says 2 / 3 = 0 for example. Try Floats.

Nope! Still splats when I use floats!

what if you wrap it (ratio/20.to_f)

If anyone has submitted BugSplats - what SketchUp versions are you using, and what OS (win or mac)?
Also, did you enter any details we can use to look up the specific crash?

I added the same string as above,
centerpoint2 = Geom::Point3d.new( radius/20, 0, 0 )
latest version of SU…
drivenupthewall is part of my email address…

I submitted a splat from Mac 15.3.329. In the comments I included a link back to this topic. It has my name (Stephen Baumgartner) on it.

Thanks guys - found them.

I can reproduce this on SU2015 Windows as well.

It’s crashing somewhere in the FollowMe operation when it tries to create the curve objects for the extrusion. I don’t know more than that right now. I’ll be filing an issue internally for this.

Thanks for reporting and apologies for the inconvenience.

Thank you all for the replies. There is a lot to learn, but probably no better place than this forum to get started.

I attempted a version of jimhami42’s suggestion of using a half circle as the face. I ran a semicircle from 0 to pi radians, and attempted to get the circle to followme that, expecting that in this case all the points of the sphere would be covered only once and hopefully eradicate problems that way. The code that I used is as follows:

CODE:

def draw_sphere
  
  UI.messagebox "in spherical2"
  
  model = Sketchup.active_model
  entities = model.active_entities
  
  radius = 10

  #the path semicircle - the path about which the extrusion is to take place
  centerpoint1 = Geom::Point3d.new( 0, 0, 0 )
  normal = Geom::Vector3d.new( 0,1,0 ) #the normal to our semicircle 
  xaxis = Geom::Vector3d.new( 1,0,0 )  #the axis (here, x), along which to reference our arc
  start_angle = 0.0                    #the angle, relative to our xaxis, with which to start our arc
  end_angle = Math::PI                 #end pi radians later, a semicircle
  edge_array = entities.add_arc centerpoint1, xaxis, normal, radius, start_angle, end_angle

  #the circle and face to be extruded
  centerpoint2 = Geom::Point3d.new( 0, 0, 0 )
  vector2 = Geom::Vector3d.new( 0,0,1 )
  vector2n = vector2.normalize!
  circle2 = entities.add_circle centerpoint2, vector2n, radius
  
  face2 = entities.add_face circle2
  
  face2.followme( edge_array )   

end

END-CODE

I imagine/know I’m not using add_arc properly. I’ve looked online but without much success for good examples. I would have thought this would do what I had hoped but clearly it’s not.

Thanks again.

You have the concepts of path and profile reversed! The “profile” is the face that will be extruded, and the “path” is the edge array along which you want to extrude.

profile.followme(path)

To do what Jim suggested, you need to make the path a full circle and the profile (face) a half circle.

I took the liberty to edit your post such that the code block got formatted properly.