schillerCFK commited on
Commit
afe3dd1
·
verified ·
1 Parent(s): 40b3c76

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -14
app.py CHANGED
@@ -130,10 +130,71 @@ def calc_offset_from_angle(distance, angle, ang_type):
130
  fig = draw_coaxiality_plot(distance, r)
131
  return text_result, fig
132
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
  # --- 2. Gradio 介面設計 ---
134
  init_ws = load_rzf_config()
135
 
136
- # 在 Gradio 6.0 中,title 留在 Blocks,theme 移到 launch
137
  with gr.Blocks(title=COMPANY_NAME) as demo:
138
  ws_state = gr.State(init_ws)
139
  gr.Markdown(f"# 🛰️ {COMPANY_NAME} 專業系統")
@@ -158,7 +219,7 @@ with gr.Blocks(title=COMPANY_NAME) as demo:
158
  submit_btn = gr.Button("✅ 提交 RZF 訂單", variant="primary")
159
  order_status = gr.Textbox(label="通知", interactive=False)
160
 
161
- with gr.Tab("📐 同軸度計算器"):
162
  gr.Markdown("## 📐 雷射同軸度與 mRad 雙向換算")
163
 
164
  with gr.Tabs():
@@ -184,15 +245,37 @@ with gr.Blocks(title=COMPANY_NAME) as demo:
184
  with gr.Column():
185
  calc_output_plot_2 = gr.Plot(label="Coaxiality Plot")
186
 
187
- gr.Markdown("""
188
- **mRad (Milliradian)** 物理意義:
189
- 在 1 公尺 (1000mm) 的距離下,若半徑偏移 1mm,則角度為 1 mRad。
190
- 公式:$$\\theta_{(mRad)} = \\frac{r}{L} \\times 1000$$
191
- *(註:此處的 r 為半徑偏移量)*
192
- """)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
193
 
194
  with gr.Tab("📖 規格說明"):
195
- gr.Markdown("### 📘 術語定義\n- **mRad**: 毫弧度,雷射發散角與同軸度常用的專業單位。")
196
 
197
  with gr.Tab("📋 訂單報表"):
198
  refresh_table_btn = gr.Button("🔄 重新整理")
@@ -205,13 +288,18 @@ with gr.Blocks(title=COMPANY_NAME) as demo:
205
  file_output = gr.File(label="下載資料", file_count="multiple")
206
 
207
  # --- 邏輯綁定 ---
208
- calc_btn_1.click(calc_angle_from_offset, [dist_input_1, off_input, off_type], [calc_output_text_1, calc_output_plot_1])
209
- calc_btn_2.click(calc_offset_from_angle, [dist_input_2, angle_input, ang_type], [calc_output_text_2, calc_output_plot_2])
210
  submit_btn.click(submit_rzf_order, [w_select, p_slider, s_radio, q_input, focus_in, spot_in, coaxial_in, ellipse_in, dim_d, dim_l, wire_m, wire_l, wire_c, connector], order_status)
211
  refresh_table_btn.click(get_order_data, outputs=order_table)
212
  add_w_btn.click(lambda n, c: (gr.update(choices=c+[n], value=n), c+[n], f"已新增: {n}"), [new_w_input, ws_state], [w_select, ws_state, order_status])
213
  download_btn.click(get_backup_files, outputs=file_output)
 
 
 
 
 
 
 
214
 
215
- # theme 放在這裡,完美符合 Gradio 6.0 規範
216
- # server_name="0.0.0.0" 允許外部網路連線,share=True 產生公開隨機連結
217
- demo.launch(theme=gr.themes.Soft(), server_name="0.0.0.0", share=True)
 
130
  fig = draw_coaxiality_plot(distance, r)
131
  return text_result, fig
132
 
