|
|
|
|
@ -4,6 +4,7 @@ use hex_color::HexColor;
|
|
|
|
|
use simple_xml_builder::XMLElement;
|
|
|
|
|
use unicode_segmentation::UnicodeSegmentation;
|
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
use dxf::enums::AttachmentPoint;
|
|
|
|
|
|
|
|
|
|
/*use parley::{
|
|
|
|
|
Alignment, FontContext, FontWeight, InlineBox, Layout, LayoutContext, PositionedLayoutItem,
|
|
|
|
|
@ -85,7 +86,20 @@ impl From<&DynamicText> for XMLElement {
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
println!("文本:{}", txt.text);
|
|
|
|
|
println!("对齐方式:{}, {}", txt.h_alignment, txt.v_alignment);
|
|
|
|
|
println!("文本宽度: {}", txt_width);
|
|
|
|
|
println!("文本高度:{}", pt_size);
|
|
|
|
|
println!("初始位置: x={}, y={}", txt.x, txt.y);
|
|
|
|
|
println!("旋转角度: {}", txt.rotation);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 计算基础位置(不考虑旋转)
|
|
|
|
|
// 如果是mtext,是没有左下角的位置的,只有对齐点的位置,
|
|
|
|
|
// 所以需要根据对齐点和文本宽度来计算最终的位置
|
|
|
|
|
|
|
|
|
|
let base_x = txt.x + 0.5 - (pt_size / 8.0) - 4.05;
|
|
|
|
|
let base_y = {
|
|
|
|
|
let base_y_pos = txt.y + 0.5 - (7.0 / 5.0 * pt_size + 26.0 / 5.0) + pt_size;
|
|
|
|
|
@ -115,6 +129,9 @@ impl From<&DynamicText> for XMLElement {
|
|
|
|
|
(base_x, base_y)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
println!("最终位置: x={}, y={}", x_pos, y_pos);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dtxt_xml.add_attribute("x", two_dec(x_pos));
|
|
|
|
|
dtxt_xml.add_attribute("y", two_dec(y_pos));
|
|
|
|
|
dtxt_xml.add_attribute("z", two_dec(txt.z));
|
|
|
|
|
@ -241,11 +258,21 @@ impl<'a> DTextBuilder<'a> {
|
|
|
|
|
VAlignment::from(txt.vertical_text_justification),
|
|
|
|
|
0.0, // as Placeholder: no "reference_rectangle_width" with Text!!!
|
|
|
|
|
),
|
|
|
|
|
TextEntity::MText(mtxt) => (
|
|
|
|
|
mtxt.insertion_point.x,
|
|
|
|
|
-mtxt.insertion_point.y,
|
|
|
|
|
TextEntity::MText(mtxt) => {
|
|
|
|
|
let (adjusted_x, adjusted_y) = adjust_mtext_coordinates(
|
|
|
|
|
mtxt.insertion_point.x,
|
|
|
|
|
mtxt.insertion_point.y,
|
|
|
|
|
mtxt.attachment_point,
|
|
|
|
|
mtxt.initial_text_height,
|
|
|
|
|
mtxt.reference_rectangle_width
|
|
|
|
|
);
|
|
|
|
|
// 计算实际的旋转角度,优先使用x_axis_direction向量
|
|
|
|
|
let actual_rotation = calculate_mtext_rotation(mtxt.rotation_angle, &mtxt.x_axis_direction);
|
|
|
|
|
(
|
|
|
|
|
adjusted_x,
|
|
|
|
|
-adjusted_y,
|
|
|
|
|
mtxt.insertion_point.z,
|
|
|
|
|
mtxt.rotation_angle,
|
|
|
|
|
actual_rotation,
|
|
|
|
|
&mtxt.text_style_name,
|
|
|
|
|
//I'm not sure what the proper value is here for Mtext
|
|
|
|
|
//becuase I haven't actually finished supporting it.
|
|
|
|
|
@ -269,12 +296,13 @@ impl<'a> DTextBuilder<'a> {
|
|
|
|
|
{
|
|
|
|
|
let mut val = mtxt.extended_text.join("");
|
|
|
|
|
val.push_str(&mtxt.text);
|
|
|
|
|
val.replace("\\P", "\n")
|
|
|
|
|
let processed_val = val.replace("\\P", "\n");
|
|
|
|
|
super::extract_text_from_pcl(&processed_val)
|
|
|
|
|
},
|
|
|
|
|
HAlignment::from(mtxt.attachment_point),
|
|
|
|
|
VAlignment::from(mtxt.attachment_point),
|
|
|
|
|
mtxt.reference_rectangle_width,
|
|
|
|
|
),
|
|
|
|
|
)},
|
|
|
|
|
TextEntity::Attrib(attrib) => (
|
|
|
|
|
attrib.location.x,
|
|
|
|
|
-attrib.location.y,
|
|
|
|
|
@ -358,3 +386,86 @@ impl<'a> DTextBuilder<'a> {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// 计算MText的实际旋转角度
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
/// * `rotation_angle` - DXF中的rotation_angle字段(编号50)
|
|
|
|
|
/// * `x_axis_direction` - DXF中的x_axis_direction向量(编号11,21,31)
|
|
|
|
|
///
|
|
|
|
|
/// # Returns
|
|
|
|
|
/// 返回实际的旋转角度(弧度)
|
|
|
|
|
fn calculate_mtext_rotation(rotation_angle: f64, x_axis_direction: &dxf::Vector) -> f64 {
|
|
|
|
|
// 检查x_axis_direction是否为默认值(1.0, 0.0, 0.0)
|
|
|
|
|
let default_x_axis = (x_axis_direction.x - 1.0).abs() < 1e-10
|
|
|
|
|
&& x_axis_direction.y.abs() < 1e-10
|
|
|
|
|
&& x_axis_direction.z.abs() < 1e-10;
|
|
|
|
|
|
|
|
|
|
if default_x_axis {
|
|
|
|
|
// 如果x_axis_direction是默认值,使用rotation_angle
|
|
|
|
|
rotation_angle
|
|
|
|
|
} else {
|
|
|
|
|
// 如果x_axis_direction不是默认值,从向量计算角度
|
|
|
|
|
// atan2返回弧度值,需要转换为度数
|
|
|
|
|
let angle_radians = x_axis_direction.y.atan2(x_axis_direction.x);
|
|
|
|
|
// 将弧度转换为度数,这里rotation字段期望度数
|
|
|
|
|
angle_radians * 180.0 / std::f64::consts::PI
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 将MText的对齐点坐标调整为左下角坐标
|
|
|
|
|
///
|
|
|
|
|
/// # 参数
|
|
|
|
|
/// * `x` - MText的insertion_point.x(对齐点X坐标)
|
|
|
|
|
/// * `y` - MText的insertion_point.y(对齐点Y坐标)
|
|
|
|
|
/// * `attachment_point` - MText的对齐方式
|
|
|
|
|
/// * `text_height` - 文本高度
|
|
|
|
|
/// * `text_width` - 文本宽度(reference_rectangle_width)
|
|
|
|
|
///
|
|
|
|
|
/// # 返回值
|
|
|
|
|
/// 返回调整后的(x, y)坐标,对应文本的左下角位置
|
|
|
|
|
pub fn adjust_mtext_coordinates(
|
|
|
|
|
x: f64,
|
|
|
|
|
y: f64,
|
|
|
|
|
attachment_point: AttachmentPoint,
|
|
|
|
|
text_height: f64,
|
|
|
|
|
text_width: f64,
|
|
|
|
|
) -> (f64, f64) {
|
|
|
|
|
let mut adjusted_x = x;
|
|
|
|
|
let mut adjusted_y = y;
|
|
|
|
|
|
|
|
|
|
// 根据水平对齐方式调整X坐标
|
|
|
|
|
match attachment_point {
|
|
|
|
|
// 左对齐:无需调整X坐标
|
|
|
|
|
AttachmentPoint::TopLeft | AttachmentPoint::MiddleLeft | AttachmentPoint::BottomLeft => {
|
|
|
|
|
// X坐标已经是左边界,无需调整
|
|
|
|
|
}
|
|
|
|
|
// 中心对齐:向左偏移文本宽度的一半
|
|
|
|
|
AttachmentPoint::TopCenter | AttachmentPoint::MiddleCenter | AttachmentPoint::BottomCenter => {
|
|
|
|
|
adjusted_x -= text_width / 2.0; // 如果文本旋转了,那就不是这么算了
|
|
|
|
|
}
|
|
|
|
|
// 右对齐:向左偏移整个文本宽度
|
|
|
|
|
AttachmentPoint::TopRight | AttachmentPoint::MiddleRight | AttachmentPoint::BottomRight => {
|
|
|
|
|
adjusted_x -= text_width;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据垂直对齐方式调整Y坐标
|
|
|
|
|
match attachment_point {
|
|
|
|
|
// 底部对齐:无需调整Y坐标(已经是底边界)
|
|
|
|
|
AttachmentPoint::BottomLeft | AttachmentPoint::BottomCenter | AttachmentPoint::BottomRight => {
|
|
|
|
|
// Y坐标已经是底边界,无需调整
|
|
|
|
|
}
|
|
|
|
|
// 中心对齐:向下偏移文本高度的一半
|
|
|
|
|
AttachmentPoint::MiddleLeft | AttachmentPoint::MiddleCenter | AttachmentPoint::MiddleRight => {
|
|
|
|
|
adjusted_y -= text_height / 2.0;
|
|
|
|
|
}
|
|
|
|
|
// 顶部对齐:向下偏移整个文本高度
|
|
|
|
|
AttachmentPoint::TopLeft | AttachmentPoint::TopCenter | AttachmentPoint::TopRight => {
|
|
|
|
|
adjusted_y -= text_height;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(adjusted_x, adjusted_y)
|
|
|
|
|
}
|