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

Cycle through all curves to find intersections C++

Hello all, 

I am trying to cycle through a group of lines and curves (all on the same plane) and create points at all intersections.

 

It's been done in VB.NET here:

https://solutions.industrysoftware.automation.siemens.com/view.php?sort=desc&q=intersection+curves&d...

 

I am relatively new to coding (apologies) so i'm having trouble getting it into c++, any help would be greatly appreciated. 

11 REPLIES

Re: Cycle through all curves to find intersections C++

by chance, do you know the name of the sample file in this folder C:\Program Files\Siemens\NX 10.0\UGOPEN\SampleNXOpenApplications\.NET\NXOpenExamples?

Re: Cycle through all curves to find intersections C++

I cant find it in my files. I'm using NX9, that might be the reason.

Re: Cycle through all curves to find intersections C++

[ Edited ]

 

I think the sample program is

Sample NX Open .NET Visual Basic program : create named point at each curve intersection

Which you can also find by searching for nx_api4404.

 

(See attached.)  I would do it a bit differently if I did it now, because we have things like the ArrayList to make things like this simpler!

 

 

 

Re: Cycle through all curves to find intersections C++

@sebbytron can you attach a sample file or a pic? 

Re: Cycle through all curves to find intersections C++

Here's how far I've reached, I'm trying to write intersection points to the information window at the moment but plan to build a vector of intersection coords and then use that to create the points on the intersections. 

 

#if ! defined ( __hp9000s800 ) && ! defined ( __sgi ) && ! defined ( __sun )
#   include <strstream>
#   include <iostream>
#	include <vector>
    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 <NXOpen/ListingWindow.hxx>
#include <uf_obj.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <uf.h>
#include <uf_ui.h>
#include <uf_so.h>
#include <uf_view.h>
#include <uf_part.h>
#include <uf_assem.h>
#include <uf_obj.h>
#include <uf_modl.h>
#include <uf_object_types.h>
#include <uf_curve.h>
#include <algorithm>
#include <NXOpen/Point.hxx>
#include <NXOpen/PointCollection.hxx>
#include <NXOpen/Session.hxx>
#include <NXOpen/Part.hxx>
#include <NXOpen/PartCollection.hxx>

#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];

        sprintf(msg, "*** ERROR code %d at line %d in %s:\n+++ ",
            irc, line, file);
        UF_get_fail_message(irc, err);

        UF_print_syslog(msg, FALSE);
        UF_print_syslog(err, FALSE);
        UF_print_syslog("\n", FALSE);
        UF_print_syslog(call, FALSE);
        UF_print_syslog(";\n", FALSE);

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

    return(irc);
}


/*****************************************************************************
**  Activation Methods
*****************************************************************************/
/*  Unigraphics Startup
**      This entry point activates the application at Unigraphics startup */
extern DllExport void ufsta( char *param, int *returnCode, int rlen )
{
    /* Initialize the API environment */
    if( UF_CALL(UF_initialize()) ) 
    {
        /* Failed to initialize */
        return;
    }
    
    /* TODO: Add your application code here */


  tag_t object;
  tag_t work_part = UF_PART_ask_display_part();
  int type, subtype;
  int num_objects = 0;
  UF_CALL (UF_UI_open_listing_window() );
  		char buffer[UF_UI_MAX_STRING_LEN];

		
// while loop to cycle through all objects
  std::vector<tag_t>curves; //set up vector for all curves
  object=NULL_TAG;
	logical isdisplayable;
	char nameofline[UF_OBJ_NAME_LEN+1];
	UF_OBJ_disp_props_t disp;

	object = UF_OBJ_cycle_all(work_part, object);

  while (object != NULL_TAG)
  {
			UF_OBJ_ask_display_properties(object,&disp);
              UF_OBJ_ask_type_and_subtype(object, &type, &subtype);
			  UF_OBJ_is_displayable(object,&isdisplayable);
			  UF_OBJ_ask_name(object,nameofline);
//lists all objects, types , names	
 

			  sprintf_s(buffer, "object%d type %d, name: %s, disp?%d \n", object, type,nameofline,(int)isdisplayable);
              UF_CALL( UF_UI_write_listing_window(buffer) );
// build vector 'curves' out of 3s, 5s and 7s
			  if (type==3 || type== 5 || type==7)
			  {

				  curves.push_back(object);
				  UF_DISP_set_highlight(object,1);
			  }
				  object = UF_OBJ_cycle_all(work_part, object);
  }

  int size= sizeof(curves);
  sprintf_s(buffer, "there are %d curves in part \n",(int)curves.size());

		UF_CALL( UF_UI_write_listing_window(buffer) );


	int i,j;
	double rp[3];
	rp[0]=0.0;
	rp[1]=0.0;
	rp[2]=0.0;
	UF_CURVE_intersect_info_t intersect;
	int intype=intersect.type_of_intersection;
	double ip=intersect.curve_point[3];

	for( i=0;i<(int)curves.size()-1;i++){
		for(j=i+1; j<(int)curves.size(); j++)
		{
		UF_CURVE_intersect(curves[i],curves[j],rp,&intersect);//take first curve and intersect with curves[i]

		 //intersection type
		
		if (intype!=0);
		//ipts.push_back(ip);


		//sprintf_s(buffer, "object: object%d, type %d, subtype %d type_of_intersection is intype%d \n",object,type,subtype,intype);

		sprintf_s(buffer, "%d",ip);
              UF_CALL( UF_UI_write_listing_window(buffer) );

	}
	}

  //Add point at every intersection point

  //Divide curve over every point

    /* Terminate the API environment */
    int UF_terminate();
}
	/*****************************************************************************
**  Utilities
*****************************************************************************/

