0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

windows11で、sketchup6 その8

Last updated at Posted at 2025-03-28

概要

windows11に、sketchup6を入れてみた。
rubyで、3Dを書く。

問題見つけた。

sketchup v6 には、geomクラスが無い。

写真

image.png

サンプルコード


class Geom::Point3d
	def dot(v)
		self.x * v.x + self.y * v.y + self.z * v.z
	end
end

class Geom::Vector3d
	def scale_by(n)
		self.x *= n
		self.y *= n
		self.z *= n
		self
	end
	def to_point3d
		Geom::Point3d.new(*self.to_a)
	end
end

class Geom::PolygonMesh
	def add_revolved_points(pts, axis, numsegments)
		numpts = pts.length
		if (numpts < 2)
			raise ArgumentError, "At least two points required", caller
		end
		planar = true
		angle = Math::PI * 2
		da = angle / numsegments
		t = Geom::Transformation.rotation(axis[0], axis[1], da)
		index_array = []
		for pt in pts do
			if (pt.on_line?(axis))
				index_array.push([self.add_point(pt)])
			else
				indices = []
				for i in 0...numsegments do
					indices.push(self.add_point(pt))
					pt.transform!(t)
				end
				index_array.push indices
			end
		end
		i1 = index_array[0]
		for i in 1...numpts do
			i2 = index_array[i]
			n1 = i1.length
			n2 = i2.length
			nest if (n1 < numsegments && n2 < numsegments)
			for j in 0...numsegments do
				jp1 = (j + 1) % numsegments
				if (n1 < numsegments)
					self.add_polygon i1[0], i2[jp1], i2[j]
				elsif (n2 < numsegments)
					self.add_polygon i1[j], i1[jp1], i2[0]
				else
					if (planar)
						self.add_polygon i1[j], i1[jp1], i2[jp1], i2[j]
					else
						self.add_polygon i1[j], i1[jp1], i2[jp1]
						self.add_polygon i1[j], i2[jp1], i2[j]
					end
				end
			end
			i1 = i2
		end
	end
	def add_extruded_points(pts, center, dir, angle, numsegments)
		numpts = pts.length
		if (numpts < 2)
			raise ArgumentError, "At least two points required", caller
		end
		vec = Geom::Vector3d.new dir
		distance = vec.length
		dz = distance / numsegments
		da = angle / numsegments
		vec.length = dz
		t = Geom::Transformation.translation vec
		r = Geom::Transformation.rotation center, dir, da
		tform = t * r
		index_array = []
		for i in 0...numsegments do
			indices = []
			for pt in pts do
				indices.push(self.add_point(pt))
				pt.transform!(tform)
			end
			index_array.push indices
		end
		i1 = index_array[0]
		for i in 1...numsegments do
			i2 = index_array[i]
			for j in 0...numpts do
				k = (j + 1) % numpts
				self.add_polygon -i1[j], i2[k], -i1[k]
				self.add_polygon i1[j], -i2[j], -i2[k]
			end
			i1 = i2
		end
	end
end

def create_dome
	model = Sketchup.active_model
	entities = model.active_entities
	r = 5
	n90 = 5
	smooth = 12
	arcpts = [] 
	delta = Math::PI / (2 * n90)
	for i in 0..n90 do
		angle = delta * i
		cosa = Math.cos(angle)
		sina = Math.sin(angle)
		arcpts.push([r * cosa, 0, r * sina])
	end
	numpoly = n90 * n90 * 4
	numpts = numpoly + 1
	mesh = Geom::PolygonMesh.new(numpts, numpoly)
	mesh.add_revolved_points(arcpts, [ORIGIN, Z_AXIS], n90 * 4)
	entities.add_faces_from_mesh(mesh, smooth)
end

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?