Skip to content

Commit

Permalink
Some code refactoring and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
shaise committed Dec 21, 2024
1 parent cc5e196 commit df8fea1
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 275 deletions.
28 changes: 14 additions & 14 deletions SheetMetalBendSolid.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,20 +242,20 @@ def bend_solid(sel_face, sel_edge, bend_r, thickness, neutral_radius, axis, flip
OuterFace.check(True) # No output = good
#Part.show(OuterFace, "OuterFace")
for fWire in sel_face.Wires :
if not(outWire.isEqual(fWire)) :
wrap_wire = wrap_face(fWire, neutral_radius, axis, normal, zero_vert, center, zero_vert_normal)
edge_list = Part.__sortEdges__(wrap_wire)
wire = Part.Wire(edge_list)
#Part.show(myWire, "myWire")
InnerFace = Part.Face(face_elt.Surface, wire)
#f.check(True)
InnerFace.validate()
#Part.show(InnerFace, "InnerFace")
InnerFace.check(True) # No output = good
#Part.show(InnerFace, "InnerFace")
OuterFace = OuterFace.cut(InnerFace)

if not(flipped) :
if not outWire.isEqual(fWire) :
wrap_wire = wrap_face(fWire, neutral_radius, axis, normal, zero_vert, center, zero_vert_normal)
edge_list = Part.__sortEdges__(wrap_wire)
wire = Part.Wire(edge_list)
#Part.show(myWire, "myWire")
InnerFace = Part.Face(face_elt.Surface, wire)
#f.check(True)
InnerFace.validate()
#Part.show(InnerFace, "InnerFace")
InnerFace.check(True) # No output = good
#Part.show(InnerFace, "InnerFace")
OuterFace = OuterFace.cut(InnerFace)

if not flipped:
bent_solid = OuterFace.makeOffsetShape(thickness, 0.0, fill = True)
else:
bent_solid = OuterFace.makeOffsetShape(-thickness, 0.0, fill = True)
Expand Down
35 changes: 9 additions & 26 deletions SheetMetalCmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,19 +196,6 @@ def smRestrict(var, fromVal, toVal):
return var


def smFace(selItem, obj):
# find face, if Edge Selected
if type(selItem) == Part.Edge:
Facelist = obj.ancestorsOfType(selItem, Part.Face)
if Facelist[0].Area < Facelist[1].Area:
selFace = Facelist[0]
else:
selFace = Facelist[1]
elif type(selItem) == Part.Face:
selFace = selItem
return selFace


def smModifiedFace(Face, obj):
# find face Modified During loop
for face in obj.Faces:
Expand All @@ -219,15 +206,6 @@ def smModifiedFace(Face, obj):
return face


def smGetEdge(Face, obj):
# find Edges that overlap
for edge in obj.Edges:
face_common = edge.common(Face)
if face_common.Edges:
break
return edge


def LineAngle(edge1, edge2):
# find angle between two lines
v1a = edge1.Vertexes[0].Point
Expand Down Expand Up @@ -306,6 +284,7 @@ def getCornerPoint(edge1, edge2):
e2 = edge2.Curve.toShape()
# Part.show(e2,'e2')
section = e1.section(e2)
cornerPoint = None
if section.Vertexes:
# Part.show(section,'section')
cornerPoint = section.Vertexes[0].Point
Expand Down Expand Up @@ -426,7 +405,7 @@ def check_parallel(edge1, edge2):

def sheet_thk(MainObject, selFaceName):
selItem = MainObject.getElement(SheetMetalTools.getElementFromTNP(selFaceName))
selFace = smFace(selItem, MainObject)
selFace = SheetMetalTools.smGetFaceByEdge(selItem, MainObject)
# find the narrow edge
thk = 999999.0
thkDir = None
Expand Down Expand Up @@ -480,7 +459,7 @@ def smEdge(selFaceName, MainObject):
elif type(selItem) == Part.Edge:
thk, thkDir = sheet_thk(MainObject, selFaceName)
seledge = selItem
selFace = smFace(selItem, MainObject)
selFace = SheetMetalTools.smGetFaceByEdge(selItem, MainObject)
p1 = seledge.valueAt(seledge.FirstParameter)
p2 = seledge.valueAt(seledge.LastParameter)
revAxisV = p2 - p1
Expand Down Expand Up @@ -774,6 +753,8 @@ def smMiter(
# Part.show(tranfacelist[j],'tranfacelist')
# Part.show(tranfacelist[i],'tranfacelist')
# Part.show(wallface_common,'wallface_common')
vp1 = None
vp2 = None
if wallface_common.Edges:
vp1 = wallface_common.Vertexes[0].Point
vp2 = wallface_common.Vertexes[1].Point
Expand Down Expand Up @@ -918,6 +899,7 @@ def smBend(
pass

# Add Bend Type details
inside = False
if BendType == "Material Outside":
offset = 0.0
inside = False
Expand Down Expand Up @@ -945,6 +927,7 @@ def smBend(
elif LengthSpec == "Outer Sharp":
extLen -= (bendR + thk) / math.tan(math.radians(90.0 - bendA / 2))

nogaptrimedgelist = []
if not (sketches):
mainlist, trimedgelist, nogaptrimedgelist = getBendetail(
selFaceNames, MainObject, bendR, bendA, flipped, offset, gap1, gap2
Expand Down Expand Up @@ -980,7 +963,7 @@ def smBend(
gap2List,
extend1List,
extend2List,
reliefDList,
_reliefDList,
) = ([0.0], [0.0], [gap1], [gap2], [extend1], [extend2], [reliefD])
agap1, agap2 = gap1, gap2
# print([agap1,agap1])
Expand Down Expand Up @@ -1011,7 +994,7 @@ def smBend(
Cface = smModifiedFace(Cface, resultSolid)
# Part.show(Cface,'Cface')
# main Length Edge
MlenEdge = smGetEdge(AlenEdge, resultSolid)
MlenEdge = SheetMetalTools.smGetIntersectingEdge(AlenEdge, resultSolid)
# Part.show(MlenEdge,'MlenEdge')
lenEdge = trimedgelist[i]
noGap_lenEdge = nogaptrimedgelist[i]
Expand Down
83 changes: 11 additions & 72 deletions SheetMetalCornerReliefCmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,66 +23,18 @@
#
###################################################################################

import FreeCAD, Part, math, os, SheetMetalTools, SheetMetalBendSolid
from SheetMetalLogger import SMLogger
import math
import os
import FreeCAD
import Part
import SheetMetalTools
import SheetMetalBendSolid

smEpsilon = SheetMetalTools.smEpsilon

# list of properties to be saved as defaults
smCornerReliefDefaultVars = ["Size", "SizeRatio", ("kfactor", "defaultKFactor")]

def smthk(obj, foldface):
normal = foldface.normalAt(0, 0)
theVol = obj.Volume
if theVol < 0.0001:
SMLogger.error(
FreeCAD.Qt.translate(
"Logger", "Shape is not a real 3D-object or to small for a metal-sheet!"
)
)
else:
# Make a first estimate of the thickness
estimated_thk = theVol / (obj.Area / 2.0)
# p1 = foldface.CenterOfMass
for v in foldface.Vertexes:
p1 = v.Point
p2 = p1 + estimated_thk * -1.5 * normal
e1 = Part.makeLine(p1, p2)
thkedge = obj.common(e1)
thk = thkedge.Length
if thk > smEpsilon:
break
return thk


def smCutFace(Face, obj):
# find face Modified During loop
for face in obj.Faces:
face_common = face.common(Face)
if face_common.Faces:
break
return face


def smGetEdge(Face, obj):
# find Edges that overlap
for edge in obj.Edges:
face_common = edge.common(Face)
if face_common.Edges:
break
return edge


def smGetEdgelist(Face, obj):
# find Edges that overlap
edgelist = []
for edge in obj.Edges:
face_common = edge.common(Face)
if face_common.Edges:
edgelist.append(edge)
return edgelist


def makeSketch(relieftype, size, ratio, cent, normal, addvector):
# create wire for face creation
if "Circle" in relieftype:
Expand All @@ -101,19 +53,6 @@ def makeSketch(relieftype, size, ratio, cent, normal, addvector):
# Part.show(sketch,'sketch')
return sketch


def equal_angle(ang1, ang2, p=5):
# compares two angles
result = False
if round(ang1 - ang2, p) == 0:
result = True
if round((ang1 - 2.0 * math.pi) - ang2, p) == 0:
result = True
if round(ang1 - (ang2 - 2.0 * math.pi), p) == 0:
result = True
return result


def bendAngle(theFace, edge_vec):
# Start to investigate the angles at self.__Shape.Faces[face_idx].ParameterRange[0]
# Part.show(theFace,"theFace")
Expand All @@ -128,7 +67,7 @@ def bendAngle(theFace, edge_vec):
edgeAngle, edgePar = theFace.Surface.parameter(edge_vec)
# print('the angles: ', angle_0, ' ', angle_1, ' ', edgeAngle, ' ', edgeAngle - 2*math.pi)

if equal_angle(angle_0, edgeAngle):
if SheetMetalTools.smIsEqualAngle(angle_0, edgeAngle):
angle_start = angle_0
angle_end = angle_1
else:
Expand Down Expand Up @@ -196,7 +135,7 @@ def getBendDetail(obj, edge1, edge2, kfactor):
break
# To get thk of sheet, top face normal, bend angle
# normal = largeface.normalAt(0,0)
thk = smthk(obj, largeface)
thk = SheetMetalTools.smGetThickness(obj, largeface)
bendA = bendAngle(cylface, cornerPoint)
# print([thk, bendA])

Expand Down Expand Up @@ -319,7 +258,7 @@ def smCornerR(
TopFace = LargeFace
# Part.show(TopFace,"TopFace")
while BalanceFace.Faces:
BendEdgelist = smGetEdgelist(BalanceFace, TopFace)
BendEdgelist = SheetMetalTools.smGetAllIntersectingEdges(BalanceFace, TopFace)
for BendEdge in BendEdgelist:
# Part.show(BendEdge,"BendEdge")
edge_facelist = resultSolid.ancestorsOfType(BendEdge, Part.Face)
Expand Down Expand Up @@ -405,7 +344,7 @@ def smCornerR(
# Part.show(sketch_face,"sketch_face")
sketch_face.rotate(revAxisP, -revAxisV, bendA)
# Part.show(sketch_face,"Rsketch_face")
CylEdge = smGetEdge(sketch_face, cylface)
CylEdge = SheetMetalTools.smGetIntersectingEdge(sketch_face, cylface)
# Part.show(CylEdge,"CylEdge")
edgefacelist = resultSolid.ancestorsOfType(CylEdge, Part.Face)
for TopFace in edgefacelist:
Expand Down Expand Up @@ -439,8 +378,8 @@ def smCornerR(


class SMCornerRelief:
'''"Add Corner Relief to Sheetmetal Bends"'''
def __init__(self, obj, selobj, sel_items):
'''"Add Corner Relief to Sheetmetal Bends"'''
_tip_ = FreeCAD.Qt.translate("App::Property", "Corner Relief Type")
obj.addProperty(
"App::PropertyEnumeration", "ReliefSketch", "Parameters", _tip_
Expand Down
26 changes: 9 additions & 17 deletions SheetMetalExtendCmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@
#
###################################################################################

import FreeCAD, Part, math, os, SheetMetalTools
import math
import os
import FreeCAD
import Part
from FreeCAD import Base
import SheetMetalTools

# IMPORTANT: please remember to change the element map version in case of any
# changes in modeling logic
Expand Down Expand Up @@ -58,20 +62,6 @@ def smMakeFace(edge, dir, extLen, gap1=0.0, gap2=0.0, angle1=0.0, angle2=0.0, op
face.mapShapes([(edge, face)], None, op)
return face


def smFace(selItem, obj):
# find face if Edge Selected
if type(selItem) == Part.Edge:
Facelist = obj.ancestorsOfType(selItem, Part.Face)
if Facelist[0].Area < Facelist[1].Area:
selFace = Facelist[0]
else:
selFace = Facelist[1]
elif type(selItem) == Part.Face:
selFace = selItem
return selFace


def smTouchFace(Face, obj, thk):
# find face Modified During loop
# Part.show(Face,'Face')
Expand Down Expand Up @@ -175,10 +165,11 @@ def smExtrude(
finalShape = selObject
for selFaceName in selFaceNames:
selItem = selObject.getElement(SheetMetalTools.getElementFromTNP(selFaceName))
selFace = smFace(selItem, selObject)
selFace = SheetMetalTools.smGetFaceByEdge(selItem, selObject)

# find the narrow edge
thk = 999999.0
thk = 999999.
thkEdge = None
for edge in selFace.Edges:
if abs(edge.Length) < thk:
thk = abs(edge.Length)
Expand Down Expand Up @@ -253,6 +244,7 @@ def smExtrude(
overlap_solid = wallSolid.common(SplitSolid2)
# Part.show(overlap_solid, "overlap_solid")

overlap_solidlist = []
if overlap_solid.Faces:
substract_face = smTouchFace(wallSolid, SplitSolid2, thk)
# Part.show(substract_face, "substract_face")
Expand Down
34 changes: 2 additions & 32 deletions SheetMetalFoldCmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,36 +32,6 @@
# list of properties to be saved as defaults
BendOnLineDefaultVars = [("radius", "defaultRadius"), ("angle", "defaultFoldAngle")]

def smthk(obj, foldface):
normal = foldface.normalAt(0, 0)
theVol = obj.Volume
if theVol < 0.0001:
SMLogger.error(
FreeCAD.Qt.translate(
"Logger", "Shape is not a real 3D-object or to small for a metal-sheet!"
)
)
else:
# Make a first estimate of the thickness
estimated_thk = theVol / (foldface.Area)
# p1 = foldface.CenterOfMass
p1 = foldface.Vertexes[0].Point
p2 = p1 + estimated_thk * -1.5 * normal
e1 = Part.makeLine(p1, p2)
thkedge = obj.common(e1)
thk = thkedge.Length
return thk


def smCutFace(Face, obj):
# find face Modified During loop
for face in obj.Faces:
face_common = face.common(Face)
if face_common.Faces:
break
return face


def smFold(
bendR=0.8,
bendA=90.0,
Expand Down Expand Up @@ -89,7 +59,7 @@ def smFold(
foldface = FoldShape.getElement(SheetMetalTools.getElementFromTNP(selFaceNames[0]))
tool = bendlinesketch.Shape.copy()
normal = foldface.normalAt(0, 0)
thk = smthk(FoldShape, foldface)
thk = SheetMetalTools.smGetThickness(FoldShape, foldface)
#print(thk)

# if not(flipped) :
Expand Down Expand Up @@ -120,7 +90,7 @@ def smFold(
solid0 = cutSolid.childShapes()[0]
else:
solid0 = cutSolid.childShapes()[1]
cutFaceDir = smCutFace(toolFaces.Faces[0], solid0)
cutFaceDir = SheetMetalTools.smGetIntersectingFace(toolFaces.Faces[0], solid0)
# Part.show(cutFaceDir,"cutFaceDir")
facenormal = cutFaceDir.Faces[0].normalAt(0, 0)
# print(facenormal)
Expand Down
Loading

0 comments on commit df8fea1

Please sign in to comment.