use super::TableEntry;
use crate::types::{Color, Handle, LineWeight};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct LayerFlags {
pub frozen: bool,
pub locked: bool,
pub off: bool,
pub xref_dependent: bool,
}
impl LayerFlags {
pub fn new() -> Self {
LayerFlags {
frozen: false,
locked: false,
off: false,
xref_dependent: false,
}
}
pub fn standard() -> Self {
Self::new()
}
}
impl Default for LayerFlags {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Layer {
pub handle: Handle,
pub name: String,
pub flags: LayerFlags,
pub color: Color,
pub line_type: String,
pub line_weight: LineWeight,
pub plot_style: String,
pub is_plottable: bool,
pub material: Handle,
pub plotstyle_handle: Handle,
pub xref_block_record_handle: Handle,
}
impl Layer {
pub fn new(name: impl Into<String>) -> Self {
Layer {
handle: Handle::NULL,
name: name.into(),
flags: LayerFlags::new(),
color: Color::WHITE,
line_type: "Continuous".to_string(),
line_weight: LineWeight::Default,
plot_style: String::new(),
is_plottable: true,
material: Handle::NULL,
plotstyle_handle: Handle::NULL,
xref_block_record_handle: Handle::NULL,
}
}
pub fn layer_0() -> Self {
Layer {
handle: Handle::NULL,
name: "0".to_string(),
flags: LayerFlags::standard(),
color: Color::WHITE,
line_type: "Continuous".to_string(),
line_weight: LineWeight::Default,
plot_style: String::new(),
is_plottable: true,
material: Handle::NULL,
plotstyle_handle: Handle::NULL,
xref_block_record_handle: Handle::NULL,
}
}
pub fn with_color(name: impl Into<String>, color: Color) -> Self {
Layer {
color,
..Self::new(name)
}
}
pub fn freeze(&mut self) {
self.flags.frozen = true;
}
pub fn thaw(&mut self) {
self.flags.frozen = false;
}
pub fn is_frozen(&self) -> bool {
self.flags.frozen
}
pub fn lock(&mut self) {
self.flags.locked = true;
}
pub fn unlock(&mut self) {
self.flags.locked = false;
}
pub fn is_locked(&self) -> bool {
self.flags.locked
}
pub fn turn_off(&mut self) {
self.flags.off = true;
}
pub fn turn_on(&mut self) {
self.flags.off = false;
}
pub fn is_off(&self) -> bool {
self.flags.off
}
pub fn is_visible(&self) -> bool {
!self.flags.off && !self.flags.frozen
}
}
impl TableEntry for Layer {
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 {
self.name == "0"
}
}