Update README.md
Browse files
README.md
CHANGED
|
@@ -10,131 +10,207 @@ msp = doc.modelspace()
|
|
| 10 |
|
| 11 |
fig, ax = plt.subplots(figsize=(8, 8))
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
def plot_poly(points, closed, lw=0.6):
|
| 21 |
xs, ys = zip(*points)
|
| 22 |
ax.plot(xs, ys, linewidth=lw, color="black")
|
| 23 |
if closed and (points[0] != points[-1]):
|
| 24 |
ax.plot([points[-1][0], points[0][0]],
|
| 25 |
[points[-1][1], points[0][1]], linewidth=lw, color="black")
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
for
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
ax.set_aspect("equal")
|
| 140 |
ax.axis("off")
|
|
|
|
| 10 |
|
| 11 |
fig, ax = plt.subplots(figsize=(8, 8))
|
| 12 |
|
| 13 |
+
# ---------------------------
|
| 14 |
+
# 공통 유틸
|
| 15 |
+
# ---------------------------
|
| 16 |
+
def plot_segments(points, closed=False, lw=0.6):
|
| 17 |
+
"""단순 선분 연결(이미 호로 근사된 점열이라는 가정)."""
|
| 18 |
+
if len(points) < 2:
|
| 19 |
+
return
|
|
|
|
| 20 |
xs, ys = zip(*points)
|
| 21 |
ax.plot(xs, ys, linewidth=lw, color="black")
|
| 22 |
if closed and (points[0] != points[-1]):
|
| 23 |
ax.plot([points[-1][0], points[0][0]],
|
| 24 |
[points[-1][1], points[0][1]], linewidth=lw, color="black")
|
| 25 |
|
| 26 |
+
def plot_lwpolyline(e):
|
| 27 |
+
"""LWPOLYLINE의 bulge를 포함해 호를 근사하여 그립니다."""
|
| 28 |
+
# get_points("xyb") → (x, y, bulge) 튜플
|
| 29 |
+
pts = list(e.get_points("xyb"))
|
| 30 |
+
if not pts:
|
| 31 |
+
return
|
| 32 |
+
approx = []
|
| 33 |
+
for i in range(len(pts) - 1 + int(e.closed)):
|
| 34 |
+
x1, y1, b1 = pts[i % len(pts)]
|
| 35 |
+
x2, y2, _ = pts[(i+1) % len(pts)]
|
| 36 |
+
p1 = np.array([x1, y1])
|
| 37 |
+
p2 = np.array([x2, y2])
|
| 38 |
+
if abs(b1) < 1e-12:
|
| 39 |
+
# 직선 세그먼트
|
| 40 |
+
approx += [tuple(p1), tuple(p2)]
|
| 41 |
+
else:
|
| 42 |
+
# bulge → 호 근사
|
| 43 |
+
# bulge = tan(delta/4), delta: 중심각
|
| 44 |
+
delta = 4 * math.atan(b1)
|
| 45 |
+
chord = p2 - p1
|
| 46 |
+
L = np.linalg.norm(chord)
|
| 47 |
+
if L < 1e-12:
|
| 48 |
+
continue
|
| 49 |
+
# 호 반지름
|
| 50 |
+
R = (L/2) / abs(math.sin(delta/2))
|
| 51 |
+
# chord 중점
|
| 52 |
+
mid = (p1 + p2) / 2
|
| 53 |
+
# chord 법선 단위벡터
|
| 54 |
+
n = np.array([-(chord[1]), chord[0]]) / (L + 1e-12)
|
| 55 |
+
# 중점에서 원 중심까지 거리
|
| 56 |
+
h = R * math.cos(delta/2)
|
| 57 |
+
# bulge의 부호로 중심 방향 결정
|
| 58 |
+
center = mid + np.sign(b1) * h * n
|
| 59 |
+
# p1, p2 각도
|
| 60 |
+
a1 = math.atan2(p1[1]-center[1], p1[0]-center[0])
|
| 61 |
+
a2 = math.atan2(p2[1]-center[1], p2[0]-center[0])
|
| 62 |
+
# 진행 방향: bulge 부호에 따라 시계/반시계
|
| 63 |
+
def angle_range(a_start, a_end, ccw=True, steps=32):
|
| 64 |
+
if ccw:
|
| 65 |
+
if a_end <= a_start:
|
| 66 |
+
a_end += 2*math.pi
|
| 67 |
+
return np.linspace(a_start, a_end, steps)
|
| 68 |
+
else:
|
| 69 |
+
if a_end >= a_start:
|
| 70 |
+
a_end -= 2*math.pi
|
| 71 |
+
return np.linspace(a_start, a_end, steps)
|
| 72 |
+
ccw = (b1 > 0) # bulge>0이면 반시계
|
| 73 |
+
angles = angle_range(a1, a2, ccw=ccw, steps=max(16, int(abs(delta)*16)))
|
| 74 |
+
for t in angles:
|
| 75 |
+
approx.append((center[0] + R*math.cos(t), center[1] + R*math.sin(t)))
|
| 76 |
+
# 중복점 정리
|
| 77 |
+
cleaned = []
|
| 78 |
+
for p in approx:
|
| 79 |
+
if not cleaned or (abs(cleaned[-1][0]-p[0])>1e-9 or abs(cleaned[-1][1]-p[1])>1e-9):
|
| 80 |
+
cleaned.append(p)
|
| 81 |
+
plot_segments(cleaned, closed=e.closed, lw=0.6)
|
| 82 |
+
|
| 83 |
+
def draw_basic_entity(ent):
|
| 84 |
+
"""INSERT로 풀린 가상 엔티티 포함, 개별 엔티티를 현재 축에 그림."""
|
| 85 |
+
t = ent.dxftype()
|
| 86 |
+
|
| 87 |
+
if t == "LINE":
|
| 88 |
+
x = [ent.dxf.start.x, ent.dxf.end.x]
|
| 89 |
+
y = [ent.dxf.start.y, ent.dxf.end.y]
|
| 90 |
+
ax.plot(x, y, linewidth=0.6, color="black")
|
| 91 |
+
|
| 92 |
+
elif t == "LWPOLYLINE":
|
| 93 |
+
# bulge 지원
|
| 94 |
+
plot_lwpolyline(ent)
|
| 95 |
+
|
| 96 |
+
elif t == "POLYLINE":
|
| 97 |
+
pts = [(v.dxf.location.x, v.dxf.location.y) for v in ent.vertices]
|
| 98 |
+
plot_segments(pts, closed=getattr(ent, "is_closed", False), lw=0.6)
|
| 99 |
+
|
| 100 |
+
elif t == "ARC":
|
| 101 |
+
c = ent.dxf.center
|
| 102 |
+
r = ent.dxf.radius
|
| 103 |
+
arc = Arc((c.x, c.y), width=2*r, height=2*r, angle=0,
|
| 104 |
+
theta1=ent.dxf.start_angle, theta2=ent.dxf.end_angle,
|
| 105 |
+
linewidth=0.6, color="black")
|
| 106 |
+
ax.add_patch(arc)
|
| 107 |
+
|
| 108 |
+
elif t == "CIRCLE":
|
| 109 |
+
c = ent.dxf.center
|
| 110 |
+
r = ent.dxf.radius
|
| 111 |
+
circle = plt.Circle((c.x, c.y), r, fill=False, linewidth=0.6, color="black")
|
| 112 |
+
ax.add_patch(circle)
|
| 113 |
+
|
| 114 |
+
elif t == "ELLIPSE":
|
| 115 |
+
center = np.array([ent.dxf.center.x, ent.dxf.center.y])
|
| 116 |
+
major = np.array([ent.dxf.major_axis.x, ent.dxf.major_axis.y])
|
| 117 |
+
ratio = ent.dxf.ratio
|
| 118 |
+
t0 = ent.dxf.start_param
|
| 119 |
+
t1 = ent.dxf.end_param
|
| 120 |
+
u = major
|
| 121 |
+
v = np.array([-major[1], major[0]])
|
| 122 |
+
v = v / (np.linalg.norm(v) + 1e-12) * (np.linalg.norm(major) * ratio)
|
| 123 |
+
ts = np.linspace(t0, t1, 200)
|
| 124 |
+
xs = center[0] + u[0]*np.cos(ts) + v[0]*np.sin(ts)
|
| 125 |
+
ys = center[1] + u[1]*np.cos(ts) + v[1]*np.sin(ts)
|
| 126 |
+
ax.plot(xs, ys, linewidth=0.6, color="black")
|
| 127 |
+
|
| 128 |
+
elif t == "SPLINE":
|
| 129 |
+
pts = ent.approximate(segments=200)
|
| 130 |
+
xs, ys = zip(*[(p[0], p[1]) for p in pts])
|
| 131 |
+
ax.plot(xs, ys, linewidth=0.6, color="black")
|
| 132 |
+
|
| 133 |
+
elif t == "TEXT":
|
| 134 |
+
ins = ent.dxf.insert
|
| 135 |
+
text = ent.dxf.text
|
| 136 |
+
height = ent.dxf.height if ent.dxf.height else 2.5
|
| 137 |
+
rot = ent.dxf.rotation if ent.dxf.hasattr("rotation") else 0.0
|
| 138 |
+
ax.text(ins.x, ins.y, text, fontsize=height, rotation=rot,
|
| 139 |
+
rotation_mode="anchor", ha="left", va="baseline", color="black")
|
| 140 |
+
|
| 141 |
+
elif t in ("MTEXT", "ATTRIB"):
|
| 142 |
+
ins = ent.dxf.insert
|
| 143 |
+
text = ent.plain_text() if t == "MTEXT" else ent.dxf.text
|
| 144 |
+
rot = ent.dxf.rotation if ent.dxf.hasattr("rotation") else 0.0
|
| 145 |
+
char_height = getattr(ent.dxf, "char_height", None) or getattr(ent.dxf, "height", None) or 2.5
|
| 146 |
+
ax.text(ins.x, ins.y, text, fontsize=char_height, rotation=rot,
|
| 147 |
+
rotation_mode="anchor", ha="left", va="top", color="black")
|
| 148 |
+
|
| 149 |
+
elif t == "HATCH":
|
| 150 |
+
for path in ent.paths:
|
| 151 |
+
if path.PATH_TYPE_EDGE:
|
| 152 |
+
pts = []
|
| 153 |
+
for edge in path.edges:
|
| 154 |
+
typ = edge.EDGE_TYPE
|
| 155 |
+
if typ == "LineEdge":
|
| 156 |
+
pts += [(edge.start[0], edge.start[1]), (edge.end[0], edge.end[1])]
|
| 157 |
+
elif typ == "ArcEdge":
|
| 158 |
+
cx, cy = edge.center
|
| 159 |
+
r = edge.radius
|
| 160 |
+
a0 = math.radians(edge.start_angle)
|
| 161 |
+
a1 = math.radians(edge.end_angle)
|
| 162 |
+
ts = np.linspace(a0, a1, 50)
|
| 163 |
+
pts += [(cx + r*np.cos(t), cy + r*np.sin(t)) for t in ts]
|
| 164 |
+
elif typ == "EllipseEdge":
|
| 165 |
+
(cx, cy) = edge.center
|
| 166 |
+
major = np.array(edge.major_axis)
|
| 167 |
+
ratio = edge.ratio
|
| 168 |
+
t0, t1 = edge.start_param, edge.end_param
|
| 169 |
+
u = major
|
| 170 |
+
v = np.array([-major[1], major[0]])
|
| 171 |
+
v = v / (np.linalg.norm(v) + 1e-12) * (np.linalg.norm(major) * ratio)
|
| 172 |
+
ts = np.linspace(t0, t1, 100)
|
| 173 |
+
pts += [(cx + u[0]*np.cos(t) + v[0]*np.sin(t),
|
| 174 |
+
cy + u[1]*np.cos(t) + v[1]*np.sin(t)) for t in ts]
|
| 175 |
+
elif typ == "SplineEdge":
|
| 176 |
+
ap = edge.spline.approximate(segments=100)
|
| 177 |
+
pts += [(p[0], p[1]) for p in ap]
|
| 178 |
+
if len(pts) >= 2:
|
| 179 |
+
xs, ys = zip(*pts)
|
| 180 |
+
ax.plot(xs, ys, linewidth=0.4, color="black")
|
| 181 |
+
elif path.PATH_TYPE_POLYLINE:
|
| 182 |
+
pts = [(v[0], v[1]) for v in path.vertices]
|
| 183 |
+
plot_segments(pts, lw=0.4)
|
| 184 |
+
|
| 185 |
+
# ---------------------------
|
| 186 |
+
# 1) 모델공간 기본 엔티티
|
| 187 |
+
# ---------------------------
|
| 188 |
+
for e in msp:
|
| 189 |
+
# 블록 참조(INSERT)는 아래에서 따로 처리
|
| 190 |
+
if e.dxftype() == "INSERT":
|
| 191 |
+
continue
|
| 192 |
+
draw_basic_entity(e)
|
| 193 |
+
|
| 194 |
+
# ---------------------------
|
| 195 |
+
# 2) 블록(INSERT) 전개
|
| 196 |
+
# ---------------------------
|
| 197 |
+
for br in msp.query("INSERT"):
|
| 198 |
+
# MINSERT 포함해 배열/스케일/회전/이동이 적용된 가상 엔티티로 확장
|
| 199 |
+
try:
|
| 200 |
+
for ve in br.virtual_entities():
|
| 201 |
+
draw_basic_entity(ve)
|
| 202 |
+
except Exception:
|
| 203 |
+
# 누락된 블록 정의 등 오류는 넘어감
|
| 204 |
+
continue
|
| 205 |
+
|
| 206 |
+
# (선택) DIMENSION은 virtual_entities()를 쓰려면 보통 render() 필요
|
| 207 |
+
for dim in msp.query("DIMENSION"):
|
| 208 |
+
try:
|
| 209 |
+
dim.render() # 치수선→선/텍스트 등으로 실체화 (레거시 DIM에만 필요)
|
| 210 |
+
for ve in dim.virtual_entities():
|
| 211 |
+
draw_basic_entity(ve)
|
| 212 |
+
except Exception:
|
| 213 |
+
continue
|
| 214 |
|
| 215 |
ax.set_aspect("equal")
|
| 216 |
ax.axis("off")
|