def draw_hatch_entity(self, entity: DXFGraphic, properties: Properties) -> None: entity = cast(Hatch, entity) ocs = entity.ocs() # all OCS coordinates have the same z-axis stored as vector (0, 0, z), default (0, 0, 0) elevation = entity.dxf.elevation.z paths = copy.deepcopy(entity.paths) paths.polyline_to_edge_path(just_with_bulge=False) # For hatches, the approximation don't have to be that precise. paths.all_to_line_edges(num=64, spline_factor=8) for p in paths: assert p.PATH_TYPE == 'EdgePath' vertices = [] last_vertex = None for e in p.edges: assert e.EDGE_TYPE == 'LineEdge' start, end = ocs.points_to_wcs([ Vector(e.start[0], e.start[1], elevation), Vector(e.end[0], e.end[1], elevation), ]) if last_vertex is None: vertices.append(start) elif not last_vertex.isclose(start): print( f'warning: {str(entity)} edges not contiguous: {last_vertex} -> {start}' ) vertices.append(start) vertices.append(end) last_vertex = end if vertices: if last_vertex.isclose(vertices[0]): vertices.append(last_vertex) self.out.draw_filled_polygon(vertices, properties)
def _get_arc_wcs_center(arc: DXFGraphic) -> Vector: """ Returns the center of an ARC or CIRCLE as WCS coordinates. """ center = arc.dxf.center if arc.dxf.hasattr('extrusion'): ocs = arc.ocs() return ocs.to_wcs(center) else: return center
def draw_solid_entity(self, entity: DXFGraphic) -> None: # Handles SOLID, TRACE and 3DFACE dxf, dxftype = entity.dxf, entity.dxftype() properties = self._resolve_properties(entity) points = get_tri_or_quad_points(entity, adjust_order=dxftype != '3DFACE') # TRACE is an OCS entity if dxftype == 'TRACE' and dxf.hasattr('extrusion'): ocs = entity.ocs() points = list(ocs.points_to_wcs(points)) if dxftype == '3DFACE': self.out.draw_path(Path.from_vertices(points, close=True), properties) else: # SOLID, TRACE self.out.draw_filled_polygon(points, properties)
def draw_solid_entity(self, entity: DXFGraphic, properties: Properties) -> None: # Handles SOLID, TRACE and 3DFACE dxf, dxftype = entity.dxf, entity.dxftype() points = get_tri_or_quad_points(entity, adjust_order=dxftype != '3DFACE') # TRACE is an OCS entity if dxftype == 'TRACE' and dxf.hasattr('extrusion'): ocs = entity.ocs() points = list(ocs.points_to_wcs(points)) if dxftype == '3DFACE': self.out.draw_path(from_vertices(points, close=True), properties) else: # Set default SOLID filling for SOLID and TRACE properties.filling = Filling() self.out.draw_filled_polygon(points, properties)
def draw_polyline_entity(self, entity: DXFGraphic): dxftype = entity.dxftype() if dxftype == 'POLYLINE': e = cast(Polyface, entity) if e.is_polygon_mesh or e.is_poly_face_mesh: self.draw_mesh_builder_entity( MeshBuilder.from_polyface(e), self._resolve_properties(entity), ) return entity = cast(Union[LWPolyline, Polyline], entity) if not entity.has_arc: properties = self._resolve_properties(entity) if dxftype == 'LWPOLYLINE': self.out.draw_line_string(Vector.generate( entity.vertices_in_wcs()), close=entity.closed, properties=properties) else: # POLYLINE if entity.is_2d_polyline: ocs = entity.ocs() elevation = Vector(entity.dxf.elevation).z vertices = ocs.points_to_wcs( Vector(p[0], p[1], elevation) for p in entity.points()) else: vertices = Vector.generate(entity.points()) self.out.draw_line_string(vertices, close=entity.is_closed, properties=properties) return self.parent_stack.append(entity) self.out.set_current_entity(entity, tuple(self.parent_stack)) # todo: end points of virtual entities are not in correct order # can't use self.out.start_path() for child in entity.virtual_entities(): # all child entities have the same properties as the parent, # no visibility check required: self.draw_entity(child) # self.out.end_path() self.parent_stack.pop() self.out.set_current_entity(None)
def draw_polyline_entity(self, entity: DXFGraphic, properties: Properties) -> None: dxftype = entity.dxftype() if dxftype == 'POLYLINE': e = cast(Polyface, entity) if e.is_polygon_mesh or e.is_poly_face_mesh: # draw 3D mesh or poly-face entity self.draw_mesh_builder_entity( MeshBuilder.from_polyface(e), properties, ) return entity = cast(Union[LWPolyline, Polyline], entity) is_lwpolyline = dxftype == 'LWPOLYLINE' if entity.has_width: # draw banded 2D polyline elevation = 0.0 ocs = entity.ocs() transform = ocs.transform if transform: if is_lwpolyline: # stored as float elevation = entity.dxf.elevation else: # stored as vector (0, 0, elevation) elevation = Vec3(entity.dxf.elevation).z trace = TraceBuilder.from_polyline( entity, segments=self.circle_approximation_count // 2 ) for polygon in trace.polygons(): # polygon is a sequence of Vec2() if transform: points = ocs.points_to_wcs( Vec3(v.x, v.y, elevation) for v in polygon ) else: points = Vec3.generate(polygon) # Set default SOLID filling for LWPOLYLINE properties.filling = Filling() self.out.draw_filled_polygon(points, properties) return path = Path.from_lwpolyline(entity) \ if is_lwpolyline else Path.from_polyline(entity) self.out.draw_path(path, properties)
def draw_hatch_entity(self, entity: DXFGraphic, properties: Properties) -> None: entity = cast(Hatch, entity) ocs = entity.ocs() # all OCS coordinates have the same z-axis stored as vector (0, 0, z), # default (0, 0, 0) elevation = entity.dxf.elevation.z paths = copy.deepcopy(entity.paths) paths.polyline_to_edge_path(just_with_bulge=False) # For hatches, the approximation don't have to be that precise. paths.all_to_line_edges(num=64, spline_factor=8) for p in paths: assert p.PATH_TYPE == 'EdgePath' vertices = [] last_vertex = None for e in p.edges: assert e.EDGE_TYPE == 'LineEdge' start, end = ocs.points_to_wcs([ Vector(e.start[0], e.start[1], elevation), Vector(e.end[0], e.end[1], elevation), ]) if last_vertex is None: vertices.append(start) elif not last_vertex.isclose(start): # this sometimes happens when a curved section is the # wrong way round. This should be fixed by moving to # rendering using Path objects: # https://github.com/mozman/ezdxf/issues/202 vertices.append(start) vertices.append(end) last_vertex = end if vertices: if not last_vertex.isclose(vertices[0]): vertices.append(last_vertex) self.out.draw_filled_polygon(vertices, properties)