Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting label name with umlaut result in STEP with encoding issues, trying extended string fails with crash #1278

Closed
manuel-koch opened this issue Nov 16, 2023 · 2 comments

Comments

@manuel-koch
Copy link

manuel-koch commented Nov 16, 2023

With pythonocc-core=7.7.2 , trying to export XCAF to STEP, using label names.
There is an encoding issue with names that contain e.g. umlaut characters.
Trying to workaround the issue by using TCollection_ExtendedString fails with a crash at runtime.

How can I fix this encoding issue, how to use pythonocc-core properly ?

Minimal working sample code

from OCC.Core import TopoDS, TopAbs
from OCC.Core.BRep import BRep_Builder
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox
from OCC.Core.BinXCAFDrivers import binxcafdrivers
from OCC.Core.Quantity import Quantity_Color, Quantity_TOC_RGB
from OCC.Core.STEPCAFControl import STEPCAFControl_Writer
from OCC.Core.TCollection import TCollection_ExtendedString
from OCC.Core.TDF import TDF_Label
from OCC.Core.TDataStd import TDataStd_Name
from OCC.Core.TDocStd import TDocStd_Application, TDocStd_Document
from OCC.Core.TopoDS import TopoDS_Shape, TopoDS_Compound
from OCC.Core.XCAFDoc import XCAFDoc_DocumentTool, XCAFDoc_ColorGen
from OCC.Extend.DataExchange import read_step_file
from OCC.Core.TopLoc import TopLoc_Location
from OCC.Core.gp import gp_Trsf, gp_Vec, gp_Quaternion
from OCC.Core.STEPControl import (
    STEPControl_AsIs,
)
from OCC.Core.Interface import Interface_Static
from OCC.Core.IFSelect import IFSelect_RetDone

# 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 )
# this has encoding issues
#   TDataStd_Name.Set(label, "Some text with umlauts äöü")
# BUT the following crashes with
# 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)
extended_string = TCollection_ExtendedString("Some text with umlauts äöü", True)
TDataStd_Name.Set(label, extended_string)

# 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)
status = step_writer.Write("compound_with_umlaut_label.step")

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

The generated STEP looks like the following in CAD Assistent:
cad_assistent_screenshot

The generated STEP ( with the encoding issue ):
compound_with_umlaut_label.step.txt

@tpaviot
Copy link
Owner

tpaviot commented Nov 17, 2023

thank you for the feedback, fixed in current master

@tpaviot tpaviot closed this as completed Nov 17, 2023
@manuel-koch
Copy link
Author

manuel-koch commented Nov 17, 2023

Thanks for the quick fix !

Will there be a new release available for conda or regular python environments ? I tried to build pythonocc-core for my regular python environment but failed due to build problems of dependencies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants