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

엑셀의 Parameter 값 -> CREO 모델에 Parameter 값으로 입력

by ToolBOX01 2022. 9. 5.
반응형

■ 개발 방향

"새로고침" 버튼을 클릭하면 현재 활성화된 모델의 매개변수 이름, 타입, 값을 엑셀 파일로 불러 옵니다.

엑셀에서 매개변수 값을 변경하고 "저장" 버튼을 클릭하면, 해당 하는 매개변수 값을 모델에 저장 합니다.

[ VBA 응용 프로그램 ]

 

 

[ 새로 고침 명령 클릭 결과 ]

 

▶ Interface IpfcModelDescriptor

이 클래스에는 Creo Parametric 모델 또는 파일을 식별하는 정보가 포함됩니다. 모델 설명자는 현재 세션에 없는 모델을 나타내는 데 사용할 수 있습니다. 

■ 엑셀 VBA 소스코드

모델이 가지고 있는 모든 Parameter들을 엑셀 파일에 표시 합니다. 모델이 가지고 있는 모든 Parameter를 카운트 하는 "번호"도 함께 표시 합니다. 이름 및 타입, 값을 표시 합니다

  • 번호 - Parameter 카운트
  • Parameter name
  • Parameter Type  (문자, 정수, 실수...)
  • Parameter Value

1) "새로고침" 명령 소스코드

Sub model_parameter()

On Error GoTo RunError
    Dim asynconn As New pfcls.CCpfcAsyncConnection
    Dim conn As pfcls.IpfcAsyncConnection : Set conn = asynconn.Connect("", "", ".", 5)
    Dim session As pfcls.IpfcBaseSession: Set session = conn.session
    Dim Model As pfcls.IpfcModel : Set Model = session.CurrentModel

    Dim oPowner As pfcls.IpfcParameterOwner: Set oPowner = Model 
    Dim oParams As IpfcParameters: Set oParams = oPowner.ListParams()

    Dim oParam As IpfcBaseParameter
    Dim oParamValue As IpfcParamValue
    Dim oParamName As IpfcNamedModelItem

    For i = 0 To oParams.Count - 1
        Set oParam = oParams(i)
        Set oParamValue = oParam.Value
        Set oParamName = oParam

        Cells(i + 3, "C") = i + 1
        Cells(i + 3, "D") = oParamName.Name


      If oParamValue.discr = 0 Then 'If parameter is string
           Cells(i + 3, "E") = "String"
           Cells(i + 3, "F") = oParamValue.StringValue

           ElseIf oParamValue.discr = 3 Then 'if parameter is real value
               Cells(i + 3, "E") = "Real"
               Cells(i + 3, "F") = oParamValue.DoubleValue

           ElseIf oParamValue.discr = 1 Then 'if parameter is Integer value
                   Cells(i + 3, "E") = "Integer"
                   Cells(i + 3, "F") = oParamValue.IntValue

            ElseIf oParamValue.discr = 2 Then 'if parameter is Boolean value
                   Cells(i + 3, "E") = "bool"
                   Cells(i + 3, "F") = oParamValue.BoolValue
       End If

    Next i

'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

 

 


2) 저장 소스코드

사용자가 Parameter 값을 변경 하면, 저장 버튼을 클릭하여 Creo Parameter 값을 변경 합니다.

1. Dim oRowcount As Integer: oRowcount = Range("c3", Range("c3").End(xlDown)).Rows.Count
    -> "번호" 행(row)의 최종 개수를 변수 "oRowcount " 반환 합니다.

 

2.   For i = 1 To oRowcount  ~ Next i

    -> For 문을 사용 하여 Cell이 가지고 있는 Parameter name, Type, Value를 확인하고, Creo의 Parameter 값을 변경 합니다

 

Sub cells_parameterinput()

On Error GoTo RunError

        Dim asynconn As New pfcls.CCpfcAsyncConnection
        Dim conn As pfcls.IpfcAsyncConnection: Set conn = asynconn.Connect("", "", ".", 5)
        Dim session As pfcls.IpfcBaseSession: Set session = conn.session
        Dim Model As pfcls.IpfcModel: Set Model = session.CurrentModel
        

        Dim oPowner As pfcls.IpfcParameterOwner: Set oPowner = Model
        Dim oBaseParameter As pfcls.IpfcBaseParameter
        Dim oParams As IpfcParameters
        Dim oParam As IpfcParameter
        Dim ParamObject As New CMpfcModelItem
        Dim oParamValue As pfcls.IpfcParamValue
        
        'Table "C3" 행렬 갯수
        Dim oRowcount As Integer: oRowcount = Range("c3", Range("c3").End(xlDown)).Rows.Count
        
      
        Dim oParamtype As String
        Dim oInteger As Integer
        Dim oDouble As Double
        Dim i As Integer
        
        Dim oStringvalue As String
        
        
        For i = 1 To oRowcount
            Cells(i + 2, "E").Select
            oParamtype = ActiveCell.Value
            
            If oParamtype = "String" Then
                        
                        Set oParam = oPowner.GetParam(Cells(i + 2, "D"))
                        Set oBaseParameter = oParam
                        'oStringvalue = Cells(i + 2, "F")
                        Set oParamValue = ParamObject.CreateStringParamValue(Cells(i + 2, "F"))
                        oBaseParameter.Value = oParamValue
            
            ElseIf oParamtype = "Integer" Then
                        
                        Set oParam = oPowner.GetParam(Cells(i + 2, "D"))
                        Set oBaseParameter = oParam
                        Set oParamValue = ParamObject.CreateIntParamValue(Cells(i + 2, "F"))
                        oBaseParameter.Value = oParamValue
             
            ElseIf oParamtype = "Bool" Then
                        
                        Set oParam = oPowner.GetParam(Cells(i + 2, "D"))
                        Set oBaseParameter = oParam
                        Set oParamValue = ParamObject.CreateBoolParamValue(Cells(i + 2, "F"))
                        oBaseParameter.Value = oParamValue
             
            ElseIf oParamtype = "Real" Then
                        
                        Set oParam = oPowner.GetParam(Cells(i + 2, "D"))
                        Set oBaseParameter = oParam
                        Set oParamValue = ParamObject.CreateDoubleParamValue(Cells(i + 2, "F"))
                        oBaseParameter.Value = oParamValue
            
             
            Else
                        Set oParam = oPowner.GetParam(Cells(i + 2, "D"))
                        Set oBaseParameter = oParam
                        Set oParamValue = ParamObject.CreateBoolParamValue(Cells(i + 2, "F"))
                        oBaseParameter.Value = oParamValuelue
            
            
            End If
                    
        Next i


    '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


 

[ 프로그램 테스트 ]

 

CREO VBA 엑셀 프로그램은 "매크로"를 확장한 개념입니다. 첨부된 소스를 복사 하여 사용 하십시요.

WEB과 유튜브에 "VBA 엑셀" 기초 적인 코딩 방법을 배울수 있습니다.