Creation of Surfaces

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

Creation of Surfaces

Post by onekk »

Hello I've encountered this posts, in an old topic.
wmayer wrote: Sat Sep 26, 2020 2:50 pm ...
https://forum.freecadweb.org/viewtopic. ... 03#p435003

As a recent post pointed to some Roman Lydgin post on his forum I have seen some of the techniques he explained.

Now I wonder if there could be some examples, like in:

https://forum.freecadweb.org/viewtopic. ... 65#p434265

But sadly this is only the curves that make the skeleton of the surface.

I know that some times has passed since these posts, and that there is Curves WB, And I have bothered @Chris_G some times.

But now I have some questions:

1) how to make the example portraited in: https://neweopencascade.wordpress.com/2 ... -n-curves/
2) how to make a solid out of similar surfaces, so reuse the curves to make proper faces that close a solid formed by some surfaces like these

TIA for any genre of help and answers 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/
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Creation of Surfaces

Post by wmayer »

1) how to make the example portraited in: https://neweopencascade.wordpress.com/2 ... -n-curves/

Code: Select all

import math
c = Part.Circle()
c.Radius=50
c = c.trim(0, math.pi)

e1 = Part.Ellipse()
e1.Center = (0, 0, 75)
e1.MajorRadius = 30
e1.MinorRadius = 5
e1 = e1.trim(0, math.pi)

e2 = Part.Ellipse()
e2.Center = (0, 0, 100)
e2.MajorRadius = 20
e2.MinorRadius = 5
e2 = e2.trim(0, math.pi)

s1 = App.ActiveDocument.addObject("Part::Feature","Section1")
s1.Shape = c.toShape()
s2 = App.ActiveDocument.addObject("Part::Feature","Section2")
s2.Shape = e1.toShape()
s3 = App.ActiveDocument.addObject("Part::Feature","Section3")
s3.Shape = e2.toShape()

# Parametric section surface
sections = App.ActiveDocument.addObject("Surface::Sections","Surface")
sections.NSections = [(s1, "Edge1"), (s2, "Edge1"), (s3, "Edge1")]
App.ActiveDocument.recompute()

# Non-parametric section surface
spline = Part.BSplineSurface()
spline.buildFromNSections([c, e1, e2])
Part.show(spline.toShape())
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Creation of Surfaces

Post by onekk »

wmayer wrote: Thu Aug 04, 2022 11:31 am ...
Many Thanks

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/
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Creation of Surfaces

Post by wmayer »

2) how to make a solid out of similar surfaces, so reuse the curves to make proper faces that close a solid formed by some surfaces like these

Code: Select all

import math
c = Part.Circle()
c.Radius=50
c = c.trim(0, math.pi)

e1 = Part.Ellipse()
e1.Center = (0, 0, 75)
e1.MajorRadius = 30
e1.MinorRadius = 5
e1 = e1.trim(0, math.pi)

e2 = Part.Ellipse()
e2.Center = (0, 0, 100)
e2.MajorRadius = 20
e2.MinorRadius = 5
e2 = e2.trim(0, math.pi)


spline = Part.BSplineSurface()
spline.buildFromNSections([c, e1, e2])
Part.show(spline.toShape())

surface = spline.toShape()
l1 = Part.makeLine(surface.Vertex1.Point, surface.Vertex3.Point)
l2 = Part.makeLine(surface.Vertex2.Point, surface.Vertex4.Point)

wire = Part.Wire([l1, surface.Edge2])
face1 = Part.Face(wire)

wire = Part.Wire([l2, surface.Edge4])
face2 = Part.Face(wire)
face2.reverse()

wire = Part.Wire([surface.Edge1, l1, surface.Edge3, l2])
face3 = Part.Face(wire)
face3.reverse()

shell = Part.Shell([surface, face1, face2, face3])
solid = Part.Solid(shell)
Part.show(solid)
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Creation of Surfaces

Post by onekk »

wmayer wrote: Thu Aug 04, 2022 11:47 am ....

Thanks, again, and this could solve the original exmaple.

But my question is sligtly more complicated.

I have had problems as example when creating something like this:
curved_a.py
(16.44 KiB) Downloaded 21 times
It is a sort of instrument case, that has to have a profile shape that follows the instruments (the interpolated bspline defined by points) and a raised part that I have derived defining some points on the profile, and then creating "arcs" using a bspline defined by these points.

In theory I should have obtained a a spline that I could extrude to form the "lower part" and a little "upper part" that create the "cover" with a slightly "straight side" to match the "lower part".

My problem was that when I create the surface that will became the "cover" the base spline that was obtained was not following the "original curve" and I have had many problem to create the "lower part" and the matching part to make the case correctly closing.

Probably my approach was not correct.

I know that is something different, but any hint will be appreciated.

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: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Creation of Surfaces

Post by onekk »

I've rearranged the script of the post above to make it easier to read, some code are not pertinent.

Here the new code:
20220806-bspline_compl_surf.py
(16.82 KiB) Downloaded 19 times
Some parts are commented out to make more visible the skeleton on which the surface has to be built.

I hope having added enough comments to make it readable.

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: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Creation of Surfaces

Post by onekk »

Ok I've found the original thread:

https://forum.freecadweb.org/viewtopic.php?f=22&t=60349

But the final questions has remained unanswered, as it is passed some time (more than 1 year)

EDIT: it seems that with the GUI is feasible:

https://forum.freecadweb.org/viewtopic. ... 72#p447872

I have had problem on trimming the curve, as in my first post.

Sadly I've not managed to find the code done to obtain the images on the posts, only intermediate works, that I have posted in this thread.

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: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Creation of Surfaces

