ForceDirected Bug painting error

When I try to apply PerformLayout over a View which has almost 500 node, an exception is thrown as

‘maxValue’ must be greater than zero.
Parameter name: maxValue
Exception type: ArgumentOutOfRangeException

and stack trace revealed

at System.Random.Next(Int32 maxValue)
at Northwoods.Go.Layout.GoLayoutForceDirected.UpdatePositions()
at Northwoods.Go.Layout.GoLayoutForceDirected.PerformIterations(Int32 num)
at Northwoods.Go.Layout.GoLayoutForceDirected.LayoutClusters(Int32 level, Int32 maxiter)
at Northwoods.Go.Layout.GoLayoutForceDirected.LayoutClusters(Int32 level, Int32 maxiter)
at Northwoods.Go.Layout.GoLayoutForceDirected.LayoutClusters(Int32 level, Int32 maxiter)
at Northwoods.Go.Layout.GoLayoutForceDirected.LayoutClusters(Int32 level, Int32 maxiter)
at Northwoods.Go.Layout.GoLayoutForceDirected.LayoutClusters(Int32 level, Int32 maxiter)
at Northwoods.Go.Layout.GoLayoutForceDirected.LayoutClusters(Int32 level, Int32 maxiter)
at Northwoods.Go.Layout.GoLayoutForceDirected.LayoutClusters(Int32 level, Int32 maxiter)
at Northwoods.Go.Layout.GoLayoutForceDirected.PerformLayout()

then I knew there is something wrong with the Random number generator. generator is throwing exception as it never expects Max value which is negative.

I even tried to create a custom Random Number generator as
public class MyRandom : Random
{

        public override int Next(int maxValue)
        {
            if (maxValue < 0) maxValue = 28;
            return base.Next();
        }
        
    }</font>

and set to RandomNumberGenerator property of layout engine

goLayoutForceDirected1.RandomNumberGenerator = new MyRandom();

the former exception is somehow suppressed but I got another Stackoverflow exception.

I have no other choice except keeping PerformLayout method inside try catch block but which indeed is not a solution.

Hope to get it fixed or waiting for other solutions.

thanks

OK, looking at the code, the only way this could happen is if at least two of your nodes have zero Width and/or Height, and they happen to have exactly the same Center positions. Is that the case?

If so, your work-around isn’t correct, since the maxValue being passed is zero, not negative. I think your code should be

public override int Next(int maxValue) { return base.Next(maxValue <= 0 ? 1 : maxValue); }
Could you try this and see if it helps?

I’m unsure about how your (incorrect) work-around would be causing a stack overflow, but I’m guessing there’s a problem with the result of Random.Next being a potentially huge number, rather than a small non-negative integer less than the argument.

Hi, thanks for your quick reply,

according to your lines.

OK, looking at the code, the only way this could happen is if at least
two of your nodes have zero Width and/or Height, and they happen to
have exactly the same Center positions. Is that the case?

I have no idea if that could be the reason.

but, as you said max value can not be negative , the value which I got when it calls to Random.Next is -2147483648.(which is pretty big) so neither of these solution worked. I still have overflow exception when I run your code.

I know there can be other workaround to solve the issue
and if this is the case that you mentioned in first paragraph, please help me find the solution.
Thank you

Are you saying that the value passed to Random.Next was -2147483648?

Or that -2147483648 was the value returned by the call to Next, which had been passed a positive integer?

Regarding the stack overflow, could you send or post a copy of the interesting part of the stack trace?

Hi walter,
Are you saying that the value passed to Random.Next was -2147483648?
yes exactly. that value is passed as an argument to Random.Next(int Max).

and the stack trace when used your workaround looks like

System.OverflowException: Overflow error.
at System.Drawing.Graphics.CheckErrorStatus(Int32 status)
at System.Drawing.Graphics.DrawPath(Pen pen, GraphicsPath path)
at Northwoods.Go.GoShape.DrawPath(Graphics g, GoView view, Pen pen, Brush brush, GraphicsPath path)
at Northwoods.Go.GoStroke.Paint(Graphics g, GoView view)
at Northwoods.Go.GoLayer.Paint(Graphics g, GoView view, RectangleF clipRect)
at Northwoods.Go.GoView.PaintObjects(Boolean doc, Boolean view, Graphics g, RectangleF clipRect)
at xxx.Diagram.GraphViewer.PaintObjects(Boolean doc, Boolean view, Graphics g, RectangleF clipRect) in E:\Diagram\GraphViewer.cs:line 134
at Northwoods.Go.GoView.PaintView(Graphics g, RectangleF clipRect)
at Northwoods.Go.GoView.onPaintCanvas(PaintEventArgs evt)
at Northwoods.Go.GoView.OnPaint(PaintEventArgs evt)

That stack trace shows a problem in GDI+, not in GoLayoutForceDirected. So I believe the amended work-around using a more careful version of Random.Next seems to be working OK.

I don’t know why painting a GoStroke (probably a GoLink, but I can’t tell) would cause this exception. What version of GoDiagram are you running?

Hi walter,
I am using 3.0.3 version of GoDiagram.

We’ve never seen this Overflow exception in DrawPath, and Google only finds one or maybe 2 instances of others asking about it in other .NET apps. So… it’s rare, to say the least. (It’s not listed in the MS .NET docs as a possible exception from DrawPath.)

What are you doing in xxx.Diagram.GraphViewer.PaintObjects?

Hi Jake
Sorry for the late reply, I have been using PaintObjects for drawing animating circle over a node when user wants to search a node. And Is any other way to do the same ??

Thanks

I’m not sure how you get animation out of a single call to PaintObjects, unless you keep painting and don’t return… ?

It’s better to use a timer to do animation. See the AnimatedBasicApp sample.

Let us know if removing your PaintObjects fixes the exception.

Hi,
I’ll let you know when I suppress drawing animating circle,but as you said I have set timer to draw an animating circle over a node.
Thanks

When you do any painting, please try to use the static methods on GoShape named “Draw…”.

They are implemented to avoid some of the GDI+ bugs that we have discovered over the years, including what I am guessing is one that you have encountered.

Thanks walter, It can be due to some nasty bugs inside framework drawing library.I’ll tell you when I am done with the way you have said.

regards