Creo Sample Code 분석
◎ Application.java 실행 결과
PTC Creo의 J-Link API를 사용하여 Creo 세션에 접근하고 모델 정보를 추적하는 프로그램입니다. 활성된 모델이 존재 한다면 아래와 같이 출력됩니다. 만일 활성화된 모델이 없다면 오류가 발생 합니다.
This is the model information tracking program
Created using J-Link - written in Java 1.1
Stand by while the program retrieves model information
Tracker: Complete
◎ Application.java 전체 코드
import com.ptc.pfc.pfcGlobal.*;
import com.ptc.pfc.pfcSession.*;
import com.ptc.pfc.pfcModel.*;
import com.ptc.tracker.Tracker;
public class Application {
public static Session session;
public static Models models;
/* Static start method for the application */
public static void app_start ()
{
title();
try {
session = pfcSession.GetCurrentSessionWithCompatibility (CreoCompatibility.C4Compatible);
models = session.ListModels();
new Tracker(session, models);
}
catch (Throwable x)
{
printMsg("Exception caught: "+x);
x.printStackTrace();
}
}
/* Start method used for model program */
public static void mp_start ()
{
title();
try {
session = pfcSession.GetCurrentSessionWithCompatibility (CreoCompatibility.C4Compatible);
models = Models.create();
models.insert(0, session.GetCurrentModel());
new Tracker (session, models);
printMsg("Tracker: Complete");
}
catch (Throwable x)
{
printMsg("Exception caught: "+x);
x.printStackTrace();
}
}
/* Stop method used for application and model program */
public static void stop ()
{
printMsg("Tracker: Stopped");
}
/* Prints messages to standard output */
public static void printMsg(String Msg)
{
System.out.println(Msg);
}
/* Prints startup message */
public static void title()
{
printMsg("This is the model information tracking program");
printMsg("Created using J-Link - written in Java 1.1");
printMsg("Stand by while the program retrieves model information");
}
}
◎ Application.java 분석
1. 임포트된 패키지
import com.ptc.pfc.pfcGlobal.*;
import com.ptc.pfc.pfcSession.*;
import com.ptc.pfc.pfcModel.*;
import com.ptc.tracker.Tracker;
Java 프로그램에서 PTC Creo Parametric 3D CAD 소프트웨어와 통신하기 위한 J-Link API의 핵심 패키지들을 import하는 구문입니다. 이 코드는 Java를 사용하여 Creo의 기능을 자동화하거나 확장하는 프로그램을 작성할 때 반드시 필요합니다. (PTC(Parametric Technology Corporation)가 제공하는 Java API와 관련된 코드입니다.)
📦 임포트된 패키지 설명
| 임포트 구문 | 소속 API | 설명 |
| import com.ptc.pfc.pfcGlobal.*; | pfcGlobal | Creo J-Link의 전역 설정 및 유틸리티 함수가 포함되어 있습니다. CreoCompatibility 열거형 등 세션을 시작하는 데 필요한 기본 요소들이 이 패키지에 정의되어 있습니다. |
| import com.ptc.pfc.pfcSession.*; | pfcSession | Creo와의 연결(세션)을 관리하는 클래스들이 포함되어 있습니다. 프로그램이 Creo에 접속하고 현재 작업 환경(Session 객체)을 가져오거나 제어하는 데 사용됩니다. |
| import com.ptc.pfc.pfcModel.*; | pfcModel | Creo의 모델(Model) 객체와 관련된 클래스들이 포함되어 있습니다. 부품(Part), 어셈블리(Assembly), 드로잉(Drawing) 등 실제 CAD 데이터를 표현하고 조작하는 데 사용됩니다. |
| import com.ptc.tracker.Tracker; | 사용자 정의 | 이것은 PTC에서 제공하는 API가 아니라, 개발자가 자체적으로 정의한 Tracker 클래스입니다. 이 클래스는 일반적으로 Creo 세션과 모델 목록을 받아 실제 모델 정보 추적 및 처리 로직을 수행하는 핵심 모듈일 가능성이 높습니다. |
이 임포트 구문들을 사용하면 Java 코드 내에서 Creo의 세션을 가져오고, 열려 있는 모델을 나열하며, 모델의 피처, 파라미터, 재생성 상태 등의 데이터에 접근하여 수정하거나 정보를 추출할 수 있습니다.
2. Application 클래스 구조
public class Application {
public static Session session;
public static Models models;
}
Java 언어로 작성된 간단한 클래스 정의입니다. 애플리케이션의 핵심 데이터 구조를 정의하고 있습니다.
| 구성 요소 | 유형 | 소속 API/역할 | 설명 |
| public class Application | 클래스 | 프로그램의 기본 구조 | 이 프로그램 또는 애플리케이션의 이름이자 컨테이너 역할을 하는 클래스입니다. 모든 로직(메서드)과 데이터(변수)가 이 안에 포함됩니다. |
| public static Session session: | 정적 변수 | com.ptc.pfc.pfcSession | Creo Parametric 소프트웨어와의 활성 연결을 나타내는 객체입니다. 이 Session 객체를 통해 Creo 환경 전체를 제어하고 접근할 수 있습니다. static으로 선언되어 이 클래스의 모든 부분에서 공유되고 접근됩니다. |
| public static Models models; | 정적 변수 | com.ptc.pfc.pfcModel | Creo 세션에서 열려 있는 모델들의 목록을 담는 컬렉션 객체입니다. 이 Models 컬렉션은 하나 이상의 부품(Part)이나 어셈블리(Assembly) 파일을 참조합니다. static으로 선언되어 세션과 마찬가지로 프로그램 전체에서 공유됩니다. |
💡 public static의 의미
두 변수(session과 models)에 붙어 있는 public static 키워드는 이 코드가 J-Link 환경에서 작동하는 방식에 매우 중요합니다.
public: 이 변수들이 클래스 외부에서도 직접 접근 가능함을 의미합니다. (예: Application.session, Application.models)
Application.session과 Application.models를 통해 Creo의 연결 상태 및 활성화된 모델 정보에 직접 접근하고 조작할 수 있다는 의미가 됩니다.
static (정적): 이 변수들이 Application 클래스 자체에 속하며, Application의 객체를 별도로 생성하지 않고도 메모리에 존재함을 의미합니다. 즉, 이 변수들은 프로그램이 실행되는 동안 단 하나만 존재하며 모든 메서드(예: app_start(), mp_start())가 이 동일한 세션과 모델 목록을 공유하게 됩니다.
💡 public static Session session; 분석
| 구성 요소 | 역할 | 상세 설명 |
| Session | 변수 타입 (Class) | 이 변수가 어떤 종류의 데이터를 저장할지 정의합니다. Session은 Creo J-Link API에서 제공하는 클래스(Class) 또는 인터페이스(Interface) 이름입니다. 즉, 이 변수는 Creo와의 활성 연결 정보를 담는 객체를 참조하게 됩니다. |
| session | 변수 이름 (Variable) | Session 타입의 데이터를 저장하기 위해 개발자가 정의한 변수의 이름입니다. 실제 코드에서 Creo 세션을 참조하고 조작할 때 이 이름을 사용합니다. |
| public static | 접근 및 사용 범위 | 이 변수가 프로그램의 어느 곳에서든 접근 가능하고 (public), Application 클래스의 모든 인스턴스에서 공유되는 (static) 정적 멤버임을 나타냅니다. |
주의
Session은 Creo J-Link API (com.ptc.pfc.pfcSession.*)에 정의되어 제공되는 클래스이며, 이 클래스를 사용해야만 Creo 환경에 접근할 수 있습니다.
3. app_start() 메서드 기능
/* Static start method for the application */
public static void app_start ()
{
title();
try {
session = pfcSession.GetCurrentSessionWithCompatibility (CreoCompatibility.C4Compatible);
models = session.ListModels();
new Tracker(session, models);
}
catch (Throwable x)
{
printMsg("Exception caught: "+x);
x.printStackTrace();
}
}
app_start() 메서드는 PTC Creo Parametric의 J-Link 애플리케이션이 시작될 때 호출되는 초기화 및 실행 진입점 역할을 합니다. 이 메서드의 주요 기능은 Creo 세션에 연결하고, 현재 열려있는 모든 모델 목록을 가져와, 추적 로직(Tracker)을 시작하는 것입니다. 다음과 같이 구성 되어 있습니다
| 코드 구문 | 기능 | 역할 |
| title(); | 시작 메시지 출력 | 프로그램의 제목과 시작 메시지를 표준 출력 (System.out)에 표시합니다. |
| try { ... } catch (Throwable x) | 예외 처리 시작 | Creo API 호출 중 발생할 수 있는 모든 오류를 포착하여 프로그램이 비정상적으로 종료되는 것을 방지합니다. |
| session = pfcSession.GetCurrentSessionWithCompatibility (CreoCompatibility.C4Compatible); | Creo 세션 연결 | 현재 실행 중인 Creo Parametric 인스턴스와의 J-Link 연결(세션)을 설정합니다. CreoCompatibility.C4Compatible은 Creo 버전 4.0 이상과의 호환성을 명시합니다. 이로써 session 변수에 Creo와의 통신 객체가 저장됩니다. |
| models = session.ListModels(); | 열린 모델 목록 획득 | 앞서 가져온 세션(session)을 사용하여, 현재 Creo 메모리에 열려 있는 모든 부품, 어셈블리, 드로잉 파일의 목록을 가져와 models 변수에 저장합니다. 이 목록에는 활성화된 모델뿐만 아니라 배경에 열려 있는 모든 모델이 포함됩니다. |
예외 처리 catch (Throwable x)
| 코드 구문 | 기능 | 역할 |
| catch (Throwable x) | 오류 포착 | try 블록 내에서 J-Link API 호출 실패(session을 가져오지 못한 경우 등)와 같은 예외가 발생하면 이를 포착합니다. |
| printMsg("Exception caught: "+x); | 오류 메시지 출력 | 발생한 예외의 간단한 메시지를 출력합니다. |
| x.printStackTrace(); | 스택 트레이스 출력 | 예외가 발생한 상세한 코드 경로를 출력하여 디버깅에 도움을 줍니다. |
4. mp_start() 메서드 기능
현재 Creo에서 활성화된 단 하나의 모델에만 연결하여 추적 로직(Tracker)을 실행하는 것입니다
/* Start method used for model program */
public static void mp_start ()
{
title();
try {
session = pfcSession.GetCurrentSessionWithCompatibility (CreoCompatibility.C4Compatible);
models = Models.create();
models.insert(0, session.GetCurrentModel());
new Tracker (session, models);
printMsg("Tracker: Complete");
}
catch (Throwable x)
{
printMsg("Exception caught: "+x);
x.printStackTrace();
}
}
| 코드 구문 | 기능 | 역할 |
| models = Models.create(); | 빈 모델 목록 생성 | 모든 열린 모델을 가져오는 대신, 비어 있는 모델 컬렉션(models)을 새로 생성합니다. |
| models.insert(0, session.GetCurrentModel()); | 현재 모델 삽입 | 세션으로부터 Creo 화면에 현재 활성화되어 있는 단 하나의 모델(session.GetCurrentModel())만 가져와 생성된 모델 목록의 첫 번째 위치에 추가합니다. 다른 열린 모델들은 무시됩니다. |
| new Tracker (session, models); | 추적 로직 실행 | Creo 세션과 현재 활성화된 단일 모델 목록을 인수로 사용하여 Tracker 클래스를 실행합니다. 이 클래스가 해당 모델에 대한 구체적인 작업을 수행합니다. |
| printMsg("Tracker: Complete"); | 완료 메시지 출력 | Tracker 로직이 성공적으로 완료되었음을 알리는 메시지를 출력합니다. |
models에 담긴 첫 번째 모델의 이름을 가져와 출력하도록 코드를 수정했습니다,
예시 활성화된 모델의 이름 " exercise1_1.prt"을 가져옵니다.
/* Start method used for model program */
public static void mp_start ()
{
title();
try {
session = pfcSession.GetCurrentSessionWithCompatibility (CreoCompatibility.C4Compatible);
models = Models.create();
// 1. 활성화된 모델을 가져와 models에 삽입
models.insert(0, session.GetCurrentModel());
new Tracker (session, models);
// 2. models 컬렉션에서 첫 번째 모델 객체를 가져옴
Model currentModel = models.get(0);
// 3. 모델 객체의 이름을 가져옴 (예시: "exercise1_1.prt")
String modelName = currentModel.GetFullName();
// 4. 이름이 포함된 메시지를 출력
printMsg("Tracker: Complete for model: " + modelName);
}
catch (Throwable x)
{
printMsg("Exception caught: "+x);
x.printStackTrace();
}
}
5. 유틸리티 메서드
애플리케이션의 종료 및 메시지 출력을 담당하는 세 개의 유틸리티 메서드입니다. 이 메서드는 애플리케이션 또는 모델 프로그램이 작업을 완료하고 종료될 때 호출되도록 설계되었습니다.
/* Stop method used for application and model program */
public static void stop ()
{
printMsg("Tracker: Stopped");
}
/* Prints messages to standard output */
public static void printMsg(String Msg)
{
System.out.println(Msg);
}
/* Prints startup message */
public static void title()
{
printMsg("This is the model information tracking program");
printMsg("Created using J-Link - written in Java 1.1");
printMsg("Stand by while the program retrieves model information");
}
5-1. public static void stop ()
이 메서드는 애플리케이션 또는 모델 프로그램이 작업을 완료하고 종료될 때 호출되도록 설계되었습니다.
| 코드 | 기능 | 역할 |
| public static void stop () | 메서드 정의 | static 메서드로 정의되어 Application.stop() 형태로 객체 생성 없이 호출 가능하며, 반환 값은 없습니다. |
| printMsg("Tracker: Stopped"); | 종료 메시지 출력 | printMsg 메서드를 사용하여 프로그램이 정상적으로 종료되었음을 나타내는 문자열을 출력합니다. |
5-2 public static void printMsg(String Msg)
이 메서드는 메시지를 표준 출력(Standard Output), 즉 콘솔이나 로그 창에 출력하는 역할을 하는 래퍼(Wrapper) 함수입니다.
| 코드 | 기능 | 역할 |
| public static void printMsg(String Msg) | 메서드 정의 | 하나의 String 타입 인수(Msg)를 받습니다. |
| System.out.println(Msg); | 실제 출력 실행 | Java의 표준 출력 스트림을 사용하여 받은 문자열을 출력하고, 출력 후 줄 바꿈을 수행합니다. 이 메서드를 사용하면 긴 System.out.println 구문 대신 간결하게 printMsg를 사용하여 메시지를 출력할 수 있습니다. |
5-3 public static void title()
이 메서드는 프로그램이 시작될 때 호출되어 초기 정보와 상태 메시지를 사용자에게 표시합니다.
| 코드 | 기능 | 역할 |
| public static void title() | 메서드 정의 | 시작 메시지 출력을 위한 함수입니다. |
| printMsg(...) | 시작 정보 출력 | 프로그램의 목적, 사용된 개발 환경 (J-Link, Java 1.1), 그리고 로딩 중임을 알리는 안내 메시지 세 줄을 순서대로 출력합니다. |
Application.java는 PTC Creo Parametric의 J-Link API를 사용하여 Creo와 연결하고, Creo의 모델 정보(이름 포함)를 가져와 추적하는 기본적인 구조를 가지고 있습니다.
다음과 같은 세 가지 주요 구조적 요소로 구성되어 있습니다.
| 구조적 요소 | 코드 예시 | 기능 |
| 1. API 임포트 및 전역 변수 선언 | import com.ptc.pfc... | Creo J-Link API 사용을 위한 라이브러리 참조 및 프로그램 전반에서 공유할 **Creo 연결 객체(session)**와 **모델 목록 객체(models)**를 준비합니다. |
| public static Session session; | ||
| 2. Creo 연결 및 모델 로직 (진입점) | app_start() | Creo 세션을 획득하고, 모델 목록을 구성한 후, 실제 모델 처리 로직을 담은 Tracker 클래스를 실행합니다. 두 가지 다른 Creo 실행 시나리오에 대응하는 별도의 진입점입니다. |
| mp_start() | ||
| 3. 유틸리티 및 상태 관리 | stop() | 프로그램의 시작/종료 메시지 출력 및 일반적인 콘솔 출력을 담당하여 사용자에게 상태를 알립니다. |
| printMsg() | ||
| title() |
다음과 같은 메소드를 가지고 있습니다.
| 1. 시작 메서드 (Creo 연결 및 모델 로딩) | ||
| 메서드 | 용도 | 주요 기능 |
| app_start() | 일반 J-Link 애플리케이션 시작 | 1. Creo 세션(session)에 연결합니다 (GetCurrentSession...). 2. 현재 Creo에 열려 있는 모든 모델(models)을 가져옵니다 (ListModels()). 3. **Tracker**를 실행하여 모든 모델 정보를 처리합니다. |
| mp_start() | 모델 프로그램 시작 | 1. Creo 세션(session)에 연결합니다. 2. 현재 활성화된 단 하나의 모델만 가져옵니다 (GetCurrentModel()). 3. Tracker를 실행하여 해당 모델 정보를 처리하고, 성공 시 "Tracker: Complete" 메시지를 출력합니다. |
| 2. 유틸리티 및 상태 메서드 | ||
| 메서드 | 용도 | 주요 기능 |
| stop() | 프로그램 종료 알림 | "Tracker: Stopped" 메시지를 출력하여 프로그램의 종료 상태를 알립니다. |
| printMsg(String Msg) | 메시지 출력 래퍼 | 입력받은 문자열(Msg)을 표준 출력(System.out.println)에 출력하는 단순화된 메서드입니다. |
| title() | 시작 메시지 출력 | 프로그램의 이름, 개발 정보, 및 로딩 중임을 알리는 시작 안내 메시지를 출력합니다. |
by korealionkk@gmail.com
