forked from oddtopus/dodo
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpObservers.py
More file actions
121 lines (109 loc) · 4.41 KB
/
pObservers.py
File metadata and controls
121 lines (109 loc) · 4.41 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
# SPDX-License-Identifier: LGPL-3.0-or-later
__title__ = "frameTools toolbar"
__author__ = "oddtopus"
__url__ = "github.com/oddtopus/dodo"
__license__ = "LGPL 3"
# import FreeCAD modules
import FreeCAD
import FreeCADGui
import pCmd
from fObservers import frameObserverPrototype
from uCmd import arrow
pCmd.o1 = None
pCmd.port1 = None
pCmd.o2 = None
pCmd.port2 = None
pCmd.arrows1 = list()
pCmd.arrows2 = list()
translate = FreeCAD.Qt.translate
class arrow_insert(arrow):
def __init__(self, name, pype, portNr, scale=100):
self.stop = False
self.pype = pype
self.portDir = pCmd.portsDir(pype)[portNr]
self.portPos = pCmd.portsPos(pype)[portNr]
rot = FreeCAD.Rotation(FreeCAD.Vector(0, 0, 1), self.portDir.negative())
placement = FreeCAD.Placement(self.portPos, rot)
super(arrow_insert, self).__init__(
pl=placement, scale=[-scale, scale, scale / 5], offset=-scale, name=name
)
def pickAction(self, path=None, event=None, arg=None): # sample
if path:
for n in path:
if str(n.getName()) == self.name:
if self.name[:4] == "obj1":
pCmd.port1 = int(self.name[-1])
FreeCAD.Console.PrintMessage(
"Destination port changed to %i\n" % pCmd.port1
)
if FreeCADGui.Control.activeDialog():
pass # to understand how to refer to the dialog to update labels
elif self.name[:4] == "obj2":
pCmd.port2 = int(self.name[-1])
FreeCAD.Console.PrintMessage(
"%s %s joined at port %i to %s\n"
% (pCmd.o2.PType, pCmd.o2.Label, pCmd.port2, pCmd.o1.Label)
)
# move o2
if isinstance(pCmd.port1, int) and isinstance(pCmd.port2, int):
FreeCAD.activeDocument().openTransaction(
translate("Transaction", "Join")
)
pCmd.join(pCmd.o1, pCmd.port1, pCmd.o2, pCmd.port2)
FreeCAD.activeDocument().commitTransaction()
else:
FreeCAD.Console.PrintError("Port(s) not selected yet\n")
self.stop = True
if self.stop:
for a in pCmd.arrows2:
a.closeArrow()
pCmd.arrows2 = []
pCmd.o2 = None
pCmd.port2 = None
self.stop = False
class joinObserver(frameObserverPrototype):
"""
Mate together the Ports of different Pypes by clicking on
arrows in the scene-graph.
"""
def __init__(self):
super(joinObserver, self).__init__("Select pypes and ports")
self.o1 = pCmd.o1 = None
pCmd.port1 = None
self.o2 = pCmd.o2 = None
pCmd.port2 = None
pCmd.arrows1 = list()
pCmd.arrows2 = list()
# self.o1=pCmd.o1
# self.o2=pCmd.o2
FreeCADGui.Selection.clearSelection()
def goOut(self, info):
down = info["State"] == "DOWN"
k = info["Key"]
if k == "ESCAPE":
FreeCAD.Console.PrintMessage("I'm escaping.\n")
for arrow in pCmd.arrows1 + pCmd.arrows2:
arrow.closeArrow()
self.av.removeEventCallback("SoKeyboardEvent", self.stop)
FreeCADGui.Selection.removeObserver(self)
def addSelection(self, doc, obj, sub, pnt):
sel = FreeCADGui.Selection.getSelection()[0]
scale = (
min(sel.Shape.BoundBox.XLength, sel.Shape.BoundBox.YLength, sel.Shape.BoundBox.ZLength)
* 1.1
)
FreeCADGui.Selection.clearSelection()
if hasattr(sel, "PType") and not pCmd.o1:
pCmd.o1 = self.o1 = sel
for i in range(len(sel.Ports)):
name = "obj1_port" + str(i)
pCmd.arrows1.append(arrow_insert(name, sel, i, scale))
else: # if hasattr(sel,'PType') and not pCmd.o2:
for a in pCmd.arrows2:
a.closeArrow()
pCmd.o2 = self.o2 = sel
for i in range(len(sel.Ports)):
name = "obj2_port" + str(i)
a = arrow_insert(name, sel, i, scale)
a.color.rgb = 0, 0, 0.8
pCmd.arrows2.append(a)