diff --git a/src/qelmt/ellipse.rs b/src/qelmt/ellipse.rs index 6dcf8ab..ca130ca 100644 --- a/src/qelmt/ellipse.rs +++ b/src/qelmt/ellipse.rs @@ -100,7 +100,9 @@ impl TryFrom<&LwPolyline> for Ellipse { type Error = &'static str; //add better error later fn try_from(poly: &LwPolyline) -> Result { - if !poly.is_circular() { + let is_bugle_circle = poly.is_circular_with_bulge(); + + if !is_bugle_circle&&!poly.is_circular() { return Err("Polyline has poor circularity, can't convert"); } @@ -126,8 +128,12 @@ impl TryFrom<&LwPolyline> for Ellipse { Ok(Ellipse { x, - y: -max_y, - height: max_y - y, + y: if is_bugle_circle {-max_y-(max_x - x)/2.0} else {-max_y}, + height: if is_bugle_circle { + max_x - x + } else { + max_y - y + }, width: max_x - x, //in the original code antialias is always set to false...I'm guessing for performance diff --git a/src/qelmt/mod.rs b/src/qelmt/mod.rs index c4a41ac..6dd8292 100644 --- a/src/qelmt/mod.rs +++ b/src/qelmt/mod.rs @@ -100,6 +100,9 @@ trait Circularity { //for a specific type 0.98..=1.02 } + + // 考虑bugle圆形判断 + fn is_circular_with_bulge(&self) -> bool; } // 为 Polyline 类型实现圆形检测,有点类似函数定义 @@ -137,6 +140,10 @@ impl Circularity for Polyline { Self::match_range().contains(&t_ratio) } + + fn is_circular_with_bulge(&self) -> bool{ + false + } } // 为 LwPolyline 类型实现相同的圆形检测逻辑 @@ -165,10 +172,32 @@ impl Circularity for LwPolyline { poly_area /= 2.0; poly_area.abs() }; + let t_ratio = 4.0 * PI * poly_area / poly_perim.powf(2.0); - Self::match_range().contains(&t_ratio) } + + fn is_circular_with_bulge(&self) -> bool { + // 检查是否有显著的bulge值 + let has_bulge = self.vertices.iter().any(|v| v.bulge.abs() > 0.5); + if !has_bulge { + return false; + } + + let all_quarter_arcs = self.vertices.iter() + .all(|v| (v.bulge.abs() - 1.0).abs() < 0.05); + + if all_quarter_arcs { + // 检查是否闭合 + if self.vertices.len() >= 3 { + let first = &self.vertices[0]; + let last = &self.vertices[self.vertices.len() - 1]; + let distance = ((last.x - first.x).powf(2.0) + (last.y - first.y).powf(2.0)).sqrt(); + return distance < 0.1; // 起点终点接近 + } + } + false + } } // 实现Definition的方法