1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
"""
This page is in the table of contents.
Clip clips the ends of loops to prevent bumps from forming.
The clip manual page is at:
http://www.bitsfrombytes.com/wiki/index.php?title=Skeinforge_Clip
==Operation==
The default 'Activate Clip' checkbox is on. When it is on, the functions described below will work, when it is off, the functions will not be called.
==Settings==
===Clip Over Perimeter Width===
Default is 0.2.
Defines the ratio of the amount each end of the loop is clipped over the perimeter width. The total gap will therefore be twice the clip. If the ratio is too high loops will have a gap, if the ratio is too low there will be a bulge at the loop ends.
===Connect Loops===
Default is on.
When selected, clip will connect nearby loops, combining them into a spiral.
==Examples==
The following examples clip the file Screw Holder Bottom.stl. The examples are run in a terminal in the folder which contains Screw Holder Bottom.stl and clip.py.
> python clip.py
This brings up the clip dialog.
> python clip.py Screw Holder Bottom.stl
The clip tool is parsing the file:
Screw Holder Bottom.stl
..
The clip tool has created the file:
.. Screw Holder Bottom_clip.gcode
> python
Python 2.5.1 (r251:54863, Sep 22 2007, 01:43:31)
[GCC 4.2.1 (SUSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import clip
>>> clip.main()
This brings up the clip dialog.
>>> clip.writeOutput( 'Screw Holder Bottom.stl' )
The clip tool is parsing the file:
Screw Holder Bottom.stl
..
The clip tool has created the file:
.. Screw Holder Bottom_clip.gcode
"""
from __future__ import absolute_import
#Init has to be imported first because it has code to workaround the python bug where relative imports don't work if the module is imported as a main module.
import __init__
from skeinforge_tools import profile
from skeinforge_tools.meta_plugins import polyfile
from skeinforge_tools.skeinforge_utilities import consecution
from skeinforge_tools.skeinforge_utilities import euclidean
from skeinforge_tools.skeinforge_utilities import gcodec
from skeinforge_tools.skeinforge_utilities import intercircle
from skeinforge_tools.skeinforge_utilities import settings
from skeinforge_tools.skeinforge_utilities import interpret
import math
import sys
__author__ = "Enrique Perez (perez_enrique@yahoo.com)"
__date__ = "$Date: 2008/21/04 $"
__license__ = "GPL 3.0"
def getCraftedText( fileName, text, clipRepository = None ):
"Clip a gcode linear move file or text."
return getCraftedTextFromText( gcodec.getTextIfEmpty( fileName, text ), clipRepository )
def getCraftedTextFromText( gcodeText, clipRepository = None ):
"Clip a gcode linear move text."
if gcodec.isProcedureDoneOrFileIsEmpty( gcodeText, 'clip' ):
return gcodeText
if clipRepository == None:
clipRepository = settings.getReadRepository( ClipRepository() )
if not clipRepository.activateClip.value:
return gcodeText
return ClipSkein().getCraftedGcode( clipRepository, gcodeText )
def getNewRepository():
"Get the repository constructor."
return ClipRepository()
def writeOutput( fileName = '' ):
"Clip a gcode linear move file. Chain clip the gcode if it is not already clipped. If no fileName is specified, clip the first unmodified gcode file in this folder."
fileName = interpret.getFirstTranslatorFileNameUnmodified( fileName )
if fileName != '':
consecution.writeChainTextWithNounMessage( fileName, 'clip' )
class ClipRepository:
"A class to handle the clip settings."
def __init__( self ):
"Set the default settings, execute title & settings fileName."
profile.addListsToCraftTypeRepository( 'skeinforge_tools.craft_plugins.clip.html', self )
self.fileNameInput = settings.FileNameInput().getFromFileName( interpret.getGNUTranslatorGcodeFileTypeTuples(), 'Open File for Clip', self, '' )
self.openWikiManualHelpPage = settings.HelpPage().getOpenFromAbsolute( 'http://www.bitsfrombytes.com/wiki/index.php?title=Skeinforge_Clip' )
self.activateClip = settings.BooleanSetting().getFromValue( 'Activate Clip', self, True )
self.clipOverPerimeterWidth = settings.FloatSpin().getFromValue( 0.1, 'Clip Over Perimeter Width (ratio):', self, 0.8, 0.5 )
self.connectLoops = settings.BooleanSetting().getFromValue( 'Connect Loops', self, True )
self.executeTitle = 'Clip'
def execute( self ):
"Clip button has been clicked."
fileNames = polyfile.getFileOrDirectoryTypesUnmodifiedGcode( self.fileNameInput.value, interpret.getImportPluginFileNames(), self.fileNameInput.wasCancelled )
for fileName in fileNames:
writeOutput( fileName )
class ClipSkein:
"A class to clip a skein of extrusions."
def __init__( self ):
self.distanceFeedRate = gcodec.DistanceFeedRate()
self.extruderActive = False
self.feedRateMinute = None
self.isLoopPerimeter = False
self.loopPath = None
self.lineIndex = 0
self.oldLocation = None
self.oldWiddershins = None
def addGcodeFromThreadZ( self, thread, z ):
"Add a gcode thread to the output."
if len( thread ) > 0:
self.distanceFeedRate.addGcodeMovementZWithFeedRate( self.travelFeedRatePerMinute, thread[ 0 ], z )
else:
print( "zero length vertex positions array which was skipped over, this should never happen" )
if len( thread ) < 2:
print( "thread of only one point in clip, this should never happen" )
print( thread )
return
self.distanceFeedRate.addLine( 'M101' )
for point in thread[ 1 : ]:
self.distanceFeedRate.addGcodeMovementZWithFeedRate( self.feedRateMinute, point, z )
def addSegmentToPixelTables( self, location, maskPixelTable, oldLocation ):
"Add the segment to the layer and mask table."
# segmentTable = {}
euclidean.addValueSegmentToPixelTable( oldLocation.dropAxis( 2 ), location.dropAxis( 2 ), self.layerPixelTable, None, self.layerPixelWidth )
# euclidean.addValueSegmentToPixelTable( oldLocation.dropAxis( 2 ), location.dropAxis( 2 ), segmentTable, None, self.layerPixelWidth )
# euclidean.addPixelTableToPixelTable( segmentTable, self.layerPixelTable )
# euclidean.addPixelTableToPixelTable( segmentTable, maskPixelTable )
# self.maskPixelTableTable[ location ] = maskPixelTable
# self.maskPixelTableTable[ oldLocation ] = maskPixelTable
def addTailoredLoopPath( self, line ):
"Add a clipped loop path."
if self.clipLength > 0.0:
removeTable = {}
euclidean.addLoopToPixelTable( self.loopPath.path, removeTable, self.layerPixelWidth )
euclidean.removePixelTableFromPixelTable( removeTable, self.layerPixelTable )
self.loopPath.path = euclidean.getClippedLoopPath( self.clipLength, self.loopPath.path )
self.loopPath.path = euclidean.getSimplifiedPath( self.loopPath.path, self.perimeterWidth )
euclidean.addLoopToPixelTable( self.loopPath.path, self.layerPixelTable, self.layerPixelWidth )
if self.oldWiddershins == None:
self.addGcodeFromThreadZ( self.loopPath.path, self.loopPath.z )
else:
if self.oldWiddershins != euclidean.isWiddershins( self.loopPath.path ):
self.loopPath.path.reverse()
# self.addGcodeFromThreadZ( self.loopPath.path, self.loopPath.z )
for point in self.loopPath.path:
self.distanceFeedRate.addGcodeMovementZWithFeedRate( self.feedRateMinute, point, self.loopPath.z )
if self.getNextThreadIsACloseLoop( self.loopPath.path ):
self.oldWiddershins = euclidean.isWiddershins( self.loopPath.path )
else:
self.oldWiddershins = None
self.distanceFeedRate.addLine( line )
self.loopPath = None
def getConnectionIsCloseWithoutOverlap( self, location, path ):
"Determine if the connection is close enough and does not overlap another thread."
if len( path ) < 1:
return False
locationComplex = location.dropAxis( 2 )
segment = locationComplex - path[ - 1 ]
segmentLength = abs( segment )
if segmentLength <= 0.0:
return True
segment /= segmentLength
distance = self.connectingStepLength
segmentEndLength = segmentLength - self.connectingStepLength
while distance < segmentEndLength:
alongPoint = distance * segment + path[ - 1 ]
if not euclidean.isPointInsideLoops( self.boundaryLoops, alongPoint ):
return False
distance += self.connectingStepLength
# removedLayerPixelTable = self.layerPixelTable.copy()
# if self.oldLocation in self.maskPixelTableTable:
# euclidean.removePixelTableFromPixelTable( self.maskPixelTableTable[ self.oldLocation ], removedLayerPixelTable )
# euclidean.addPathToPixelTable( path[ : - 2 ], removedLayerPixelTable, None, self.layerPixelWidth )
segmentTable = {}
euclidean.addSegmentToPixelTable( path[ - 1 ], locationComplex, segmentTable, 2.0, 2.0, self.layerPixelWidth )
# euclidean.addValueSegmentToPixelTable( path[ - 1 ], locationComplex, segmentTable, None, self.layerPixelWidth )
# euclidean.addValueSegmentToPixelTable( path[ - 1 ], locationComplex, segmentTable, None, self.layerPixelWidth )
# maskPixelTable = {}
# if location in self.maskPixelTableTable:
# maskPixelTable = self.maskPixelTableTable[ location ]
if euclidean.isPixelTableIntersecting( self.layerPixelTable, segmentTable, {} ):
# if euclidean.isPixelTableIntersecting( removedLayerPixelTable, segmentTable, {} ):
return False
euclidean.addValueSegmentToPixelTable( path[ - 1 ], locationComplex, self.layerPixelTable, None, self.layerPixelWidth )
# euclidean.addPixelTableToPixelTable( segmentTable, self.layerPixelTable )
return True
def getCraftedGcode( self, clipRepository, gcodeText ):
"Parse gcode text and store the clip gcode."
self.lines = gcodec.getTextLines( gcodeText )
self.parseInitialization( clipRepository )
for self.lineIndex in xrange( self.lineIndex, len( self.lines ) ):
line = self.lines[ self.lineIndex ]
self.parseLine( line )
return self.distanceFeedRate.output.getvalue()
def getNextThreadIsACloseLoop( self, path ):
"Determine if the next thread is a loop."
if self.oldLocation == None:
return False
isLoop = False
location = self.oldLocation
for afterIndex in xrange( self.lineIndex + 1, len( self.lines ) ):
line = self.lines[ afterIndex ]
splitLine = gcodec.getSplitLineBeforeBracketSemicolon( line )
firstWord = gcodec.getFirstWord( splitLine )
if firstWord == '(<loop>' or firstWord == '(<perimeter>':
isLoop = True
elif firstWord == 'G1':
location = gcodec.getLocationFromSplitLine( self.oldLocation, splitLine )
elif firstWord == 'M101':
if not isLoop:
return False
return self.getConnectionIsCloseWithoutOverlap( location, path )
elif firstWord == '(<layer>':
return False
return False
def isNextExtruderOn( self ):
"Determine if there is an extruder on command before a move command."
for afterIndex in xrange( self.lineIndex + 1, len( self.lines ) ):
line = self.lines[ afterIndex ]
splitLine = gcodec.getSplitLineBeforeBracketSemicolon( line )
firstWord = gcodec.getFirstWord( splitLine )
if firstWord == 'G1' or firstWord == 'M103':
return False
elif firstWord == 'M101':
return True
return False
def linearMove( self, splitLine ):
"Add to loop path if this is a loop or path."
location = gcodec.getLocationFromSplitLine( self.oldLocation, splitLine )
self.feedRateMinute = gcodec.getFeedRateMinute( self.feedRateMinute, splitLine )
if self.isLoopPerimeter:
if self.isNextExtruderOn():
self.loopPath = euclidean.PathZ( location.z )
if self.loopPath == None:
if self.extruderActive:
self.oldWiddershins = None
else:
self.loopPath.path.append( location.dropAxis( 2 ) )
self.oldLocation = location
def parseInitialization( self, clipRepository ):
"Parse gcode initialization and store the parameters."
for self.lineIndex in xrange( len( self.lines ) ):
line = self.lines[ self.lineIndex ]
splitLine = gcodec.getSplitLineBeforeBracketSemicolon( line )
firstWord = gcodec.getFirstWord( splitLine )
self.distanceFeedRate.parseSplitLine( firstWord, splitLine )
if firstWord == '(</extruderInitialization>)':
self.distanceFeedRate.addLine( '(<procedureDone> clip </procedureDone>)' )
return
elif firstWord == '(<perimeterWidth>':
self.perimeterWidth = float( splitLine[ 1 ] )
self.clipLength = clipRepository.clipOverPerimeterWidth.value * self.perimeterWidth
self.layerPixelWidth = 0.1 * self.perimeterWidth
self.connectingStepLength = 0.5 * self.perimeterWidth
elif firstWord == '(<travelFeedRatePerSecond>':
self.travelFeedRatePerMinute = 60.0 * float( splitLine[ 1 ] )
self.distanceFeedRate.addLine( line )
def parseLine( self, line ):
"Parse a gcode line and add it to the clip skein."
splitLine = gcodec.getSplitLineBeforeBracketSemicolon( line )
if len( splitLine ) < 1:
return
firstWord = splitLine[ 0 ]
if firstWord == 'G1':
self.linearMove( splitLine )
elif firstWord == 'M101':
self.extruderActive = True
elif firstWord == 'M103':
self.extruderActive = False
self.isLoopPerimeter = False
if self.loopPath != None:
self.addTailoredLoopPath( line )
return
elif firstWord == '(<layer>':
self.setLayerPixelTable()
if firstWord == '(<loop>' or firstWord == '(<perimeter>':
self.isLoopPerimeter = True
if self.loopPath == None:
self.distanceFeedRate.addLine( line )
def setLayerPixelTable( self ):
"Set the layer pixel table."
boundaryLoop = None
extruderActive = False
maskPixelTable = {}
self.boundaryLoops = []
self.maskPixelTableTable = {}
self.lastInactiveLocation = None
self.layerPixelTable = {}
oldLocation = self.oldLocation
for afterIndex in xrange( self.lineIndex + 1, len( self.lines ) ):
line = self.lines[ afterIndex ]
splitLine = gcodec.getSplitLineBeforeBracketSemicolon( line )
firstWord = gcodec.getFirstWord( splitLine )
if firstWord == 'G1':
location = gcodec.getLocationFromSplitLine( oldLocation, splitLine )
if extruderActive and oldLocation != None:
self.addSegmentToPixelTables( location, maskPixelTable, oldLocation )
if not extruderActive:
self.lastInactiveLocation = location
oldLocation = location
elif firstWord == 'M101':
extruderActive = True
elif firstWord == 'M103':
if extruderActive and self.lastInactiveLocation != None:
self.addSegmentToPixelTables( self.lastInactiveLocation, maskPixelTable, oldLocation )
extruderActive = False
maskPixelTable = {}
elif firstWord == '(</boundaryPerimeter>)':
boundaryLoop = None
elif firstWord == '(<boundaryPoint>':
if boundaryLoop == None:
boundaryLoop = []
self.boundaryLoops.append( boundaryLoop )
location = gcodec.getLocationFromSplitLine( None, splitLine )
boundaryLoop.append( location.dropAxis( 2 ) )
elif firstWord == '(</layer>)':
return
def main():
"Display the clip dialog."
if len( sys.argv ) > 1:
writeOutput( ' '.join( sys.argv[ 1 : ] ) )
else:
settings.startMainLoopFromConstructor( getNewRepository() )
if __name__ == "__main__":
main()
|