From 55ca13ecb4a93c423ef1c5f4fb614f54d1118d4f Mon Sep 17 00:00:00 2001 From: liaoxianglian Date: Wed, 17 Sep 2025 09:30:49 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E6=A0=87=E6=B3=A8=E9=A2=9C?= =?UTF-8?q?=E8=89=B2=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/qelmt/dynamictext.rs | 11 +++++++-- src/qelmt/mod.rs | 52 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/src/qelmt/dynamictext.rs b/src/qelmt/dynamictext.rs index 1499410..a606d17 100644 --- a/src/qelmt/dynamictext.rs +++ b/src/qelmt/dynamictext.rs @@ -90,9 +90,10 @@ impl From<&DynamicText> for XMLElement { let base_y = { let base_y_pos = txt.y + 0.5 - (7.0 / 5.0 * pt_size + 26.0 / 5.0) + pt_size; match txt.v_alignment { - VAlignment::Top => base_y_pos, + VAlignment::Top => base_y_pos + pt_size / 2.0, VAlignment::Center => base_y_pos - pt_size / 2.0, - VAlignment::Bottom => base_y_pos - pt_size, + VAlignment::Bottom => base_y_pos - pt_size + pt_size / 2.0, + } }; @@ -136,6 +137,12 @@ impl From<&DynamicText> for XMLElement { text_xml.add_text(&txt.text); dtxt_xml.add_child(text_xml); + // 添加颜色 + let mut color_xml = XMLElement::new("color"); + color_xml.add_text(&txt.color.display_rgb()); + dtxt_xml.add_child(color_xml); + + if let Some(i_name) = &txt.info_name { dtxt_xml.add_attribute("info_name", i_name); } diff --git a/src/qelmt/mod.rs b/src/qelmt/mod.rs index 14ea42c..32b585d 100644 --- a/src/qelmt/mod.rs +++ b/src/qelmt/mod.rs @@ -704,7 +704,13 @@ impl<'a> ObjectsBuilder<'a> { //how best to pass in the flag for dynamic text or not....should the flag also default to true? let mut text: Text = ( text, - HexColor::from_u32(self.ent.common.color_24_bit as u32), + { + 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) + } + }, ) .into(); @@ -716,7 +722,15 @@ impl<'a> ObjectsBuilder<'a> { Objects::Text(text) } else { let mut dtext = DTextBuilder::from_text(text) - .color(HexColor::from_u32(self.ent.common.color_24_bit as u32)) + .color({ + // 优先使用 ACI 颜色索引 + if let Some(aci_index) = self.ent.common.color.index() { + aci_to_hex_color(aci_index) + } else { + // 回退到24位颜色 + HexColor::from_u32(self.ent.common.color_24_bit as u32) + } + }) .build(); dtext.scale(self.scale_fact.x, self.scale_fact.y); @@ -971,7 +985,16 @@ impl<'a> ObjectsBuilder<'a> { EntityType::AttributeDefinition(attrib) => Ok({ //need to look up the proper way to get the color for the Attrib let mut dtext = DTextBuilder::from_attrib(attrib) - .color(HexColor::from_u32(self.ent.common.color_24_bit as u32)) + .color({ + println!("color index:{:?}", self.ent.common.color.index()); + // 优先使用 ACI 颜色索引 + if let Some(aci_index) = self.ent.common.color.index() { + aci_to_hex_color(aci_index) + } else { + // 回退到24位颜色 + HexColor::from_u32(self.ent.common.color_24_bit as u32) + } + }) .build(); dtext.scale(self.scale_fact.x, self.scale_fact.y); @@ -1662,3 +1685,26 @@ enum TextEntity<'a> { Attrib(&'a AttributeDefinition), } +// AutoCAD Color Index (ACI) 到 RGB 转换函数 +fn aci_to_rgb(aci: u8) -> (u8, u8, u8) { + match aci { + 1 => (255, 0, 0), // 红色 + 2 => (255, 255, 0), // 黄色 + 3 => (0, 255, 0), // 绿色 + 4 => (0, 255, 255), // 青色 + 5 => (0, 0, 255), // 蓝色 + 6 => (255, 0, 255), // 洋红色 + 7 => (255, 255, 255), // 白色 + 8 => (128, 128, 128), // 深灰色 + 9 => (192, 192, 192), // 浅灰色 + 94 => (0, 129, 0), // 深绿色 + // 更多标准颜色可以根据需要添加 + _ => (0, 0, 0), // 默认黑色 + } +} + +// ACI 转换为 HexColor +fn aci_to_hex_color(aci: u8) -> HexColor { + let (r, g, b) = aci_to_rgb(aci); + HexColor::rgb(r, g, b) +} \ No newline at end of file