본문 바로가기
  • 환영 합니다 ! Welcome!
VBA For Creo

VB API Fundamentals -- VBA 예제 (4) File Open

by ToolBOX01 2020. 12. 16.
반응형

작업공간에서 코드에서 정의한 파일 이름 불러오기

Sub Newmodel()
    Dim asynconn As New pfcls.CCpfcAsyncConnection
    'Make an asynchronous connection with Creo
    Dim conn As pfcls.IpfcAsyncConnection: Set conn = asynconn.Connect("", "", ".", 5)
On Error GoTo RunError
    'Get the current session
    Dim oBaseSession As pfcls.IpfcBaseSession: Set oBaseSession = conn.session
    Dim oModel As pfcls.IpfcModel
   
    'Show the current Working Directory
    Cells(1, 1) = vbCrLf & oBaseSession.GetCurrentDirectory
    'Create a modeldescriptor to the Creo Model
    Dim oModelDescriptorCreate As New CCpfcModelDescriptor
    Dim oModelDescriptor As IpfcModelDescriptor
    Set oModelDescriptor = oModelDescriptorCreate.CreateFromFileName("korea01.prt")
     
    'Retrieve the model into the Model handle
    Set oModel = oBaseSession.RetrieveModel(oModelDescriptor)
    
    'Create a new window which displays the Creo Model
    Dim oWindow As pfcls.IpfcWindow: Set oWindow = oBaseSession.OpenFile(oModelDescriptor)
    oWindow.Activate

    'Massege Info Window
    Dim oSession As IpfcSession: Set oSession = oBaseSession
    Call oSession.UIShowMessageDialog(oModel.Filename, Nothing)
    
    'Disconnect with Creo
    conn.Disconnect (2)

    'Cleanup
    Set asynconn = Nothing
    Set conn = Nothing
    Set oBaseSession = Nothing
    Set oSession = Nothing
    Set oModel = Nothing
    

RunError:

    If Err.Number <> 0 Then
        MsgBox "Process Failed : Unknown error occurred." + Chr(13) + _
                "Error No: " + CStr(Err.Number) + Chr(13) + _
                "Error: " + Err.Description, vbCritical, "Error"

        If Not conn Is Nothing Then
            If conn.IsRunning Then
                conn.Disconnect (2)
            End If
        End If
    End If

End Sub

 Set oModelDescriptor = oModelDescriptorCreate.CreateFromFileName("korea01.prt")
""korea01.prt"는 사용자 PC에 저장된 파일 이름 입니다.

Call oSession.UIShowMessageDialog(oModel.Filename, Nothing)
화면에 불러온 파일 이름이 표시 됩니다.