Steps To Reproduce | Follow the code to import objects from DXF.
// To extract name of layers is necessary to have a TDocStd_Document
Handle(TDocStd_Document) document;
Handle(XCAFApp_Application) application = XCAFApp_Application::GetApplication();
// Is necessary create a new document
application->NewDocument(m_filePath.toLatin1().constData(), document);
DXFCAFControl_Reader dxfReaderLayer;
dxfReaderLayer.SetNameMode(true);
TCollection_AsciiString filePathUtf8(m_filePath.toUtf8().constData());
dxfReaderLayer.ReadFile(filePathUtf8.ToCString());
// You must transfer your reader to document
dxfReaderLayer.Transfer(document);
TDF_Label rootLabel = document->Main();
// This object is important to extract layer from DXF
Handle(XCAFDoc_LayerTool) layerTool = XCAFDoc_DocumentTool::LayerTool(rootLabel);
// This object is important to extract shapes from layers
Handle(XCAFDoc_ShapeTool) shapeTool = XCAFDoc_DocumentTool::ShapeTool(rootLabel);
TDF_ChildIterator it;
Handle(TDataStd_Name) layerName;
for (it.Initialize(rootLabel, Standard_True); it.More(); it.Next())
{
// Get only the layers attributes
TDF_Label labelLayer = it.Value();
layerTool->FindAttribute(TDataStd_Name::GetID(), layerName);
if (layerTool->IsLayer(labelLayer))
{
if (labelLayer.FindAttribute(TDataStd_Name::GetID(), layerName))
{
TDF_LabelSequence labelSequence = TDF_LabelSequence();
layerTool->GetShapesOfLayer(labelLayer, labelSequence);
// Is necessary to take the layer of any edge
int numbersOfShapes = labelSequence.Size();
if (numbersOfShapes > 0)
{
QString layer = QString(TCollection_AsciiString(layerName->Get()).ToCString());
for (TDF_LabelSequence::Iterator labelIt(labelSequence); labelIt.More(); labelIt.Next())
{
TDF_Label shapeLabel = labelIt.Value();
TopoDS_Shape layerShape = shapeTool->GetShape(shapeLabel);
if (layerShape.IsNull())
{
continue;
}
if (TopAbs_VERTEX == layerShape.ShapeType())
{
insertLayer(layer);
insertVisibilityShape(QString(TCollection_AsciiString(layerName->Get()).ToCString()), layerShape);
m_numberOfVertices++;
}
else if (TopAbs_EDGE == layerShape.ShapeType())
{
insertLayer(layer);
insertVisibilityShape(QString(TCollection_AsciiString(layerName->Get()).ToCString()), layerShape);
m_numberOfLines++;
}
else if (TopAbs_WIRE == layerShape.ShapeType())
{
insertLayer(layer);
insertVisibilityShape(QString(TCollection_AsciiString(layerName->Get()).ToCString()), layerShape);
m_numberOfWires++;
}
else if (TopAbs_FACE == layerShape.ShapeType())
{
insertLayer(layer);
insertVisibilityShape(QString(TCollection_AsciiString(layerName->Get()).ToCString()), layerShape);
m_numberOfFaces++;
}
else if (TopAbs_COMPOUND == layerShape.ShapeType())
{
insertLayer(layer);
for (TopExp_Explorer explorer(layerShape, TopAbs_ShapeEnum::TopAbs_WIRE); explorer.More(); explorer.Next())
{
TopoDS_Wire wire = TopoDS::Wire(explorer.Current());
if (!wire.IsNull())
{
insertVisibilityShape(QString(TCollection_AsciiString(layerName->Get()).ToCString()), wire);
m_numberOfWires++;
}
}
for (TopExp_Explorer explorer(layerShape, TopAbs_ShapeEnum::TopAbs_FACE); explorer.More(); explorer.Next())
{
TopoDS_Face face = TopoDS::Face(explorer.Current());
if (!face.IsNull())
{
insertVisibilityShape(QString(TCollection_AsciiString(layerName->Get()).ToCString()), face);
m_numberOfFaces++;
}
}
}
}
}
}
}
}
}
|
---|