API: build a joint using joint origins

API: build a joint using joint origins

st00katz
Participant Participant
543 Views
1 Reply
Message 1 of 2

API: build a joint using joint origins

st00katz
Participant
Participant

Hi, I have a component (a linkage, see attachment) with 2 joint origins. Here's what I'm trying to do: programmatically load 2 of those linkages and build a joint using the joint origins. Here's what I got so far:

	app = Application::get();
	if (!app)
		return false;

	ui = app->userInterface();
	if (!ui)
		return false;

	Ptr<Design> design = app->activeProduct();

	app->activeDocument()->saveAs("linkage test", app->data()->activeProject()->rootFolder(), "", "");

	auto root = design->rootComponent();
	auto project = app->data()->activeProject();
	
	Ptr<Occurrence> occ1, occ2;
	Ptr<DataFile> dataFile = nullptr;

	for (auto file : project->rootFolder()->dataFiles()) {
		if (file->name() == "linkage") {
			dataFile = file;
			break;
		}
	}
	if (!dataFile)
		ui->messageBox("file not found");
	else {
		occ1 = root->occurrences()->addByInsert(dataFile, Matrix3D::create(), false);
		occ2 = root->occurrences()->addByInsert(dataFile, Matrix3D::create(), false);
//here's something wrong: g0 and g1 are null
		auto g0 = JointGeometry::createByPoint(occ1->component()->jointOrigins()->item(0));
		auto g1 = JointGeometry::createByPoint(occ2->component()->jointOrigins()->item(1));
		auto joint = root->joints()->createInput(g0, g1);
		root->joints()->add(joint);
	}

 

I would appreciate any input, hint etc on this. Pseudocode or python is ok. Thank you.

0 Likes
544 Views
1 Reply
Reply (1)
Message 2 of 2

st00katz
Participant
Participant

Hi, I think I got it working:

 

extern "C" XI_EXPORT bool run(const char* context)
{
	app = Application::get();
	if (!app)
		return false;

	ui = app->userInterface();
	if (!ui)
		return false;

	Ptr<Design> design = app->activeProduct();
	auto root = design->rootComponent();
	auto project = app->data()->activeProject();

	Ptr<DataFile> dataFile = nullptr;
	for (const auto &file : project->rootFolder()->dataFiles())
		if (file->name() == "linkage") {
			dataFile = file;
			break;
		}

	if (app->activeDocument()->name() == "Untitled")
		app->activeDocument()->saveAs("linkage test", app->data()->activeProject()->rootFolder(), "", "");

	for (const auto &occ : root->allOccurrences())
		occ->deleteMe();

	Ptr<Occurrence> occ1, occ2;

	if (!dataFile)
		ui->messageBox("file not found");
	else {
		occ1 = root->occurrences()->addByInsert(dataFile, Matrix3D::create(), false);
		occ2 = root->occurrences()->addByInsert(dataFile, Matrix3D::create(), false);
		auto x = occ1->component()->sketches()->item(0)->sketchCurves()->sketchCircles()->item(0);
		auto y = occ2->component()->sketches()->item(0)->sketchCurves()->sketchCircles()->item(0);
		auto g1 = JointGeometry::createByCurve(x, JointKeyPointTypes::CenterKeyPoint);
		auto g2 = JointGeometry::createByCurve(y, JointKeyPointTypes::CenterKeyPoint);
		auto joint = root->joints()->createInput(g1, g2);
		joint->geometryOrOriginOne(occ1->component()->allOccurrences()->item(0)->component()->jointOrigins()->item(0));
		joint->geometryOrOriginTwo(occ2->component()->allOccurrences()->item(0)->component()->jointOrigins()->item(0));
		joint->setAsRevoluteJointMotion(JointDirections::ZAxisJointDirection);
		root->joints()->add(joint);
	}
	
	return true;
}

 

Thoughts?

 

Also, why I'm seeing this site in Chinese?

0 Likes