Project OOP refactorisation.
This commit is contained in:
@@ -7,29 +7,16 @@ Created on Thu Apr 27 12:03:30 2023
|
||||
Procedural OpenSCAD Generation
|
||||
https://github.com/taxpon/openpyscad/blob/develop/openpyscad/base.py
|
||||
"""
|
||||
#import argument_validation as av
|
||||
from colour_theme_braille_character import Colour_Theme_Character_Braille
|
||||
|
||||
# CLASSES
|
||||
# ATTRIBUTE DECLARATION
|
||||
# METHODS
|
||||
# FUNCTION
|
||||
# ARGUMENTS
|
||||
# ARGUMENT VALIDATION
|
||||
# ATTRIBUTE + VARIABLE INSTANTIATION
|
||||
# METHODS
|
||||
# RETURNS
|
||||
|
||||
# NORMAL METHODS
|
||||
# FUNCTION
|
||||
# ARGUMENTS
|
||||
# ARGUMENT VALIDATION
|
||||
# VARIABLE INSTANTIATION
|
||||
# METHODS
|
||||
# RETURNS
|
||||
|
||||
|
||||
import openpyscad as ops
|
||||
import argument_validation as av
|
||||
|
||||
# import openpyscad as ops
|
||||
from openpyscad import Union
|
||||
from typing import Optional
|
||||
from pydantic import BaseModel, Field, validator
|
||||
# from pydantic.fields import ModelField
|
||||
from abc import ABC, abstractmethod
|
||||
import os
|
||||
|
||||
def bool_2_str(mybool):
|
||||
# FUNCTION
|
||||
@@ -103,7 +90,7 @@ def triprism(a, L, centre = True):
|
||||
# RETURNS
|
||||
return ops.Polygon([[0,0], [0, a], [a, 0]]).linear_extrude(L, center = bool_2_str(centre)).translate([-a/2 if centre else 0, -a/3 if centre else 0, 0])
|
||||
|
||||
def gen_openscad_colours():
|
||||
def get_openscad_colours():
|
||||
return [ # openscad_colours = [
|
||||
"Lavender",
|
||||
"Thistle",
|
||||
@@ -246,4 +233,84 @@ def gen_openscad_colours():
|
||||
"SlateGray",
|
||||
"DarkSlateGray",
|
||||
"Black"
|
||||
]
|
||||
]
|
||||
|
||||
"""
|
||||
def base_fillet_cube(a, b, c, f, q, centre = "true"):
|
||||
# fillets removed as they print horribly
|
||||
# now just cube
|
||||
my_dif = ops.Difference();
|
||||
my_dif.append(ops.Cube([a, b, c], center = "true", _fn = q));
|
||||
return my_dif.translate([0 if (centre == "true") else a/2, 0 if (centre == "true") else b/2, 0 if (centre == "true") else c/2])
|
||||
|
||||
|
||||
def triprism(a, L, centre = "true"):
|
||||
return ops.Polygon([[0,0], [0, a], [a, 0]]).linear_extrude(L, center = "true").translate([0, 0, 0 if (centre == "true") else L/2]);
|
||||
"""
|
||||
|
||||
"""
|
||||
def __get_pydantic_core_schema__(cls, handler):
|
||||
return handler.generate_schema(ops.Union)
|
||||
|
||||
ops.Union.__get_pydantic_core_schema__ = classmethod(__get_pydantic_core_schema__)
|
||||
class Union_Field(ModelField):
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
@classmethod
|
||||
def __get_validators__(cls):
|
||||
yield cls.validate
|
||||
@classmethod
|
||||
def validate(cls, value):
|
||||
if not isinstance(value, Union):
|
||||
raise ValueError(f'Union_Field: {value} is not a valid Union. Type: {type(value)}')
|
||||
return value
|
||||
"""
|
||||
|
||||
class ObjectOpenSCAD(BaseModel, ABC):
|
||||
path_dir: str
|
||||
filename: str
|
||||
|
||||
def __init__(self, **kwargs): # , path_dir, filename
|
||||
# print(f'ObjectOpenSCAD: {path_dir}, {filename}')
|
||||
# BaseModel.__init__(self, path_dir=path_dir, filename=filename)
|
||||
# super().__init__(path_dir=path_dir, filename=filename, **kwargs)
|
||||
BaseModel.__init__(self, **kwargs)
|
||||
|
||||
@validator('path_dir')
|
||||
def validate_path_dir(cls, value):
|
||||
if not os.path.exists(value):
|
||||
raise ValueError("Path not found: " + value)
|
||||
return value
|
||||
|
||||
@abstractmethod
|
||||
def write(self):
|
||||
pass
|
||||
|
||||
class ModelOpenSCAD(ObjectOpenSCAD): # , BaseModel):
|
||||
colour_theme: Colour_Theme_Character_Braille
|
||||
fn: int = 25
|
||||
|
||||
model: Optional[Union] = None #= Field(default_factory=Union) # : Optional[ops.Union] = None
|
||||
|
||||
def __init__(self, **kwargs): # , path_dir, filename, colour_theme, fn):
|
||||
# print(f'ModelOpenSCAD: {path_dir}, {filename}, {colour_theme}, {fn}')
|
||||
ObjectOpenSCAD.__init__(self, **kwargs) # , path_dir=path_dir, filename=filename)
|
||||
BaseModel.__init__(self, **{**kwargs, 'model': None}) # , colour_theme=colour_theme, fn=fn)
|
||||
self.model = Union()
|
||||
|
||||
def write(self):
|
||||
self.model.write(self.path_dir + self.filename + '.scad')
|
||||
|
||||
class Config:
|
||||
arbitrary_types_allowed = True
|
||||
|
||||
class AssemblyOpenSCAD(ObjectOpenSCAD): # , BaseModel):
|
||||
dx: int
|
||||
dy: int
|
||||
max_models_per_row: int
|
||||
fn: int = Field(ge=0)
|
||||
|
||||
def __init__(self, path_dir, filename, dx, dy, max_models_per_row, fn=25):
|
||||
ObjectOpenSCAD.__init__(self, path_dir, filename)
|
||||
BaseModel.__init__(self, dx, dy, max_models_per_row, fn)
|
||||
self.assembly = ops.Assembly()
|
||||
Reference in New Issue
Block a user