Position_material (Translation and Rotation)

The documentation on how exactly to use this method in the API seems a bit sparse to me or at least I’m not figuring it out very well.

In this particular case I am interested in using position_material on horizontal faces in my group hence you will see that I use the normal vector for the faces of interest in my test code below.

I have been getting the pure translation to work, that one is easy with only two points:

# Pure Translation

				pt_array = []
				pt_array[0] = Geom::Point3d.new(@Flooring_offsetx,@Flooring_offsety,0)
				pt_array[1] = Geom::Point3d.new(0,0,0)

				face_list = group1.entities.grep(Sketchup::Face)
				mainfaces = face_list.find_all {|f| f.normal.parallel?(Z_AXIS)}
				for facei in mainfaces
					facei.material = @Flooring_mat
					facei.position_material(@Flooring_mat, pt_array, true)
				end

The one that is giving me problems is the pure rotation (eight points), it is rotating as expected but it is also scaling the material very small. Obviously I’m missing something here.

# Pure Rotation

			phi = @Flooring_rot.degrees
			
			pt_array = []

			pt_array[0] = Geom::Point3d.new(0,0,0)
			pt_array[1] = Geom::Point3d.new(0,0,0)

			xp2 = cos(phi)
			yp2 = sin(phi)
			pt_array[2] = Geom::Point3d.new(xp2,yp2,0)
			pt_array[3] = Geom::Point3d.new(1.0,0,0)

			xp3 = cos(phi) - sin(phi)
			yp3 = sin(phi) + cos(phi)
			pt_array[4] = Geom::Point3d.new(xp3,yp3,0)
			pt_array[5] = Geom::Point3d.new(1.0,1.0,0)

			xp4 = -sin(phi)
			yp4 = cos(phi)
			pt_array[6] = Geom::Point3d.new(xp4,yp4,0)
			pt_array[7] = Geom::Point3d.new(0,1.0,0)

			face_list = group1.entities.grep(Sketchup::Face)
			mainfaces = face_list.find_all {|f| f.normal.parallel?(Z_AXIS)}
			for facei in mainfaces
				facei.material = @Flooring_mat
				facei.position_material(@Flooring_mat, pt_array, true)
			end

I do not want any scaling, just rotation.

I’m comparing what I have here with what Thomas @tt_su is doing here and my math seems to be correct:

The scaling is shrinking my texture down by 48X, which also coincides with the size of my material, something is going on here.

This seems to do the trick but maybe there is a better way to accomplish the same thing:

# Pure Rotation

			twidth = @Flooring_mat.texture.width
			theight = @Flooring_mat.texture.height

			phi = @Flooring_rot.degrees
			
			pt_array = []

			pt_array[0] = Geom::Point3d.new(0,0,0)
			pt_array[1] = Geom::Point3d.new(0,0,0)

			xp2 = twidth * cos(phi)
			yp2 = twidth * sin(phi)
			pt_array[2] = Geom::Point3d.new(xp2,yp2,0)
			pt_array[3] = Geom::Point3d.new(1.0,0,0)
 
			xp3 = twidth * cos(phi) - theight * sin(phi)
			yp3 = twidth * sin(phi) + theight * cos(phi)
			pt_array[4] = Geom::Point3d.new(xp3,yp3,0)
			pt_array[5] = Geom::Point3d.new(1.0,1.0,0)

			xp4 = -theight * sin(phi)
			yp4 = theight * cos(phi)
			pt_array[6] = Geom::Point3d.new(xp4,yp4,0)
			pt_array[7] = Geom::Point3d.new(0,1.0,0)

			face_list = group1.entities.grep(Sketchup::Face)
			mainfaces = face_list.find_all {|f| f.normal.parallel?(Z_AXIS)}
			for facei in mainfaces
				facei.material = @Flooring_mat
				facei.position_material(@Flooring_mat, pt_array, true)
			end

Translation and a Rotation:

# Translation and Rotation

				x1 = @Flooring_offsetx
				y1 = @Flooring_offsety

				twidth = @Flooring_mat.texture.width
				theight = @Flooring_mat.texture.height

				phi = @Flooring_rot.degrees
			
				pt_array = []

				pt_array[0] = Geom::Point3d.new(x1,y1,0)
				pt_array[1] = Geom::Point3d.new(0,0,0)

				xp2 = twidth * cos(phi) + x1
				yp2 = twidth * sin(phi) + y1
				pt_array[2] = Geom::Point3d.new(xp2,yp2,0)
				pt_array[3] = Geom::Point3d.new(1.0,0,0)

				xp3 = twidth * cos(phi) - theight * sin(phi) + x1
				yp3 = twidth * sin(phi) + theight * cos(phi) + y1
				pt_array[4] = Geom::Point3d.new(xp3,yp3,0)
				pt_array[5] = Geom::Point3d.new(1.0,1.0,0)

				xp4 = -theight * sin(phi) + x1
				yp4 = theight * cos(phi) + y1
				pt_array[6] = Geom::Point3d.new(xp4,yp4,0)
				pt_array[7] = Geom::Point3d.new(0,1.0,0)

				face_list = group1.entities.grep(Sketchup::Face)
				mainfaces = face_list.find_all {|f| f.normal.parallel?(Z_AXIS)}
				for facei in mainfaces
					facei.material = @Flooring_mat
					facei.position_material(@Flooring_mat, pt_array, true)
				end

Thanks a lot for sharing!

Just an advice how to make your code more readable (colorized) in a forum:

When you insert your code use the ruby keyword after the first 3 ```
:wink:
colorizecode

4 Likes