How to run 'refine shape' from python

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
RandomPipeBender
Posts: 18
Joined: Wed May 05, 2021 12:33 am

How to run 'refine shape' from python

Post by RandomPipeBender »

Hi everyone,

I have a part that's constructed by joining two parts via 'fuse':

Code: Select all

part = face.extrude(Base.Vector(x, 0, 0))

anotherface = Part.Face(w)
anotherpart = anotherface.extrude(Base.Vector(some_other_x, 0, 0 ))

union = part.fuse(anotherpart)
union.removeSplitter()
Part.show(union)
Unfortunately this still has a 'seam' where the two original parts were joined:
Screenshot_20210518_081723.png
Screenshot_20210518_081723.png (4.48 KiB) Viewed 1965 times
This seam disappears after running Part -> Create a copy -> Refine shape manually in the GUI. According to https://wiki.freecadweb.org/Part_RefineShape/en the equivalent in python is to run 'removeSplitter()' on the part in question but that doesn't seem to work (see code above). Am I missing something? Any pointers would be greatly appreciated.

Just in case: I'm running freecad 0.19.2 on Fedora 34 which is built against opencascade 7.5.0.
User avatar
onekk
Veteran
Posts: 6222
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: How to run 'refine shape' from python

Post by onekk »

Try to do:

Code: Select all

union = part.fuse(anotherpart).removeSplitter()
Part.show(union)
But if you have shown the original thing ie anotherpart or part you will have two solid visualized so you have at lest hide "original" solids.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
RandomPipeBender
Posts: 18
Joined: Wed May 05, 2021 12:33 am

Re: How to run 'refine shape' from python

Post by RandomPipeBender »

onekk wrote: Tue May 18, 2021 6:45 am Try to do:

Code: Select all

union = part.fuse(anotherpart).removeSplitter()
Part.show(union)
But if you have shown the original thing ie anotherpart or part you will have two solid visualized so you have at lest hide "original" solids.
I see. That worked, thank you very much. It wasn't quite clear from the description in the wiki if 'removeSplitter()' has a side effect on the part on which it is called or it would return a modified part. I guess I should have check https://freecad.github.io/SourceDoc/d2/ ... ePart.html?
User avatar
Chris_G
Veteran
Posts: 2601
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: How to run 'refine shape' from python

Post by Chris_G »

removeSplitter() doesn't modify the shape, it creates a new shape.
So, you should do :

Code: Select all

cleaned_shape = union.removeSplitter()
Part.show(cleaned_shape)
EDIT : Nevermind, I was too slow :lol:
chrisb
Veteran
Posts: 54302
Joined: Tue Mar 17, 2015 9:14 am

Re: How to run 'refine shape' from python

Post by chrisb »

You may also consider to set the Refine property to true in the union.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
RandomPipeBender
Posts: 18
Joined: Wed May 05, 2021 12:33 am

Re: How to run 'refine shape' from python

Post by RandomPipeBender »

chrisb wrote: Tue May 18, 2021 7:42 am You may also consider to set the Refine property to true in the union.
Thanks! I there a piece of documentation listing the available properties for the 'fuse' function? If it's not asking too much, could you tell me how to set this property.

Code: Select all

union = part.fuse(part2, Refine=True)
Doesn't work and I can't find any details in the wiki.
chrisb
Veteran
Posts: 54302
Joined: Tue Mar 17, 2015 9:14 am

Re: How to run 'refine shape' from python

Post by chrisb »

RandomPipeBender wrote: Tue May 18, 2021 12:56 pm If it's not asking too much, could you tell me how to set this property.
Of course it's not too much, but I am no expert in this. I don't know how to do this in a single step, so I would do it in two:

Code: Select all

union = part.fuse(part2, Refine=True)
union.Refine = True
I think that all Properties which you see in the GUI's data and view tabs are available in this way.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
RandomPipeBender
Posts: 18
Joined: Wed May 05, 2021 12:33 am

Re: How to run 'refine shape' from python

Post by RandomPipeBender »

chrisb wrote: Tue May 18, 2021 5:02 pm

Code: Select all

union = part.fuse(part2, Refine=True)
union.Refine = True
I think that all Properties which you see in the GUI's data and view tabs are available in this way.
Both don't work for me. The first one throws:

Code: Select all

<class 'TypeError'>: fuse() takes no keyword arguments
The second one:

Code: Select all

<class 'AttributeError'>: 'Part.Shape' object has no attribute 'Refine'
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: How to run 'refine shape' from python

Post by openBrain »

Beware not to mix up things.

'Fusion' as FreeCAD feature/operation (that acts on objects) has a 'Refine' property.

'fuse()' as a function that applies on shapes (TopoShape) --what you're using here-- doesn't have such an option. You have to post process the resulting shape of the fuse function with 'removeSplitter()'.
chrisb
Veteran
Posts: 54302
Joined: Tue Mar 17, 2015 9:14 am

Re: How to run 'refine shape' from python

Post by chrisb »

Sorry! I had tested it here and then transferred it to the names you used - and got the wrong post with your not working example. You have to set Refine for the fusion you created, so it should be

Code: Select all

part = face.extrude(Base.Vector(x, 0, 0))

anotherface = Part.Face(w)
anotherpart = anotherface.extrude(Base.Vector(some_other_x, 0, 0 ))

union = part.fuse(anotherpart)
union.Refine = True
Only the last line is from me.

Edit: Obviously does openBrain know much better what's going on. :D
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
Post Reply