본문 바로가기
  • Welcome!
VBA For Creo

connection to Creo

by ToolBOX01 2024. 9. 3.
반응형

The script establishes a connection to Creo, accesses the current model, and checks if there is an active model loaded in the session.

▷Code

Option Explicit
Public asynconn As New pfcls.CCpfcAsyncConnection
Public conn As pfcls.IpfcAsyncConnection
Public BaseSession As pfcls.IpfcBaseSession
Public model As pfcls.IpfcModel

Public Sub CreoConnt01()
     '// connect creo model
     Set conn = asynconn.Connect("", "", ".", 5)
     Set BaseSession = conn.Session
     Set model = BaseSession.CurrentModel
     
    '// creo model connection check
     If model Is Nothing Then
        MsgBox "There are No Active Creo Models", vbInformation, "korealionkk@gmail.com"
        Exit Sub
     End If
          
End Sub

1. Variable Declarations

Option Explicit
Public conn As pfcls.IpfcAsyncConnection
Public BaseSession As pfcls.IpfcBaseSession
Public model As pfcls.IpfcModel
Public solid As IpfcSolid
  • Option Explicit: This statement forces the declaration of all variables before they are used, which helps prevent errors due to typos or undeclared variables.
  • Public conn As pfcls.IpfcAsyncConnection: Declares a public variable conn of type IpfcAsyncConnection from the pfcls library. This is used to handle the asynchronous connection to Creo.
  • Public BaseSession As pfcls.IpfcBaseSession: Declares a public variable BaseSession of type IpfcBaseSession. This represents the session object in Creo, which manages the current working environment.
  • Public model As pfcls.IpfcModel: Declares a public variable model of type IpfcModel, representing a generic model in Creo.
  • Public solid As IpfcSolid: Declares a public variable solid of type IpfcSolid. This represents a solid model in Creo (though the type IpfcSolid should also be prefixed with pfcls, which seems to be a minor inconsistency in the code).

 

2. Subroutine Definition

Public Sub CreoConnt01()

This defines a public subroutine named CreoConnt01. A subroutine (Sub) is a block of code that performs actions but does not return a value.

 

3. Connecting to Creo

'// connect creo model
Dim asynconn As New pfcls.CCpfcAsyncConnection   
    
Set conn = asynconn.Connect("", "", ".", 5)
Set BaseSession = conn.Session
Set model = BaseSession.CurrentModel
Set solid = model
  • Dim asynconn As New pfcls.CCpfcAsyncConnection: Declares and initializes a local variable asynconn as a new instance of CCpfcAsyncConnection. This is a COM object provided by the pfcls library, used to establish a connection to Creo.
  • Set conn = asynconn.Connect("", "", ".", 5): Uses the Connect method of asynconn to establish an asynchronous connection to a running instance of Creo. 
    The parameters ("", "", ".", 5) are used to define the connection properties (e.g., startup directory, initialization settings).

               1) First three parameters: Empty strings ("") are placeholders that could be used to specify command line
                   options or a startup script.
               2) ".": Represents the current working directory.
               3) 5: The timeout value (in seconds) for the connection attempt.

  • Set BaseSession = conn.Session: Assigns the current Creo session to the BaseSession variable, allowing access to the active session and its properties.
  • Set model = BaseSession.CurrentModel: Retrieves the currently active model in the session and assigns it to the model variable.
  • Set solid = model: Assigns the active model to the solid variable. This line assumes that the current model is a solid model (it works as long as model is a type that can be assigned to solid).

 

4. Checking the Connection

    '// creo model connection check
    If model Is Nothing Then
        MsgBox "There are No Active Creo Models", vbInformation, "korealionkk@gmail.com"
        Exit Sub
    End If

 

  • If model Is Nothing Then: Checks if the model variable is Nothing, which means there is no active model in Creo.
  • MsgBox "There are No Active Creo Models", vbInformation, "korealionkk@gmail.com": If there is no active model, it shows a message box with the text "There are No Active Creo Models" and the title as an email address (korealionkk@gmail.com).
  • Exit Sub: Exits the subroutine if there is no active model, preventing further execution of the script.

 

5. Optional Model Information 

    '// Current Model Information
    '// MsgBox model.Filename , vbInformation, "korealionkk@gmail.com"

 

  • MsgBox model.Filename , vbInformation, "korealionkk@gmail.com": This line is commented out. If uncommented, it would display a message box showing the filename of the current model in Creo. The message box would have an information icon and the title would be the same email address.

 

CreoVBAStart.bas
0.00MB

 

This VBA script connects to PTC Creo, retrieves the currently active model, checks if a model is loaded, and optionally displays the model’s filename. It's useful for automating tasks in Creo by interacting with its API through VBA.