From bf5e28a502eb3d033fab6898188424ade36e0086 Mon Sep 17 00:00:00 2001 From: Roy-043 Date: Fri, 21 Feb 2025 22:15:37 +0100 Subject: [PATCH] Draft: fix alignment of sketches in SVG and legacy DXF export Fixes #5990 Supersedes #13652 This solution only works for sketches. The export of other planar objects remains the same. This is similar to the way the C++ DXF exporter handles things. What is different here is that if multiple sketches are exported they are assumed to have the same normal, and their position relative to each other is preserved. --- src/Mod/Draft/importDXF.py | 23 ++++++++++++++--------- src/Mod/Draft/importSVG.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/src/Mod/Draft/importDXF.py b/src/Mod/Draft/importDXF.py index c0009af83f..4be863fc73 100644 --- a/src/Mod/Draft/importDXF.py +++ b/src/Mod/Draft/importDXF.py @@ -3674,7 +3674,7 @@ def export(objectslist, filename, nospline=False, lwPoly=False): dxf.layers.append(dxfLibrary.Layer(name=ob.Label, color=getACI(ob), lineType=ltype)) - + base_sketch_pla = None # Placement of the 1st sketch. for ob in exportList: obtype = Draft.getType(ob) # print("processing " + str(ob.Name)) @@ -3788,10 +3788,16 @@ def export(objectslist, filename, nospline=False, lwPoly=False): elif ob.isDerivedFrom("Part::Feature"): tess = None - if hasattr(ob, "Tessellation"): - if ob.Tessellation: - tess = [ob.Tessellation, ob.SegmentLength] - if params.get_param("dxfmesh"): + if getattr(ob, "Tessellation", False): + tess = [ob.Tessellation, ob.SegmentLength] + if ob.isDerivedFrom("Sketcher::SketchObject"): + if base_sketch_pla is None: + base_sketch_pla = ob.Placement + sh = Part.Compound() + sh.Placement = base_sketch_pla + sh.add(ob.Shape.copy()) + sh.transformShape(base_sketch_pla.inverse().Matrix) + elif params.get_param("dxfmesh"): sh = None if not ob.Shape.isNull(): writeMesh(ob, dxf) @@ -3799,11 +3805,10 @@ def export(objectslist, filename, nospline=False, lwPoly=False): _view = FreeCADGui.ActiveDocument.ActiveView direction = _view.getViewDirection().multiply(-1) sh = projectShape(ob.Shape, direction, tess) + elif ob.Shape.Volume > 0: + sh = projectShape(ob.Shape, Vector(0, 0, 1), tess) else: - if ob.Shape.Volume > 0: - sh = projectShape(ob.Shape, Vector(0, 0, 1), tess) - else: - sh = ob.Shape + sh = ob.Shape if sh: if not sh.isNull(): if sh.ShapeType == 'Compound': diff --git a/src/Mod/Draft/importSVG.py b/src/Mod/Draft/importSVG.py index e60cbd83ba..5156e1e997 100644 --- a/src/Mod/Draft/importSVG.py +++ b/src/Mod/Draft/importSVG.py @@ -1813,6 +1813,29 @@ def export(exportList, filename): "Unknown SVG export style, switching to Translated")) svg_export_style = 0 + tmp = [] + hidden_doc = None + base_sketch_pla = None # Placement of the 1st sketch. + for obj in exportList: + if obj.isDerivedFrom("Sketcher::SketchObject"): + if hidden_doc is None: + hidden_doc = FreeCAD.newDocument(name="hidden", hidden=True, temp=True) + base_sketch_pla = obj.Placement + import Part + sh = Part.Compound() + sh.Placement = base_sketch_pla + sh.add(obj.Shape.copy()) + sh.transformShape(base_sketch_pla.inverse().Matrix) + new = hidden_doc.addObject("Part::Part2DObjectPython") + new.Shape = sh + if FreeCAD.GuiUp: + for attr in ("DrawStyle", "LineColor", "LineWidth"): + setattr(new.ViewObject, attr, getattr(obj.ViewObject, attr)) + tmp.append(new) + else: + tmp.append(obj) + exportList = tmp + # Determine the size of the page by adding the bounding boxes # of all shapes bb = FreeCAD.BoundBox() @@ -1900,3 +1923,8 @@ def export(exportList, filename): # Close the file svg.write('') svg.close() + if hidden_doc is not None: + try: + App.closeDocument(hidden_doc.Name) + except: + pass