블로그 이미지
진연
3차원 CAD프로그램인 UGS에서 지원하는 API를 이용하여 프로그램하는 방법등을 소개하는 블로그입니다. 혹시 연락이 필요하신분은 youni7311@hanmail.net로 메일 보내주세요..

calendar

1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
3. 마우스로 라인그리기(1)

안녕하세요.. 벌써 세번째 네요...^^
이전 강좌에서 예고 했듯이 오늘은 저번에 line을 생성하는 방법을 가지고 응용해 보겠습니다.

지난번에 만든 코드는 라인을 그리긴 그렸는데... 좌표를 바꾸고 싶으면 소스 레벨에서 값을 변경해야되고, 불편한게 이만저만이 아닙니다.

그래서, 오늘은 마우스를 이용해서 라인을 생성해 보도록 하겠습니다.. 전체 코드는 아래와 같습니다.

/*****************************************************************************
**
** basic_line.cpp
**
** Description:
** Contains Unigraphics entry points for the application.
**
*****************************************************************************/

#if ! defined ( __hp9000s800 ) && ! defined ( __sgi ) && ! defined ( __sun )
# include <strstream>
# include <iostream>
using std::ostrstream;
using std::endl;
using std::ends;
using std::cerr;
#else
# include <strstream.h>
# include <iostream.h>
#endif

#include <uf.h>
#include <uf_ui.h>
#include <uf_exit.h>
#include <uf_curve.h>

#include <uf_ui.h>

#define UF_CALL(X) (report_error( __FILE__, __LINE__, #X, (X)))

static int report_error( char *file, int line, char *call, int irc)
{
   if (irc)
   {
      char err[133], msg[133];

      UF_get_fail_message(irc, err);
      sprintf(msg, "error %d at line %d in %s", irc, line, file);

      if (!UF_UI_open_listing_window())
      {
          UF_UI_write_listing_window(err);
          UF_UI_write_listing_window("\n");
          UF_UI_write_listing_window(msg);
          UF_UI_write_listing_window("\n");
          UF_UI_write_listing_window(call);
          UF_UI_write_listing_window(";\n\n");
      }
      else {}
   }

   return(irc);
}

extern DllExport void ufsta( char *param, int *returnCode, int rlen )
{
    int rtn;

    UF_CURVE_line_t line_coords;
    tag_t line_tag;


    int mode[2] = {0,0};
    double cp[3];


    if( UF_CALL(UF_initialize()) ) {
         return;
    }

    rtn = uc1616 ("Select start point", mode, 0, cp);
    if(rtn == 1 || rtn == 2) goto END_SUB;

    line_coords.start_point[0] = cp[0];
    line_coords.start_point[1] = cp[1];
    line_coords.start_point[2] = cp[2];

    rtn = uc1616 ("Select end point", mode, 0, cp);
    if(rtn == 1 || rtn == 2) goto END_SUB;

    line_coords.end_point[0] = cp[0];
    line_coords.end_point[1] = cp[1];
    line_coords.end_point[2] = cp[2];


    rtn = UF_CALL(UF_CURVE_create_line (&line_coords, &line_tag));

END_SUB:
    UF_CALL(UF_terminate());
}

extern int ufusr_ask_unload( void )
{
    return( UF_UNLOAD_UG_TERMINATE );
}

이번에는 설명하는 방식을 바꿔서 위에다 주석을 달지 않고, 새로 추가되거나 변경된 내용을 위주로 설명하겠습니다. 그럼 하나 하나 뜯어 볼까요..?

- "curve.h" 파일에 이어서 또 하나의 Header File을 추가 했습니다. 이 Header File을 User Interface의 앞글자를 사용했습니다. 더 이상 설명이 필요없는것 같네요.
말 그대로 User Interface에 관련된 함수들이 모여 있는 파일입니다.

#include <uf_ui.h>

- 아래 본문에서 사용하는 변수들의 선언입니다.
int mode[2] = {0,0};
double cp[3];

- 이 넘이 새래 추가된 함수입니다. uc1616 도데체 이름만 보고서는 알수가 없는.... 이런 함수들을 UG에서는 Legacy 함수라고 합니다. 그 옛날 유닉스 시절부터 대대로 물려 내려온... 그건 그렇고 아래 그림을 한번 보죠.


바로 이 넘 입니다. uc1616을 실행하면 위 그림과 같이 "Point Constructor" Dialog가 실행되서 UG화면상에 한 점을 선택 할 수 있고, 그 좌표값을 받아올 수 있습니다. 아래 코드에서는 cp라는 변수에 선택된 한점의 위치를 가져옵니다.

uc1616에 대한 자세한 내용은 UG NX3 Document -> NX Open -> Open C Reference Guide를 읽어보시기 바랍니다.
rtn = uc1616 ("Select start point", mode, 0, cp);
if(rtn == 1 || rtn == 2) goto END_SUB;

- uc1616에 의해 받아온 좌표를 start_point에 대입 합니다.
line_coords.start_point[0] = cp[0];
line_coords.start_point[1] = cp[1];
line_coords.start_point[2] = cp[2];

- 마찬가지로 uc1616으로 한 점을 받아와 end_point에 대입 합니다.
rtn = uc1616 ("Select end point", mode, 0, cp);
if(rtn == 1 || rtn == 2) goto END_SUB;

line_coords.end_point[0] = cp[0];
line_coords.end_point[1] = cp[1];
line_coords.end_point[2] = cp[2];

- 받아온 두 점을 라인을 생성하는 함수에 넣으면 끝....
rtn = UF_CALL(UF_CURVE_create_line (&line_coords, &line_tag));

알고보면 너무 쉽지 않습니까..?

조금만 바꾸면 Poly Line을 그릴 수 있는데 코드는 아래와 같습니다.

extern DllExport void ufsta( char *param, int *returnCode, int rlen )
{
    int rtn;

    UF_CURVE_line_t line_coords;
    tag_t line_tag;

    int mode[2] = {0,0};
    double cp[3];

    if( UF_CALL(UF_initialize()) ) {
         return;
    }

    rtn = uc1616 ("Select start point", mode, 0, cp);
    if(rtn == 1 || rtn == 2) goto END_SUB;

    line_coords.start_point[0] = cp[0];
    line_coords.start_point[1] = cp[1];
    line_coords.start_point[2] = cp[2];

    while(1) {

       rtn = uc1616 ("Select end point", mode, 0, cp);
       if(rtn == 1 || rtn == 2) break;

       line_coords.end_point[0] = cp[0];
       line_coords.end_point[1] = cp[1];
       line_coords.end_point[2] = cp[2];

      rtn = UF_CALL(UF_CURVE_create_line (&line_coords, &line_tag));

       line_coords.start_point[0] = line_coords.end_point[0];
       line_coords.start_point[1] = line_coords.end_point[1];
       line_coords.start_point[2] = line_coords.end_point[2];

   }

END_SUB:
    UF_CALL(UF_terminate());
}

오늘은 여기까지 하겠습니다. 지금 보여드린 코드가 완전한 프로그램은 아닙니다.
아직 해야 할일이 많습니다. 아직 에러도 있고...^^

다음에는 발생할 수 있는 에러를 처리하는 방법과 시간이 되면 좀 더 CAD프로그램답게 업그레이드를 해보겠습니다.

그럼 행운을 빌겠습니다...

posted by 진연