Showing results for 
Search instead for 
Do you mean 
Reply

NXOpen: setting values to UDO results in memory violation

[ Edited ]

Hello. 

I am trying to create an User Defined Object which displays a rectangle on screen.

I am using the project SimpleUDO as an example with NX 10 under C++.

I have an array of ints that I want to assing to the UDO. However, when I call the SetIntegers method, an error ocurrs and an exception of "Internal error: memory access violation" is thrown.

 

Here's my code (which createUDO() is called from the Close callback of a dialog):

 

static NXOpen::UserDefinedObjects::UserDefinedClass* myUDOclass = nullptr;

// The display callback (is never called so I guess is not important at this moment)
int dispCB(NXOpen::UserDefinedObjects::UserDefinedDisplayEvent*  displayEvent)
{
	try
	{
		std::vector<int> myUDOints = displayEvent->UserDefinedObject()->GetIntegers();
		size_t numNodes = myUDOints.size() / 3;
		
		std::vector<NXOpen::Point3d> myPoints(numNodes);
		for (size_t i = 0; i < numNodes; ++i)
		{
			myPoints[i].X = myUDOints[i * 3]; myPoints[i].Y = myUDOints[i * 3 + 1]; myPoints[i].Z = myUDOints[i * 3 + 2];
		}

		NXOpen::UserDefinedObjects::UserDefinedObjectDisplayContext* dispContext = displayEvent->DisplayContext();
		dispContext->DisplayPolyline(myPoints);

		delete dispContext;
		delete displayEvent;
	}
	catch (NXOpen::NXException ex) { std::cout << "Caught Exception: " << ex.Message() << std::endl; }

	return 0;
}

int editCB(NXOpen::UserDefinedObjects::UserDefinedEvent* editEvent)
{
	delete editEvent;
	return 0;
}

int infoCB(NXOpen::UserDefinedObjects::UserDefinedEvent* infoEvent)
{
	delete infoEvent;
	return 0;
}

void DialogLogic::createUDO()
{
	try
	{
		UF_initialize();
		NXOpen::Session* theSession = NXOpen::Session::GetSession(); // session
		NXOpen::BasePart* basePart = theSession->Parts()->BaseDisplay();
		if (!myUDOclass)
		{
			// Define your custom UDO class
			myUDOclass = theSession->UserDefinedClassManager()->CreateUserDefinedObjectClass("Triangle", "Triangle");
			// Setup properties on the custom UDO class
			myUDOclass->SetAllowQueryClassFromName(NXOpen::UserDefinedObjects::UserDefinedClass::AllowQueryClassOn);
			// Register callbacks for the UDO class
			myUDOclass->AddDisplayHandler(make_callback(&dispCB));
			myUDOclass->AddAttentionPointHandler(make_callback(&dispCB));
			myUDOclass->AddFitHandler(make_callback(&dispCB));
			myUDOclass->AddSelectionHandler(make_callback(&dispCB));
			myUDOclass->AddEditHandler(make_callback(&editCB));
			myUDOclass->AddInformationHandler(make_callback(&infoCB));
			NXOpen::UI::GetUI()->SelectionManager()->SetSelectionStatusOfUserDefinedClass(myUDOclass, true);
		}

		NXOpen::UserDefinedObjects::UserDefinedObjectManager* myUDOmanager = basePart->UserDefinedObjectManager();
		NXOpen::UserDefinedObjects::UserDefinedObject* firstUDO = myUDOmanager->CreateUserDefinedObject(myUDOclass);

		int howMany = 9;
		std::vector<int> theInts(howMany);

		for (int i = 0; i < howMany; i++)
		{
			theInts[i] = i;
		}

		firstUDO->SetIntegers(theInts); //!!! The exception is thrown here !!!!!

		UF_DISP_add_item_to_display(firstUDO->Tag());
		UF_terminate();
	}
	catch (NXOpen::NXException ex) { std::cout << "Caught Exception: " << ex.Message() << std::endl; }
}

Any ideas? The SimpleUDO example works wonderful, but it is an standalone application and I need this to be called from a Dialog and access its data...

 

Thank you very much.