Node displacement in polar coordinates

I'm trying to write a script to move nodes on a circular selection for value dr in radial direction. I'm not so sharp with API library. Can anyone please help?

# Before running this script, select the circle edge nodes and input radial displacement dr.
# It then moves selected nodes for dr in polar coordinate system.
import math

dr = 1.0 # <-- your input (model units)

def set_node_xyz(nid, x, y, z):
# Try the most likely setter names across Mecway versions
for name in ("set_node", "set_node_pos", "set_node_coords", "set_node_coord", "set_node_position"):
if hasattr(mw, name):
getattr(mw, name)(nid, Vector(x, y, z))
return
raise Exception("Can't find a node-coordinate setter on 'mw'. "
"Try printing dir(mw) to see available functions.")

for nid in mw.selected_nodes():
p = mw.node(nid) # Vector: current node position
x1, y1, z1 = p.X, p.Y, p.Z

th = math.atan2(y1, x1) # polar angle at the node

x2 = x1 + dr*math.cos(th)
y2 = y1 + dr*math.sin(th)

set_node_xyz(nid, x2, y2, z1)

Comments

  • You can do that with Mesh tools -> Move/copy, Set Coordinate system to Cylindrical (Z axis).

    But if you do need a script, could you please format the script to "Code" in the forum otherwise indenting is lost and other things get changed.

    Is it searching for a function to set node coordinates? That hasn't changed across versions. It's 3 functions:
    mw.set_node_x(node_id, value)
    mw.set_node_y(node_id, value)
    mw.set_node_z(node_id, value)
    
  • Ok, here it is again. I know what I want to achieve, but I know nothing about Python coding. I'm using AI for this.
    And I don't want to edit the mesh. This is meant as a load as "Displacement". Let's say the edge of a hole moves out radially for 0,05 mm.

    # Before running this script, select the circle edge nodes and input radial displacement dr. # It then moves selected nodes for dr in polar coordinate system. import math dr = 1.0 # <-- your input (model units) def set_node_xyz(nid, x, y, z): # Try the most likely setter names across Mecway versions for name in ("set_node", "set_node_pos", "set_node_coords", "set_node_coord", "set_node_position"): if hasattr(mw, name): getattr(mw, name)(nid, Vector(x, y, z)) return raise Exception("Can't find a node-coordinate setter on 'mw'. " "Try printing dir(mw) to see available functions.") for nid in mw.selected_nodes(): p = mw.node(nid) # Vector: current node position x1, y1, z1 = p.X, p.Y, p.Z th = math.atan2(y1, x1) # polar angle at the node x2 = x1 + dr*math.cos(th) y2 = y1 + dr*math.sin(th) set_node_xyz(nid, x2, y2, z1)
  • I see. It looks like the AI is trying to editing the mesh instead of applying a displacement constraint.

    You can do a radial prescribed displacement without a script, using formulas in Displacement like this:


    If you need to adjust the distance in a script, you can create the displacement constraint in advance then set the values in the script using

    mw.set_load_property(load, property, value)

    See Help -> API Reference for details.

    The API is generally not very capable at setting loads and constraints. It can't set formula values, for instance, or create them from scratch or assign nodes to them.
  • Ok. This works. Thanks.
  • Scale works nicely, too. Equal scale factors for each relevant direction (R +/- dR)/R.
  • For mesh editing or displacement input as load?
  • Sorry. Yes, the Scale technique is for mesh editing alone. I didn't catch you wanted the dR to be the input load passed to solver(?) :|
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!