-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathBimClone.py
202 lines (175 loc) · 8.12 KB
/
BimClone.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
# -*- coding: utf8 -*-
# ***************************************************************************
# * *
# * Copyright (c) 2017 Yorik van Havre <[email protected]> *
# * *
# * 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 *
# * *
# ***************************************************************************
"""This module contains FreeCAD commands for the BIM workbench"""
import os
import FreeCAD
from BimTranslateUtils import *
import DraftTools
class BIM_Copy(DraftTools.Move):
def __init__(self):
DraftTools.Move.__init__(self)
self.copymode = True
def GetResources(self):
return {
"Pixmap": os.path.join(os.path.dirname(__file__), "icons", "BIM_Copy.svg"),
"MenuText": QT_TRANSLATE_NOOP("BIM_Copy", "Copy"),
"ToolTip": QT_TRANSLATE_NOOP(
"BIM_Copy", "Copies selected objects to another location"
),
"Accel": "C,P",
}
class BIM_Clone(DraftTools.Draft_Clone):
def __init__(self):
DraftTools.Draft_Clone.__init__(self)
self.moveAfterCloning = True
def GetResources(self):
return {
"Pixmap": os.path.join(os.path.dirname(__file__), "icons", "BIM_Clone.svg"),
"MenuText": QT_TRANSLATE_NOOP("BIM_Clone", "Clone"),
"ToolTip": QT_TRANSLATE_NOOP(
"BIM_Clone", "Clones selected objects to another location"
),
"Accel": "C,L",
}
class BIM_ResetCloneColors:
def GetResources(self):
return {
"Pixmap": os.path.join(
os.path.dirname(__file__), "icons", "BIM_ResetCloneColors.svg"
),
"MenuText": QT_TRANSLATE_NOOP("BIM_ResetCloneColors", "Reset colors"),
"ToolTip": QT_TRANSLATE_NOOP(
"BIM_ResetCloneColors",
"Resets the colors of this object from its cloned original",
),
"Accel": "D,O",
}
def Activated(self):
import FreeCADGui
for obj in FreeCADGui.Selection.getSelection():
if hasattr(obj, "CloneOf") and obj.CloneOf:
obj.ViewObject.DiffuseColor = obj.CloneOf.ViewObject.DiffuseColor
class BIM_Unclone:
def GetResources(self):
return {
"Pixmap": os.path.join(
os.path.dirname(__file__), "icons", "BIM_Unclone.svg"
),
"MenuText": QT_TRANSLATE_NOOP("BIM_Unclone", "Unclone"),
"ToolTip": QT_TRANSLATE_NOOP(
"BIM_Unclone",
"Makes a selected clone object independent from its original",
),
}
def IsActive(self):
import FreeCADGui
if FreeCADGui.Selection.getSelection():
return True
else:
return False
def Activated(self):
import FreeCADGui
import Draft
# get selected object and face
sel = FreeCADGui.Selection.getSelection()
if len(sel) == 1:
# make this undoable
FreeCAD.ActiveDocument.openTransaction("Reextrude")
obj = sel[0]
# check that types are identical
if hasattr(obj, "CloneOf") and obj.CloneOf:
cloned = obj.CloneOf
placement = FreeCAD.Placement(obj.Placement)
if Draft.getType(obj) != Draft.getType(cloned):
# wrong type - we need to create a new object
newobj = getattr(Arch, "make" + Draft.getType(cloned))()
else:
newobj = obj
newobj.CloneOf = None
# copy properties over, except special ones
for prop in cloned.PropertiesList:
if not prop in [
"Objects",
"CloneOf",
"ExpressionEngine",
"HorizontalArea",
"Area",
"VerticalArea",
"PerimeterLength",
"Proxy",
"Shape",
]:
setattr(newobj, prop, getattr(cloned, prop))
FreeCAD.ActiveDocument.recompute()
newobj.Placement = cloned.Placement.multiply(placement)
# update/reset view properties too? no i think...
# for prop in cloned.ViewObject.PropertiesList:
# if not prop in ["Proxy"]:
# setattr(newobj.ViewObject,prop,getattr(cloned.ViewObject,prop))
# update objects relating to this one
for parent in obj.InList:
for prop in parent.PropertiesList:
if getattr(parent, prop) == obj:
setattr(parent, prop, newobj)
FreeCAD.Console.PrintMessage(
"Object "
+ parent.Label
+ "'s reference to this object has been updated\n"
)
elif isinstance(getattr(parent, prop), list) and (
obj in getattr(parent, prop)
):
if (prop == "Group") and hasattr(parent, "addObject"):
parent.addObject(newobj)
else:
g = getattr(parent, prop)
g.append(newobj)
setattr(parent, prop, g)
FreeCAD.Console.PrintMessage(
"Object "
+ parent.Label
+ "'s reference to this object has been updated\n"
)
# TODO treat PropertyLinkSub / PropertyLinkSubList DANGEROUS - toponaming
# remove old object if needed, and relabel new object
if newobj != obj:
name = obj.Name
label = obj.Label
from DraftGui import todo
FreeCAD.ActiveDocument.removeObject(name)
newobj.Label = label
# commit changes
FreeCAD.ActiveDocument.commitTransaction()
FreeCAD.ActiveDocument.recompute()
elif Draft.getType(obj) == "Clone":
FreeCAD.Console.PrintError(
translate("BIM", "Draft Clones are not supported yet!") + "\n"
)
else:
FreeCAD.Console.PrintError(
translate("BIM", "The selected object is not a clone") + "\n"
)
else:
FreeCAD.Console.PrintError(
translate("BIM", "Please select exactly one object") + "\n"
)