업무 자동화/VBA, VB.NET For Creo

Excel VBA Study] Assembling the Part File #1

ToolBOX01 2025. 7. 8. 11:07
반응형

▣ Part 파일 조립하는 코드 입니다. (This is the code to assemble the part files.)

  • 현재의 어셈블리 파일애 "korea.prt" 파일을 조립 조건 없이 조립하는 기능입니다.  
  • This is a function to assemble the "korea.prt" file in the current assembly file without assembly conditions.

[Program execution results]

프로그램 코드 해석은 AI를 활용하세요, 자세한 설명을 제공 합니다

Use AI to interpret program code, providing detailed explanations

▷ Creo 연결 코드 (Creo connection code)

Option Explicit

Public asynconn As New pfcls.CCpfcAsyncConnection
Public conn As pfcls.IpfcAsyncConnection
Public BaseSession As pfcls.IpfcBaseSession

Public Sub CreoConnt02()
    On Error GoTo ErrorHandler   '// Setting up an error handler

    '// Creo Connection Settings
    Set conn = asynconn.Connect("", "", "", 5)
    Set BaseSession = conn.Session

    Exit Sub '// Terminates so that the flow does not go to the error handler.

ErrorHandler:
    '// Exception handling when Creo is not running
    If InStr(Err.Description, "XToolkitNotFound") > 0 Then
        MsgBox "Make sure Creo is running.", vbCritical, "error"
    Else
        MsgBox "An error occurred: " & Err.Description, vbCritical, "alarm"
    End If
End Sub

CreoVBAStart.bas
0.00MB

▷ "korea.prt" 조립 코드 ("korea.prt" assembly code)

Option Explicit
Sub CreoAssy01()

     On Error GoTo RunError
     Application.EnableEvents = False
   
     '// Creo Connection
     Call CreoVBAStart02.CreoConnt02
       
    Dim model As IpfcModel
    Dim Solid As IpfcSolid
    Dim CreatepartModelDescriptor As New CCpfcModelDescriptor
    Dim partModelDescriptor As IpfcModelDescriptor
    Dim partToAssembleModel As IpfcSolid
    Dim currentAssembly As IpfcAssembly
    Dim ComponentFeat As IpfcComponentFeat
    Dim CreateComponentConstraint As New CCpfcComponentConstraint
    Dim ComponentConstraint As IpfcComponentConstraint
    
    
    '// Ensure the current model is an assembly
    Set model = BaseSession.CurrentModel
    If model.Type <> EpfcMDL_ASSEMBLY Then
        MsgBox "Current model is not an assembly. Please open or create an assembly.", vbCritical, "Error"
        GoTo CleanUp
    End If
    Set Solid = model
    Set currentAssembly = Solid '// Cast Solid to Assembly if it is an assembly
        
    '// Retrieve the part to be assembled
    Set partModelDescriptor = CreatepartModelDescriptor.CreateFromFileName("korea.prt")
    Set partToAssembleModel = BaseSession.RetrieveModel(partModelDescriptor)
    
    If partToAssembleModel Is Nothing Then
        
        MsgBox "Part' " & "korea.prt" & "'Could not find or load . Make sure it is in the working directory or search path.", vbCritical, "Error"
        GoTo CleanUp
        
    End If
    
    '//Set Assembly = Componrnt01
    Set ComponentFeat = currentAssembly.AssembleComponent(partToAssembleModel, Nothing)
        
     conn.Disconnect (2)

CleanUp:
    Set asynconn = Nothing
    Set conn = Nothing
    Set BaseSession = Nothing
    Set model = Nothing
    Application.EnableEvents = True

RunError:
            If Err.Number <> 0 Then
                MsgBox "Process Failed: An error occurred." & vbCrLf & _
                       "Error No: " & CStr(Err.Number) & vbCrLf & _
                       "Error Description: " & Err.Description & vbCrLf & _
                       "Error Source: " & Err.Source, vbCritical, "Error"
                If Not conn Is Nothing Then
                    If conn.IsRunning Then
                        conn.Disconnect (2)
                    End If
                End If
            End If    
End Sub

▷ 조립 조건을 사용하는 경우 (When using assembly conditions)

'// 기본면 선택 예시 (앞면 기준)  Example of selecting a base surface (based on the front surface)
Dim asmSel As IpfcSelection
Dim compSel As IpfcSelection
Dim asmSelObj As IpfcModelItem
Dim compSelObj As IpfcModelItem

'// 어셈블리 모델에서 기본 평면 (예: FRONT) 찾기  Finding the base plane (e.g. FRONT) in the assembly model
Set asmSelObj = currentAssembly.GetItemByName(EpfcITEM_SURFACE, "FRONT")
Set asmSel = BaseSession.CreateModelItemSelection(asmSelObj, currentAssembly)

'// 조립할 부품에서 기본 평면 (예: FRONT) 찾기  Find the base plane (e.g. FRONT) on the part to be assembled
Set compSelObj = partToAssembleModel.GetItemByName(EpfcITEM_SURFACE, "FRONT")
Set compSel = BaseSession.CreateModelItemSelection(compSelObj, partToAssembleModel)

'// 제약 조건 생성 및 참조 지정   Creating constraints and specifying references
Set ComponentConstraint = CreateComponentConstraint.Create(EpfcASM_CONSTRAINT_MATE)
Set ComponentConstraint.AssemblyReference = asmSel
Set ComponentConstraint.ComponentReference = compSel

'// 제약 조건 배열로 구성  Consists of an array of constraints
Dim Constraints As CCpfcComponentConstraintList
Set Constraints = New CCpfcComponentConstraintList
Call Constraints.Append(ComponentConstraint)

'// 조립  assembly
Set ComponentFeat = currentAssembly.AssembleComponent(partToAssembleModel, Constraints)

 

 

자동으로 부품을 기본 (Deault) 구속으로 조립하기 - 작업중

□ vba 조입 구속 조건 ▷ 조립품 구속조건 유형이 포함되어 있습니다.EpfcASM_CONSTRAINT_MATEMate two surfaces.EpfcASM_CONSTRAINT_MATE_OFFMate two surfaces, with an offset.EpfcASM_CONSTRAINT_ALIGNAlign two items.EpfcASM_CONSTRAINT_A

tool-2020.tistory.com

 

▷ Youtube

 

 

creo file assemble.cls
0.00MB

 

by : korealionkk@gmail.com


반응형