/* Unload Handler
**     This function specifies when to unload your application from Unigraphics.
**     If your application registers a callback (from a MenuScript item or a
**     User Defined Object for example), this function MUST return
**     "UF_UNLOAD_UG_TERMINATE". */
extern int ufusr_ask_unload( void )
{
    return( UF_UNLOAD_UG_TERMINATE );
}

Re: Cycle through all curves to find intersections C++

Just curious ... you have code that works written in VB, so why do you want to rewrite it in C++?? For an inexperienced programmer (or even an experienced one), C++ is a pretty difficult language.
山田
yamada

Re: Cycle through all curves to find intersections C++

[ Edited ]

> we have things like the ArrayList

 

Using ArrayList is generally not recommended, nowadays. We now have System.Generic.Collection.List(of type) which is type safe and usually gives better performance.

山田
yamada

Re: Cycle through all curves to find intersections C++

 

I posted this on the GTAC examples site recently:

 

/*****************************************************************************
**
** create_named_point_at_each_curve_intersection_using_interop.cpp
**
*****************************************************************************/
#include <stdio.h>
#include <string.h>
#include <uf.h>
#include <uf_ui.h>
#include <uf_defs.h>
#include <uf_obj.h>
#include <uf_modl.h>
#include <uf_curve.h>

#include <NXOpen/Session.hxx>
#include <NXOpen/NXException.hxx>
#include <NXOpen/BasePart.hxx>
#include <NXOpen/NXObject.hxx>
#include <NXOpen/Part.hxx>
#include <NXOpen/PartCollection.hxx>
#include <NXOpen/Update.hxx>
#include <NXOpen/Point.hxx>
#include <NXOpen/PointCollection.hxx>
#include <NXOpen/Curve.hxx>
#include <NXOpen/CurveCollection.hxx>
#include <NXOpen/IBaseCurve.hxx>
#include <algorithm>
#include <vector>

using namespace std;

using namespace NXOpen;

#include <stdarg.h>

#pragma warning(disable:4996)

static void ECHO(char *format, ...)
{
    char msg[UF_UI_MAX_STRING_LEN+1];
    va_list args;
    va_start(args, format);
    vsprintf(msg, format, args);
    va_end(args);
    UF_UI_open_listing_window();
    UF_UI_write_listing_window(msg);
    UF_print_syslog(msg, FALSE);
}

#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];

        UF_get_fail_message(irc, err);
        ECHO("*** ERROR code %d at line %d in %s:\n",
            irc, line, file);
        ECHO("+++ %s\n", err);
        ECHO("%s;\n", call);
    }

    return(irc);
}

static void create_intersection_points(tag_t c1, tag_t c2)
{
    int
        numInts = 0;
    double
        *data = NULL;
    static int
        pointCounter = 1;

    UF_CALL(UF_MODL_intersect_curve_to_curve(c1, c2, &numInts, &data));

    for(int inx= 0; inx < numInts; inx++)
    {
        double pntLoc[3] = { data[inx*5], data[(inx*5)+1], data[(inx*5)+2]};
        tag_t newPt = NULL_TAG;

        UF_CALL(UF_CURVE_create_point(pntLoc, &newPt));
        char pntName[MAX_ENTITY_NAME_SIZE+1];
        sprintf_s(pntName, sizeof(pntName), "POINT_%d", pointCounter);
        UF_CALL(UF_OBJ_set_name(newPt, pntName));

        pointCounter += 1;
    }

    if(numInts > 0) UF_free(data);
}

static void do_it(void)
{
    Session *theSession = Session::GetSession();
    BasePart *displayPart = theSession->Parts()->Display();
    Part *dp = theSession->Parts()->Display();

    if(NULL_TAG == displayPart->Tag()) return;

    CurveCollection *allCurves = dp->Curves();
    
    vector<Curve> backward;
    vector<Curve> forward;
   
    for(NXOpen::CurveCollection::iterator it = allCurves->begin();
        it != allCurves->end(); it++)
    {
        Curve *thisCrv = dynamic_cast<Curve *>(*it);
        forward.push_back(*thisCrv);
        backward.push_back(*thisCrv);
    }

    std::reverse(backward.begin(), backward.end());

    int inxArraySize = (int)forward.size();
    int jnxArraySize = (int)backward.size();

    int inx = 0, jnx = 0;

    for(inx = 0; inx < inxArraySize; inx++)
    {         
        tag_t c1 = forward.at(inx).Tag();   

        for(jnx = 0; jnx < jnxArraySize; jnx++)
        {
            tag_t c2 = backward.at(jnx).Tag();
             
            if(c1!=c2)
            {
                create_intersection_points(c1, c2);
            }
        }
                 
        if((int) backward.size() > 1 ) backward.pop_back();
        jnxArraySize = (int)backward.size();     
    }

    return;
    
}

void ufusr(char *param, int *retcode, int paramLen)
{
    if (UF_CALL(UF_initialize())) return;
    do_it();
    UF_terminate();
}

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

Re: Cycle through all curves to find intersections C++

This function is part of a larger project, and one of the constraints is that I have to do it in c++