希望我的分享能帮助到你。
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
{
//仅为图形没有下级
}
}
}
}
文章讲述了如何使用XDE替代原始的STEPControl_Reader来读取STEP文件,并提取零件及其子图形的信息,包括名称和层级结构。作者提供了从XCAFDoc_ShapeTool获取形状和子组件的示例代码。
1500

被折叠的 条评论
为什么被折叠?



