-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathApp.py
1485 lines (1210 loc) · 53.3 KB
/
App.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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import wx
import sys
import cad
from Frame import Frame
import os
from HeeksConfig import HeeksConfig
import ContextTool
import SelectMode
from HDialog import HDialog
from HDialog import control_border
from NiceTextCtrl import LengthCtrl
from Printout import Printout
from PointDrawing import point_drawing
import geom
import Gear
from About import AboutBox
from FilterDlg import FilterDlg
import HImage
from Ribbon import RB
import Drawing
import LineArcDrawing
import DigitizeMode
import Key
SKETCH_OP_UNION = 1
SKETCH_OP_SUBTRACT = 2
SKETCH_OP_INTERSECT = 3
GraphicsTextModeNone = 0
GraphicsTextModeInputTitle = 1
GraphicsTextModeWithHelp = 2
pycad_dir = os.path.dirname(os.path.realpath(__file__))
HEEKS_WILDCARD_STRING = 'Heeks files |*.heeks;*.HEEKS'
wx_image_extensions = ['bmp','png','jpeg','jpg','gif','pcx','pnm','tif','tga','iff','xpm','ico','cur','ani'] # couldn't find GetHandlers in wxpython
sys.path.append(pycad_dir)
def OnMessageBox(error_message):
wx.MessageBox(error_message)
def OnPaint():
if wx.GetApp().coordsys_for_P3P != None:
wx.GetApp().RenderCoordSys()
wx.GetApp().input_mode_object.OnRender()
tools = []
save_filter_for_StartPickObjects = 0
save_just_one_for_EndPickObjects = False
save_mode_for_EndPickObjects = None
def CreateImage(): return HImage.HImage()
def CreateGear(): return Gear.Gear()
def ImportImageFile():
image = HImage.HImage(cad.GetFilePathForImportExport())
cad.AddUndoably(image)
class App(wx.App):
def __init__(self):
self.version_number = '2 0' # just main number and sub number. I'm not using sub-sub numbers
self.settings_restored = False
self.recent_files = []
self.MAX_RECENT_FILES = 20
self.filepath = None
self.hideable_windows = {} # map of window:bitmap_path
self.cad_dir = pycad_dir
self.bitmap_path = self.cad_dir + '/bitmaps'
self.inMainLoop = False
self.select_mode = SelectMode.SelectMode()
self.digitizing = DigitizeMode.DigitizeMode()
self.coordsys_for_P3P = None
self.paint_registered = False
self.previous_input_mode = None
self.input_mode_object = None
self.render_on_front_done = False
self.import_file_types = [
(['heeks'], 'Heeks files'),
(['stl'], 'STL files'),
(['svg'], 'Scalar Vector Graphics files'),
(['dxf'], 'DXF files'),
(['gbr', '.rs274x'], 'RS274X/Gerber files'),
(['pho'], 'PHO files'),
(['wrl'], 'VRML files for KiCAD'),
]
self.export_file_types = [
(['stl'], 'STL files'),
(['3mf'], '3MF files'),
(['dxf'], 'DXF files'),
(['cpp'], 'CPP files'),
(['py'], 'OpenCAMLib python files'),
(['obj'], 'Wavefront .obj files'),
(['wrl'], 'VRML files for KiCAD'),
]
self.graphics_text_mode = GraphicsTextModeWithHelp
save_out = sys.stdout
save_err = sys.stderr
wx.App.__init__(self)
sys.stdout = save_out
sys.stderr = save_err
def GetAppTitle(self):
# The title to appear on the title bar
return 'Python-Based CAD Software'
def GetAppConfigName(self):
# The name of the registry item
return 'HeeksCAD'
def OnInit(self):
self.RegisterMessageBoxCallback()
self.InitCad()
self.RegisterObjectTypes()
self.printData = wx.PrintData()
self.pageSetupData = wx.PageSetupDialogData(self.printData)
wx.InitAllImageHandlers()
# Turn on high-DPI awareness to make sure rendering is sharp on big
# monitors with font scaling enabled.
# from ctypes import OleDLL
# OleDLL('shcore').SetProcessDpiAwareness(1)
config = HeeksConfig()
width = config.ReadInt('MainFrameWidth', -1);
height = config.ReadInt('MainFrameHeight', -1);
x = config.ReadInt('MainFramePosX', -1);
y = config.ReadInt('MainFramePosY', -1);
if width < 0:
width = -1
height = -1
x = -1
y = -1
else:
stored_rect = wx.Rect(x, y, width, height)
in_display = False
for idx in range(wx.Display.GetCount()):
d = wx.Display(idx)
rect = d.GetGeometry()
if rect.Contains(wx.Point(x, y)):
in_display = True
if in_display == False:
width = -1
height = -1
x = -1
y = -1
self.LoadConfig()
self.frame = self.NewFrame(wx.Point(x, y), wx.Size(width, height))
self.frame.Show()
self.OnNewOrOpen(False)
cad.ClearHistory()
cad.SetLikeNewFile()
self.frame.SetFrameTitle()
return True
def RegisterObjectTypes(self):
HImage.type = cad.RegisterObjectType("Image", CreateImage)
Gear.type = cad.RegisterObjectType("Gear", CreateGear)
self.RegisterImportFileTypes(wx_image_extensions, 'Picture Files', ImportImageFile)
import Svg
self.RegisterExportFileTypes(['svg'], 'Svg Files', Svg.Export)
def OnNewOrOpen(self, open):
pass
def NewFrame(self, pos=wx.DefaultPosition, size=wx.DefaultSize):
return Frame(None, pos = pos, size = size)
def LoadConfig(self):
config = HeeksConfig(self.settings_restored)
self.LoadRecentFiles(config)
# snapping
cad.SetDigitizeEnd(config.ReadBool("Endof", True))
cad.SetDigitizeInters(config.ReadBool("Inters", False))
cad.SetDigitizeCentre(config.ReadBool("Centre", True))
cad.SetDigitizeMidpoint(config.ReadBool("Midpoint", False))
cad.SetDigitizeSnapToGrid(config.ReadBool("Grid", True))
cad.SetRotateUpright(config.ReadBool("RotateUpright", False))
self.graphics_text_mode = config.ReadInt("TextMode", GraphicsTextModeWithHelp)
cad.SetBackgroundColor(0, cad.Color(config.ReadInt("BackgroundColor0", cad.Color(230, 255, 255).ref())))
cad.SetBackgroundColor(1, cad.Color(config.ReadInt("BackgroundColor1", cad.Color(255, 255, 255).ref())))
cad.SetCurrentColor(cad.Color(config.ReadInt("CurrentColor", cad.Color(0, 0, 0).ref())))
cad.SetAntialiasing(config.ReadBool('Antialiasing', False))
cad.SetShowDatum(config.ReadBool('ShowDatum', True))
def GetDefaultDir(self):
default_directory = os.getcwd()
if len(self.recent_files) > 0:
default_directory = self.recent_files[0]
default_directory = os.path.dirname(os.path.realpath(default_directory))
return default_directory
def GetDefaultFilename(self):
return 'Untitled.heeks'
def LoadRecentFiles(self, config):
for i in range(0, self.MAX_RECENT_FILES):
key = 'RecentFilePath' + str(i)
filepath = config.Read(key)
if len(filepath) == 0:
break
self.recent_files.append(filepath)
def WriteRecentFiles(self, config = None):
if config == None:
config = HeeksConfig(self.settings_restored)
index = 0
for filepath in self.recent_files:
if index >= self.MAX_RECENT_FILES:
break
config.Write('RecentFilePath' + str(index), self.recent_files[index])
index += 1
def InsertRecentFileItem(self, filepath):
if filepath in self.recent_files:
self.recent_files.remove(filepath)
self.recent_files.insert(0,filepath)
if len(self.recent_files) > self.MAX_RECENT_FILES:
self.recent_files.pop()
def RegisterMessageBoxCallback(self):
cad.RegisterMessageBoxCallback(OnMessageBox)
def RegisterHideableWindow(self, w, bitmap_path = None):
if bitmap_path == None:
bitmap_path = self.bitmap_path
self.hideable_windows[w] = bitmap_path
def RemoveHideableWindow(self, w):
del self.hideable_windows[w]
def InitCad(self):
self.input_mode_object = self.select_mode
cad.SetResFolder(pycad_dir)
def EndHistory(self):
cad.EndHistory()
# to do, update the undo and redo buttons
#self.undo_button.ChangeBitmap()
def SplitSketch(self, object):
sketch = object
sketch.__class__ = cad.Sketch
new_sketches = sketch.Split()
cad.StartHistory('Split Sketch')
cad.DeleteUndoably(object)
for sketch in new_sketches:
cad.AddUndoably(sketch, object.GetOwner(), None)
self.EndHistory()
def FitArcs(self, object):
value = self.InputLength('Set tolerance for Fit Arcs', 'tolerance', geom.get_accuracy())
if value != None:
geom.set_accuracy(value)
sketch = object
sketch.__class__ = cad.Sketch
curve = sketch.GetCurve()
curve.FitArcs()
cad.StartHistory('Fit Arcs')
cad.DeleteUndoably(object)
cad.AddUndoably(cad.NewSketchFromCurve(curve))
self.EndHistory()
def CopyUndoably(self, object, copy_with_new_data):
copy_undoable = CopyObjectUndoable(object, copy_with_new_data)
cad.PyIncref(copy_undoable)
cad.DoUndoable(copy_undoable)
def CopyUndoablyWithChildren(self, object, copy_with_new_data):
copy_undoable = CopyObjectUndoable(object, copy_with_new_data, copy_children=True)
cad.PyIncref(copy_undoable)
cad.DoUndoable(copy_undoable)
def EditUndoably(self, object):
if object.HasEdit() == False:
return
copy_object = object.MakeACopy()
copy_object.SetID(object.GetID())
if copy_object:
if copy_object.Edit():
self.CopyUndoably(object, copy_object)
#cad.CopyUndoably(object, copy_object)
def SplitStlAtZ(self):
p = self.PickPosition('Pick point at z height')
if p == None:
return
temp_file = wx.StandardPaths.Get().GetTempDir() + '/split.stl'
self.object.WriteSTL(0.001, temp_file)
stl = geom.Stl(temp_file)
new_stl = stl.SplitAtZ(p.z)
if new_stl != None:
stl.WriteStl(temp_file)
cad.Import(temp_file)
new_stl.WriteStl(temp_file)
cad.Import(temp_file)
def SplitStl(self):
temp_file = wx.StandardPaths.Get().GetTempDir() + '/split.stl'
self.object.WriteSTL(0.001, temp_file)
stl = geom.Stl(temp_file)
new_stls = stl.Split()
if len(new_stls) > 1:
print('len(new_stls) = ' + str(len(new_stls)))
cad.StartHistory('Split STL')
cad.DeleteUndoably(self.object)
for stl in new_stls:
stl.WriteStl(temp_file)
cad.Import(temp_file)
cad.EndHistory()
def MakeGearSketch(self, object):
pts = object.GetPoints(0.1)
if len(pts)>1:
s = cad.NewSketch()
first_point = None
prev_point = None
for p in pts:
p3d = geom.Point3D(p.x, p.y, 0)
if first_point == None:
first_point = geom.Point3D(p3d)
else:
s.Add(cad.NewLine(prev_point, p3d))
prev_point = p3d
s.Add(cad.NewLine(prev_point, first_point))
cad.AddUndoably(s)
return s
def GetObjectTools(self, object, control_pressed, from_tree_canvas = False):
tools = []
self.object = object
type = self.object.GetType()
if type == cad.OBJECT_TYPE_SKETCH:
if self.object.GetNumChildren() > 1:
tools.append(ContextTool.CADObjectContextTool(object, "Split Sketch", "splitsketch", self.SplitSketch))
tools.append(ContextTool.CADObjectContextTool(object, "Fit Arcs", "fitarcs", self.FitArcs))
tools.append(ContextTool.CADObjectContextTool(object, "Offset", "offset", self.OffsetSketch))
tools.append(ContextTool.CADObjectContextTool(object, "Measure", "measure", self.MeasureSketch))
if type == cad.OBJECT_TYPE_STL_SOLID:
tools.append(ContextTool.CADContextTool("Split at Z", "splitsketch", self.SplitStlAtZ))
tools.append(ContextTool.CADContextTool("Split", "splitsketch", self.SplitStl))
if type == Gear.type:
tools.append(ContextTool.CADObjectContextTool(object, "Make Sketch", "lines", self.MakeGearSketch))
if len(tools)>0:
tools.append(None) # a separator
if not from_tree_canvas: # tree window doesn't need a "Select" menu item, because right clicking will have selected the object anyway
tools.append(ContextTool.SelectTool(object, control_pressed))
if object.HasEdit():
tools.append(ContextTool.EditTool(object))
if object.CanBeDeleted():
tools.append(ContextTool.DeleteTool(object))
if self.IsPasteReady():
tools.append(ContextTool.CADObjectContextTool(object, "Paste Into", 'paste', self.OnPasteInto))
return tools
def GetTools(self, x, y, control_pressed):
self.frame.graphics_canvas.SetCurrent(self.frame.graphics_canvas.context)# should be somewhere else, but for now fixes a problem
# get object tools
objects = cad.ObjectsUnderWindow(cad.IRect(x, y), False, True, self.select_mode.filter, False)
global tools
tools = []
make_container = len(objects)>1
for object in objects:
object_tools = self.GetObjectTools(object, control_pressed)
if make_container:
tool_list = ContextTool.ObjectToolList(object)
tool_list.tools += object_tools
tools.append(tool_list)
else:
tools.append(ContextTool.ObjectTitleTool(object))# object name with icon
tools += object_tools
return tools
def GetSelectionFilterTools(self, filter):
# get tools for the selection given types found in selection
tools = []
if filter.Size() > 0:
tools.append(ContextTool.CADContextTool('Delete Marked Items', 'delete', self.DeleteMarkedList))
if self.SketchChildTypeInFilter(filter):
tools.append(ContextTool.CADContextTool('Make to Sketch', 'makesketch', self.MakeToSketch))
if filter.IsTypeInFilter(cad.OBJECT_TYPE_SKETCH):
# check number of sketches
sketch_count = 0
for obj in cad.GetSelectedObjects():
if obj.GetType() == cad.OBJECT_TYPE_SKETCH:
sketch_count += 1
if sketch_count >= 2:
tools.append(ContextTool.CADContextTool('Combine sketches', 'sketchjoin', cad.CombineSelectedSketches))
tools.append(ContextTool.CADContextTool('Unite sketches', 'sketchunite', self.UniteSelectedSketches))
tools.append(ContextTool.CADContextTool('Cut sketches', 'sketchcut', self.CutSelectedSketches))
tools.append(ContextTool.CADContextTool('Intersect sketches', 'sketchintersect', self.IntersectSelectedSketches))
break
first_coord_sys = None
second_coord_sys = None
for object in cad.GetSelectedObjects():
if object.GetType() == cad.OBJECT_TYPE_COORD_SYS:
if first_coord_sys == None: first_coord_sys = object
elif second_coord_sys == None: second_coord_sys = object
return tools
def GetSelectionTools(self):
return self.GetSelectionFilterTools(cad.GetSelectionTypes())
def SketchChildTypeInFilter(self, filter):
if filter.IsTypeInFilter(cad.OBJECT_TYPE_SKETCH_LINE): return True
if filter.IsTypeInFilter(cad.OBJECT_TYPE_SKETCH_ARC): return True
return False
def RotateSelected(self):
if cad.GetNumSelected()>0:
selbox = geom.Box3D()
for object in cad.GetSelectedObjects():
box = object.GetBox()
selbox.InsertBox(box)
centre = selbox.Center()
draw_mat = cad.GetDrawMatrix(True)
inv_draw_mat = draw_mat.Inverse()
tr = geom.Matrix()
tr.Translate(-centre)
tr.Multiply(inv_draw_mat)
tr.RotateAxis(0.785398163397448309, geom.Point3D(0,0,1))
tr.Multiply(draw_mat)
tr.Translate(centre)
cad.StartHistory('Rotate Selected')
for object in cad.GetSelectedObjects():
cad.TransformUndoably(object, tr)
cad.EndHistory()
def DeleteMarkedList(self):
to_delete = []
for object in cad.GetSelectedObjects():
if object.CanBeDeleted():
to_delete.append(object)
cad.DeleteObjectsUndoably(to_delete)
def MakeToSketch(self):
objects_to_delete = []
sketch = cad.NewSketch()
for object in cad.GetSelectedObjects():
t = object.GetType()
if object.CanAddTo(sketch):
new_object = object.MakeACopy()
objects_to_delete.append(object)
sketch.Add(new_object)
cad.StartHistory('Make To Sketch')
cad.AddUndoably(sketch)
cad.DeleteObjectsUndoably(objects_to_delete)
self.EndHistory()
def OffsetSketch(self, object):
config = HeeksConfig()
value = config.ReadFloat('SketchOffsetValue', 1.0)
value = self.InputLength('Offset Sketch', 'Offset Value +ve for Outwards -ve for Inwards', value)
if value == None: return
config.WriteFloat('SketchOffsetValue', value)
sketch = object
sketch.__class__ = cad.Sketch
area = sketch.GetArea()
area.Reorder()
geom.set_fitarcs(False)
area.Offset(-value)
new_object = cad.NewSketchFromArea(area)
cad.AddUndoably(new_object)
cad.ClearSelection()
cad.Select(new_object)
def SketchOperation(self, mode):
objects_to_delete = []
area = None
for object in cad.GetSelectedObjects():
t = object.GetType()
if t == cad.OBJECT_TYPE_SKETCH:
objects_to_delete.append(object)
sketch = object
sketch.__class__ = cad.Sketch
a = sketch.GetArea()
if area == None:
area = a
else:
if mode == SKETCH_OP_UNION:
area.Union(a)
elif mode == SKETCH_OP_SUBTRACT:
area.Subtract(a)
elif mode == SKETCH_OP_INTERSECT:
area.Intersect(a)
if area != None:
cad.StartHistory('Sketch Operation')
sketch = cad.NewSketchFromArea(area)
cad.AddUndoably(sketch)
cad.DeleteObjectsUndoably(objects_to_delete)
self.EndHistory()
def UniteSelectedSketches(self):
self.SketchOperation(SKETCH_OP_UNION)
def CutSelectedSketches(self):
self.SketchOperation(SKETCH_OP_SUBTRACT)
def IntersectSelectedSketches(self):
self.SketchOperation(SKETCH_OP_INTERSECT)
def MeasureSketch(self, object):
sketch = object
sketch.__class__ = cad.Sketch
area = sketch.GetArea()
area.Reorder()
s = 'num curves = ' + str(area.NumCurves()) + '\ntotal perimeter = '
p = 0.0
for curve in area.GetCurves():
p += curve.Perim()
s += str(p)
wx.MessageBox(s)
def DrawFront(self):
if not self.render_on_front_done:
self.FrontRender()
self.render_on_front_done = True
def EndDrawFront(self):
if self.render_on_front_done:
self.FrontRender()
self.render_on_front_done = False
def FrontRender(self):
self.GetViewport().SetView()
self.GetViewport().SetXOR()
self.input_mode_object.OnFrontRender()
self.GetViewport().EndXOR()
def GetInputModeTools(self):
return self.input_mode_object.GetTools()
def SetMenuItemBitmap(self, item, tool):
bitmap_path = tool.BitmapPath()
if bitmap_path:
image = wx.Image(bitmap_path)
image.Rescale(tool.BitmapSize(), tool.BitmapSize())
item.SetBitmap(wx.Bitmap(image))
def AddToolToListAndMenu(self, tool, menu):
if tool == None:
menu.AppendSeparator()
else:
title = tool.GetTitle()
if title == '':
print('Empty title, tool = ' + str(tool))
title = 'Empty' # error if no title
if tool.IsAToolList():
menu2 = wx.Menu()
for tool2 in tool.GetTools():
self.AddToolToListAndMenu(tool2, menu2)
item = wx.MenuItem(menu, wx.ID_ANY, title)
item.SetSubMenu(menu2)
self.SetMenuItemBitmap(item, tool)
menu.Append(item)
else:
item = wx.MenuItem(menu, wx.ID_ANY, title)
self.SetMenuItemBitmap(item, tool)
self.Bind(wx.EVT_MENU, tool.Run, menu.Append(item))
if not tool.IsEnabled():
menu.Enable(item.GetId(), False)
def GetDropDownTools(self, x, y, control_pressed):
tools = self.GetTools(x, y, control_pressed)
# add a separator
if len(tools) > 0 and tools[-1] != None:
tools.append(None) # separator
# add selection tools
tools += self.GetSelectionTools()
return tools
def DoDropDownMenu(self, wnd, x, y, control_pressed):
tools = self.GetDropDownTools(x, y, control_pressed)
menu = wx.Menu()
for tool in tools:
self.AddToolToListAndMenu(tool, menu)
wnd.PopupMenu(menu, wx.Point(x, y))
def RestoreDefaults(self):
config = HeeksConfig()
config.DeleteAll()
self.settings_restored = True
def StartPickObjects(self, str, filter, just_one):
global save_filter_for_StartPickObjects
global save_just_one_for_EndPickObjects
global save_mode_for_EndPickObjects
save_mode_for_EndPickObjects = self.input_mode_object
self.select_mode.prompt = str
save_just_one_for_EndPickObjects = self.select_mode.just_one
self.select_mode.just_one = just_one
self.SetInputMode(self.select_mode)
save_filter_for_StartPickObjects = self.select_mode.filter
self.select_mode.filter = filter
def EndPickObjects(self):
global save_filter_for_StartPickObjects
global save_just_one_for_EndPickObjects
global save_mode_for_EndPickObjects
self.select_mode.filter = save_filter_for_StartPickObjects
self.select_mode.prompt = ''
self.select_mode.just_one = save_just_one_for_EndPickObjects
self.SetInputMode(save_mode_for_EndPickObjects)
def PickObjects(self, str, filter = cad.Filter(), just_one = False):
if self.inMainLoop:
wx.MessageBox('recursive call to PickObjects')
return
if isinstance(filter,int):
filter_int = filter
filter = cad.Filter()
filter.AddType(filter_int)
self.StartPickObjects(str, filter, just_one)
self.OnRun()
return self.EndPickObjects()
def OnRun(self):
self.inMainLoop = True
wx.App.OnRun(self)
self.inMainLoop = False
def PickPosition(self, title):
save_mode = self.input_mode_object
self.digitizing.prompt = title
self.SetInputMode(self.digitizing)
self.OnRun()
return_point = None
if self.digitizing.digitized_point.type != cad.DigitizeType.DIGITIZE_NO_ITEM_TYPE:
import geom
return_point = geom.Point3D(self.digitizing.digitized_point.point)
self.SetInputMode(save_mode);
return return_point
def GetViewport(self):
return self.frame.graphics_canvas.viewport
def IsSolidApp(self):
return False
def AddExtraWindows(self, frame):
pass
def GetOptions(self):
properties = []
properties.append(PyProperty("End Beyond Full Profile", 'end_beyond_full_profile', self))
return properties
def CheckForNumberOrMore(self, min_num, types, msg, caption):
num_found = 0
for object in cad.GetSelectedObjects():
for type in types:
if object.GetType() == type:
num_found += 1
break
if num_found < min_num:
filter = cad.Filter()
for t in types: filter.AddType(t)
objects = self.PickObjects(msg, filter, False)
if cad.GetNumSelected() < min_num:
wx.MessageBox(msg)
return False
return True
def InputLength(self, prompt, name, value):
dlg = HDialog('Input')
sizerMain = wx.BoxSizer(wx.VERTICAL)
static_label = wx.StaticText(dlg, label = prompt)
sizerMain.Add( static_label, 0, wx.ALL, wx.ALIGN_LEFT, control_border )
value_control = LengthCtrl(dlg)
value_control.SetValue(value)
dlg.AddLabelAndControl( sizerMain, name, value_control )
dlg.MakeOkAndCancel( wx.HORIZONTAL ).AddToSizer( sizerMain )
dlg.SetSizer( sizerMain )
sizerMain.SetSizeHints(dlg)
sizerMain.Fit(dlg)
value_control.SetFocus()
if dlg.ShowModal() == wx.ID_OK:
return value_control.GetValue()
return None
def OnKeyDown(self, e):
k = e.GetKeyCode()
if k == wx.WXK_ESCAPE and self.frame.IsFullScreen():
self.ShowFullScreen(False)
return True
else:
key_code = Key.KeyCodeFromWx(e)
if wx.GetApp().input_mode_object.OnKeyDown(key_code):
return True
if k == wx.WXK_DELETE:
if cad.GetNumSelected() > 0:
self.DeleteMarkedList()
elif k == wx.WXK_RETURN:
if self.inMainLoop:
self.ExitMainLoop()
self.Repaint()
elif k == ord('Z'):
if e.ControlDown():
if e.ShiftDown():
self.OnRedo(e)
else:
self.OnUndo(e)
elif k == ord('X'):
if e.ControlDown():
self.OnCut(e)
elif k == ord('C'):
if e.ControlDown():
self.OnCopy(e)
elif k == ord('V'):
if e.ControlDown():
self.OnPaste(e)
elif k == ord('R'):
self.RotateSelected()
else:
return False
return True
def Repaint(self):
self.frame.graphics_canvas.Update()
self.frame.graphics_canvas.Refresh()
def AddExtraRibbonPages(self, ribbon):
pass
def AddOptionsRibbonPanels(self, ribbon):
panel = RB.RibbonPanel(ribbon.options_page, wx.ID_ANY, 'View', ribbon.Image('mag'))
self.view_toolbar = RB.RibbonButtonBar(panel)
from Ribbon import ScreenTextButton
from Ribbon import ModeButton
ScreenTextButton().AddToToolbar(self.view_toolbar)
ModeButton( cad.GetRotateUpright, cad.SetRotateUpright, 'rotate upright', 'rotate free', "RotateUpright", 'Rotate Upright', 'Rotate Free').AddToToolbar(self.view_toolbar)
ModeButton( cad.GetAntialiasing, cad.SetAntialiasing, 'smoothed', 'pixelated', "Antialiasing", 'Smoothed', 'Pixelated', 'Lines are drawn with antialiasing', 'Lines are drawn withput antialiasing').AddToToolbar(self.view_toolbar)
ModeButton( cad.GetShowDatum, cad.SetShowDatum, 'showdatum', 'hidedatum', "ShowDatum", 'Hide Datum', 'Show Datum', 'Showing the Datum', 'Not Showing the Datum').AddToToolbar(self.view_toolbar)
panel = RB.RibbonPanel(ribbon.options_page, wx.ID_ANY, 'View Colors', ribbon.Image('mag'))
toolbar = RB.RibbonButtonBar(panel)
from Ribbon import BackgroundColorButton
BackgroundColorButton('Background Color Top', 'Edit top background color').AddToToolbar(toolbar)
BackgroundColorButton('Background Color Bottom', 'Edit bottom background color').AddToToolbar(toolbar)
def OnAntialiasing(self, event):
cad.SetAntialiasing(event.IsChecked())
config = HeeksConfig()
config.WriteBool('Antialiasing', cad.GetAntialiasing())
self.frame.graphics_canvas.Refresh()
def OnNew(self, e):
res = self.CheckForModifiedDoc()
if res != wx.CANCEL:
cad.Reset()
self.SetInputMode(self.select_mode)
self.OnNewOrOpen(False)
cad.ClearHistory()
cad.SetLikeNewFile()
self.filepath = None
self.frame.SetFrameTitle()
def SaveProject(self, force_dialog = False):
if self.GetProjectFileName().IsOk():
self.SaveFile()
def CheckForModifiedDoc(self, force_dialog = False):
# returns wxCANCEL if not OK to continue opening file
if cad.IsModified():
str = 'Save changes to file ' + (self.filepath if self.filepath else 'Untitled')
res = wx.MessageBox(str, wx.MessageBoxCaptionStr, wx.CANCEL | wx.YES_NO | wx.CENTER | wx.ICON_WARNING)
if res == wx.CANCEL or res == wx.NO: return res
if res == wx.YES:
return self.OnSave(None)
return wx.OK
def DoFileOpenViewMag(self):
self.frame.graphics_canvas.viewport.OnMagExtents(True, 25)
def OnOpenFilepath(self, filepath, check = True):
if check:
res = self.CheckForModifiedDoc()
else:
res = wx.OK
if res != wx.CANCEL:
# self.OnBeforeNewOrOpen(True, res)
cad.Reset()
self.SetInputMode(self.select_mode)
if cad.OpenFile(filepath):
self.DoFileOpenViewMag()
self.OnNewOrOpen(True)
cad.ClearHistory()
cad.SetLikeNewFile()
self.filepath = filepath
self.frame.SetFrameTitle()
self.InsertRecentFileItem(filepath)
self.WriteRecentFiles()
return True
else:
wx.MessageBox('Invalid file type chosen expecting .heeks')
return True
def OnOpen(self, e):
dialog = wx.FileDialog(self.frame, 'Open File', self.GetDefaultDir(), '', HEEKS_WILDCARD_STRING)
dialog.CenterOnParent()
if dialog.ShowModal() == wx.ID_OK:
self.OnOpenFilepath(dialog.GetPath())
def OnOpenRecent(self, e):
file_path = self.recent_files[e.GetId() - self.ID_RECENT_FIRST]
self.OnOpenFilepath(file_path)
def OnUpdateOpenRecent(self, e):
size = self.recent_files_menu.GetMenuItemCount()
menu_items = []
for i in range(0, size):
menu_items.append(self.recent_files_menu.FindItemByPosition(i))
for menu_item in menu_items:
self.recent_files_menu.Delete(menu_item)
recent_id = self.ID_RECENT_FIRST
for filepath in self.recent_files:
self.recent_files_menu.Append(recent_id, filepath)
recent_id += 1
def OnSaveFilepath(self, filepath):
# save .heeks file
# filepath must end in .heeks
if (filepath[-6:].lower()) != '.heeks':
filepath += '.heeks'
if cad.SaveFile(filepath):
self.filepath = filepath
cad.SetLikeNewFile()
self.frame.SetFrameTitle()
self.InsertRecentFileItem(filepath)
self.WriteRecentFiles()
return wx.ID_OK
return wx.ID_CANCEL
def OnSave(self, e):
if self.filepath:
return self.OnSaveFilepath(self.filepath)
self.OnSaveAs(e)
def OnUpdateSave(self, e):
e.Enable(cad.IsModified())
def OnSaveAs(self, e):
if self.filepath:
default_directory = ''
default_filepath = self.filepath
else:
default_directory = self.GetDefaultDir()
default_filepath = self.GetDefaultFilename()
dialog = wx.FileDialog(self.frame, 'Save File', default_directory, default_filepath, HEEKS_WILDCARD_STRING, wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT)
dialog.SetFilterIndex(0)
dialog.CenterOnParent()
if dialog.ShowModal() != wx.ID_CANCEL:
self.OnSaveFilepath(dialog.GetPath())
def GetWildcardString(self, import_not_export):
wild_card_string1 = 'Known Files |'
wild_card_string2 = ''
first = True
for suffix_list, description in (self.import_file_types if import_not_export else self.export_file_types):
wild_card_string2 += '|' + description + ' ('
imageExtStr = ''
imageExtStr2 = ''
for suffix in suffix_list:
if not first:
wild_card_string1 += ';'
wild_card_string1 += '*.' + suffix + ';'
wild_card_string1 += '*.' + suffix.upper()
if imageExtStr:
imageExtStr += ' '
imageExtStr2 += ';'
imageExtStr += ('*.' + suffix)
imageExtStr2 += ('*.' + suffix + ';')
imageExtStr2 += ('*.' + suffix.upper())
wild_card_string2 += (imageExtStr + ')|' + imageExtStr2)
first = False
return wild_card_string1 + wild_card_string2
def OnImport(self, e):
config = HeeksConfig()
default_directory = config.Read('ImportDirectory', self.GetDefaultDir())
dialog = wx.FileDialog(self.frame, 'Import File', default_directory, '', self.GetWildcardString(True))
dialog.CenterOnParent()
if dialog.ShowModal() == wx.ID_OK:
filepath = dialog.GetPath()
suffix = self.GetPathSuffix(filepath)
if suffix == 'wrl':
import Wrl
cad.StartHistory('Import Wrl')
res = Wrl.Import(filepath)
cad.EndHistory()
else:
res = cad.Import(filepath)
if res:
self.DoFileOpenViewMag()
if self.filepath == None:
dot = filepath.rfind('.')
if dot != -1:
self.filepath = filepath[:dot+1] + 'heeks'
self.frame.SetFrameTitle()
config.Write('ImportDirectory', dialog.GetDirectory())
self.Repaint()
def GetPathSuffix(self, path):
dot = path.rfind('.')
if dot == -1:
return ''
return path[dot+1:].lower()
def RegisterImportFileTypes(self, suffix_list, description, ImportCallback):
self.import_file_types.append((suffix_list, description))
for suffix in suffix_list:
cad.RegisterImportFileType(suffix, ImportCallback)
def RegisterExportFileTypes(self, suffix_list, description, ExportCallback):
self.export_file_types.append((suffix_list, description))
for suffix in suffix_list:
cad.RegisterExportFileType(suffix, ExportCallback)
def OnExport(self, e):
config = HeeksConfig()
default_directory = config.Read('ExportDirectory', self.GetDefaultDir())
dialog = wx.FileDialog(self.frame, 'Export File', default_directory, '', self.GetWildcardString(False), wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT)
dialog.CenterOnParent()
if dialog.ShowModal() == wx.ID_OK:
path = dialog.GetPath()
suffix = self.GetPathSuffix(path)
if suffix == 'svg':
import Svg
Svg.Export(path)
elif suffix == '3mf':