How to handle encoding issues for labels - Label names with umlauts not handled properly by STEP export

manuel-koch

Looking around for some CAD
I'm trying build a STEP file. While building content of the XCAF document I'm assigning strings to labels.

After loading the exported STEP in CAD Assistant I can see that some of the labels contain strange characters
that were originally umlaut characters.
I guess this is some kind of encoding issue regarding label strings.

How can I fix this encoding issue, i.e. how is this done properly ?

My code is build from various samples I found. It's python using pythonocc-core=7.7.2 but I guess it is compatible with C++ writings.

# Create XDE document
app = TDocStd_Application()
binxcafdrivers.DefineFormat(app)
doc = TDocStd_Document(f"example")
app.NewDocument("BinXCAF", doc)

# Tools
shape_tool = XCAFDoc_DocumentTool.ShapeTool(doc.Main()) # XCAFDoc_ShapeTool
color_tool = XCAFDoc_DocumentTool.ColorTool(doc.Main()) # XCAFDoc_ColorTool

compound = TopoDS_Compound()
brep_builder = BRep_Builder()
brep_builder.MakeCompound(compound)

shape = BRepPrimAPI_MakeBox(10, 10, 10).Shape()
label = shape_tool.AddShape(shape, False)

# Set custom name on label ( i.e. the shape it refers to )
TDataStd_Name.Set(label, "Some text with umlauts äöü") # FIXME this causes the encoding issue

# add moved instance instance of a prototype to our compound shape
brep_builder.Add(compound, shape)

compund_label = shape_tool.AddShape(compound, True)

# Set custom name on label ( i.e. the shape it refers to )
TDataStd_Name.Set(compund_label, "compound")

# Initialize the STEP exporter
step_writer = STEPCAFControl_Writer()

# To make sub-shape names work, we have to turn on the following static
# variable of OpenCascade.
Interface_Static.SetIVal("write.stepcaf.subshapes.name", 1)

Interface_Static.SetCVal("write.step.schema", "AP214")
Interface_Static.SetCVal("write.step.product.name", "my product")

# transfer compound shape and write STEP file
step_writer.Transfer(doc, STEPControl_AsIs)
output_step_path = ASSETS_DIR / "compound_with_umlaut_label.step"
status = step_writer.Write(output_step_path.as_posix())

if status != IFSelect_RetDone:
raise AssertionError("write failed")
 

Attachments

  • compound_with_umlaut_label.step
    16.6 KB · Views: 0
  • cad_assistent_screenshot.png
    cad_assistent_screenshot.png
    93.2 KB · Views: 1

manuel-koch

Looking around for some CAD
Someone suggested to replace the name setting snippet like the following, but that yields a runtime error

extended_string = TCollection_ExtendedString("Some text with umlauts äöü", True) TDataStd_Name.Set(label, extended_string)

TypeError: Wrong number or type of arguments for overloaded function 'TDataStd_Name_Set'. Possible C/C++ prototypes are: TDataStd_Name::Set(TDF_Label const &,TCollection_ExtendedString) TDataStd_Name::Set(TDF_Label const &,Standard_GUID const &,TCollection_ExtendedString) TDataStd_Name::Set(TCollection_ExtendedString)
 
Top