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

Exporting STEP with colors doesn't de-duplicate as expected with different, but same-valued Colors #1689

Closed
johnbeard opened this issue Oct 24, 2024 · 1 comment · Fixed by #1690
Labels

Comments

@johnbeard
Copy link

When adding to an Assembly, it seems that the color/object tuples aren't correctly de-duplicating:

        # define the current color
        current_color = el.color if el.color else color

        # add a leaf with the actual part if needed
        if el.obj:
            # get/register unique parts referenced in the assy
            key0 = (current_color, el.obj)  # (color, shape)  # <<------- a different object (not value) here
            key1 = el.obj  # shape

            if key0 in unique_objs:   # <---- fails this check
                lab = unique_objs[key0]
            else:
                lab = tool.NewShape()
                if key1 in compounds:

If you add items to the assembly using a single Color, it's works like you expect:

def single_color_object():
    # Create an assembly
    assembly = cq.Assembly()
    color = cq.Color(0, 0, 1) # <---- same!

    # Add 4 cubes in a 2x2 grid
    for i in range(2):
        for j in range(2):
            assembly.add(
                cube,
                loc=cq.Location(
                    cq.Vector(i * 2, j * 2, 0),
                ),
                # Same object each time
                color=color,
            )

    return assembly

This returns 4 cubes all the same object (exports to ~25kB)

If you make a new color each time with the same value, you get duplicated cubes:

def multi_same_color_object():
    # Create an assembly
    assembly = cq.Assembly()

    # Add 4 cubes in a 2x2 grid
    for i in range(2):
        for j in range(2):
            assembly.add(
                cube,
                loc=cq.Location(
                    cq.Vector(i * 2, j * 2, 0),
                ),
                # This is always the same value, but it's a different object
                color=cq.Color(0, 0, 1),
            )

    return assembly

This returns 4 same coloured cubes, but they're all "new" objects (exports to ~75kB)

In fact, you can even subvert it the other way by changing the value, not the object:

def multi_different_color_object():
    # Create an assembly
    assembly = cq.Assembly()
    color = cq.Color(0, 0, 1)

    # Add 4 cubes in a 2x2 grid
    for i in range(2):
        for j in range(2):
            # Change the color "secretly"
            color.r = i / 2
            color.g = j / 2
            color.b = 0
            assembly.add(
                cube,
                loc=cq.Location(
                    cq.Vector(i * 2, j * 2, 0),
                ),
                # Same object each time
                # but the value changed
                color=color,
            )

    return assembly

Saving this makes 4 identically coloured cubes (exports to ~25kB)

It seems to me that this should be a by-value comparison rather than checking if it's literally the same object?

Script to replicate:
cq_export_color.zip

Version:

cadquery                      2.4.0
cadquery-ocp                  7.7.2
@lorenzncode
Copy link
Member

Yes, agree. I can reproduce it. Thanks for the report and script.

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

Successfully merging a pull request may close this issue.

2 participants