Click here to Skip to main content
15,176,823 members
Articles / Desktop Programming / WPF
Posted 14 Jan 2011


38 bookmarked

WPF 3D Dome Creator

Rate me:
Please Sign up or sign in to vote.
4.96/5 (13 votes)
14 Jan 2011CPOL4 min read
WPF 3D Dome Creator using MVVM


I am an adult fan of LEGO and while working on my recent LEGO project, I had to build a dome made of LEGO bricks. After trying a couple of LEGO CAD applications, I still was having difficulty building the dome. Then I stumbled upon a LEGO dome creator application from a guy called Arthur Gugick. This application was developed using VB6 and it had some bugs like the form would be cleared once it was minimised. This application gives the height of the dome and every stud location and this is also how WPF 3D Dome Creator functions. However it was all 2D, one could not visualise how the dome will look like. So I contacted Arthur and asked him if it would be possible to send the source code. He was more than happy to provide it and I took that as a base for the WPF 3D Dome Creator.

About LDrawPartLib

LDrawPartLib was developed by me for LEGO CAD application similar to MLCad, which is still work in progress but the library is complete. LDrawPartLib uses the LDraw part library for the part geometry. LDraw is an open source standard of LEGO CAD application. They have a very active community and regularly update the library with new parts. I feel that discussing about LDrawPartLib here is beyond the scope of this article. However, if you are interested in how the LDrawPartLib parses and creates 3D LEGO part, you will have to go through the code, which had decent amount of comments and the documentation from LDraw. You can of course contact me anytime if you have something very specific.

About the Dome Creator

I have used this application as a way to learn more about WPF, 3D and the famed MVVM. This application is understanding for MVVM and WPF implementation should be done. I am open to your comments and ways to improve this application. Since this application uses only one part, all the required files are places in the “Parts” folder. If you want to use the LDraw parts library, you will need to download it from here and the place the path in the App.Config. Also ldconfig.ldr, which a part of LDraw, needs to placed in the application path as it has all the details about the part colors.

The application itself is very simple to use. Choose the type of dome that you would like to build, set the various parameters like the diameter and the height of the dome, the height is consider only when the dome type is not spherical. You can change the color of the dome by clicking on the color palette.

Points of Interest

First and foremost, it uses WPF 3D. I have used WPF 3D as I wanted to know what are its capabilities. It is a great 3D framework, however I seriously doubt that this will be used for developing games. There are many drawbacks, for one the framework does not come with built in support for creating basic figures like cubes, spheres, etc., secondly, as I learnt the hard way that drawing line on a 3D canvas can be a real pain. However, I later found out about how to draw lines by using 3DTools and a library from Charles Petzold. He has also written an excellent book on WPF 3D named 3D Programming for Windows. The former has a bug in the implementation, more on this here, and so I did not to use the line implementation of 3DTools. However, the TrackBall implementation of 3DTools is fantastic. The second one has some excellent features but the performance goes down considerably as the number parts with lines increases in my 3D scene. If you want to see the parts with lines, uncomment the following line:

