UDP Python Example

This Python script example demonstrates how to use the UDPBLNDElements structure and the UDP chamfer and fillet functions.

import sys

primitive_info = UDPPrimitiveTypeInfo(

name="Fillet_Chamfer",

purpose="Fillet Chamfer Example",

company="Ansys",

date="09/11/2020",

version="1.0")

primitive_param_definitions = [

UDPPrimitiveParameterDefinition2(

"x_size",

"",

UnitType.LengthUnit,

ParamPropType.Value,

ParamPropFlag.MustBeReal,

UDPParam(ParamDataType.Double, 10)),

UDPPrimitiveParameterDefinition2(

"y_size",

"",

UnitType.LengthUnit,

ParamPropType.Value,

ParamPropFlag.MustBeReal,

UDPParam(ParamDataType.Double, 5)),

UDPPrimitiveParameterDefinition2(

"z_size",

"",

UnitType.LengthUnit,

ParamPropType.Value,

ParamPropFlag.MustBeReal,

UDPParam(ParamDataType.Double, 2))

]

length_units = "mm"

 

##############################################################

# Class Implementation

##############################################################

class UDPExtension(IUDPExtension):

def CreatePrimitive2(self, func_lib, param_values):

"""

Inbuilt function that is called to generate a UDP after successful validation

:param func_lib: drawing inbuilt class, see in Help: UDMFunctionLibrary

:param param_values: list of udp parameter values (user input) generated by UDP Core

:return: None

"""

param_dict = self.get_param_dict(param_values)

 

start_point = UDPPosition(0, 0, 0)

box = func_lib.CreateBox(start_point, [

param_dict["x_size"],

param_dict["y_size"],

param_dict["z_size"]

])

 

# points on the middle of 4 vertical edges

points = [

[0, 0, param_dict["z_size"]/2],

[param_dict["x_size"], 0, param_dict["z_size"]/2],

[param_dict["x_size"], param_dict["y_size"], param_dict["z_size"]/2],s

[0, param_dict["y_size"], param_dict["z_size"]/2]

]

 

edges = [func_lib.GetEdgeIDFromPosition(UDPPosition(point[0], point[1], point[2])) for point in points]

 

fillet_rad = 0.1 * param_dict["x_size"] # 10% of X size

fillet_opt = UDPBLNDFilletOptions(True, BLNDFilletRadiusLaw.BLNDConstantRadius, fillet_rad, 0.0, True, BLNDFilletType.BLNDRound, 0.0, 0.0)

 

chamfer_length = 0.1 * param_dict["x_size"] # 10% of X size

chamfer_opt = UDPBLNDChamferOptions(False, BLNDChamferRangeLaw.BLNDConstantRange, chamfer_length, 0.0)

 

# select your geometry to which to apply operations

blend_element = UDPBLNDElements(box)

 

# specify attribute ListOfEdges to which edges to apply fillet operation

blend_element.ListOfEdges = edges[0:2]

func_lib.Fillet(blend_element, fillet_opt)

 

# redeclare attribute ListOfEdges to which edges to apply chamfer operation

blend_element = UDPBLNDElements(box)

blend_element.ListOfEdges = edges[2:4]

func_lib.Chamfer(blend_element, chamfer_opt)

 

# Provide to the user Info message indicating success

func_lib.AddMessage(MessageSeverity.InfoMessage, "Completed!")

 

def GetPrimitiveTypeInfo(self):

return primitive_info

 

def GetLengthParameterUnits(self):

return length_units

 

def GetPrimitiveParametersDefinition2(self):

return primitive_param_definitions

 

def AreParameterValuesValid2(self, error, udp_params):

return True

 

# Custom Functions

def get_param_value_by_name(self, param_values, param_name):

"""

Function to get a value of a single parameter accessing it by name

:param param_values: list of udp parameter values (user input) generated by UDP Core

:param param_name: name of the parameter as specified in definition list

:return: Value of the parameter or None if parameter does not exist

"""

param_dict = self.get_param_dict(param_values)

value = param_dict.get(param_name, None)

return value

 

def get_param_dict(self, param_values):

"""

Function to return a dictionary of UDP parameter name and value (key: value) pairs

:param param_values: list of udp parameter values (user input) generated by UDP Core

:return: dict of parameter name and values

"""

udm_param_def = self.GetPrimitiveParametersDefinition2()

param_dict = {}

for i, param in enumerate(udm_param_def):

param_value = param_values[i].Data

if str(param.PropType) != "Menu":

param_dict[param.Name] = param_value

else:

param_dict[param.Name] = param_value.replace('"', '').split(",")[0]

return param_dict