OpenCasCade(OCCT) 7.7.0 初探(一) Step文件相关(C#、C++/CLI)

文章讲述了如何使用XDE替代原始的STEPControl_Reader来读取STEP文件,并提取零件及其子图形的信息,包括名称和层级结构。作者提供了从XCAFDoc_ShapeTool获取形状和子组件的示例代码。
Python3.8

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

希望我的分享能帮助到你。

Step文件的读取,在原始例程中代码如下:

		STEPControl_Reader aReader;
		IFSelect_ReturnStatus aStatus = aReader.ReadFile(theFileName.ToCString());
		if (aStatus == IFSelect_RetDone)
		{
			bool isFailsonly = false;
			aReader.PrintCheckLoad(isFailsonly, IFSelect_ItemsByEntity);
			int aNbRoot = aReader.NbRootsForTransfer();
			aReader.PrintCheckTransfer(isFailsonly, IFSelect_ItemsByEntity);
			for (Standard_Integer n = 1; n <= aNbRoot; n++)
			{
				Standard_Boolean ok = aReader.TransferRoot(n);
				int aNbShap = aReader.NbShapes();
				if (aNbShap > 0)
				{
					for (int i = 1; i <= aNbShap; i++)
					{
						TopoDS_Shape aShape = aReader.Shape(i);
						myAISContext()->Display(new AIS_Shape(aShape), Standard_False);
					}
					myAISContext()->UpdateCurrentViewer();
				}
			}
		}
		else
		{
			return false;
		}
		return true;

原始例程中用STEPControl_Reader读取Step文件显示,这种方法可以满足图纸读取显示,但有个关键问题就是,无法读取图纸中零件的信息。改用XDE进行读取即可以获取到零件的信息。

代码如不可用,请自行添加XDE相关的头文件及LIB库文件。本代码是从我程序段中摘取出来并修改成的,请自行调试,代码中需要自行完成嵌套遍历子图形。

//首先在过程外面需要定义一个公共变量,这在以后或许会用到
NCollection_Haft<Handle(XCAFDoc_ShapeTool)> myShapeTool;

	bool readfile(const TCollection_AsciiString& theFileName)
	{

		//XDE读取文件代码
		STEPCAFControl_Reader reader;
		reader.SetColorMode(true);
		reader.SetNameMode(true);
		IFSelect_ReturnStatus status = reader.ReadFile(theFileName.ToCString());
		if (!status)//读取错误
			return false;
		Handle(XCAFApp_Application) anApp = XCAFApp_Application::GetApplication();
		Handle(TDocStd_Document) doc;
		anApp->NewDocument("MDTV-XCAF", doc);
		bool yes = reader.Transfer(doc);
		if (yes)
		{
			TDF_Label mainLabel = doc->Main();
			myShapeTool() = XCAFDoc_DocumentTool::ShapeTool(mainLabel);
			//获取顶级Shapes
			TDF_LabelSequence aLabelSequence;
			myShapeTool()->GetFreeShapes(aLabelSequence);
			int Roots = aLabelSequence.Length();
			for (int index = 1; index <= Roots; index++)
			{
				//获取顶形状Lable
				TDF_Label aLabel = aLabelSequence.Value(index);
				//获取图形->这个就是原始图形
				TopoDS_Shape aShape = myShapeTool()->GetShape(aLabel);
                myAISContext()->Display(new AIS_Shape(aShape), Standard_True);
				//获取名字
				Handle(TDataStd_Name) aName;
				if (aLabel.FindAttribute(TDataStd_Name::GetID(), aName))
				{
					std::cout << "  Name: " << aName->Get() << std::endl;
				}
				//获取下一级图形Child
				//这里需要注意一下,判断是否为组件,如果是则继续向下,如果不是,那就是图形了不用再分解了
				//我这里仅做示例,实际中需要把下面的代码写成嵌套遍历方法进行遍历
				TDF_LabelSequence components;
				if (myShapeTool()->GetComponents(aLabel, components))
				{
					//有下级
					std::cout << "  总数: " << components.Length() << std::endl;
					for (Standard_Integer compIndex = 1; compIndex <= components.Length(); ++compIndex)
					{
						TDF_Label childLabel = components.Value(compIndex);
						//这里要判断一下是否为引用的图形
						if (myShapeTool()->IsReference(childLabel))
						{
							TDF_Label bShapeLabel;
							if (myShapeTool()->GetReferredShape(childLabel, bShapeLabel))
							{
								//获取引用的图形,用上面的代码方式获取名字
								Handle(TDataStd_Name) bName;
								if (bShapeLabel.FindAttribute(TDataStd_Name::GetID(), bName))
								{
									std::cout << "  Name: " << bName->Get() << std::endl;
								}
							}
							//自行编写代码->嵌套遍历bShapeLabel
						}
						else
						{
							//获取引用的图形,用上面的代码方式获取名字
							Handle(TDataStd_Name) bName;
							if (childLabel.FindAttribute(TDataStd_Name::GetID(), bName))
							{
								std::cout << "  Name: " << bName->Get() << std::endl;
							}
						}
						//自行编写代码->嵌套遍历childLabel
					}

				}
				else
				{
					//仅为图形没有下级
				}
			}
		}
	}

您可能感兴趣的与本文相关的镜像

Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

霜吹落花

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值