Using the FreeCAD Python modules to create constrained SVG layouts

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
seands
Posts: 2
Joined: Thu Apr 06, 2017 4:13 pm

Using the FreeCAD Python modules to create constrained SVG layouts

Postby seands » Thu Apr 06, 2017 4:44 pm

Hi all, this is a long post but I think I need to describe my story to get the right kind of help - please bear with me!

I recently started to use FreeCAD to do some 3D modelling at work, and it's excellent - great job! At the same time, I realised how powerful FreeCAD could be for solving optical layouts based on a series of constraints.

I'm a physicist working with optics, and when I want to draw an SVG diagram of an optical layout, I have to load up Inkscape and move lots of things around. If I want to move a mirror in an existing layout, I also have to move the light beams that I've drawn with respect to that mirror, and if I change an angle I also have to redraw the light that "reflects" off of other mirrors. Here's the sort of thing I draw:

Image

My idea to solve this issue is as follows: I want to be able to programmatically define the relationship between mirrors and other optical components, and the links (light!) between them, and have a program draw the SVG diagram for me. For instance, a mirror at some angle will reflect its ingoing light at twice that angle, so a program can easily calculate this. I want to allow a user to define what optics there are, and how they link together (e.g. "mirror A is separated from mirror B by 50cm, and they are oriented at 45 degrees with respect to one another"), and have the program draw what it looks like in 2D.

However...

...Initially I thought I could just have a user-defined angle associated with each optic, and a position, and an angle of incidence for a "start" node, like a laser. Then I thought it would be straightforward to just propagate the light beam from laser towards the first optic, calculating the angle it reflects, then propagating the light in turn to the next optic, and so on, recursively. This is actually what I did - I made a Python program to do this, and got it quite polished. At that point, though, I realised that this approach does not work for layouts with cycles (i.e. when the relation between mirror inputs/outputs is described by a cyclic graph). This occurs if you try to reflect light from a mirror back towards that same mirror via some other mirrors. In this case, my program recursively lays out each optic until it gets to the link between the last optic and the first, and then it realises it can't rotate the first optic because it is already constrained by an earlier link. This results in light that doesn't reflect at the right angle:

Image

How can the program solve this system's angles when the angle of one is dependent upon the angle of the others? With a constraint solver, like the one in FreeCAD!

After a lot of searching I found an academic paper which describes an approach to solve angular cyclic graphs by breaking the graph of connected nodes down into "clusters", identifying what "type" they are, then reducing the total number of clusters by merging adjacent ones until there is only one big cluster. Its authors even made an example program in Python. I thought this was ideal, but the program is an absolute monster, with a lot of early Python libraries which are now outdated, and a lot of undocumented code.

I think FreeCAD can do what I want: it can solve systems of angular constraints. My question is: can anyone with more experience using/developing FreeCAD, having read the above description of what I'd like to do, give me some insight into how easy or difficult it would be to use FreeCAD to solve this sort of problem?

Thanks for reading!
User avatar
microelly2
Posts: 4255
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: Using the FreeCAD Python modules to create constrained SVG layouts

Postby microelly2 » Thu Apr 06, 2017 8:22 pm

Hallo and welcome to the forum,
reading (and understand) the paper will take some time :roll:
but when I see your example my first question is: are your systems 2D?
In this case I would try the sketcher.
there you can define constraints and you can use expressions too.
abdullah
Posts: 3119
Joined: Sun May 04, 2014 3:16 pm

Re: Using the FreeCAD Python modules to create constrained SVG layouts

Postby abdullah » Thu Apr 06, 2017 9:47 pm

I am not sure I understand the whole problem, but it seems to me that you have a lot of specialized work to do. Therefore I am not sure a generic tool will suit you well. If any, the sketcher is the obvious answer. I am not sure if you want to use the sketcher to only solve the mirror cyclic problem or the whole.

An approach could be: You have a nice diagram like the one above in your post. Every part of this that is self-contained regarding beams-mirrors is an sketch. In the sketch you use lines for beams and lines for mirrors. When you programatically load a sketch, you keep track which geometry is a beam and which is a mirror (geoid=0 beam, geoid=1 mirror). You solve it (you can use expressions, you also have Snell Law constraint in case it is useful for you). Then you read the result and map back your beams a mirror with the right orientation/position of the lines after solving.

