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:
-
#region HOUSEKEEPING
-
PPL_Lib
.
PPLMain cPPLMain
=
null
;
-
public PLUGIN_TYPE Type
-
{
-
get
-
{
-
return PLUGIN_TYPE
.
MENU_ITEM
;
-
}
-
}
-
public
String Name
-
{
-
get
-
{
-
return
"Show on Google Earth"
;
-
}
-
}
-
public
String Description
-
{
-
get
-
{
-
return
"Show on Google Earth"
;
-
}
-
}
public
void AddToMenu
(PPL_Lib
.
PPLMain pPPLMain,
System
.
Windows
.
Forms
.
ToolStrip pToolStrip
)
-
{
-
cPPLMain
= pPPLMain
;
-
cShowOnGoogleEarthButton
=
new ToolStripMenuItem
(
"Show on Google Earth"
)
;
-
int itemindex
=
0
;
-
System.Diagnostics
.
Debug
.
Assert
(pToolStrip
.
Items
[itemindex
]
is ToolStripDropDownButton
)
;
-
if
(pToolStrip
.
Items
[itemindex
]
is ToolStripDropDownButton
)
-
{
-
ToolStripDropDownButton tsb
= pToolStrip
.
Items
[itemindex
]
as ToolStripDropDownButton
;
-
System.Diagnostics
.
Debug
.
Assert
(tsb
.
Text
==
"&File"
)
;
-
tsb
.
DropDownItems
.
Insert
(tsb
.
DropDownItems
.
Count
-
1, cShowOnGoogleEarthButton
)
;
-
tsb
.
DropDownItems
.
Insert
(tsb
.
DropDownItems
.
Count
-
1,
new ToolStripSeparator
(
)
)
;
-
tsb
.
DropDownOpened
+= tsb_DropDownOpened
;
-
}
-
cShowMarkerButton
=
new ToolStripMenuItem
(
"Show Marker"
)
;
-
cRenderStructureButton
=
new ToolStripMenuItem
(
"Render Structure"
)
;
-
cCreateIndexeButton
=
new ToolStripMenuItem
(
"Batch Create Index"
)
;
-
cHelpButton
=
new ToolStripMenuItem
(
"Help"
)
;
-
cShowOnGoogleEarthButton
.
DropDownItems
.
Add
(cShowMarkerButton
)
;
-
cShowMarkerButton
.
Click
+= cShowMarkerButton_Click
;
-
cShowOnGoogleEarthButton
.
DropDownItems
.
Add
(cRenderStructureButton
)
;
-
cRenderStructureButton
.
Click
+= cRenderStructureButton_Click
;
-
cShowOnGoogleEarthButton
.
DropDownItems
.
Add
(
new ToolStripSeparator
(
)
)
;
-
cShowOnGoogleEarthButton
.
DropDownItems
.
Add
(cCreateIndexeButton
)
;
-
cCreateIndexeButton
.
Click
+= cCreateIndexeButton_Click
;
-
cShowOnGoogleEarthButton
.
DropDownItems
.
Add
(cHelpButton
)
;
-
cHelpButton
.
Click
+= cHelpButton_Click
;
-
}
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:
- 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.
- 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.
- 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.
-
private
void BoundingMeshes
(
)
-
{
-
double rangeX
=
0
;
-
double rangeZ
=
0
;
-
foreach
(Mesh mesh
in cMeshes
)
-
{
-
foreach
(Point3D pt
in mesh
.
cPoints
)
-
{
-
rangeX
= Math
.
Max
(Math
.
Abs
(pt
.
X
), rangeX
)
;
-
rangeZ
= Math
.
Max
(Math
.
Abs
(pt
.
Z
), rangeZ
)
;
-
}
-
}
-
-
Mesh result
=
new Mesh
(
)
;
-
//add corner markers to make it center on the pole
-
for
(
int xi
=
-
1
; xi
<
;=
1
; xi
+=
2
)
-
{
-
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
)
-
{
-
foreach
(GeometryModel3D gm
in pElem
.
DisplayedGeometries
)
-
{
-
if
(gm
.
Geometry
is MeshGeometry3D
)
-
{
-
Mesh mesh
=
new Mesh
(
)
;
-
cMeshes
.
Add
(mesh
)
;
-
MeshGeometry3D m3d
=
(gm
.
Geometry
as MeshGeometry3D
)
;
-
System.Diagnostics
.
Debug
.
Assert
(gm
.
Material
is DiffuseMaterial
)
;
-
if
(gm
.
Material
is DiffuseMaterial
)
-
{
-
DiffuseMaterial dm
= gm
.
Material
as DiffuseMaterial
;
-
System
.
Windows
.
Media
.
SolidColorBrush sb
= dm
.
Brush
as
System
.
Windows
.
Media
.
SolidColorBrush
;
-
mesh
.
cColor
=
System.Drawing
.
Color
.
FromArgb
(sb
.
Color
.
R, sb
.
Color
.
G, sb
.
Color
.
B
)
;
-
}
-
foreach
(Point3D pt
in m3d
.
Positions
)
-
{
-
mesh
.
cPoints
.
Add
(pt
)
;
-
}
-
foreach
(
int tidx
in m3d
.
TriangleIndices
)
-
{
-
mesh
.
cTrangles
.
Add
(tidx
)
;
-
}
-
}
-
}
-
}
-
foreach
(PPLElement elem
in pElem
.
Children
)
-
{
-
BuildMesh
(elem
)
;
-
}
-
}
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:
-
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