use crate::entities::{Entity, EntityCommon};
use crate::types::{BoundingBox3D, Color, Handle, LineWeight, Transparency, Vector3};
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Block {
pub common: EntityCommon,
pub name: String,
pub base_point: Vector3,
pub description: String,
pub xref_path: String,
}
impl Block {
pub fn new(name: impl Into<String>, base_point: Vector3) -> Self {
Self {
common: EntityCommon::default(),
name: name.into(),
base_point,
description: String::new(),
xref_path: String::new(),
}
}
pub fn with_description(mut self, description: impl Into<String>) -> Self {
self.description = description.into();
self
}
pub fn with_xref_path(mut self, xref_path: impl Into<String>) -> Self {
self.xref_path = xref_path.into();
self
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockEnd {
pub common: EntityCommon,
}
impl BlockEnd {
pub fn new() -> Self {
Self {
common: EntityCommon::default(),
}
}
}
impl Default for BlockEnd {
fn default() -> Self {
Self::new()
}
}
impl Entity for Block {
fn handle(&self) -> Handle {
self.common.handle
}
fn set_handle(&mut self, handle: Handle) {
self.common.handle = handle;
}
fn layer(&self) -> &str {
&self.common.layer
}
fn set_layer(&mut self, layer: String) {
self.common.layer = layer;
}
fn color(&self) -> Color {
self.common.color
}
fn set_color(&mut self, color: Color) {
self.common.color = color;
}
fn line_weight(&self) -> LineWeight {
self.common.line_weight
}
fn set_line_weight(&mut self, line_weight: LineWeight) {
self.common.line_weight = line_weight;
}
fn transparency(&self) -> Transparency {
self.common.transparency
}
fn set_transparency(&mut self, transparency: Transparency) {
self.common.transparency = transparency;
}
fn is_invisible(&self) -> bool {
self.common.invisible
}
fn set_invisible(&mut self, invisible: bool) {
self.common.invisible = invisible;
}
fn bounding_box(&self) -> BoundingBox3D {
BoundingBox3D::from_point(self.base_point)
}
fn translate(&mut self, offset: Vector3) {
super::translate::translate_block(self, offset);
}
fn entity_type(&self) -> &'static str {
"BLOCK"
}
fn apply_transform(&mut self, transform: &crate::types::Transform) {
super::transform::transform_block(self, transform);
}
}
impl Entity for BlockEnd {
fn handle(&self) -> Handle {
self.common.handle
}
fn set_handle(&mut self, handle: Handle) {
self.common.handle = handle;
}
fn layer(&self) -> &str {
&self.common.layer
}
fn set_layer(&mut self, layer: String) {
self.common.layer = layer;
}
fn color(&self) -> Color {
self.common.color
}
fn set_color(&mut self, color: Color) {
self.common.color = color;
}
fn line_weight(&self) -> LineWeight {
self.common.line_weight
}
fn set_line_weight(&mut self, line_weight: LineWeight) {
self.common.line_weight = line_weight;
}
fn transparency(&self) -> Transparency {
self.common.transparency
}
fn set_transparency(&mut self, transparency: Transparency) {
self.common.transparency = transparency;
}
fn is_invisible(&self) -> bool {
self.common.invisible
}
fn set_invisible(&mut self, invisible: bool) {
self.common.invisible = invisible;
}
fn bounding_box(&self) -> BoundingBox3D {
BoundingBox3D::from_point(Vector3::new(0.0, 0.0, 0.0))
}
fn translate(&mut self, _offset: Vector3) {
super::translate::translate_block_end(self, _offset);
}
fn entity_type(&self) -> &'static str {
"ENDBLK"
}
fn apply_transform(&mut self, _transform: &crate::types::Transform) {
super::transform::transform_block_end(self, _transform);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_block_creation() {
let block = Block::new("MyBlock", Vector3::new(10.0, 20.0, 0.0));
assert_eq!(block.name, "MyBlock");
assert_eq!(block.base_point, Vector3::new(10.0, 20.0, 0.0));
assert_eq!(block.description, "");
}
#[test]
fn test_block_with_description() {
let block = Block::new("MyBlock", Vector3::new(0.0, 0.0, 0.0))
.with_description("Test block");
assert_eq!(block.description, "Test block");
}
#[test]
fn test_block_end_creation() {
let block_end = BlockEnd::new();
assert!(block_end.common.handle.is_null());
}
}