Showing posts with label XAML. Show all posts
Showing posts with label XAML. Show all posts

Friday, June 5, 2009

Converting Campaign Cartographer Files to XAML Revisited

I have spent a lot of time working with Campaign Cartographer 3, Adobe Illustrator CS, Corel Draw 12, and Expression Blend 3. I finally came up with a solution on how to create a XAML file from Campaign Cartographer 3. CC3 will export a Windows Metafile (.emf). Adobe Illustrator CS sometimes has problems with these files. The solution is to open the .emf file in Corel Draw and save the result as an Adobe Illustrator file. This also offers the opportunity to convert all the text to curves. Adobe is very proprietary about fonts. Converting text to curves fools Adobe Illustrator and you have no more issues with fonts, at the cost of a bigger file.

After saving the file as an Adobe Illustrator file from Corel Draw you need to load the file into Adobe Illustrator and save it out again. Expression Blend 3 will not load the files created by Corel Draw. Adobe Illustrator adds "[converted]" to the file name when it saves the file.

Now you can load the converted Adobe Illustrator file directly into Expression Blend. You may need to do a few transformations to move and scale the map but it works.

If you export something small from CC3, then you must scale it up in Corel Draw. Full maps work without the need to scale it up.

Monday, May 4, 2009

Rounding Error Are an Important Consideration

My background includes a lot of mathematical analysis. I took a lot of numerical analysis at the University of Utah. Many more classes than were actually required because I enjoyed the combination of math and programming. In addition, I was a teaching assistant for the computational physics classes taught as part of the master's of instrumentation program of the physics department. My first job when I left the University of Utah was working on mathematical educational software. My assignment was part of a group creating a system that symbolically solved mathematical problems. I learned a lot. One of the challenges was checking the work of the system. I spent months doing nothing but math. After giving the system a problem it would generate an answer. Often the answer didn't match the solution printed in the book. We had to determine if the computer's answer was actually the correct answer. The system may not have simplified the answer, or not done some trigonometric substitution or maybe the answer in the book was wrong. It was a lot of work. That taught me the importance of mathematical simplification.

Years ago I worked on a respiratory therapy patient demographic and therapy management system. The company's core intellectual property was the set of equations modeling the treatment efficacy. The equations took the raw numbers provided by the medical instruments and the patient demography and created a meaningful human understandable diagnosis. My task was implementing those equations on a new system. I took one look at the equations and knew something was wrong with them. My background in numerical analysis told me curve fitting software created the equations. The curve fitting software mathematically matched the curve but did not simplify the equations. I convinced the professor that created the equations I could simplify them. For the next month I proved symbolically and numerically my simplification of the equations was correct. Imagine working on a month long math test. I proved the error analysis of my equations was significantly smaller than the original equations. More importantly, the resulting program ran more than ten times as fast.

What does all that have to do with XAML, hex grids, and user controls? You would think I would know better. I start working on making a hexagon a user control and what bites me? Rounding errors! I should have known that rounding errors would cause problems.

I started working on converting the shaded hexagon from my previous blog into a user control. I needed to work out the coordinates where to put each hexagon to create a hex grid. I created a column of hexagons. They looked good. I then added a second column, a third, and a fourth. Things looked great as you can see below.

Then I zoomed in a bit. Then I noticed that the hexagons did not always touch. I immediately knew the problem: rounding errors. You can see that below.

Hexagons tile the plane. Unfortunately you cannot tile them in XAML because you can only tile using simple rectangular patterns. I could use more precision. That would make the problem less likely to appear. The other option is just to make the hexagon grid as a series of polygons; each one calculated using as much accuracy as possible. You can see that below.

It looks like all is well with the hex grid. Until I made a simple change: I changed the stroke width to zero. You can see the result below.

Rounding errors are still visible. What does that mean? The short answer is you cannot expect to perfectly replicate a hexagon grid.

Monday, April 20, 2009

Converting Campaign Cartographer Files to XAML

ProFantasy created a program named Campaign Cartographer. The program allows the quick creation of very high quality maps for role playing type of games. I was working on a logo for the company I am trying to start. I wanted to create a Silverlight XAML file for the logo. I knew that Campaign Cartographer could quickly and easily create a piece of the Logo. Now for the challenges in getting that file into XAML. Campaign Cartographer has only a limited number of vector output options. I could easily create a bitmapped graphic in a variety of formats, but I wanted to stay in the vector space. The logical place to start is as a Windows Metafile (.wmf or .emf). I exported the metafile and tried using one of the freeway tools EMFtoXAML. This produces basically produces an empty file. EMFtoXAML does not support any of the drawing commands used by Campaign Cartographer. I then tried reading the .emf file into Adobe Illustrator CS 1 (the most recent version I own). The result is very low quality. My next step was Corel Draw 12 (the most recent version I own). Again, the result was awful. I gave up on .emf files. Now to try another vector format: AutoCad .dxf. Now the fun begins. The current release of Campaign Cartographer is version 3. On my system, Campaign Cartographer 3 crashes when trying to export a .dxf. I then fire up an old PC that has Campaign Cartographer version 2 installed. This program exported the DXF without problem. Then went back to Adobe Illustrator CS 1 to try and import the .dxf file. Again, Adobe Illustrator did not render the import usable. My next step was back to Corel Draw 12. Corel Draw 12 imports the .dxf. The result of the import into Corel Draw is not perfect. The import removes most but not all of the color. At least the outline is clean. Corel Draw can export an Adobe Illustrator file or .ai file. I installed the preview version of Expression Blend 3.0 from Microsoft. Expression Blend 3.0 is supposed to be able to import Adobe Illustrator files and create XAML. I fired up Expression Blend and created a Silverlight project. Expression Blend disables the menu options to import an Adobe Illustrator file when you create a Silverlight project. After fumbling around for a while I tried creating a WPF project in Expression Blend. That allows importing Adobe Illustrator files. I did some searching on the web, and nothing mentions you can only import Adobe Illustrator into WPF projects. I tried importing the Adobe Illustrator file created by Corel Draw 12. Expression Blend 3.0 Preview says it cannot understand the file and cannot import it. I then go back to Adobe Illustrator. I open up the file created by Corel Draw in Adobe Illustrator and save the file back as another .ai file. It is interesting to note the Adobe Illustrator's default name for the file adds the word "[Converted]" to the file name. I open Expression Blend 3.0 back up to my WPF project. Finally I can import the .ai file and the image appears in Expression Blend. The colors are gone, but at least I have the XAML file I can work with. I hope that someday ProFantasy will add the ability to export data directly into XAML.