[solved] Cut two polygons in 2D

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
reox
Posts: 929
Joined: Sat Aug 13, 2016 10:06 am
Contact:

[solved] Cut two polygons in 2D

Post by reox »

I have this small example:

Code: Select all

import Part
i_wire = Part.makePolygon([
    App.Vector(0.2, 0.5, 0),
    App.Vector(0.5, 1.2, 0),
    App.Vector(1.2, 1.1, 0),
    App.Vector(0.8, 0.5, 0),
    App.Vector(0.2, 0.5, 0),
])
o_wire = Part.makePolygon([
    App.Vector(0,0,0),
    App.Vector(0,1,0),
    App.Vector(1,1,0),
    App.Vector(1,0,0),
    App.Vector(0,0,0),
])

Part.show(i_wire.cut(o_wire)))
I would like to get the following polygon afterwards:
[(0.2, 0.5, 0.0), (0.41428571428571426, 1.0, 0.0), (1.0, 1.0, 0.0), (1.0, 0.8, 0.0), (0.5, 1.2, 0.0)]

However, I get this polygon instead:
[(0.2, 0.5, 0.0), (0.41428571428571426, 1.0, 0.0), (0.8, 0.5, 0.0), (1.0, 0.8, 0.0), (1.2, 1.1, 0.0), (0.5, 1.2, 0.0)]
Which is the inner one including the two intersection points.

Is it possible to get the cut from these two polygons?
The only requirements I have are:
* 2D only
* I have the vertices of the two polygons
* I want the vertices of the output polygon
Last edited by reox on Sat Feb 22, 2020 5:45 pm, edited 1 time in total.
mario52
Veteran
Posts: 4692
Joined: Wed May 16, 2012 2:13 pm

Re: Cut two polygons in 2D

Post by mario52 »

hi

Find intersection of a line with a Part2DObject

Code: Select all

s = FreeCADGui.Selection.getSelection()
sect = s[0].Shape.section(s[1].Shape)
points = sect.Vertexes
for i in points:
    print(i.Point)
other How to intersect a line with a part

Intersection between two Draft objects

Code: Select all

DraftGeomUtils.findIntersection(line1.Shape,line2.Shape)
mario
Last edited by mario52 on Sat Feb 22, 2020 2:07 pm, edited 1 time in total.
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
reox
Posts: 929
Joined: Sat Aug 13, 2016 10:06 am
Contact:

Re: Cut two polygons in 2D

Post by reox »

But these only give me the intersection points:

Code: Select all

>>> print([(x.X, x.Y) for x in  i_wire.section(o_wire).Vertexes])
[(0.41428571428571426, 1.0), (1.0, 0.8)]
I would like to have the full polygon. I sketeched that really quick; I want to get the hatched polygon, i.e. the 5 vertices forming it:
20200222_150251_sw.png
20200222_150251_sw.png (82 KiB) Viewed 524 times
User avatar
wandererfan
Veteran
Posts: 6315
Joined: Tue Nov 06, 2012 5:42 pm
Contact:

Re: Cut two polygons in 2D

Post by wandererfan »

reox wrote: Sat Feb 22, 2020 2:05 pm I would like to have the full polygon. I sketeched that really quick; I want to get the hatched polygon, i.e. the 5 vertices forming it:
How about:
Polygons -> Faces, Common(Face1, Face2), Common->OuterWire, OuterWire->Vertexes, Vertexes->Polygon.

Might not need the OuterWire steps.
reox
Posts: 929
Joined: Sat Aug 13, 2016 10:06 am
Contact:

Re: Cut two polygons in 2D

Post by reox »

wandererfan wrote: Sat Feb 22, 2020 2:57 pm Polygons -> Faces, Common(Face1, Face2), Common->OuterWire, OuterWire->Vertexes, Vertexes->Polygon.
Yes! That works!

Code: Select all

import Part

inner = [
    App.Vector(0.2, 0.5, 0),
    App.Vector(0.5, 1.2, 0),
    App.Vector(1.2, 1.1, 0),
    App.Vector(0.8, 0.5, 0),
    App.Vector(0.2, 0.5, 0),
]

outer = [
    App.Vector(0,0,0),
    App.Vector(0,1,0),
    App.Vector(1,1,0),
    App.Vector(1,0,0),
    App.Vector(0,0,0),
]

i_wire = Part.makeFace(Part.makePolygon(inner), 'Part::FaceMakerSimple')
o_wire = Part.makeFace(Part.makePolygon(outer), 'Part::FaceMakerSimple')

section = i_wire.common(o_wire)
assert len(section.Wires) == 1, 'there are none or multiple wires!'

print([(x.X, x.Y) for x in section.Wires[0].OrderedVertexes])

gives:
[(0.8, 0.5), (0.2, 0.5), (0.41428571428571426, 1.0), (1.0, 1.0), (1.0, 0.8)]

Thanks a lot!
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: Cut two polygons in 2D

Post by vocx »

reox wrote: Sat Feb 22, 2020 4:58 pm Yes! That works!
This seems to be the same as using the Part_Common tool. Although it seems to be intended for 3D objects it also works for 2D objects. It should work, I think, for every object that has a Part_TopoShape, so essentially all geometrical objects created in FreeCAD.
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
reox
Posts: 929
Joined: Sat Aug 13, 2016 10:06 am
Contact:

Re: Cut two polygons in 2D

Post by reox »

vocx wrote: Mon Feb 24, 2020 2:51 am
reox wrote: Sat Feb 22, 2020 4:58 pm Yes! That works!
This seems to be the same as using the Part_Common tool. Although it seems to be intended for 3D objects it also works for 2D objects. It should work, I think, for every object that has a Part_TopoShape, so essentially all geometrical objects created in FreeCAD.
I tried Part.Wire.common() before (which I also thought would be the operation I needed), but it returned an empty shape instead.
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: Cut two polygons in 2D

Post by vocx »

reox wrote: Mon Feb 24, 2020 8:12 am ...
I tried Part.Wire.common() before (which I also thought would be the operation I needed), but it returned an empty shape instead.
Use Part_Common and see the generated code. It creates a third object, which uses the two other objects internally.
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
Post Reply