OpenCASCADE+MFC读取PMI信息
1、用到的类: XCAFDoc_DimTolTool
说明:包含XCAF文档的GD&T部分。为GD&T部分管理提供工具。可用于获取stp文件里的尺寸,形位公差、基准。
2、示例代码
//PMI工具
Handle(XCAFDoc_DimTolTool) m_DimTolTool;
//Shape工具
Handle(XCAFDoc_ShapeTool) m_ShapeTool;
//基准
TDF_LabelSequence DatumLabels;
m_DimTolTool->GetDatumLabels(DatumLabels);
//尺寸
TDF_LabelSequence DimensionLabels;
m_DimTolTool->GetDimensionLabels(DimensionLabels);
//形位公差
TDF_LabelSequence GeomTolLabels;
m_DimTolTool->GetGeomToleranceLabels( GeomTolLabels);
//基准
for (TDF_LabelSequence::Iterator aIter(DatumLabels); aIter.More(); aIter.Next())
{
const TDF_Label& aLabel = aIter.Value();
Handle(XCAFDoc_Datum) aDatum;
//获取标签类基准属性
if (!aLabel.FindAttribute(XCAFDoc_Datum::GetID(), aDatum))
continue;
Handle(XCAFDimTolObjects_DatumObject) aDatumObj = aDatum->GetObject();
if (!aDatumObj.IsNull())
{
TDF_LabelSequence theShapeLFirst;
TDF_LabelSequence theShapeLSecond;
//获取基准关联的shape对象的标签 theShapeLSecond应该是空的
m_DimTolTool->GetRefShapeLabel(aLabel, theShapeLFirst, theShapeLSecond);
TopoDS_Shape aShape;
if (theShapeLFirst.Size() > 0)
{
TDF_Label aLabel = theShapeLFirst.First();
aShape = m_ShapeTool->GetShape(aLabel);
}
else
continue;
//基准名字 如:A
Handle(TCollection_HAsciiString) DatumName = aDatumObj->GetName();
}
}
//尺寸
for (TDF_LabelSequence::Iterator aIter(DimensionLabels); aIter.More(); aIter.Next())
{
const TDF_Label& aLabel = aIter.Value();
Handle(XCAFDoc_Dimension) aDimAttr;
//获取标签内的尺寸属性
if (!aLabel.FindAttribute(XCAFDoc_Dimension::GetID(), aDimAttr))
continue;
Handle(XCAFDimTolObjects_DimensionObject) aDimTolObj = aDimAttr->GetObject();
if (!aDimTolObj.IsNull())
{
TDF_LabelSequence theShapeLFirst;
TDF_LabelSequence theShapeLSecond;
m_DimTolTool->GetRefShapeLabel(aLabel, theShapeLFirst, theShapeLSecond);
TopoDS_Shape aFirstShape;
TopoDS_Shape aSecondShape;
if (theShapeLFirst.Size() > 0)
{
TDF_Label aLabel = theShapeLFirst.First();
aFirstShape = m_ShapeTool->GetShape(aLabel);
}
if (theShapeLSecond.Size() > 0)
{
TDF_Label aLabel = theShapeLSecond.First();
aSecondShape = m_ShapeTool->GetShape(aLabel);
}
//尺寸类型 如:长度尺寸 半径直径尺寸,没有角度尺寸
XCAFDimTolObjects_DimensionType DimensionType = aDimTolObj->GetType();
//公差类型
//极值表示法
if (aDimTolObj->IsDimWithRange())
{
Standard_Real UpperBound = aDimTolObj->GetUpperBound();//最大值
Standard_Real LowerBound = aDimTolObj->GetLowerBound();//最小值
}
//上下偏差表示法
if (aDimTolObj->IsDimWithPlusMinusTolerance())
{
Standard_Real UpperTolValue = aDimTolObj->GetUpperTolValue();//上偏差
Standard_Real LowerTolValue = aDimTolObj->GetLowerTolValue();//下偏差
}
//标准公差 如H7
if (aDimTolObj->IsDimWithClassOfTolerance())
{
Standard_Boolean theHole; //是否是孔
XCAFDimTolObjects_DimensionFormVariance theFormVariance;//定义形式方差的值
XCAFDimTolObjects_DimensionGrade theGrade;//定义公差等级值
Standard_Boolean bClassOfTolerance = aDimTolObj->GetClassOfTolerance(theHole, theFormVariance, theGrade);
}
}
}
//形位公差
for (TDF_LabelSequence::Iterator aIter(GeomTolLabels); aIter.More(); aIter.Next())
{
const TDF_Label& aLabel = aIter.Value();
Handle(XCAFDoc_GeomTolerance) aDimTol;
//获取标注内的形位公差属性
if (!aLabel.FindAttribute(XCAFDoc_GeomTolerance::GetID(), aDimTol))
continue;
Handle(XCAFDimTolObjects_GeomToleranceObject) aDimTolObj = aDimTol->GetObject();
if (!aDimTolObj.IsNull())
{
TDF_LabelSequence theShapeLFirst;
TDF_LabelSequence theShapeLSecond;
m_DimTolTool->GetRefShapeLabel(aLabel, theShapeLFirst, theShapeLSecond);
TopoDS_Shape aShape;
if (theShapeLFirst.Size() > 0)
{
TDF_Label aLabel = theShapeLFirst.First();
aShape = m_ShapeTool->GetShape(aLabel);
}
else
continue;
//公差类型
XCAFDimTolObjects_GeomToleranceType GeomTolType = aDimTolObj->GetType();
//公差值
Standard_Real GeomTolValue = aDimTolObj->GetValue();
//公差值前缀符号
XCAFDimTolObjects_GeomToleranceTypeValue TypeOfValue = aDimTolObj->GetTypeOfValue();
//公差值实体要求
XCAFDimTolObjects_GeomToleranceMatReqModif MaterialRequirementModifier = aDimTolObj->GetMaterialRequirementModifier();
//关联基准
Handle(XCAFDoc_GraphNode) aNode;
if (aLabel.FindAttribute(XCAFDoc::DatumTolRefGUID(), aNode) && aNode->NbChildren() > 0)
{
for (Standard_Integer k = 1; k <= aNode->NbChildren(); k++)
{
Handle(XCAFDoc_Datum) aDatum;
TDF_Label aDatumLabel = aNode->GetChild(k)->Label();
if (aDatumLabel.FindAttribute(XCAFDoc_Datum::GetID(), aDatum))
{
Handle(XCAFDimTolObjects_DatumObject) aDatumObj = aDatum->GetObject();
//基准名称 如:A
Handle(TCollection_HAsciiString) DatumName = aDatumObj->GetName();
}
}
}
//返回公差的修饰符序列
XCAFDimTolObjects_GeomToleranceModifiersSequence aModif = aDimTolObj->GetModifiers();
if (!aModif.IsEmpty())
{
for (Standard_Integer k = aModif.Lower(); k <= aModif.Upper(); k++)
{
XCAFDimTolObjects_GeomToleranceModif GeomToleranceModif = aModif.Value(k);
}
}
}
}
注:获取到PMI信息后,根据OpenCASCADE提供的相关类重构PMI图形显示!