Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ray Trace Request 2

Status
Not open for further replies.

DennisD

Mechanical
Jan 3, 2000
42
0
0
US
Greetings fellow SWX heavies! I need some help. I'm trying to expedite a ray tracing for a crude optical analysis. I've already searched these forums for previous threads and FAQs but have come up blank. I've also looked at the SWX Partners and the subscriber's knowledge base and model library hoping to find a free or 30-day version of some ray tracing routine, a macro, a techinique, anything, but to no avail.

In a SWX sketch I have a profile of a transparent part. I want to draw a ray from my source to the first surface of the transparency. I want to draw the appropriate trace of this (these) ray(s) all the way through the transparency and out the other side. I figure some kind of macro or API may be in order but I'm not skilled in either so I need your help.

The governing equation is Snell's Law: n1*sin(theta_1) = n2*sin(theta_2) where theta is the angle of incidence (which is measured from the surface normal at the point of intersection with the ray). The indexes of refraction of the two materials are n1 and n2. I am using air and acrylic so n1=1.0003 and n2=1.491. (It would be nice if n1 and n2 were user-inputs so this could easily handle other material combinations.)

If I were intersecting a flat this would be no big deal, but I am intersecting surfaces with differing shapes. A good chunk (technical term) of the difficulty will come from determining the normal. Probably this routine would draw a construction line for the normal and reference that for the incoming theta_1 and outgoing theta_2.

Who is up to the challenge? I'd love the help.

Thanks in advance,

- - -DennisD
 
Replies continue below

Recommended for you

DennisD,

I have done ray tracing off of mirrors, on a scanner system. My scanned beam was able to follow the mirrors from position to position. This take a lot of geometry, and there are a few gotchas I can tell you about.

I haven't tried refraction. This should be simple on a 2D model, assuming planar and spherical optics. Melles Griot provides coordinates across their aspheric lenses, but finding normals will be a challenge. Finding the normal to a spherical surface in a 3D model could be complicated too.

I think modeling a telescope in 3D should be fairly simple.

JHG
 
macPT,
Thanks for the link to nhcad. However, theirs is more of a PhotoWorks type of reflection appearance. I am actually looking for a refractive ray trace through a lens.

optech,
You are correct about the inherent difficulty associated with surface normals. I cannot use any catalog lenses such as Melles Griot, but that is a good thought.

I have done this manually by creating an incident ray to the curve created by a section of the lens. I had to create a line at this point and make it tangent to the curve of the lens profile in this sketch, then make a construction line normal to this line. I want to be able to expedite this operation (a.k.a. automate it) and include the computed output ray using Snell's Law.

This should be a good and fun challenge. I know there are quite a few API and macro gurus out there. I'm requesting your help, folks.

Thanks,

DennisD
 
I'm really looking for some help with this. I know this isn't something that very many need, but I think solving this problem would have many applications that would benefit us all (such as a technique for finding the normal of an edge at a given point).

Can someone point me to a good way to learn how to do API, macros or VBA. I'll figure out the geometry and mathematical relationships. I just don't know how to program this in some "language" to automate it.

Thanks in advance,

- - -DennisD
 
I think the best way to learn API for SW is:
1 - take a look at the API help in SW for code and examples
2 - take a look at the free stuff on the internet (there is a lot of nice people that share their code). Examples
solidworks site
FAQ folder from this site
3 - take a look at the VBA editor help (you access this editor when you edit a macro in SW, Excel,...)
4 - start to make your own code and experiments. Make it simple at first

By the way, take also a look at these sites:
mabe you find there someone or something that helps.

Regards
 
I don't know much about ray tracing, but the coding part doesn't seem unreasonable. Creating a user form to enter data is really easy, but some more details would help:

1) From the sounds of your posts, you are modelling this situation in 2D. How do you want to enter the position/orientation of the source? You could use features (a face and axis) or sketch entities (sketch point and line) to define them or you could manually enter the data in a form.

2) What should the output be? Do you need to determine both reflection and refraction? Should these outputs be sent to a file, displayed on a form, or do you want to create sketch lines to indicate them?

3) Does the ray travel through multiple lenses or does the calculation only have to be done once?
 
Thanks for the suggestions, but these links haven't yielded what I'm looking for.

I am trying to draw the path of a ray of light from its source and all the way through a transparency. The transparency is some kind of solid, either created in SWX or imported. I'll draw the ray from the source to intersect the first surface of the trasparency. I'll then create a plane (if necessary) that includes this ray and on this plane open a sketch. In this sketch I'll convert the edges of the transparency that represent the incoming and exiting surfaces. I currently manually create a construction/center line perpendicular to the first surface at the point of intersection with the incident ray. Then I manually compute the refracted angle and draw the ray inside the transparency at this angle. I do the same thing as the ray exits the transparency.

