How to create isocurves on faces?

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
onekk
Veteran
Posts: 6149
Joined: Sat Jan 17, 2015 7:48 am
Contact:

How to create isocurves on faces?

Post by onekk »

I'm trying to explain some basics about UV surfaces, so I have made some example code to show isogrids, but it don't work as expected.

https://forum.freecadweb.org/viewtopic. ... 6&start=10

Probably there is some stupid error (and probably is about some basic things about Topology).

Here the code:
20220707-isocurves.py
(3.77 KiB) Downloaded 24 times

Edit:

Found stupid errors, now problem is how to limit the upper and lower face of the cylinder to the real "face area"?

End >Edit

Anyone could be of any help:

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
Roy_043
Veteran
Posts: 8455
Joined: Thu Dec 27, 2018 12:28 pm

Re: How to create isocurves on faces?

Post by Roy_043 »

I think this can work:

Code: Select all

face.common(edge)
User avatar
onekk
Veteran
Posts: 6149
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: How to create isocurves on faces?

Post by onekk »

Roy_043 wrote: Thu Jul 07, 2022 3:36 pm I think this can work:

Code: Select all

face.common(edge)
Thanks, I will try, as usual answer is not too far (if you know in what direction to go!) :D

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
onekk
Veteran
Posts: 6149
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: How to create isocurves on faces?

Post by onekk »

This code seems to work for sphere, cylinder, cone, box and torus probably it is rough as parameters are derived in a rough way.
20220707-isocurves.py
(4.33 KiB) Downloaded 19 times
Thanks @Roy_043

Edit:

Added a different parametrization for u and v divisions.

End edit:

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
edwilliams16
Veteran
Posts: 3112
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: How to create isocurves on faces?

Post by edwilliams16 »

@onekk

It even works for a complicated B-spline surface, which the Curve WB|Surfaces|IsoCurves tool fails on.

Code: Select all

shp = App.ActiveDocument.getObject("Sweep").Shape
isocurveslist = isocurves(t_shp, divs, False, True)
for f_idx, iso_c in enumerate(isocurveslist):
    Part.show(iso_c, f"face_{f_idx}_ic")
I changed the list isocurves to isocurveslist because it was shadowing the function of the same name.
Screen Shot 2022-07-07 at 12.40.17 PM.png
Screen Shot 2022-07-07 at 12.40.17 PM.png (50.66 KiB) Viewed 1038 times
For @Chris_G
Screen Shot 2022-07-07 at 12.42.15 PM.png
Screen Shot 2022-07-07 at 12.42.15 PM.png (26.91 KiB) Viewed 1038 times

Code: Select all

12:42:34  Traceback (most recent call last):
  File "/Users/ed/Library/Application Support/FreeCAD/Mod/Curves/./freecad/Curves/IsoCurve.py", line 108, in execute
    w = isocurves.multiIso(face, selfobj.NumberU, selfobj.NumberV).toShape()
  File "/Users/ed/Library/Application Support/FreeCAD/Mod/Curves/./freecad/Curves/isocurves.py", line 156, in toShape
    c.append(u.toShape())
  File "/Users/ed/Library/Application Support/FreeCAD/Mod/Curves/./freecad/Curves/isocurves.py", line 89, in toShape
    bounds = self.faceBounds2d()
  File "/Users/ed/Library/Application Support/FreeCAD/Mod/Curves/./freecad/Curves/isocurves.py", line 71, in faceBounds2d
    if _utils.geom_equal(pc[1], self.face.Surface):
  File "/Users/ed/Library/Application Support/FreeCAD/Mod/Curves/./freecad/Curves/_utils.py", line 363, in geom_equal
    if not have_equal_property(geom1, geom2, prop, tol):
  File "/Users/ed/Library/Application Support/FreeCAD/Mod/Curves/./freecad/Curves/_utils.py", line 310, in have_equal_property
    attr1 = getattr(geom1, prop)
<class 'Base.FreeCADError'>: Unknown exception while reading attribute 'UKnotSequence' of object 'GeomBSplineSurface'
12:42:34  Recompute failed! Please check report view.
Attachments
isocurvesbsplinesurface.FCStd
(62.48 KiB) Downloaded 14 times
User avatar
onekk
Veteran
Posts: 6149
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: How to create isocurves on faces?

Post by onekk »

edwilliams16 wrote: Thu Jul 07, 2022 10:48 pm @onekk

It even works for a complicated B-spline surface, which the Curve WB|Surfaces|IsoCurves tool fails on.
...

I changed the list isocurves to isocurveslist because it was shadowing the function of the same name.
Thanks for participating, my fault for the choice if names sometimes you are catched in coding and don't see such genre of things.

Now I have to formalize in a decent way these findings to make a decent explanation.

Speaking of code the "common" will select correctly only the part of the U,V surface covered by the face as example for cylinder's upper and lower faces.

There is a way to find boundaries of the face in the UV Surface?

