use super::TableEntry;
use crate::types::Handle;
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct LineTypeElement {
pub length: f64,
}
impl LineTypeElement {
pub fn dash(length: f64) -> Self {
LineTypeElement { length: length.abs() }
}
pub fn space(length: f64) -> Self {
LineTypeElement { length: -length.abs() }
}
pub fn dot() -> Self {
LineTypeElement { length: 0.0 }
}
pub fn is_dash(&self) -> bool {
self.length > 0.0
}
pub fn is_space(&self) -> bool {
self.length < 0.0
}
pub fn is_dot(&self) -> bool {
self.length == 0.0
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct LineType {
pub handle: Handle,
pub name: String,
pub description: String,
pub elements: Vec<LineTypeElement>,
pub pattern_length: f64,
pub alignment: char,
pub xref_dependent: bool,
}
impl LineType {
pub fn new(name: impl Into<String>) -> Self {
LineType {
handle: Handle::NULL,
name: name.into(),
description: String::new(),
elements: Vec::new(),
pattern_length: 0.0,
alignment: 'A',
xref_dependent: false,
}
}
pub fn continuous() -> Self {
LineType {
handle: Handle::NULL,
name: "Continuous".to_string(),
description: "Solid line".to_string(),
elements: Vec::new(),
pattern_length: 0.0,
alignment: 'A',
xref_dependent: false,
}
}
pub fn by_layer() -> Self {
LineType {
handle: Handle::NULL,
name: "ByLayer".to_string(),
description: String::new(),
elements: Vec::new(),
pattern_length: 0.0,
alignment: 'A',
xref_dependent: false,
}
}
pub fn by_block() -> Self {
LineType {
handle: Handle::NULL,
name: "ByBlock".to_string(),
description: String::new(),
elements: Vec::new(),
pattern_length: 0.0,
alignment: 'A',
xref_dependent: false,
}
}
pub fn dashed() -> Self {
let mut lt = LineType::new("Dashed");
lt.description = "__ __ __ __ __ __".to_string();
lt.add_element(LineTypeElement::dash(0.5));
lt.add_element(LineTypeElement::space(0.25));
lt.pattern_length = 0.75;
lt
}
pub fn dotted() -> Self {
let mut lt = LineType::new("Dotted");
lt.description = ". . . . . . . .".to_string();
lt.add_element(LineTypeElement::dot());
lt.add_element(LineTypeElement::space(0.25));
lt.pattern_length = 0.25;
lt
}
pub fn add_element(&mut self, element: LineTypeElement) {
self.elements.push(element);
}
pub fn element_count(&self) -> usize {
self.elements.len()
}
pub fn is_continuous(&self) -> bool {
self.elements.is_empty()
}
}
impl TableEntry for LineType {
fn handle(&self) -> Handle {
self.handle
}
fn set_handle(&mut self, handle: Handle) {
self.handle = handle;
}
fn name(&self) -> &str {
&self.name
}
fn set_name(&mut self, name: String) {
self.name = name;
}
fn is_standard(&self) -> bool {
matches!(
self.name.as_str(),
"Continuous" | "ByLayer" | "ByBlock"
)
}
}