help on sheet metal script

Need help, or want to share a macro? Post here!
jaisejames
Posts: 325
Joined: Sat Sep 24, 2016 6:51 am

Re: help on sheet metal script

Postby jaisejames » Mon Feb 04, 2019 5:28 am

Seems to be some precision issue with part 3d offset. if I change Length from 20 to 21 or 19, it works. If I changes thickness to 1.001 or any other value, offset is success. No much idea why it is happening.
reox
Posts: 884
Joined: Sat Aug 13, 2016 10:06 am
Contact:

Re: help on sheet metal script

Postby reox » Wed Feb 20, 2019 6:49 pm

A topic came up in the german subforum regarding bend along sketch line.
It looks like that this feature was somewhere developed but did not find it's way into master.
@wega found the code and merged it into master here: https://forum.freecadweb.org/viewtopic. ... 20#p288098 (not in git)

Would be nice to see this feature in the master. Is the code sitting somewhere, waiting to be merged?
User avatar
Kunda1
Posts: 8759
Joined: Thu Jan 05, 2017 9:03 pm

Re: help on sheet metal script

Postby Kunda1 » Sat Feb 23, 2019 12:36 pm

reox wrote:
Wed Feb 20, 2019 6:49 pm
Would be nice to see this feature in the master. Is the code sitting somewhere, waiting to be merged?
I don't see it in the PR queue
Care to make a PR ?
Want to contribute back to FC? Checkout:
#lowhangingfruit | Use the Source, Luke. | How to Help FreeCAD | How to report FC bugs and features
reox
Posts: 884
Joined: Sat Aug 13, 2016 10:06 am
Contact:

Re: help on sheet metal script

Postby reox » Sat Feb 23, 2019 12:43 pm

Kunda1 wrote:
Sat Feb 23, 2019 12:36 pm
reox wrote:
Wed Feb 20, 2019 6:49 pm
Would be nice to see this feature in the master. Is the code sitting somewhere, waiting to be merged?
I don't see it in the PR queue
Care to make a PR ?
I somehow put the files @wega supplied into a repo, see here: https://github.com/shaise/FreeCAD_Sheet ... x:new_wall

But some things do not work and there are three different invert options and I have no idea what they are about.
Also I found some code which will not work (grep for FIXME)
I'll create a PR and comment on the things which probably need some fixing.
User avatar
Kunda1
Posts: 8759
Joined: Thu Jan 05, 2017 9:03 pm

Re: help on sheet metal script

Postby Kunda1 » Sat Feb 23, 2019 1:45 pm

shaise wrote:
At your convenience, care to weight in?
Want to contribute back to FC? Checkout:
#lowhangingfruit | Use the Source, Luke. | How to Help FreeCAD | How to report FC bugs and features
User avatar
shaise
Posts: 373
Joined: Thu Jun 11, 2015 8:11 am

Re: help on sheet metal script

Postby shaise » Sat Feb 23, 2019 3:21 pm

Its a nice feature, but unless most of the issues are resolved, I do not think it should be merged.
jaisejames
Posts: 325
Joined: Sat Sep 24, 2016 6:51 am

Re: help on sheet metal script

Postby jaisejames » Sun Feb 24, 2019 2:51 pm

pls see new Fold Along line code

Code: Select all

import Part
import BOPTools.SplitFeatures, BOPTools.JoinFeatures

