Skip to content

Commit

Permalink
LINES polarity kept from parametric drawing #25
Browse files Browse the repository at this point in the history
  • Loading branch information
MadScrewdriver committed Jun 2, 2024
1 parent 7bd0a52 commit 630b594
Show file tree
Hide file tree
Showing 3 changed files with 236 additions and 125 deletions.
49 changes: 31 additions & 18 deletions qsketchmetric/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
from typing import Optional, Dict

import ezdxf
from ezdxf import units, bbox
from ezdxf import bbox
from ezdxf.addons import Importer
from ezdxf.entities import DXFGraphic, Insert
from ezdxf.document import Drawing
from ezdxf.entities import DXFGraphic
from ezdxf.layouts import Modelspace
from ezdxf.math import Vec3
from py_expression_eval import Parser # type: ignore
from ezdxf.document import Drawing


class Renderer:
Expand Down Expand Up @@ -167,12 +167,13 @@ def _prepare_graph(self):
else:
new_length = Parser().parse(constant_xdata).evaluate(self.variables)

e_data = {"layer": layer, "linetype": line_type}
e_data_start = {"layer": layer, "linetype": line_type, "start": True}
e_data_end = {"layer": layer, "linetype": line_type, "start": False}

self.graph[end] = self.graph.get(end, []) + [("LINE", start, new_length, e_data)]
self.graph[start] = self.graph.get(start, []) + [("LINE", end, new_length, e_data)]
self.visited_graph[end] = self.visited_graph.get(end, []) + [(e_data["layer"], start)]
self.visited_graph[start] = self.visited_graph.get(start, []) + [(e_data["layer"], end)]
self.graph[end] = self.graph.get(end, []) + [("LINE", start, new_length, e_data_end)]
self.graph[start] = self.graph.get(start, []) + [("LINE", end, new_length, e_data_start)]
self.visited_graph[end] = self.visited_graph.get(end, []) + [(layer, start)]
self.visited_graph[start] = self.visited_graph.get(start, []) + [(layer, end)]

elif entity.dxftype() == "CIRCLE":
center = entity.dxf.center
Expand Down Expand Up @@ -273,11 +274,17 @@ def _construct_rest_of_dxf(self):
for node, v in self.graph.items():
for line in [l for l in v if
l[0] == "LINE" and l[2] == "?" and (l[3]["layer"], l[1]) in self.visited_graph[node]]:
self.new_entities.append(self.output_msp.add_line((
self.new_points[node][0] + self.offset_x, self.new_points[node][1] + self.offset_y),
(self.new_points[line[1]][0] + self.offset_x,
self.new_points[line[1]][1] + self.offset_y),
dxfattribs={"layer": line[3]["layer"], "linetype": line[3]["linetype"]}))
start = (self.new_points[node][0] + self.offset_x, self.new_points[node][1] + self.offset_y)
end = (self.new_points[line[1]][0] + self.offset_x, self.new_points[line[1]][1] + self.offset_y)
start, end = (start, end) if line[3]["start"] else (end, start)

self.new_entities.append(
self.output_msp.add_line(
start, end,
dxfattribs={"layer": line[3]["layer"], "linetype": line[3]["linetype"]}
)
)

self.visited_graph[line[1]].remove((line[3]["layer"], node))
self.visited_graph[node].remove((line[3]["layer"], line[1]))

Expand Down Expand Up @@ -318,10 +325,14 @@ def _dfs(self, node: Vec3, offset_x: float, offset_y: float):
self.new_points[vector] = (vector.x + offset_x + new_offset_x, vector.y + offset_y + new_offset_y)

if data["layer"] != "VIRTUAL_LAYER":
start = (node.x + offset_x + self.offset_x, node.y + offset_y + self.offset_y)
end = (vector.x + offset_x + new_offset_x + self.offset_x,
vector.y + offset_y + new_offset_y + self.offset_y)

start, end = (start, end) if data["start"] else (end, start)

self.new_entities.append(self.output_msp.add_line(
(node.x + offset_x + self.offset_x, node.y + offset_y + self.offset_y), (
vector.x + offset_x + new_offset_x + self.offset_x,
vector.y + offset_y + new_offset_y + self.offset_y),
start, end,
dxfattribs={"layer": data["layer"], "linetype": data["linetype"]}))

self.visited_graph[node].remove((data["layer"], vector))
Expand All @@ -332,8 +343,10 @@ def _dfs(self, node: Vec3, offset_x: float, offset_y: float):
elif name == "INSERT":
self.new_entities.append(self.output_msp.add_blockref(data["name"], (
node.x + offset_x + self.offset_x, node.y + offset_y + self.offset_y),
dxfattribs={"layer": data["layer"], "xscale": data["xscale"], "yscale": data["yscale"],
"linetype": data["linetype"]}))
dxfattribs={"layer": data["layer"],
"xscale": data["xscale"],
"yscale": data["yscale"],
"linetype": data["linetype"]}))

elif name == "POINT":
self.points[data["name"]] = (node.x + offset_x, node.y + offset_y)
Expand Down
16 changes: 10 additions & 6 deletions tests/test_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,25 @@ def setUp(self):

self.graph = {
self.point1: [
("LINE", self.point2, self.line1_length, {"layer": "VIRTUAL_LAYER", "linetype": "line_linetype"}),
("LINE", self.point3, self.line2_length, {"layer": "line_layer", "linetype": "line_linetype"}),
("LINE", self.point2, self.line1_length, {"layer": "VIRTUAL_LAYER", "linetype": "line_linetype",
"start": True}),
("LINE", self.point3, self.line2_length, {"layer": "line_layer", "linetype": "line_linetype",
"start": True}),
("ARC", self.point1, self.arc_radius, {"layer": "arc_layer", "linetype": "arc_linetype",
"radius": self.arc_radius, "start_angle": self.start_angle,
"end_angle": self.end_angle})],

self.point2: [("LINE", self.point3, "?", {"layer": "line_layer", "linetype": "line_linetype"}),
self.point2: [("LINE", self.point3, "?", {"layer": "line_layer", "linetype": "line_linetype",
"start": True}),
("LINE", self.point1, self.line1_length,
{"layer": "VIRTUAL_LAYER", "linetype": "line_linetype"}),
{"layer": "VIRTUAL_LAYER", "linetype": "line_linetype", "start": True}),
("CIRCLE", self.point2, self.circle_radius,
{"layer": "circle_layer", "linetype": "circle_linetype", "radius": self.circle_radius})],

self.point3: [("LINE", self.point2, "?", {"layer": "line_layer", "linetype": "line_linetype"}),
self.point3: [("LINE", self.point2, "?", {"layer": "line_layer", "linetype": "line_linetype",
"start": True}),
("LINE", self.point1, self.line2_length,
{"layer": "circle_layer", "linetype": "circle_linetype"}),
{"layer": "circle_layer", "linetype": "circle_linetype", "start": True}),
("POINT", self.point3, 0, {"name": "mock"}),
("INSERT", self.point3, 0,
{"layer": "insert_layer", "linetype": "insert_linetype", "name": "insert",
Expand Down
Loading

0 comments on commit 630b594

Please sign in to comment.