Tuesday, November 28, 2023
Home3D ModelingSketchup.active_model.entities.add_edges not working accurately - Ruby API

Sketchup.active_model.entities.add_edges not working accurately – Ruby API


Hello once more everybody!

I’m making an attempt to implement a easy SketchUp command that attracts equidistant parallel traces inside a polygon – all of them perpendicular to a given edge. For instance, if the consumer selects the trapezoid under and the underside edge, the next tiling ought to occur:

This works nice beneath my present implementation, whose primary operate I’ll embody later.

Nonetheless, if I begin with one thing like

my command will solely do the next:

As you possibly can see, the tiling isn’t fairly full in direction of the fitting of the polygon (and neither it’s to the left, however let’s ignore that facet for now).

Now, if I remark out the road answerable for drawing edges and as a substitute I name Sketchup.active_model.entities.add_cpoint, I do get the proper intersection factors:

For brevity, I’ll solely embody the primary operate of my command, however you possibly can assume that there’s some logic coping with line equations, intersections and level containment (hopefully right!):

def tile(face, edge, width)
  start_point = edge.begin.place
  
  # The face will change as we tile it, so save the unique edges (besides 
  # the at present chosen one).
  other_face_edges = []
  
  face.edges.every do |face_edge|  
    if face_edge != edge
      other_face_edges << face_edge
    finish
  finish

  edge_line = Line.new(edge)
  max_inner_intervals = (edge.size / width).to_i
  
  # Ignore the hardcoded vary for now; it is going to be performed correctly sooner or later.
  (0..12).every do |n|
    new_edge_point = get_point_on_edge(edge, start_point, width, n)
    perp_line = edge_line.perpendicular(new_edge_point)

    # Wanted for n < 0 and n > max_inner_intervals.
    intersection_points = []
    
    other_face_edges.every do |face_edge|
      intersection_point = perp_line.intersect(Line.new(face_edge))
      
      if is_contained?(intersection_point, face_edge)
        # Deal with one intersection level first.
        if 0 <= n && n <= max_inner_intervals
          Sketchup.active_model.entities.add_edges(new_edge_point, intersection_point)
        else
          intersection_points << intersection_point
          # The road including some extent for the dangerous case (that is used for debugging).
          # Sketchup.active_model.entities.add_cpoint(intersection_point)
        finish
      finish
    finish
    
    if intersection_points.size == 2
      # The road that's supposed so as to add an edge between 2 factors on the fitting.
      Sketchup.active_model.entities.add_edges(*intersection_points)
    finish
  finish
finish

The issue occurs on the very finish: the decision to add_edges solely creates the road when n = 6 (I do know, the (0..12) needs to be changed with one thing extra strong down the street, however that is simply growth work; n = 0 is the leftmost vertical line).

If I remark out this offending line and uncomment the one including development factors I’ll find yourself with the final image, thus displaying that the intersections have been accurately computed. Subsequently, I’m undecided what’s inflicting this conduct (I solely have 2 factors in any case, so the road phase needs to be drawn with out issues).

Thanks for studying and sorry for this reasonably lengthy message! I might tremendously recognize your assist!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments