symcad.core.Geometry

  1#!/usr/bin/env python3
  2# Copyright (C) 2022, Will Hedgecock
  3#
  4# This program is free software: you can redistribute it and/or modify
  5# it under the terms of the GNU General Public License as published by
  6# the Free Software Foundation, either version 3 of the License, or
  7# (at your option) any later version.
  8#
  9# This program is distributed in the hope that it will be useful,
 10# but WITHOUT ANY WARRANTY; without even the implied warranty of
 11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12# GNU General Public License for more details.
 13#
 14# You should have received a copy of the GNU General Public License
 15# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 16
 17from __future__ import annotations
 18from typing import Dict
 19from copy import deepcopy
 20from sympy import Symbol
 21
 22class Geometry(object):
 23   """Represents the shape-specific parametric geometry of a `SymPart`."""
 24
 25   # Public attributes ----------------------------------------------------------------------------
 26
 27   name: str
 28   """Unique, identifying name of the `Geometry` instance."""
 29
 30
 31   # Constructor ----------------------------------------------------------------------------------
 32
 33   def __init__(self, identifier: str) -> None:
 34      """Initializes a `Geometry` instance, where `identifier` uniquely identifies the instance."""
 35      super().__init__()
 36      self.name = identifier
 37
 38
 39   # Built-in method implementations --------------------------------------------------------------
 40
 41   def __repr__(self) -> str:
 42      output = [key+' = '+str(val)+', ' for key, val in self.__dict__.items() if key != 'name']
 43      return ''.join(output).strip(' ,')
 44
 45   def __eq__(self, other: Geometry) -> bool:
 46      for key, val in self.__dict__.items():
 47         if key != 'name' and (key not in other.__dict__ or val != getattr(other, key)):
 48            return False
 49      return True
 50
 51   def __copy__(self) -> Geometry:
 52      copy = self.__class__.__new__(self.__class__)
 53      copy.__dict__.update(self.__dict__)
 54      return copy
 55
 56   def __deepcopy__(self, memo) -> Geometry:
 57      copy = self.__class__.__new__(self.__class__)
 58      memo[id(self)] = copy
 59      for key, val in self.__dict__.items():
 60         setattr(copy, key, deepcopy(val, memo))
 61      return copy
 62
 63   def __imul__(self, value: float) -> Geometry:
 64      for key, val in self.__dict__.items():
 65         if key != 'name':
 66            setattr(self, key, val * value)
 67      return self
 68
 69   def __itruediv__(self, value: float) -> Geometry:
 70      for key, val in self.__dict__.items():
 71         if key != 'name':
 72            setattr(self, key, val / value)
 73      return self
 74
 75
 76   # Public methods -------------------------------------------------------------------------------
 77
 78   def clone(self) -> Geometry:
 79      """Returns an exact clone of this `Geometry` instance."""
 80      return deepcopy(self)
 81
 82
 83   def copy_from(self, other: Geometry) -> Geometry:
 84      """Copies the geometric parameters from another `Geometry` instance into this one.
 85
 86      The name of this instance will not be changed or overwritten.
 87
 88      Parameters
 89      ----------
 90      other : `Geometry`
 91         A Geometry object whose attributes should be copied into this instance.
 92
 93      Returns
 94      -------
 95      self : `Geometry`
 96         The Geometry instance being manipulated.
 97      """
 98      for key, val in other.__dict__.items():
 99         if key != 'name':
100            setattr(self, key, val)
101      return self
102
103
104   def set(self, **kwargs) -> Geometry:
105      """Sets the underlying geometric parameters to the values specified.
106
107      The keys in the `**kwargs` dictionary will be different for each `SymPart`. If a key is
108      missing or its value is `None`, the corresponding geometric property will be treated
109      as a symbol.
110
111      Parameters
112      ----------
113      **kwargs : `Dict`
114         A dictionary containing values or symbols for the geometric properties present in this
115         Geometry instance.
116
117      Returns
118      -------
119      self : `Geometry`
120         The Geometry instance being manipulated.
121      """
122      for key in self.__dict__:
123         if key != 'name':
124            setattr(self, key, kwargs[key] if key in kwargs and kwargs[key] is not None else
125                    Symbol(self.name + '_' + key))
126      return self
127
128
129   def clear(self) -> Geometry:
130      """Clears all geometric properties to `0.0`.
131
132      Returns
133      -------
134      self : `Geometry`
135         The Geometry instance being manipulated.
136      """
137      for key in self.__dict__:
138         if key != 'name':
139            setattr(self, key, 0.0)
140      return self
141
142
143   def as_dict(self) -> Dict[str, float]:
144      """Returns the current geometric properties as a dictionary."""
145      return { key: val for key, val in self.__dict__.items() if key != 'name' }
class Geometry:
 23class Geometry(object):
 24   """Represents the shape-specific parametric geometry of a `SymPart`."""
 25
 26   # Public attributes ----------------------------------------------------------------------------
 27
 28   name: str
 29   """Unique, identifying name of the `Geometry` instance."""
 30
 31
 32   # Constructor ----------------------------------------------------------------------------------
 33
 34   def __init__(self, identifier: str) -> None:
 35      """Initializes a `Geometry` instance, where `identifier` uniquely identifies the instance."""
 36      super().__init__()
 37      self.name = identifier
 38
 39
 40   # Built-in method implementations --------------------------------------------------------------
 41
 42   def __repr__(self) -> str:
 43      output = [key+' = '+str(val)+', ' for key, val in self.__dict__.items() if key != 'name']
 44      return ''.join(output).strip(' ,')
 45
 46   def __eq__(self, other: Geometry) -> bool:
 47      for key, val in self.__dict__.items():
 48         if key != 'name' and (key not in other.__dict__ or val != getattr(other, key)):
 49            return False
 50      return True
 51
 52   def __copy__(self) -> Geometry:
 53      copy = self.__class__.__new__(self.__class__)
 54      copy.__dict__.update(self.__dict__)
 55      return copy
 56
 57   def __deepcopy__(self, memo) -> Geometry:
 58      copy = self.__class__.__new__(self.__class__)
 59      memo[id(self)] = copy
 60      for key, val in self.__dict__.items():
 61         setattr(copy, key, deepcopy(val, memo))
 62      return copy
 63
 64   def __imul__(self, value: float) -> Geometry:
 65      for key, val in self.__dict__.items():
 66         if key != 'name':
 67            setattr(self, key, val * value)
 68      return self
 69
 70   def __itruediv__(self, value: float) -> Geometry:
 71      for key, val in self.__dict__.items():
 72         if key != 'name':
 73            setattr(self, key, val / value)
 74      return self
 75
 76
 77   # Public methods -------------------------------------------------------------------------------
 78
 79   def clone(self) -> Geometry:
 80      """Returns an exact clone of this `Geometry` instance."""
 81      return deepcopy(self)
 82
 83
 84   def copy_from(self, other: Geometry) -> Geometry:
 85      """Copies the geometric parameters from another `Geometry` instance into this one.
 86
 87      The name of this instance will not be changed or overwritten.
 88
 89      Parameters
 90      ----------
 91      other : `Geometry`
 92         A Geometry object whose attributes should be copied into this instance.
 93
 94      Returns
 95      -------
 96      self : `Geometry`
 97         The Geometry instance being manipulated.
 98      """
 99      for key, val in other.__dict__.items():
100         if key != 'name':
101            setattr(self, key, val)
102      return self
103
104
105   def set(self, **kwargs) -> Geometry:
106      """Sets the underlying geometric parameters to the values specified.
107
108      The keys in the `**kwargs` dictionary will be different for each `SymPart`. If a key is
109      missing or its value is `None`, the corresponding geometric property will be treated
110      as a symbol.
111
112      Parameters
113      ----------
114      **kwargs : `Dict`
115         A dictionary containing values or symbols for the geometric properties present in this
116         Geometry instance.
117
118      Returns
119      -------
120      self : `Geometry`
121         The Geometry instance being manipulated.
122      """
123      for key in self.__dict__:
124         if key != 'name':
125            setattr(self, key, kwargs[key] if key in kwargs and kwargs[key] is not None else
126                    Symbol(self.name + '_' + key))
127      return self
128
129
130   def clear(self) -> Geometry:
131      """Clears all geometric properties to `0.0`.
132
133      Returns
134      -------
135      self : `Geometry`
136         The Geometry instance being manipulated.
137      """
138      for key in self.__dict__:
139         if key != 'name':
140            setattr(self, key, 0.0)
141      return self
142
143
144   def as_dict(self) -> Dict[str, float]:
145      """Returns the current geometric properties as a dictionary."""
146      return { key: val for key, val in self.__dict__.items() if key != 'name' }

Represents the shape-specific parametric geometry of a SymPart.

Geometry(identifier: str)
34   def __init__(self, identifier: str) -> None:
35      """Initializes a `Geometry` instance, where `identifier` uniquely identifies the instance."""
36      super().__init__()
37      self.name = identifier

Initializes a Geometry instance, where identifier uniquely identifies the instance.

name: str

Unique, identifying name of the Geometry instance.

def clone(self) -> Geometry:
79   def clone(self) -> Geometry:
80      """Returns an exact clone of this `Geometry` instance."""
81      return deepcopy(self)

Returns an exact clone of this Geometry instance.

def copy_from( self, other: Geometry) -> Geometry:
 84   def copy_from(self, other: Geometry) -> Geometry:
 85      """Copies the geometric parameters from another `Geometry` instance into this one.
 86
 87      The name of this instance will not be changed or overwritten.
 88
 89      Parameters
 90      ----------
 91      other : `Geometry`
 92         A Geometry object whose attributes should be copied into this instance.
 93
 94      Returns
 95      -------
 96      self : `Geometry`
 97         The Geometry instance being manipulated.
 98      """
 99      for key, val in other.__dict__.items():
100         if key != 'name':
101            setattr(self, key, val)
102      return self

Copies the geometric parameters from another Geometry instance into this one.

The name of this instance will not be changed or overwritten.

Parameters
  • other (Geometry): A Geometry object whose attributes should be copied into this instance.
Returns
  • self (Geometry): The Geometry instance being manipulated.
def set(self, **kwargs) -> Geometry:
105   def set(self, **kwargs) -> Geometry:
106      """Sets the underlying geometric parameters to the values specified.
107
108      The keys in the `**kwargs` dictionary will be different for each `SymPart`. If a key is
109      missing or its value is `None`, the corresponding geometric property will be treated
110      as a symbol.
111
112      Parameters
113      ----------
114      **kwargs : `Dict`
115         A dictionary containing values or symbols for the geometric properties present in this
116         Geometry instance.
117
118      Returns
119      -------
120      self : `Geometry`
121         The Geometry instance being manipulated.
122      """
123      for key in self.__dict__:
124         if key != 'name':
125            setattr(self, key, kwargs[key] if key in kwargs and kwargs[key] is not None else
126                    Symbol(self.name + '_' + key))
127      return self

Sets the underlying geometric parameters to the values specified.

The keys in the **kwargs dictionary will be different for each SymPart. If a key is missing or its value is None, the corresponding geometric property will be treated as a symbol.

Parameters
  • **kwargs (Dict): A dictionary containing values or symbols for the geometric properties present in this Geometry instance.
Returns
  • self (Geometry): The Geometry instance being manipulated.
def clear(self) -> Geometry:
130   def clear(self) -> Geometry:
131      """Clears all geometric properties to `0.0`.
132
133      Returns
134      -------
135      self : `Geometry`
136         The Geometry instance being manipulated.
137      """
138      for key in self.__dict__:
139         if key != 'name':
140            setattr(self, key, 0.0)
141      return self

Clears all geometric properties to 0.0.

Returns
  • self (Geometry): The Geometry instance being manipulated.
def as_dict(self) -> Dict[str, float]:
144   def as_dict(self) -> Dict[str, float]:
145      """Returns the current geometric properties as a dictionary."""
146      return { key: val for key, val in self.__dict__.items() if key != 'name' }

Returns the current geometric properties as a dictionary.