133
+ # --- 光束發散 (Beam Divergence) 邏輯 ---
134
+
135
+ def calculate_and_plot_divergence(z1, d1, z2, d2, size_type):
136
+ if z1 >= z2: return "❌ 錯誤:第二個位置 (z2) 必須大於第一個位置 (z1)", None
137
+ if d1 < 0 or d2 < 0: return "❌ 錯誤:光斑尺寸不能為負數", None
138
+
139
+ delta_L = z2 - z1
140
+
141
+ # 轉換為半徑 (作為直角三角形的高度)
142
+ r1 = d1 if size_type == "半徑 (Radius)" else d1 / 2.0
143
+ r2 = d2 if size_type == "半徑 (Radius)" else d2 / 2.0
144
+
145
+ delta_r = r2 - r1
146
+
147
+ # 計算半角 (Half-Angle)
148
+ half_rad = math.atan(delta_r / delta_L)
149
+ half_deg = math.degrees(half_rad)
150
+ half_mrad = (delta_r / delta_L) * 1000
151
+
152
+ # 計算全角 (Full-Angle)
153
+ full_rad = half_rad * 2
154
+ full_deg = half_deg * 2
155
+ full_mrad = half_mrad * 2
156
+
157
+ # 結果文字
158
+ text_result = f"### 💡 光束發散計算結果:\n" \
159
+ f"- 測量距離差 (${\Delta L}$): **{delta_L:.2f} mm**\n" \
160
+ f"- 半徑尺寸差 (${\Delta r}$): **{delta_r:.4f} mm**\n\n" \
161
+ f"#### 📐 角度結果\n" \
162
+ f"- **半角發散 (Half-Angle, ${\\theta/2}$)**: **{half_deg:.4f}°** ({half_mrad:.4f} mRad)\n" \
163
+ f"- **全角發散 (Full-Angle, ${\\theta}$)**: **{full_deg:.4f}°** ({full_mrad:.4f} mRad)"
164
+
165
+ # 繪製圖表
166
+ fig, ax = plt.subplots(figsize=(6, 4), dpi=100)
167
+ ax.set_title(f"{COMPANY_NAME} - Beam Divergence", fontsize=12)
168
+
169
+ # 繪製光束形狀 (多邊形)
170
+ beam_coords = [[z1, r1], [z2, r2], [z2, -r2], [z1, -r1]]
171
+ poly = plt.Polygon(beam_coords, closed=True, facecolor='#FFF9C4', edgecolor='#FBC02D', linewidth=2, alpha=0.6)
172
+ ax.add_patch(poly)
173
+
174
+ # 畫中心線
175
+ ax.plot([z1 - delta_L*0.1, z2 + delta_L*0.1], [0, 0], color='#9E9E9E', linestyle='-.', linewidth=1)
176
+
177
+ # 標註參數
178
+ ax.plot([z1, z1], [-r1, r1], color='#1976D2', linewidth=2)
179
+ ax.plot([z2, z2], [-r2, r2], color='#1976D2', linewidth=2)
180
+ ax.text(z1, r1 * 1.1 + 0.1, f"size 1: {d1}", ha='center', color='#1976D2', fontsize=9)
181
+ ax.text(z2, r2 * 1.1 + 0.1, f"size 2: {d2}", ha='center', color='#1976D2', fontsize=9)
182
+
183
+ # 標註角度 (顯示全角)
184
+ ax.text(z2 * 1.02, r2 / 2, f"Θ ≈ {full_deg:.4f}°", color='#D32F2F', fontweight='bold', fontsize=11)
185
+
186
+ # 調整圖表邊界
187
+ ax.set_xlim(z1 - delta_L*0.2, z2 + delta_L*0.3)
188
+ ax.set_ylim(-max(r1, r2)*1.5 - 0.5, max(r1, r2)*1.5 + 0.5)
189
+ ax.axis('off')
190
+
191
+ plt.tight_layout()
192
+ return text_result, fig
193
+
194
+
195
  # --- 2. Gradio 介面設計 ---
196
  init_ws = load_rzf_config()
197
 
 
198
  with gr.Blocks(title=COMPANY_NAME) as demo:
199
  ws_state = gr.State(init_ws)
200
  gr.Markdown(f"# 🛰️ {COMPANY_NAME} 專業系統")
 
219
  submit_btn = gr.Button("✅ 提交 RZF 訂單", variant="primary")
