Skip to content

Labels missing when using export_step on nested compounds. #954

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

Open
jsmnbom opened this issue Mar 28, 2025 · 1 comment
Open

Labels missing when using export_step on nested compounds. #954

jsmnbom opened this issue Mar 28, 2025 · 1 comment
Labels
enhancement New feature or request

Comments

@jsmnbom
Copy link

jsmnbom commented Mar 28, 2025

When nesting compounds are used, the labels are lost when exporting.

Example:

cube = Box(1,1,1)
cube.label = 'Cube'

arm = Compound(
  label="Arm",
  children=[
    copy.copy(cube),
  ]
)

assembly = Compound(
  label="Root",
  children=[
    copy.copy(arm),
    copy.copy(arm).locate(Pos(10,0,0))
  ]
)

print(assembly.show_topology())
build123d.export_step(assembly, 'build/assembly.step')

Shows correct topology tree:

Root         Compound at 0x74cc4dde22b0, Location(p=(0.00, 0.00, 0.00), o=(-0.00, 0.00, -0.00))
├── Arm      Compound at 0x74cc4dde1db0, Location(p=(0.00, 0.00, 0.00), o=(-0.00, 0.00, -0.00))
│   └── Cube Box      at 0x74cc4de24a10, Location(p=(0.00, 0.00, 0.00), o=(-0.00, 0.00, -0.00))
└── Arm      Compound at 0x74cc4dde2850, Location(p=(10.00, 0.00, 0.00), o=(-0.00, 0.00, -0.00))
    └── Cube Box      at 0x74cc4de24ad0, Location(p=(0.00, 0.00, 0.00), o=(-0.00, 0.00, -0.00))

But if opened in any external program the step file looks more like this:

Image

Applying the fix in #953 gets us closer to the correct result:

Image

Unfortunately it still leaves the Solids unnamed which is not optimal.
Ideally it would end up like this:

Image

@gumyr gumyr added the enhancement New feature or request label Mar 29, 2025
@gumyr gumyr added this to the Not Gating Release 1.0.0 milestone Mar 29, 2025
@jsmnbom
Copy link
Author

jsmnbom commented Apr 1, 2025

I'm using the below code and so far it seems to function well. Leaving here since I don't wanna make too many draft PR's and it would fit well in a PR refactoring export/import code more broadly see my comment in #942 (comment).

def _add_shape(
  shape_tool: XCAFDoc_ShapeTool,
  color_tool: XCAFDoc_ColorTool,
  shape: Shape,
) -> TDF_Label:
  is_assembly = isinstance(shape, Compound) and len(shape.children) > 0
  label = self.shape_tool.AddShape(
      shape.wrapped,
      is_assembly,
      True,
  )

  for node in PreOrderIter(shape):
    found = shape_tool.FindShape(node.wrapped)
    if not found.IsNull():
      instances = TDF_LabelSequence()
      shape_tool.GetUsers_s(found, instances)
      children = get_non_assembly_children(shape_tool, found)
      for label in list(children) + list(instances) + [found]:
        _add_tdf_data(color_tool, label, node)

def _add_tdf_data(
  color_tool: XCAFDoc_ColorTool,
  label: TDF_Label,
  node: Shape | None = None,
):
  if node.label:
    TDataStd_Name.Set_s(label, TCollection_ExtendedString(node.label))

  if node.color:
    color_tool.SetColor(label, node.color.wrapped, XCAFDoc_ColorType.XCAFDoc_ColorSurf)

def get_non_assembly_children(
  shape_tool: XCAFDoc_ShapeTool,
  label: TDF_Label,
  children: TDF_LabelSequence | None = None,
):
  if children is None:
    children = TDF_LabelSequence()
  it = TDF_ChildIterator(label)
  while it.More():
    child_label = it.Value()
    ref_label = TDF_Label()
    if shape_tool.GetReferredShape_s(child_label, ref_label):
      if not shape_tool.IsAssembly_s(ref_label):
        children.Append(child_label)
        children.Append(ref_label)
        get_non_assembly_children(shape_tool, ref_label, children)
    else:
      if not shape_tool.IsAssembly_s(child_label):
        children.Append(child_label)
    it.Next()
  return children

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants