Macro to draw folding lines on a loft

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
nm2107
Posts: 42
Joined: Sun Feb 20, 2022 10:37 pm

Re: Macro to draw folding lines on a loft

Post by nm2107 »

Have you tried an oloid?
Not yet, as the code only supports sketches selection for now (which are planar). It would be a nice improvement to be able to select any face, and to invoke the macro to draw the folding lines and flatten it. I think I'll have to follow the paper about the more complex shapes.
Do you have the code worked out for parts at an angle yet ?
Yes, any loft made by two planar sketches should work, e.g. :
Screenshot from 2023-03-26 15-29-13.png
Screenshot from 2023-03-26 15-29-13.png (13.64 KiB) Viewed 903 times
Screenshot from 2023-03-26 15-29-06.png
Screenshot from 2023-03-26 15-29-06.png (16.53 KiB) Viewed 903 times
Screenshot from 2023-03-26 15-29-38.png
Screenshot from 2023-03-26 15-29-38.png (17.27 KiB) Viewed 903 times
a nice fish !

You can tweak the tolerance value in the code which is used to check about planarity if the result isn't as expected (i.e. if missing folding lines, try to lower the tolerance power, e.g. from 1e-10 to 1e-2).

I still have to tweak the flattening rendering because it sometimes looses the appropriate orientation, e.g. for the rounded corners rectangle to circle loft :
Screenshot from 2023-03-26 15-38-32.png
Screenshot from 2023-03-26 15-38-32.png (34.75 KiB) Viewed 903 times
User avatar
nm2107
Posts: 42
Joined: Sun Feb 20, 2022 10:37 pm

Re: Macro to draw folding lines on a loft

Post by nm2107 »

I added a notion of cosmetic lines to the macro to also draw lines from the middle of straight edges :
Screenshot from 2023-03-26 18-37-25.png
Screenshot from 2023-03-26 18-37-25.png (27.82 KiB) Viewed 864 times
I wanted to draw such cosmetic folding lines in an other color than black (because there is actually no folding on such lines, they are used only as a guide), but changing the edge color requires the creation of a `Part::Feature` object (added to the document), which can be cumbersome to do, cf viewtopic.php?t=7335 . So for now, they are painted in black.

Such cosmetic lines also helped to fix the issue from my previous comment.

I also updated the flat representation drawing to draw the first folding line twice : at the beginning and at the end, so the flatten representation can be wrapped up having the two drawings of the first folding line superposed.

Before :
Screenshot from 2023-03-26 18-43-24.png
Screenshot from 2023-03-26 18-43-24.png (41.17 KiB) Viewed 864 times
After :
Screenshot from 2023-03-26 18-37-47.png
Screenshot from 2023-03-26 18-37-47.png (42.69 KiB) Viewed 864 times
Code available at https://github.com/nm2107/FreeCADLoftFo ... es.FCMacro
edwilliams16
Veteran
Posts: 3106
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Macro to draw folding lines on a loft

Post by edwilliams16 »

@nm2107
Can you handle sketches that aren't convex? Here's a simple example done by hand between Sketch and Sketch001. That's where I haven't got a handle on how to do the 'piecewise' part.
Screenshot 2023-03-26 at 8.39.03 AM.png
Screenshot 2023-03-26 at 8.39.03 AM.png (76.85 KiB) Viewed 840 times
BTW, the oloid is a loft between two planar circles.
Attachments
pants.FCStd
(19.99 KiB) Downloaded 10 times
edwilliams16
Veteran
Posts: 3106
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Macro to draw folding lines on a loft

Post by edwilliams16 »

@nm2107
In fact, there's a lack of uniqueness, at least the way I'm doing it, when the sketches aren't convex. Here's another development between the two sketches.
Screenshot 2023-03-26 at 12.04.29 PM.png
Screenshot 2023-03-26 at 12.04.29 PM.png (81.56 KiB) Viewed 806 times
You can check the Gaussian Curvature, by selecting a face, with

