Here’s a video demonstrating how my script works:
- The tetrahedron that is being created is a pre-set block called “tetra”
Here’s the script itself:
# -*- coding: utf-8 -*-
import rhinoscriptsyntax as rs
import Rhino
def insert_tetra_block_precisely():
pts = rs.GetPoints("Выберите 3 точки (A, B, C)", max_points=3)
if not pts or len(pts) != 3:
return
A, B, C = pts
O = [(A[i] + B[i] + C[i]) / 3.0 for i in range(3)]
vecAB = rs.VectorCreate(B, A)
vecAC = rs.VectorCreate(C, A)
normal = rs.VectorCrossProduct(vecAB, vecAC)
normal = rs.VectorUnitize(normal)
y_vec = rs.VectorCreate(B, O)
y_vec = rs.VectorUnitize(y_vec)
perp_in_plane = rs.VectorCrossProduct(normal, y_vec)
perp_in_plane = rs.VectorUnitize(perp_in_plane)
ray_end = [O[i] + perp_in_plane[i] * 1000 for i in range(3)]
intersection = rs.LineLineIntersection((O, ray_end), (B, C))
if not intersection:
return
Q = intersection[0]
x_vec = rs.VectorCreate(Q, O)
x_vec = rs.VectorUnitize(x_vec)
z_vec = rs.VectorCrossProduct(x_vec, y_vec)
z_vec = rs.VectorUnitize(z_vec)
y_vec = rs.VectorCrossProduct(z_vec, x_vec)
y_vec = rs.VectorUnitize(y_vec)
origin = Rhino.Geometry.Point3d(*O)
plane = Rhino.Geometry.Plane(origin, x_vec, y_vec)
xform = Rhino.Geometry.Transform.PlaneToPlane(
Rhino.Geometry.Plane.WorldXY, plane)
block_name = "tetra"
if rs.IsBlock(block_name):
rs.InsertBlock2(block_name, xform)
insert_tetra_block_precisely()
My goal is to upgrade the script and make it create a new tetrahedron based on surface, not on 3 points. On a video, you can see the problem when I select points counterclockwise: the new tetrahedron just flips inside another one. I’m still not sure how to control that, so the thing with worrking based on existing surface makes things harder.
How do I make script work using the surface-based creation? I was thinging about making it on pure math (calculating all of the new geometry based on the coordinates of selected triangle surface), but I need to work with blocks because later, when the structure is finished, the block will be replaced with a more complex tetrahedron-like model (it will have rods and more details)
Thanks in advance for anyone who gets interest in solving such a problem!