/*
	Copyright (c) 2024 PTC Inc. and/or Its Subsidiary Companies. All Rights Reserved.
*/

#include <ProToolkit.h>
#include <ProDtlgroup.h>

/*====================================================================*\ 
FUNCTION : UsrGroupCreate() 
PURPOSE  : Command to create a new group with selected items 
\*====================================================================*/ 
int UsrGroupCreate()
 {
     ProError status;
     ProSelection *sel;
     int n_sel, s;
     ProModelitem *items;
     ProDrawing drawing;
     ProName name;
     ProDtlgroupdata gdata;
     ProDtlgroup group;
     ProFileName msgfil;

/*--------------------------------------------------------------------*\ 
    Select notes, draft entities, symbol instances 
\*--------------------------------------------------------------------*/     
     ProStringToWstring (msgfil, "msg_ugdrawing.txt");

     ProMessageDisplay(msgfil,"USER Select the items to belong to the group");
     status = ProSelect("any_note,draft_ent,dtl_symbol",-1,
                 NULL,NULL,NULL,NULL,&sel,&n_sel);

     if(status != PRO_TK_NO_ERROR || n_sel < 1)
         return(0);

/*--------------------------------------------------------------------*\ 
    Allocate and fill a ProArray with the detail item handles 
\*--------------------------------------------------------------------*/     
     ProArrayAlloc(n_sel, sizeof(ProModelitem), 1, (ProArray*)&items);
     for(s=0;s<n_sel;s++)
       {
         status = ProSelectionModelitemGet(sel[s], &items[s]);
	 ProTKPrintf ("  **** SEL MDLITEM GET: %d *****\n", status);
	 ProTKPrintf ("  %%%% MDLITEM %d TYPE: %d %%%%%\n", items[s].id, items[s].type);
       }
/*--------------------------------------------------------------------*\ 
    Get the current drawing 
\*--------------------------------------------------------------------*/     
     ProMdlCurrentGet((ProMdl*)&drawing);

/*--------------------------------------------------------------------*\ 
    Prompt for the group name 
\*--------------------------------------------------------------------*/     
     ProMessageDisplay(msgfil,"USER Enter group name");

     if(ProMessageStringRead(PRO_NAME_SIZE, name) != PRO_TK_NO_ERROR)
         return(0);

     ProTKPrintf ("About to create\n");

/*--------------------------------------------------------------------*\ 
    Allocate group data 
\*--------------------------------------------------------------------*/     
     status = ProDtlgroupdataAlloc(drawing, name, &gdata);
     ProTKPrintf ("  **** DTLGROUP DATA ALLOC: %d *****\n", status);

/*--------------------------------------------------------------------*\ 
    Set the group items 
\*--------------------------------------------------------------------*/     
     status = ProDtlgroupdataItemsSet(gdata, items);
     ProTKPrintf ("  **** DTLGROUP DATA SET: %d *****\n", status);

/*--------------------------------------------------------------------*\ 
    Create the group 
\*--------------------------------------------------------------------*/     
     status = ProDtlgroupCreate(drawing, gdata, &group);
     ProTKPrintf ("  **** DTLGROUP CREATE: %d *****\n", status);
     

/*--------------------------------------------------------------------*\ 
    Free the data 
\*--------------------------------------------------------------------*/     
     ProDtlgroupdataFree(gdata);
     ProArrayFree((ProArray*)&items); 
}