Page 1 of 2

[Solved]:How to create a convex lens

Posted: Fri Jul 01, 2022 7:53 am
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 1212 times

Re: Question:How to create a convex lens

Posted: Fri Jul 01, 2022 8:07 am
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.

Re: Question:How to create a convex lens

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

https://forum.freecad.org/viewtopic.php ... 0a16fd3123

Re: Question:How to create a convex lens

Posted: Fri Jul 01, 2022 9:09 am
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.

Re: Question:How to create a convex lens

Posted: Fri Jul 01, 2022 9:13 am
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

Re: Question:How to create a convex lens

Posted: Fri Jul 01, 2022 9:23 am
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

Re: Question:How to create a convex lens

Posted: Fri Jul 01, 2022 9:30 am
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.

Re: Question:How to create a convex lens

Posted: Fri Jul 01, 2022 9:40 am
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

Re: Question:How to create a convex lens

Posted: Mon Jul 04, 2022 6:26 am
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")

Re: Question:How to create a convex lens

Posted: Mon Jul 04, 2022 7:16 am
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