ข้ามไปยังเนื้อหา

บทที่ 9 Troubleshooting

Import & Connection Errors

ModuleNotFoundError: No module named 'comtypes'

สาเหตุ: ยังไม่ได้ติดตั้ง comtypes หรือใช้ผิด Python environment

แก้ไข:

Terminal window
# ตรวจว่า pip ตรงกับ Python ที่ใช้
python -m pip install comtypes
# ตรวจ environment
python -c "import sys; print(sys.executable)"

OSError: -2147221005 / CO_E_CLASSNOTREG

สาเหตุ: ETABS COM object ไม่ได้ลงทะเบียนในระบบ

แก้ไข:

  1. ตรวจว่าติดตั้ง ETABS แล้ว (ต้อง install ไม่ใช่แค่ copy)
  2. ลอง repair ETABS installation
  3. ตรวจ Registry: HKEY_CLASSES_ROOT\CSI.ETABS.API.ETABSObject

OSError: -2147221021 / MK_E_UNAVAILABLE

สาเหตุ: ETABS ไม่ได้เปิดอยู่

แก้ไข: เปิด ETABS และเปิดโมเดลก่อนรัน script

try:
etabs = comtypes.client.GetActiveObject("CSI.ETABS.API.ETABSObject")
except OSError:
print("กรุณาเปิด ETABS ก่อนรัน script")
exit(1)

Runtime Errors

AttributeError: 'NoneType' has no attribute 'FrameObj'

สาเหตุ: sap_model เป็น None เพราะไม่ได้เปิดโมเดล

แก้ไข:

sap_model = etabs.SapModel
if sap_model is None:
print("กรุณาเปิดโมเดลใน ETABS ก่อน")
exit(1)

comtypes.COMError: HRESULT -2147...

สาเหตุ: API call ล้มเหลว — อาจเป็นเพราะ:

  • ส่ง parameter ผิด type
  • เรียก Results API ก่อนรัน Analysis
  • element ชื่อที่ระบุไม่มีในโมเดล

แก้ไข:

# ตรวจ parameter type ก่อนเรียก
frame_name = str(frame_name) # ต้องเป็น string
# ตรวจว่า element มีอยู่จริง
ret, count, names = sap_model.FrameObj.GetNameList()
if frame_name not in names:
print(f"⚠️ Frame {frame_name} ไม่มีในโมเดล")

API Return Code ≠ 0

ถ้า API คืนค่า ≠ 0 ให้ตรวจ:

Return Codeความหมายวิธีแก้
0✅ สำเร็จ-
1 (non-zero)❌ ล้มเหลวตรวจ param, ตรวจ state

Debugging Tips

def check(ret, method_name: str):
"""ตรวจ return code พร้อมบอกว่าเรียกจากไหน"""
if ret != 0:
print(f"❌ {method_name} failed (ret={ret})")
# เพิ่ม breakpoint ที่นี่ถ้าใช้ debugger
raise Exception(f"{method_name} failed with code: {ret}")
return ret

ETABS ค้าง / ปิดไม่ได้

อาการสาเหตุแก้ไข
ETABS ค้างหลัง script จบไม่ได้ del COM objectsเพิ่ม del + gc.collect()
ETABS ไม่ตอบสนองInfinite loop ใน scriptKill script → Task Manager kill ETABS
เปิด ETABS ใหม่ไม่ได้COM process ค้างจากครั้งก่อนEnd Task ETABS.exe ใน Task Manager

Emergency cleanup:

Terminal window
# Kill ETABS process ใน Powershell
Get-Process -Name "ETABS" -ErrorAction SilentlyContinue | Stop-Process -Force

Python-Specific Tips

Dynamic Dispatch ไม่มี IntelliSense

ปัญหา: comtypes ใช้ dynamic dispatch → IDE ไม่ auto-complete methods

วิธีแก้:

  1. ใช้ CSi API documentation เป็น reference
  2. สร้าง type stubs (.pyi) สำหรับ methods ที่ใช้บ่อย
  3. ใช้ comment เพื่อ document return type
# Return: (ret: int, count: int, names: tuple[str, ...])
ret, count, names = sap_model.FrameObj.GetNameList()

Tuple Unpacking ผิดจำนวน

ปัญหา: ValueError: not enough values to unpack

สาเหตุ: API return จำนวน values ไม่ตรงกับที่ unpack

แก้ไข: ตรวจ API documentation ว่า method return กี่ค่า

# ❌ ผิด — ลืม return value บางตัว
count, names = sap_model.FrameObj.GetNameList()
# ✅ ถูก — ret มาก่อนเสมอ
ret, count, names = sap_model.FrameObj.GetNameList()

สำรองข้อมูลเสมอ

Debugging Checklist

เมื่อเจอปัญหา:

  • ETABS เปิดอยู่ + โมเดลเปิดอยู่?
  • ใช้ Python environment ที่มี comtypes?
  • ตรวจ return code ทุก API call?
  • ข้อมูลที่ส่งเป็น type ที่ถูกต้อง? (string vs int)
  • รัน Analysis แล้วก่อนอ่าน Results?
  • ตรวจ Analysis Log ว่าไม่มี error?
  • del COM objects ครบ + gc.collect()?