Skip to content

Commit

Permalink
Merge pull request #451 from alexneufeld/dxf-export-fix
Browse files Browse the repository at this point in the history
Dxf export fix
  • Loading branch information
shaise authored Jan 21, 2025
2 parents d023c3c + cef7f58 commit 2511e03
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 33 deletions.
86 changes: 54 additions & 32 deletions SheetMetalNewUnfolder.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

import FreeCAD
import Part
import Draft
import SheetMetalTools
from FreeCAD import Matrix, Placement, Rotation, Vector
from TechDraw import projectEx as project_shape_to_plane
Expand Down Expand Up @@ -486,45 +485,64 @@ def edges_to_sketch_object(
existing_sketches: list[str] = None,
color: str = "#00FF00",
) -> FreeCAD.DocumentObject:
"""Uses functionality from the Draft API to convert a list of edges into a
Sketch document object. This allows the user to more easily make small
changes to the sheet metal cutting pattern when prepping it
for fabrication."""
"""Converts a list of edges to an un-constrained sketch object.
This allows the user to more easily make small changes to the sheet
metal cutting pattern when prepping it for fabrication."""
cleaned_up_edges = Edge2DCleanup.cleanup_sketch(edges, 0.1)
# cleaned_up_edges = edges

# See if there is an existing sketch with the same name, use it insted of creating
# See if there is an existing sketch with the same name,
# use it insted of creating a new one.
if existing_sketches is None:
existing_sketch_name = ""
else:
existing_sketch_name = next(
(item for item in existing_sketches if item.startswith(object_name)), ""
)
existing_sketch = FreeCAD.ActiveDocument.getObject(existing_sketch_name)
if existing_sketch is not None:
existing_sketch.deleteAllGeometry()

sk = Draft.makeSketch(
# NOTE: in testing, using the autoconstraint feature
# caused errors with some shapes
cleaned_up_edges,
autoconstraints=False,
addTo=existing_sketch,
delete=False,
name=object_name,
)
sk.Label = object_name
sk.recompute()

sketch = FreeCAD.ActiveDocument.getObject(existing_sketch_name)
if sketch is not None:
sketch.deleteAllGeometry()
else:
# if there is not already an existing sketch, create one.
sketch = FreeCAD.ActiveDocument.addObject(
"Sketcher::SketchObject", object_name
)
sketch.Placement = Placement()

for edge in cleaned_up_edges:
startpoint = edge.firstVertex().Point
endpoint = edge.lastVertex().Point
curvetype = edge.Curve.TypeId
if curvetype == "Part::GeomLine":
sketch.addGeometry(Part.LineSegment(startpoint, endpoint))
elif curvetype == "Part::GeomCircle":
if startpoint.distanceToPoint(endpoint) < eps:
# full circle
sketch.addGeometry(
Part.Circle(
edge.Curve.Center, Vector(0, 0, 1), edge.Curve.Radius
)
)
else:
# arc
pmin, pmax = edge.ParameterRange
midpoint = edge.valueAt(pmin + 0.5 * (pmax - pmin))
sketch.addGeometry(Part.Arc(startpoint, midpoint, endpoint))
else:
errmsg = (
"Unuseable curve type found during sketch creation: "
+ curvetype
)
raise RuntimeError(errmsg)
sketch.Label = object_name
sketch.recompute()
# if the gui is running, change the color of the sketch lines and vertices
if FreeCAD.GuiUp:
rgb_color = tuple(int(color[i : i + 2], 16) for i in (1, 3, 5))
v = FreeCAD.Version()
if v[0] == "0" and int(v[1]) < 21:
rgb_color = tuple(i / 255 for i in rgb_color)
sk.ViewObject.LineColor = rgb_color
sk.ViewObject.PointColor = rgb_color

return sk
sketch.ViewObject.LineColor = rgb_color
sketch.ViewObject.PointColor = rgb_color
return sketch

@staticmethod
def wire_is_a_hole(w: Part.Wire) -> bool:
Expand Down Expand Up @@ -605,7 +623,11 @@ def __init__(self) -> None:
def from_single_value(cls, k_factor: float, kfactor_standard: str):
"""one k-factor for all radius:thickness ratios"""
instance = cls()
instance.k_factor_standard = cls.KFactorStandard.ANSI if kfactor_standard == "ansi" else cls.KFactorStandard.DIN
instance.k_factor_standard = (
cls.KFactorStandard.ANSI
if kfactor_standard == "ansi"
else cls.KFactorStandard.DIN
)
instance.radius_thickness_values = [
1.0,
]
Expand Down Expand Up @@ -1154,7 +1176,7 @@ def getUnfoldSketches(
split_sketches: bool = False,
sketch_color: str = "#000080",
bend_sketch_color: str = "#c00000",
internal_sketch_solor: str = "#ff5733",
internal_sketch_color: str = "#ff5733",
) -> list[Part.Feature]:
sketch_profile, inner_wires, hole_wires = SketchExtraction.extract_manually(
unfolded_shape, root_normal
Expand Down Expand Up @@ -1195,7 +1217,7 @@ def getUnfoldSketches(
inner_lines.Edges,
"Unfold_Sketch_Internal",
existing_sketches,
internal_sketch_solor,
internal_sketch_color,
)
sketch_objects_list.append(inner_lines_doc_obj)
if hole_wires:
Expand All @@ -1204,7 +1226,7 @@ def getUnfoldSketches(
hole_lines.Edges,
"Unfold_Sketch_Holes",
existing_sketches,
internal_sketch_solor,
internal_sketch_color,
)
sketch_objects_list.append(hole_lines_doc_obj)
return sketch_objects_list
2 changes: 1 addition & 1 deletion SheetMetalUnfoldCmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def smUnfoldExportSketches(obj, useDialog = True):
if len(obj.UnfoldSketches) == 1:
sketchNames = [obj.UnfoldSketches[0]]
else:
sketchNames = obj.UnfoldSketches[1:]
sketchNames = obj.UnfoldSketches
for name in sketchNames:
sketch = obj.Document.getObject(name)
if sketch is None:
Expand Down

0 comments on commit 2511e03

Please sign in to comment.