Add close option to Wire.makePolygon

Close wire when creating polygonal face in Sketch
This commit is contained in:
Lorenz Neureuter
2023-02-02 20:30:24 -05:00
parent 58447f8f56
commit 24ae7bc61c
5 changed files with 34 additions and 4 deletions

View File

@ -3816,7 +3816,7 @@ class Workplane(object):
# Creates interpolated plate
f: Face = Face.makeNSidedSurface(
edges if not points else [Wire.makePolygon(points).close()],
edges if not points else [Wire.makePolygon(points, False, True)],
surf_pts,
degree=degree,
nbPtsOnCur=nbPtsOnCur,

View File

@ -2067,14 +2067,23 @@ class Wire(Shape, Mixin1D):
@classmethod
def makePolygon(
cls, listOfVertices: Iterable[VectorLike], forConstruction: bool = False,
cls,
listOfVertices: Iterable[VectorLike],
forConstruction: bool = False,
close: bool = False,
) -> "Wire":
# convert list of tuples into Vectors.
"""
Construct a polygonal wire from points.
"""
wire_builder = BRepBuilderAPI_MakePolygon()
for v in listOfVertices:
wire_builder.Add(Vector(v).toPnt())
if close:
wire_builder.Close()
w = cls(wire_builder.Wire())
w.forConstruction = forConstruction

View File

@ -307,7 +307,9 @@ class Sketch(object):
Construct a polygonal face.
"""
w = Wire.makePolygon(p if isinstance(p, Vector) else Vector(*p) for p in pts)
w = Wire.makePolygon(
(p if isinstance(p, Vector) else Vector(*p) for p in pts), False, True
)
return self.face(w, angle, mode, tag)

View File

@ -1,5 +1,6 @@
# system modules
import math
import pytest
import unittest
from tests import BaseTest
from OCP.gp import gp_Vec, gp_Pnt, gp_Ax2, gp_Circ, gp_Elips, gp, gp_XYZ, gp_Trsf
@ -721,5 +722,18 @@ class TestCadObjects(BaseTest):
self.assertAlmostEqual(many_rad.radius(), 1.0)
@pytest.mark.parametrize(
"points, close, expected_edges",
[
(((0, 0, 0), (0, 1, 0), (1, 0, 0)), False, 2),
(((0, 0, 0), (0, 1, 0), (1, 0, 0)), True, 3),
(((0, 0, 0), (0, 1, 0), (1, 0, 0), (0, 0, 0)), False, 3),
(((0, 0, 0), (0, 1, 0), (1, 0, 0), (0, 0, 0)), True, 3),
],
)
def test_wire_makepolygon(points, close, expected_edges):
assert len(Wire.makePolygon(points, False, close).Edges()) == expected_edges
if __name__ == "__main__":
unittest.main()

View File

@ -5264,6 +5264,11 @@ class TestCadQuery(BaseTest):
self.assertEqual(len(r4.solids().vals()), 1)
r5 = (
Workplane().sketch().polygon([(0, 0), (0, 1), (1, 0)]).finalize().extrude(1)
)
assert r5.val().Volume() == approx(0.5)
def testCircumscribedPolygon(self):
"""
Test that circumscribed polygons result in the correct shapes