Face pushpull goes in different directions

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.

2 Likes

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…

“opposite direction” ?

1 Like

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 think that will do the trick. Once I create my face (always flat) I will check the .normal z value and adjust accordingly.

Thanks everyone for the quick answer!

1 Like

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.