The DM or similar algorithms is depicted in the attached figure. My question is, how to calculate lx and ly for every node? Is there any automatic solution if the simulated structure is complicated?
I suggest learning some computational geometry. Doing this reduces to figuring out where lines cross each other. The metal will likely be described by lines around its perimeter and your Yee grid cells are composed of other lines. If you write a nice function to calculate where lines cross each other, you will be able to figure this out.