The ‘Show on Google Earth’ O-Calc Pro Plugin Explained

Home » Plugin development » The ‘Show on Google Earth’ O-Calc Pro Plugin Explained
Plugin development No Comments

As a more advanced example of an O-Calc Pro plugin, this article describes the ‘Show on Google Earth’ Plugin. First to understand how this particular integration with Google Earth works from a user perspective, have a look at the following video.

The ‘Show on Google Earth’ plugin is a Menu Item style plugin and has been placed within the File drop down menu in the O-Calc Pro interface. The ‘Show on Google Earth’ menu pick also has four sub menu items called ‘Show Marker’, ‘Render Structure’, ‘Batch Create Index’, and ‘Help’. The code snippets below show some of this house keeping:

  1. #region HOUSEKEEPING
  2. PPL_Lib.PPLMain cPPLMain = null;
  3. public PLUGIN_TYPE Type
  4. {
  5. get
  6. {
  7. return PLUGIN_TYPE.MENU_ITEM;
  8. }
  9. }
  10. public String Name
  11. {
  12. get
  13. {
  14. return "Show on Google Earth";
  15. }
  16. }
  17. public String Description
  18. {
  19. get
  20. {
  21. return "Show on Google Earth";
  22. }
  23. }public void AddToMenu(PPL_Lib.PPLMain pPPLMain, System.Windows.Forms.ToolStrip pToolStrip)
  24. {
  25. cPPLMain = pPPLMain;
  26. cShowOnGoogleEarthButton = new ToolStripMenuItem("Show on Google Earth");
  27. int itemindex = 0;
  28. System.Diagnostics.Debug.Assert(pToolStrip.Items[itemindex] is ToolStripDropDownButton);
  29. if (pToolStrip.Items[itemindex] is ToolStripDropDownButton)
  30. {
  31. ToolStripDropDownButton tsb = pToolStrip.Items[itemindex] as ToolStripDropDownButton;
  32. System.Diagnostics.Debug.Assert(tsb.Text == "&File");
  33. tsb.DropDownItems.Insert(tsb.DropDownItems.Count - 1, cShowOnGoogleEarthButton);
  34. tsb.DropDownItems.Insert(tsb.DropDownItems.Count - 1, new ToolStripSeparator());
  35. tsb.DropDownOpened += tsb_DropDownOpened;
  36. }
  37. cShowMarkerButton = new ToolStripMenuItem("Show Marker");
  38. cRenderStructureButton = new ToolStripMenuItem("Render Structure");
  39. cCreateIndexeButton = new ToolStripMenuItem("Batch Create Index");
  40. cHelpButton = new ToolStripMenuItem("Help");
  41. cShowOnGoogleEarthButton.DropDownItems.Add(cShowMarkerButton);
  42. cShowMarkerButton.Click += cShowMarkerButton_Click;
  43. cShowOnGoogleEarthButton.DropDownItems.Add(cRenderStructureButton);
  44. cRenderStructureButton.Click += cRenderStructureButton_Click;
  45. cShowOnGoogleEarthButton.DropDownItems.Add(new ToolStripSeparator());
  46. cShowOnGoogleEarthButton.DropDownItems.Add(cCreateIndexeButton);
  47. cCreateIndexeButton.Click += cCreateIndexeButton_Click;
  48. cShowOnGoogleEarthButton.DropDownItems.Add(cHelpButton);
  49. cHelpButton.Click += cHelpButton_Click;
  50. }

