loft in python API

Post Reply
ergarrett
Posts: 5
Joined: Wed Oct 09, 2024 8:07 pm

loft in python API

Post by ergarrett » Wed Oct 23, 2024 7:27 pm

Hi STKO Team -

Is there a function in the Python API for loft? I've used the Python API to create a top and bottom cylinder face, with the top face at an incline. Using the STKO interface, I can create the vertical face with Loft and then use Make Compound to create the solid? Is this possible to do in the Python API as well or how do you recommend doing this?

I've attached what I've done so far... thank you for your help!
Attachments
cylinder - python.zip
(24 KiB) Downloaded 122 times

STKO Team
Posts: 3066
Joined: Tue Oct 29, 2019 8:45 am

Re: loft in python API

Post by STKO Team » Fri Oct 25, 2024 9:13 am

Unfortunately the Loft command is not yet exported to python (we put it in our todo list and will be available in the next version).
If you can approximate the curved vertical surface with piecewise planar faces, we can show you how to do it. Is it fine for you?

ergarrett
Posts: 5
Joined: Wed Oct 09, 2024 8:07 pm

Re: loft in python API

Post by ergarrett » Fri Oct 25, 2024 12:06 pm

I think that should be fine if you can show me how to do it? thank you!

STKO Team
Posts: 3066
Joined: Tue Oct 29, 2019 8:45 am

Re: loft in python API

Post by STKO Team » Mon Oct 28, 2024 10:47 am

Code: Select all

from PyMpc import *
import math
import itertools

def _ellipse(center, Rx, Ry, dz, dx, nseg):
	dy = dz.cross(dx)
	R = Math.mat3(dx, dy, dz)
	nseg = max(3, nseg)
	da = 2.0*math.pi/nseg
	vertices = [None]*nseg
	for i in range(nseg):
		ia = da*i
		p = R*Math.vec3(Rx*math.cos(ia), Ry*math.sin(ia), 0.0) + center
		vertices[i] = FxOccBuilder.makeVertex(p.x, p.y, p.z)
	edges = [FxOccBuilder.makeEdge(vertices[i], vertices[j]) for i,j in zip(range(nseg), itertools.chain(range(1, nseg), [0]))]
	return (edges, vertices)

try:
	
	# init
	App.clearTerminal()
	doc = App.caeDocument()
	
	# avoid Regenerate on working thread
	App.setRegenerateOnWorkingThreadFlag(False)
	
	# number of segments
	nseg = 30
	
	# bottom ellipse
	# center
	C = Math.vec3(0.0, 0.0, 0.0)
	# normal vector (local Z)
	N = Math.vec3(0.0, 0.0, 1.0)
	# direction vector (local X)
	X = Math.vec3(1.0, 0.0, 0.0)
	# radii (equal for a circle)
	Rx = 1.5
	Ry = 1.5
	bottom_edges, bottom_vertices = _ellipse(C, Rx, Ry, N, X, nseg)
	
	# top ellipse
	# center
	C = Math.vec3(0.0, 0.0, 6.0)
	# normal vector (local Z = inclined in the global XZ plane)
	N = Math.vec3(-math.sin(math.pi/6), 0.0, math.cos(math.pi/6))
	# direction vector (local X, try to aling it to the global X)
	Y = Math.vec3(0.0, 1.0, 0.0)
	X = Y.cross(N).normalized()
	# radii (compute them so that their projection is a circle)
	Ry = 1.5
	Rx = Ry / X.x
	top_edges, top_vertices = _ellipse(C, Rx, Ry, N, X, nseg)
	
	# now we have the edges and their vertices.
	# we can use them to create vertical lines, in this way they will
	# share the same vertices in memory and we can build the final wire using
	# FxOccBuilder.makeWire([...])
	vertical_edges = [FxOccBuilder.makeEdge(vi, vj) for vi, vj in zip(bottom_vertices, top_vertices)]
	
	# allocate a list of faces
	faces = []
	
	# build the bottom face
	faces.append(FxOccBuilder.makeFace(FxOccBuilder.makeWire(bottom_edges)))
	
	# build the top face
	faces.append(FxOccBuilder.makeFace(FxOccBuilder.makeWire(top_edges)))
	
	# build the side faces
	for B,T,L,R in zip(bottom_edges, top_edges, vertical_edges, vertical_edges[1:]+[vertical_edges[0]]):
		faces.append(FxOccBuilder.makeFace(FxOccBuilder.makeWire([B,R,T,L])))
	
	# build the shell
	shell = FxOccBuilder.makeShell(faces)
	
	# fill the shell with a solid
	solid = FxOccBuilder.makeSolid(shell)
	
	geom = MpcGeometry(doc.geometries.getlastkey(0)+1, 'c', solid)
	doc.addGeometry(geom)
	App.runCommand('Regenerate')
	
finally:
	
	# reset Regenerate on working thread
	App.setRegenerateOnWorkingThreadFlag(True)



ergarrett
Posts: 5
Joined: Wed Oct 09, 2024 8:07 pm

Re: loft in python API

Post by ergarrett » Mon Oct 28, 2024 3:30 pm

Thank you very much for your help!

STKO Team
Posts: 3066
Joined: Tue Oct 29, 2019 8:45 am

Re: loft in python API

Post by STKO Team » Mon Oct 28, 2024 5:08 pm

You're welcome

Post Reply