220
  order_status = gr.Textbox(label="通知", interactive=False)
221
 
222
+ with gr.Tab("📐 同軸度計算"):
223
  gr.Markdown("## 📐 雷射同軸度與 mRad 雙向換算")
224
 
225
  with gr.Tabs():
 
245
  with gr.Column():
246
  calc_output_plot_2 = gr.Plot(label="Coaxiality Plot")
247
 
248
+ # 新增:光束發散分頁
249
+ with gr.Tab("🔦 光束發散計算"):
250
+ gr.Markdown("## 🔦 測量兩點計算光束發散角 (Beam Divergence)")
251
+ with gr.Row():
252
+ with gr.Column():
253
+ gr.Markdown("### 輸入測量數據")
254
+ size_type_div = gr.Radio(["直徑 (Diameter)", "半徑 (Radius)"], label="光斑尺寸類型", value="直徑 (Diameter)")
255
+
256
+ with gr.Row():
257
+ z1_in = gr.Number(label="位置 1 距離 (z1) mm", value=100)
258
+ d1_in = gr.Number(label="位置 1 尺寸 (d1) mm", value=2.0)
259
+
260
+ with gr.Row():
261
+ z2_in = gr.Number(label="位置 2 距離 (z2) mm", value=1000)
262
+ d2_in = gr.Number(label="位置 2 尺寸 (d2) mm", value=4.0)
263
+
264
+ div_calc_btn = gr.Button("開始計算發散角", variant="primary")
265
+ div_out_text = gr.Markdown("等待輸入...")
266
+
267
+ with gr.Column():
268
+ div_out_plot = gr.Plot(label="Beam Divergence Plot")
269
+ gr.Markdown("""
270
+ **計算原理說明**:
271
+ 1. 測量距離差:${\Delta L = z_2 - z_1}$
272
+ 2. 光束半徑差:${\Delta r = r_2 - r_1}$ (直角三角形高度)
273
+ 3. **半角發散**:$\\theta_{half} = \\arctan(\\frac{\\Delta r}{\\Delta L})$
274
+ 4. **全角發散** (業界標準):$\\theta_{full} = 2 \\times \\theta_{half}$
275
+ """)
276
 
277
  with gr.Tab("📖 規格說明"):
278
+ gr.Markdown("### 📘 術語定義\n- **mRad**: 毫弧度,雷射發散角與同軸度常用的專業單位。\n- **發散角**: 光束隨距離擴散的程度,通常以全角 (Full-Angle) 標示。")
279
 
280
  with gr.Tab("📋 訂單報表"):
281
  refresh_table_btn = gr.Button("🔄 重新整理")
 
288
  file_output = gr.File(label="下載資料", file_count="multiple")
289
 
290
  # --- 邏輯綁定 ---
291
+ # 訂單與系統
 
292
  submit_btn.click(submit_rzf_order, [w_select, p_slider, s_radio, q_input, focus_in, spot_in, coaxial_in, ellipse_in, dim_d, dim_l, wire_m, wire_l, wire_c, connector], order_status)
293
  refresh_table_btn.click(get_order_data, outputs=order_table)
294
  add_w_btn.click(lambda n, c: (gr.update(choices=c+[n], value=n), c+[n], f"已新增: {n}"), [new_w_input, ws_state], [w_select, ws_state, order_status])
295
  download_btn.click(get_backup_files, outputs=file_output)
296
+
297
+ # 同軸度計算
298
+ calc_btn_1.click(calc_angle_from_offset, [dist_input_1, off_input, off_type], [calc_output_text_1, calc_output_plot_1])
299
+ calc_btn_2.click(calc_offset_from_angle, [dist_input_2, angle_input, ang_type], [calc_output_text_2, calc_output_plot_2])
300
+
301
+ # 光束發散計算
302
+ div_calc_btn.click(calculate_and_plot_divergence, [z1_in, d1_in, z2_in, d2_in, size_type_div], [div_out_text, div_out_plot])
303
 
304
+ # 部署在 Hugging Face Spaces 時不需加 share=True
305
+ demo.launch(theme=gr.themes.Soft())