Skip to content
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

Element translation #39

Merged
merged 4 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Removed

* Removed `ElementType` class.
* Removed `Element.from_id` method.
* Removed `Element.type` property. Use `Element.is_*` properties instead.
* Removed `Dimension.from_element` method. Use `Dimension(element_id)` instead.
* Removed `Dimension.from_id` method. Use `Dimension(element_id)` instead.

## [0.8.0] 2024-11-15

Expand Down
1 change: 0 additions & 1 deletion docs/api/compas_cadwork.datamodel.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Classes
:nosignatures:

ElementGroupingType
ElementType
ElementGroup
Element

Expand Down
2 changes: 0 additions & 2 deletions src/compas_cadwork/datamodel/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from .element import Element
from .element import ElementType
from .element import ElementGroup
from .element import ElementGroupingType
from .element import ATTR_INSTRUCTION_ID
Expand All @@ -9,7 +8,6 @@

__all__ = [
"Element",
"ElementType",
"ElementGroup",
"ElementGroupingType",
"ATTR_INSTRUCTION_ID",
Expand Down
39 changes: 1 addition & 38 deletions src/compas_cadwork/datamodel/dimension.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from compas_cadwork.conversions import vector_to_compas

from .element import Element
from .element import ElementType

TOL = Tolerance(unit="MM", absolute=1e-3, relative=1e-3)

Expand Down Expand Up @@ -53,7 +52,7 @@ class Dimension(Element):
"""Represents a cadwork dimension"""

def __init__(self, id):
super().__init__(id, ElementType.DIMENSION)
super().__init__(id)
self._frame = None
# not lazy-instantiating this so that it can be used to compare the modified instances of the same dimension
# otherwise, the anchors values that are compared depend on the time `anchors` was first accessed
Expand Down Expand Up @@ -106,39 +105,3 @@ def _init_anchors(self):
direction = dc.get_segment_direction(self.id, index)
anchors.append(AnchorPoint(point_to_compas(point), distance, vector_to_compas(direction)))
return tuple(anchors)

@classmethod
def from_id(cls, element_id: int) -> Dimension:
"""Creates a dimension object from an element id.

This is an override of :func:`Element.from_id`.

Parameters
----------
element_id : int
The id of the element to create the dimension from.

Returns
-------
:class:`Dimension`
The dimension object created from the element id.

"""
return cls(id=element_id)

@classmethod
def from_element(cls, element: Element) -> Dimension:
"""Creates a dimension object from an element.

Parameters
----------
element : :class:`Element`
The element to create the dimension from.

Returns
-------
:class:`Dimension`
The dimension object created from the element.

"""
return cls(id=element.id)
74 changes: 4 additions & 70 deletions src/compas_cadwork/datamodel/element.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from __future__ import annotations

from dataclasses import dataclass
from enum import Enum
from enum import IntEnum
from enum import auto
from typing import Generator
from typing import List
from typing import Optional
Expand All @@ -13,7 +11,6 @@
import cadwork # noqa: F401
import element_controller as ec
import geometry_controller as gc
import utility_controller as uc
from compas.geometry import Frame
from compas.geometry import Line
from compas.geometry import Point
Expand Down Expand Up @@ -55,45 +52,6 @@ def to_cadwork(self):
return cadwork.element_grouping_type(self.value)


class ElementType(str, Enum):
"""CADWork Element type"""

BEAM = auto() # Stab
PLATE = auto() # Platte
SURFACE = auto()
SHAFT = auto()
LINE = auto()
INSTALLATION_ROUND = auto()
INSTALLATION_STRAIGHT = auto()
DIMENSION = auto()
OTHER = auto()


ELEMENT_TYPE_MAP = {
"de": {
"Stab": ElementType.BEAM,
"Platte": ElementType.PLATE,
"Achse": ElementType.SHAFT,
"Linie": ElementType.LINE,
"Installation rechteckig": ElementType.INSTALLATION_STRAIGHT,
"Fläche": ElementType.SURFACE,
"Installation rund": ElementType.INSTALLATION_ROUND,
"Measurement": ElementType.DIMENSION,
},
"en": {
"Beam": ElementType.BEAM,
"Plate": ElementType.PLATE,
"Bemassung": ElementType.DIMENSION,
},
}

# TODO: get rid of this whole thing
try:
LOCAL_TYPE_MAP = ELEMENT_TYPE_MAP[uc.get_language()]
except AttributeError:
pass


@dataclass
class ElementGroup:
"""Represents a cadwork Element Group
Expand Down Expand Up @@ -152,9 +110,6 @@ class Element:
----------
id : int
The ID of the Element
type : ElementType
The type of the Element


Attributes
----------
Expand All @@ -180,6 +135,8 @@ class Element:
The midpoint of the Element's centerline.
ifc_guid : str
The IFC GUID of the Element. See also: ifc_base64_guid.
is_beam : bool
Whether the Element is a beam
is_wall : bool
Whether the Element is a framed wall i.e. container for all other elements in the building group.
is_drilling : bool
Expand All @@ -192,7 +149,6 @@ class Element:
"""

id: int
type: ElementType

@property
def name(self) -> str:
Expand Down Expand Up @@ -280,28 +236,6 @@ def is_beam(self) -> bool:
type_ = ac.get_element_type(self.id)
return type_.is_rectangular_beam()

@classmethod
def from_id(cls, element_id: int) -> Element:
"""Returns an Element object for the cadwork Element with the given ID

Parameters
----------
element_id : int
The ID of the Element

Returns
-------
Element
The Element object for the given ID

"""
type_description = ec.get_element_type_description(element_id)
try:
type_ = LOCAL_TYPE_MAP[type_description]
except KeyError:
type_ = ElementType.OTHER
return Element(element_id, type_)

@classmethod
def from_selection(cls) -> Generator[Element]:
"""Returns a generator containing Element objects for all currently activated Elements
Expand All @@ -312,7 +246,7 @@ def from_selection(cls) -> Generator[Element]:
A generator containing Element objects for all currently activated Elements

"""
return (Element.from_id(e_id) for e_id in ec.get_active_identifiable_element_ids())
return (Element(e_id) for e_id in ec.get_active_identifiable_element_ids())

def set_attribute(self, name, value):
"""Sets an attribute on the Element
Expand Down Expand Up @@ -371,7 +305,7 @@ def get_instruction_id(self) -> Optional[str]:

def get_elements_in_contact(self) -> List[Element]:
"""Returns a list of elements in contact with the current element"""
return [Element.from_id(e_id) for e_id in ec.get_elements_in_contact(self.id)]
return [Element(e_id) for e_id in ec.get_elements_in_contact(self.id)]

def remove(self):
"""Removes the Element from the cadwork file"""
Expand Down
2 changes: 1 addition & 1 deletion src/compas_cadwork/scene/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def add_element(self, element_id) -> Element:

"""
self.DRAWN_ELEMENTS.append(element_id)
return Element.from_id(element_id)
return Element(element_id)

@classmethod
def refresh(cls):
Expand Down
8 changes: 4 additions & 4 deletions src/compas_cadwork/utilities/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def get_active_elements() -> list[Element]:
List of currently selected elements.

"""
return [Element.from_id(e_id) for e_id in ec.get_active_identifiable_element_ids()]
return [Element(e_id) for e_id in ec.get_active_identifiable_element_ids()]


def get_language() -> str:
Expand Down Expand Up @@ -205,7 +205,7 @@ def get_element_groups(is_wall_frame: bool = True) -> Dict[str, ElementGroup]:

if group_name not in groups_elements:
groups_elements[group_name] = ElementGroup(group_name)
groups_elements[group_name].add_element(Element.from_id(element_id))
groups_elements[group_name].add_element(Element(element_id))

if is_wall_frame:
_remove_wallless_groups(groups_elements)
Expand Down Expand Up @@ -372,7 +372,7 @@ def get_all_elements(include_instructions: bool = False) -> Generator[Element, N

"""
for element_id in ec.get_all_identifiable_element_ids():
element = Element.from_id(element_id)
element = Element(element_id)
if include_instructions or not element.is_instruction:
yield element

Expand All @@ -394,7 +394,7 @@ def get_all_elements_with_attrib(attrib_number, attrib_value=None):
"""
for element_id in ec.get_all_identifiable_element_ids():
if ac.get_user_attribute(element_id, attrib_number) == attrib_value:
yield Element.from_id(element_id)
yield Element(element_id)


def remove_elements(elements: List[Union[Element, int]]) -> None:
Expand Down
2 changes: 1 addition & 1 deletion src/compas_cadwork/utilities/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def check_for_changed_elements(self):
new_ids = current_ids - self._known_element_ids
removed_ids = self._known_element_ids - current_ids
self._known_element_ids = current_ids
return [Element.from_id(id) for id in new_ids], [Element.from_id(id) for id in removed_ids]
return [Element(id) for id in new_ids], [Element(id) for id in removed_ids]

def reset(self):
"""Reset the known element ids"""
Expand Down
Loading