How can i draw inclined single and double lines on the every face of a given model

I have written the code but it’s not drawing in every face of the model. I have written code for the single inclined line.

def draw_hatch face,line_type,space,direction
=begin
	left_bottom=face.bounds.corner(0)
	puts left_bottom
	right_bottom=face.bounds.corner(1)
	puts right_bottom
	right_top=face.bounds.corner(5)
	puts right_top
	left_top=face.bounds.corner(4)
	puts left_top
=end
pts = face.vertices.map(&:position)
pts = pts.sort_by {|pt| pt.x }
	if pts[0].z<pts[1].z
		left_bottom=pts[0]
		left_top=pts[1]
	else
		left_bottom=pts[1]
		left_top=pts[0]
	end
	
	if pts[2].z<pts[3].z
		right_bottom=pts[2]
		right_top=pts[3]
	else
		right_bottom=pts[3]
		right_top=pts[2]
	end
	if(space=="close")
		count=20
		count_half1=count/2
		count_half2=count/2-1
	elsif space=="avg"
		count=15
		count_half1=count/2
		count_half2=count/2
	else
		count=10
		count_half1=count/2
		count_half2=count/2-1
	end
	temp1=count_half1
	temp2=count_half2
	if(line_type=="single" && direction=="right")
		len_lt_rt=left_top.distance(right_top)
		len_lt_lb=left_top.distance(left_bottom)
		gap_lt_rt=len_lt_rt/(count_half1+1)
		gap_lt_lb=len_lt_lb/(count_half1+1)
		dir_lt_rt=left_top.vector_to(right_top)
		dir_lt_lb=left_top.vector_to(left_bottom)
		while(count_half1>0)
			pt1=left_top.offset(dir_lt_rt,gap_lt_rt*count_half1)
			pt2=left_top.offset(dir_lt_lb,gap_lt_lb*count_half1)
			line1=Sketchup.active_model.entities.add_line(pt1,pt2)
			count_half1=count_half1-1
		end
		line2=Sketchup.active_model.entities.add_line(left_bottom,right_top)
		len_rb_rt=right_bottom.distance(right_top)
		len_rb_ld=right_bottom.distance(left_bottom)
		gap_rb_rt=len_rb_rt/(count_half2+1)
		gap_rb_lb=len_rb_ld/(count_half2+1)
		dir_rb_rt=right_bottom.vector_to(right_top)
		dir_rb_lb=right_bottom.vector_to(left_bottom)
		while(count_half2>0)
			pt3=right_bottom.offset(dir_rb_rt,gap_rb_rt*count_half2)
			pt4=right_bottom.offset(dir_rb_lb,gap_rb_lb*count_half2)
			line3=Sketchup.active_model.entities.add_line(pt3,pt4)
			count_half2=count_half2-1
		end
		
	end
	count_half1=temp1
	count_half2=temp2
	if(line_type=="single" && direction=="left")
		len_rt_rb=right_top.distance(right_bottom)
		len_rt_lt=right_top.distance(left_top)
		gap_rt_rb=len_rt_rb/(count_half1+1)
		gap_rt_lt=len_rt_lt/(count_half1+1)
		dir_rt_rb=right_top.vector_to(right_bottom)
		dir_rt_lt=right_top.vector_to(left_top)
		while(count_half1>0)
			pt5=right_top.offset(dir_rt_rb,gap_rt_rb*count_half1)
			pt6=right_top.offset(dir_rt_lt,gap_rt_lt*count_half1)
			line4=Sketchup.active_model.entities.add_line(pt5,pt6)
			count_half1=count_half1-1
		end
		line5=Sketchup.active_model.entities.add_line(left_top,right_bottom)
		
		len_lb_lt=left_bottom.distance(left_top)
		len_lb_rb=left_bottom.distance(right_bottom)
		gap_lb_lt=len_lb_lt/(count_half2+1)
		gap_lb_rb=len_lb_rb/(count_half2+1)
		dir_lb_lt=left_bottom.vector_to(left_top)
		dir_lb_rb=left_bottom.vector_to(right_bottom)
		
		while(count_half2>0)
			pt7=left_bottom.offset(dir_lb_lt,gap_lb_lt*count_half2)
			pt8=left_bottom.offset(dir_lb_rb,gap_lb_rb*count_half2)
			line6=Sketchup.active_model.entities.add_line(pt7,pt8)
			count_half2=count_half2-1
		end
	end
	
end

draw_hatch Sketchup.active_model.selection[0],"single","close","right"

if i give input=‘right’ then output should be like thisright
if i give input=‘left’ then output should be like this
left

After this, subsequent points in pts may not be neighboring on the face (for example the most-right points of an “E” shape are not neighboring).

You seem to assume that you can conclude certain properties of points 2 and 3. Do you apply the algorithm only to rectangles with exactly 4 vertices? Are they always oriented in the xz plane so that x corresponds to left/right?

When you have two touching rectangles and you have hatched the first of them, the edges are split into smaller edges. So the second rectangle has more than 4 vertices.

Maybe you need to revise the algorithm to work for more general cases (polygons with 3 or more vertices). Or quit the algorithm if the preconditions are not fulfilled.

For example you can get extreme limits with:

min_x = pts.map(&:x).min
max_x = pts.map(&:x).max
min_y = pts.map(&:y).min
max_y = pts.map(&:y).max

or

bounds = Geom::BoundingBox.new().add(pts)
min_x = bounds.min.x

SketchUp has (currently) only edges in the 3D model space. There are no lines.

Note that the API method add_line really should not have been named this. It should have been named add_edge, because it really adds a single edge to an entities collection.


So it is probably better to use a texture with the pattern of diagonal hashing. Look in the “Patterns” category of the Materials inspector for examples.

Get this plugin example and study it…

It is quite old, but will include some useful ideas on how to establish a face’s edges and then add ‘hatching’ edges between them, at a desired spacing, angle etc…

I am applying this algorithm just for rectangle and this rectangle can be in any plane.