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

added combine="cut" option for 3D operations #954

Merged
merged 32 commits into from
Jan 25, 2022

Conversation

Jojain
Copy link
Contributor

@Jojain Jojain commented Jan 7, 2022

Aims to target #30 I added the possibility to use combine="cut" for 3D operations to remove material from the context solid.
It's essentially a shorcut of calling cut() later.

I didn't added it for extrude as it has already cutBlind that does the same thing, I didn't added it to text since it has a cut kwarg. I think it should be changed to be consistent with all the other methods but since it will probably not be agreed on I didn't changed it.

As I already said in #832, I still think everything should be made consistent and that we should remove/deprecate things that aren't consistent. I know it would require breaking change but not doing it will likely confuse more and more new users.

added the option to combine="cut" for revolve and loft, added tests

removed old arg of loft that was actually unused
added combine cut for twistextrude sweep.
Didn't added it for extrude as it already as cutBlind and the option to extrude until a given face is combine by default and would need a little bit more change to handle combine=cut (but its still possible)
@codecov
Copy link

codecov bot commented Jan 7, 2022

Codecov Report

Merging #954 (67e057d) into master (4fcada6) will increase coverage by 0.08%.
The diff coverage is 98.64%.

❗ Current head 67e057d differs from pull request most recent head 9217b3f. Consider uploading reports for the commit 9217b3f to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master     #954      +/-   ##
==========================================
+ Coverage   96.08%   96.17%   +0.08%     
==========================================
  Files          40       40              
  Lines        9257     9286      +29     
  Branches     1112     1109       -3     
==========================================
+ Hits         8895     8931      +36     
+ Misses        209      208       -1     
+ Partials      153      147       -6     
Impacted Files Coverage Δ
cadquery/cq.py 92.28% <96.55%> (+0.53%) ⬆️
cadquery/occ_impl/shapes.py 92.54% <100.00%> (ø)
cadquery/occ_impl/sketch_solver.py 91.14% <100.00%> (ø)
tests/test_cadquery.py 99.29% <100.00%> (+0.01%) ⬆️
tests/test_sketch.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4fcada6...9217b3f. Read the comment docs.

@Jojain
Copy link
Contributor Author

Jojain commented Jan 10, 2022

I've added test to target the lines marked as non tested but it keeps failing

@jmwright
Copy link
Member

I've added test to target the lines marked as non tested but it keeps failing

I think it's fine. We have to sometimes ignore codecov when it complains about minor things.