When the ‘Render Structure’ submenu option is clicked, the plugin makes a call to the private method “ExportToCollada()” that has the principle function to create a KML file that gets sent to the Google Earth application. It does this by accomplishing three major tasks:

  1. It uses the PPLMain method GetMainStructure().GetValueAndConvertToString(“Pole Number”) to get the pole number attribute of the pole to be used for the name of KML file.
  2. It obtains the Latitude and Longitude of the pole (provided they are not set to zero) and adds this coordinate pair to the KML file. It gets the Lat/Long values using the PoleLocation members variables obtained with the PPLMain.GetMaintStructure() method.
  3. And finally, it creates a DAE (digital asset exchange) file, which is a 3D interchange file format based on the COLLADA XML schema (https://www.khronos.org/collada/ gives more information on the COLLADA specifications). The plugin has two privately defined methods BuildMesh() and BoundingMeshes() that creates a 3D mesh of the O-Calc Pro model geometry. The BuildMesh() method takes each element of the O-Calc Pro model and converts the 3D geometry model into a mesh geometry as required for the DAE file format.
  1. private void BoundingMeshes()
  2. {
  3. double rangeX = 0;
  4. double rangeZ = 0;
  5. foreach (Mesh mesh in cMeshes)
  6. {
  7. foreach (Point3D pt in mesh.cPoints)
  8. {
  9. rangeX = Math.Max(Math.Abs(pt.X), rangeX);
  10. rangeZ = Math.Max(Math.Abs(pt.Z), rangeZ);
  11. }
  12. }
  13.  
  14. Mesh result = new Mesh();
  15. //add corner markers to make it center on the pole
  16. for (int xi = -1; xi <= 1; xi += 2)
  17. {
  18. for (int zi = -1; zi <= 1; zi += 2) { result.cTrangles.Add(result.cPoints.Count); result.cPoints.Add(new Point3D((xi * rangeX) - 1, 0, zi * rangeZ)); result.cTrangles.Add(result.cPoints.Count); result.cPoints.Add(new Point3D((xi * rangeX), 0, zi * rangeZ)); result.cTrangles.Add(result.cPoints.Count); result.cPoints.Add(new Point3D(xi * rangeX, 0, (zi * rangeZ) + 1)); } } cMeshes.Add(result); } private void BuildMesh(PPLElement pElem) { if (pElem.DisplayedGeometries.Count > 0)
  19. {
  20. foreach (GeometryModel3D gm in pElem.DisplayedGeometries)
  21. {
  22. if (gm.Geometry is MeshGeometry3D)
  23. {
  24. Mesh mesh = new Mesh();
  25. cMeshes.Add(mesh);
  26. MeshGeometry3D m3d = (gm.Geometry as MeshGeometry3D);
  27. System.Diagnostics.Debug.Assert(gm.Material is DiffuseMaterial);
  28. if (gm.Material is DiffuseMaterial)
  29. {
  30. DiffuseMaterial dm = gm.Material as DiffuseMaterial;
  31. System.Windows.Media.SolidColorBrush sb = dm.Brush as System.Windows.Media.SolidColorBrush;
  32. mesh.cColor = System.Drawing.Color.FromArgb(sb.Color.R, sb.Color.G, sb.Color.B);
  33. }
  34. foreach (Point3D pt in m3d.Positions)
  35. {
  36. mesh.cPoints.Add(pt);
  37. }
  38. foreach (int tidx in m3d.TriangleIndices)
  39. {
  40. mesh.cTrangles.Add(tidx);
  41. }
  42. }
  43. }
  44. }
  45. foreach (PPLElement elem in pElem.Children)
  46. {
  47. BuildMesh(elem);
  48. }
  49. }

The Show Marker option of the ‘Show on Google Earth’ plugin also creates a temporary KML file using the O-Calc Pro pole’s coordinates and places a standard Google Earth pushpin at that location.
Similarly, the Batch Create Index option performs a similar function as the Show Marker option, but creates a temporary KML file for all O-Calc Pro PPLX files found within the selected folder and subfolders if the user selected “Yes” to the MessageBox call.
The other interesting feature added to the Show Marker and Batch Create Index options is the added lines to the KML file that gives a link on the Google Earth pushpin to the full path and name of the PPLX file:

  1. sw.Write(" <![CDATA[<a href=\"file:///"); sw.Write(file.Replace(" ", "%20").Replace("\\", "/").Trim()); sw.WriteLine("\">O-Calc Pro</a>]]>");

This enables one to click on the pushpin within Google Earth and have the O-Calc Pro application launched with the particular PPLX file loaded. But note, as the Help option indicates you need to have the “Show web results in external browser” and “Allow access to local and personal data” options checked within Google Earth to have the O-Calc Pro application launched.
We hope this article has been useful and further shows the power of the O-Calc Pro Plugin architecture and API. A zip file of the full Visual Studio project of the ‘Show on Google Earth’ plugin can be downloaded from the O-Calc Pro User’s Website at:

http://www.osmoseutilities.com/o-calcpro

Facebooktwitterlinkedinmail