def getSVGPlaneFromAxis(axis=FreeCAD.Vector(0, -1, 0)): view_plane = WorkingPlane.Plane() # axis is closed to +X axis if axis.getAngle(FreeCAD.Vector(1, 0, 0)) < 0.00001: view_plane.axis = FreeCAD.Vector(1, 0, 0) view_plane.u = FreeCAD.Vector(0, 1, 0) view_plane.v = FreeCAD.Vector(0, 0, -1) # axis is closed to -X axis elif axis.getAngle(FreeCAD.Vector(-1, 0, 0)) < 0.00001: view_plane.axis = FreeCAD.Vector(-1, 0, 0) view_plane.u = FreeCAD.Vector(0, -1, 0) view_plane.v = FreeCAD.Vector(0, 0, -1) else: view_plane.axis = axis y_axis = axis.cross(FreeCAD.Vector(1, 0, 0)) y_axis.normalize() if y_axis.z > 0: y_axis = y_axis.negative() elif y_axis.y > 0: y_axis = y_axis.negative() view_plane.v = y_axis view_plane.u = DraftVecUtils.rotate(view_plane.v, math.pi / 2, view_plane.axis) return view_plane
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 Exception: # TODO: trap only specific exception. # Impossible to generate DXF from Shape? Which exception is throw? _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 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