2022-12-15 13:15:05 -05:00
|
|
|
try:
|
|
|
|
del App.Flatpack
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
ad = Gui.ActiveDocument.Document
|
|
|
|
|
|
|
|
def selectionContainsAssembly():
|
|
|
|
sel = Gui.Selection.getSelection()
|
|
|
|
|
|
|
|
if not sel or not hasattr(sel[0], 'Type') or sel[0].Type != 'Assembly':
|
|
|
|
return False
|
|
|
|
|
|
|
|
return sel[0]
|
|
|
|
|
|
|
|
class FlatpackGroupViewProvider():
|
|
|
|
def __init__(self, group):
|
|
|
|
self.Proxy = group
|
|
|
|
|
|
|
|
class FlatpackWBTools():
|
2022-12-25 03:04:17 -05:00
|
|
|
def createDatumPlaneOnLink(self, part):
|
2022-12-15 13:15:05 -05:00
|
|
|
plane = part.newObject('PartDesign::Plane', 'DatumPlane')
|
|
|
|
|
|
|
|
return plane
|
|
|
|
|
|
|
|
def createGroup(self, asm):
|
2022-12-25 03:03:50 -05:00
|
|
|
#pack = ad.addObject('App::Part', 'Flatpack')
|
|
|
|
#pack.Type = 'Flatpack'
|
2022-12-15 13:15:05 -05:00
|
|
|
|
2022-12-25 03:03:50 -05:00
|
|
|
#FlatpackGroupViewProvider(pack)
|
2022-12-15 13:15:05 -05:00
|
|
|
|
|
|
|
#asm.Visibility = False
|
|
|
|
|
|
|
|
self.links = self.findLinks(asm)
|
|
|
|
|
2022-12-25 03:04:17 -05:00
|
|
|
assembly_bodies = []
|
|
|
|
|
2022-12-15 13:15:05 -05:00
|
|
|
for link in self.links:
|
2022-12-25 03:04:17 -05:00
|
|
|
if link.LinkedObject not in assembly_bodies:
|
|
|
|
assembly_bodies.append(link.LinkedObject)
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
for body in assembly_bodies:
|
|
|
|
sketch_planes = {}
|
|
|
|
sketch_pockets = {}
|
|
|
|
sketch_pads = {}
|
|
|
|
|
|
|
|
for lcs in self.findLCS(body.OutList):
|
|
|
|
base = lcs.Placement.Base
|
|
|
|
z = base.z
|
|
|
|
|
|
|
|
if sketch_planes.get(z) is None:
|
|
|
|
# create new plane to hold sketch
|
|
|
|
plane = self.createDatumPlaneOnLink(body)
|
|
|
|
plane.Visibility = False
|
|
|
|
plane.Placement.Base.z = z
|
|
|
|
|
|
|
|
sketch_planes[z] = plane
|
|
|
|
else:
|
|
|
|
plane = sketch_planes[z]
|
|
|
|
|
|
|
|
bb = body.Shape.BoundBox
|
|
|
|
if self.insideBB(bb, lcs.Placement.Base):
|
|
|
|
sketch_book = sketch_pockets
|
|
|
|
sketch_label = 'Inside'
|
|
|
|
else:
|
|
|
|
sketch_book = sketch_pads
|
|
|
|
sketch_label = 'Outside'
|
|
|
|
|
|
|
|
if sketch_book.get(z) is None:
|
|
|
|
sketch = self.createSketch(body, plane)
|
|
|
|
sketch.Visibility = False
|
|
|
|
sketch.Label = sketch_label
|
|
|
|
sketch_book[z] = sketch
|
|
|
|
else:
|
|
|
|
sketch = sketch_book[z]
|
|
|
|
|
|
|
|
tab_length = abs(bb.ZMin) + bb.ZMax
|
|
|
|
|
|
|
|
sketch.addGeometry(Part.LineSegment(App.Vector(base.x-10, base.y - (tab_length / 2), 0), App.Vector(base.x+10, base.y - (tab_length / 2), 0)), False)
|
|
|
|
sketch.addGeometry(Part.LineSegment(App.Vector(base.x+10, base.y - (tab_length / 2), 0), App.Vector(base.x+10, base.y + tab_length / 2, 0)), False)
|
|
|
|
sketch.addGeometry(Part.LineSegment(App.Vector(base.x+10, base.y + tab_length / 2, 0), App.Vector(base.x-10, base.y + tab_length / 2, 0)), False)
|
|
|
|
sketch.addGeometry(Part.LineSegment(App.Vector(base.x-10, base.y + tab_length / 2, 0), App.Vector(base.x-10, base.y - (tab_length / 2), 0)), False)
|
|
|
|
|
|
|
|
for z in sketch_pads:
|
|
|
|
sketch = sketch_pads[z]
|
2022-12-15 13:15:05 -05:00
|
|
|
pad = link.LinkedObject.newObject('PartDesign::Pad', 'Pad')
|
|
|
|
pad.Profile = sketch
|
2022-12-25 03:04:17 -05:00
|
|
|
pad.Length = tab_length
|
|
|
|
pad.Midplane = 1
|
|
|
|
pad.Refine = 1
|
|
|
|
|
|
|
|
for z in sketch_pockets:
|
|
|
|
sketch = sketch_pockets[z]
|
|
|
|
pad = link.LinkedObject.newObject('PartDesign::Pocket', 'Pocket')
|
|
|
|
pad.Profile = sketch
|
|
|
|
pad.Length = tab_length
|
2022-12-15 13:15:05 -05:00
|
|
|
pad.Midplane = 1
|
|
|
|
pad.Refine = 1
|
|
|
|
|
|
|
|
def createSketch(self, part, attached_to):
|
|
|
|
sketch = part.newObject('Sketcher::SketchObject', 'Sketch')
|
|
|
|
|
|
|
|
# determine face orientation....
|
|
|
|
|
|
|
|
sketch.Support = (attached_to, ['Face001'])
|
|
|
|
sketch.MapMode = 'FlatFace'
|
|
|
|
|
|
|
|
return sketch
|
|
|
|
|
|
|
|
def findAssembly(self):
|
|
|
|
sel = selectionContainsAssembly()
|
|
|
|
|
|
|
|
if not sel:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return sel
|
|
|
|
|
|
|
|
def findLinks(self, asm, discard_origin = False):
|
|
|
|
return [] if not asm else [el for el in asm.OutList if el.TypeId == 'App::Link']
|
|
|
|
|
2022-12-25 03:04:17 -05:00
|
|
|
def findLCS(self, obj_list):
|
|
|
|
return [ l for l in obj_list if l.TypeId == 'PartDesign::CoordinateSystem' ]
|
|
|
|
|
2022-12-15 13:15:05 -05:00
|
|
|
def getLinkOwner(self, link):
|
|
|
|
return link.LinkedObject
|
|
|
|
|
2022-12-25 03:04:17 -05:00
|
|
|
def insideBB(self, bb, p):
|
|
|
|
return (p.x > bb.XMin and p.x < bb.XMax) and (p.y > bb.YMin and p.y < bb.YMax) and (p.z > bb.ZMin and p.z < bb.ZMax)
|
|
|
|
|
2022-12-15 13:15:05 -05:00
|
|
|
FP = App.Flatpack = FlatpackWBTools()
|
|
|
|
asm = Gui.ActiveDocument.Document.getObject('Model')
|
|
|
|
|
|
|
|
asm = FP.findAssembly()
|
|
|
|
|
|
|
|
if asm:
|
2022-12-25 03:04:17 -05:00
|
|
|
ad.openTransaction('flatpack')
|
2022-12-15 13:15:05 -05:00
|
|
|
FP.createGroup(asm)
|
2022-12-25 03:04:17 -05:00
|
|
|
ad.commitTransaction()
|
2022-12-15 13:15:05 -05:00
|
|
|
else:
|
|
|
|
print('No Assembly selected.')
|
|
|
|
|
|
|
|
ad.recompute()
|