def smthk(obj, foldface) :
  normal = foldface.normalAt(0,0)
  theVol = obj.Volume
  if theVol < 0.0001:
      SMError("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
  p2 = foldface.CenterOfMass + estimated_thk * -1.3 * normal
  e1 = Part.makeLine(p1, p2)
  thkedge = obj.common(e1)
  thk = thkedge.Length
  return thk

def smFold(bendR = 1.0, bendA = 90.0, kfactor = 0.45, invertbend = False, flipped = False, unfold = False,
            bendlinesketch = None, selFaceNames = '', MainObject = None):
  FoldShape = MainObject.Shape
  foldface = FoldShape.getElement(selFaceNames[0])
  normal = foldface.normalAt(0,0)
  thk = smthk(FoldShape, foldface)
  offset = thk / 2.0 * kfactor
  tool = bendlinesketch.Shape
  tool_faces = tool.extrude(normal * -thk)
  #Part.show(tool_faces)
  cutSolid = BOPTools.SplitAPI.slice(FoldShape, tool_faces.Faces, "Standard", 0.0)
  cutface = BOPTools.SplitAPI.slice(foldface, tool.Edges, "Standard", 0.0)
  sketch = tool.copy()
  sketch.translate(normal * -offset)
  cutface.translate(normal * -offset)
  Axis = FoldShape.common(sketch)
  #Part.show(Axis)
  edge = Axis.Edges[0]
  revAxisP = edge.valueAt(edge.FirstParameter)
  revAxisV = edge.valueAt(edge.LastParameter) - edge.valueAt(edge.FirstParameter)
  revAxisV.normalize()
  face0 = cutface.childShapes()[0]
  face1 = cutface.childShapes()[1]
  face1.rotate(revAxisP, revAxisV, bendA)
  #Part.show(face1)
  facecommon = face1.common(tool)
  #Part.show(facecommon)
  if not(facecommon.Edges) :
    revAxisV = revAxisV * -1
    face1 = cutface.childShapes()[1]
    face1.rotate(revAxisP, revAxisV, bendA)
  solid0 = cutSolid.childShapes()[0]
  solid1 = cutSolid.childShapes()[1]
  solid1.rotate(revAxisP, revAxisV, bendA)
  #Part.show(solid0)
  #Part.show(solid1)
  facelist = [face0, face1]
  joinface = BOPTools.JoinAPI.connect(facelist)
  filletedface = joinface.makeFillet(bendR + offset, joinface.Edges)
  #Part.show(filletedface)
  offsetfacelist = []
  offsetsolidlist = []
  for face in filletedface.Faces :
    if str(face.Surface) != "<Plane object>" :
      offsetface = face.makeOffsetShape(offset-thk, 0.0)
      #Part.show(offsetface)
      offsetfacelist.append(offsetface)
  for face in offsetfacelist :
    offsetsolid = face.makeOffsetShape(thk, 0.0, fill = True)
    #Part.show(offsetsolid)
    offsetsolidlist.append(offsetsolid)
  if len(offsetsolidlist) > 1 :
    offsetsolid = offsetsolidlist[0].multiFuse(offsetsolidlist[1:])
  else :
    offsetsolid = offsetsolidlist[0]
  cutsolid1 = BOPTools.JoinAPI.cutout_legacy(solid0, offsetsolid, 0.0)
  cutsolid2 = BOPTools.JoinAPI.cutout_legacy(solid1, offsetsolid, 0.0)
  solidlist = [cutsolid1, cutsolid2, offsetsolid]
  resultsolid = BOPTools.JoinAPI.connect(solidlist)
  return resultsolid

base = Gui.Selection.getSelectionEx()[0].Object
face = Gui.Selection.getSelectionEx()[0].SubElementNames
sketch = Gui.Selection.getSelectionEx()[1].Object
a = smFold(bendR = 1.0, bendA = 90.0, kfactor = 0.4, bendlinesketch = sketch, selFaceNames = face, MainObject = base)
Part.show(a)

Steps
1. select face required to bend.
2. select sketch on that
Attachments
unfolded view.png
unfolded view.png (27.69 KiB) Viewed 561 times
folded view.png
folded view.png (21.61 KiB) Viewed 561 times
User avatar
easyw-fc
Posts: 2915
Joined: Thu Jul 09, 2015 9:34 am

Re: help on sheet metal script

Postby easyw-fc » Sun Feb 24, 2019 3:15 pm

jaisejames wrote:
Sun Feb 24, 2019 2:51 pm
pls see new Fold Along line code
@jaisejames
would you mind to post the FC file too?
Thanks
freecad-heini-1
Posts: 7369
Joined: Tue Jan 07, 2014 11:10 am
Contact:

Re: help on sheet metal script

Postby freecad-heini-1 » Sun Feb 24, 2019 4:43 pm

jaisejames wrote:
Sun Feb 24, 2019 2:51 pm
pls see new Fold Along line code

Code: Select all

import Part
import BOPTools.SplitFeatures, BOPTools.JoinFeatures

def smthk(obj, foldface) :
  normal = foldface.normalAt(0,0)
  theVol = obj.Volume
  if theVol < 0.0001:
      SMError("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
  p2 = foldface.CenterOfMass + estimated_thk * -1.3 * normal
  e1 = Part.makeLine(p1, p2)
  thkedge = obj.common(e1)
  thk = thkedge.Length
  return thk

def smFold(bendR = 1.0, bendA = 90.0, kfactor = 0.45, invertbend = False, flipped = False, unfold = False,
            bendlinesketch = None, selFaceNames = '', MainObject = None):
  FoldShape = MainObject.Shape
  foldface = FoldShape.getElement(selFaceNames[0])
  normal = foldface.normalAt(0,0)
  thk = smthk(FoldShape, foldface)
  offset = thk / 2.0 * kfactor
  tool = bendlinesketch.Shape
  tool_faces = tool.extrude(normal * -thk)
  #Part.show(tool_faces)
  cutSolid = BOPTools.SplitAPI.slice(FoldShape, tool_faces.Faces, "Standard", 0.0)
  cutface = BOPTools.SplitAPI.slice(foldface, tool.Edges, "Standard", 0.0)
  sketch = tool.copy()
  sketch.translate(normal * -offset)
  cutface.translate(normal * -offset)
  Axis = FoldShape.common(sketch)
  #Part.show(Axis)
  edge = Axis.Edges[0]
  revAxisP = edge.valueAt(edge.FirstParameter)
  revAxisV = edge.valueAt(edge.LastParameter) - edge.valueAt(edge.FirstParameter)
  revAxisV.normalize()
  face0 = cutface.childShapes()[0]
  face1 = cutface.childShapes()[1]
  face1.rotate(revAxisP, revAxisV, bendA)
  #Part.show(face1)
  facecommon = face1.common(tool)
  #Part.show(facecommon)
  if not(facecommon.Edges) :
    revAxisV = revAxisV * -1
    face1 = cutface.childShapes()[1]
    face1.rotate(revAxisP, revAxisV, bendA)
  solid0 = cutSolid.childShapes()[0]
  solid1 = cutSolid.childShapes()[1]
  solid1.rotate(revAxisP, revAxisV, bendA)
  #Part.show(solid0)
  #Part.show(solid1)
  facelist = [face0, face1]
  joinface = BOPTools.JoinAPI.connect(facelist)
  filletedface = joinface.makeFillet(bendR + offset, joinface.Edges)
  #Part.show(filletedface)
  offsetfacelist = []
  offsetsolidlist = []
  for face in filletedface.Faces :
    if str(face.Surface) != "<Plane object>" :
      offsetface = face.makeOffsetShape(offset-thk, 0.0)
      #Part.show(offsetface)
      offsetfacelist.append(offsetface)
  for face in offsetfacelist :
    offsetsolid = face.makeOffsetShape(thk, 0.0, fill = True)
    #Part.show(offsetsolid)
    offsetsolidlist.append(offsetsolid)
  if len(offsetsolidlist) > 1 :
    offsetsolid = offsetsolidlist[0].multiFuse(offsetsolidlist[1:])
  else :
    offsetsolid = offsetsolidlist[0]
  cutsolid1 = BOPTools.JoinAPI.cutout_legacy(solid0, offsetsolid, 0.0)
  cutsolid2 = BOPTools.JoinAPI.cutout_legacy(solid1, offsetsolid, 0.0)
  solidlist = [cutsolid1, cutsolid2, offsetsolid]
  resultsolid = BOPTools.JoinAPI.connect(solidlist)
  return resultsolid

base = Gui.Selection.getSelectionEx()[0].Object
face = Gui.Selection.getSelectionEx()[0].SubElementNames
sketch = Gui.Selection.getSelectionEx()[1].Object
a = smFold(bendR = 1.0, bendA = 90.0, kfactor = 0.4, bendlinesketch = sketch, selFaceNames = face, MainObject = base)
Part.show(a)

Steps
1. select face required to bend.
2. select sketch on that
Whow, thank you so much, very nice. Here is an example from the German forum section:
Forumfrage_Sheetmetal2_FH-1.FCStd
(22.02 KiB) Downloaded 36 times
If the sketch dimension is on 0mm it works, but if the sketch is on 2,5mm it fails, because of the hole, I guess.
The hole should be deformed a little bit, like to see in my video with Creo:
https://peertube.mastodon.host/videos/w ... afe1c4eeaf

The wallthickness is 5mm, the bend radius 5mm, angle 45°.

Any idea to solve this?
reox
Posts: 884
Joined: Sat Aug 13, 2016 10:06 am
Contact:

Re: help on sheet metal script

Postby reox » Sun Feb 24, 2019 5:29 pm

shaise wrote:
Sat Feb 23, 2019 3:21 pm
Its a nice feature, but unless most of the issues are resolved, I do not think it should be merged.
true. better use the new one then.