블로그 이미지
진연
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

'NX Open for C/C++'에 해당되는 글 13

  1. 2007.03.12 Block 만들기
2007. 3. 12. 20:13 NX Open for C/C++/Open Source
아래 프로그램 소스는 Diameter = 20mm인 원을 생성하고, 입력한 Vertex 갯수 만큼의 점을 원에서 축출하여 Polyline으로 연결하고, 높이가 20mm인 블럭을 생성하는 코드입니다.

백문이 불여일견이라고, 직접 만들어서 실행해 보시면 무엇인지 바로 알 수 있습니다..^^

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

/* Include files */
#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_modl.h>
#include <uf_obj.h>
#include <uf_vec.h>
#include <uf_csys.h>
#include <uf_part.h>


tag_t create_circle(double center[3], double diameter);
void get_end_point_of_curve(tag_t cv_tag, double cv_ends[6]);
int get_points_by_equal_arc_length(tag_t cv_tag, int num_pts, double (**pptpts)[3]);
tag_t create_line(double start_pts[3], double end_pts[3]);
int create_poly_line(int num_pts, double (*ptpts)[3], tag_t **ppline_tags, int is_close);
tag_t create_body_by_extrude(int num_obj, tag_t* pobj_tags, double limits[2], double dir[3]);

void create_block();

extern DllExport void ufusr( char *parm, int *returnCode, int rlen )
{
    int errorCode = UF_initialize();

    if ( 0 == errorCode ) {

       //------------------------------------------------------------------
       //------------------------------------------------------------------
       create_block();
 
       errorCode = UF_terminate();
    }  
}

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

/*
===============================================================================

===============================================================================
*/
void create_block()
{
    int rtn=0;

    double diameter = 20.;
    double center[3] = {0.};

    tag_t circle_tag;

    int num_vertex = 6;
    double (*pvertex_pts)[3] = NULL;

    int num_line;
    tag_t *pline_tags=NULL;

   double limit[2] = {0, 20.};
   double dir[3] = {0.,0.,1.};
   tag_t body_tag;

   //------------------------------------------------------------------
   //원을 생성한다.
   //------------------------------------------------------------------
   circle_tag = create_circle(center, diameter);
   if(circle_tag == NULL) goto END_SUB;

   //------------------------------------------------------------------
   //입력한 갯수만큼 등각격으로 커브위에 포인트르 가져온다.
   //------------------------------------------------------------------
   rtn = get_points_by_equal_arc_length(circle_tag, num_vertex, &pvertex_pts);
   if(rtn != 0) goto END_SUB;

   //------------------------------------------------------------------
   //poly line 생성
   //------------------------------------------------------------------
   num_line = create_poly_line(num_vertex, pvertex_pts, &pline_tags, true);
   if(num_line == 0) goto END_SUB;

   //------------------------------------------------------------------
   //extrude
   //------------------------------------------------------------------
   body_tag = create_body_by_extrude(num_line, pline_tags, limit, dir);
   if(body_tag == NULL)  goto END_SUB;

  //------------------------------------------------------------------
  //------------------------------------------------------------------
  END_SUB:

    if(pvertex_pts != NULL)
       delete[] pvertex_pts;
    pvertex_pts = NULL;

    if(pline_tags != NULL)
       delete[] pline_tags;
    pline_tags = NULL;

 return;
}

/*
===============================================================================

===============================================================================
*/
tag_t create_circle(double center[3], double diameter)
{
   int rtn;
   double org[3];
   tag_t wcs_tag, mtx_tag;
   tag_t circle_tag = NULL;

   UF_CURVE_arc_t arc_data = { NULL_TAG, 0, TWOPI, { 0,0,0 }, 1.0 };

   UF_CSYS_ask_wcs(&wcs_tag);
   UF_CSYS_ask_csys_info(wcs_tag, &mtx_tag, org);

   arc_data.matrix_tag = mtx_tag;
 
   UF_VEC3_copy(center, arc_data.arc_center);
   arc_data.radius = diameter / 2.0;

   rtn = UF_CURVE_create_arc(&arc_data, &circle_tag);
   if(rtn != 0) return NULL;
   
   return circle_tag;
}

/*
===============================================================================

===============================================================================
*/
void get_end_point_of_curve(tag_t cv_tag, double cv_ends[6])
{  
    int num_pts;
    double *ptpts = NULL;
    double ctol=0., atol=0., stol=0;

    UF_MODL_ask_curve_points (cv_tag, ctol, atol, stol, &num_pts, &ptpts);
 
    UF_VEC3_copy(&ptpts[0], &cv_ends[0]);
    UF_VEC3_copy(&ptpts[(num_pts-1)*3], &cv_ends[3]);

    if(ptpts != NULL)
       UF_free(ptpts);
    ptpts = NULL;

    return;
}

