Ich verwende NXOpen++ mit Microsoft Visual Studio 2005 unter Windows XP SP2. Ich habe einen Server mit einer Socket-Kommunikation
entwickelt, um Daten von einem externen Programm zu bekommen. Das Programm läuft interaktiv. Der Socket-Server läuft als Thread, damit
man mit NXOpen weiter arbeiten kann, wenn das Programm läuft. Wenn ich eine prt-Datei innerhalb von dem Thread öffne, bekomme ich
eine Ausnahme: "Fatal error detected unable to continue". Ausserhalb von dem Thread funktioniert es alles wunderbar.
Das Problem tritt bei NXOpen::Part *pPart = theSession->GetParts()->OpenDisplay( "C:\\NXPitCon\\parts\\axis.prt", &pStatus ) auf.
Am Ender habe ich den Code eingefügt.
Vielen Dank im voraus.
Vincent
#define BUF_SIZE 64
/* Include files */
#include <sstream>
#include <winsock2.h>
#if ! defined ( __hp9000s800 ) && ! defined ( __sgi ) && ! defined ( __sun )
# include <strstream>
# include <iostream>
using std: strstream;
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>
#define UF_CALL(X) (report_error( __FILE__, __LINE__, #X, (X)))
#include <NXOpen/Session.hxx>
#include <NXOpen/Part.hxx>
#include <NXOpen/PartCollection.hxx>
#include <NXOpen/NXString.hxx>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
HANDLE _pipeHandle;
int StartWinsock()
{
WSADATA wsa;
int ret = WSAStartup( MAKEWORD( 2, 0 ), &wsa );
return ret;
}
int CreatePipe()
{
LPTSTR lpszPipename = TEXT("\\\\.\\pipe\\bbToCatia");
int bufsize = 64;
_pipeHandle = CreateNamedPipe(
lpszPipename, // pipe name
PIPE_ACCESS_DUPLEX | // read/write access
FILE_FLAG_OVERLAPPED, // overlapped mode
PIPE_TYPE_MESSAGE | // message-type pipe
PIPE_READMODE_MESSAGE | // message read mode
PIPE_NOWAIT, // blocking mode
PIPE_UNLIMITED_INSTANCES, // unlimited instances
bufsize*sizeof(TCHAR), // output buffer size
bufsize*sizeof(TCHAR), // input buffer size
0, // client time-out
NULL ); // default security attributes
if ( _pipeHandle == INVALID_HANDLE_VALUE )
{
return -1;
}
return 0;
}
void DisconnectAndClosePipe()
{
DisconnectNamedPipe( _pipeHandle );
// Close the handle to the pipe instance.
CloseHandle(_pipeHandle);
}
DWORD WINAPI ThreadProc( void *par )
{
long rc;
SOCKET acceptSocket;
SOCKET connectedSocket;
SOCKADDR_IN addr;
char buf[BUF_SIZE + 1];
// Start Winsock
rc = StartWinsock();
if (rc != 0)
{
return -1;
}
// Create Socket
acceptSocket = socket( AF_INET, SOCK_STREAM, 0 );
if ( acceptSocket == INVALID_SOCKET )
{
return -1;
}
// Bind Socket
memset( &addr, 0, sizeof( SOCKADDR_IN ) );
addr.sin_family = AF_INET;
addr.sin_port = htons( 3141 );
addr.sin_addr.s_addr = ADDR_ANY;
rc = bind( acceptSocket, (SOCKADDR*)&addr, sizeof( SOCKADDR_IN ) );
if ( rc == SOCKET_ERROR )
{
return -1;
}
// Set Listen Modus
rc = listen( acceptSocket, 10 );
if ( rc == SOCKET_ERROR )
{
return -1;
}
for ( ;; )
{
connectedSocket = accept( acceptSocket, NULL, NULL );
if( connectedSocket == INVALID_SOCKET )
{
return -1;
}
// Create Pipe
CreatePipe();
// Receive Data
std: stringstream sout;
int read = 0;
rc = recv( connectedSocket, buf, BUF_SIZE, 0 );
read += rc;
while ( rc != SOCKET_ERROR )
{
if ( rc == 0 )
{
cout << "Server closed connection." << endl;
break;
}
if( rc == SOCKET_ERROR )
{
cout << "Receive error. Error#: %d" << WSAGetLastError() << endl;
break;
}
buf[rc] = '\0';
sout << buf;
rc = recv(connectedSocket, buf, BUF_SIZE, 0);
if(rc == SOCKET_ERROR)
{
cout << "Receive error. Error#: " << WSAGetLastError() << endl;
break;
}
read += rc;
}
NXOpen::NXString message = "";
if (rc == SOCKET_ERROR)
{
///@todo create global constants file and add Parameter "SOCKET_ERROR"
message = "SOCKET_ERROR";
}
else
{
std::string output = sout.str();
message = output.c_str();
cout << "message=" << message.GetText() << endl;
NXOpen::Session *theSession = NXOpen::Session::GetSession();
NXOpen::PartLoadStatus *pStatus;
NXOpen::Part *pPart = theSession->GetParts()->OpenDisplay( "C:\\NXPitCon\\parts\\axis.prt", &pStatus );
/* Set Undo mark */
NXOpen::Session::UndoMarkId session_UndoMarkId1;
session_UndoMarkId1 = theSession->SetUndoMark(NXOpen::Session::MarkVisibilityVisible, "NXPitcon_Server");
}
// Close Pipe
DisconnectAndClosePipe();
}
WSACleanup();
return 0;
}
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 */
// Socket
DWORD dwThreadParam = 1;
DWORD dwThreadID;
HANDLE hThread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)ThreadProc, &dwThreadParam, 0, &dwThreadID );
CloseHandle( hThread );
/* Terminate the API environment */
UF_CALL(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 );
}
Eine Antwort auf diesen Beitrag verfassen (mit Zitat/Zitat des Beitrags) IP