public DataBufferRoutine(IApplication theApplication, string strConfigFile) // comes from the interface hence through the form. { // Get all the essentials up and running myArcMapFuncs = new ArcMapFunctions(theApplication); myConfig = new BufferToolConfig(strConfigFile); // Must now pass the correct XML name. myFileFuncs = new FileFunctions(); }
bool blOpenForm; // this tracks all the way through initialisation whether the form should open. public frmDataBuffer() { blOpenForm = true; InitializeComponent(); myFileFuncs = new FileFunctions(); myStringFuncs = new StringFunctions(); myLaunchConfig = new BufferToolLaunchConfig(); if (!myLaunchConfig.XMLFound) { MessageBox.Show("XML file 'DataBuffer.xml' not found; form cannot load.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); blOpenForm = false; } if (!myLaunchConfig.XMLLoaded) { MessageBox.Show("Error loading XML File 'DataBuffer.xml'; form cannot load.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); blOpenForm = false; } if (blOpenForm) { string strXMLFolder = myFileFuncs.GetDirectoryName(Settings.Default.XMLFile); bool blOnlyDefault = true; int intCount = 0; if (myLaunchConfig.ChooseConfig) // If we are allowed to choose, check if there are multiple profiles. // If there is only the default XML file in the directory, launch the form. Otherwise the user has to choose. { foreach (string strFileName in myFileFuncs.GetAllFilesInDirectory(strXMLFolder)) { if (myFileFuncs.GetFileName(strFileName).ToLower() != "databuffer.xml" && myFileFuncs.GetExtension(strFileName).ToLower() == "xml") { // is it the default? intCount++; if (myFileFuncs.GetFileName(strFileName) != myLaunchConfig.DefaultXML) { blOnlyDefault = false; } } } if (intCount > 1) { blOnlyDefault = false; } } if (myLaunchConfig.ChooseConfig && !blOnlyDefault) { // User has to choose the configuration file first. using (var myConfigForm = new frmChooseConfig(strXMLFolder, myLaunchConfig.DefaultXML)) { var result = myConfigForm.ShowDialog(); if (result == System.Windows.Forms.DialogResult.OK) { strConfigFile = strXMLFolder + "\\" + myConfigForm.ChosenXMLFile; } else { MessageBox.Show("No XML file was chosen; form cannot load.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); blOpenForm = false; } } } else { strConfigFile = strXMLFolder + "\\" + myLaunchConfig.DefaultXML; // don't allow the user to choose, just use the default. // Just check it exists, though. if (!myFileFuncs.FileExists(strConfigFile)) { MessageBox.Show("The default XML file '" + myLaunchConfig.DefaultXML + "' was not found in the XML directory; form cannot load.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); blOpenForm = false; } } } if (blOpenForm) { // Firstly let's read the XML. myConfig = new BufferToolConfig(strConfigFile); // Must now pass the correct XML name. // Did we find the XML? if (!myConfig.FoundXML) { MessageBox.Show("XML file not found; form cannot load.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); blOpenForm = false; } // Did it load OK? else if (!myConfig.LoadedXML) { MessageBox.Show("Error loading XML File; form cannot load.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); blOpenForm = false; } } // Close the form if there are any errors at this point. if (!blOpenForm) { Load += (s, e) => Close(); return; } // Fix any illegal characters in the user name string strUserID = myStringFuncs.StripIllegals(Environment.UserName, "_", false); // We're all set to show the form. Set it up. // Initialise all the helper classes. theApplication = ArcMap.Application; myArcMapFuncs = new ArcMapFunctions(theApplication); myDataBufferFuncs = new DataBufferRoutine(theApplication, strConfigFile); myFileFuncs = new FileFunctions(); // Now fill up the menu with the required layers. // Firstly check for missing layers. MapLayers theInputLayers = myConfig.InputLayers; List <string> MissingLayerList = new List <string>(); foreach (MapLayer aLayer in theInputLayers) { if (!myArcMapFuncs.LayerExists(aLayer.LayerName)) // We do not accept group layers. { MissingLayerList.Add(aLayer.LayerName); } else { lstInput.Items.Add(aLayer.DisplayName); } } // Tell the user that there's a problem if there is one. if (MissingLayerList.Count > 0) { string strMessage = "Warning: "; if (MissingLayerList.Count == 1) { strMessage = "the layer " + MissingLayerList[0] + " is not loaded in the Table of Contents."; } else if (MissingLayerList.Count > 1) { strMessage = "the following layers are not loaded in the Table of Contents: "; foreach (string aLayer in MissingLayerList) { strMessage = strMessage + aLayer + ", "; } strMessage = strMessage.Substring(0, strMessage.Length - 2) + "."; // Trim the last comma and space; add a full stop. } MessageBox.Show(strMessage, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); } // Set the default for clear log file. chkClearLog.Checked = myConfig.DefaultClearLog; }