UG/NX二次开发,在面上生成文本并且拉伸的方法——适用于面刻字(基于NXOpen)

//创建文本
tag_t CreateText
(const string text/*刻字文本*/, const tag_t faceTag/*刻字面*/, double point[3]/*刻字中心点*/,
	const double textH/*刻字高*/, const double textL/*刻字宽*/, vector<NXObject*>& vecTextTag/*刻字体*/,
	Features::Feature* textFeature/*刻字特征*/, tag_t CsysId/*刻字的坐标系*/)
{
	try
	{
		Session* theSession = Session::GetSession();
		Part* workPart(theSession->Parts()->Work());
		Part* displayPart(theSession->Parts()->Display());

		Features::Text* nullFeatures_Text(NULL);
		vector<NXOpen::Features::Feature*> vecFeatOld = workPart->Features()->GetFeatures();
		vector<tag_t>vecOld;
		for (size_t i = 0; i < vecFeatOld.size(); i++)
			vecOld.push_back(vecFeatOld[i]->Tag());

		Features::TextBuilder* textBuilder1;
		textBuilder1 = workPart->Features()->CreateTextBuilder(nullFeatures_Text);
		Point3d origin1(0.0, 0.0, 0.0);
		Vector3d normal1(0.0, 0.0, 1.0);
		Plane* plane1;
		plane1 = workPart->Planes()->CreatePlane(origin1, normal1, SmartObject::UpdateOptionWithinModeling);
		textBuilder1->SetSectionPlane(plane1);

		textBuilder1->FrameOnPath()->AnchorPosition()->Expression()->SetRightHandSide("50");
		textBuilder1->SetScript(Features::TextBuilder::ScriptOptionsWestern);
		textBuilder1->SetCanUseKerningSpaces(false);
		textBuilder1->PlanarFrame()->SetAnchorLocation(GeometricUtilities::RectangularFrameBuilder::AnchorLocationTypeMiddleCenter);

		//textBuilder1->PlanarFrame()->Length()->SetRightHandSide(DoubleString(textL));
		textBuilder1->PlanarFrame()->Height()->SetRightHandSide("10");
		textBuilder1->PlanarFrame()->SetWScale(80);
		textBuilder1->PlanarFrame()->Shear()->SetRightHandSide("0");

		textBuilder1->FrameOnPath()->AnchorPosition()->Expression()->SetRightHandSide("50");
		textBuilder1->FrameOnPath()->Offset()->SetRightHandSide("18");

		textBuilder1->FrameOnPath()->SetWScale(80);
		textBuilder1->SelectFont("Arial", Features::TextBuilder::ScriptOptionsWestern);
		textBuilder1->SetTextString((char*)text.c_str());
		//textBuilder1->PlanarFrame()->SetAnchorLocation(GeometricUtilities::RectangularFrameBuilder::AnchorLocationTypeMiddleCenter);

		Face* face1(dynamic_cast<Face*>(NXObjectManager::Get(faceTag)));
		/*Point* point2;
		Point3d point3(point[0], point[1], point[2]);
		point2 = workPart->Points()->CreatePoint(point3);
		
		textBuilder1->PlanarFrame()->AnchorLocator()->SetValue(point2, workPart->ModelingViews()->WorkView(), point3);*/
		tag_t wcs = NULL;
		UF_CSYS_ask_wcs(&wcs);

		//设置WCS位置
		UF_CSYS_set_origin(CsysId, point);
		UF_CSYS_set_wcs(CsysId);

		CartesianCoordinateSystem* cartesianCoordinateSystem1(dynamic_cast<CartesianCoordinateSystem*>(NXObjectManager::Get(CsysId)));
		Xform* xform1;
		xform1 = workPart->Xforms()->CreateXform(cartesianCoordinateSystem1, SmartObject::UpdateOptionWithinModeling);

		CartesianCoordinateSystem* cartesianCoordinateSystem2;
		cartesianCoordinateSystem2 = workPart->CoordinateSystems()->CreateCoordinateSystem(xform1, SmartObject::UpdateOptionWithinModeling);
		
		textBuilder1->PlanarFrame()->SetCoordinateSystem(cartesianCoordinateSystem2);
		textBuilder1->PlanarFrame()->UpdateOnCoordinateSystem();

		NXObject* nXObject2 = textBuilder1->Commit();
		tag_t textTag = NULL_TAG;
		UF_MODL_ask_current_feature(UF_ASSEM_ask_work_part(), &textTag);

		//恢复WCS位置
		UF_CSYS_set_wcs(wcs);

		vector<NXOpen::Features::Feature*> vecFeatNew = workPart->Features()->GetFeatures();
		vector<tag_t>vecnew;
		for (size_t i = 0; i < vecFeatNew.size(); i++)
			vecnew.push_back(vecFeatNew[i]->Tag());

		for (size_t i = 0; i < vecOld.size(); i++)
		{
			vector<tag_t>::iterator iter;
			iter = find(vecnew.begin(), vecnew.end(), vecOld[i]);
			if (iter != vecnew.end())
				vecnew.erase(iter);
		}

		NXOpen::Features::Feature* Feat = nullptr;
		if (!vecnew.empty())
			Feat = (dynamic_cast<Features::Feature*>(NXObjectManager::Get(vecnew.front())));
		else
			return NULL_TAG;
		textFeature = Feat;
		for (size_t i = 0; i < Feat->GetEntities().size(); i++)
		{
			vecTextTag.push_back(Feat->GetEntities()[i]);
		}

		textBuilder1->Destroy();
		plane1->DestroyPlane();

		return textTag;
	}
	catch (const std::exception&)
	{

	}
}



