Sometimes when I run the following code, it creates a box that is pushpulled up. Other times, the same values produce a box that pushpulled down. It may be other things I am doing in code elsewhere. But I have no idea. Can anyone explain why the same code with the save values would sometimes produce different results?
def CreateBoxGroup(height, length, width, x, y, z)
#Footprint
pts1 = []
pts1[0] = [x,y,z]
pts1[1] = [x + length, y, z]
pts1[2] = [x + length, y + width, z]
pts1[3] = [x, y + width, z]
group = Sketchup.active_model.entities.add_group
face = group.entities.add_face(pts1)
face.pushpull(height,true)
return group
end
The orientation of a new face is magically flipped if it is drawn on the ground plane, so that the back side faces upward. That will cause it to extrude in the opposite direction to faces placed at other z.
The PushPull goes in the direction of the face.normal
So if the face is facing down it extrudes up.
If it faces up it extrudes down.
Unfortunately you canât always be sure which way the face is orientatedâŚ
Typically a loop of vertex points makes a face oriented positively when its winding is ccw.
But if the face is flat on the ground at z=0, then no matter what the âwindingâ of its vertex points is it always faces downwards !
So itâs best to check the vertex points âwindingâ direction, and the faceâs flat z level [correcting as necessary using face.reverse etc]
Otherwise check the face.normal and use that to determine the PushPull value as + or -ve to suitâŚ
Yes - the PushPull extrusion goes contrary to the direction of the face.normal, would be a more accurate phrasingâŚ
But of course when you try it out itâll be obvious !!
Iâd recommend always checking the normal direction and reverse the face if needed.
def create_box_group(height, length, width, x, y, z)
points = [
[x, y, z],
[x + length, y, z],
[x + length, y + width, z],
[x, y + width, z]
]
group = Sketchup.active_model.entities.add_group
face = group.entities.add_face(points)
face.reverse! unless face.normal.samedirection?(Z_AXIS)
face.pushpull(height)
group
end
You could in theory figure out the exact behavior of face orientation and rely on it, but clearly stating the intention in your code makes it easier to maintain, and avoiding relying on arbitrary implementation details makes the code less fragile.