Exemple #1
0
def test_get_start_and_end_vertex():

    ellipse = Ellipse.new(handle='ABBA',
                          owner='0',
                          dxfattribs={
                              'center': (1, 2, 3),
                              'major_axis': (4, 3, 0),
                              'ratio': .7,
                              'start_param': math.pi / 2,
                              'end_param': math.pi,
                              'extrusion': (0, 0, -1),
                          })

    start, end = list(
        ellipse.vertices([
            ellipse.dxf.start_param,
            ellipse.dxf.end_param,
        ]))
    # test values from BricsCAD
    assert start.isclose(Vector(3.1, -0.8, 3), abs_tol=1e-6)
    assert end.isclose(Vector(-3, -1, 3), abs_tol=1e-6)

    # for convenience, but Ellipse.vertices is much more efficient:
    assert ellipse.start_point.isclose(Vector(3.1, -0.8, 3), abs_tol=1e-6)
    assert ellipse.end_point.isclose(Vector(-3, -1, 3), abs_tol=1e-6)
Exemple #2
0
def test_ellipse_to_code():
    from ezdxf.entities.ellipse import Ellipse

    entity = Ellipse.new(
        handle="ABBA",
        owner="0",
        dxfattribs={
            "color": "7",
            "center": (1, 2, 3),
            "major_axis": (2, 0, 0),
            "ratio": 0.5,
            "start_param": 1,
            "end_param": 3,
        },
    )
    new_entity = translate_to_code_and_execute(entity)
    for name in (
            "color",
            "center",
            "major_axis",
            "ratio",
            "start_param",
            "end_param",
    ):
        assert new_entity.get_dxf_attrib(name) == entity.get_dxf_attrib(name)
Exemple #3
0
def test_get_start_and_end_vertex():
    ellipse = Ellipse.new(
        handle="ABBA",
        owner="0",
        dxfattribs={
            "center": (1, 2, 3),
            "major_axis": (4, 3, 0),
            "ratio": 0.7,
            "start_param": math.pi / 2,
            "end_param": math.pi,
            "extrusion": (0, 0, -1),
        },
    )

    start, end = list(
        ellipse.vertices([
            ellipse.dxf.start_param,
            ellipse.dxf.end_param,
        ]))
    # test values from BricsCAD
    assert start.isclose(Vec3(3.1, -0.8, 3), abs_tol=1e-6)
    assert end.isclose(Vec3(-3, -1, 3), abs_tol=1e-6)

    # for convenience, but Ellipse.vertices is much more efficient:
    assert ellipse.start_point.isclose(Vec3(3.1, -0.8, 3), abs_tol=1e-6)
    assert ellipse.end_point.isclose(Vec3(-3, -1, 3), abs_tol=1e-6)
Exemple #4
0
def test_from_arc():
    from ezdxf.entities.arc import Arc
    arc = Arc.new(dxfattribs={'center': (2, 2, 2), 'radius': 3})
    ellipse = Ellipse.from_arc(arc)
    assert ellipse.dxf.center == (2, 2, 2)
    assert ellipse.dxf.major_axis == (3, 0, 0)
    assert ellipse.dxf.ratio == 1
    assert ellipse.dxf.start_param == 0
    assert math.isclose(ellipse.dxf.end_param, math.pi * 2)
Exemple #5
0
def test_malformed_ellipse():
    ellipse = Ellipse.from_text(MALFORMED_ELLIPSE)
    assert ellipse.dxf.layer == "LY_EZDXF"
    assert ellipse.dxf.linetype == "LT_EZDXF"
    assert ellipse.dxf.color == 7
    assert ellipse.dxf.center.isclose((1, 2, 3))
    assert ellipse.dxf.major_axis.isclose((1, 0, 0))
    assert ellipse.dxf.ratio == 1
    assert ellipse.dxf.start_param == 0
    assert ellipse.dxf.end_param == math.pi * 2
Exemple #6
0
def test_swap_axis_full_ellipse():
    ellipse = Ellipse.new(dxfattribs={
        'major_axis': (5, 0, 0),
        'ratio': 2,  # > 1 is not valid
    })
    assert ellipse.minor_axis.isclose((0, 10, 0))

    ellipse.swap_axis()
    assert ellipse.dxf.ratio == 0.5
    assert ellipse.dxf.major_axis == (0, 10, 0)
    assert ellipse.minor_axis == (-5, 0, 0)
    assert ellipse.dxf.start_param == 0
    assert ellipse.dxf.end_param == math.pi * 2
Exemple #7
0
def test_ellipse_to_code():
    from ezdxf.entities.ellipse import Ellipse
    entity = Ellipse.new(handle='ABBA', owner='0', dxfattribs={
        'color': '7',
        'center': (1, 2, 3),
        'major_axis': (2, 0, 0),
        'ratio': .5,
        'start_param': 1,
        'end_param': 3,
    })
    new_entity = translate_to_code_and_execute(entity)
    for name in ('color', 'center', 'major_axis', 'ratio', 'start_param', 'end_param'):
        assert new_entity.get_dxf_attrib(name) == entity.get_dxf_attrib(name)
Exemple #8
0
def test_default_new():
    entity = Ellipse.new(handle='ABBA', owner='0', dxfattribs={
        'color': 7,
        'ratio': 2,
        'center': (1, 2, 3),
        'major_axis': (4, 5, 6),
        'start_param': 10,
        'end_param': 20,
    })
    assert entity.dxf.layer == '0'
    assert entity.dxf.color == 7
    assert entity.dxf.center == (1, 2, 3)
    assert entity.dxf.major_axis == (4, 5, 6)
    assert entity.dxf.ratio == 2
    assert entity.dxf.start_param == 10
    assert entity.dxf.end_param == 20
Exemple #9
0
def test_default_new():
    entity = Ellipse.new(
        handle="ABBA",
        owner="0",
        dxfattribs={
            "color": 7,
            "ratio": 0.5,
            "center": (1, 2, 3),
            "major_axis": (4, 5, 6),
            "start_param": 10,
            "end_param": 20,
        },
    )
    assert entity.dxf.layer == "0"
    assert entity.dxf.color == 7
    assert entity.dxf.center == (1, 2, 3)
    assert entity.dxf.major_axis == (4, 5, 6)
    assert entity.dxf.ratio == 0.5
    assert entity.dxf.start_param == 10
    assert entity.dxf.end_param == 20
Exemple #10
0
def test_angle_to_param():
    random_tests_count = 100
    random.seed(0)

    angle = 1.23
    assert math.isclose(angle_to_param(1.0, angle), angle)

    angle = 1.23 + math.pi / 2
    assert math.isclose(angle_to_param(1.0, angle), angle)

    angle = 1.23 + math.pi
    assert math.isclose(angle_to_param(1.0, angle), angle)

    angle = 1.23 + 3 * math.pi / 2
    assert math.isclose(angle_to_param(1.0, angle), angle)

    angle = math.pi / 2 + 1e-15
    assert math.isclose(angle_to_param(1.0, angle), angle)

    for _ in range(random_tests_count):
        ratio = random.uniform(1e-6, 1)
        angle = random.uniform(0, math.tau)
        param = angle_to_param(ratio, angle)
        ellipse = Ellipse.new(
            dxfattribs={
                # avoid (0, 0, 0) as major axis
                'major_axis': (non_zero_random(), non_zero_random(), 0),
                'ratio': ratio,
                'start_param': 0,
                'end_param': param,
                'extrusion': (0, 0, random.choice((1, -1))),
            })
        calculated_angle = ellipse.dxf.extrusion.angle_about(
            ellipse.dxf.major_axis, ellipse.end_point)
        calculated_angle_without_direction = ellipse.dxf.major_axis.angle_between(
            ellipse.end_point)
        assert math.isclose(calculated_angle, angle, abs_tol=1e-9)
        assert (math.isclose(calculated_angle,
                             calculated_angle_without_direction)
                or math.isclose(math.tau - calculated_angle,
                                calculated_angle_without_direction))
Exemple #11
0
def test_swap_axis_arbitrary_params():
    random_tests_count = 100
    random.seed(0)

    for _ in range(random_tests_count):
        ellipse = Ellipse.new(
            dxfattribs={
                # avoid (0, 0, 0) as major axis
                'major_axis': (non_zero_random(), non_zero_random(), 0),
                'ratio': 2,
                'start_param': random.uniform(0, math.tau),
                'end_param': random.uniform(0, math.tau),
                'extrusion': (0, 0, random.choice((1, -1))),
            })

        # Test if coordinates of start- and end point stay at the same location
        # before and after swapping axis.
        start_point = ellipse.start_point
        end_point = ellipse.end_point
        minor_axis = ellipse.minor_axis
        ellipse.swap_axis()
        assert ellipse.dxf.major_axis.isclose(minor_axis, abs_tol=1e-9)
        assert ellipse.start_point.isclose(start_point, abs_tol=1e-9)
        assert ellipse.end_point.isclose(end_point, abs_tol=1e-9)
Exemple #12
0
def test_default_init():
    entity = Ellipse()
    assert entity.dxftype() == 'ELLIPSE'
    assert entity.dxf.handle is None
    assert entity.dxf.owner is None
Exemple #13
0
def entity():
    return Ellipse.from_text(ELLIPSE)
Exemple #14
0
def test_ratio_can_be_negative(ratio):
    e = Ellipse.new(dxfattribs={'ratio': ratio})
    assert e.dxf.ratio < 0
Exemple #15
0
def test_ratio_is_always_valid(ratio):
    e = Ellipse.new(dxfattribs={'ratio': ratio})
    assert MIN_RATIO <= abs(e.dxf.ratio) <= MAX_RATIO
Exemple #16
0
def test_write_dxf():
    entity = Ellipse.from_text(ELLIPSE)
    result = TagCollector.dxftags(entity)
    expected = basic_tags_from_text(ELLIPSE)
    assert result == expected
Exemple #17
0
def test_extrusion_can_not_be_a_null_vector():
    e = Ellipse.new(dxfattribs={'extrusion': (0, 0, 0)})
    assert e.dxf.extrusion == (0, 0, 1), 'expected default extrusion'