//拉伸文本
vector<Body*> CreateExtruded(tag_t text/*文本Tag*/, tag_t faceTag/*刻字面Tag*/,
 double dir[3]/*拉伸方向*/, Features::Feature* feature/*拉伸特征*/)
{
	//拉伸方向
	double Direction[3] = { dir[0], dir[1], dir[2] };

	Session* theSession = Session::GetSession();
	Part* workPart(theSession->Parts()->Work());
	
	Features::Feature* nullFeatures_Feature(NULL);
	Features::ExtrudeBuilder* extrudeBuilder1;
	extrudeBuilder1 = workPart->Features()->CreateExtrudeBuilder(nullFeatures_Feature);
	extrudeBuilder1->AllowSelfIntersectingSection(true);
	
	tag_t bodyTag = NULL_TAG;
	UF_MODL_ask_face_body(faceTag, &bodyTag);

	std::vector<Body*> targetBodies(1);
	Body* body1(dynamic_cast<Body*>(NXObjectManager::Get(bodyTag)));
	targetBodies[0] = body1;
	extrudeBuilder1->BooleanOperation()->SetTargetBodies(targetBodies);
	extrudeBuilder1->Limits()->StartExtend()->Value()->SetRightHandSide("-2");
	extrudeBuilder1->Limits()->EndExtend()->Value()->SetRightHandSide("5");//固定

	extrudeBuilder1->BooleanOperation()->SetType(GeometricUtilities::BooleanOperation::BooleanTypeCreate);//创造
	Section* section1;
	section1 = workPart->Sections()->CreateSection(0.00095, 0.001, 0.05);
	extrudeBuilder1->SetSection(section1);
	section1->SetAllowedEntityTypes(Section::AllowTypesOnlyCurves);
	section1->AllowSelfIntersection(true);

	std::vector<Features::Feature*> features1(1);
	features1[0] = dynamic_cast<Features::Text*>(NXObjectManager::Get(text));//文本对象
	CurveFeatureRule* curveFeatureRule1;
	curveFeatureRule1 = workPart->ScRuleFactory()->CreateRuleCurveFeature(features1);

	std::vector<SelectionIntentRule*> rules1(1);
	rules1[0] = curveFeatureRule1;
	NXObject* nullNXObject(NULL);
	Point3d helpPoint1(0.0, 0.0, 0.0);
	section1->AddToSection(rules1, nullNXObject, nullNXObject, nullNXObject, helpPoint1, Section::ModeCreate, false);

	Point3d origin1(0.0, 0.0, 0.0);
	Vector3d vector1(Direction[0], Direction[1], Direction[2]);
	NXOpen::Direction* direction1;
	direction1 = workPart->Directions()->CreateDirection(origin1, vector1, SmartObject::UpdateOptionWithinModeling);
	extrudeBuilder1->SetDirection(direction1);

	Features::Feature* feature2;
	feature2 = extrudeBuilder1->CommitFeature();
	Features::Extrude* extrude = dynamic_cast<Features::Extrude*>(feature2);
	feature = extrudeBuilder1->CommitFeature();
	extrudeBuilder1->Destroy();
	return extrude->GetBodies();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值