본문 바로가기
  • Welcome!
VBA SOLIDWORK

Template Program Development #1

by ToolBOX01 2025. 1. 6.
반응형

□ Develop a very simple program

  1. Opens Solidworks files in a specific folder.
  2. Enter the changeable dimension values ​​that will be displayed in Excel.
  3. Save it with a new solid file name.

□ Create an admin sheet. Name the sheet "template_data".

[Admin screen]

  • Template Model A
    Enter the folder name and file name where the template file is saved.
    Example) C:\KOREA\KO.SLDPRT
  • Dimension Name
    Enter the name of the dimension you want to change
    Example) "KOREA001@Base-Extrude@crank-knob.Part"
  • Default Value
    These are the basic dimension values.
  • Dimension Min & Dimension Max
    Dimension A is a changeable range value.
    Example)  10(min) < X < 20 (max)
  • Dimension Symbol
    The dimension name to display on the user's screen.
    Example)  Please enter the overall horizontal dimensions of the model?
  • Description
    Describes the dimension items

 

Code : Open Solidworks files in a specific folder

Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Dim swFile As String
Dim swFolderPath As String

Sub OpenSpecificSolidworksFile()
    On Error Resume Next
    '// Setting up SolidWorks application objects
    Set swApp = GetObject(, "SldWorks.Application")

    On Error GoTo 0
    If swApp Is Nothing Then
        MsgBox "Make sure SolidWorks is running.", vbCritical
        Exit Sub
    End If

    '// Specify the folder path containing the SolidWorks files
    swFolderPath = "C:\Path\To\Your\Solidworks\Files\" ' Replace with the actual path

    '// Get the file name from the user (optional)
    swFile = InputBox("Enter the file name (without extension):", "File Name")
    If swFile = "" Then Exit Sub ' Exit if no file name is entered

    '// Construct the full file path
    swFile = swFolderPath & swFile & ".sldprt" ' Assuming part files, adjust extension if needed

    '// Open the specified file
    Set swModel = swApp.OpenDoc6(swFile, 1, 0, "", "", 0) 

    On Error GoTo 0
    If swModel Is Nothing Then
        MsgBox "Failed to open the file: " & swFile, vbCritical
    Else
        MsgBox swModel.GetTitle
    End If

End Sub

 

Code :  Save as an array of dimension names

Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Dim swFeat As Feature
Dim swDim As Dimension
Dim strDimName As String
Dim dimNames() As String ' 치수 이름을 저장할 배열

Sub GetDimensionNamesToArray()
    ' SolidWorks 애플리케이션 객체 가져오기
    Set swApp = GetObject(, "SldWorks.Application")
    If swApp Is Nothing Then
        MsgBox "SolidWorks가 실행 중이지 않습니다.", vbCritical
        Exit Sub
    End If

    ' 활성 문서 가져오기
    Set swModel = swApp.ActiveDoc
    If swModel Is Nothing Then
        MsgBox "활성 문서가 없습니다.", vbCritical
        Exit Sub
    End If

    ' 치수 개수를 미리 알 수 없으므로 Dynamic Array 사용
    ReDim dimNames(0)

    ' FeatureManager 디자인 트리 루프
    For Each swFeat In swModel.FeatureManager.GetFeatures
        ' 각 피쳐의 치수 루프
        For Each swDim In swFeat.GetDimensions
            strDimName = swDim.Name
            ' 배열에 치수 이름 추가
            ReDim Preserve dimNames(UBound(dimNames) + 1)
            dimNames(UBound(dimNames)) = strDimName
        Next
    Next

    ' 배열에 저장된 치수 이름 확인 (예시)
    For i = LBound(dimNames) To UBound(dimNames)
        MsgBox dimNames(i)
    Next
End Sub

 

Code :  Save as new file name

Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Dim newFileName As String

Sub SaveAsNewName()
    ' SolidWorks 애플리케이션 객체 가져오기
    Set swApp = GetObject(, "SldWorks.Application")
    If swApp Is Nothing Then
        MsgBox "SolidWorks가 실행 중이지 않습니다.", vbCritical
        Exit Sub
    End If

    ' 활성 문서 가져오기
    Set swModel = swApp.ActiveDoc
    If swModel Is Nothing Then
        MsgBox "활성 문서가 없습니다.", vbCritical
        Exit Sub
    End If

    ' 새로운 파일 이름 설정 (경로 포함)
    newFileName = "C:\YourPath\NewFileName.sldprt"  ' 원하는 경로와 파일명으로 변경

    ' 모델 저장
    swModel.SaveAs2 newFileName, 0, 0, ""

    MsgBox "파일이 성공적으로 저장되었습니다."
End Sub