6

- Building a CAD program in WPF:

I want to build a CAD program that will have 10000 LINE objects at a time. I'm using LineGeomery class for drawing lines that are added to a Canvas. I have implemented Zoom and Pan and the performance is great so far.

Only one major disappointment:

The Thickness of the lines gets scaled while zooming. I have tried to Bind the Thickness property of the lines to a factor to keep them unchanged, This works but reduces the performance dramatically while zooming. Clearing and drawing new lines with new thickness on MouseWheel is out of the question as well. This one too reduces performance and is not practical in the current method.

- Now what solutions I have?

  • Stick with the current method and ignore the change in Thickness
  • Do the whole job in GDI+
  • Host GDI in WPF
  • Use WPF Viewport3D (Will the LineThickness be invariant there?)

- Other solutions?

What other paths you would take. I'm new to WPF and programming and I'm eager to learn.

UPDATE:

This is the way I'm doing it right now. I draw 3000 Lines on the Visual Layer using Pen an Brushes. Then on MouseWheel event I redraw all the Lines with the updated Thickness. Also I don't show the rest of the Lines to the user until he zooms so I only create 3000 out of 10000 Lines in each MouseWheel event.

Vahid
  • 5,144
  • 13
  • 70
  • 146
  • 1
    Did you also consider [drawing in the Visual Layer](http://msdn.microsoft.com/en-us/library/ms742254(v=vs.110).aspx)? – Clemens Mar 21 '14 at 13:06
  • @Clemens Yes, actually I have done it and it has improved the things a little bit. I've bound the inverse of the scale to line thickness. And I'm redrawing the lines on mousewheel. I tried the path method you suggested but it also lacked performance. Using Visual Layer, now I'm able to zoom/redraw 2000-3000 lines. Do you have any suggestions? Should I use Viewport3D? Will I have the same problem with thickness there too? – Vahid Mar 21 '14 at 13:12
  • @Clemens My last resort would be to redraw only the parts that I need and clear the parts that are out of the view I guess. But before I resort to this I want to check all the possible ways. – Vahid Mar 21 '14 at 13:14
  • 1
    Using a DrawingVisual and drawing only the visual parts sounds like a sensible approach. – Clemens Mar 21 '14 at 13:29

2 Answers2

4

Instead of using Line objects, you could draw your lines by Path objects. Here is an answer https://stackoverflow.com/a/15323221/1305119

Community
  • 1
  • 1
Suresh Kumar Veluswamy
  • 4,193
  • 2
  • 20
  • 35
  • Using Path objects decreases performance when number of objects are 1000+. – Vahid Mar 21 '14 at 07:35
  • 1
    Here is one more example... You can draw your own shapes by overriding DefiningGeometry property http://khason.net/blog/how-to-high-performance-graphics-in-wpf/. But Zooming might be a bit tricky – Suresh Kumar Veluswamy Mar 21 '14 at 08:42
  • This also doesn't support Zoom :( – Vahid Mar 21 '14 at 08:52
  • Have you tried zoomable canvas... http://blogs.msdn.com/b/kaelr/archive/2010/08/11/zoomableapplication2-a-million-items.aspx You can inverse the stoke thickness relative to the scaling – Suresh Kumar Veluswamy Mar 21 '14 at 09:44
2

Next to hosting a winforms element inside WPF, I would also implement partial rendering on the zooming feature, e.g. when you zoom in everything that is not visible should not be calculated as well!

woutervs
  • 1,500
  • 12
  • 28
  • Still in WPF 3000 lines represent 3000+ objects. WPF is known to be slower in graphics especially when it needs to recalculate in short periods of time. There is until now, still no solution for that particular issue. – woutervs Mar 24 '14 at 11:26
  • What would be your suggestion? Which of the 4 ways should I go? – Vahid Mar 24 '14 at 11:28
  • 1
    If there is enough time I'd try each of the for ways and do a performance test. Otherwise I'd go for WPF and host GDI inside a container. – woutervs Mar 24 '14 at 11:35
  • The problem is I'm not even sure if GDI+ will be faster or not to draw 10000+ lines. Do you have experience regarding this? – Vahid Mar 24 '14 at 11:41
  • 1
    http://www.dotnetfunda.com/interviews/exclusive/show/970/what-is-the-need-of-wpf-when-we-had-gdi-gdi-and-directx a comparison about drawing techniques. Basically to sum it up, GDI(+) is slowest because it doesn't utilize hardware acceleration, DirectX is fastest because it uses hardware acceleration and WPF is a layer above DirectX to enable (Grids, Flows, InputBoxes,...) And due to that WPF is slower because it makes objects more complex and bigger in size. There for the conclusion would be to use DirectX, however this one will take some time to learn. – woutervs Mar 24 '14 at 11:46
  • Thanks woutervs for your interest in this. I really appreciate it, I actually tried to draw 1000 Lines using GDI+, it seems that it lags when it is drawing. by lag I mean I actually see the process of drawing. It doesn't happen in a blink of an eye like WPF, am I missing something here? – Vahid Mar 24 '14 at 11:51
  • http://www.codeproject.com/Articles/113991/Using-Direct-D-with-WPF tutorial on how to start working with DirectX 2D in WPF – woutervs Mar 24 '14 at 11:51