cadquery/cq.py Outdated Show resolved Hide resolved
cadquery/cq.py Outdated Show resolved Hide resolved
cadquery/cq.py Outdated
@@ -2993,7 +2993,9 @@ def twistExtrude(

r = Compound.makeCompound(shapes).fuse()

if combine:
if isinstance(combine, str) and combine == "cut":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like this section of code ends up being repeated quite a bit, but I'm not sure that there's an quick/efficient way to avoid the code duplication. Probably best just to leave it for now unless someone has an idea.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd propose to refactor and move it to _combineWithBase

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure to understand what we are talking about. The plan would be to move what to _combineWithBase ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a lot of repetitive code like:

        if isinstance(combine, str) and combine == "cut":
            newS = self._cutFromBase(r)
        elif isinstance(combine, bool) and combine:
            newS = self._combineWithBase(r)
        else:
            newS = self.newObject([r])

it could be refactored to:

return self._combineWithBase(r, combine)

(ofc you'd need to update _combineWithBase accordingly).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I think I've refactored _combineWithBase like you thought about it.
I'm proposing to add a breaking change to text that has both cut and combine kwarg. In my opinion we should get rid of the cut kwarg and keep the combine consistent with other methods. It would be fairly easy for user to make the change but would result in errors in model that still use it

Let me know if there is an agreement on this or not so I can make the change (or not) @adam-urbanczyk @jmwright @marcus7070

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Long term it is fine, but please use this for now:

class deprecate_kwarg:
.

Copy link
Member

@jmwright jmwright left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Jojain !

@Jojain
Copy link
Contributor Author

Jojain commented Jan 11, 2022

I've added test to target the lines marked as non tested but it keeps failing

I think it's fine. We have to sometimes ignore codecov when it complains about minor things.

Well ok, just let me know if there is something I missed, I'm not really used to add tests so I'm not always sure if it's done right or not

@Jojain
Copy link
Contributor Author

Jojain commented Jan 12, 2022

As discussed on discord I'm not sure it make sense to add the option for each for me it would result in wrong use of the method. This target the issue #754

I've not added it to extrude since there is already cutBlind and the extrude method is already very complex, it would however make sense to still add it (but requires a bit more work) and deprecate cutBlind, what are your thoughts about it ?

Finally github says there is conflicts but there isnt, I cannot solve it so a lead dev will have to resolve the so called conflicts

@jmwright
Copy link
Member

I've not added it to extrude since there is already cutBlind and the extrude method is already very complex, it would however make sense to still add it (but requires a bit more work) and deprecate cutBlind, what are your thoughts about it ?

My vote would probably be to keep cutBlind(), but re-implement it to use the new combine/cut functionality. That way we don't have to break existing scripts, but we're leveraging the latest best practices within the codebase. Thoughts?

@Jojain
Copy link
Contributor Author

Jojain commented Jan 12, 2022

My vote would probably be to keep cutBlind(), but re-implement it to use the new combine/cut functionality. That way we don't have to break existing scripts, but we're leveraging the latest best practices within the codebase. Thoughts?

Well I don't think it makes sense to have combine/cut with cutBlind since what it does is explicitly in the name, we can maybe make extrude call cutBlind internaly if it's set with combine="cut" and keep it cutBlind as it is, just telling user that best practice are to use extrude with combine="cut" instead of cutBlind. That a good compromise IMHO

@adam-urbanczyk
Copy link
Member

To make everything self-consistent (make the code shorter and get rid of special cases) it would make sense to add the option to every method that allows to use combine. For now forget about cutBlind etc.

@Jojain
Copy link
Contributor Author

Jojain commented Jan 14, 2022

Hopefully this is done.

In the end I didn't added combine option to each because I don't think it fits the method well, and actually having it only in eachpoint may lead users to better choose which method they actually need for what they want to do.

I still don't know about the merging issues I let you check that, all test are green on my side.

@jmwright jmwright self-requested a review January 15, 2022 19:36
@jmwright
Copy link
Member

I see that there were commits after the last comment that this was ready. Is it ready for a final review now?

@Jojain
Copy link
Contributor Author

Jojain commented Jan 17, 2022

@jmwright yes it is

@fedorkotov
Copy link
Contributor

Please don't forget to update changes.md file. It's very helpful when trying to get up to speed with CadQuery after a long pause.

* _combineWithBase allows to clean too
* added missing clean params
Needed for backward compatibility (one test failing).
else:
newS = self.newObject([r])
# do not combine branch
newS = self.newObject(obj if not isinstance(obj, Shape) else [obj])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that that is the optimal way. We might want to rewrite the failing test

@@ -61,6 +62,7 @@

CQObject = Union[Vector, Location, Shape, Sketch]
VectorLike = Union[Tuple[float, float], Tuple[float, float, float], Vector]
CombineMode = Union[bool, Literal["cut", "a", "s"]] # a : additive, s: subtractive
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added also modes used in Sketch. Note that it is better to use Literal for typing.

Co-authored-by: Jeremy Wright <wrightjmf@gmail.com>
@adam-urbanczyk
Copy link
Member

Thanks for the contribution @Jojain ! I did some rework, let me know what you think.

Copy link
Contributor Author

@Jojain Jojain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My comments aside I'm good with what you came up with. I think the code simplification is great.
It would be useful to document the changes in the docs as they introduce a "new concept" which is that every protusion method can also be used to remove material.
I think we should lead newcomers in the docs to use the combine argument so hopefully CQ scripts would be more unified

cadquery/cq.py Outdated

r = self._extrude(None, both=both, taper=taper, upToFace=faceIndex)
# Handle `until` multiple values
if isinstance(until, str) and until in ("next", "last") and combine:
Copy link
Contributor Author

@Jojain Jojain Jan 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If combine is "cut" the conditional test will be True but the executed code won't be what's expected.
That's why I added an explicit condition to check if combine=="cut" and if this True we call the cutBlind method.
I think we should keep my first check

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, missed that. I think it is now fixed.

cadquery/cq.py Outdated Show resolved Hide resolved
@@ -2392,7 +2393,7 @@ def each(
self: T,
callback: Callable[[CQObject], Shape],
useLocalCoordinates: bool = False,
combine: Union[bool, str] = True,
combine: CombineMode = True,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I already discussed my opinion on adding combine arg for this method so I won't talk about it again but in any case if we decide to add it we should probably write somewhere that the new default behaviour is to combine with the base which wasnt the case before. Same remark goes for eachpoint

Copy link
Member

@adam-urbanczyk adam-urbanczyk Jan 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Do you think it would make sense to set it to False? I'm kind of hesitating about it. What do you think @jmwright?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting it to False would make things transparent but we would loose consistency with other methods.
I don't think each and eachpoint are that used so a breaking change here would probably not be a big deal.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I added a note in changes.md

@adam-urbanczyk
Copy link
Member

@jmwright @Jojain I'm done with my adjustments. Please take a final look.

@jmwright
Copy link
Member

@Jojain This looks good to me. Do you think it is ready to merge?

@Jojain
Copy link
Contributor Author

Jojain commented Jan 25, 2022

@jmwright @adam-urbanczyk It's all good for me

@jmwright
Copy link
Member

Thanks!

@jmwright jmwright merged commit e0b4c94 into CadQuery:master Jan 25, 2022
@Jojain Jojain deleted the cut_kw_arg branch January 25, 2022 20:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants