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



/*---------------------- Pro/Toolkit Includes ------------------------*/

#include <ProToolkit.h>
#include <ProMenu.h>
#include <ProMdl.h>
#include <ProIntfimport.h>

#include <ProUI.h>
#include <ProUIDialog.h>
#include <ProUIPushbutton.h>
#include <ProUIInputpanel.h>
#include <ProMessage.h>
#include <ProUtil.h>

/*---------------------- Application Includes ------------------------*/
#include <TestError.h>
#include <UgUtilUI.h>
#include <UtilString.h>

#define MSGFIL L##"msg_uginterface.txt"

int UserIGESImportNewModelViaFilter(ProPath input_file, 
									ProMdlName new_model_name, 
									ProName excluded_layer, 
									ProMdl* model);
/*====================================================================*\
FUNCTION: UserIGESImportViaFilter()
PURPOSE:  Import an IGES model into session and display it 
\*====================================================================*/
int UserIGESImportViaFilter()
{
  	ProError status;
	ProMdl model;
	ProPath input_file;
	ProMdlName new_model_name;
	ProName excluded_layer;
	ProLine filter_string;
	wchar_t* input;

/*--------------------------------------------------------------------*\
	Prompt for IGES file to import
\*--------------------------------------------------------------------*/
	ProMessageDisplay (MSGFIL, "USER Select an IGES file to import");

	ProStringToWstring (filter_string, "*.igs");
	status = ProFileMdlnameOpen (NULL, filter_string, NULL, NULL, NULL, NULL, 
							input_file);
	if (status != PRO_TK_NO_ERROR)
		return status;

/*--------------------------------------------------------------------*\
	Prompt for new model name
\*--------------------------------------------------------------------*/
	status = UserPromptUI ("Create new model", 
							"Enter new model name for IGES import", 
							PRO_NAME_SIZE, &input);
	if (status != PRO_TK_NO_ERROR)
		return status;
	else
	{
		ProUtilWstrcpy (new_model_name, input);
		ProWstringFree (input);
	}

/*--------------------------------------------------------------------*\
	Prompt for the name of a layer that should be blanked
\*--------------------------------------------------------------------*/
	status = UserPromptUI ("Import layer filter", 
							"Enter name of imported layer to blank", 
							PRO_NAME_SIZE, &input);
	if (status != PRO_TK_NO_ERROR)
		return status;
	else
	{
		ProUtilWstrcpy (excluded_layer, input);
		ProWstringFree (input);
	}

	status = UserIGESImportNewModelViaFilter (input_file, new_model_name, 
												excluded_layer, &model);
		
	if (status == PRO_TK_NO_ERROR)
	{
		ProMdlDisplay (model);
	}
	
	return(PRO_TK_NO_ERROR);	
}


/*====================================================================*\
STRUCTURE: UserImportLayerFilterdata
PURPOSE:   Contains user data to be passed to the import layer filter 
				function.
\*====================================================================*/
typedef struct 
{
	ProName find_layer;
} UserImportLayerFilterdata;

/*====================================================================*\
FUNCTION: UserLayerFilter
PURPOSE:  Blanks and renames an unneeded layer
\*====================================================================*/
ProError UserLayerFilter  ( ProLayerfilterdata data, 
					       ProAppData  application_data)
{
	ProName layer_name;
	ProError status;
	UserImportLayerFilterdata* filterdata = 
				(UserImportLayerFilterdata*)application_data;

/*--------------------------------------------------------------------*\
	Check if the current layer is the target
\*--------------------------------------------------------------------*/
	status = ProLayerfilterdataNameGet (data, layer_name);

	if (ProUtilWstrcmp (filterdata->find_layer, layer_name) == 0)
	{
/*--------------------------------------------------------------------*\
	Blank the found layer
\*--------------------------------------------------------------------*/
		status = ProLayerfilterdataActionSet (data, 
												PRO_LAYER_IMPORT_BLANK);

/*--------------------------------------------------------------------*\
	Rename the found layer
\*--------------------------------------------------------------------*/
		status = ProLayerfilterdataNameSet (data, L"FOUND");
	}

	return PRO_TK_NO_ERROR;
}


/*====================================================================*\
FUNCTION: UserIGESImportNewModelViaFilter()
PURPOSE:  Imports an IGES assembly file to an new model while filtering layers
\*====================================================================*/
int UserIGESImportNewModelViaFilter(ProPath input_file, 
									ProMdlName new_model_name, 
									ProName excluded_layer, 
									ProMdl* model)
{
	ProError status;
	ProMdl created_model;
        ProIntfImportType import_type = PRO_INTF_IMPORT_IGES;
        ProMdlType mdl_type;

/*--------------------------------------------------------------------*\
	Set up the layer filter data - search for needed layer
\*--------------------------------------------------------------------*/
	UserImportLayerFilterdata data;
	ProUtilWstrcpy (data.find_layer, excluded_layer);

	*model = NULL;

/*--------------------------------------------------------------------*\
	Check for proper import model type
\*--------------------------------------------------------------------*/	
        status = ProIntfimportSourceTypeGet (input_file, import_type, &mdl_type);
        
         if (status != PRO_TK_NO_ERROR)
         	return PRO_TK_GENERAL_ERROR;
                
/*--------------------------------------------------------------------*\
	Import IGES file
\*--------------------------------------------------------------------*/
	status = ProIntfimportModelWithOptionsMdlnameCreate(input_file, NULL,
					   import_type, 
					   mdl_type, PRO_IMPORTREP_MASTER,
					   new_model_name, 
					   UserLayerFilter, &data,
					   &created_model);

	if (status == PRO_TK_NO_ERROR)
	{
		*model = created_model;
		return PRO_TK_NO_ERROR;
	}
	else
		return status;
}

#undef MSGFIL