본문 바로가기
  • Welcome!
VBA, VB.NET For Creo

Creo] Get drawing information - 작업중

by ToolBOX01 2025. 1. 27.
반응형

□ Get drawing information

  • Get names of drawing views
  • Get dimension names and values ​​placed in a drawing view
  • Get the dimensions type
  • Get the dimensions location information
  • Get tolerance values ​​for dimensions
  • Get the type of tolerance

 

▷ Code : Creo Connectivity Module

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 연결 설정
        Set conn = asynconn.Connect("", "", "", 5)
    
        '// Creo Connection Settings
    On Error Resume Next '// Keep code execution going even when an exception occurs
        Set BaseSession = conn.Session
        If Err.Number <> 0 Then
            MsgBox "Failed to import Creo session. Check your connection status." & vbCrLf & _
                   "Error message: " & Err.Description, vbCritical, "error"
            Err.Clear
            Exit Sub
        End If
        
    On Error GoTo ErrorHandler '// Revert back to default error handling   
ErrorHandler:
   ' ToolkitNotFound Special handling of error messages
    If InStr(Err.Description, "XToolkitNotFound") > 0 Then
        MsgBox "Make sure Creo is running.", vbCritical, "error"
        Exit Sub
    End If
End Sub

 

I listened to music and curled up with AI.

 

▷ Code : Get drawing model information

  • Get the number of all sheets a drawing has
  • Get the names of the views placed on the sheet
Dim ws As Worksheet
Dim Model2D As pfcls.IpfcModel2D
Dim SheetOwner As pfcls.IpfcSheetOwner
Dim View2Ds As pfcls.IpfcView2Ds
Dim View2D As pfcls.IpfcView2D
Dim i As integer

Set ws = ThisWorkbook.Worksheets("2d_dimension")
Set Model2D = BaseSession.CurrentModel
Set SheetOwner = Model2D
ws.Cells(4, "C") = SheetOwner.NumberOfSheets

Set View2Ds = Model2D.List2DViews

For i = 0 To View2Ds.Count - 1
    Set View2D = View2Ds.item(i)
    ws.Cells(i + 6, "B") = View2D.Name
next i

Set View2Ds = Model2D.List2DViews

  • Displays all view names in multiple SHEETs.
View Name Execution results

 

▷ Code :  Get dimension names and values ​​placed in a drawing view

  • You can retrieve the model associated with the view.
  • You can get the dimensions of the model
  • You can retrieve all dimensions placed in the drawing from all dimensions of the model.
  • All dimensions placed in the drawing are saved in the 3D model.
  • Compare the view with the dimensions placed on it to the active view.
Sub DimInfo01()

     '// Creo Connection
    Call CreoVBAStart02.CreoConnt02
    
    Dim ws As Worksheet
    Dim Model As pfcls.IpfcModel
    Dim Model2D As pfcls.IpfcModel2D
    Dim Drawing As pfcls.IpfcDrawing
    Dim SheetOwner As pfcls.IpfcSheetOwner
    Dim View2Ds As pfcls.IpfcView2Ds
    Dim View2D As pfcls.IpfcView2D
    Dim TargetView2D As pfcls.IpfcView2D
    
    Dim ModelItemOwner As pfcls.IpfcModelItemOwner
    Dim ModelItems As pfcls.IpfcModelItems
    Dim ModelItem As pfcls.IpfcModelItem
    Dim BaseDimension As pfcls.IpfcBaseDimension
    Dim Dimension As pfcls.IpfcDimension
    Dim i As Integer
    Dim j As Integer
      
    Set ws = ThisWorkbook.Worksheets("2d_dimension")
     
    Set Model2D = BaseSession.CurrentModel  
    Set Drawing = Model2D
    Set SheetOwner = Model2D
    ws.Cells(4, "C") = SheetOwner.NumberOfSheets
    Set View2Ds = Model2D.List2DViews
    
    For i = 0 To View2Ds.Count - 1
            Set View2D = View2Ds.Item(i)
            Set Model = View2D.GetModel
            Set ModelItemOwner = Model
            Set ModelItems = ModelItemOwner.ListItems(EpfcModelItemType.EpfcITEM_DIMENSION)
                        
            For j = 0 To ModelItems.Count - 1
            
                Set BaseDimension = ModelItems.Item(j)            
                If View2D.CheckIsDimensionDisplayed(BaseDimension) = True Then
                   
                   Set Dimension = BaseDimension
                   Set TargetView2D = Drawing.GetDimensionView(Dimension)
                   If TargetView2D.Name = View2D.Name Then
                   
                     Debug.Print TargetView2D.Name
                     Debug.Print BaseDimension.Symbol
                   
                   End If
                End If
            Next j      
    Next i    
End Sub