Code: Select all

sel = Gui.Selection.getSelectionEx()[0]
face = sel.SubObjects[0]
u1,u2,v1,v2 = face.ParameterRange
gc = face.Surface.curvature((u1+u2)/2, (v1+v2)/2, 'Gauss')
print(f'Curvature = {gc}')
Attachments
pants2.FCStd
(17.71 KiB) Downloaded 11 times
User avatar
nm2107
Posts: 42
Joined: Sun Feb 20, 2022 10:37 pm

Re: Macro to draw folding lines on a loft

Post by nm2107 »

mh concave parts aren't well supported by the current code unfortunatly, even by tweaking the tolerance value. I should read some more about this case, I haven't dug into the `piecewise` part so deep for now.

Also, the issue I'm currently having is that two points can be connected by a line which is going through the sape (i.e. not staying on the shape surface only), e.g. the line in yellow from your pants.FCStd file :
Screenshot from 2023-03-27 21-40-43.png
Screenshot from 2023-03-27 21-40-43.png (25.29 KiB) Viewed 748 times
Also, the code doesn't work very well for simple pieces, such as a square to square loft :
Screenshot from 2023-03-27 21-42-32.png
Screenshot from 2023-03-27 21-42-32.png (17.7 KiB) Viewed 748 times
Maybe I should try to detect the largest planar face as possible, which stays on the surface... I currently have no idea of how I could do that ^^ . I still have to read some more :p
edwilliams16
Veteran
Posts: 3106
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Macro to draw folding lines on a loft

Post by edwilliams16 »

One thought about 'piecewise' - If you have two planar closed sketches like Sketch and Sketch001 in Pants.FCStd one way to segment the surface might be to first take the convex hull of each, which adds some straight lines connecting the high points. Then compute the developable surface of the two now convex curves. Keep the parts that connect segments of the original curves. Connect up the missing pieces with generalized cones.

From your image, it appears that your errant lines arise from the fact that as soon as you have non-convex curves, the condition for accepting a line as a ruling of a developable surface has multiple roots - basically the tangent vectors are parallel at more than one point.

https://www.cs.ubc.ca/labs/imager/th/20 ... enneth.pdf is interesting. But I haven't penetrated very far into it.
@nm2107
jfc4120
Posts: 448
Joined: Sat Jul 02, 2022 11:16 pm

Re: Macro to draw folding lines on a loft

Post by jfc4120 »

@nm2107 a rectangular to rectangular can be directly triangulated, because it's easy to get all coordinates to work with.

As I suggested in an earlier reply, you may need a few python scripts that fit each type use case.

I have been experimenting with auto setting some points to triangulate, been tough going.

But I do agree with the mesh unwrap, I don't like that, I'd rather triangulate.

I have also worked on getting the true amount of stretch out for circular parts, as example.

If 16 segments is used, I have a factor I use.

Say a 10 inch diameter, the factor is 1.006455, so a 10.06455 inch diameter is used. Then when triangulated it lays it out to a 10 inch diameter stretch out. It just handles the small error in the polygon straight sections.

An XLS spreadsheet is attached.

Edit:

Code for arc / circle so far.

Code: Select all

# -*- coding: utf-8 -*-

import FreeCAD,FreeCADGui
import Draft
import math
import Get_circle_factor
from PySide import QtGui

WIRE = True
INPUTININCHES = True
if INPUTININCHES:
    convert = 25.4
else:
    convert =1.0

getDouble = QtGui.QInputDialog.getDouble
Vector, Placement = App.Vector, App.Placement
doc = App.ActiveDocument

sel = Gui.Selection.getSelectionEx()
start_point = Vector(0, 0, 0)
if sel:
    sel, = sel
    if sel.PickedPoints:
        start_point = sel.PickedPoints[-1]

FLAG=0

TCLRD, tclrdflag = getDouble(None, 'example', 'Radius', decimals=3)
TCLRD *= convert