If this is really wrong it proves I did not understand ;)
triplus
Posts: 8364
Joined: Mon Dec 12, 2011 4:45 pm

Re: Using the FreeCAD Python modules to create constrained SVG layouts

Postby triplus » Fri Apr 07, 2017 12:54 pm

Hi @seands
At that point, though, I realised that this approach does not work for layouts with cycles (i.e. when the relation between mirror inputs/outputs is described by a cyclic graph). This occurs if you try to reflect light from a mirror back towards that same mirror via some other mirrors. In this case, my program recursively lays out each optic until it gets to the link between the last optic and the first, and then it realises it can't rotate the first optic because it is already constrained by an earlier link.
Based on my knowledge currently FreeCAD works in the same way. That is there is DAG and circular dependency can be introduced. If it is introduced it has to be removed after or things stop to work.
seands
Posts: 2
Joined: Thu Apr 06, 2017 4:13 pm

Re: Using the FreeCAD Python modules to create constrained SVG layouts

Postby seands » Fri Apr 07, 2017 2:08 pm

Hi all, thank you for taking the time to read and reply.
microelly2 wrote:but when I see your example my first question is: are your systems 2D?
In this case I would try the sketcher.
there you can define constraints and you can use expressions too.
Yup, I currently only care about 2D (though having the option to move to 3D in the future would be interesting!). I guess the constraint solver in the sketcher is the one I'd be interested in using, since that's 2D. Do you know of some examples of using the 2D constraint solver with Python?
abdullah wrote:I am not sure I understand the whole problem, but it seems to me that you have a lot of specialized work to do. Therefore I am not sure a generic tool will suit you well. If any, the sketcher is the obvious answer. I am not sure if you want to use the sketcher to only solve the mirror cyclic problem or the whole.
As I said in my first post, I have been trying to implement my own algorithm to solve this problem but it's becoming clear to me that this is actually quite a difficult problem to solve, and that the use of some existing constraint solver like that of FreeCAD's would be better. I hope to be able to use FreeCAD as a dependency for my program, and call out to its solver to calculate the positions of the optics, then have my program draw the results. That's the ideal scenario, if of course FreeCAD can actually solve the sort of problems I'm trying to solve (i.e. the cyclic problem). The ability to model refraction (Snell's law) is really nice, so I hope FreeCAD can work.
triplus wrote:Based on my knowledge currently FreeCAD works in the same way. That is there is DAG and circular dependency can be introduced. If it is introduced it has to be removed after or things stop to work.
Argh, that's unfortunate. To be clear, what do you mean by "DAG"? Directed angular graph?

I don't know how open the developers are to this sort of thing, but the solution to solve systems of angular dependencies proposed in that academic paper seems quite neat. Perhaps one day the FreeCAD solver could also handle this sort of thing?
ickby
Posts: 2880
Joined: Wed Oct 05, 2011 7:36 am

Re: Using the FreeCAD Python modules to create constrained SVG layouts

Postby ickby » Fri Apr 07, 2017 2:13 pm

The cylcic graph problem mentioned by Triplus is only true for the document object graph. The sketcher and its solver does handle cyclic constraint definitions, that is what he is there for. So your use case should easily work. Most likely you would need to be carefull with starting positions, meaning let the user provide a "close to final" layout already, as otherwise you will run in problems with the many possible solutions.

However, the sketcher python interface, even if fully functional, is not the simpleast one. So it may take some time to get into it.
User avatar
microelly2
Posts: 4255
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: Using the FreeCAD Python modules to create constrained SVG layouts

Postby microelly2 » Fri Apr 07, 2017 2:27 pm

ickby wrote:
However, the sketcher python interface, even if fully functional, is not the simpleast one. So it may take some time to get into it.
Are there some tutorial or non trivial examples?

At the moment I work the try and error method to get results.
here my example https://github.com/microelly2/freecad-n ... shoerib.py