getObjectsByLabel - with regex?

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
sdaau_ml
Posts: 35
Joined: Fri Dec 07, 2018 12:18 am

getObjectsByLabel - with regex?

Post by sdaau_ml »

I have read in getObjectsByLabel - FreeCAD Forum:
wmayer wrote: Thu Feb 13, 2014 4:36 pm
Or can I use this call with wildcards to get more objects?
No, you can't. If you like this can be changed to support regular expressions.
I'm not sure what they meant exactly by "this can be changed to support regular expressions." - whether I as user can change it, or whether if there are enough requests the devs might change it.

However, as far I can see from https://github.com/FreeCAD/FreeCAD/blob ... p.cpp#L430 - getObjectsByLabel only supports a plain string as an argument.

And on https://github.com/FreeCAD/FreeCAD/blob ... p.cpp#L447, it says that findObjects does accept a regex, however, it seems to search object names, not object labels.

So is there a function that I could use from Python, that does getObjectsByLabel but with a regex (without having to change C++ source code, that is)?
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: getObjectsByLabel - with regex?

Post by DeepSOIC »

use re module from standard python library. You'll have to iterate through all objects and match their labels to a regex with your code.
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: getObjectsByLabel - with regex?

Post by wmayer »

I'm not sure what they meant exactly by "this can be changed to support regular expressions." - whether I as user can change it, or whether if there are enough requests the devs might change it.
After all the OP didn't make a request that getObjectsByLabel should support regular expressions and thus it was never implemented. But as DeepSOIC already said you can do this on your own, too. And at the end it doesn't matter if you do this in your script or if it's done by FreeCAD internally.
sdaau_ml
Posts: 35
Joined: Fri Dec 07, 2018 12:18 am

Re: getObjectsByLabel - with regex?

Post by sdaau_ml »

Hi @DeepSOIC, @wmayer

Thanks a ton for the responses!
wmayer wrote: Fri Jan 11, 2019 6:58 pm After all the OP didn't make a request that getObjectsByLabel should support regular expressions and thus it was never implemented.
Ok, now I get it - much appreciated!
DeepSOIC wrote: Fri Jan 11, 2019 4:19 pm use re module from standard python library. You'll have to iterate through all objects and match their labels to a regex with your code.
That's what I wanted to know too - whether I have to iterate myself; thanks for specifying this!
wmayer wrote: Fri Jan 11, 2019 6:58 pm And at the end it doesn't matter if you do this in your script or if it's done by FreeCAD internally.
Well, it could be a regex lookup in a loop might be faster in C++ than in Python, but that's just a hunch, I don't really have any numbers to back that up. But for the kind of one-off scripts where I currently need this, it really doesn't matter. :)
Post Reply