def test_polyline_path_transform_interface(hatch, m44): vertices = list(box(1.0, 2.0)) path = hatch.paths.add_polyline_path(vertices) hatch.transform(m44) chk = m44.transform_vertices(vertices) for v, c in zip(path.vertices, chk): assert c.isclose(v)
def add_rectangle(self, rectangle: Rectangle, start_from_x: float = 0.0, start_from_y: float = 0.0) -> None: # Rectangle with evenly spaced holes rect_coordinates = translate(box(rectangle.width, rectangle.height), (start_from_x, start_from_y)) self.msp.add_lwpolyline(rect_coordinates, close=True, dxfattribs=self.attribs) space_minus_offsets_from_side = rectangle.width - ( 2 * rectangle.offset_from_side) space_remaining = space_minus_offsets_from_side - rectangle.holes_total_width if len(rectangle.holes) <= 1: raise ValueError("Need to have at least two holes") space_between_holes = space_remaining / (len(rectangle.holes) - 1) for i, hole in enumerate(rectangle.holes): hole_center_x = (start_from_x + rectangle.offset_from_side + (i * hole.width + (hole.width / 2)) + (i * space_between_holes)) hole_center_y = (start_from_y + rectangle.offset_from_bottom + (hole.height / 2)) if isinstance(hole, Circle): self.msp.add_circle( (hole_center_x, hole_center_y), radius=hole.radius, dxfattribs=self.attribs, ) elif isinstance(hole, Slot): # TODO doesn't work correctly yet # TODO add lines connecting circles left_x = hole_center_x - ( (hole.length / 2) * math.cos(hole.angle)) left_y = hole_center_y + ( (hole.length / 2) * math.sin(hole.angle)) self.msp.add_circle( (left_x, left_y), radius=hole.radius, dxfattribs=self.attribs, ) right_x = hole_center_x + ( (hole.length / 2) * math.cos(hole.angle)) right_y = hole_center_y - ( (hole.length / 2) * math.sin(hole.angle)) self.msp.add_circle( (right_x, right_y), radius=hole.radius, dxfattribs=self.attribs, )
def test_box(): b = box(3, 2) assert len(b) == 4 assert close_vectors(b, [(0, 0), (3, 0), (3, 2), (0, 2)])
doc = ezdxf.new() doc.layers.new("FORMS", dxfattribs={"color": 1}) doc.layers.new("HATCHES") msp = doc.modelspace() attribs = {"layer": "FORMS"} # Create DXF primitives: msp.add_circle((2, 3), radius=2, dxfattribs=attribs) # Ellipse with hole: msp.add_ellipse((5, 0), major_axis=(3, 1), ratio=0.5, dxfattribs=attribs) msp.add_circle((5, 0), radius=1.5, dxfattribs=attribs) # Rectangle with a hole rect = translate(box(3, 2), (3, 6)) msp.add_lwpolyline(rect, close=True, dxfattribs=attribs) hole = translate(box(2, 1), (3.4, 6.4)) msp.add_lwpolyline(hole, close=True, dxfattribs=attribs) # Convert entities to primitives primitives = disassemble.to_primitives(msp) # Collect paths from primitives: paths = [p.path for p in primitives if p.path] # Render this paths as HATCH entities path.render_hatches(msp, paths, dxfattribs={"layer": "HATCHES", "color": 2}) doc.set_modelspace_vport(15, (4, 4)) doc.saveas(DIR / "hatches_from_entities.dxf")
def test_box(): b = box(3, 2) assert len(b) == 4 assert b == (Vec3(0, 0), Vec3(3, 0), Vec3(3, 2), Vec3(0, 2))
import ezdxf from ezdxf.render import forms DIR = Path("~/Desktop/Outbox").expanduser() doc = ezdxf.new(setup=True) msp = doc.modelspace() # Create a special DIMSTYLE for "vertical" centered measurement text: dimstyle = doc.dimstyles.duplicate_entry("EZDXF", "ORD_CENTER") dimstyle.dxf.dimtad = 0 # "vertical" centered measurement text # Add a rectangle: width=4, height = 2.5, lower left corner is WCS(x=2, y=3), # rotated about 30 degrees: origin = Vec3(2, 3) msp.add_lwpolyline(forms.translate(forms.rotate(forms.box(4, 2.5), 30), origin), close=True) # Define the rotated local render UCS. # The origin is the lower-left corner of the rectangle and the axis are # aligned to the rectangle edges: # The y-axis "uy" is calculated automatically by the right-hand rule. ucs = UCS(origin, ux=Vec3.from_deg_angle(30), uz=(0, 0, 1)) # Add a x-type ordinate DIMENSION with local feature locations: # the origin is now the origin of the UCS, which is (0, 0) the default value of # "origin" and the feature coordinates are located in the UCS: msp.add_ordinate_x_dim( # lower left corner feature_location=(0, 0), # feature location in the UCS
from pathlib import Path from ezdxf.math import Vec3 import ezdxf from ezdxf.render import forms DIR = Path("~/Desktop/Outbox").expanduser() # Use argument setup=True to setup the default dimension styles. doc = ezdxf.new(setup=True) # Add new entities to the modelspace: msp = doc.modelspace() # Add a rectangle: width=4, height = 2.5, lower left corner is WCS(x=2, y=3) origin = Vec3(2, 3) msp.add_lwpolyline(forms.translate(forms.box(4, 2.5), origin), close=True) # Add a x-type ordinate DIMENSION with global feature locations: msp.add_ordinate_x_dim( # lower left corner feature_location=origin + (0, 0), # feature location in the WCS offset=(0, -2), # end of leader, relative to the feature location origin=origin, ).render() msp.add_ordinate_x_dim( # lower right corner feature_location=origin + (4, 0), # feature location in the WCS offset=(0, -2), origin=origin, ).render()