/*
===============================================================================

===============================================================================
*/
int get_points_by_equal_arc_length(tag_t cv_tag, int num_pts, double (**pptpts)[3])
{
    int i;
    int part_units;

    double dl, dp, junk[3];
    double arc_length;
    double cv_ends[6];

    double (*ptpts)[3] = NULL;
 
    tag_t part_tag;
    UF_MODL_units_t unit_flag;

   //------------------------------------------------------------------
   //------------------------------------------------------------------
   part_tag = UF_PART_ask_display_part();

   //------------------------------------------------------------------
   //------------------------------------------------------------------
   UF_PART_ask_units (part_tag, &part_units );
   if(part_units == UF_PART_METRIC)
       unit_flag = UF_MODL_MMETER;
   else unit_flag = UF_MODL_INCH;
   
   //------------------------------------------------------------------
   // spline의 total arc length
   //------------------------------------------------------------------
   UF_CURVE_ask_arc_length (cv_tag, 0.0, 1.0, unit_flag, &arc_length);
 
   //------------------------------------------------------------------
   //------------------------------------------------------------------
   get_end_point_of_curve(cv_tag, cv_ends);

   //------------------------------------------------------------------
   //------------------------------------------------------------------
   dl =  arc_length / num_pts;

   //------------------------------------------------------------------
   //allocate(ptpts)
   //------------------------------------------------------------------
   ptpts = new double[num_pts][3];

   //get points
   for(i = 0; i < num_pts; i++) {      
       UF_MODL_ask_point_along_curve (&cv_ends[0], cv_tag, dl * i, 1, 0.0254, &dp);
       UF_MODL_ask_curve_props (cv_tag, dp,  ptpts[i], junk, junk, junk, junk, junk);
   }
 
   //------------------------------------------------------------------
   //return points
   //------------------------------------------------------------------
   *pptpts = ptpts;  

   //------------------------------------------------------------------
   //------------------------------------------------------------------
   return 0;
}

/*
===============================================================================

===============================================================================
*/
tag_t create_line(double start_pts[3], double end_pts[3])
{
   tag_t line_tag;
   UF_CURVE_line_t line_coords;

   UF_VEC3_copy(start_pts, line_coords.start_point);
   UF_VEC3_copy(end_pts, line_coords.end_point);

   UF_CURVE_create_line(&line_coords, &line_tag);

   return line_tag;
}

/*
===============================================================================

===============================================================================
*/
int create_poly_line(int num_pts, double (*ptpts)[3], tag_t **ppline_tags, int is_close)
{
   int i;

   int num_line;
   tag_t *pline_tags = NULL;

   if(is_close == true)
      num_line = num_pts;
   else num_line = num_pts-1;
 
    pline_tags = new tag_t[num_line];
 
   if(is_close == true) {
      for(i = 0; i < num_line-1; i++)
          pline_tags[i] = create_line(ptpts[i], ptpts[i+1]);
      pline_tags[num_line-1] = create_line(ptpts[num_pts-1], ptpts[0]);

  } else {
      for(i = 0; i < num_line; i++)
         pline_tags[i] = create_line(ptpts[i], ptpts[i+1]);
   }

   *ppline_tags = pline_tags;
   return num_line;
}

/*
===============================================================================
===============================================================================
*/
tag_t create_body_by_extrude(int num_obj, tag_t* pobj_tags, double limits[2], double dir[3])
{
   int i, rtn;
   double junk[3];

   char ch_limit1[2][UF_MAX_EXP_LENGTH];
   char *ch_limit2[2];

   tag_t body_tag = NULL;

   uf_list_t *pobject_list;
   uf_list_t *pfeat_list;
 
   UF_MODL_create_list(&pobject_list);
   for(i = 0; i < num_obj; i++)
       UF_MODL_put_list_item(pobject_list, pobj_tags[i]);

   for(i = 0; i < 2; i++) {
     sprintf(ch_limit1[i], "%f", limits[i]);
     ch_limit2[i] = ch_limit1[i];
   }

   rtn = UF_MODL_create_extruded (pobject_list, "0", ch_limit2, junk, dir, UF_NULLSIGN,

                                                  &pfeat_list);
 
   UF_MODL_delete_list(&pobject_list);
   UF_MODL_delete_list(&pfeat_list);

   return body_tag;
}

posted by 진연