use crate::dwg::version::DwgVersion;
#[derive(Debug, Clone)]
pub struct WriterDocument {
pub version: DwgVersion,
pub modelspace: Vec<WriterEntity>,
pub blocks: Vec<BlockDef>,
pub layers: Vec<LayerDef>,
pub linetypes: Vec<LinetypeDef>,
pub text_styles: Vec<TextStyleDef>,
pub metadata: WriterMetadata,
pub image_defs: Vec<ImageDef>,
pub paperspace: Vec<WriterEntity>,
pub layout: LayoutSettings,
}
impl Default for WriterDocument {
fn default() -> Self {
Self {
version: DwgVersion::R2000,
modelspace: Vec::new(),
blocks: Vec::new(),
layers: vec![LayerDef::default()],
linetypes: Vec::new(),
text_styles: Vec::new(),
metadata: WriterMetadata::default(),
image_defs: Vec::new(),
paperspace: Vec::new(),
layout: LayoutSettings::default(),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct LayoutSettings {
pub name: String,
pub paper_width: f64,
pub paper_height: f64,
pub margins: (f64, f64, f64, f64),
pub paper_size: String,
pub plot_origin: (f64, f64),
pub paper_units: u16,
pub plot_rotation: u16,
pub plot_type: u16,
pub plot_layout_flags: u16,
pub plot_window: ((f64, f64), (f64, f64)),
pub scale_type: u16,
pub scale_numerator: f64,
pub scale_denominator: f64,
pub paper_image_origin: (f64, f64),
pub page_setup_name: String,
pub printer: String,
pub plot_view_name: String,
pub style_sheet: String,
}
impl Default for LayoutSettings {
fn default() -> Self {
Self {
name: "Layout1".into(),
paper_width: 420.,
paper_height: 297.,
margins: (0., 0., 0., 0.),
paper_size: String::new(),
plot_origin: (0., 0.),
paper_units: 1,
plot_rotation: 0,
plot_type: 5,
plot_layout_flags: 688,
plot_window: ((0., 0.), (0., 0.)),
scale_type: 16,
scale_numerator: 1.,
scale_denominator: 1.,
paper_image_origin: (0., 0.),
page_setup_name: String::new(),
printer: String::new(),
plot_view_name: String::new(),
style_sheet: String::new(),
}
}
}
impl LayoutSettings {
pub fn oriented_paper(&self) -> (f64, f64) {
if self.plot_rotation % 2 == 1 {
(self.paper_height, self.paper_width)
} else {
(self.paper_width, self.paper_height)
}
}
pub fn limits(&self) -> ((f64, f64), (f64, f64)) {
let (width, height) = self.oriented_paper();
let (left, bottom, _, _) = self.margins;
((-left, -bottom), (width - left, height - bottom))
}
pub fn paper_center(&self) -> (f64, f64) {
let ((x0, y0), (x1, y1)) = self.limits();
((x0 + x1) / 2., (y0 + y1) / 2.)
}
pub fn scale_factor(&self) -> f64 {
self.scale_numerator / self.scale_denominator
}
pub fn paper_viewport(&self) -> ViewportEntity {
let (width, height) = self.oriented_paper();
let center = self.paper_center();
ViewportEntity {
center: (center.0, center.1, 0.),
width,
height,
view_height: height,
view_center: center,
status_flags: PAPER_VIEWPORT_STATUS,
..ViewportEntity::default()
}
}
}
pub const PAPER_VIEWPORT_STATUS: u32 = 0xC8020;
pub const MODEL_VIEWPORT_STATUS: u32 = 0xC8060;
pub const VIEWPORT_LOCKED: u32 = 0x4000;
#[derive(Debug, Clone)]
pub struct ViewportEntity {
pub common: CommonEntityProps,
pub center: (f64, f64, f64),
pub width: f64,
pub height: f64,
pub view_target: (f64, f64, f64),
pub view_direction: (f64, f64, f64),
pub twist_angle: f64,
pub view_height: f64,
pub lens_length: f64,
pub front_clip_z: f64,
pub back_clip_z: f64,
pub snap_angle: f64,
pub view_center: (f64, f64),
pub snap_base: (f64, f64),
pub snap_spacing: (f64, f64),
pub grid_spacing: (f64, f64),
pub circle_zoom: u16,
pub status_flags: u32,
pub style_sheet: String,
pub render_mode: u8,
pub ucs_at_origin: bool,
pub ucs_per_viewport: bool,
pub ucs_origin: (f64, f64, f64),
pub ucs_x_axis: (f64, f64, f64),
pub ucs_y_axis: (f64, f64, f64),
pub ucs_elevation: f64,
pub ucs_ortho_view_type: u16,
pub frozen_layers: Vec<String>,
}
impl Default for ViewportEntity {
fn default() -> Self {
Self {
common: CommonEntityProps::default(),
center: (0., 0., 0.),
width: 1.,
height: 1.,
view_target: (0., 0., 0.),
view_direction: (0., 0., 1.),
twist_angle: 0.,
view_height: 1.,
lens_length: 50.,
front_clip_z: 0.,
back_clip_z: 0.,
snap_angle: 0.,
view_center: (0., 0.),
snap_base: (0., 0.),
snap_spacing: (10., 10.),
grid_spacing: (10., 10.),
circle_zoom: 1000,
status_flags: MODEL_VIEWPORT_STATUS,
style_sheet: String::new(),
render_mode: 0,
ucs_at_origin: false,
ucs_per_viewport: true,
ucs_origin: (0., 0., 0.),
ucs_x_axis: (1., 0., 0.),
ucs_y_axis: (0., 1., 0.),
ucs_elevation: 0.,
ucs_ortho_view_type: 0,
frozen_layers: Vec::new(),
}
}
}
#[derive(Debug, Clone)]
pub struct ImageDef {
pub name: String,
pub file_path: String,
pub size: (f64, f64),
pub is_loaded: bool,
pub resolution_units: u8,
pub pixel_size: (f64, f64),
}
#[derive(Debug, Clone)]
pub struct WriterMetadata {
pub codepage: u16,
pub insunits: u16,
pub lunits: u16,
pub luprec: u16,
pub aunits: u16,
pub auprec: u16,
pub ltscale: f64,
pub insertion_base: (f64, f64, f64),
pub ext_min: Option<(f64, f64, f64)>,
pub ext_max: Option<(f64, f64, f64)>,
}
impl Default for WriterMetadata {
fn default() -> Self {
Self {
codepage: 30,
insunits: 0,
lunits: 2,
luprec: 4,
aunits: 0,
auprec: 0,
ltscale: 1.0,
insertion_base: (0.0, 0.0, 0.0),
ext_min: None,
ext_max: None,
}
}
}
#[derive(Debug, Clone)]
pub struct LayerDef {
pub name: String,
pub color_index: u16,
pub true_color: Option<u32>,
pub linetype: String,
pub lineweight: i16,
pub frozen: bool,
pub off: bool,
pub frozen_in_new_viewports: bool,
pub locked: bool,
pub plot: bool,
}
impl Default for LayerDef {
fn default() -> Self {
Self {
name: "0".to_string(),
color_index: 7,
true_color: None,
linetype: "Continuous".into(),
lineweight: -3,
frozen: false,
off: false,
frozen_in_new_viewports: false,
locked: false,
plot: true,
}
}
}
#[derive(Debug, Clone, Default)]
pub struct LinetypeDef {
pub name: String,
pub description: String,
pub dashes: Vec<f64>,
}
#[derive(Debug, Clone, Copy)]
pub struct EntityFormatting {
pub linetype_scale: f64,
pub lineweight: i16,
pub invisible: bool,
pub extrusion: (f64, f64, f64),
pub thickness: f64,
pub elevation: f64,
pub alignment: Option<(f64, f64, f64)>,
pub oblique: f64,
pub width_factor: f64,
pub generation: u16,
pub halign: u16,
pub valign: u16,
pub line_spacing_style: u16,
pub line_spacing_factor: f64,
}
impl Default for EntityFormatting {
fn default() -> Self {
Self {
linetype_scale: 1.,
lineweight: -1,
invisible: false,
extrusion: (0., 0., 1.),
thickness: 0.,
elevation: 0.,
alignment: None,
oblique: 0.,
width_factor: 1.,
generation: 0,
halign: 0,
valign: 0,
line_spacing_style: 1,
line_spacing_factor: 1.,
}
}
}
#[derive(Debug, Clone, Default)]
pub struct CommonEntityProps {
pub handle: Option<u64>,
pub layer_name: String,
pub color_index: Option<u16>,
pub true_color: Option<u32>,
pub linetype: String,
pub style_name: String,
pub formatting: EntityFormatting,
}
#[derive(Debug, Clone)]
pub enum WriterEntity {
Line(LineEntity),
Point(PointEntity),
Ray(RayEntity),
XLine(XLineEntity),
Arc(ArcEntity),
Circle(CircleEntity),
Ellipse(EllipseEntity),
Spline(Box<SplineEntity>),
Solid(PlanarFaceEntity),
Trace(PlanarFaceEntity),
Face3d(Box<Face3dEntity>),
Polyline2d(PolylineEntity),
Polyline3d(PolylineEntity),
LwPolyline(LwPolylineEntity),
Text(TextEntity),
MText(MTextEntity),
Insert(Box<InsertEntity>),
Attdef(Box<AttributeEntity>),
Attrib(Box<AttributeEntity>),
Hatch(Box<HatchEntity>),
Dimension(Box<DimensionEntity>),
Image(Box<ImageEntity>),
Viewport(Box<ViewportEntity>),
}
pub const MAX_ENTITY_SIZE: usize = 336;
impl WriterEntity {
pub fn common(&self) -> &CommonEntityProps {
match self {
Self::Line(e) => &e.common,
Self::Point(e) => &e.common,
Self::Ray(e) => &e.common,
Self::XLine(e) => &e.common,
Self::Arc(e) => &e.common,
Self::Circle(e) => &e.common,
Self::Ellipse(e) => &e.common,
Self::Spline(e) => &e.common,
Self::Solid(e) | Self::Trace(e) => &e.common,
Self::Face3d(e) => &e.common,
Self::Polyline2d(e) | Self::Polyline3d(e) => &e.common,
Self::LwPolyline(e) => &e.common,
Self::Text(e) => &e.common,
Self::MText(e) => &e.common,
Self::Insert(e) => &e.common,
Self::Attdef(e) | Self::Attrib(e) => &e.text.common,
Self::Hatch(e) => &e.common,
Self::Dimension(e) => &e.common,
Self::Image(e) => &e.common,
Self::Viewport(e) => &e.common,
}
}
pub fn common_mut(&mut self) -> &mut CommonEntityProps {
match self {
Self::Line(e) => &mut e.common,
Self::Point(e) => &mut e.common,
Self::Ray(e) => &mut e.common,
Self::XLine(e) => &mut e.common,
Self::Arc(e) => &mut e.common,
Self::Circle(e) => &mut e.common,
Self::Ellipse(e) => &mut e.common,
Self::Spline(e) => &mut e.common,
Self::Solid(e) | Self::Trace(e) => &mut e.common,
Self::Face3d(e) => &mut e.common,
Self::Polyline2d(e) | Self::Polyline3d(e) => &mut e.common,
Self::LwPolyline(e) => &mut e.common,
Self::Text(e) => &mut e.common,
Self::MText(e) => &mut e.common,
Self::Insert(e) => &mut e.common,
Self::Attdef(e) | Self::Attrib(e) => &mut e.text.common,
Self::Hatch(e) => &mut e.common,
Self::Dimension(e) => &mut e.common,
Self::Image(e) => &mut e.common,
Self::Viewport(e) => &mut e.common,
}
}
}
#[derive(Debug, Clone)]
pub struct ImageEntity {
pub common: CommonEntityProps,
pub image_def: String,
pub insert: (f64, f64, f64),
pub u_vector: (f64, f64, f64),
pub v_vector: (f64, f64, f64),
pub size: (f64, f64),
pub display_props: u16,
pub clipping: bool,
pub brightness: u8,
pub contrast: u8,
pub fade: u8,
pub clip_type: u16,
pub clip_vertices: Vec<(f64, f64)>,
}
#[derive(Debug, Clone)]
pub struct DimensionEntity {
pub common: CommonEntityProps,
pub kind: DimensionKind,
pub text_midpoint: (f64, f64),
pub elevation: f64,
pub flags: u8,
pub user_text: String,
pub text_rotation: f64,
pub horizontal_direction: f64,
pub insert_scale: (f64, f64, f64),
pub insert_rotation: f64,
pub attachment_point: u16,
pub line_spacing_style: u16,
pub line_spacing_factor: f64,
pub actual_measurement: f64,
pub insert_point: (f64, f64),
pub dimstyle: String,
pub block_name: String,
}
#[derive(Debug, Clone)]
pub enum DimensionKind {
Linear {
point13: (f64, f64, f64),
point14: (f64, f64, f64),
point10: (f64, f64, f64),
ext_line_rotation: f64,
dim_rotation: f64,
},
Aligned {
point13: (f64, f64, f64),
point14: (f64, f64, f64),
point10: (f64, f64, f64),
ext_line_rotation: f64,
},
Angular3Pt {
point10: (f64, f64, f64),
point13: (f64, f64, f64),
point14: (f64, f64, f64),
point15: (f64, f64, f64),
},
Angular2Ln {
point16: (f64, f64),
point13: (f64, f64, f64),
point14: (f64, f64, f64),
point15: (f64, f64, f64),
point10: (f64, f64, f64),
},
Radius {
point10: (f64, f64, f64),
point15: (f64, f64, f64),
leader_length: f64,
},
Diameter {
point15: (f64, f64, f64),
point10: (f64, f64, f64),
leader_length: f64,
},
Ordinate {
point10: (f64, f64, f64),
point13: (f64, f64, f64),
point14: (f64, f64, f64),
flags2: u8,
},
}
impl DimensionKind {
pub fn type_code(&self) -> u16 {
match self {
Self::Ordinate { .. } => 20,
Self::Linear { .. } => 21,
Self::Aligned { .. } => 22,
Self::Angular3Pt { .. } => 23,
Self::Angular2Ln { .. } => 24,
Self::Radius { .. } => 25,
Self::Diameter { .. } => 26,
}
}
}
#[derive(Debug, Clone)]
pub struct HatchEntity {
pub common: CommonEntityProps,
pub name: String,
pub solid_fill: bool,
pub paths: Vec<HatchPathDef>,
pub style: u16,
pub pattern_type: u16,
pub pattern: Option<HatchPatternDef>,
pub pixel_size: Option<f64>,
pub seed_points: Vec<(f64, f64)>,
}
#[derive(Debug, Clone)]
pub struct HatchPathDef {
pub flags: u32,
pub data: HatchPathData,
}
#[derive(Debug, Clone)]
pub enum HatchPathData {
Polyline {
closed: bool,
vertices: Vec<(f64, f64)>,
bulges: Option<Vec<f64>>,
},
Edges(Vec<HatchEdge>),
}
#[derive(Debug, Clone)]
pub enum HatchEdge {
Line {
start: (f64, f64),
end: (f64, f64),
},
Arc {
center: (f64, f64),
radius: f64,
start_angle: f64,
end_angle: f64,
ccw: bool,
},
Ellipse {
center: (f64, f64),
major_endpoint: (f64, f64),
ratio: f64,
start_angle: f64,
end_angle: f64,
ccw: bool,
},
Spline {
degree: u32,
rational: bool,
periodic: bool,
knots: Vec<f64>,
control_points: Vec<(f64, f64)>,
weights: Vec<f64>,
},
}
#[derive(Debug, Clone)]
pub struct HatchPatternDef {
pub angle: f64,
pub scale: f64,
pub double: bool,
pub lines: Vec<HatchPatternLineDef>,
}
#[derive(Debug, Clone)]
pub struct HatchPatternLineDef {
pub angle: f64,
pub base: (f64, f64),
pub offset: (f64, f64),
pub dashes: Vec<f64>,
}
#[derive(Debug, Clone, Default)]
pub struct LineEntity {
pub common: CommonEntityProps,
pub start: (f64, f64, f64),
pub end: (f64, f64, f64),
}
#[derive(Debug, Clone, Default)]
pub struct PointEntity {
pub common: CommonEntityProps,
pub location: (f64, f64, f64),
pub x_axis_angle: f64,
}
#[derive(Debug, Clone, Default)]
pub struct RayEntity {
pub common: CommonEntityProps,
pub start: (f64, f64, f64),
pub unit_vector: (f64, f64, f64),
}
#[derive(Debug, Clone, Default)]
pub struct XLineEntity {
pub common: CommonEntityProps,
pub start: (f64, f64, f64),
pub unit_vector: (f64, f64, f64),
}
#[derive(Debug, Clone, Default)]
pub struct ArcEntity {
pub common: CommonEntityProps,
pub center: (f64, f64, f64),
pub radius: f64,
pub angle_start_rad: f64,
pub angle_end_rad: f64,
}
#[derive(Debug, Clone, Default)]
pub struct CircleEntity {
pub common: CommonEntityProps,
pub center: (f64, f64, f64),
pub radius: f64,
}
#[derive(Debug, Clone)]
pub struct EllipseEntity {
pub common: CommonEntityProps,
pub center: (f64, f64, f64),
pub major_axis: (f64, f64, f64),
pub axis_ratio: f64,
pub start_param: f64,
pub end_param: f64,
}
#[derive(Debug, Clone)]
pub struct SplineEntity {
pub common: CommonEntityProps,
pub degree: u32,
pub data: SplineData,
}
#[derive(Debug, Clone)]
pub enum SplineData {
Control {
rational: bool,
closed: bool,
periodic: bool,
knot_tolerance: f64,
ctrl_tolerance: f64,
knots: Vec<f64>,
control_points: Vec<(f64, f64, f64)>,
weights: Vec<f64>,
},
Fit {
fit_tolerance: f64,
start_tangent: (f64, f64, f64),
end_tangent: (f64, f64, f64),
fit_points: Vec<(f64, f64, f64)>,
},
}
#[derive(Debug, Clone)]
pub struct PlanarFaceEntity {
pub common: CommonEntityProps,
pub points: [(f64, f64, f64); 4],
}
#[derive(Debug, Clone)]
pub struct Face3dEntity {
pub common: CommonEntityProps,
pub points: [(f64, f64, f64); 4],
pub invisible_edge_flags: u16,
}
#[derive(Debug, Clone, Default)]
pub struct LwPolylineEntity {
pub common: CommonEntityProps,
pub flags: u16,
pub vertices: Vec<(f64, f64)>,
pub const_width: Option<f64>,
pub bulges: Vec<f64>,
pub widths: Vec<(f64, f64)>,
}
#[derive(Debug, Clone, Default)]
pub struct PolylineEntity {
pub common: CommonEntityProps,
pub flags: u16,
pub width_start: f64,
pub width_end: f64,
pub vertices: Vec<PolylineVertex>,
}
#[derive(Debug, Clone, Default)]
pub struct PolylineVertex {
pub position: (f64, f64, f64),
pub start_width: f64,
pub end_width: f64,
pub bulge: f64,
}
#[derive(Debug, Clone, Default)]
pub struct TextEntity {
pub common: CommonEntityProps,
pub text: String,
pub insert: (f64, f64, f64),
pub height: f64,
pub rotation_rad: f64,
}
#[derive(Debug, Clone, Default)]
pub struct MTextEntity {
pub common: CommonEntityProps,
pub text: String,
pub insert: (f64, f64, f64),
pub text_direction: (f64, f64, f64),
pub rect_width: f64,
pub char_height: f64,
pub attachment_point: u16,
pub drawing_direction: u16,
}
#[derive(Debug, Clone)]
pub struct TextStyleDef {
pub name: String,
pub vertical: bool,
pub height: f64,
pub width: f64,
pub oblique: f64,
pub generation: u8,
pub last_height: f64,
pub font: String,
pub bigfont: String,
}
impl Default for TextStyleDef {
fn default() -> Self {
Self {
name: "Standard".into(),
vertical: false,
height: 0.,
width: 1.,
oblique: 0.,
generation: 0,
last_height: 2.5,
font: "txt".into(),
bigfont: String::new(),
}
}
}
#[derive(Debug, Clone, Default)]
pub struct BlockDef {
pub name: String,
pub base_point: (f64, f64, f64),
pub description: String,
pub entities: Vec<WriterEntity>,
}
#[derive(Debug, Clone)]
pub struct InsertEntity {
pub common: CommonEntityProps,
pub block_name: String,
pub insert: (f64, f64, f64),
pub scale: (f64, f64, f64),
pub rotation_rad: f64,
pub attributes: Vec<AttributeEntity>,
}
#[derive(Debug, Clone, Default)]
pub struct AttributeEntity {
pub text: TextEntity,
pub tag: String,
pub prompt: String,
pub flags: u8,
pub field_length: u16,
}
impl WriterDocument {
pub fn layouts(&self) -> impl Iterator<Item = &[WriterEntity]> {
std::iter::once(self.modelspace.as_slice())
.chain(std::iter::once(self.paperspace.as_slice()))
.chain(self.blocks.iter().map(|b| b.entities.as_slice()))
}
pub const PAPER_LAYOUT: usize = 1;
pub const FIRST_BLOCK_LAYOUT: usize = 2;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn entity_enum_stays_small() {
assert!(std::mem::size_of::<WriterEntity>() <= MAX_ENTITY_SIZE);
}
}