-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDXFObjects.py
More file actions
274 lines (234 loc) · 10.3 KB
/
DXFObjects.py
File metadata and controls
274 lines (234 loc) · 10.3 KB
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
#**************************************************************************
#* *
#* Copyright (c) 2023 Keith Sloan <keith@sloan-home.co.uk> *
#* *
#* This program is free software; you can redistribute it and/or modify *
#* it under the terms of the GNU Lesser General Public License (LGPL) *
#* as published by the Free Software Foundation; either version 2 of *
#* the License, or (at your option) any later version. *
#* for detail see the LICENCE text file. *
#* *
#* This program is distributed in the hope that it will be useful, *
#* but WITHOUT ANY WARRANTY; without even the implied warranty of *
#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
#* GNU Library General Public License for more details. *
#* *
#* You should have received a copy of the GNU Library General Public *
#* License along with this program; if not, write to the Free Software *
#* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
#* USA *
#* *
#* Acknowledgements : *
#* *
#***************************************************************************
import FreeCAD, FreeCADGui, Part, Mesh, OpenSCADUtils
import os, sys, tempfile
from OpenSCADdxf import importEZDXFshape
class DXFBase:
def __init__(self, obj, filename):
super().__init__()
obj.addProperty("App::PropertyFile","source","DXF-Object","DXF source")
obj.source = obj.Label+".dxf"
obj.setEditorMode("source",1)
obj.addProperty("App::PropertyFile","sourceFile","DXF-Object","DXF source File")
obj.sourceFile = filename
#obj.setEditorMode("sourceFile",2)
obj.addProperty("App::PropertyString","message","DXF-Object","DXF-Objectmessage")
obj.addProperty("App::PropertyBool","edit","DXF-Object","Edit DXF")
obj.addProperty("App::PropertyBool","execute","DXF-Object","Process DXF source")
#self.obj = obj
obj.Proxy = self
self.createGeometry(obj)
def onChanged(self, fp, prop):
print(f"{fp.Label} State : {fp.State} prop : {prop}")
if "Restore" in fp.State:
return
if prop in ["Shape"]:
print(f"OnChange Shape {fp.Shape}")
return
if prop in ["execute"]:
if fp.execute == True:
self.executeFunction(fp)
fp.execute = False
else:
print(f"Touched execute Shape {fp.Shape}")
#obj.Shape = self.newShape
if prop in ["edit"]:
if fp.edit == True:
self.editFile(fp.sourceFile)
fp.edit = False
FreeCADGui.Selection.addSelection(fp)
if prop in ["message"]:
print("message changed")
FreeCADGui.updateGui()
def execute(self, fp):
'''Do something when doing a recomputation, this method is mandatory'''
print(f"execute")
def executeFunction(self, obj):
from timeit import default_timer as timer
print(f"Execute {obj.Name}")
#start = timer()
#print(dir(obj))
obj.message = ""
shp = importEZDXFshape(obj.sourceFile)
if shp is not None:
print(f"Initial Shape {obj.Shape}")
print(f"Returned Shape {shp}")
#shp.check()
#newShp = shp.copy()
#print(f"New Shape {newShp}")
#print(f"Old Shape {shp}")
obj.Shape = shp
#if isinstance(shp, tuple): #MattC commented out 20230807 0838
# obj.Shape = shp[0]
#else:
# obj.Shape = shp
else:
print(f"Shape is None")
obj.Shape = Part.Shape()
print(f"Function Object Shape {obj.Shape}")
obj.execute = False
#end = timer()
#print(f"==== Create Shape took {end-start} secs ====")
#obj.recompute()
FreeCAD.ActiveDocument.recompute()
FreeCADGui.Selection.addSelection(obj)
FreeCADGui.SendMsgToActiveView("ViewFit")
# Need to update Gui for properties change
# try and catch as puts out warning
try:
obj.execute = False
FreeCADGui.updateGui()
except err:
print(f"Warning {err}")
#FreeCADGui.Selection.addSelection(obj)
def copyFile(self, src, trg):
print(f"Copy File {src} {trg}")
fps = open(src,'r')
fpt = open(trg,'w')
buffer = fps.read()
fpt.write(buffer)
fpt.close()
fps.close()
def editFile(self, fname):
import FreeCAD
import subprocess, os, sys
editorPathName = FreeCAD.ParamGet(\
"User parameter:BaseApp/Preferences/Mod/OpenSCAD").GetString('externalDXFeditor')
print(f"Path to external DXF editor {editorPathName}")
# ToDo : Check pathname valid
if editorPathName != "":
p1 = subprocess.Popen( \
[editorPathName, fname], \
stdin=subprocess.PIPE,\
stdout=subprocess.PIPE,stderr=subprocess.PIPE)
else:
print(f"External DXF Editor preference editorPathName not set")
def createGeometry(self, obj):
import FreeCAD, Part
print("create Geometry") #def getSource(self):
print("Do not process DXF source on Document recompute")
return
print(f"Active Document {FreeCAD.ActiveDocument.Name}")
#shp = shapeFromSourceFile(obj, keepWork, modules = obj.modules)
shp = shapeFromSourceFile(obj, modules = obj.modules)
print(f"Active Document {FreeCAD.ActiveDocument.Name}")
if shp is not None:
print(f"Initial Shape {obj.Shape}")
print(f"Returned Shape {shp}")
shp.check()
newShp = shp.copy()
print(f"New Shape {newShp}")
print(f"Old Shape {shp}")
#obj.Shape = shp.copy()
obj.Shape = newShp
else:
print(f"Shape is None")
class DXFObject(DXFBase):
def __init__(self, obj, filename):
import FreeCAD, os, tempfile, Part
super().__init__(obj, filename)
#tmpDir = obj.Document.TransientDir
#print(f"Doc temp dir {tmpDir}")
tmpDir = tempfile.gettempdir()
#tmpDir = obj.Document.getPropertyByName("TransientDir")
print(f"Doc temp dir {tmpDir}")
tmpPath = os.path.join(tmpDir, obj.source)
print(f"Path {tmpPath}")
self.copyFile(filename, tmpPath)
# After creating
#dir_list = os.listdir(tmpDir)
#print("List of directories and files after creation:")
#print(dir_list)
obj.sourceFile = tmpPath
obj.setEditorMode("sourceFile",2)
def __getstate__(self):
"""When saving the document this object gets stored using Python's json
module.
Since we have some un-serializable parts here -- the Coin stuff --
we must define this method\
to return a tuple of all serializable objects or None."""
if hasattr(self, "obj"):
if hasattr(self.obj, "sourceFile"):
print(f"Save Source File {self.obj.sourceFile}")
sf = open(self.obj.sourceFile, 'r')
buffer = sf.read()
return {"sourceFile": [self.obj.sourceFile, buffer]}
else:
pass
def __setstate__(self, arg):
import os, tempfile
"""When restoring the serialized object from document we have the
chance to set some internals here. Since no data were serialized
nothing needs to be done here."""
print(f"Restore {type(arg)} {arg}")
tmpDir = tempfile.gettempdir()
sourceName = arg.keys()[0]
print(f"{arg[sourceName]}")
sourcePath = os.path.join(tmpDir, soureName)
print(f"Source Path {sourcePath}")
fp = open(sourcePath,"w")
if fp is not None:
fp.write(arg[sourceName])
else:
print(f"Failed to open {sourcePath}")
class ViewDXFProvider:
def __init__(self, obj):
"""Set this object to the proxy object of the actual view provider"""
obj.Proxy = self
def updateData(self, fp, prop):
"""If a property of the handled feature has changed we have the chance to handle this here"""
pass
def getDisplayModes(self, obj):
"""Return a list of display modes."""
# print("getDisplayModes")
modes = []
modes.append("Shaded")
modes.append("Wireframe")
modes.append("Points")
return modes
def getDefaultDisplayMode(self):
"""Return the name of the default display mode. It must be defined in getDisplayModes."""
return "Shaded"
def setDisplayMode(self, mode):
"""Map the display mode defined in attach with those defined in getDisplayModes.\
Since they have the same names nothing needs to be done. This method is optional"""
return mode
def onChanged(self, vp, prop):
"""Here we can do something when a single property got changed"""
print(f"View Provider OnChanged : prop {prop}")
def getIcon(self):
"""Return the icon in XPM format which will appear in the tree view. This method is\
optional and if not defined a default icon is shown."""
def __getstate__(self):
"""When saving the document this object gets stored using Python's json
module.
Since we have some un-serializable parts here -- the Coin stuff --
we must define this method\
to return a tuple of all serializable objects or None."""
return None
def __setstate__(self, arg):
"""When restoring the serialized object from document we have the
chance to set some internals here. Since no data were serialized
nothing needs to be done here."""
pass