Sub Main()
    ' ESP32-WROOM-32D DevKitC V4 3D 모델 생성
    ' Autodesk Inventor 2025 호환
    
    Dim oApp As Inventor.Application
    Set oApp = ThisApplication
    
    ' 새 파트 문서 생성
    Dim oPartDoc As PartDocument
    Set oPartDoc = oApp.Documents.Add(kPartDocumentObject, , True)
    
    Dim oCompDef As PartComponentDefinition
    Set oCompDef = oPartDoc.ComponentDefinition
    
    Dim oTG As TransientGeometry
    Set oTG = oApp.TransientGeometry
    
    ' 단위: cm
    Dim cm As Double
    cm = 1
    Dim mm As Double
    mm = 0.1
    
    ' ===== 치수 정의 =====
    Dim boardLength As Double: boardLength = 5.53 * cm
    Dim boardWidth As Double: boardWidth = 2.8 * cm
    Dim boardThickness As Double: boardThickness = 1.6 * mm
    
    Dim moduleLength As Double: moduleLength = 2.55 * cm
    Dim moduleWidth As Double: moduleWidth = 1.8 * cm
    Dim moduleHeight As Double: moduleHeight = 3.1 * mm
    
    Dim shieldLength As Double: shieldLength = 1.8 * cm
    Dim shieldWidth As Double: shieldWidth = 1.6 * cm
    Dim shieldHeight As Double: shieldHeight = 2.0 * mm
    
    Dim usbWidth As Double: usbWidth = 7.5 * mm
    Dim usbLength As Double: usbLength = 5.5 * mm
    Dim usbHeight As Double: usbHeight = 2.6 * mm
    
    Dim buttonSize As Double: buttonSize = 3.0 * mm
    Dim buttonHeight As Double: buttonHeight = 1.5 * mm
    
    Dim pinLength As Double: pinLength = 1.15 * cm
    Dim pinWidth As Double: pinWidth = 0.64 * mm
    Dim pinSpacing As Double: pinSpacing = 2.54 * mm
    Dim numPinsPerSide As Integer: numPinsPerSide = 19
    
    ' ===== 1. PCB 보드 생성 =====
    Dim oSketch1 As PlanarSketch
    Set oSketch1 = oCompDef.Sketches.Add(oCompDef.WorkPlanes.Item(3)) ' XY 평면
    
    ' PCB 사각형 그리기 (oRect 변수 제거)
    Call oSketch1.SketchLines.AddAsTwoPointRectangle( _
        oTG.CreatePoint2d(0, 0), _
        oTG.CreatePoint2d(boardLength, boardWidth))
    
    ' PCB 돌출
    Dim oProfile1 As Profile
    Set oProfile1 = oSketch1.Profiles.AddForSolid
    Dim oExtrude1 As ExtrudeFeature
    Set oExtrude1 = oCompDef.Features.ExtrudeFeatures.AddByDistanceExtent( _
        oProfile1, boardThickness, kPositiveExtentDirection, kNewBodyOperation)
    
    ' PCB 색상 설정 (초록색)
    On Error Resume Next
    oExtrude1.SurfaceBodies.Item(1).Appearance = oPartDoc.AppearanceAssets.Item("Green - Matte")
    If Err.Number <> 0 Then
        oExtrude1.SurfaceBodies.Item(1).Appearance = oPartDoc.AppearanceAssets.Item("Green")
    End If
    On Error GoTo 0
    
    ' ===== 2. ESP32-WROOM-32D 모듈 =====
    Dim oSketch2 As PlanarSketch
    Dim oWorkPlane1 As WorkPlane
    Set oWorkPlane1 = oCompDef.WorkPlanes.AddByPlaneAndOffset( _
        oCompDef.WorkPlanes.Item(3), boardThickness)
    Set oSketch2 = oCompDef.Sketches.Add(oWorkPlane1)
    
    Dim moduleX As Double: moduleX = 1.5 * cm
    Dim moduleY As Double: moduleY = (boardWidth - moduleWidth) / 2
    
    Call oSketch2.SketchLines.AddAsTwoPointRectangle( _
        oTG.CreatePoint2d(moduleX, moduleY), _
        oTG.CreatePoint2d(moduleX + moduleLength, moduleY + moduleWidth))
    
    Dim oProfile2 As Profile
    Set oProfile2 = oSketch2.Profiles.AddForSolid
    Dim oExtrude2 As ExtrudeFeature
    Set oExtrude2 = oCompDef.Features.ExtrudeFeatures.AddByDistanceExtent( _
        oProfile2, moduleHeight, kPositiveExtentDirection, kJoinOperation)
    
    ' 모듈 색상 (은색)
    On Error Resume Next
    oExtrude2.SurfaceBodies.Item(1).Appearance = oPartDoc.AppearanceAssets.Item("Aluminum - Satin")
    If Err.Number <> 0 Then
        oExtrude2.SurfaceBodies.Item(1).Appearance = oPartDoc.AppearanceAssets.Item("Aluminum")
    End If
    On Error GoTo 0
    
    ' ===== 3. 금속 실드 캔 =====
    Dim oSketch3 As PlanarSketch
    Dim oWorkPlane2 As WorkPlane
    Set oWorkPlane2 = oCompDef.WorkPlanes.AddByPlaneAndOffset( _
        oWorkPlane1, moduleHeight)
    Set oSketch3 = oCompDef.Sketches.Add(oWorkPlane2)
    
    Dim shieldX As Double: shieldX = 1.75 * cm
    Dim shieldY As Double: shieldY = (boardWidth - shieldWidth) / 2
    
    Call oSketch3.SketchLines.AddAsTwoPointRectangle( _
        oTG.CreatePoint2d(shieldX, shieldY), _
        oTG.CreatePoint2d(shieldX + shieldLength, shieldY + shieldWidth))
    
    Dim oProfile3 As Profile
    Set oProfile3 = oSketch3.Profiles.AddForSolid
    Dim oExtrude3 As ExtrudeFeature
    Set oExtrude3 = oCompDef.Features.ExtrudeFeatures.AddByDistanceExtent( _
        oProfile3, shieldHeight, kPositiveExtentDirection, kJoinOperation)
    
    ' 실드 색상 (회색 금속)
    On Error Resume Next
    oExtrude3.SurfaceBodies.Item(1).Appearance = oPartDoc.AppearanceAssets.Item("Steel - Satin")
    If Err.Number <> 0 Then
        oExtrude3.SurfaceBodies.Item(1).Appearance = oPartDoc.AppearanceAssets.Item("Steel")
    End If
    On Error GoTo 0
    
    ' ===== 4. PCB 안테나 =====
    Dim oSketch4 As PlanarSketch
    Set oSketch4 = oCompDef.Sketches.Add(oWorkPlane1)
    
    Dim antennaX As Double: antennaX = 4.05 * cm
    Dim antennaY As Double: antennaY = (boardWidth - 1.2 * cm) / 2
    
    Call oSketch4.SketchLines.AddAsTwoPointRectangle( _
        oTG.CreatePoint2d(antennaX, antennaY), _
        oTG.CreatePoint2d(antennaX + 1.3 * cm, antennaY + 1.2 * cm))
    
    Dim oProfile4 As Profile
    Set oProfile4 = oSketch4.Profiles.AddForSolid
    Dim oExtrude4 As ExtrudeFeature
    Set oExtrude4 = oCompDef.Features.ExtrudeFeatures.AddByDistanceExtent( _
        oProfile4, moduleHeight, kPositiveExtentDirection, kJoinOperation)
    
    ' ===== 5. USB 커넥터 =====
    Dim oSketch5 As PlanarSketch
    Set oSketch5 = oCompDef.Sketches.Add(oWorkPlane1)
    
    Dim usbY As Double: usbY = (boardWidth - usbWidth) / 2
    
    Call oSketch5.SketchLines.AddAsTwoPointRectangle( _
        oTG.CreatePoint2d(-0.2 * mm, usbY), _
        oTG.CreatePoint2d(usbLength - 0.2 * mm, usbY + usbWidth))
    
    Dim oProfile5 As Profile
    Set oProfile5 = oSketch5.Profiles.AddForSolid
    Dim oExtrude5 As ExtrudeFeature
    Set oExtrude5 = oCompDef.Features.ExtrudeFeatures.AddByDistanceExtent( _
        oProfile5, usbHeight, kPositiveExtentDirection, kJoinOperation)
    
    ' USB 색상 (은색)
    On Error Resume Next
    oExtrude5.SurfaceBodies.Item(1).Appearance = oPartDoc.AppearanceAssets.Item("Chrome")
    If Err.Number <> 0 Then
        oExtrude5.SurfaceBodies.Item(1).Appearance = oPartDoc.AppearanceAssets.Item("Steel")
    End If
    On Error GoTo 0
    
    ' ===== 6. BOOT 버튼 =====
    Dim oSketch6 As PlanarSketch
    Set oSketch6 = oCompDef.Sketches.Add(oWorkPlane1)
    
    Call oSketch6.SketchLines.AddAsTwoPointRectangle( _
        oTG.CreatePoint2d(0.5 * cm, 0.35 * cm), _
        oTG.CreatePoint2d(0.5 * cm + buttonSize, 0.35 * cm + buttonSize))
    
    Dim oProfile6 As Profile
    Set oProfile6 = oSketch6.Profiles.AddForSolid
    Dim oExtrude6 As ExtrudeFeature
    Set oExtrude6 = oCompDef.Features.ExtrudeFeatures.AddByDistanceExtent( _
        oProfile6, buttonHeight, kPositiveExtentDirection, kJoinOperation)
    
    ' 버튼 색상 (검정)
    On Error Resume Next
    oExtrude6.SurfaceBodies.Item(1).Appearance = oPartDoc.AppearanceAssets.Item("Black")
    On Error GoTo 0
    
    ' ===== 7. EN 버튼 =====
    Dim oSketch7 As PlanarSketch
    Set oSketch7 = oCompDef.Sketches.Add(oWorkPlane1)
    
    Call oSketch7.SketchLines.AddAsTwoPointRectangle( _
        oTG.CreatePoint2d(0.5 * cm, boardWidth - 0.65 * cm), _
        oTG.CreatePoint2d(0.5 * cm + buttonSize, boardWidth - 0.65 * cm + buttonSize))
    
    Dim oProfile7 As Profile
    Set oProfile7 = oSketch7.Profiles.AddForSolid
    Dim oExtrude7 As ExtrudeFeature
    Set oExtrude7 = oCompDef.Features.ExtrudeFeatures.AddByDistanceExtent( _
        oProfile7, buttonHeight, kPositiveExtentDirection, kJoinOperation)
    
    On Error Resume Next
    oExtrude7.SurfaceBodies.Item(1).Appearance = oPartDoc.AppearanceAssets.Item("Black")
    On Error GoTo 0
    
    ' ===== 8. 왼쪽 핀 헤더 (19개) =====
    Dim i As Integer
    For i = 0 To numPinsPerSide - 1
        Dim oSketchPin1 As PlanarSketch
        Set oSketchPin1 = oCompDef.Sketches.Add(oCompDef.WorkPlanes.Item(3))
        
        Dim pinX As Double
        pinX = boardLength - 0.3 * cm - (i * pinSpacing)
        
        Call oSketchPin1.SketchLines.AddAsTwoPointRectangle( _
            oTG.CreatePoint2d(pinX, 0.254 * cm), _
            oTG.CreatePoint2d(pinX + pinWidth, 0.254 * cm + pinWidth))
        
        Dim oProfilePin1 As Profile
        Set oProfilePin1 = oSketchPin1.Profiles.AddForSolid
        Dim oExtrudePin1 As ExtrudeFeature
        Set oExtrudePin1 = oCompDef.Features.ExtrudeFeatures.AddByDistanceExtent( _
            oProfilePin1, pinLength, kNegativeExtentDirection, kJoinOperation)
        
        ' 핀 색상 (금색)
        On Error Resume Next
        oExtrudePin1.SurfaceBodies.Item(1).Appearance = oPartDoc.AppearanceAssets.Item("Gold - Polished")
        If Err.Number <> 0 Then
            oExtrudePin1.SurfaceBodies.Item(1).Appearance = oPartDoc.AppearanceAssets.Item("Brass - Polished")
        End If
        On Error GoTo 0
    Next i
    
    ' ===== 9. 오른쪽 핀 헤더 (19개) =====
    For i = 0 To numPinsPerSide - 1
        Dim oSketchPin2 As PlanarSketch
        Set oSketchPin2 = oCompDef.Sketches.Add(oCompDef.WorkPlanes.Item(3))
        
        pinX = boardLength - 0.3 * cm - (i * pinSpacing)
        
        Call oSketchPin2.SketchLines.AddAsTwoPointRectangle( _
            oTG.CreatePoint2d(pinX, boardWidth - 0.254 * cm - pinWidth), _
            oTG.CreatePoint2d(pinX + pinWidth, boardWidth - 0.254 * cm))
        
        Dim oProfilePin2 As Profile
        Set oProfilePin2 = oSketchPin2.Profiles.AddForSolid
        Dim oExtrudePin2 As ExtrudeFeature
        Set oExtrudePin2 = oCompDef.Features.ExtrudeFeatures.AddByDistanceExtent( _
            oProfilePin2, pinLength, kNegativeExtentDirection, kJoinOperation)
        
        ' 핀 색상 (금색)
        On Error Resume Next
        oExtrudePin2.SurfaceBodies.Item(1).Appearance = oPartDoc.AppearanceAssets.Item("Gold - Polished")
        If Err.Number <> 0 Then
            oExtrudePin2.SurfaceBodies.Item(1).Appearance = oPartDoc.AppearanceAssets.Item("Brass - Polished")
        End If
        On Error GoTo 0
    Next i
    
    ' ===== 10. LED =====
    Dim oSketchLED As PlanarSketch
    Set oSketchLED = oCompDef.Sketches.Add(oWorkPlane1)
    
    Dim oCircle As SketchCircle
    Set oCircle = oSketchLED.SketchCircles.AddByCenterRadius( _
        oTG.CreatePoint2d(1.0 * cm, 0.15 * cm), 1.0 * mm)
    
    Dim oProfileLED As Profile
    Set oProfileLED = oSketchLED.Profiles.AddForSolid
    Dim oExtrudeLED As ExtrudeFeature
    Set oExtrudeLED = oCompDef.Features.ExtrudeFeatures.AddByDistanceExtent( _
        oProfileLED, 1.0 * mm, kPositiveExtentDirection, kJoinOperation)
    
    ' LED 색상 (빨강)
    On Error Resume Next
    oExtrudeLED.SurfaceBodies.Item(1).Appearance = oPartDoc.AppearanceAssets.Item("Red")
    On Error GoTo 0
    
    ' 뷰 업데이트
    oApp.ActiveView.Fit
    
    MsgBox "ESP32-WROOM-32D DevKitC V4 모델이 생성되었습니다!" & vbCrLf & _
           "총 피처: PCB + 모듈 + 실드 + 안테나 + USB + 버튼 2개 + 핀 38개 + LED", vbInformation
    
End Sub