Post by onekk »

wmayer wrote: Thu Aug 04, 2022 11:47 am ...
Sorry for bothering again.

I have to make the surface, and trim it at the boundaries of the "base_curve"

like in the code below:

Code: Select all

newpoints = []

for point in cm_pts:
    newpoints.append(Vector(*point))

base_spline = Part.BSplineCurve()
base_spline.buildFromPoles(newpoints)

base_curve = bsp2arcs(base_spline, a_tol)
I have transformed it to biarcs, as the original method will work better with biarcs, but it is not necessary:

I've been advised by @Chris_G to use Part.GeomPlate.BuildPlateSurface()

https://forum.freecadweb.org/viewtopic. ... 99#p519499

and I've found the code in the example from:

https://github.com/FreeCAD/FreeCAD/blob ... ePyImp.cpp


I could obtain the point cloud instead of splines:
20220806-bspline_compl_surf.py
(18.66 KiB) Downloaded 11 times
So it is not a problem to pass a sort of point cloud defining the surface.

But now I have some doubt about example code and then way to trim the obtained surface (I have to trim it with base_curve eventuallly using the original BSpline).

This way I could extrude the surface downward (-Z and obtain sides of the "instrument box").

Could you please advise me on how to use the methods to trim the obtained surface.

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/
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Creation of Surfaces

Post by wmayer »

Could you please advise me on how to use the methods to trim the obtained surface.
The four boundary curves do not exactly lie on the face. So, in a first step you must project them onto the surface. And then you can create a surface from the four boundary edges and the given face as support surface.

Another possibility is to create a filled face from the boundary edges, extrude it to create a solid and then perform a boolean intersection.

Code: Select all

face  = App.ActiveDocument.Shape.Shape
edge1 = App.ActiveDocument.Shape001.Shape
edge2 = App.ActiveDocument.Shape002.Shape
edge3 = App.ActiveDocument.Shape003.Shape
edge4 = App.ActiveDocument.Shape004.Shape

wire = Part.Wire([edge1, edge2, edge3, edge4])
comp = face.project([wire])

face_from_proj_wire = Part.makeFilledFace(comp.Edges)
solid = face_from_proj_wire.extrude(App.Vector(0,0,10))
solid.Placement.Base.z = -5
trimmed_face = solid.common(face)
Part.show(trimmed_face)
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Creation of Surfaces

Post by onekk »

wmayer wrote: Sun Aug 07, 2022 1:35 pm
Could you please advise me on how to use the methods to trim the obtained surface.
The four boundary curves do not exactly lie on the face. So, in a first step you must project them onto the surface. And then you can create a surface from the four boundary edges and the given face as support surface.

Another possibility is to create a filled face from the boundary edges, extrude it to create a solid and then perform a boolean intersection.
...

Ok thanks, now it works, using the following code:

Code: Select all

import FreeCAD
from FreeCAD import Base, Rotation, Vector # noqa
import Part

def test_platesurface():
    """From testcode."""
    v1 = Vector(0,0,0)
    v2 = Vector(10,0,0)
    v3 = Vector(10,10,3)
    v4 = Vector(0,10,0)
    v5 = Vector(5,5,5)
    l1 = Part.LineSegment(v1, v2)
    l2 = Part.LineSegment(v2, v3)
    l3 = Part.LineSegment(v3, v4)
    l4 = Part.LineSegment(v4, v1)
    c1 = Part.GeomPlate.CurveConstraint(l1)
    c2 = Part.GeomPlate.CurveConstraint(l2)
    c3 = Part.GeomPlate.CurveConstraint(l3)
    c4 = Part.GeomPlate.CurveConstraint(l4)
    c5 = Part.GeomPlate.PointConstraint(v5)
    bp = Part.GeomPlate.BuildPlateSurface()
    bp.add(c1)
    bp.add(c2)
    bp.add(c3)
    bp.add(c4)
    bp.add(c5)
    bp.perform()
    s = bp.surface()
    bs = s.makeApprox()

    # Part.show(bs.toShape())
    # Part.show(l1.toShape())
    # Part.show(l2.toShape())
    # Part.show(l3.toShape())
    # Part.show(l4.toShape())

    bp.surfInit()

    face = bs.toShape()
    wire = Part.Wire([l1.toShape(), l2.toShape(), l3.toShape(), l4.toShape()])
    comp = face.project([wire])

    face_from_proj_wire = Part.makeFilledFace(comp.Edges)
    solid = face_from_proj_wire.extrude(App.Vector(0,0,10))
    solid.Placement.Base.z = -5
    trimmed_face = solid.common(face)
    Part.show(trimmed_face, "trim_surface")
    
A little question:

- what it the scope of:

Code: Select all

  bp.surfInit()
Help says that it "returns the initial surface" but being assigned to a variable, I don't guess what is the scope.

EDIT:

Reading this description:

https://dev.opencascade.org/doc/occt-7. ... plate.html

I've thought that supplying the initial surface to "deform" using constraints, I will obtain something more regular, that is near the point cloud, but I obtain this:

geomplate1.png
geomplate1.png (23.58 KiB) Viewed 693 times
geomplate2.png
geomplate2.png (2.39 KiB) Viewed 693 times
geomplate3.png
geomplate3.png (5.85 KiB) Viewed 693 times


This is the code elaborate some more and probably simplified a little, The initial definition of the bspline is using many lines.
20220807-geomplate.py
(18.34 KiB) Downloaded 14 times


Regards

Carlo D.
Last edited by onekk on Sun Aug 07, 2022 4:00 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/
Post Reply