diff --git a/src/Mod/Draft/CMakeLists.txt b/src/Mod/Draft/CMakeLists.txt index ea3232f089..b6d8e43535 100644 --- a/src/Mod/Draft/CMakeLists.txt +++ b/src/Mod/Draft/CMakeLists.txt @@ -86,6 +86,7 @@ SET(Draft_functions draftfunctions/cut.py draftfunctions/downgrade.py draftfunctions/draftify.py + draftfunctions/dxf.py draftfunctions/extrude.py draftfunctions/fuse.py draftfunctions/heal.py diff --git a/src/Mod/Draft/Draft.py b/src/Mod/Draft/Draft.py index 32d49010f1..2a2499cb4f 100644 --- a/src/Mod/Draft/Draft.py +++ b/src/Mod/Draft/Draft.py @@ -91,13 +91,14 @@ from draftutils.utils import (string_encode_coin, svg_patterns, svgpatterns, get_rgb, - getrgb, - get_DXF, - getDXF) + getrgb) from draftfunctions.svg import (get_svg, getSVG) +from draftfunctions.dxf import (get_dxf, + getDXF) + from draftutils.gui_utils import (get3DView, get_3d_view, autogroup, diff --git a/src/Mod/Draft/draftfunctions/dxf.py b/src/Mod/Draft/draftfunctions/dxf.py new file mode 100644 index 0000000000..eee3137170 --- /dev/null +++ b/src/Mod/Draft/draftfunctions/dxf.py @@ -0,0 +1,128 @@ +# -*- coding: utf8 -*- +# *************************************************************************** +# * Copyright (c) 2009 Yorik van Havre * +# * Copyright (c) 2020 Eliud Cabrera Castillo * +# * * +# * This program is free software; you can redistribute it and/or modify * +# * it under the terms of the GNU Lesser General Public License (LGPL) * +# * as published by the Free Software Foundation; either version 2 of * +# * the License, or (at your option) any later version. * +# * for detail see the LICENCE text file. * +# * * +# * This program is distributed in the hope that it will be useful, * +# * but WITHOUT ANY WARRANTY; without even the implied warranty of * +# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +# * GNU Library General Public License for more details. * +# * * +# * You should have received a copy of the GNU Library General Public * +# * License along with this program; if not, write to the Free Software * +# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * +# * USA * +# * * +# *************************************************************************** +"""Provides functions to return the DXF representation of various shapes.""" +## @package dxf +# \ingroup draftfuctions +# \brief Provides functions to return the SVG representation of shapes. + +import lazy_loader.lazy_loader as lz + +import FreeCAD as App +import DraftVecUtils +import WorkingPlane +import draftutils.utils as utils + +from draftutils.messages import _wrn + +# Delay import of module until first use because it is heavy +Part = lz.LazyLoader("Part", globals(), "Part") +DraftGeomUtils = lz.LazyLoader("DraftGeomUtils", globals(), "DraftGeomUtils") +Drawing = lz.LazyLoader("Drawing", globals(), "Drawing") + + +## \addtogroup draftfuctions +# @{ + +def _get_proj(vec, plane=None): + if not plane: + return vec + + nx = DraftVecUtils.project(vec, plane.u) + ny = DraftVecUtils.project(vec, plane.v) + return App.Vector(nx.Length, ny.Length, 0) + + +def get_dxf(obj, direction=None): + """Return a DXF entity from the given object. + + If direction is given, the object is projected in 2D. + """ + plane = None + result = "" + if (obj.isDerivedFrom("Drawing::View") + or obj.isDerivedFrom("TechDraw::DrawView")): + if obj.Source.isDerivedFrom("App::DocumentObjectGroup"): + for o in obj.Source.Group: + result += get_dxf(o, obj.Direction) + else: + result += get_dxf(obj.Source, obj.Direction) + return result + + if direction and isinstance(direction, App.Vector): + if direction != App.Vector(0, 0, 0): + plane = WorkingPlane.Plane() + plane.alignToPointAndAxis(App.Vector(0, 0, 0), direction) + + if utils.get_type(obj) in ("Dimension", "LinearDimension"): + p1 = _get_proj(obj.Start, plane=plane) + p2 = _get_proj(obj.End, plane=plane) + p3 = _get_proj(obj.Dimline, plane=plane) + result += "0\nDIMENSION\n8\n0\n62\n0\n3\nStandard\n70\n1\n" + result += "10\n"+str(p3.x)+"\n20\n"+str(p3.y)+"\n30\n"+str(p3.z)+"\n" + result += "13\n"+str(p1.x)+"\n23\n"+str(p1.y)+"\n33\n"+str(p1.z)+"\n" + result += "14\n"+str(p2.x)+"\n24\n"+str(p2.y)+"\n34\n"+str(p2.z)+"\n" + + elif utils.get_type(obj) == "Annotation": + # Only for App::Annotation + p = _get_proj(obj.Position, plane=plane) + count = 0 + for t in obj.LabeLtext: + result += "0\nTEXT\n8\n0\n62\n0\n" + result += "10\n" + result += str(p.x) + "\n20\n" + result += str(p.y + count) + "\n30\n" + result += str(p.z) + "\n" + result += "40\n1\n" + result += "1\n" + str(t) + "\n" + result += "7\nSTANDARD\n" + count += 1 + + elif hasattr(obj, 'Shape'): + # TODO do this the Draft way, for ex. using polylines and rectangles + if not direction: + direction = App.Vector(0, 0, -1) + + if DraftVecUtils.isNull(direction): + direction = App.Vector(0, 0, -1) + + try: + d = Drawing.projectToDXF(obj.Shape, direction) + except: + _wrn("get_dxf: " + "unable to project '{}' to {}".format(obj.Label, direction)) + else: + result += d + else: + _wrn("get_dxf: unsupported object, '{}'".format(obj.Label)) + + return result + + +def getDXF(obj, + projection=None): + """Return DXF string of the object. DEPRECATED. Use 'get_dxf'.""" + utils.use_instead("get_dxf") + return get_dxf(obj, + projection=projection) + +## @} diff --git a/src/Mod/Draft/draftobjects/drawingview.py b/src/Mod/Draft/draftobjects/drawingview.py index 53a7d2e3a6..015460cec2 100644 --- a/src/Mod/Draft/draftobjects/drawingview.py +++ b/src/Mod/Draft/draftobjects/drawingview.py @@ -40,6 +40,7 @@ this object should no longer be available. from PySide.QtCore import QT_TRANSLATE_NOOP import draftfunctions.svg as get_svg +import draftfunctions.dxf as get_dxf import draftutils.utils as utils import draftutils.groups as groups @@ -182,7 +183,7 @@ class DrawingView(DraftObject): def getDXF(self, obj): """Return a DXF fragment.""" - return utils.getDXF(obj) + return get_dxf.get_dxf(obj) # Alias for compatibility with v0.18 and earlier diff --git a/src/Mod/Draft/draftutils/utils.py b/src/Mod/Draft/draftutils/utils.py index 41b7766506..a3fa54ff11 100644 --- a/src/Mod/Draft/draftutils/utils.py +++ b/src/Mod/Draft/draftutils/utils.py @@ -786,75 +786,6 @@ def get_rgb(color, testbw=True): getrgb = get_rgb -def get_DXF(obj,direction=None): - """getDXF(object,[direction]): returns a DXF entity from the given - object. If direction is given, the object is projected in 2D.""" - plane = None - result = "" - if obj.isDerivedFrom("Drawing::View") or obj.isDerivedFrom("TechDraw::DrawView"): - if obj.Source.isDerivedFrom("App::DocumentObjectGroup"): - for o in obj.Source.Group: - result += getDXF(o,obj.Direction) - else: - result += getDXF(obj.Source,obj.Direction) - return result - if direction: - if isinstance(direction, App.Vector): - import WorkingPlane - if direction != App.Vector(0,0,0): - plane = WorkingPlane.Plane() - plane.alignToPointAndAxis(App.Vector(0,0,0), direction) - - def getProj(vec): - if not plane: return vec - nx = DraftVecUtils.project(vec,plane.u) - ny = DraftVecUtils.project(vec,plane.v) - return App.Vector(nx.Length,ny.Length,0) - - if getType(obj) in ["Dimension","LinearDimension"]: - p1 = getProj(obj.Start) - p2 = getProj(obj.End) - p3 = getProj(obj.Dimline) - result += "0\nDIMENSION\n8\n0\n62\n0\n3\nStandard\n70\n1\n" - result += "10\n"+str(p3.x)+"\n20\n"+str(p3.y)+"\n30\n"+str(p3.z)+"\n" - result += "13\n"+str(p1.x)+"\n23\n"+str(p1.y)+"\n33\n"+str(p1.z)+"\n" - result += "14\n"+str(p2.x)+"\n24\n"+str(p2.y)+"\n34\n"+str(p2.z)+"\n" - - elif getType(obj) == "Annotation": - p = getProj(obj.Position) - count = 0 - for t in obj.LabeLtext: - result += "0\nTEXT\n8\n0\n62\n0\n" - result += "10\n"+str(p.x)+"\n20\n"+str(p.y+count)+"\n30\n"+str(p.z)+"\n" - result += "40\n1\n" - result += "1\n"+str(t)+"\n" - result += "7\nSTANDARD\n" - count += 1 - - elif hasattr(obj,'Shape'): - # TODO do this the Draft way, for ex. using polylines and rectangles - import Drawing - import DraftVecUtils - if not direction: - direction = App.Vector(0,0,-1) - if DraftVecUtils.isNull(direction): - direction = App.Vector(0,0,-1) - try: - d = Drawing.projectToDXF(obj.Shape,direction) - except: - print("Draft.getDXF: Unable to project ",obj.Label," to ",direction) - else: - result += d - - else: - print("Draft.getDXF: Unsupported object: ",obj.Label) - - return result - - -getDXF = get_DXF - - def filter_objects_for_modifiers(objects, isCopied=False): filteredObjects = [] for obj in objects: