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

엑셀에서 Parameter 값 입력 -> Creo 변경

by ToolBOX01 2022. 10. 23.
반응형

모델에 String 타입의 매개변수 값을 입력 합니다.

 

■ 소스 코드

Parameter는 IpfcParameterOwner 부터  시작 하여 코드를 작성 합니다. IpfcBaseParameters는 Parameter와 Dimension 값을 가져옵니다.

Option Explicit
Sub part_parameter()
    
  On Error GoTo RunError
    Dim asynconn As New pfcls.CCpfcAsyncConnection
    Dim conn As pfcls.IpfcAsyncConnection
    Dim oBaseSession As pfcls.IpfcBaseSession
    Dim oModel As pfcls.IpfcModel
     
    Set conn = asynconn.Connect("", "", ".", 5)
    Set oBaseSession = conn.session
    Set oModel = oBaseSession.CurrentModel
   
    '엑셀파일에 있는 Parameter List Count
    Dim oColumnscount As Long: oColumnscount = Cells(6, Columns.Count).End(xlToLeft).Column
    
    'Parameter
    Dim oParameterOwner As IpfcParameterOwner
    Dim oBaseParameter As IpfcBaseParameter
    Dim oParamValue As IpfcParamValue
    Dim oParameter As IpfcParameter
    Dim oCMModelItem As New CMpfcModelItem

    Dim oModelItem As IpfcModelItem
    Dim i As Long
    Dim oCellsParameterName As String
      
    
    For i = 0 To oColumnscount - 1
    
            Set oParameterOwner = oModel
            oCellsParameterName = Cells(6, i + 1).Value
            Set oParameter = oParameterOwner.GetParam(oCellsParameterName)
            Set oBaseParameter = oParameter
            
            Set oParamValue = oCMModelItem.CreateStringParamValue(Cells(7, i + 1))
            oBaseParameter.Value = oParamValue
    
    Next i

    oModel.save

'Disconnect with Creo
    conn.Disconnect (2)
    
Exit Sub
    
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

 

part parameter v01.xlsm
0.02MB


모델에 Parameter를 만들고, 엑셀에 입력한 값을 모델에 저장 합니다.

 

■ 소스 코드

Sub CreateParameter()

     On Error GoTo RunError
        Dim asynconn As New pfcls.CCpfcAsyncConnection
        Dim conn As pfcls.IpfcAsyncConnection
        Dim oBaseSession As pfcls.IpfcBaseSession
        Dim oModel As pfcls.IpfcModel
         
        Set conn = asynconn.Connect("", "", ".", 5)
        Set oBaseSession = conn.session
        Set oModel = oBaseSession.CurrentModel
        
        
        Dim oBaseParameter As IpfcBaseParameter
        Dim oParameterOwner As IpfcParameterOwner
        Dim oParameter As IpfcParameter
        Dim oParamValue As IpfcParamValue
        Dim oCMModelItem As New CMpfcModelItem

        
        
                
        Set oParameterOwner = oModel
        'Parameter Create

        Set oParamValue = oCMModelItem.CreateStringParamValue(Cells(5, "I"))
        Set oBaseParameter = oParameterOwner.CreateParam(Cells(4, "I"), oParamValue)
    
    
'Disconnect with Creo
    conn.Disconnect (2)
    
    
Exit Sub
    
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

 

[ Parameter 및 값 변경 ]