def execute(self, obj): import Part # import OpenSCAD2Dgeom if obj.String and obj.FontFile: if obj.Placement: plm = obj.Placement CharList = Part.makeWireString(obj.String, obj.FontFile, obj.Size, obj.Tracking) if len(CharList) == 0: App.Console.PrintWarning( translate("draft", "ShapeString: string has no wires") + "\n") return SSChars = [] # test a simple letter to know if we have a sticky font or not sticky = False testWire = Part.makeWireString("L", obj.FontFile, obj.Size, obj.Tracking)[0][0] if testWire.isClosed: try: testFace = Part.Face(testWire) except Part.OCCError: sticky = True else: if not testFace.isValid(): sticky = True else: sticky = True fill = True if hasattr(obj, "MakeFace"): fill = obj.MakeFace for char in CharList: if sticky or (not fill): for CWire in char: SSChars.append(CWire) else: CharFaces = [] for CWire in char: f = Part.Face(CWire) if f: CharFaces.append(f) # whitespace (ex: ' ') has no faces. This breaks OpenSCAD2Dgeom... if CharFaces: # s = OpenSCAD2Dgeom.Overlappingfaces(CharFaces).makeshape() # s = self.makeGlyph(CharFaces) s = self.makeFaces(char) SSChars.append(s) shape = Part.Compound(SSChars) obj.Shape = shape if plm: obj.Placement = plm obj.positionBySupport()
def GetTextObj(BMK="Test", BMKsize=20): '''"Print a short message when doing a recomputation, this method is mandatory" ''' txtString = BMK FontFile = EB_Auxiliaries.fontFilePath() ff8 = FontFile.encode('utf8') Size = BMKsize Tracking = 0 if sys.version_info.major < 3: CharList = Part.makeWireString(txtString, ff8, Size, Tracking) else: CharList = Part.makeWireString(txtString, FontFile, Size, Tracking) if len(CharList) == 0: print("ShapeString: string has no wires\n") return SSChars = [] # test a simple letter to know if we have a sticky font or not sticky = False if sys.version_info.major < 3: testWire = Part.makeWireString("L", ff8, Size, Tracking)[0][0] else: testWire = Part.makeWireString("L", FontFile, Size, Tracking)[0][0] if testWire.isClosed: try: testFace = Part.Face(testWire) except Part.OCCError: sticky = True else: if not testFace.isValid(): sticky = True else: sticky = True for char in CharList: if sticky: for CWire in char: SSChars.append(CWire) else: CharFaces = [] for CWire in char: f = Part.Face(CWire) if f: CharFaces.append(f) if CharFaces: s = makeFaces(char) SSChars.append(s) textObj = Part.Compound(SSChars) return textObj
def makeTag(props, tagString, slotWidth): import Part fontdir, font = getFont(props) # test: tagS=Part.makeWireString("34öäüZB", "/usr/share/fonts/truetype/dejavu/", "DejaVuSans-Bold.ttf", 6.0, 0) tagS = Part.makeWireString(unicode(tagString), fontdir, font, props.tagTextSize, 0) # Draft causes problems with GUI #tagShapeString = Draft.makeShapeString(String=unicode(tagString),\ # FontFile=join(fontdir,font),Size=props.tagTextSize,Tracking=0) #tag=tagShapeString.Shape.extrude(FreeCAD.Vector(0,0,props.tagTextHeight)) #tagF = [Part.Face(c) for c in tagS] # does not work for multi wire chars like "4", so I do this: tagF = [] tagFcuts = [] for c in tagS: for ci in c: if not isInside(ci, c): tagF.append(Part.Face(ci)) else: tagFcuts.append(Part.Face(ci)) tagC = Part.Compound(tagF) tagCcuts = Part.Compound(tagFcuts) tagC = tagC.cut(tagCcuts) tag = tagC.extrude(FreeCAD.Vector(0, 0, props.tagTextHeight)) tag.translate( FreeCAD.Vector((slotWidth - tag.BoundBox.XLength) / 2, props.marginBottom, 0)) return tag
def text (string, fontFile='/usr/share/fonts/TTF/FreeMono.ttf', height=100, returnWires = False): """returns a list of faces corresponding to each character in a string that trace text letters maybe the default fontdir and font variables have only been tested in Arch Linux""" wires = Part.makeWireString(string, fontFile, height) if returnWires: # flat_wires = [] # for letter in wires: # for wire in letter: # flat_wires.append(wire) flat_wires = [item for sublist in wires for item in sublist] return flat_wires else: faces = [] for letter in wires: faces.append(Part.Face(letter)) return faces
def execute(self, obj): import Part pl = obj.Placement if obj.Width.Value and obj.Height.Value: l2 = obj.Width.Value / 2 w2 = obj.Height.Value / 2 v1 = Vector(-l2, -w2, 0) v2 = Vector(l2, -w2, 0) v3 = Vector(l2, w2, 0) v4 = Vector(-l2, w2, 0) base = Part.makePolygon([v1, v2, v3, v4, v1]) self.sheetborder = base wires = [] area = obj.Width.Value * obj.Height.Value subarea = 0 for v in obj.Group: if v.isDerivedFrom("Part::Feature"): wires.extend(v.Shape.Wires) if Draft.getType(v) == "PanelCut": if v.Source: subarea += v.Source.Area.Value else: for w in v.Shape.Wires: if w.isClosed(): f = Part.Face(w) subarea += f.Area if wires: base = Part.Compound([base] + wires) if obj.FontFile and obj.TagText and obj.TagSize.Value: chars = [] for char in Part.makeWireString(obj.TagText, obj.FontFile, obj.TagSize.Value, 0): chars.extend(char) textshape = Part.Compound(chars) textshape.translate(obj.TagPosition) textshape.rotate(textshape.BoundBox.Center, Vector(0, 0, 1), obj.TagRotation.Value) self.sheettag = textshape base = Part.Compound([base, textshape]) obj.Shape = base obj.Placement = pl obj.FillRatio = int((subarea / area) * 100)
def execute(self, obj): import Part pl = obj.Placement if obj.Width.Value and obj.Height.Value: l2 = obj.Width.Value/2 w2 = obj.Height.Value/2 v1 = Vector(-l2,-w2,0) v2 = Vector(l2,-w2,0) v3 = Vector(l2,w2,0) v4 = Vector(-l2,w2,0) base = Part.makePolygon([v1,v2,v3,v4,v1]) self.sheetborder = base wires = [] area = obj.Width.Value * obj.Height.Value subarea = 0 for v in obj.Group: if v.isDerivedFrom("Part::Feature"): wires.extend(v.Shape.Wires) if Draft.getType(v) == "PanelCut": if v.Source: subarea += v.Source.Area.Value else: for w in v.Shape.Wires: if w.isClosed(): f = Part.Face(w) subarea += f.Area if wires: base = Part.Compound([base]+wires) if obj.FontFile and obj.TagText and obj.TagSize.Value: chars = [] for char in Part.makeWireString(obj.TagText,obj.FontFile,obj.TagSize.Value,0): chars.extend(char) textshape = Part.Compound(chars) textshape.translate(obj.TagPosition) textshape.rotate(textshape.BoundBox.Center,Vector(0,0,1),obj.TagRotation.Value) self.sheettag = textshape base = Part.Compound([base,textshape]) obj.Shape = base obj.Placement = pl obj.FillRatio = int((subarea/area)*100)
def execute(self, obj): pl = obj.Placement if obj.Source: base = None if Draft.getType(obj.Source) == "Panel": import Part, DraftGeomUtils baseobj = None if obj.Source.CloneOf: baseobj = obj.Source.CloneOf.Base if obj.Source.Base: baseobj = obj.Source.Base if baseobj: if baseobj.isDerivedFrom("Part::Feature"): if baseobj.Shape.Solids: return else: base = Part.makeCompound(baseobj.Shape.Wires) n = None for w in base.Wires: n = DraftGeomUtils.getNormal(w) if n: break if not n: n = Vector(0, 0, 1) base.translate(base.Vertexes[0].Point.negative()) r = FreeCAD.Rotation(n, Vector(0, 0, 1)) base.rotate(Vector(0, 0, 0), r.Axis, math.degrees(r.Angle)) elif baseobj.isDerivedFrom("Mesh::Feature"): return else: l2 = obj.Source.Length / 2 w2 = obj.Source.Width / 2 v1 = Vector(-l2, -w2, 0) v2 = Vector(l2, -w2, 0) v3 = Vector(l2, w2, 0) v4 = Vector(-l2, w2, 0) base = Part.makePolygon([v1, v2, v3, v4, v1]) if base: self.outline = base if obj.FontFile and obj.TagText and obj.TagSize.Value: if obj.TagPosition.Length == 0: pos = base.BoundBox.Center else: pos = obj.TagPosition if obj.TagText == "%tag%": string = obj.Source.Tag elif obj.TagText == "%label%": string = obj.Source.Label elif obj.TagText == "%description%": string = obj.Source.Description else: string = obj.TagText chars = [] for char in Part.makeWireString( string, obj.FontFile, obj.TagSize.Value, 0): chars.extend(char) textshape = Part.Compound(chars) textshape.translate(pos.sub(textshape.BoundBox.Center)) textshape.rotate(textshape.BoundBox.Center, Vector(0, 0, 1), obj.TagRotation.Value) self.tag = textshape base = Part.Compound([base, textshape]) else: base = Part.Compound([base]) obj.Shape = base obj.Placement = pl
def execute(self, obj): import Part # import OpenSCAD2Dgeom import os if obj.String and obj.FontFile: if obj.Placement: plm = obj.Placement ff8 = obj.FontFile.encode('utf8') # 1947 accents in filepath # TODO: change for Py3?? bytes? # Part.makeWireString uses FontFile as char* string if sys.version_info.major < 3: CharList = Part.makeWireString(obj.String, ff8, obj.Size, obj.Tracking) else: CharList = Part.makeWireString(obj.String, obj.FontFile, obj.Size, obj.Tracking) if len(CharList) == 0: App.Console.PrintWarning( translate("draft", "ShapeString: string has no wires") + "\n") return SSChars = [] # test a simple letter to know if we have a sticky font or not sticky = False if sys.version_info.major < 3: testWire = Part.makeWireString("L", ff8, obj.Size, obj.Tracking)[0][0] else: testWire = Part.makeWireString("L", obj.FontFile, obj.Size, obj.Tracking)[0][0] if testWire.isClosed: try: testFace = Part.Face(testWire) except Part.OCCError: sticky = True else: if not testFace.isValid(): sticky = True else: sticky = True for char in CharList: if sticky: for CWire in char: SSChars.append(CWire) else: CharFaces = [] for CWire in char: f = Part.Face(CWire) if f: CharFaces.append(f) # whitespace (ex: ' ') has no faces. This breaks OpenSCAD2Dgeom... if CharFaces: # s = OpenSCAD2Dgeom.Overlappingfaces(CharFaces).makeshape() # s = self.makeGlyph(CharFaces) s = self.makeFaces(char) SSChars.append(s) shape = Part.Compound(SSChars) obj.Shape = shape if plm: obj.Placement = plm obj.positionBySupport()
def execute(self, obj): pl = obj.Placement if obj.Source: base = None if Draft.getType(obj.Source) == "Panel": import Part,DraftGeomUtils baseobj = None if obj.Source.CloneOf: baseobj = obj.Source.CloneOf.Base if obj.Source.Base: baseobj = obj.Source.Base if baseobj: if baseobj.isDerivedFrom("Part::Feature"): if baseobj.Shape.Solids: return else: base = Part.makeCompound(baseobj.Shape.Wires) n = None for w in base.Wires: n = DraftGeomUtils.getNormal(w) if n: break if not n: n = Vector(0,0,1) base.translate(base.Vertexes[0].Point.negative()) r = FreeCAD.Rotation(n,Vector(0,0,1)) base.rotate(Vector(0,0,0),r.Axis,math.degrees(r.Angle)) elif baseobj.isDerivedFrom("Mesh::Feature"): return else: l2 = obj.Source.Length/2 w2 = obj.Source.Width/2 v1 = Vector(-l2,-w2,0) v2 = Vector(l2,-w2,0) v3 = Vector(l2,w2,0) v4 = Vector(-l2,w2,0) base = Part.makePolygon([v1,v2,v3,v4,v1]) if base: self.outline = base if obj.FontFile and obj.TagText and obj.TagSize.Value: if obj.TagPosition.Length == 0: pos = base.BoundBox.Center else: pos = obj.TagPosition if obj.TagText == "%tag%": string = obj.Source.Tag elif obj.TagText == "%label%": string = obj.Source.Label elif obj.TagText == "%description%": string = obj.Source.Description else: string = obj.TagText chars = [] for char in Part.makeWireString(string,obj.FontFile,obj.TagSize.Value,0): chars.extend(char) textshape = Part.Compound(chars) textshape.translate(pos.sub(textshape.BoundBox.Center)) textshape.rotate(textshape.BoundBox.Center,Vector(0,0,1),obj.TagRotation.Value) self.tag = textshape base = Part.Compound([base,textshape]) else: base = Part.Compound([base]) obj.Shape = base obj.Placement = pl
def generate(self, obj): if self.block: return # test a simple letter to know if we have a sticky font or not if obj.String and obj.FontFile: sticky = False testWire = Part.makeWireString("L",obj.FontFile,obj.Size,obj.Tracking)[0][0] if testWire.isClosed: try: testFace = Part.Face(testWire) except Part.OCCError: sticky = True else: if not testFace.isValid(): sticky = True else: sticky = True ################### if obj.String and obj.FontFile: if obj.Placement: plm = obj.Placement if type(obj.String) == list: # multilines poz_Y = 0 shapes = [] for i in range(len(obj.String), 0, -1): line = obj.String[i-1] if line != "": CharList = Part.makeWireString(line,obj.FontFile,obj.Size,obj.Tracking) if len(CharList) == 0: FreeCAD.Console.PrintWarning(translate("draft","ShapeString: string has no wires")+"\n") return SSChars = [] for char in CharList: if sticky: for CWire in char: for k in CWire: k.Placement.Base.y = poz_Y SSChars.append(CWire) else: CharFaces = [] for CWire in char: CWire.Placement.Base.y = poz_Y f = Part.Face(CWire) if f: CharFaces.append(f) # whitespace (ex: ' ') has no faces. This breaks OpenSCAD2Dgeom... if CharFaces: # s = OpenSCAD2Dgeom.Overlappingfaces(CharFaces).makeshape() # s = self.makeGlyph(CharFaces) s = self.makeFaces(char) SSChars.append(s) shapes.append(Part.Compound(SSChars)) poz_Y += obj.Size.Value + (obj.LineDistance * obj.Size.Value) / 100. shape = Part.Compound(shapes) else: CharList = Part.makeWireString(obj.String,obj.FontFile,obj.Size,obj.Tracking) if len(CharList) == 0: FreeCAD.Console.PrintWarning(translate("draft","ShapeString: string has no wires")+"\n") return SSChars = [] for char in CharList: if sticky: for CWire in char: SSChars.append(CWire) else: CharFaces = [] for CWire in char: f = Part.Face(CWire) if f: CharFaces.append(f) # whitespace (ex: ' ') has no faces. This breaks OpenSCAD2Dgeom... if CharFaces: # s = OpenSCAD2Dgeom.Overlappingfaces(CharFaces).makeshape() # s = self.makeGlyph(CharFaces) s = self.makeFaces(char) SSChars.append(s) shape = Part.Compound(SSChars) ################### obj.Shape = shape if plm: obj.Placement = plm obj.positionBySupport() self.changeJustification(obj)
#String = 'AVWAIXA.V' # kerning String = 'FreeCAD' # ASCII #FontPath = '/usr/share/fonts/truetype/msttcorefonts/' #FontName = 'Times_New_Roman_Italic.ttf' FontPath = '/usr/share/fonts/truetype/msttcorefonts/' FontName = 'Arial.ttf' #FontName = 'NOTArial.ttf' # font file not found error #FontPath = '/usr/share/fonts/truetype/msttcorefonts/' #FontName = 'ariali.ttf' #symlink to ttf #FontPath = '/usr/share/fonts/truetype/' #FontName = 'Peterbuilt.ttf' # overlapping script font #FontPath = '/usr/share/fonts/truetype/' #FontName = 'dyspepsia.ttf' # overlapping script font # :) Height = 2000 # out string height FCunits Track = 0 # intercharacter spacing print "testWire.py input String contains ", len(String), " characters." s = Part.makeWireString(String,FontPath,FontName,Height,Track) print "returned from makeWireString" print "testWire.py output contains ", len(s), " WireChars." for char in s: for contour in char: Part.show(contour) print "testWire ended."
#String = 'AVWAIXA.V' # kerning String = 'FreeCAD' # ASCII #FontPath = '/usr/share/fonts/truetype/msttcorefonts/' #FontName = 'Times_New_Roman_Italic.ttf' FontPath = '/usr/share/fonts/truetype/msttcorefonts/' FontName = 'Arial.ttf' #FontName = 'NOTArial.ttf' # font file not found error #FontPath = '/usr/share/fonts/truetype/msttcorefonts/' #FontName = 'ariali.ttf' #symlink to ttf #FontPath = '/usr/share/fonts/truetype/' #FontName = 'Peterbuilt.ttf' # overlapping script font #FontPath = '/usr/share/fonts/truetype/' #FontName = 'dyspepsia.ttf' # overlapping script font # :) Height = 2000 # out string height FCunits Track = 0 # intercharacter spacing print("testWire.py input String contains ", len(String), " characters.") s = Part.makeWireString(String, FontPath, FontName, Height, Track) print("returned from makeWireString") print("testWire.py output contains ", len(s), " WireChars.") for char in s: for contour in char: Part.show(contour) print("testWire ended.")