pythonocc-demos icon indicating copy to clipboard operation
pythonocc-demos copied to clipboard

Example for loading XmlXCAF or BinXCAF file?

Open fuyb1992 opened this issue 1 year ago • 1 comments

cad.xmlxcaf.zip having a hard time getting the load XCAF files, here is my code:

import os
from OCC.Core.TDocStd import TDocStd_Document
from OCC.Core.TDF import TDF_LabelSequence

from OCC.Core.XmlXCAFDrivers import xmlxcafdrivers
from OCC.Core.XCAFApp import XCAFApp_Application
from OCC.Core.XCAFDoc import XCAFDoc_DocumentTool
from OCC.Core.TCollection import TCollection_ExtendedString


def read_xml_file(file_path):
    # Initialize the application
    print("Initializing application...")
    app = XCAFApp_Application.GetApplication()
    xmlxcafdrivers.DefineFormat(app)
    if app is None:
        raise Exception("Failed to initialize application")

    # Create a new document
    print("Creating new document...")
    doc = TDocStd_Document("pythonocc-doc")
    if doc is None:
        raise Exception("Failed to create document")

    # Create the storage driver

    # Read the XML file
    app.Open(file_path, doc)

    # Access the shape tool
    print("Accessing shape tool...")
    shape_tool = XCAFDoc_DocumentTool.ShapeTool(doc.Main())
    if shape_tool is None:
        raise Exception("Failed to access shape tool")
    labels = TDF_LabelSequence()
    shape_tool.GetFreeShapes(labels)
    # Debug information
    print(f"Number of shapes at root : {labels.Length()}")

I got 0 shape , xml file in zip file

fuyb1992 avatar Nov 14 '24 11:11 fuyb1992

Same problem when loading XBF file, got 0 shape.

geraldozzz avatar May 07 '25 15:05 geraldozzz