Showing results for 
Search instead for 
Do you mean 
Reply
Solved! Go to solution

NX Open C/C++ API : Select body and display density

Hello,

 

I am a beginner trying to learn how to create NX applications with Open C++ API. I am currently trying to create an application to select a single solid body and display its density in an information window. I have included my code below.

 

The code builds and runs without error. However, it displays an incorrect density value. It displays the same value "Density = 1105006.000000" irrespective of the solid model used.

 

I refered to Open C Reference Manual website for the implementation of the functions used. Any help on how to correct this problem would be much appreciated. Thanks.

 

 

 

//Required header files
#include<stdio.h>
#include<uf.h>
#include<uf_ui.h>
#include<uf_object_types.h>
#include<uf_disp.h>
#include<uf_modl.h>
#include<uf_modl_types.h>

// Error Handling Function
#define UF_CALL(X) (report( __FILE__, __LINE__, #X, (X)))

static int report(char *file, int line, char *call, int irc)
{
	if (irc)
	{
		char    messg[133];
		printf("%s, line %d:  %s\n", file, line, call);
		(UF_get_fail_message(irc, messg)) ? printf("returned a %d\n", irc) : printf("returned error %d:  %s\n", irc, messg);
	}
	return(irc);
}

// Pre-declaring the selection initialization function
static int init_proc(UF_UI_selection_p_t select, void *user_data);

// Function to implement the application
static void do_ugopen_api() {
	
	// Variable initialization for UF_UI_select_with_single_dialog()
	char *msg = "Select body";
	char *title = "Body Density";
	int response;
	tag_t object, view;
	double *density = 0;
	int off = 0;	
	double cursor[3];

	// Buffer for Listing window message
	char buffer[UF_UI_MAX_STRING_BUFSIZE];

	// If there is no error during UF function call, print body density to the information window
	if (!UF_CALL( UF_UI_select_with_single_dialog ( msg, title, UF_UI_SEL_SCOPE_WORK_PART, init_proc,nullptr, &response, &object, cursor, &view))) {

		if (response == UF_UI_OBJECT_SELECTED || response == UF_UI_OBJECT_SELECTED_BY_NAME)	{
						
				UF_CALL(UF_UI_open_listing_window()); // Open Listing Window
				
				// Variable initialization for the UF_MODL_ask_body_density function
				double ans = UF_MODL_ask_body_density ( object, UF_MODL_grams_centimeters, density);
				snprintf(buffer, UF_UI_MAX_STRING_BUFSIZE, "======================================\n\tDensity = %f\n======================================", ans);
				UF_UI_write_listing_window(buffer);
			
			}
			/* unhighlight selected object */
			UF_DISP_set_highlight(object, off);
		
		
	}

}

// Initialization procedure function
static int init_proc(UF_UI_selection_p_t select, void *user_data) {
	UF_UI_mask_t mask_triples[] = { UF_solid_type, 0, 0 };
	int num_triples = 1;

	if ((UF_CALL(UF_UI_set_sel_mask(select,	UF_UI_SEL_MASK_CLEAR_AND_ENABLE_SPECIFIC,num_triples, mask_triples))) == 0)
	{
		return (UF_UI_SEL_SUCCESS);
	}
	else
	{
		return (UF_UI_SEL_FAILURE);
	}
}

// Main Function:
void ufusr(char *param, int *retcode, int param_len)
{
	if (!UF_CALL(UF_initialize()))
	{
		do_ugopen_api();
		UF_CALL(UF_terminate());
	}
}

int ufusr_ask_unload(void)
{
	return (UF_UNLOAD_IMMEDIATELY);
}

 

2 REPLIES
Solution
Solution
Accepted by topic author sushmit
‎09-09-2016 10:09 AM

Re: NX Open C/C++ API : Select body and display density

UF_MODL_ask_body_density() does not return a double on the left of the equals sign, so the "ans" variable is not what you want. instead it writes it into the double that the third parameter points to.

 

Try this instead:

 

double density = 0.0;

. . .

UF_MODL_ask_body_density ( object, UF_MODL_grams_centimeters, &density);

 

Then print out the value of "density".

 

That worked for me.

 

Re: NX Open C/C++ API : Select body and display density

It worked. Thank you. Smiley Happy