Skip to content

Commit

Permalink
Fix for Trapezoid Duct Adaptive Templates Example (#449)
Browse files Browse the repository at this point in the history
* Simplify point calculation using get_coordinate_from_distance() method. Fixes issues with half ducts.

* update text

* improve comments
  • Loading branch information
JackB-Ansys authored Jan 7, 2025
1 parent 7a3c83b commit 73b7f74
Showing 1 changed file with 28 additions and 30 deletions.
58 changes: 28 additions & 30 deletions examples/adaptive_library/TrapezoidalDuct.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import tempfile

import ansys.motorcad.core as pymotorcad
from ansys.motorcad.core.geometry import Coordinate, rt_to_xy, xy_to_rt
from ansys.motorcad.core.geometry import xy_to_rt

# %%
# Connect to Motor-CAD
Expand All @@ -66,7 +66,7 @@
mc.set_variable("MessageDisplayState", 2)
mc.set_visible(True)
mc.load_template("e10")
mc.set_variable("RotorDuctType", 4) # selected rectangular ducts
mc.set_variable("RotorDuctType", 4) # select rectangular ducts
mc.set_array_variable("RotorCircularDuctLayer_ChannelWidth", 0, 4) # set duct width

# Open relevant file
Expand Down Expand Up @@ -159,7 +159,7 @@ def check_line_origin_distance(i, duct_region):
#
# * Find the top line that makes up the duct.
#
# * Modify the start and end points of the line.
# * Modify the start and end points of the line using the get_coordinate_from_distance() method.
#
# * Set the region in Motor-CAD.
#
Expand All @@ -169,63 +169,61 @@ def check_line_origin_distance(i, duct_region):
for child_name in rt_region.child_names:
if "RotorDuctFluidRegion" in child_name:
duct_region = mc.get_region(child_name)
if round(duct_region.area / duct_area, 2) == 1: # check if full duct is drawn
if round(duct_region.area / duct_area, 2) == 1: # check if a full duct is drawn
for i, entity in enumerate(duct_region.entities):
if round(entity.length / Trap_W, 2) == 1: # check if the line is width
# additional check in case width = height
if (
round(entity.length / Trap_W, 2) == 1
): # check if the line length is the same as the duct width
# additional check in case the duct width = height
r_start_point, angle_start_point = xy_to_rt(entity.start.x, entity.start.y)
r_end_point, angle_end_point = xy_to_rt(entity.end.x, entity.end.y)
if abs(angle_end_point - angle_start_point) > 0.05: # 0.05 degree is tolerance
# check if the line located at top or bottom
# check if the line is located at top or bottom
Line_origin = check_line_origin_distance(i, duct_region)
if not Line_origin:
del_trap_angle = (
(angle_end_point - angle_start_point) * (1 - Trap_ratio) / 2
distance = entity.length * (1 - Trap_ratio)
new_start_point = entity.get_coordinate_from_distance(
entity.start, fraction=(1 - Trap_ratio) / 2
)
new_angle_start_point = angle_start_point + del_trap_angle
new_angle_end_point = angle_end_point - del_trap_angle
new_start_x, new_start_y = rt_to_xy(
r_start_point, new_angle_start_point
new_end_point = entity.get_coordinate_from_distance(
entity.end, fraction=(1 - Trap_ratio) / 2
)
new_end_x, new_end_y = rt_to_xy(r_end_point, new_angle_end_point)
new_start_point = Coordinate(new_start_x, new_start_y)
new_end_point = Coordinate(new_end_x, new_end_y)
duct_region.edit_point(entity.start, new_start_point)
duct_region.edit_point(entity.end, new_end_point)
mc.set_region(duct_region)

elif round(duct_region.area / duct_area, 2) == 0.5: # half duct
elif (
round(duct_region.area / duct_area, 2) == 0.5
): # account for the case where we have half ducts
Symm_angle = 360 / duct_region.duplications # angle of symmetry

for i, entity in enumerate(duct_region.entities):
if round(entity.length / Trap_W, 2) == 0.5: # check if the line is width
# additional check in case width = height
if (
round(entity.length / Trap_W, 2) == 0.5
): # check if the line length is the same as the duct width
# additional check in case the duct width = height
r_start_point, angle_start_point = xy_to_rt(entity.start.x, entity.start.y)
r_end_point, angle_end_point = xy_to_rt(entity.end.x, entity.end.y)
if abs(angle_end_point - angle_start_point) > 0.05: # 0.05 degree is tolerance
Line_origin = check_line_origin_distance(i, duct_region)
if not Line_origin:
del_trap_angle = (angle_end_point - angle_start_point) * (
1 - Trap_ratio
)
distance = entity.length * (1 - Trap_ratio)
if (
angle_start_point - 0 < 1e-10
or angle_start_point == 0
or round(angle_start_point / Symm_angle, 2) == 1
): # on symmetry plane
new_angle_end_point = angle_end_point - del_trap_angle
new_end_x, new_end_y = rt_to_xy(r_end_point, new_angle_end_point)
new_end_point = Coordinate(new_end_x, new_end_y)
new_end_point = entity.get_coordinate_from_distance(
entity.end, fraction=(1 - Trap_ratio) / 2
)
duct_region.edit_point(entity.end, new_end_point)
elif (
angle_end_point - 0 < 1e-10
or round(angle_end_point / Symm_angle, 2) == 1
): # symmetry plan
new_angle_start_point = angle_start_point + del_trap_angle
new_start_x, new_start_y = rt_to_xy(
r_start_point, new_angle_start_point
): # symmetry plane
new_start_point = entity.get_coordinate_from_distance(
entity.start, fraction=(1 - Trap_ratio) / 2
)
new_start_point = Coordinate(new_start_x, new_start_y)
duct_region.edit_point(entity.start, new_start_point)

mc.set_region(duct_region)
Expand Down

0 comments on commit 73b7f74

Please sign in to comment.