Sketcher ConstrainSnellsLaw: Difference between revisions

From FreeCAD Documentation
(Added a depiction of Snell's law)
(→‎Scripting: (in progress))
Line 20: Line 20:


==Scripting==
==Scripting==
The ExampleCommandModel tool can by used in [[macros]] and from the python console by using the following function:
The constraints can be created from [[macros]] and from the python console by using the following function:
</translate>
</translate>
{{Code|code=
'''makeExampleCommandModel (Data, Data)'''
Sketch.addConstraint(Sketcher.Constraint('SnellsLaw',line1,pointpos1,line2,pointpos2,interface,n2byn1))
}}
<translate>
<translate>
where:

: <tt>Sketch</tt> is a sketch object
* Creates a CommandModel using Data.
: <tt>line1</tt> and <tt>pointpos1</tt> are two integers identifying the endpoint of the line in medium with refractive index of ''n1''. <tt>line1</tt> is the line's index in the sketch (the value, returned by Sketch.addGeometry), and <tt>pointpos1</tt> should be 1 for start point and 2 for end point.
* Returns the newly created object.
: <tt>line2</tt> and <tt>pointpos2</tt> are the indexes specifying the endpoint of the second line (in medium ''n2'')
: <tt>n2byn1</tt> is a floating-point number equal to the ratio of refractive indices ''n2''/''n1''


Example:
Example:
</translate>
</translate>
{{Code|code=
import FreeCAD, Base
import FreeCAD, Base, Part, Sketcher
Base.makeExampleCommandModel(FreeCAD.Data,FreeCAD.Data)
Base.makeExampleCommandModel(FreeCAD.Data,FreeCAD.Data)
}}
<translate>
<translate>

==Other==
==Other==
Optional
Optional

Revision as of 23:46, 12 January 2015

Under construction!

Constraint SnellsLaw

Menu location
Sketch → Sketcher Constraints → Constrain refraction (Snell's law)
Workbenches
Sketcher, PartDesign
Default shortcut
None
Introduced in version
-
See also
None

Description

Snell's law

Constrains two lines to follow the law of refraction of light as it penetrates through an interface, where two materials of different refraction indices meet. See Snell's law on Wikipedia for more info.

How to use

The sequence of clicks is indicated by yellow arrows with numbers. n1, n2 are labeles on this picture to show where the indices of refraction are.
  • You'll need two lines that are to follow a beam of light, and a curve to act as an interface. The lines should be on different sides of the interface.
  • Select the endpoint of one line, an endpoint of another line, and the interface edge. The interface can be a line, circle/arc, ellipse/arc of ellipse. Note the order you've selected the endpoints.
  • Invoke the constraint. A dialog will appear asking for a ratio of indices of refraction n2/n1. n2 corresponds to the medium where the second selected endpoint's line resides, n1 is for the first line.
  • The endpoints will be made coincident (if needed), constrained onto the interface (if needed), and the Snell's law will become constrained.

Note that there are several helper constraints smart-added (point-on-object, coincident), and they can be deleted if they cause redundancy, or added manually if they were not added automatically. For the actual Snell's law constraint, the endpoints of lines must coincide and lie on the interface, otherwise the behavior is undefined.

Using polyline tool, it is possible to speedup drawing of rays of light. In this case, one can select two coincident endpoints by box selection.

Scripting

The constraints can be created from macros and from the python console by using the following function:

Sketch.addConstraint(Sketcher.Constraint('SnellsLaw',line1,pointpos1,line2,pointpos2,interface,n2byn1))

where:

Sketch is a sketch object
line1 and pointpos1 are two integers identifying the endpoint of the line in medium with refractive index of n1. line1 is the line's index in the sketch (the value, returned by Sketch.addGeometry), and pointpos1 should be 1 for start point and 2 for end point.
line2 and pointpos2 are the indexes specifying the endpoint of the second line (in medium n2)
n2byn1 is a floating-point number equal to the ratio of refractive indices n2/n1

Example:

import FreeCAD, Base, Part, Sketcher
Base.makeExampleCommandModel(FreeCAD.Data,FreeCAD.Data)

Other

Optional