/
shape2dview.py
291 lines (267 loc) · 13.7 KB
/
shape2dview.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
# ***************************************************************************
# * Copyright (c) 2009, 2010 Yorik van Havre <yorik@uncreated.net> *
# * Copyright (c) 2009, 2010 Ken Cline <cline@frii.com> *
# * Copyright (c) 2020 FreeCAD Developers *
# * *
# * 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 *
# * *
# ***************************************************************************
"""Provides the object code for the Shape2dView object."""
## @package shape2dview
# \ingroup draftobjects
# \brief Provides the object code for the Shape2dView object.
## \addtogroup draftobjects
# @{
from PySide.QtCore import QT_TRANSLATE_NOOP
import FreeCAD as App
import DraftVecUtils
import draftutils.utils as utils
import draftutils.gui_utils as gui_utils
import draftutils.groups as groups
from draftutils.translate import translate
from draftobjects.base import DraftObject
class Shape2DView(DraftObject):
"""The Shape2DView object"""
def __init__(self,obj):
_tip = QT_TRANSLATE_NOOP("App::Property",
"The base object this 2D view must represent")
obj.addProperty("App::PropertyLink", "Base",
"Draft", _tip)
_tip = QT_TRANSLATE_NOOP("App::Property",
"The projection vector of this object")
obj.addProperty("App::PropertyVector", "Projection",
"Draft", _tip)
_tip = QT_TRANSLATE_NOOP("App::Property",
"The way the viewed object must be projected")
obj.addProperty("App::PropertyEnumeration", "ProjectionMode",
"Draft", _tip)
_tip = QT_TRANSLATE_NOOP("App::Property",
"The indices of the faces to be projected in Individual Faces mode")
obj.addProperty("App::PropertyIntegerList", "FaceNumbers",
"Draft", _tip)
_tip = QT_TRANSLATE_NOOP("App::Property",
"Show hidden lines")
obj.addProperty("App::PropertyBool", "HiddenLines",
"Draft", _tip)
_tip = QT_TRANSLATE_NOOP("App::Property",
"Fuse wall and structure objects of same type and material")
obj.addProperty("App::PropertyBool", "FuseArch",
"Draft", _tip)
_tip = QT_TRANSLATE_NOOP("App::Property",
"Tessellate Ellipses and B-splines into line segments")
obj.addProperty("App::PropertyBool", "Tessellation",
"Draft", _tip)
_tip = QT_TRANSLATE_NOOP("App::Property",
"For Cutlines and Cutfaces modes, \
this leaves the faces at the cut location")
obj.addProperty("App::PropertyBool", "InPlace",
"Draft", _tip)
_tip = QT_TRANSLATE_NOOP("App::Property",
"Length of line segments if tessellating Ellipses or B-splines \
into line segments")
obj.addProperty("App::PropertyFloat", "SegmentLength",
"Draft", _tip)
_tip = QT_TRANSLATE_NOOP("App::Property",
"If this is True, this object will be recomputed only if it is \
visible")
obj.addProperty("App::PropertyBool", "VisibleOnly",
"Draft", _tip)
obj.Projection = App.Vector(0,0,1)
obj.ProjectionMode = ["Solid", "Individual Faces",
"Cutlines", "Cutfaces","Solid faces"]
obj.HiddenLines = False
obj.Tessellation = False
obj.VisibleOnly = False
obj.InPlace = True
obj.SegmentLength = .05
super(Shape2DView, self).__init__(obj, "Shape2DView")
def getProjected(self,obj,shape,direction):
"returns projected edges from a shape and a direction"
import Part, Drawing, DraftGeomUtils
edges = []
_groups = Drawing.projectEx(shape, direction)
for g in _groups[0:5]:
if g:
edges.append(g)
if hasattr(obj,"HiddenLines"):
if obj.HiddenLines:
for g in _groups[5:]:
edges.append(g)
#return Part.makeCompound(edges)
if hasattr(obj,"Tessellation") and obj.Tessellation:
return DraftGeomUtils.cleanProjection(Part.makeCompound(edges),
obj.Tessellation,
obj.SegmentLength)
else:
return Part.makeCompound(edges)
#return DraftGeomUtils.cleanProjection(Part.makeCompound(edges))
def execute(self,obj):
if hasattr(obj,"VisibleOnly"):
if obj.VisibleOnly:
if obj.ViewObject:
if obj.ViewObject.Visibility == False:
return False
import Part, DraftGeomUtils
obj.positionBySupport()
pl = obj.Placement
if obj.Base:
if utils.get_type(obj.Base) in ["BuildingPart","SectionPlane"]:
objs = []
if utils.get_type(obj.Base) == "SectionPlane":
objs = obj.Base.Objects
cutplane = obj.Base.Shape
else:
objs = obj.Base.Group
cutplane = Part.makePlane(1000, 1000, App.Vector(-500, -500, 0))
m = 1
if obj.Base.ViewObject and hasattr(obj.Base.ViewObject,"CutMargin"):
m = obj.Base.ViewObject.CutMargin.Value
cutplane.translate(App.Vector(0,0,m))
cutplane.Placement = cutplane.Placement.multiply(obj.Base.Placement)
if objs:
onlysolids = True
if hasattr(obj.Base,"OnlySolids"):
onlysolids = obj.Base.OnlySolids
import Arch, Part, Drawing
objs = groups.get_group_contents(objs, walls=True)
objs = gui_utils.remove_hidden(objs)
shapes = []
if hasattr(obj,"FuseArch") and obj.FuseArch:
shtypes = {}
for o in objs:
if utils.get_type(o) in ["Wall","Structure"]:
if onlysolids:
shtypes.setdefault(o.Material.Name
if (hasattr(o,"Material") and o.Material)
else "None",[]).extend(o.Shape.Solids)
else:
shtypes.setdefault(o.Material.Name
if (hasattr(o,"Material") and o.Material)
else "None",[]).append(o.Shape.copy())
elif hasattr(o,'Shape'):
if onlysolids:
shapes.extend(o.Shape.Solids)
else:
shapes.append(o.Shape.copy())
for k, v in shtypes.items():
v1 = v.pop()
if v:
v1 = v1.multiFuse(v)
v1 = v1.removeSplitter()
if v1.Solids:
shapes.extend(v1.Solids)
else:
print("Shape2DView: Fusing Arch objects produced non-solid results")
shapes.append(v1)
else:
for o in objs:
if hasattr(o,'Shape'):
if onlysolids:
shapes.extend(o.Shape.Solids)
else:
shapes.append(o.Shape.copy())
clip = False
if hasattr(obj.Base,"Clip"):
clip = obj.Base.Clip
cutp, cutv, iv = Arch.getCutVolume(cutplane, shapes, clip)
cuts = []
opl = App.Placement(obj.Base.Placement)
proj = opl.Rotation.multVec(App.Vector(0, 0, 1))
if obj.ProjectionMode in ["Solid","Solid faces"]:
shapes_to_cut = shapes
if obj.ProjectionMode == "Solid faces":
shapes_to_cut = []
for s in shapes:
shapes_to_cut.extend(s.Faces)
for sh in shapes_to_cut:
if cutv:
if sh.Volume < 0:
sh.reverse()
#if cutv.BoundBox.intersect(sh.BoundBox):
# c = sh.cut(cutv)
#else:
# c = sh.copy()
c = sh.cut(cutv)
if onlysolids:
cuts.extend(c.Solids)
else:
cuts.append(c)
else:
if onlysolids:
cuts.extend(sh.Solids)
else:
cuts.append(sh.copy())
comp = Part.makeCompound(cuts)
obj.Shape = self.getProjected(obj,comp,proj)
elif obj.ProjectionMode in ["Cutlines", "Cutfaces"]:
for sh in shapes:
if sh.Volume < 0:
sh.reverse()
c = sh.section(cutp)
faces = []
if (obj.ProjectionMode == "Cutfaces") and (sh.ShapeType == "Solid"):
if hasattr(obj,"InPlace"):
if not obj.InPlace:
c = self.getProjected(obj, c, proj)
wires = DraftGeomUtils.findWires(c.Edges)
for w in wires:
if w.isClosed():
faces.append(Part.Face(w))
if faces:
cuts.extend(faces)
else:
cuts.append(c)
comp = Part.makeCompound(cuts)
opl = App.Placement(obj.Base.Placement)
comp.Placement = opl.inverse()
if comp:
obj.Shape = comp
elif obj.Base.isDerivedFrom("App::DocumentObjectGroup"):
shapes = []
objs = groups.get_group_contents(obj.Base)
for o in objs:
if hasattr(o,'Shape'):
if o.Shape:
if not o.Shape.isNull():
shapes.append(o.Shape)
if shapes:
import Part
comp = Part.makeCompound(shapes)
obj.Shape = self.getProjected(obj,comp,obj.Projection)
elif hasattr(obj.Base,'Shape'):
if not DraftVecUtils.isNull(obj.Projection):
if obj.ProjectionMode == "Solid":
obj.Shape = self.getProjected(obj,obj.Base.Shape,obj.Projection)
elif obj.ProjectionMode == "Individual Faces":
import Part
if obj.FaceNumbers:
faces = []
for i in obj.FaceNumbers:
if len(obj.Base.Shape.Faces) > i:
faces.append(obj.Base.Shape.Faces[i])
views = []
for f in faces:
views.append(self.getProjected(obj,f,obj.Projection))
if views:
obj.Shape = Part.makeCompound(views)
else:
App.Console.PrintWarning(obj.ProjectionMode+" mode not implemented\n")
if not DraftGeomUtils.isNull(pl):
obj.Placement = pl
# Alias for compatibility with v0.18 and earlier
_Shape2DView = Shape2DView
## @}