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

4-5 # Parameter : 엑셀에서 매개변수 읽고, CREO 변경 하기

by ToolBOX01 2021. 1. 23.
반응형

CREO 파일에서 매개 변수 이름 확인하고, 동일한 매개변수가 있으면, 값만 변경 하고, 동일한 매개변수 이름이
없으면 매개변수 이름을 생성 하고, 값을 입력 하는 프로그램. 다음과 같은 순서로 코딩 작업을 한다.

Parameter 관련 코딩 순서

 


프로그램 코딩 순서

1 단계 : 폴더 및 파일 이름 선택

 

Sub Parameter01()
    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 oForderName As String
    oForderName = Cells(4, 5)
    session.ChangeDirectory (oForderName)
    
    Dim oCreofileName As String
    oCreofileName = Cells(5, 5)
    
End Sub    

 

엑셀의 작업 공간 Cells 및 Creo 파일 이름의 데이터 화면 표시 하기
Dim oForderName As String   : 폴더 이름 변수로 정의
Dim oCreofileName As String : Creo 파일 이름 변수 정의 하기

엑셀 화면

2 단계 : CREO 화면에  파일 읽어 오기 

 

Creo 파일 이름으로 표시하려면 설명자 (IpfcModelDescriptor) 형식을 정의 해야 한다.
설명자 유형 + 파일 이름 + NULL 또는 페밀리 테이블 Instance 이름)

 

    Dim oForderName As String
    oForderName = Cells(4, 5)
    session.ChangeDirectory (oForderName)
    
    Dim oCreofileName As String
    oCreofileName = Cells(5, 5)
    
    Dim oModelDescriptorCreate As New CCpfcModelDescriptor
    Dim oModelDescriptor As IpfcModelDescriptor
    Set oModelDescriptor = oModelDescriptorCreate.Create(EpfcMDL_PART, oCreofileName, Null)
    
    Dim oModel As pfcls.IpfcModel
    Set oModel = session.RetrieveModel(oModelDescriptor)

    Dim oWindow As pfcls.IpfcWindow
    Set oWindow = session.OpenFile(oModelDescriptor)

 

Creo 파일을 읽어 오는 순서는 Session에 표시 한다음, Creo  파일을 메인 화면에 표시 합니다.

설명자 유형은 아래와 같다

 

EpfcMDL_ASSEMBLY    - An assembly.
EpfcMDL_PART        - A part.
EpfcMDL_DRAWING     - A drawing.
EpfcMDL_2D_SECTION  - A two-dimensional section.
EpfcMDL_LAYOUT      - A layout.
EpfcMDL_DWG_FORMAT  - A drawing format.
EpfcMDL_MFG         - A manufacturing model.
EpfcMDL_REPORT      - A report.
EpfcMDL_MARKUP      - A drawing markup.
EpfcMDL_DIAGRAM     - A diagram.
EpfcMDL_UNSPECIFIED - Not specifying a particular model type
EpfcMDL_CE_SOLID    - Layout model; NOTE: this type is read-only, passing it back to Creo may lead to unpredictable behavior.
EpfcMDL_CE_DRAWING  - Reserved for internal use.
EpfcModelType_nil   - Use this enumerated value to represent "null" passed to optional properties or method arguments.

3 단계 :

현재 모델의 매개변수를 모두 List로 만듭니다. List는 카운트가 정의 되며, 시작은 "0" 부터 카운트 합니다. 

 

    Dim solid As IpfcSolid
    Set solid = oModel
    Dim LenUnit As IpfcUnit
    Set LenUnit = solid.GetUnit("in", False)
    
    Dim oParameter As IpfcBaseParameter
    Dim Powner As pfcls.IpfcParameterOwner
    Set Powner = oModel
    Dim params As IpfcParameters
    Set params = Powner.ListParams()

 

4 단계:

FOR문과 IF 문을 사용하여 "LENGTH" 매개변수 존재 하는지 알아보기

 

Dim oParamString As String
    
    For i = 0 To params.Count - 1
         Set param = params.Item(i)
         Set oParamItem = param
         oParamName = oParamItem.Name
         If oParamName = oRangeParamName Then
            Set paramValue = ParamObject.CreateDoubleParamValue(CDbl(oRangeDouble))
            oParameter.Value = paramValue
            oModel.Save
         Else
            oParamString = "mango"
         End If
    Next i
    
         If oParamString = "mango" Then
            Set oCreateParamValue = ParamObject.CreateDoubleParamValue(CDbl(oRangeDouble))
            Set oParameter = Powner.CreateParamWithUnits(oRangeParamName, oCreateParamValue, LenUnit)
            oModel.Save
         End If

 


소스 코드

 

Sub Parameter02()
    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 oForderName As String
    oForderName = Cells(4, 5)
    session.ChangeDirectory (oForderName)
    
    Dim oCreofileName As String
    oCreofileName = Cells(5, 5)
    
    Dim oModelDescriptorCreate As New CCpfcModelDescriptor
    Dim oModelDescriptor As IpfcModelDescriptor
    Set oModelDescriptor = oModelDescriptorCreate.Create(EpfcMDL_PART, oCreofileName, Null)
    
    Dim oModel As pfcls.IpfcModel
    Set oModel = session.RetrieveModel(oModelDescriptor)

    Dim oWindow As pfcls.IpfcWindow
    Set oWindow = session.OpenFile(oModelDescriptor)
    oWindow.Activate
    
    Dim solid As IpfcSolid
    Set solid = oModel
    
    Dim oParameter As pfcls.IpfcBaseParameter
    Dim Powner As pfcls.IpfcParameterOwner
    Set Powner = oModel
    Dim params As IpfcParameters
    Set params = Powner.ListParams()
     
    Dim ParamObject As New CMpfcModelItem
    Dim oCreateParamValue As CpfcParamValue

    Dim oParamItem As IpfcNamedModelItem
    Dim oParamValue As IpfcParamValue

    Dim oParamName As String
    Dim oRangeParamName As String
    oRangeParamName = Cells(6, 4)
    Set param = Powner.GetParam(oRangeParamName)
    
    Dim oRangeDouble As Double
    oRangeDouble = Cells(6, 5)
    
    Dim oParamString As String
    
    For i = 0 To params.Count - 1
         Set param = params.Item(i)
         Set oParamItem = param
         oParamName = oParamItem.Name
         If oParamName = oRangeParamName Then
            Set paramValue = ParamObject.CreateDoubleParamValue(CDbl(oRangeDouble))
            Set param = Powner.GetParam(oRangeParamName)
            Set oParameter = param
            oParameter.Value = paramValue
            oModel.Save
            oParamString = "apple"
         Else
            oParamString = "mango"
         End If
    Next i
    
         If oParamString = "mango" Then
            Set oCreateParamValue = ParamObject.CreateDoubleParamValue(CDbl(oRangeDouble))
            Set oParameter = Powner.CreateParam(oRangeParamName, oCreateParamValue)
            oModel.Save
         End If


conn.Disconnect (2)

End Sub

 

parameter v2.xlsm
0.02MB