Skip to content

Add geometry parameters from json into Python region object #490

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions src/ansys/motorcad/core/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ def __init__(self, region_type=RegionType.adaptive, motorcad_instance=None):
self._motorcad_instance = motorcad_instance
self._region_type = region_type
self._mesh_length = 0
self._extrusion_blocks = ExtrusionBlockList()
self._temperature = 0
self._weight_reduction_factor = 1

self._linked_region = None
self._singular = False
self._lamination_type = ""
Expand All @@ -155,6 +159,7 @@ def __eq__(self, other):
# Region coordinate is an output, cannot guarantee will be same for identical regions
and self._duplications == other._duplications
and self._entities == other._entities
and self._singular == other._singular
)

@classmethod
Expand Down Expand Up @@ -295,6 +300,15 @@ def _from_json(cls, json, motorcad_instance=None):
if "lamination_type" in json:
new_region._lamination_type = json["lamination_type"]

if "extrusion_blocks" in json:
new_region._extrusion_blocks._from_json(json["extrusion_blocks"])

if "region_temperature" in json:
new_region._temperature = json["region_temperature"]

if "weight_reduction_factor" in json:
new_region._weight_reduction_factor = json["weight_reduction_factor"]

return new_region

# method to convert python object to send to Motor-CAD
Expand Down Expand Up @@ -327,6 +341,8 @@ def _to_json(self):
"on_boundary": False if self._linked_region is None else True,
"singular": self._singular,
"lamination_type": lamination_type,
"extrusion_blocks": self._extrusion_blocks._to_json,
"region_temperature": self._temperature,
}

return region_dict
Expand Down Expand Up @@ -382,6 +398,16 @@ def singular(self):
def singular(self, singular):
self._singular = singular

@property
def extrusion_blocks(self):
"""Get extrusion blocks list.

Returns
-------
list of ExtrusionBlock
"""
return self._extrusion_blocks

@property
def child_names(self):
"""Get child names list.
Expand Down Expand Up @@ -484,6 +510,27 @@ def material(self):
def material(self, material):
self._material = material

@property
def weight_reduction_factor(self):
"""Get weight reduction factor.

Returns
-------
float : weight reduction factor
"""
return self._weight_reduction_factor

@weight_reduction_factor.setter
def weight_reduction_factor(self, factor):
"""Set weight reduction factor.

Parameters
----------
factor : float
weight reduction factor
"""
self._weight_reduction_factor = factor

@property
def colour(self):
"""Get or set region colour."""
Expand Down Expand Up @@ -520,6 +567,16 @@ def mesh_length(self):
def mesh_length(self, mesh_length):
self._mesh_length = mesh_length

@property
def temperature(self):
"""Get region temperature.

Returns
-------
float : region temperature
"""
return self._temperature

@property
def area(self):
"""Get the region area."""
Expand All @@ -535,6 +592,11 @@ def region_coordinate(self):
"""Get the reference coordinate within the region."""
return self._region_coordinate

@property
def duplication_angle(self):
"""Get linked Motor-CAD instance."""
return 360 / self.duplications

def subtract(self, region):
"""Subtract region from self, returning any additional regions.

Expand Down Expand Up @@ -2160,6 +2222,90 @@ def _entities_same_with_direction(entities_1, entities_2):
return _entities_same_with_direction(self, entities_to_compare)


class ExtrusionBlock:
"""Generic class for storing 3D extrusion data."""

def __init__(self):
"""Initialise extrusion block."""
self.start_pos = 0
self.end_pos = 0
self.angle_shift = 0

def __eq__(self, other):
"""Compare equality of 2 ExtrusionBlock objects."""
return (
(self.start_pos == other.start_pos)
& (self.end_pos == other.end_pos)
& (self.start_pos == other.end_pos)
)

def from_json(self, json):
"""Convert the class from a JSON object.

Parameters
----------
json: dict
Dictionary representing the extrusion block.
"""
self.start_pos = json["extrusion_block_start"]
self.end_pos = json["extrusion_block_end"]
self.angle_shift = json["extrusion_block_angle_step"]

def to_json(self):
"""Convert from a Python class to a JSON object.

Returns
-------
dict
Dictionary of the extrusion block represented as JSON.
"""
block_dict = {
"extrusion_block_start": self.start_pos,
"extrusion_block_end": self.end_pos,
"extrusion_block_angle_step": self.start_pos,
}

return block_dict

@property
def extrusion_length(self):
"""Return extrusion length between start and end positions.

Returns
-------
float
Block extrusion length.
"""
return abs(self.end_pos - self.start_pos)


class ExtrusionBlockList(list):
"""Generic class for list of Entities."""

def _to_json(self):
"""Convert from a Python class to a JSON object.

Returns
-------
list
List of the extrusion blocks represented as JSON.
"""
return [block.to_json for block in self]

def _from_json(self, json_list):
"""Convert the class from a JSON object.

Parameters
----------
json: list
List of extrusion blocks in json.
"""
for json_object in json_list:
block = ExtrusionBlock()
block.from_json(json_object)
self.append(block)


def _convert_entities_to_json(entities):
"""Get entities list as a json object.

Expand Down
24 changes: 24 additions & 0 deletions tests/test_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,8 @@ def test_region_from_json():
"region type": RegionType.stator_copper,
"mesh_length": 0.035,
"singular": False,
"region_temperature": 25,
"weight_reduction_factor": 0.5,
}

test_region = geometry.Region(region_type=RegionType.stator_copper)
Expand All @@ -380,6 +382,8 @@ def test_region_from_json():
test_region._child_names = ["Duct", "Duct_1"]
test_region.mesh_length = (0.035,)
test_region.singular = (False,)
test_region._temperature = 25
test_region.weight_reduction_factor = 0.5

region = geometry.Region._from_json(raw_region)

Expand All @@ -403,6 +407,8 @@ def test_region_to_json():
"mesh_length": 0.035,
"singular": True,
"on_boundary": False,
"region_temperature": 35,
"weight_reduction_factor" : 0.75,
}

test_region = geometry.Region(region_type=RegionType.stator_copper)
Expand All @@ -418,6 +424,8 @@ def test_region_to_json():
test_region.parent_name = "Insulation"
test_region.mesh_length = 0.035
test_region.singular = True
test_region._temperature = 35
test_region._weight_reduction_factor = 0.75

assert test_region._to_json() == raw_region

Expand Down Expand Up @@ -2696,3 +2704,19 @@ def test_region_creation_warnings(mc):
_ = Region()
with pytest.warns():
_ = Region(mc)


def test_get_temperature():
region_temp = 100
test_region = Region()
test_region._temperature = region_temp

assert test_region.temperature == region_temp


def test_get_weight_reduction_factor():
factor = 0.789
test_region = Region()
test_region.weight_reduction_factor = factor

assert test_region.weight_reduction_factor == factor
Loading