This is laborious and would dramatically benefit from some form of automation, i.e., API, VBA/VB, or macro. Also I need to do this for multiple rays emanating from the source. Perhaps such a routine could be invoked after I have drawn the (multiple) incident ray(s), opened a sketch and converted the edges of the transparency. Then the routine could be invoked after picking the incident ray and the intersecting surface. The only information needed for the computation of the refracted ray is the indices of refraction for the two mediums. The routine would need to be capable of creating the normal at the point of intersection.

I hope this is a clearer explanation. Now, can some of you help? Pretty please.

- - -DennisD
 
DennisD,

My mirror model computes reflection automatically, using SolidWorks constraints. To do refraction, you need the equation editor.

You will have to construct the geometry manually, but you should be able to get your angles automatically.

JHG
 
This sounds like an interesting problem. I may be able to get you started with the code, but it would help to eliminate some unknowns.

The main challenge is creating a sketch line to represent the normal vector at the point where the ray intersects the transparency. When you convert the edges of the transparency to sketch entities, don't you get a spline? If the transparency is an irregular shape then you must get splines, but I notice that you don't mention this. You can't create a perpendicular relation to a spline, only a tangent relation at the endpoints. Does this mean that you are trimming the spline to the intersection point, creating a sketch line tangent to the spline and then creating a sketch line which is perpendicular to the first sketch line? I don't see any other way of getting a sketch line perpendicular to a spline at a point.

You draw the ray from the source to the first surface of the transparency. Coding this action directly isn't really easy because there is no convenient way of telling SWX in VBA to "increase the length of this sketch segment until it reaches another sketch segment". I think the best way to code this is to add a sketch point somewhere in space. It doens't matter where it is as long as it has no relations. The it is easy to use ModelDoc.SketchAddConstraints ("sgATINTERSECT") to move the point to the intersection of the two sketch segments. The ray and transparency sketch segments obviously need to be fixed so that adding this relation causes the point to move rather than the sketch segments. This point can then be used as a reference for the creation of the remaining geometry.

Do you consistently set up the orientation of the source and the transparency in a particular fashion? For example, is you sketch plane always one of the primary planes or is it totally random? The reason this is important is that I think the best way to do this is to mimic what is being done manually - create sketch geometry and then add the required relations. It would be easier for the creation of the sketch geometry if there was a consistent orientation of the model with respect to the source.
 
Stoker,
Thanks for the offer to help!! Your comments are correct in that I have to use a tangent constraint to the spline and then create a normal to that tangent.

Here is what I think is reasonable to do from the user standpoint:
1. Draw a set of rays that eminate from the source and go through the first surface. Originally I was thinking of stopping these at the first surface, but going through will help with the proper direction of the refracted ray.
2. In the plane of these initial rays open a new sketch.
3. Convert the edges of the transparency.
4. Convert the incident rays. It might be good to make these construction lines.
5. Put points at the intersection of the incident rays and the first surface of the transparency.
6. Pick a point, its ray and the line of the first surface and run the ray trace routine.

I would like the routine to then:
a. Create a unit construction line at the point tangent to the surface line.
b. Create a unit construction line normal to this tangency. It would probably be good for this line to have a midpoint constraint at the point of intersection.
c. Prompt the user for the indeces of refraction: N1 for incident material, N2 for refracting material.
d. Determine the angle of the incident ray to the surface normal.
e. Compute and draw the refracted ray using Snell's Law: sin(Theta-2)=(N2/N1)*sin(Theta-1). This refracted ray should start at the point of intersection and go into the transparency (it can/should be a unit length since all that is really needed is the direction). This ray is angled onto the same side of the surface normal as the extension of the incident ray. This is why I think it would be advisable for the incident ray to extend through the first surface.
f. Rerun this routine for the other incident rays.
g. Make another sketch of lines for these rays going through the second surface (the exiting rays) and re-run this routine as needed.
h. Make DennisD happy! [bigsmile]

I'd be delighted to work with you directly on this and pass files. Let me know.

Thanks for your help!!

- - -DennisD
 
This code should take care of A, B and part of H. It requires you to preselect the ray, intersection point and the transparency sketch segment. The transparency segment must be trimmed to the intersection point if it is a spline.

It does not matter if the ray extends past the transparency. The code only uses the start point and the intersection point, so the endpoint is not important. You can use "run to cursor" and "step into" (F8) in VBA to run the routine line by line so you can see how the macro works. Please try it to verify that it is working ok.
The one important thing is that the ray, intersection point and transparency must be selected in order. Any other order will not work.

If you consistently set up the geometry in a specific way it might be possible to get rid of some of these if statements. The code uses the coordinates of the enpoints to create the geometry, so the direction in which entities are created matters. If there are no tests for the orientation of the entities you will wind up with incorrect results.

What remains is to calculate the angle between the perpendicular line and the ray so that the refracted line can be created. Adding a form to input the constants is trivial and can be done at the end.

