def main(): doc = ezdxf.new() doc.layers.add(ENTITIES, color=colors.YELLOW) doc.layers.add(INTERSECTION_POINTS, color=colors.RED) doc.layers.add(CURVE_APPROXIMATIONS, color=colors.CYAN) msp = doc.modelspace() ellipse = msp.add_ellipse( center=(0, 0), major_axis=(3, 0), ratio=0.5, dxfattribs=GfxAttribs(layer=ENTITIES), ) fit_points = [(-4, -4), (-2, -1), (2, 1), (4, 4)] spline = msp.add_spline_control_frame( fit_points, dxfattribs=GfxAttribs(layer=ENTITIES) ) p1 = Vec2.list(ellipse.flattening(distance=0.001)) p2 = Vec2.list(spline.flattening(distance=0.001)) msp.add_lwpolyline(p1, dxfattribs=GfxAttribs(layer=CURVE_APPROXIMATIONS)) msp.add_lwpolyline(p2, dxfattribs=GfxAttribs(layer=CURVE_APPROXIMATIONS)) res = intersect_polylines_2d(p1, p2) for point in res: msp.add_circle( center=point, radius=0.1, dxfattribs=GfxAttribs(layer=INTERSECTION_POINTS), ) doc.set_modelspace_vport(height=10) doc.saveas(DIR / "intersect_ellipse_and_spline.dxf")
def test_update_dxf_attributes_from_gfx_attribs(): attribs = GfxAttribs( layer="Test", color=3, rgb=(10, 20, 30), transparency=0.3019607843137255, linetype="SOLID", lineweight=50, ltscale=3.0, ) line = factory.new("LINE") line.update_dxf_attribs(dict(attribs)) assert attribs.layer == line.dxf.layer assert attribs.color == line.dxf.color assert attribs.rgb == ezdxf.colors.int2rgb(line.dxf.true_color) assert attribs.transparency == ezdxf.colors.transparency2float( line.dxf.transparency) assert attribs.linetype == line.dxf.linetype assert attribs.ltscale == line.dxf.ltscale line = factory.new("LINE") line.dxf.update(dict(attribs)) assert attribs.layer == line.dxf.layer assert attribs.color == line.dxf.color assert attribs.rgb == ezdxf.colors.int2rgb(line.dxf.true_color) assert attribs.transparency == ezdxf.colors.transparency2float( line.dxf.transparency) assert attribs.linetype == line.dxf.linetype assert attribs.ltscale == line.dxf.ltscale
def simple_mtext_content_vertical(name: str): doc = ezdxf.new(DXFVERSION, setup=True) msp = doc.modelspace() ml_builder = msp.add_multileader_mtext("Standard") ml_builder.set_content("Line1\nLine2", style="OpenSans") # Construction plane of the entity is defined by an render UCS. # The default render UCS is the WCS. # The leader lines vertices are expected in render UCS coordinates, which # means relative to the UCS origin! # This example shows the simplest way UCS==WCS! ml_builder.set_connection_types( top=mleader.VerticalConnection.center_overline, bottom=mleader.VerticalConnection.center_overline, ) ml_builder.add_leader_line(mleader.ConnectionSide.top, [Vec2(15, 40)]) ml_builder.add_leader_line(mleader.ConnectionSide.bottom, [Vec2(15, -40)]) # The insert point (in UCS coordinates= is the alignment point for MTEXT # content and the insert location for BLOCK content: ml_builder.build(insert=Vec2(5, 0)) msp.add_circle( ml_builder.multileader.context.base_point, radius=0.5, dxfattribs=GfxAttribs(color=colors.RED), ) doc.set_modelspace_vport(60, center=(10, 5)) doc.saveas(OUTDIR / f"{name}_{DXFVERSION}.dxf")
def create_block( doc: "Drawing", size: float, margin: float, base_point: Vec2 = NULLVEC ) -> "BlockLayout": attribs = GfxAttribs(color=colors.RED) block = doc.blocks.new("SQUARE", base_point=base_point) block.add_lwpolyline(forms.square(size), close=True, dxfattribs=attribs) attdef_attribs = dict(attribs) attdef_attribs["height"] = 1.0 attdef_attribs["style"] = "OpenSans" tag = "ONE" attdef_attribs["prompt"] = tag bottom_left_attdef = block.add_attdef( tag, text=tag, dxfattribs=attdef_attribs ) bottom_left_attdef.set_placement( (margin, margin), align=TextEntityAlignment.BOTTOM_LEFT ) tag = "TWO" attdef_attribs["prompt"] = tag top_right_attdef = block.add_attdef( tag, text=tag, dxfattribs=attdef_attribs ) top_right_attdef.set_placement( (size - margin, size - margin), align=TextEntityAlignment.TOP_RIGHT ) return block
def test_as_dict_default_values(self): assert GfxAttribs().asdict(default_values=True) == { "layer": gfxattribs.DEFAULT_LAYER, "color": gfxattribs.DEFAULT_ACI_COLOR, "linetype": gfxattribs.DEFAULT_LINETYPE, "lineweight": gfxattribs.DEFAULT_LINEWEIGHT, "ltscale": gfxattribs.DEFAULT_LTSCALE, }
def test_write_back_header_defaults(): doc = ezdxf.new() doc.layers.new("Test") doc.linetypes.new("SOLID") attribs = GfxAttribs( layer="Test", color=1, linetype="SOLID", lineweight=50, ltscale=2, ) attribs.write_to_header(doc) assert doc.header["$CLAYER"] == "Test" assert doc.header["$CECOLOR"] == 1 assert doc.header["$CELTYPE"] == "SOLID" assert doc.header["$CELWEIGHT"] == 50 assert doc.header["$CELTSCALE"] == 2.0
def test_load_header_defaults(): doc = ezdxf.new() attribs = GfxAttribs.load_from_header(doc) assert attribs.layer == "0" assert attribs.color == ezdxf.colors.BYLAYER assert attribs.linetype == "ByLayer" assert attribs.lineweight == ezdxf.const.LINEWEIGHT_BYLAYER assert attribs.ltscale == 1.0
def test_default_init(self): attribs = GfxAttribs() assert attribs.layer == "0" assert attribs.color == ezdxf.colors.BYLAYER assert attribs.rgb is None assert attribs.linetype == "ByLayer" assert attribs.lineweight == ezdxf.const.LINEWEIGHT_BYLAYER assert attribs.transparency is None assert attribs.ltscale == 1.0
def test_from_entity(): line = factory.new( "LINE", dict( GfxAttribs( layer="Test", color=3, rgb=(10, 20, 30), transparency=0.3019607843137255, linetype="SOLID", lineweight=50, ltscale=3.0, )), ) attribs = GfxAttribs.from_entity(line) assert attribs.layer == "Test" assert attribs.color == 3 assert attribs.rgb == (10, 20, 30) assert attribs.transparency == 0.3019607843137255 assert attribs.linetype == "SOLID" assert attribs.ltscale == 3.0
def test_gfx_attribs_as_dict(): attribs = GfxAttribs( layer="Test", color=1, rgb=(0xA, 0xB, 0xC), linetype="SOLID", lineweight=50, transparency=0.5, ltscale=2, ) expected = { "layer": "Test", "color": 1, "true_color": 0x0A0B0C, "linetype": "SOLID", "lineweight": 50, "transparency": 0x200007F, "ltscale": 2.0, } assert sorted(attribs.items()) == sorted(expected.items()) assert attribs.asdict() == expected assert dict(attribs) == expected
def quick_mtext_horizontal(name: str): doc = ezdxf.new(DXFVERSION, setup=True) mleaderstyle = doc.mleader_styles.duplicate_entry("Standard", "EZDXF") mleaderstyle.set_mtext_style("OpenSans") # type: ignore msp = doc.modelspace() target_point = Vec2(40, 15) msp.add_circle( target_point, radius=0.5, dxfattribs=GfxAttribs(color=colors.RED) ) for angle in [45, 135, 225, -45]: ml_builder = msp.add_multileader_mtext("EZDXF") ml_builder.quick_leader( "Line1\nLine2", target=target_point, segment1=Vec2.from_deg_angle(angle, 14), ) doc.set_modelspace_vport(60, center=(10, 5)) doc.saveas(OUTDIR / f"{name}_{DXFVERSION}.dxf")
def test_as_dict(self): assert dict(GfxAttribs()) == dict()
def test_set_value(self): attribs = GfxAttribs() attribs.layer = "Test" assert attribs.layer == "Test"
def test_init_by_value(self): assert GfxAttribs(rgb=(10, 20, 30)).rgb == (10, 20, 30)
def test_init_by_invalid_value_raises_exception(self): """RGB color tests see validator test suite 020: is_valid_rgb()""" with pytest.raises(ezdxf.DXFValueError): assert GfxAttribs(rgb=(-1, 0, 0))
def test_str(self): assert str(GfxAttribs(color=1)) == "color=1"
def test_repr(self): assert repr(GfxAttribs(color=1)) == "GfxAttribs(color=1)"
def test_set_value(self): attribs = GfxAttribs() attribs.color = ezdxf.colors.RED assert attribs.color == ezdxf.colors.RED
def test_set_invalid_value_raises_exception(self): attribs = GfxAttribs() with pytest.raises(ezdxf.DXFValueError): attribs.color = 300
def test_init_by_value(self): assert GfxAttribs(color=ezdxf.colors.RED).color == ezdxf.colors.RED
def test_init_by_invalid_value_raises_exception(self): """ACI color tests see validator test suite 020: is_valid_aci_color()""" with pytest.raises(ezdxf.DXFValueError): assert GfxAttribs(color=300)
def test_gfx_attribs_string(): attribs = GfxAttribs(layer="Test", color=1, ltscale=2) assert str(attribs) == "layer='Test', color=1, ltscale=2.0"
def test_gfx_attribs_repr(): attribs = GfxAttribs(layer="Test", color=1, ltscale=2) assert repr(attribs) == "GfxAttribs(layer='Test', color=1, ltscale=2.0)"
def test_update_transparency_by_block_from_gfx_attribs(): attribs = GfxAttribs(transparency=gfxattribs.TRANSPARENCY_BYBLOCK, ) line = factory.new("LINE") line.dxf.update(dict(attribs)) assert line.dxf.transparency == ezdxf.colors.TRANSPARENCY_BYBLOCK
def test_init_by_invalid_value_raises_exception(self): with pytest.raises(ezdxf.DXFValueError): assert GfxAttribs(layer="*Test")
def test_init_by_value(self): assert GfxAttribs(layer="Test").layer == "Test"
def all_mtext_content_horizontal(name: str): doc = ezdxf.new(DXFVERSION, setup=True) msp = doc.modelspace() attribs = GfxAttribs(color=colors.RED) for direction in range(9): for ax, alignment in enumerate( [ mleader.TextAlignment.left, mleader.TextAlignment.center, mleader.TextAlignment.right, ] ): ml_builder = msp.add_multileader_mtext("Standard") dir_enum = mleader.HorizontalConnection(direction) ml_builder.set_connection_types( left=dir_enum, right=dir_enum, ) ml_builder.set_content( f"{dir_enum.name}\n{dir_enum.name}\n{dir_enum.name}", alignment=alignment, style="OpenSans", ) width = len(dir_enum.name) * 2.5 x0 = -30 x1 = 40 + width y0 = -15 y1 = 20 ml_builder.add_leader_line( mleader.ConnectionSide.right, [Vec2(x1, y1)] ) ml_builder.add_leader_line( mleader.ConnectionSide.right, [Vec2(x1, y0)] ) ml_builder.add_leader_line( mleader.ConnectionSide.left, [Vec2(x0, y0)] ) ml_builder.add_leader_line( mleader.ConnectionSide.left, [Vec2(x0, y1)] ) x = 5 if alignment == mleader.TextAlignment.center: x += width / 2 elif alignment == mleader.TextAlignment.right: x += width ucs = UCS(origin=(ax * 250, direction * 50)) insert = Vec2(x, y1 / 2) ml_builder.build(insert=insert, ucs=ucs) e = msp.add_lwpolyline( [(x0, y0), (x1, y0), (x1, y1), (x0, y1)], close=True, dxfattribs=attribs, ) e.transform(ucs.matrix) e = msp.add_circle(insert, radius=0.5, dxfattribs=attribs) e.transform(ucs.matrix) height = 600 doc.set_modelspace_vport(height, center=(200, height / 2)) doc.saveas(OUTDIR / f"{name}_{DXFVERSION}.dxf")
def test_str(self): assert str(GfxAttribs(layer="Test")) == "layer='Test'"
# License: MIT License from pathlib import Path import ezdxf from ezdxf import colors from ezdxf.gfxattribs import GfxAttribs from ezdxf.render import forms DIR = Path("~/Desktop/Outbox").expanduser() cube = forms.cube().scale_uniform(10).subdivide(2) cylinder = forms.cylinder(12, radius=5, top_center=(0, 0, 10)).translate(0, 20) doc = ezdxf.new() msp = doc.modelspace() red = GfxAttribs(color=colors.RED) green = GfxAttribs(color=colors.GREEN) blue = GfxAttribs(color=colors.BLUE) cube.render(msp, dxfattribs=red) cube.translate(20) cube.render_polyface(msp, dxfattribs=green) cube.translate(20) cube.render_3dfaces(msp, dxfattribs=blue) cylinder.render(msp, dxfattribs=red) cylinder.translate(20) cylinder.render_polyface(msp, dxfattribs=green) cylinder.translate(20) cylinder.render_3dfaces(msp, dxfattribs=blue)
def test_repr(self): assert repr(GfxAttribs(layer="Test")) == "GfxAttribs(layer='Test')"