Here's the code in full, I'd only held off posting it till now as I was keen to learn enough to fix it myself, but as this is getting frustrating I'll welcome any solutions
the initialization of the variables is hashed out because I changed it to initialize in the def initialize sub, rather than here. Not sure if that's good or not though as they might need to be "@point" to carry the value over.
Hope you can decipher it with all the wordwraping, I tend to leave long lines and comments, I was going to tidy up the defunct code hashed out when I finally got it working.
def onLButtonDown(flags, x, y, view)
# When the user clicks the first time, we switch to getting the
# second point. When they click a second time we create the line
#point=[], perpvector=[], angle=0.0, tr=nil
if( @state == 0 )
@ip1.pick view, x, y
if( @ip1.valid? )
@state = 1
Sketchup;;set_status_text $exStrings.GetString("Select second point (top right)"), SB_PROMPT
@xdown = x
@ydown = y
face = @ip1.face
puts "facenormal " + face.normal.to_s
angle = Z_AXIS.angle_between(face.normal)
perpvector = Z_AXIS.cross(face.normal)
point = @ip1.position
tr = Geom;;Transformation.rotation(point,perpvector,angle) if perpvector.length!=0
#tr=Geom;;Transformation.rotation(ORIGIN, Z_AXIS, 90.degrees)
puts "point " + point.to_s, "angle " + angle.to_s, "perpvector " + perpvector.to_s, "transformation " + tr.to_s, "trans class " + tr.class.to_s, tr.class
if face == nil
UI.messagebox "The points must be on the face of an object"
self.reset
end
end
else
# create the line on the second click
if( @ip2.valid? )
self.create_geometry(@ip1.position, @ip2.position,view)
@ip2.pick view, x, y
model = Sketchup.active_model
entities = model.entities
master_group = entities.add_group()
#master_group = master_group.transform!(tr)
numsquaresX = 10 #number of squares in the X direction
xsize = 30 #size of squares in the X direction (mm)
xsize = xsize/25.4 #converts xsize into mm (from inches)
numsquaresY = 10 #number of squares in the Y direction
#ysize = 3 #size of squares in the Y direction
#ysize = ysize/25.4 #converts ysize into mm (from inches)
ysize = xsize #as we're generally only dealing with squares, edit this line out and reinstate the two above lines for rectangles
zsize = xsize
z = 0
x1 = @ip1.position.x
y1 = @ip1.position.y
z1 = @ip1.position.z
xfinal = @ip2.position.x
yfinal = @ip2.position.y
zfinal = @ip2.position.z
#puts x1, y1, z1, xfinal, yfinal, zfinal
if (xfinal - x1) > (25 * xsize)
largetest = UI.messagebox "You have selected a large area, this may make the program unstable. Suggest multiple smaller areas", MB_OKCANCEL
if largetest == 2
self.reset
end
end
while y1 < yfinal
y2 = y1 + ysize
while x1 < xfinal
x2 = x1 + xsize
pt1 = [x1, y1, z] #plot the first line
pt2 = [x1, y2, z] #plot the second line
pt3 = [x2, y2, z] #plot the third line
pt4 = [x2, y1, z] #plot the fourth line, back to start
#new_face = entities.add_face pt1, pt2, pt3, pt4 #plot a face on the square to make it visible
#group.entities.add_face pt1, pt2, pt3, pt4
group = master_group.entities.add_group
face = group.entities.add_face pt1, pt2, pt3, pt4
x1 = x2 - (xsize * 0.08) #let the next spot begin at 92% of the way across the previous
Sketchup;;set_status_text $exStrings.GetString("Working"), SB_PROMPT
#puts "Point1 " + pt1.to_s, "Point2 " + pt2.to_s, "Point3 " + pt3.to_s, "Point4 " + pt4.to_s
end
y1 = y2 - (ysize * 0.08) #let the next spot begin at 92% of the way across the previous
#x1 = x1 - (xsize * 0.92 * xstep) #go back to initial x position for next line in y
x1 = @ip1.position.x
end
master_group.transform! tr
self.reset(view)
end #end click 2
end
# Clear any inference lock
view.lock_inference
end
let me know if you want me to send the complete .rb files, as this chunk of code wont run without the other def's around it