How do I create nCode Weld definition file from Mechanical?

Member, Moderator, Employee Posts: 479
100 Answers 250 Likes 100 Comments Second Anniversary
✭✭✭✭
edited August 2023 in Structures

Best Answer

  • Member, Moderator, Employee Posts: 479
    100 Answers 250 Likes 100 Comments Second Anniversary
    ✭✭✭✭
    edited December 2022 Answer ✓

    The following code creates 3 points along the weldline, normals and toe vectors. The input has to be a surface named selection named “face” and an edge called “weldline”. It creates a local coordinate system at the points, with the X-Axis pointing in the normal / toe direction. If the vector directions are not as you need them, you just need to negate the vectors.

    1. vec_global = [1, 0, 0]  # Global X axis
    2.  
    3. # Dot product of two vectors
    4. dot_product = lambda vec_1, vec_2: sum(map(lambda x, y: x * y, vec_1, vec_2))
    5.  
    6.  
    7. def cross_prod(a, b):
    8.     result = [a[1]*b[2] - a[2]*b[1],
    9.               a[2]*b[0] - a[0]*b[2],
    10.               a[0]*b[1] - a[1]*b[0]]
    11.     return result
    12.  
    13.  
    14. # Vector magnitude
    15. def magnitude(vec):
    16.     res = (vec[0] ** 2 + vec[1] ** 2 + vec[2] ** 2) ** 0.5
    17.     if res == 0.0:
    18.         return 1.0
    19.     return res
    20.  
    21.  
    22. def create_cs(vec_normal_in, point):
    23.     # Euler axis - angle
    24.     rot_rad = acos(dot_product(vec_global, vec_normal_in) / magnitude(vec_global) * magnitude(vec_normal_in))
    25.     # Euler axis - Axis of rotation
    26.     axis = cross_prod(vec_global, vec_normal_in)
    27.     # Unit vector normal to Surface and Global X axis vector
    28.     axis_unit_vec = [axis[0] / magnitude(axis), axis[1] / magnitude(axis), axis[2] / magnitude(axis)]
    29.         
    30.     def get_euler_angles_from_axis(euler_axis, rot_angle):
    31.         # Euler axis–angle - quaternions
    32.         q1 = euler_axis[0] * sin(rot_angle / 2)
    33.         q2 = euler_axis[1] * sin(rot_angle / 2)
    34.         q3 = euler_axis[2] * sin(rot_angle / 2)
    35.         q4 = cos(rot_angle / 2)
    36.     
    37.         # Quaternion - Euler angles (z-y'-x'' intrinsic)
    38.         roll = atan2((2 * q4 * q1 + 2 * q2 * q3), (1 - 2 * (q1 ** 2 + q2 ** 2)))
    39.         pitch = asin(2 * (q4 * q2 - q1 * q3))
    40.         yaw = atan2((2 * q4 * q3 + 2 * q1 * q2), (1 - 2 * (q2 ** 2 + q3 ** 2)))
    41.     
    42.         # Euler angles (degrees)
    43.         roll_deg = roll * 180 / pi
    44.         pitch_deg = pitch * 180 / pi
    45.         yaw_deg = yaw * 180 / pi
    46.     
    47.         return yaw_deg, pitch_deg, roll_deg
    48.         
    49.     yaw_deg, pitch_deg, roll_deg = get_euler_angles_from_axis(axis_unit_vec, rot_rad)
    50.     
    51.     # New coordinate system creation and orientation change to match surface normal vector
    52.     new_cs = ExtAPI.DataModel.Project.Model.CoordinateSystems.AddCoordinateSystem()
    53.     new_cs.OriginX = Quantity(point[0], "m")
    54.     new_cs.OriginY = Quantity(point[1], "m")
    55.     new_cs.OriginZ = Quantity(point[2], "m")
    56.     new_cs.RotateZ()
    57.     new_cs.RotateY()
    58.     new_cs.RotateX()
    59.     new_cs.SetTransformationValue(1, yaw_deg)
    60.     new_cs.SetTransformationValue(2, pitch_deg)
    61.     new_cs.SetTransformationValue(3, roll_deg)
    62.  
    63. # Create face and node named selections
    64. geom_id = ExtAPI.DataModel.GetObjectsByName("face")[0].Ids[0]
    65. weldline_id = ExtAPI.DataModel.GetObjectsByName("weldline")[0].Ids[0]
    66. weldline = ExtAPI.DataModel.AnalysisList[0].GeoData.GeoEntityById(weldline_id)
    67.  
    68. mesh_data = DataModel.MeshDataByName(DataModel.MeshDataNames[0])
    69. my_face = DataModel.GeoData.GeoEntityById(geom_id)
    70. base_point = weldline.StartVertex.X, weldline.StartVertex.Y, weldline.StartVertex.Z
    71. end_point = weldline.EndVertex.X, weldline.EndVertex.Y, weldline.EndVertex.Z
    72.  
    73. length = weldline.Length
    74. line_vector = ((end_point[0] - base_point[0]) / (length),
    75.                (end_point[1] - base_point[1]) / (length),
    76.                (end_point[2] - base_point[2]) / (length))
    77.  
    78. # 3D line equation
    79. fx = lambda t: base_point[0] + line_vector[0] * t
    80. fy = lambda t: base_point[1] + line_vector[1] * t
    81. fz = lambda t: base_point[2] + line_vector[2] * t
    82.  
    83. # Get equidistant points on the weldline
    84. n_points = 3
    85. delta = length / (n_points - 1)
    86. points = [
    87.     [fx(t * delta), fy(t * delta), fz(t * delta)] for t in range(n_points)
    88. ]
    89.  
    90. # Get normal vectors at all points
    91. vec_normal = lambda (u, v): my_face.NormalAtParam(u, v)  # Normal Vector to the face at given node
    92. normals = [vec_normal(my_face.ParamAtPoint(tuple(point))) for point in points]
    93.  
    94. # Get Toe vector at the Vertex in plane of the face
    95. vec_tangent = lambda u: weldline.TangentAtParam(u)
    96. tangents =  [vec_tangent(weldline.ParamAtPoint(tuple(point))) for point in points]
    97. toe_vec = [cross_prod(tangents[index], normal) for index, normal in enumerate(normals)]
    98.     
    99. for index, normal in enumerate(normals):
    100.     create_cs(normal, points[index])
    101.     create_cs(toe_vec[index], points[index])

Welcome!

It looks like you're new here. Sign in or register to get started.