解决弧线和线的颜色问题(未成功解决)

master
liaoxianglian 5 months ago
parent 89acf05ad5
commit 373d6dfa14

@ -1,6 +1,7 @@
use super::{two_dec, ScaleEntity}; use super::{two_dec, ScaleEntity};
use dxf::entities; use dxf::entities;
use dxf::entities::LwPolyline; use dxf::entities::LwPolyline;
use hex_color::HexColor;
use simple_xml_builder::XMLElement; use simple_xml_builder::XMLElement;
#[derive(Debug)] #[derive(Debug)]
@ -19,7 +20,13 @@ pub struct Arc {
impl From<&entities::Arc> for Arc { impl From<&entities::Arc> for Arc {
fn from(arc: &entities::Arc) -> Self { fn from(arc: &entities::Arc) -> Self {
Self::from_arc_with_color(arc, HexColor::BLACK)
}
}
impl Arc {
/// 从DXF Arc实体创建Arc支持自定义颜色
pub fn from_arc_with_color(arc: &entities::Arc, color: HexColor) -> Self {
let is_mirror = arc.normal.z < 0.0; let is_mirror = arc.normal.z < 0.0;
let (start_angle, angle_span) = if is_mirror { let (start_angle, angle_span) = if is_mirror {
@ -74,11 +81,10 @@ impl From<&entities::Arc> for Arc {
//reasons...I'm trying to think if there is a time we might want to turn it on? //reasons...I'm trying to think if there is a time we might want to turn it on?
antialias: false, antialias: false,
style: if arc.thickness > 0.1 { style: if arc.thickness > 0.1 {
"line-style:normal;line-weight:normal;filling:none;color:black" format!("line-style:normal;line-weight:normal;filling:none;color:{}", color.display_rgb())
} else { } else {
"line-style:normal;line-weight:thin;filling:none;color:black" format!("line-style:normal;line-weight:thin;filling:none;color:{}", color.display_rgb())
} },
.into(),
} }
} }
} }

@ -81,7 +81,7 @@ impl From<&DynamicText> for XMLElement {
} }
} }
let txt_width = if txt.reference_rectangle_width > 2.0 { let txt_width = if txt.reference_rectangle_width > 2.0 {
txt.reference_rectangle_width txt.reference_rectangle_width
} else { } else {
(graphene_count as f64) * pt_size * 0.75 (graphene_count as f64) * pt_size * 0.75
@ -106,12 +106,15 @@ impl From<&DynamicText> for XMLElement {
// } // }
// }; // };
// 如果采用计算对齐的方式,可能还要考虑字体宽度的偏差。。
println!("文本:{}", txt.text); println!("文本:{}", txt.text);
println!("对齐方式:{}, {}", txt.h_alignment, txt.v_alignment); println!("对齐方式:{}, {}", txt.h_alignment, txt.v_alignment);
println!("文本宽度: {}", txt_width); println!("文本宽度: {}", txt_width);
println!("文本高度:{}", pt_size); println!("文本高度:{}", pt_size);
println!("初始位置: x={}, y={}", txt.x, txt.y); println!("初始位置: x={}, y={}", txt.x, txt.y);
println!("旋转角度: {}", txt.rotation); println!("旋转角度: {}", txt.rotation);
println!("cjk_char_count: {}", cjk_char_count);
// 计算基础位置(不考虑旋转) // 计算基础位置(不考虑旋转)
// txt.x和txt.y现在是对齐点坐标需要根据对齐方式计算出左上角位置 // txt.x和txt.y现在是对齐点坐标需要根据对齐方式计算出左上角位置
@ -128,7 +131,7 @@ impl From<&DynamicText> for XMLElement {
// 根据是否包含中文字符调整字体大小相关的偏移量 // 根据是否包含中文字符调整字体大小相关的偏移量
let cjk_offset:f64 = (cjk_char_count as f64) * pt_size * 0.75; let cjk_offset:f64 = (cjk_char_count as f64) * pt_size * 0.75;
// 左对齐是不需要偏移的,居中对齐需要偏移,右对齐要偏移 // 左对齐是不需要中文偏移的,居中对齐需要偏移,右对齐要偏移
let base_x = left_x + 0.5 - (pt_size / 8.0) - 4.05 let base_x = left_x + 0.5 - (pt_size / 8.0) - 4.05
- match txt.h_alignment { - match txt.h_alignment {
HAlignment::Left => 0.0, HAlignment::Left => 0.0,

@ -2,6 +2,7 @@ use super::two_dec;
use super::LineEnd; use super::LineEnd;
use super::ScaleEntity; use super::ScaleEntity;
use dxf::entities::{self, LwPolyline, Polyline}; use dxf::entities::{self, LwPolyline, Polyline};
use hex_color::HexColor;
use simple_xml_builder::XMLElement; use simple_xml_builder::XMLElement;
#[derive(Debug)] #[derive(Debug)]
@ -25,26 +26,7 @@ pub struct Leader(pub Vec<Line>);
impl From<&entities::Line> for Line { impl From<&entities::Line> for Line {
fn from(line: &entities::Line) -> Self { fn from(line: &entities::Line) -> Self {
Line { Self::from_line_with_color(line, HexColor::BLACK)
x1: line.p1.x,
y1: -line.p1.y,
length1: 1.5, //why is this statically set at 1.5?
end1: LineEnd::None,
x2: line.p2.x,
y2: -line.p2.y,
length2: 1.5, //why is this statically set at 1.5?
end2: LineEnd::None,
//in the original code antialias is always set to false...I'm guessing for performance
//reasons...I'm trying to think if there is a time we might want to turn it on?
antialias: false,
style: if line.thickness > 0.5 {
"line-style:normal;line-weight:normal;filling:none;color:black"
} else {
"line-style:normal;line-weight:thin;filling:none;color:black"
}
.into(),
}
} }
} }
@ -174,6 +156,29 @@ impl From<&Line> for XMLElement {
} }
impl Line { impl Line {
/// 从DXF Line实体创建Line支持自定义颜色
pub fn from_line_with_color(line: &entities::Line, color: HexColor) -> Self {
Line {
x1: line.p1.x,
y1: -line.p1.y,
length1: 1.5, //why is this statically set at 1.5?
end1: LineEnd::None,
x2: line.p2.x,
y2: -line.p2.y,
length2: 1.5, //why is this statically set at 1.5?
end2: LineEnd::None,
//in the original code antialias is always set to false...I'm guessing for performance
//reasons...I'm trying to think if there is a time we might want to turn it on?
antialias: false,
style: if line.thickness > 0.5 {
format!("line-style:normal;line-weight:normal;filling:none;color:{}", color.display_rgb())
} else {
format!("line-style:normal;line-weight:thin;filling:none;color:{}", color.display_rgb())
},
}
}
pub fn new(x1: f64, y1: f64, x2: f64, y2: f64, style: String) -> Self { pub fn new(x1: f64, y1: f64, x2: f64, y2: f64, style: String) -> Self {
Line { Line {
length2: 1.5, length2: 1.5,

@ -642,7 +642,14 @@ impl<'a> ObjectsBuilder<'a> {
Ok(Objects::Ellipse(ellipse)) Ok(Objects::Ellipse(ellipse))
} }
EntityType::Line(line) => { EntityType::Line(line) => {
let mut line: Line = line.into(); // 获取颜色
let color = if let Some(aci_index) = self.ent.common.color.index() {
aci_to_hex_color(aci_index)
} else {
HexColor::from_u32(self.ent.common.color_24_bit as u32)
};
let mut line: Line = Line::from_line_with_color(line, color);
// 根据line_type_name更新线型样式 // 根据line_type_name更新线型样式
line.update_line_style(&update_line_style); line.update_line_style(&update_line_style);
@ -661,7 +668,14 @@ impl<'a> ObjectsBuilder<'a> {
Ok(Objects::Line(line)) Ok(Objects::Line(line))
} }
EntityType::Arc(arc) => { EntityType::Arc(arc) => {
let mut arc: Arc = arc.into(); // 获取颜色
let color = if let Some(aci_index) = self.ent.common.color.index() {
aci_to_hex_color(aci_index)
} else {
HexColor::from_u32(self.ent.common.color_24_bit as u32)
};
let mut arc: Arc = Arc::from_arc_with_color(arc, color);
// 根据line_type_name更新线型样式 // 根据line_type_name更新线型样式
arc.update_line_style(&update_line_style); arc.update_line_style(&update_line_style);
@ -1352,6 +1366,7 @@ impl From<HorizontalTextJustification> for HAlignment {
match value { match value {
HorizontalTextJustification::Left => HAlignment::Left, HorizontalTextJustification::Left => HAlignment::Left,
HorizontalTextJustification::Center => HAlignment::Center, HorizontalTextJustification::Center => HAlignment::Center,
HorizontalTextJustification::Middle => HAlignment::Center,
HorizontalTextJustification::Right => HAlignment::Right, HorizontalTextJustification::Right => HAlignment::Right,
//TODO: Handling the Aligned Middle and Fit alignments are a bit more complicated //TODO: Handling the Aligned Middle and Fit alignments are a bit more complicated
@ -1700,6 +1715,8 @@ fn aci_to_rgb(aci: u8) -> (u8, u8, u8) {
8 => (128, 128, 128), // 深灰色 8 => (128, 128, 128), // 深灰色
9 => (192, 192, 192), // 浅灰色 9 => (192, 192, 192), // 浅灰色
94 => (0, 129, 0), // 深绿色 94 => (0, 129, 0), // 深绿色
15 => (129, 86, 86), // 深棕色
// 更多标准颜色可以根据需要添加 // 更多标准颜色可以根据需要添加
_ => (0, 0, 0), // 默认黑色 _ => (0, 0, 0), // 默认黑色
} }

Loading…
Cancel
Save