BASEON, baseonflag = getDouble(None, 'example', 'POLY no. sides 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 56, 60', decimals=0)

FS, fsflag = getDouble(None, 'example', 'Right segment - 1 Left segment 2', decimals=0)

baselist = [12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 56, 60]
exist_count = baselist.count(BASEON)

# checking if it is more than 0
if exist_count > 0:
    based = BASEON
    r = TCLRD
    NEWTCLRD = Get_circle_factor.getfactor(based, r)
    SANG = 0
    EANG = 360
    zaxis = App.Vector(0, 0, 1)
    p1 = App.Vector(0, 0, 0)
    place1 = App.Placement(p1, App.Rotation(zaxis, 0))
    circle1 = Draft.make_circle(NEWTCLRD, placement=place1, face=None, startangle=SANG, endangle=EANG, support=None)
else:
    print("Invalid number")
 


Function:

Code: Select all

# -*- coding: utf-8 -*-

import math


def getfactor(based=12, r=5):
    global newclr
    if based == 12:
        newclr = r * 1.011516
        return newclr
    if based == 16:
        newclr = r * 1.006455
        return newclr
    if based == 20:
        newclr = r * 1.004125
        return newclr
    if based == 24:
        newclr = r * 1.002863
        return newclr
    if based == 28:
        newclr = r * 1.002102
        return newclr
    if based == 32:
        newclr = r * 1.001609
        return newclr
    if based == 36:
        newclr = r * 1.001268
        return newclr
    if based == 40:
        newclr = r * 1.001029
        return newclr
    if based == 44:
        newclr = r * 1.000851
        return newclr
    if based == 48:
        newclr = r * 1.000715
        return newclr
    if based == 56:
        newclr = r * 1.000525
        return newclr
    if based == 60:
        newclr = r * 1.000458
        return newclr
 


Edit:
I still haven't automated setting all points yet.

For the circular sections I can (kind of) automate point setting, but for flat section still manual setting.
Attachments
ob2.png
ob2.png (28.5 KiB) Viewed 620 times
ob1.png
ob1.png (8.83 KiB) Viewed 620 times
User avatar
nm2107
Posts: 42
Joined: Sun Feb 20, 2022 10:37 pm

Re: Macro to draw folding lines on a loft

Post by nm2107 »

Thank you for your answers and the code snippets. I haven't continued my work yet, by lack of time and lack of understanding the maths in the previously posted papers ^^

Another paper : https://hal.inria.fr/inria-00337445/document
jfc4120
Posts: 448
Joined: Sat Jul 02, 2022 11:16 pm

Re: Macro to draw folding lines on a loft

Post by jfc4120 »

@nm2107 Good luck and Godspeed in your learning and programming. In a few days I will send you a PM on my code I have so far, not quite ready.
jfc4120
Posts: 448
Joined: Sat Jul 02, 2022 11:16 pm

Re: Macro to draw folding lines on a loft

Post by jfc4120 »

@nm2107 I am also working on getting lengths for intersections, image explains:

And I sent PM.

Code so far:

Code: Select all

import FreeCAD, FreeCADGui
import Draft
import math
# from math import cos, sin, radians
from PySide import QtGui

convert = 25.4

Vector, Placement = App.Vector, App.Placement
doc = App.ActiveDocument

edges = FreeCADGui.Selection.getSelectionEx()[0].SubObjects
KOUNT = 0

for edge in edges:
  KOUNT = KOUNT + 1
  L = edge.Length / convert
  if KOUNT == 1:
    multilist = [L]
  if KOUNT > 1:
    multilist.append(L)
  print(L)

print(multilist)
print(multilist[2])

for i in range(len(multilist)):
  print('length', multilist[i])
#L = Gui.Selection.getSelectionEx()[0].SubObjects[0].Length
#print(L)

############   Still in progress  ##############
Attachments
lengths.png
lengths.png (51.53 KiB) Viewed 485 times
Post Reply