There is a way to chech if a UV coordinate is on the face or outside the face?

Regards.

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
Chris_G
Veteran
Posts: 2579
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: How to create isocurves on faces?

Post by Chris_G »

edwilliams16 wrote: Thu Jul 07, 2022 10:48 pm For @Chris_G
Thanks. This is fixed.
onekk wrote: Fri Jul 08, 2022 7:26 am There is a way to find boundaries of the face in the UV Surface?
Not directly.
You have to loop over the face OuterWire edges, and extract the pcurves :

Code: Select all

for e in face.OuterWire.Edges:
    curve2d, fp, lp = face.curveOnSurface(e)
onekk wrote: Fri Jul 08, 2022 7:26 am There is a way to chech if a UV coordinate is on the face or outside the face?

Code: Select all

face.isPartOfDomain(u, v)
User avatar
onekk
Veteran
Posts: 6149
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: How to create isocurves on faces?

Post by onekk »

Chris_G wrote: Fri Jul 08, 2022 9:33 am
onekk wrote: Fri Jul 08, 2022 7:26 am There is a way to find boundaries of the face in the UV Surface?
Not directly.
You have to loop over the face OuterWire edges, and extract the pcurves :

Code: Select all

for e in face.OuterWire.Edges:
    curve2d, fp, lp = face.curveOnSurface(e)

Thanks for answering.

I have put together this test code for a cylinder:

Code: Select all

for f_idx, face in enumerate(t_shp.Faces):
    print(f"face_{f_idx} >> {face}")

    for e in face.OuterWire.Edges:
        curve2d, fp, lp = face.curveOnSurface(e)
        print(f"curve {curve2d}, fp: {fp} lp: {lp}")
And obtained this result

Code: Select all

  face_0 >> <Face object at 0x556837fa9d50>
  curve <Line2d object>, fp: 0.0 lp: 6.283185307179586
  curve <Line2d object>, fp: 0.0 lp: 20.0
  curve <Line2d object>, fp: 0.0 lp: 6.283185307179586
  face_1 >> <Face object at 0x55683a258080>
  curve <Circle2d object>, fp: 0.0 lp: 6.283185307179586
  face_2 >> <Face object at 0x55683a258320>
  curve <Circle2d object>, fp: 0.0 lp: 6.283185307179586
So I have to find intersections of all "isocurves" lines with the curve in the example with face_1 and face_2 Circle2d object returned and trim all lines at these intersection points?

A little explanations, about face_0, I could guess that is returned as a "square", but it seems that there is a missing line to close the square?

Probably I'm guessing wrong.

TIA and Regards.

Carlo D.
Last edited by onekk on Fri Jul 08, 2022 7:36 pm, edited 1 time in total.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
Chris_G
Veteran
Posts: 2579
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: How to create isocurves on faces?

Post by Chris_G »

onekk wrote: Fri Jul 08, 2022 3:54 pm So I have to find intersections of all "isocurves" lines with the curve in the example with face_1 and face_2 Circle2d object returned and trim all lines at these intersection points?
Unfortunately, yes. Not very funny.
Or it may be less work to do a boolean operation in the XY plane.
This allows you to work with all the features offered by topology (like boolean operations).

- create a flat representation of the face outerwire on XY plane :

Code: Select all

flat_edges = []
for e in face.OuterWire.OrderedEdges:
    curve2d, fp, lp = face.curveOnSurface(e)
    flat_edges.append(curve2d.toShape(fp, lp))
- build a face :

Code: Select all

face = Part.Face(Part.Wire(flat_edges))
- build a grid (compound of isocurves), again on XY plane :

Code: Select all

import numpy
vec2 = FreeCAD.Base.Vector2d
u0, u1, v0, v1 = face.ParameterRange
uiso = []
for u in numpy.linspace(u0, u1, nb_samples):
    uiso.append(Part.Geom2d.Line2dSegment(vec2(u, v0), vec2(u, v1)).toShape())

# do the same for viso

grid = Part.Compound(uiso + viso)
- make a boolean common between grid and face, to obtain a trimmed grid. Not tested. I am not sure it will succeed
- loop through the trimmed grid edges, and map them back on the original face.
onekk wrote: Fri Jul 08, 2022 3:54 pm A little explanations, about face_0, I could guess that is returned as a "square", but it seems that there is a missing line to close the square?
One of the edges of the outerwire is a seam edge.
You can test with :

Code: Select all

edge.isSeam()
In this case, the edge has 2 pcurves.
Currently, it is not possible to obtain the second pcurve with face.curveOnSurface(edge)
The easiest way is to loop over the wire OrderedEdges like I did above, where the seam edges are repeated.
User avatar
onekk
Veteran
Posts: 6149
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: How to create isocurves on faces?

Post by onekk »

Many thanks for the insight.

I have many things to study as usual.

Tanks again and best regards.

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
Post Reply