본문 바로가기
  • Welcome!
VBA, VB.NET For Creo

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

by ToolBOX01 2020. 12. 16.
반응형

This is the code to open a model with the Creo file name in the current working folder.

Option Explicit
Dim asynconn As New pfcls.CCpfcAsyncConnection
Dim conn As pfcls.IpfcAsyncConnection
Dim oBaseSession As pfcls.IpfcBaseSession
Dim oModel As pfcls.IpfcModel
Dim ws as Worksheet
Dim oModelDescriptorCreate As New CCpfcModelDescriptor
Dim oModelDescriptor As IpfcModelDescriptor
Dim oSession As IpfcSession
Dim oWindow As pfcls.IpfcWindow
Sub Newmodel()
On Error GoTo RunError

    '\\ Make an asynchronous connection with Creo
    Set conn = asynconn.Connect("", "", ".", 5)
    '\\Get the current session
    Set oBaseSession = conn.session
    '\\Show the current Working Directory
    Set ws = ThisWorkbook.Worksheets("Template")
    ws.Cells(1, "B") = vbCrLf & oBaseSession.GetCurrentDirectory
    '\\ Create a modeldescriptor to the Creo Model
    Set oModelDescriptor = oModelDescriptorCreate.CreateFromFileName(ws.Cells(1, "C")) '\\ws.Cells(1, "C") = "korea.prt"
    '\\Retrieve the model into the Model handle
    Set oModel = oBaseSession.RetrieveModel(oModelDescriptor)
    
    '\\Create a new window which displays the Creo Model
    Set oSession = oBaseSession
    Call oSession.UIShowMessageDialog(oModel.Filename, Nothing)
    
    Set oWindow = oBaseSession.GetModelWindow(Model)
    oWindow.Activate
    
    '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

 

by korealionkk@gmail.com