You will have to paste this code into a blank macro. It may be necessary to add the _ character at the end of each line that wraps to the next, since VBA treats each line as a separate command unless you use _ to carry on to the next line.
*******************************************************

Option Explicit

Dim swApp As Object
Dim ActiveDoc As Object
Dim ActiveSketch As Object
Dim SelMgr As Object

' These objects are pre-selected by the user prior to running the macro
Dim SelRayLine As Object ' ray sketch segment
Dim SelIntPoint As Object ' intersection point
Dim SelTransSeg As Object ' transparency sketch segment

' These objects are used to create the new geometry
Dim TangentLine As Object ' line created tangent to transparency
Dim PerpLine As Object ' line created perpedicular to transparency
Dim SelRayMidpointX As Variant ' X coord of SelRayLine midpoint
Dim SelRayMidpointY As Variant ' Y coord of SelRayLine midpoint
Dim PerpLineOppLength, PerpLineAdjLength As Long 'Perpendicular line X/Y lengths

'Return values from SWX functions
Dim Msg As String
Dim LongVal As Long

Dim PerpLineAngle As Long
Dim RayAngle As Long

'Constants
Const Pi = 3.14159265
Const swDocPART = 1
Const swMbInformation = 2
Const swMbOk = 2

Function AngleInRads(AngleInDegrees)
AngleInRads = AngleInDegrees * Pi / 180
End Function

Function AngleInDegrees(AngleInRadians)
AngleInDegrees = AngleInRadians * 180 / Pi
End Function

Sub main()

Set swApp = CreateObject("SldWorks.Application")
Set ActiveDoc = swApp.ActiveDoc
Set ActiveSketch = ActiveDoc.GetActiveSketch2

If ActiveSketch Is Nothing Then
Msg = "A sketch must be active to use this command!"
LongVal = swApp.SendMsgToUser2(Msg, swMbInformation, swMbOk)
End
End If

Set SelMgr = ActiveDoc.SelectionManager
Set SelRayLine = SelMgr.GetSelectedObject2(1)
Set SelIntPoint = SelMgr.GetSelectedObject2(2)
Set SelTransSeg = SelMgr.GetSelectedObject2(3)

ActiveDoc.SetAddToDB (True)

' Create line tangent to transparency
Set TangentLine = ActiveDoc.CreateLine2(SelIntPoint.X, SelIntPoint.Y, SelIntPoint.z, SelIntPoint.X, SelIntPoint.Y - (0.25 * SelRayLine.GetLength), SelIntPoint.z)
ActiveDoc.sketchconstraintsdel 0, "sgVERTICAL"
TangentLine.ConstructionGeometry = True

If (SelTransSeg.Select2(True, 0)) Then
ActiveDoc.SketchAddConstraints ("sgTANGENT")
Else
Msg = "Error constructing line tangent to transparency!"
LongVal = swApp.SendMsgToUser2(Msg, swMbInformation, swMbOk)
End
End If

' Create line perpendicular to transparency
If (SelIntPoint.X > SelRayLine.GetStartPoint2.X) Then
SelRayMidpointX = SelIntPoint.X - 0.25 * (SelIntPoint.X - SelRayLine.GetStartPoint2.X)
Else
SelRayMidpointX = SelRayLine.GetStartPoint2.X - 0.25 * (SelRayLine.GetStartPoint2.X - SelIntPoint.X)
End If

If (SelIntPoint.Y > SelRayLine.GetStartPoint2.Y) Then
SelRayMidpointY = SelIntPoint.Y - 0.25 * (SelIntPoint.Y - SelRayLine.GetStartPoint2.Y)
Else
SelRayMidpointY = SelRayLine.GetStartPoint2.Y - 0.25 * (SelRayLine.GetStartPoint2.Y - SelIntPoint.Y)
End If

Set PerpLine = ActiveDoc.CreateLine2(SelIntPoint.X, SelIntPoint.Y, 0, SelRayMidpointX, SelRayMidpointY, 0)

If (TangentLine.Select2(True, 0)) Then
ActiveDoc.SketchAddConstraints ("sgPERPENDICULAR")
Else
Msg = "Error constructing line perpendicular to transparency!"
LongVal = swApp.SendMsgToUser2(Msg, swMbInformation, swMbOk)
End
End If

End Sub


 
If you just want raytracing, try the OSLO LT download:
Might be easier for optics.
This is for designing lenses and mirrors.
I tried (manually) in SW-sketch, but this will only give a direction, not the intensity of a ray of light. The Oslo programme allows to show the intensity of light also.
(worked on a lamp system) Morten K. Thillemann
 
Thanks, Stoker, for the macro. I'll let you know when I
run it how well it suits my needs.

Thanks, MortenDK, for the link to OSLO. I've been downloading their technical stuff since I went there from your link. Perhaps I need to use a different search engine, because whatever I used didn't bring up this one. This has been a big help.

- - -DennisD
 
Status
Not open for further replies.
Back
Top