Gist URL: https://gist.github.com/stephensmitchell/8cb1d7e17dddfa57f415fc9e7055d1cb
- File Copier.py
- Image to Python.py
- Pattern Along.py
- PatternAlong.png
- Sketch Copier.py
- SketchCopier.png
Gist URL: https://gist.github.com/stephensmitchell/8cb1d7e17dddfa57f415fc9e7055d1cb
| # File Copier script | |
| # Demonstrates copying a set of parts and assemblies | |
| # For use with Alibre Design | |
| import fnmatch | |
| import os | |
| from os.path import expanduser | |
| Win = Windows() | |
| ScriptName = 'File Copier' | |
| CopyTypes = ['Only parts', 'Assemblies, sub-assemblies and parts in assemblies'] | |
| Options = [] | |
| Options.append(['Source folder', WindowsInputTypes.Folder, expanduser('~'), 'Choose the folder containing the items to copy']) | |
| Options.append(['Destination folder', WindowsInputTypes.Folder, expanduser('~'), 'Choose the folder to place the copies']) | |
| Options.append(['Copy', WindowsInputTypes.StringList, CopyTypes, CopyTypes[0]]) | |
| Values = Win.OptionsDialog(ScriptName, Options, 300) | |
| if Values == None: | |
| sys.exit() | |
| # get the inputs | |
| SourceFolder = Values[0] | |
| DestinationFolder = Values[1] | |
| CopyType = Values[2] | |
| # validate | |
| if not SourceFolder: | |
| Win.ErrorDialog('No source folder selected', ScriptName) | |
| sys.exit(); | |
| if os.path.isdir(SourceFolder) == False: | |
| Win.ErrorDialog('Source folder does not exist', ScriptName) | |
| sys.exit(); | |
| if not DestinationFolder: | |
| Win.ErrorDialog('No destination folder selected', ScriptName) | |
| sys.exit(); | |
| if os.path.isdir(DestinationFolder) == False: | |
| Win.ErrorDialog('Destination folder does not exist', ScriptName) | |
| sys.exit(); | |
| if CopyType == 0: | |
| print "Searching for parts..." | |
| else: | |
| print "Searching for assemblies..." | |
| # create empty lists | |
| Parts = [] | |
| Assemblies = [] | |
| # perform the search | |
| for Root, Dirnames, Filenames in os.walk(SourceFolder): | |
| if CopyType == 0: | |
| for Filename in fnmatch.filter(Filenames, '*.AD_PRT'): | |
| Parts.append(os.path.join(Root, Filename)) | |
| else: | |
| for Filename in fnmatch.filter(Filenames, '*.AD_ASM'): | |
| Assemblies.append(os.path.join(Root, Filename)) | |
| # if no parts or assemblies found... | |
| if len(Parts) == 0 and len(Assemblies) == 0: | |
| Win.ErrorDialog('No parts or assemblies found', ScriptName) | |
| sys.exit(); | |
| # copy each part | |
| for PartFileName in Parts: | |
| print "Copying {0}...".format(PartFileName) | |
| Folder = os.path.dirname(os.path.abspath(PartFileName)) | |
| FileName = os.path.basename(PartFileName) | |
| FileNameNoExt, Ext = os.path.splitext(FileName) | |
| OutputFileName = DestinationFolder + '\\' + FileName | |
| # open, save, close | |
| P = Part(Folder, FileName) | |
| P.SaveAs(DestinationFolder, FileNameNoExt) | |
| P.Close() | |
| print " -> {0}".format(OutputFileName) | |
| # copy each assembly | |
| for AssemblyFileName in Assemblies: | |
| print "Copying {0}...".format(AssemblyFileName) | |
| # open, save, close | |
| Folder = os.path.dirname(os.path.abspath(AssemblyFileName)) | |
| FileName = os.path.basename(AssemblyFileName) | |
| OutputFileName = DestinationFolder + '\\' + FileName | |
| A = Assembly(Folder, Filename) | |
| A.SaveAll(DestinationFolder) | |
| A.Close() | |
| print " -> {0}".format(OutputFileName) | |
| if CopyType == 0: | |
| Win.InfoDialog('Copied {0} part{1}'.format(len(Parts), '' if len(Parts) == 1 else 's'), ScriptName) | |
| else: | |
| Win.InfoDialog('Copied {0} assembl{1}'.format(len(Assemblies), 'y' if len(Assemblies) == 1 else 'ies'), ScriptName) |
| # Image to Python Converter | |
| # (c) Alibre, LLC 2019, All Rights Reserved | |
| # Version 1.00 | |
| from array import array | |
| import os | |
| Win = Windows() | |
| ScriptName = "Image to Python Converter" | |
| Options = [] | |
| Options.append(['Image', WindowsInputTypes.File, None, 'Choose an image', 'PNG Files|*.png|JPEG Files|*.jpg|Bitmap Files|*.bmp|All Files|*.*']) | |
| Options.append(['Python Output', WindowsInputTypes.SaveFile, None, 'Python File to Generate', 'Python Files|*.py|All Files|*.*', '.py']) | |
| Values = Win.OptionsDialog(ScriptName, Options) | |
| if Values == None: | |
| sys.exit() | |
| ImageFile = Values[0] | |
| OutputFile = Values[1] | |
| # get some details about the image file | |
| ImageSize = os.path.getsize(ImageFile) | |
| ImageName = os.path.splitext(os.path.basename(ImageFile))[0] | |
| ImageName = "Img_" + ImageName.Replace(' ', '_') | |
| # load image into binary array | |
| data = array('B') | |
| with open(ImageFile, 'rb') as f: | |
| data.fromfile(f, ImageSize) | |
| Out = open(OutputFile, 'w') | |
| Out.write('%s = [\n' % ImageName) | |
| Line = '' | |
| Offset = 0 | |
| for b in range(0, ImageSize): | |
| Line += '0x%2.2X, ' % data[b] | |
| Offset += 1 | |
| if Offset % 20 == 0: | |
| Out.write(' ' + Line + '\n') | |
| Line = '' | |
| if len(Line) > 0: Out.write(' ' + Line) | |
| Out.write(']\n') | |
| Out.close() | |
| Win.InfoDialog('Python file generated', ScriptName) |
| # Pattern Along script | |
| # Demonstrates copying a sketch along a bspline | |
| # For use with Alibre Design | |
| from __future__ import division | |
| from math import sqrt | |
| ScriptName = 'Pattern Along' | |
| # adds a copy of a sketch to a specific point and normal | |
| def AddPattern(CurrentPart, Point, Normal, SourceSketch): | |
| Pl = CurrentPart.AddPlane('Sk', Normal, Point) | |
| PlSk = CurrentPart.AddSketch('PlSk', Pl) | |
| SkPoint = PlSk.GlobaltoPoint(Point[0], Point[1], Point[2]) | |
| PlSk.CopyFrom(SourceSketch, 0, 0, 0, SkPoint[0], SkPoint[1], 0, 0, 100.0) | |
| Pl.Hide() | |
| Win = Windows() | |
| # define options to show in dialog window | |
| Options = [] | |
| Options.append([None, WindowsInputTypes.Image, 'PatternAlong.png', 170]) | |
| Options.append(['Path 3D Sketch', WindowsInputTypes.Sketch3D, None]) | |
| Options.append(['Pattern 2D Sketch', WindowsInputTypes.Sketch, None]) | |
| Options.append(['Number of Patterns', WindowsInputTypes.Integer, 10]) | |
| Options.append(['Add Pattern to Start', WindowsInputTypes.Boolean, False]) | |
| Options.append(['Add Pattern to End', WindowsInputTypes.Boolean, False]) | |
| # show dialog to user, get inputs | |
| Values = Win.OptionsDialog(ScriptName, Options, 170) | |
| if Values == None: | |
| sys.exit() | |
| # get the inputs | |
| PathSketch = Values[1] | |
| PatternSketch = Values[2] | |
| NumPatterns = Values[3] | |
| AddtoStart = Values[4] | |
| AddtoEnd = Values[5] | |
| # validate | |
| if PathSketch == None: | |
| Win.ErrorDialog('No path sketch selected', ScriptName) | |
| sys.exit() | |
| if PatternSketch == None: | |
| Win.ErrorDialog('No pattern sketch selected', ScriptName) | |
| sys.exit() | |
| if NumPatterns < 1: | |
| Win.ErrorDialog('Invalid number of patterns', ScriptName) | |
| sys.exit() | |
| NumSegments = NumPatterns + 1 | |
| # get the part to use | |
| Prt = PathSketch.GetPart() | |
| # get the bspline from the sketch | |
| Spl = None | |
| for Figure in PathSketch.Figures: | |
| if isinstance(Figure, Bspline3D): | |
| Spl = Figure | |
| # check bspline was found | |
| if Spl == None: | |
| Win.ErrorDialog('No Bspline found in the path sketch', ScriptName) | |
| sys.exit() | |
| print 'Calculating...' | |
| # divide the bspline up into segments and get the point between the | |
| # segments and the normal at each point | |
| SubPt = Spl.SubdivideGetNormals(NumSegments) | |
| # extract points and normals and create sketch copies | |
| it = iter(SubPt) | |
| for Pt in it: | |
| Point = [Pt, next(it), next(it)] | |
| Normal = [next(it), next(it), next(it)] | |
| AddPattern(Prt, Point, Normal, PatternSketch) | |
| # add copy of sketch to start of bspline | |
| if AddtoStart == True: | |
| Point = Spl.GetPointAt(0.0) | |
| Normal = Spl.GetNormalAt(0.0) | |
| AddPattern(Prt, Point, Normal, PatternSketch) | |
| # add copy of sketch to end of bspline | |
| if AddtoEnd == True: | |
| Point = Spl.GetPointAt(1.0) | |
| Normal = Spl.GetNormalAt(1.0) | |
| AddPattern(Prt, Point, Normal, PatternSketch) |
| # sketch copier | |
| # Demonstrates copying a sketch inside a part | |
| Win = Windows() | |
| ScriptName = 'Sketch Copier' | |
| # create dialog window and show to user | |
| Options = [] | |
| Options.append([None, WindowsInputTypes.Image, 'SketchCopier.png', 170]) | |
| Options.append(['Source Sketch', WindowsInputTypes.Sketch, None]) | |
| Options.append(['Destination Plane', WindowsInputTypes.Plane, None]) | |
| Values = Win.OptionsDialog(ScriptName, Options, 170) | |
| if Values == None: | |
| sys.exit() | |
| # get user inputs | |
| SourceSketch = Values[1] | |
| DestPlane = Values[2] | |
| MyPart = CurrentPart() | |
| DestSketch = MyPart.AddSketch("Copy of " + SourceSketch.Name, DestPlane) | |
| DestSketch.CopyFrom(SourceSketch) | |