summaryrefslogtreecommitdiff
path: root/trunk/reprap/miscellaneous/python-beanshell-scripts/skeinforge_tools/craft_plugins/coil.py
blob: f3b8050f6f8427c99ec574d568734d731020ec63 (plain)
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
"""
This page is in the table of contents.
Coil is a script to coil wire or filament around an object.

==Operation==
The default 'Activate Coil' checkbox is on.  When it is on, the functions described below will work, when it is off, the functions will not be called.

==Settings==
===Minimum Tool Distance===
Default is twenty millimeters.

Defines the minimum distance between the wire dispenser and the object.  The 'Minimum Tool Distance' should be set to the maximum radius of the wire dispenser, times at least 1.3 to get a reasonable safety margin.

==Examples==
The following examples coil the file Screw Holder Bottom.stl.  The examples are run in a terminal in the folder which contains Screw Holder Bottom.stl and coil.py.


> python coil.py
This brings up the coil dialog.


> python coil.py Screw Holder Bottom.stl
The coil tool is parsing the file:
Screw Holder Bottom.stl
..
The coil tool has created the file:
Screw Holder Bottom_coil.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 coil
>>> coil.main()
This brings up the coil dialog.


>>> coil.writeOutput( 'Screw Holder Bottom.stl' )
Screw Holder Bottom.stl
The coil tool is parsing the file:
Screw Holder Bottom.stl
..
The coil tool has created the file:
Screw Holder Bottom_coil.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 interpret
from skeinforge_tools.skeinforge_utilities import settings
from skeinforge_tools.skeinforge_utilities import triangle_mesh
from skeinforge_tools.skeinforge_utilities.vector3 import Vector3
import os
import sys


__author__ = "Enrique Perez (perez_enrique@yahoo.com)"
__date__ = "$Date: 2008/21/04 $"
__license__ = "GPL 3.0"


def getCraftedText( fileName, gcodeText = '', repository = None ):
	"Coil the file or gcodeText."
	return getCraftedTextFromText( gcodec.getTextIfEmpty( fileName, gcodeText ), repository )

def getCraftedTextFromText( gcodeText, repository = None ):
	"Coil a gcode linear move gcodeText."
	if gcodec.isProcedureDoneOrFileIsEmpty( gcodeText, 'coil' ):
		return gcodeText
	if repository == None:
		repository = settings.getReadRepository( CoilRepository() )
	if not repository.activateCoil.value:
		return gcodeText
	return CoilSkein().getCraftedGcode( gcodeText, repository )

def getNewRepository():
	"Get the repository constructor."
	return CoilRepository()

def writeOutput( fileName = '' ):
	"Coil a gcode linear move file."
	fileName = interpret.getFirstTranslatorFileNameUnmodified( fileName )
	if fileName == '':
		return
	consecution.writeChainTextWithNounMessage( fileName, 'coil' )


class CoilRepository:
	"A class to handle the coil settings."
	def __init__( self ):
		"Set the default settings, execute title & settings fileName."
		profile.addListsToCraftTypeRepository( 'skeinforge_tools.craft_plugins.coil.html', self )
		self.fileNameInput = settings.FileNameInput().getFromFileName( interpret.getGNUTranslatorGcodeFileTypeTuples(), 'Open File for Coil', self, '' )
		self.activateCoil = settings.BooleanSetting().getFromValue( 'Activate Coil', self, True )
		self.minimumToolDistance = settings.FloatSpin().getFromValue( 10.0, 'Minimum Tool Distance (millimeters):', self, 50.0, 20.0 )
		self.executeTitle = 'Coil'

	def execute( self ):
		"Coil button has been clicked."
		fileNames = polyfile.getFileOrDirectoryTypesUnmodifiedGcode( self.fileNameInput.value, interpret.getImportPluginFileNames(), self.fileNameInput.wasCancelled )
		for fileName in fileNames:
			writeOutput( fileName )



class CoilSkein:
	"A class to coil a skein of extrusions."
	def __init__( self ):
		self.boundaryLayers = []
		self.distanceFeedRate = gcodec.DistanceFeedRate()
		self.lineIndex = 0
		self.lines = None
		self.oldLocationComplex = complex()
		self.perimeterWidth = 0.6
		self.shutdownLines = []

	def addCoilLayer( self, boundaryLayers, radius, z ):
		"Add a coil layer."
		self.distanceFeedRate.addLine( '(<layer> %s )' % z ) # Indicate that a new layer is starting.
		self.distanceFeedRate.addLine( '(<surroundingLoop>)' )
		thread = []
		for boundaryLayerIndex in xrange( 1, len( boundaryLayers ) - 1 ):
			boundaryLayer = boundaryLayers[ boundaryLayerIndex ]
			boundaryLayerBegin = boundaryLayers[ boundaryLayerIndex - 1 ]
			boundaryLayerEnd = boundaryLayers[ boundaryLayerIndex + 1 ]
			beginLocation = Vector3( 0.0, 0.0, 0.5 * ( boundaryLayerBegin.z + boundaryLayer.z ) )
			outsetLoop = intercircle.getLargestInsetLoopFromLoop( boundaryLayer.loops[ 0 ], - radius )
			self.addCoilToThread( beginLocation, 0.5 * ( boundaryLayer.z + boundaryLayerEnd.z ), outsetLoop, thread )
		self.addGcodeFromThread( thread )
		self.distanceFeedRate.addLine( '(</surroundingLoop>)' )
		self.distanceFeedRate.addLine( '(</layer>)' )

	def addCoilLayers( self ):
		"Add the coil layers."
		numberOfLayersFloat = round( self.perimeterWidth / self.layerThickness )
		numberOfLayers = int( numberOfLayersFloat )
		halfLayerThickness = 0.5 * self.layerThickness
		startOutset = self.repository.minimumToolDistance.value + halfLayerThickness
		startZ = self.boundaryLayers[ 0 ].z + halfLayerThickness
		zRange = self.boundaryLayers[ - 1 ].z - self.boundaryLayers[ 0 ].z
		zIncrement = 0.0
		if zRange >= 0.0:
			zIncrement = zRange / numberOfLayersFloat
		for layerIndex in xrange( numberOfLayers ):
			boundaryLayers = self.boundaryLayers
			if layerIndex % 2 == 1:
				boundaryLayers = self.boundaryReverseLayers
			radius = startOutset + layerIndex * self.layerThickness
			z = startZ + layerIndex * zIncrement
			self.addCoilLayer( boundaryLayers, radius, z )

	def addCoilToThread( self, beginLocation, endZ, loop, thread ):
		"Add a coil to the thread."
		if len( loop ) < 1:
			return
		loop = euclidean.getLoopStartingNearest( self.halfPerimeterWidth, self.oldLocationComplex, loop )
		length = euclidean.getPolygonLength( loop )
		if length <= 0.0:
			return
		oldPoint = loop[ 0 ]
		pathLength = 0.0
		for point in loop[ 1 : ]:
			pathLength += abs( point - oldPoint )
			along = pathLength / length
			z = ( 1.0 - along ) * beginLocation.z + along * endZ
			location = Vector3( point.real, point.imag, z )
			thread.append( location )
			oldPoint = point
		self.oldLocationComplex = loop[ - 1 ]

	def addGcodeFromThread( self, thread ):
		"Add a thread to the output."
		if len( thread ) > 0:
			firstLocation = thread[ 0 ]
			self.distanceFeedRate.addGcodeMovementZ( firstLocation.dropAxis( 2 ), firstLocation.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 addGcodeFromThread in coil, this should never happen" )
			print( thread )
			return
		self.distanceFeedRate.addLine( "M101" ) # Turn extruder on.
		for location in thread[ 1 : ]:
			self.distanceFeedRate.addGcodeMovementZ( location.dropAxis( 2 ), location.z )
		self.distanceFeedRate.addLine( "M103" ) # Turn extruder off.

	def getCraftedGcode( self, gcodeText, repository ):
		"Parse gcode text and store the coil gcode."
		self.repository = repository
		self.lines = gcodec.getTextLines( gcodeText )
		self.parseInitialization()
		self.parseBoundaries()
		self.parseUntilLayer()
		self.addCoilLayers()
		self.distanceFeedRate.addLines( self.shutdownLines )
		return self.distanceFeedRate.output.getvalue()

	def parseBoundaries( self ):
		"Parse the boundaries and add them to the boundary layers."
		boundaryLoop = None
		boundaryLayer = None
		for line in self.lines[ self.lineIndex : ]:
			splitLine = gcodec.getSplitLineBeforeBracketSemicolon( line )
			firstWord = gcodec.getFirstWord( splitLine )
			if len( self.shutdownLines ) > 0:
				self.shutdownLines.append( line )
			if firstWord == '(</boundaryPerimeter>)':
				boundaryLoop = None
			elif firstWord == '(<boundaryPoint>':
				location = gcodec.getLocationFromSplitLine( None, splitLine )
				if boundaryLoop == None:
					boundaryLoop = []
					boundaryLayer.loops.append( boundaryLoop )
				boundaryLoop.append( location.dropAxis( 2 ) )
			elif firstWord == '(<layer>':
				boundaryLayer = euclidean.LoopLayer( float( splitLine[ 1 ] ) )
				self.boundaryLayers.append( boundaryLayer )
			elif firstWord == '(</extrusion>)':
				self.shutdownLines = [ line ]
		for boundaryLayer in self.boundaryLayers:
			if not euclidean.isWiddershins( boundaryLayer.loops[ 0 ] ):
				boundaryLayer.loops[ 0 ].reverse()
		self.boundaryReverseLayers = self.boundaryLayers[ : ]
		self.boundaryReverseLayers.reverse()

	def parseInitialization( self ):
		"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> coil </procedureDone>)' )
				return
			elif firstWord == '(<layerThickness>':
				self.layerThickness = float( splitLine[ 1 ] )
			elif firstWord == '(<perimeterWidth>':
				self.perimeterWidth = float( splitLine[ 1 ] )
				self.halfPerimeterWidth = 0.5 * self.perimeterWidth
			self.distanceFeedRate.addLine( line )

	def parseUntilLayer( self ):
		"Parse until the layer line and add it to the coil skein."
		for self.lineIndex in xrange( self.lineIndex, len( self.lines ) ):
			line = self.lines[ self.lineIndex ]
			splitLine = gcodec.getSplitLineBeforeBracketSemicolon( line )
			firstWord = gcodec.getFirstWord( splitLine )
			self.distanceFeedRate.parseSplitLine( firstWord, splitLine )
			if firstWord == '(<layer>':
				return
			self.distanceFeedRate.addLine( line )


def main():
	"Display the coil dialog."
	if len( sys.argv ) > 1:
		writeOutput( ' '.join( sys.argv[ 1 : ] ) )
	else:
		settings.startMainLoopFromConstructor( getNewRepository() )

if __name__ == "__main__":
	main()