-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprusaFusionCommand_v3.py
executable file
·216 lines (162 loc) · 7.72 KB
/
prusaFusionCommand_v3.py
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
import adsk.core, traceback
import adsk.fusion
import tempfile
import json
import webbrowser
from .packages import requests
from .packages.requests_toolbelt import MultipartEncoder
from xml.etree import ElementTree
from xml.etree.ElementTree import SubElement
from os.path import expanduser
import os
import sys
import subprocess
from .Fusion360CommandBase import Fusion360CommandBase
# Creates directory and returns file name for settings file
def get_file_name():
# Get Home directory
home = expanduser("~")
home += '/PrusaFusion/'
# Create if doesn't exist
if not os.path.exists(home):
os.makedirs(home)
# Create file name in this path
xmlFileName = home + 'settings.xml'
return xmlFileName
# Writes user settings to a file in local home directory
def write_settings(xml_file_name, key, host):
# If file doesn't exist create it
if not os.path.isfile(xml_file_name):
new_file = open(xml_file_name, 'w')
new_file.write('<?xml version="1.0"?>')
new_file.write("<PrusaFusion /> ")
new_file.close()
tree = ElementTree.parse(xml_file_name)
root = tree.getroot()
# Otherwise delete existing settings
else:
tree = ElementTree.parse(xml_file_name)
root = tree.getroot()
root.remove(root.find('settings'))
# Write settings
settings = SubElement(root, 'settings')
SubElement(settings, 'host', value=host)
SubElement(settings, 'key', value=key)
tree.write(xml_file_name)
# Read user settings in from XML file
def read_settings(xmlFileName):
# Get the root of the XML tree
tree = ElementTree.parse(xmlFileName)
root = tree.getroot()
# Get the settings values
host = root.find('settings/host').attrib['value']
key = root.find('settings/key').attrib['value']
return host, key
# Export an STL file of selection to local temp directory
def export_file(stl_refinement, selection, filename):
# Get the ExportManager from the active design.
app = adsk.core.Application.get()
design = adsk.fusion.Design.cast(app.activeProduct)
export_mgr = design.exportManager
# Set model units to mm for export to cura engine
fusion_units_manager = design.fusionUnitsManager
current_units = fusion_units_manager.distanceDisplayUnits
fusion_units_manager.distanceDisplayUnits = adsk.fusion.DistanceUnits.MillimeterDistanceUnits
# Create a temporary directory.
temp_dir = tempfile.mkdtemp()
# If you want to randomize the file name
# resultFilename = tempDir + '//' + str(uuid.uuid1())
# Create temp file name
result_filename = temp_dir + '//' + filename
result_filename = result_filename + '.stl'
# Create export options for STL export
stl_options = export_mgr.createSTLExportOptions(selection, result_filename)
# Set export options based on refinement drop down:
if stl_refinement == 'Low':
stl_options.meshRefinement = adsk.fusion.MeshRefinementSettings.MeshRefinementLow
elif stl_refinement == 'Medium':
stl_options.meshRefinement = adsk.fusion.MeshRefinementSettings.MeshRefinementMedium
elif stl_refinement == 'High':
stl_options.meshRefinement = adsk.fusion.MeshRefinementSettings.MeshRefinementHigh
# Execute Export command
export_mgr.execute(stl_options)
fusion_units_manager.distanceDisplayUnits = current_units
return result_filename
# Get the current values of the command inputs.
def get_inputs(inputs):
try:
stlRefinementInput = inputs.itemById('stlRefinement')
stlRefinement = stlRefinementInput.selectedItem.name
selection = inputs.itemById('selection').selection(0).entity
if selection.objectType == adsk.fusion.Occurrence.classType():
selection = selection.component
key = inputs.itemById('key').text
host = inputs.itemById('host').text
saveSettings = inputs.itemById('saveSettings').value
return stlRefinement, selection, key, host, saveSettings
except:
app = adsk.core.Application.get()
ui = app.userInterface
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
############# Create your Actions Here #################################################
class octoFusionCommand(Fusion360CommandBase):
# Runs when when any input in the command dialog is changed
def onInputChanged(self, command, inputs, changedInput):
# Get current input values
host = inputs.itemById('host').text
key = inputs.itemById('key').text
# Runs when the user presses ok button
def onExecute(self, command, inputs):
# Get the inputs.
(stlRefinement, selection, key, host, saveSettings) = get_inputs(
inputs)
filename = selection.name
# Export the selected file as an STL to temp directory
result_filename = export_file(stlRefinement, selection, filename)
# Optionally save the users settings to a local XML
if saveSettings:
xml_file_name = get_file_name()
write_settings(xml_file_name, key, host)
# Connect to the OctoPrint server and upload the new file
#upload_file(result_filename, filename + '.stl', host, key)
#os.system("C:\\Windows\\putty.exe");
pscpPath = os.getcwd() + '\\Api\\InternalAddins\\OctoFusion\\Resources\\'
#path.expandvars(r'%APPDATA%\Microsoft\Windows\SendTo')
subprocess.call([pscpPath + 'pscp.exe', '-pw', key , result_filename, host +':/tmp'],shell=True)
# Launch local browser and display OctoPrint page
url = 'http://' + pscpPath
webbrowser.open_new(url)
# Runs when user selects your command from Fusion UI, Build UI here
def onCreate(self, command, inputs):
inputs.addImageCommandInput('image1', '', './/Resources//octoprint-logo.png')
inputs.addTextBoxCommandInput('labelText2', '',
'<a href="http://github.org">Github</a></span> Export from am Virtual Machine to Prusa Slicer on a Linux Host',
4, True)
inputs.addTextBoxCommandInput('labelText3', '',
'Choose the file type and selection to send to Octoprint for quotes.', 2, True)
stldropDown = inputs.addDropDownCommandInput('stlRefinement', 'STL refinement',
adsk.core.DropDownStyles.LabeledIconDropDownStyle)
stldropDown.listItems.add('Low', False)
stldropDown.listItems.add('Medium', False)
stldropDown.listItems.add('High', True)
selection = inputs.addSelectionInput('selection', 'Selection', 'Select the component to print')
selection.addSelectionFilter('Occurrences')
selection.addSelectionFilter('RootComponents')
# selection.addSelectionFilter('SolidBodies')
host_input = inputs.addTextBoxCommandInput('host', 'SSH to host: ', '[email protected]',
1, False)
key_input = inputs.addTextBoxCommandInput('key', 'Password: ', 'Password for user', 1, False)
inputs.addBoolValueInput("saveSettings", 'Save settings?', True)
command.setDialogInitialSize(500, 300)
command.setDialogMinimumSize(300, 300)
command.okButtonText = 'Ok'
# Get filename for settings file
xml_file_name = get_file_name()
# If there is a local settings file apply the values
if os.path.isfile(xml_file_name):
(host, key) = read_settings(xml_file_name)
host_input.text = host
key_input.text = key
# Update drop down values based on currently available profiles
#octoProfiles(key, host)