#plugin XML Import 'uncomment below to place the xml import on the File menu '#menuinsert 1 9 #author NBOS Software Staff #desc Example AlienAPI script that imports AstroSynthesis sectors from XML Files 'do window stuff ShiftX = 0 ShiftY = 0 ShiftZ = 0 XMLImportFile = "" XMLImportPath = "" ImportBodies = true ImportRoutes = true ImportSubsectors = true 'we may need this when loading resources Dim FileSystem Set FileSystem = CreateObject("Scripting.FileSystemObject") ' oSector = GetCurrentSector() If XMLImportWindow() Then Set xml = CreateObject("Microsoft.XMLDOM") xml.Load XMLImportFile Set eSector = xml.selectSingleNode("//Sector") If eSector is Nothing Then MsgBox "No 'Sector' Entry found!" Else ImportSectorFromXML oSector, eSector End If End If oSector.RenderMessageBig = "" oSector.RenderMessageSmall = "" RefreshScene Sub ImportSectorFromXML( oSector, eSector) Dim bodies, routes, subsectors oSector.New UpdateScene "Importing Sector Attributes" oSector.IDString = getAttribute( eSector, "ID", "") 'oSector.FileName = getStringNode( eSector, "FileName") oSector.RandomSeed = getNumberNode( eSector, "RandomSeed") oSector.UnitStarMeasureName = getStringNode( eSector, "UnitStarMeasureName") oSector.UnitStarMeasureAbbrev = getStringNode( eSector, "UnitStarMeasureAbbrev") oSector.UnitStarMeasureRatio = getStringNode( eSector, "UnitStarMeasureRatio") oSector.SizeX = getNumberNode( eSector, "SizeX") oSector.SizeY = getNumberNode( eSector, "SizeY") oSector.SizeZ = getNumberNode( eSector, "SizeZ") oSector.Frequency = getNumberNode( eSector, "Frequency") oSector.GridStyle = getNumberNode( eSector, "GridStyle") oSector.GridX = getNumberNode( eSector, "GridX") oSector.GridY = getNumberNode( eSector, "GridY") oSector.GridZ = getNumberNode( eSector, "GridZ") oSector.GridTick = getNumberNode( eSector, "GridTick") oSector.GridTickLabels = getStringNode( eSector, "GridTickLabels") oSector.GridColor = getNumberNode( eSector, "GridColor") oSector.BaseFont = getStringNode( eSector, "BaseFont") Set eCF = eSector.selectSingleNode("CustomFields") If not (eCF is Nothing) Then ImportCustomFieldsFromXML oSector, eCF End If If ImportBodies Then Set bodies = eSector.selectSingleNode( "Bodies") If not (bodies is Nothing) Then ImportSectorBodiesFromXML oSector, bodies End If End If If ImportRoutes Then UpdateScene "Importing Routes" Set routes = eSector.selectSingleNode( "Routes") If not (routes is Nothing) Then ImportRoutesFromXML oSector, routes End If oSector.UpdateRouteBodies End If If ImportSubsectors Then UpdateScene "Importing Subsectors" Set subsectors = eSector.selectSingleNode("Subsectors") If not (subsectors is Nothing) Then ImportSubsectorsFromXML oSector, subsectors End If End If oSector.UpdateVisibleCount oSector.BuildPoliticalList true oSector.BuildRouteTypeList true UpdateCaption End Sub Sub ImportSectorBodiesFromXML( oSector, eBodies) Dim allbodies Dim i, b Set allbodies = eBodies.selectNodes("Body") For Each eBody in allbodies ImportBodyFromXML oSector, eBody, true, 0 Next 'update the statistics for each system For i = 1 to oSector.SystemCount b = oSector.GetSystem( i-1) b.UpdateRootBody Next End Sub Sub ImportBodyFromXML( oSector, eBody, bRoot, bParent) Dim b, t, n Dim eAtm, eChildren, allchildren, eChild, eCF Dim sResource b = CreateBody("") b.IDString = getAttribute( eBody, "ID", "") b.Name = getAttribute( eBody, "Name", "") t = getAttribute( eBody, "TypeID", "Unassigned") b.TypeID = BodyTypeFromText( t) b.BodyType = getStringNode( ebody, "BodyType") If bRoot Then UpdateScene "Importing " & b.Name b.X = getNumberNode( eBody, "X") b.Y = getNumberNode( eBody, "Y") b.Z = getNumberNode( eBody, "Z") b.X = b.X + ShiftX b.Y = b.Y + ShiftY b.Z = b.Z + ShiftZ End If b.Color = getNumberNode( eBody, "Color") b.LabelColor = getNumberNode( eBody, "LabelColor") b.FontStyle = getNumberNode( eBody, "FontStyle") b.RenderDistance = getNumberNode( eBody, "RenderDistance") b.LabelDistance = getNumberNode( eBody, "LabelDistance") b.RouteDistance = getNumberNode( eBody, "RouteDistance") b.BlazonImageFile = getStringNode( eBody, "BlazonImageFile") b.BlazonDisplay = getNumberNode( eBody, "BlazonDisplay") b.BlazonWrap = getNumberNode( eBody, "BlazonWrap") b.Spectral = getStringNode( eBody, "Spectral") b.Distance = getNumberNode( eBody, "Distance") b.Radius = getNumberNode( eBody, "Radius") b.Density = getNumberNode( eBody, "Density") b.Mass = getNumberNode( eBody, "Mass") b.Rotation = getNumberNode( eBody, "Rotation") b.Luminosity = getNumberNode( eBody, "Luminosity") b.Albedo = getNumberNode( eBody, "Albedo") b.Temp = getNumberNode( eBody, "Temp") b.Atmosphere = getNumberNode( eBody, "Atmosphere") b.AtmosphereNotes = getStringNode( eBody, "AtmosphereNotes") Set eAtm = eBody.selectSingleNode("AtmosphereComponents") If not (eAtm is Nothing) Then ImportBodyAtmosphereFromXML b, eAtm End If b.Water = getNumberNode( eBody, "Water") b.Population = getNumberNode( eBody, "Population") b.ChildPopulation = getNumberNode( eBody, "ChildPopulation") b.Habitability = getNumberNode( eBody, "Habitability") b.ChildHabitability =getNumberNode( eBody, "ChildHabitability") b.BodyCount = getNumberNode( eBody, "BodyCount") b.PoliticalAffiliation = getStringNode( eBody, "PoliticalAffiliation") b.SphereInfluence = getNumberNode( eBody, "SphereInfluence") b.SphereInfluenceColor = getNumberNode( eBody, "SphereInfluenceColor") b.SphereInfluenceSize = getNumberNode( eBody, "SphereInfluenceSize") b.Notes = getStringNode( eBody, "Notes") b.GMNotes = getStringNode( eBody, "GMNotes") b.DSOIntensity = getNumberNode( eBody, "DSOIntensity") b.RandomSeed = getNumberNode( eBody, "RandomSeed") b.PreviewImageFile = getStringNode( eBody, "PreviewImageFile") b.RetrogradeOrbit = getNumberNode( eBody, "RetrogradeOrbit") b.Eccentricity = getNumberNode( eBody, "Eccentricity") b.Inclination = getNumberNode( eBody, "Inclination") b.AngleAscendingNode = getNumberNode( eBody, "AngleAscendingNode") b.AnglePeriapsis = getNumberNode( eBody, "AnglePeriapsis") b.AxialTilt = getNumberNode( eBody, "AxialTilt") b.TimeOffset = getNumberNode( eBody, "TimeOffset") 'import preview image? If Len( b.PreviewImageFile) > 0 Then 'check for file in dir sResource = XMLFilePath & FileSystem.GetFileName( b.PreviewImageFile) If FileSystem.FileExists( sResource) Then 'already added? if not, add, otherwise just assign resouce n = oSector.FindResource( sResource) If n < 0 Then oSector.AddResourceFile( sResource) End If b.PreviewImageFile = sResource End If End If 'import blazon? If Len( b.BlazonImageFile) > 0 Then 'check for file in dir sResource = XMLFilePath & FileSystem.GetFileName( b.BlazonImageFile) MsgBox sResource If FileSystem.FileExists( sResource) Then 'already added? if not, add, otherwise just assign resouce n = oSector.FindResource( sResource) If n < 0 Then oSector.AddResourceFile( sResource) End If b.BlazonImageFile = sResource End If End If Set eCF = eBody.selectSingleNode("CustomFields") If not (eCF is Nothing) Then ImportCustomFieldsFromXML b, eCF End If Set eChildren = eBody.selectSingleNode( "Children") If not (eChildren is Nothing) Then Set allchildren = eChildren.selectNodes( "Body") For Each eChild in allchildren ImportBodyFromXML oSector, eChild, false, b Next End If If bRoot Then oSector.AddSystem b b.ModifySystem Else bParent.AddChild( b) b.ModifySystem End If End Sub Sub ImportBodyAtmosphereFromXML( b, eAtm) Dim AllAtm Dim e Dim sElement, sPercent, nPercent Set AllAtm = eAtm.selectNodes( "Component") For Each e in AllAtm sElement = getAttribute( e, "AtmElement", "") sPercent = getAttribute( e, "AtmPercent", 0) nPercent = sPercent + 0 b.SetAtmElement sElement, nPercent Next End Sub Sub ImportCustomFieldsFromXML( oAstro, eNode) Dim allfields Dim eF Dim sName, sVal Set allfields = eNode.selectNodes("CustomField") If not (allfields is nothing) Then For Each eF in allfields sName = getAttribute( eF, "FieldName", "") sVal = getAttribute( eF, "FieldValue", "") If Len( sName) > 0 Then oAstro.SetField sName, sVal End If Next End If End Sub Sub ImportRoutesFromXML( oSector, routes) Dim allroutes, e Set allroutes = routes.selectNodes("Route") For Each e in allroutes ImportRouteXML oSector, e Next End Sub Sub ImportRouteXML( oSector, eRoute) Dim r Dim eWP, allwaypoints, e Dim wp r = CreateRoute() r.Loaded = true r.Modified = true r.Name = getAttribute( eRoute, "Name", "") r.RouteType = getAttribute( eRoute, "RouteType", "") r.Red = getAttribute( eRoute, "Red", 1) r.Green = getAttribute( eRoute, "Green", 1) r.Blue = getAttribute( eRoute, "Blue", 1) r.LineWidth = getAttribute( eRoute, "LineWidth", 1) r.LineStyle = getAttribute( eRoute, "LineStyle", 0) r.Stipple = getAttribute( eRoute, "Stipple", &HFFFFFF) Set eWP = eRoute.selectSingleNode("Waypoints") If not (eWP is Nothing) Then Set allwaypoints = eWP.selectNodes( "Waypoint") For Each e in allwaypoints wp = CreateWaypoint() wp.ID = getAttribute( e, "ID", "") wp.X = getAttribute( e, "X", 0) wp.Y = getAttribute( e, "Y", 0) wp.Z = getAttribute( e, "Z", 0) r.AddWaypoint( wp) Next End If 'Import custom fields Set eCF = eRoute.selectSingleNode("CustomFields") If not (eCF is Nothing) Then ImportCustomFieldsFromXML r, eCF End If oSector.AddRoute r End Sub Sub ImportSubsectorsFromXML( oSector, subsectors) Dim allss, e Set allss = subsectors.selectNodes("Subsector") If not (allss is Nothing) Then For Each e in allss ImportSubsectorFromXML oSector, e Next End If End Sub Sub ImportSubsectorFromXML( oSector, e) Dim ss ss = CreateSubsector() ss.Loaded = false ss.Modified = true ss.Name = getAttribute( e, "Name", "") ss.Visible = getAttribute( e, "Visible", True) ss.Color = getAttribute( e, "Color", RGB(255,255,255)) ss.ShowLabel = getAttribute( e, "ShowLabel", True) ss.Shape = getAttribute( e, "Shape", 0) ss.X = getAttribute( e, "X", 0) ss.Y = getAttribute( e, "Y", 0) ss.Z = getAttribute( e, "Z", 0) ss.X = ss.X + ShiftX ss.Y = ss.Y + ShiftY ss.Z = ss.Z + ShiftZ ss.SizeX = getAttribute( e, "SizeX", 0) ss.SizeY = getAttribute( e, "SizeY", 0) ss.SizeZ = getAttribute( e, "SizeZ", 0) ss.RotateX = getAttribute( e, "RotateX", 0) ss.RotateY = getAttribute( e, "RotateY", 0) ss.RotateZ = getAttribute( e, "RotateZ", 0) ss.ShowGrid = getAttribute( e, "ShowGrid", true) ss.GridX = getAttribute( e, "GridX", 0) ss.GridY = getAttribute( e, "GridY", 0) ss.GridZ = getAttribute( e, "GridZ", 0) ss.GridTick = getAttribute( e, "GridTick", 0) ss.TickLabels = getAttribute( e, "TickLabels", 0) 'Import custom fields Set eCF = e.selectSingleNode("CustomFields") If not (eCF is Nothing) Then ImportCustomFieldsFromXML ss, eCF End If oSector.AddSubsector( ss) End Sub '---------- Util Funcs Function getStringNode( node, value) Dim s, e Set e = node.selectSingleNode(value) If e is Nothing Then s = "" Else s = e.text End If getStringNode = s End Function Function getNumberNode( node, value) Dim s, e Set e = node.selectSingleNode(value) If e is Nothing Then s = "0" Else s = e.text End If s = s + 0 getNumberNode = s End Function Function getAttribute( node, value, default) Dim s s = node.getAttribute(value) If IsNull(s) Then s = default End If getAttribute = s End Function Sub UpdateScene( s) oSector.RenderMessageBig = "Importing XML Data" oSector.RenderMessageSmall = s RefreshScene End Sub Function XMLImportWindow() w = NewDialogWindow() w.Width = 425 w.Height = 300 w.Caption = "XML Import (Plug-in)" w.Centered = true grp = w.AddGroupBox() grp.Top = 10 grp.left = 10 grp.Width = 400 grp.Height = 60 grp.Caption = "XML File" c = w.AddLabel() c.SetPosition 35, 35, 80, 24 c.Caption = "File:" fo = w.AddFileOpen() fo.SetPosition 70, 32, 320, 24 fo.FileMask = "XML Files (*.xml)|*.xml|All Files (*.*)|*.*" grp2 = w.AddGroupBox() grp2.SetPosition 10, 75, 400, 90 grp2.Caption = "Import Options" cbxBodies = w.AddCheckBox() cbxBodies.Top = 90 cbxBodies.Left = 20 cbxBodies.Width = 300 cbxBodies.Height = 24 cbxBodies.Caption = "Import Bodies" cbxBodies.Checked = true cbxRoutes = w.AddCheckBox() cbxRoutes.Top = 110 cbxRoutes.Left = 20 cbxRoutes.Width = 300 cbxRoutes.Height = 24 cbxRoutes.Caption = "Import Routes" cbxRoutes.Checked = true cbxSubsectors = w.AddCheckBox() cbxSubsectors.SetPosition 20, 130, 300, 24 cbxSubsectors.Caption = "Import Subsectors" cbxSubsectors.Checked = true grpShift = w.AddGroupBox() grpShift.SetPosition 10, 170, 400, 60 grpShift.Caption = "Center Point" c = w.AddLabel() c.SetPosition 30, 198, 50, 24 c.Caption = "X:" edX = w.AddTextEdit() edX.SetPosition 50, 195, 50, 24 edX.Text = "0" c = w.AddLabel() c.SetPosition 130, 198, 50, 24 c.Caption = "Y:" edY = w.AddTextEdit() edY.SetPosition 150, 195, 50, 24 edY.Text = "0" c = w.AddLabel() c.SetPosition 230, 198, 50, 24 c.Caption = "Z:" edZ = w.AddTextEdit() edZ.SetPosition 250, 195, 50, 24 edZ.Text = "0" If w.ShowModal Then If not FileExists( fo.FileName) Then MsgBox "'" & fo.FileName & "' Does not exist. You must select an XML file to import" XMLImportWindow = false Else bDoImport = true If not IsNumeric( edX.Text) Then bDoImport = false If not IsNumeric( edY.Text) Then bDoImport = false If not IsNumeric( edZ.Text) Then bDoImport = false If bDoImport Then ShiftX = edX.Text ShiftY = edY.Text ShiftZ = edZ.Text ImportBodies = cbxBodies.Checked ImportRoutes = cbxRoutes.Checked ImportSubsectors = cbxSubsectors.Checked XMLImportFile = fo.FileName XMLImportPath = GetFilePath( XMLImportFile) & "\" XMLImportWindow = true Else MsgBox "X, Y, and Z shift must be numbers" XMLImportWindow = false End If End If Else XMLImportWindow = false End If End Function Function GetFilePath( sFile) Dim fso Set fso = CreateObject("Scripting.FileSystemObject") GetFilePath = fso.GetParentFolderName( sFile) End Function Function FileExists( sFile) Dim fso Set fso = CreateObject("Scripting.FileSystemObject") FileExists = fso.FileExists(sFile) End Function