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

Part 파일을 jpg 이미지로 저장 하기

by ToolBOX01 2022. 9. 2.
반응형

■ 함수 ExportCurrentRasterImage

Sub ExportCurrentRasterImage (ImageFileName as String, Instructions as IpfcRasterImageExportInstructions)

표준 Creo Parametric 래스터 출력 파일(현재 Window 화면)을 출력합니다. Session 기준이 아닌 Window 기준으로 모델을  이미지로 저장 합니다.
참고: DMA의 경우 BMP, TIFF, EPS 및 JPEG 이미지 유형이 지원됩니다.

 

- ImageFileName   :  확장자를 포함한 출력 파일 이름

- Instructions          : 

  Raster-Image 내보내기 작업을 제어하는 Raster-Export-Instructions 개체입니다. 이러한 모든 객체는
  IpfcRasterImageExportInstructions 에서 상속됩니다.

 

■ Interface IpfcRasterImageExportInstructions

Creo Parametric RasterImage 내보내기를 처리하는 모든 raster-image-export-instructions 클래스에 대한 기본 클래스입니다.  서브 Class는 아래와 같습니다

1) IpfcBitmapImageExportInstructions

2) IpfcTIFFImageExportInstructions

3) IpfcEPSImageExportInstructions      참고 : EPS(Encapsulated Post Script)

4) IpfcJPEGImageExportInstructions

 

IpfcRasterImageExportInstructions 속성은 아래와 같습니다. 

1) Property DotsPerInch as IpfcDotsPerInch [optional]
    -  출력 이미지의 인치당 도트 수입니다.

2) Property ImageDepth as IpfcRasterDepth [optional]
    - 출력 이미지의 깊이입니다.

3) Property ImageHeight as Double
    -출력 이미지의 높이(인치)입니다.

4) Property ImageWidth as Double

   -  출력 이미지의 너비(인치)입니다.

 

IpfcRasterImageExportInstructions 함수는 아래와 같습니다. 

1) Function GetRasterType () as IpfcRasterType

  - 래스터 이미지 내보내기 명령의 유형을 나타내는 RasterType 인스턴스를 반환합니다.

 

■ Interface IpfcRasterImageExportInstructions 예제 코드

Dim instructions As IpfcRasterImageExportInstructions
Dim imageExtension As String
Dim rasterHeight As Double, rasterWidth As Double
        rasterHeight = 5
        rasterWidth = 5
Dim dotsPerInch As Integer, imageDepth As Integer
        dotsPerInch = EpfcDotsPerInch.EpfcRASTERDPI_100
        imageDepth = EpfcRasterDepth.EpfcRASTERDEPTH_24

 

아래 그림은 creo에서 jpg 이지미 만들기 옵션 입니다. rasterHeight = 5, rasterWidth = 5, EpfcDotsPerInch.EpfcRASTERDPI_100, EpfcRasterDepth.EpfcRASTERDEPTH_24를 정의 합니다.

[ JPG 옵션 정의 ]

 

 Dim creJPEG As New CCpfcJPEGImageExportInstructions
 Dim JPEGInstrs As IpfcJPEGImageExportInstructions

 Dim oJpgfilename As String
 Set JPEGInstrs = creJPEG.Create(rasterWidth, rasterHeight)
 Set instructions = JPEGInstrs

oJpgfilename = Model.FullName & ".JPG"

Call window.ExportRasterImage(oJpgfilename, instructions)

 

 

■ 프로그램 개발 방향

1) 현재 Window 모델을 확인 합니다

2) 정의한 뷰로 화면을 이동 합니다

3) 옵션을 정의 합니다

4) jpg 파일을 특정 폴더에 저장 합니다

5) 엑셀 파일에 변환된 파일 이름을 표시 합니다.

 

 

■ 소스 코드

Sub jpg_trans()
    
On Error GoTo RunError
    Dim asynconn As New pfcls.CCpfcAsyncConnection
    Dim conn As pfcls.IpfcAsyncConnection
    Dim session As pfcls.IpfcBaseSession
    Dim Model As pfcls.IpfcModel
    Dim owindow As IpfcWindow
    
    Set conn = asynconn.Connect("", "", ".", 5)
    
    'Get the current session
    Set session = conn.session
    Set Model = session.CurrentModel
    Set owindow = session.GetModelWindow(Model)
    
    'Activate the new window before jpg (Good practice)
    owindow.Activate
     
     
    'jpg 변환 옵션변수 정의
    Dim rasterHeight As Double: rasterHeight = 5

    Dim rasterWidth As Double: rasterWidth = 5
             
    Dim creJPEG As New CCpfcJPEGImageExportInstructions
    Dim JPEGInstrs As IpfcJPEGImageExportInstructions
    Set JPEGInstrs = creJPEG.Create(rasterWidth, rasterHeight)

    Dim instructions As IpfcRasterImageExportInstructions
    Set instructions = JPEGInstrs

    instructions.dotsPerInch = EpfcDotsPerInch.EpfcRASTERDPI_100
    instructions.imageDepth = EpfcRasterDepth.EpfcRASTERDEPTH_8

   

    'ISOView MOVE
    Dim oViewOwner As IpfcViewOwner
    Dim oIpfcView As IpfcView
    Set oViewOwner = session.CurrentModel
    Set oIpfcView = oViewOwner.RetrieveView("isoview")
    oJpgfilename = Model.FullName & ".JPG"
    

    'test MsgBox 
    'MsgBox (oJpgfilename)
    

     'jpg image location 
    session.ChangeDirectory ("C:\PTC\WORK60\TEST")    
    

    'jpg image 변환 실행

    Call owindow.ExportRasterImage(oJpgfilename, JPEGInstrs)
    

     'jpg image 변환된 파일 표시
    Range("C2").Value = Model.Filename
    

    'jpg image 변환된 파일 닫기
    owindow.Close
   
    'Disconnect with Creo
    conn.Disconnect (2)
    
    'Cleanup
    Set asynconn = Nothing
    Set conn = Nothing
    Set session = Nothing
    Set Model = Nothing
       
    
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

 

■ 프로그램 실행 결과

 VBA  오류

철차 오류 발생한 경우 에러 코드 '424'가 발생 하였습니다. 예시  오류 : "window.Close"   /  수정 :  "owindow.Close"

- "window" 변수가 정의되지 않았기 때문에 발생 하는 문제 입니다.  F8 키를 이용하여 한줄씩 마다 코드를 실행 합니다.

 

 

 

 

VBA Object Required Error (Error 424)

In this tutorial, you will learn why Object Required (Error 424) occurs, and how to deal with it while write a VBA code.

excelchamps.com

 

 

 

 

 

 

 

 

 

 

 

VBA 기초 개념

 

 

[VBA] 객체(Object) 완벽 가이드

이 포스트는 Excel Macro Mastery 사이트의 'VBA Objects - The Ultimate Guide(by Paul Kelly)'의 내용을 다시 정리한 것입니다. 이번 포스트에서는 엑셀 VBA의 객체 대해 다룹니다. Quick Guide Task Examples..

kukuta.tistory.com