[Solved]:How to create a convex lens

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
xianyu
Posts: 66
Joined: Mon Jun 27, 2022 7:34 am

[Solved]:How to create a convex lens

Post by xianyu »

Hello,I'm working on a new raytracing workbench.
I want to create a concave or convex lens in my workbench.Can I use "Part::" to create in Python code?Or create lenses without using "Part::"?like this, it seems a little difficult
Lens in LightTools
Lens in LightTools
lens.jpg (72.07 KiB) Viewed 1167 times
Last edited by xianyu on Wed Jul 06, 2022 7:24 am, edited 3 times in total.
Freecad novice, A Python enthusiast
thyssentishman
Posts: 82
Joined: Mon May 16, 2022 10:35 am

Re: Question:How to create a convex lens

Post by thyssentishman »

Hi,

Concave lens (like in the picture):

you could use Part_Cut between a Cylinder and two Spheres

1. In the Part WB create the primitives (Cylinder and Spheres)
2. Position the Spheres relative to the Cylinder with the desired overlap (probably only by adjusting the Z value of position)
3. Select the Cylinder (object to be kept), hold Ctrl and select one Sphere
4. Click on Part Cut
5. Select the Cut in the TreeView (object to be kept), hold Ctrl and select the second Sphere
6. Click on Part Cut

Convex lens:

you could use Part_Common between two overlapping Spheres:

1. In the Part WB create the primitives (Spheres)
2. Position the Spheres relative to eachother with the desired overlap (probably only by adjusting the Z value of position)
3. Select both Spheres by holding Ctrl
4. Click on Part Common

Kind regards,
Johannes

EDIT: Complemented answer with instructions for Convex lens due to confusion between the provided picture and the title of the post.
Last edited by thyssentishman on Fri Jul 01, 2022 8:19 am, edited 1 time in total.
User avatar
johnwang
Veteran
Posts: 1345
Joined: Sun Jan 27, 2019 12:41 am

Re: Question:How to create a convex lens

Post by johnwang »

Maybe there is a lens code in optics workbench

https://forum.freecad.org/viewtopic.php ... 0a16fd3123
hfc series CAE workbenches for FreeCAD (hfcNastran95, hfcMystran, hfcFrame3DD, hfcSU2 and more)
xianyu
Posts: 66
Joined: Mon Jun 27, 2022 7:34 am

Re: Question:How to create a convex lens

Post by xianyu »

thyssentishman wrote: Fri Jul 01, 2022 8:07 am Hi,
Concave lens (like in the picture):
you could use Part_Cut between a Cylinder and two Spheres
Thank you for your reply.This is a very detailed way on how to create a Lens in a Part,this is very useful. :) But I am sorry that I did not describe the problem clearly.
I want to do this in Python code, not in the Part workbench.
Freecad novice, A Python enthusiast
xianyu
Posts: 66
Joined: Mon Jun 27, 2022 7:34 am

Re: Question:How to create a convex lens

Post by xianyu »

johnwang wrote: Fri Jul 01, 2022 8:09 am Maybe there is a lens code in optics workbench

https://forum.freecad.org/viewtopic.php ... 0a16fd3123
Thank you for your reply.This helped me a lot
Freecad novice, A Python enthusiast
thyssentishman
Posts: 82
Joined: Mon May 16, 2022 10:35 am

Re: Question:How to create a convex lens

Post by thyssentishman »

xianyu wrote: Fri Jul 01, 2022 9:09 am I want to do this in Python code, not in the Part workbench.
Makes sense, after all you posted in Python scripting and macros. My bad sorry.

You can do exactly the same that I explained in my previous reply in a script. Check out Part_API for creating the primitives and TopoShape_API for the boolean operations.

Make sure to import the Part library on your script.

Hope this helps.

Kind regards,
Johannes
xianyu
Posts: 66
Joined: Mon Jun 27, 2022 7:34 am

Re: Question:How to create a convex lens

Post by xianyu »

thyssentishman wrote: Fri Jul 01, 2022 9:23 am
xianyu wrote: Fri Jul 01, 2022 9:09 am I want to do this in Python code, not in the Part workbench.
You can do exactly the same that I explained in my previous reply in a script. Check out Part_API for creating the primitives and TopoShape_API for the boolean operations.
This is a very good suggestion and well worth a try.
Freecad novice, A Python enthusiast
thyssentishman
Posts: 82
Joined: Mon May 16, 2022 10:35 am

Re: Question:How to create a convex lens

Post by thyssentishman »

Check out these quick examples I made:

Concave Lens:

Code: Select all

import FreeCAD
import Part

doc = FreeCAD.newDocument()

def makeConcaveLens():
    cylinder = Part.makeCylinder(5,10)
    sphere1 = Part.makeSphere(5)
    sphere2 = Part.makeSphere(5)
    sphere2.Placement = FreeCAD.Placement(App.Vector(0.0000,0.0000,10.0000),App.Rotation(App.Vector(0.0000,0.0000,1.0000),0.0000))
    cut1 = cylinder.cut(sphere1)
    cut2 = cut1.cut(sphere2)
    return cut2
    
concaveLens = doc.addObject("Part::Feature", "Concave Lens")
concaveLens.Shape = makeConcaveLens()
doc.recompute()
Convex Lens:

Code: Select all

import FreeCAD
import Part

doc = FreeCAD.newDocument()

def makeConvexLens():
    sphere1 = Part.makeSphere(5)
    sphere2 = Part.makeSphere(5)
    sphere2.Placement = FreeCAD.Placement(App.Vector(0.0000,0.0000,5.0000),App.Rotation(App.Vector(0.0000,0.0000,1.0000),0.0000))
    intersection = sphere1.common(sphere2)
    return intersection

convexLens = doc.addObject("Part::Feature", "Convex Lens")
convexLens.Shape = makeConvexLens()
doc.recompute()
Kind regards,
Johannes
xianyu
Posts: 66
Joined: Mon Jun 27, 2022 7:34 am

Re: Question:How to create a convex lens

Post by xianyu »

thyssentishman wrote: Fri Jul 01, 2022 9:40 am Check out these quick examples I made:
Kind regards,
Johannes
Thanks, your code is very useful.I try to implement the function of modifying the curvature in ComboView-Data

Code: Select all

convexLens.addProperty("App::PropertyFloat", "curv", "Shape", "central curvature")
Freecad novice, A Python enthusiast
thyssentishman
Posts: 82
Joined: Mon May 16, 2022 10:35 am

Re: Question:How to create a convex lens

Post by thyssentishman »

xianyu wrote: Mon Jul 04, 2022 6:26 am Thanks, your code is very useful.I try to implement the function of modifying the curvature in ComboView-Data
No problem glad I could help. If there is nothing else you need, please make sure to mark the topic as solved by editing the title of your first post and prepending [Solved].

Good luck!

Kind regards,
Johannes
Post Reply