public class Dome3DViewModel : DomeViewModel
	public void CalculateDomeValues() {
		int y = (currentHeight - noOfPlates) * (int)ScaleHeight;
		for (int i = 0; i < noOfPlates; i++, y += (int)ScaleHeight) {

			Point3D pt = new Point3D(x * ScaleWidth, y, z * ScaleWidth);

			PartColors color = 

			Part3D part = 
                new Part3D( PLATE_PART_CODE, 
                            PLATE_PART_CODE, color) { Position = pt };
			//part.ShowLines = true;


There is an excellent tutorial on WPF 3D here.

Secondly, as I mentioned before, the program by Arthur gives the height in LEGO plates at a particular location. I could have inserted the exact number of plates from the “ground” but this however would create a solid dome. Hence, I decided to add only “viewable” plates. This is achieved by finding which of its eight neighbours in the grid have the lowest height and then add only the difference between the lowest and the current height. In order to achieve this, I used the following:

List<int> neighbours = new List<int>();
// Fill all the values from the surrounding neighbours.
FillNeighbours(diameter, z, drCurrent, neighbours);
if (drPrev != null) FillNeighbours(dt.Rows.Count, z, drPrev, neighbours);
if (drNext != null) FillNeighbours(dt.Rows.Count, z, drNext, neighbours);

// Gets the lowest values on the top.

// no of viewable plates.
if (drNext == null || drPrev == null || z == 0 || z == (diameter - 1))
    noOfPlates = currentHeight; // if dome edges then show all the plates.
else if (currentHeight == neighbours[0] && currentHeight > 0)
    noOfPlates = 1; 	// If lowest neighbours is of the current height 
			// then add only one plate.
else // no need the show all the plate. Just show the 'viewable' plates.
    noOfPlates = currentHeight - neighbours[0];

Thirdly, I wanted to have access the one view model from other. To achieve this, I created a base class called ApplicationViewModel.

public abstract class ApplicationViewModel : ViewModelBase
	#region ctor
	protected ApplicationViewModel(MainWindowViewModel mainWindowModel) {
		MainWindowVM = mainWindowModel;

	#region Public Properties
	public MainWindowViewModel MainWindowVM { get; set; }

The constructor of this class takes MainWindowViewModel as a parameter. All the view models that need to talk to each other in the application derive from ApplicationViewModel.

public class ParametersViewModel : ApplicationViewModel
	#region ctor

	public ParametersViewModel(MainWindowViewModel mainWindowModel) 
            : base(mainWindowModel) {
		DomeDiameter = 12;
		DomeHeight = 16;

The MainWindowViewModel has properties for each of these models.

public class MainWindowViewModel : ViewModelBase
	#region ctor
	public MainWindowViewModel() {
		ParametersVM = new ParametersViewModel(this);
		Dome2DVM = new Dome2DViewModel(this);
		Dome3DVM = new Dome3DViewModel(this);
		ColorChooserVM = new ColorChooserViewModel(this);

	#region Public Properties
	public ParametersViewModel ParametersVM { get; set; }
	public Dome2DViewModel Dome2DVM { get; set; }
	public Dome3DViewModel Dome3DVM { get; set; }
	public ColorChooserViewModel ColorChooserVM { get; set; }


As a learner, I would love to know what you think about the way in which this application is developed. It would be great if the criticism is constructive.

Lastly, LEGO is a registered trademark of the LEGO Group, which does not sponsor, endorse, or authorize this article or this application. Visit the official Lego website at


  • 14th January, 2011: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Team Leader
India India
No Biography provided

Comments and Discussions

QuestionZooming 3d view Pin
ru4given24-Feb-11 19:08
Memberru4given24-Feb-11 19:08 
GeneralMy vote of 5 Pin
Ashley Davis18-Jan-11 2:08
MemberAshley Davis18-Jan-11 2:08 
GeneralAFOLs everywhere thank you Pin
coreymd17-Jan-11 13:45
Membercoreymd17-Jan-11 13:45 
GeneralRe: AFOLs everywhere thank you Pin
Muffadal17-Jan-11 21:53
MemberMuffadal17-Jan-11 21:53 
QuestionVery Nice Artical But......? Pin
Pritesh Aryan17-Jan-11 2:54
MemberPritesh Aryan17-Jan-11 2:54 
AnswerRe: Very Nice Artical But......? Pin
Muffadal17-Jan-11 19:24
MemberMuffadal17-Jan-11 19:24 
GeneralNot working ... Pin
Selvin17-Jan-11 1:09
MemberSelvin17-Jan-11 1:09 
GeneralRe: Not working ... Pin
Muffadal17-Jan-11 19:36
MemberMuffadal17-Jan-11 19:36 
GeneralTruly among the cool Pin
KenJohnson15-Jan-11 6:49
MemberKenJohnson15-Jan-11 6:49 
GeneralMy vote of 5 Pin
rcollina15-Jan-11 3:04
Memberrcollina15-Jan-11 3:04 
GeneralRe: My vote of 5 Pin
Muffadal15-Jan-11 4:41
MemberMuffadal15-Jan-11 4:41 
GeneralMy vote of 5 Pin
RaviRanjanKr14-Jan-11 20:03
professionalRaviRanjanKr14-Jan-11 20:03 
GeneralRe: My vote of 5 Pin
Muffadal15-Jan-11 4:38
MemberMuffadal15-Jan-11 4:38 
GeneralMy vote of 5 Pin
SledgeHammer0114-Jan-11 13:15
MemberSledgeHammer0114-Jan-11 13:15 
GeneralRe: My vote of 5 Pin
Muffadal15-Jan-11 4:37
MemberMuffadal15-Jan-11 4:37 
GeneralAce, so 5 from me Pin
Sacha Barber14-Jan-11 11:56
MemberSacha Barber14-Jan-11 11:56 
GeneralRe: Ace, so 5 from me Pin
Muffadal15-Jan-11 4:35
MemberMuffadal15-Jan-11 4:35 
GeneralRe: Ace, so 5 from me Pin
Sacha Barber15-Jan-11 6:27
MemberSacha Barber15-Jan-11 6:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.