Example #1
0
        private void PrepareMapForOutput(object sender, MapOutput_EventArgs e)
        {
            ITrackCancel pTrackCancel = new CancelTrackerClass();
            IProgressDialogFactory pPDFact = new ProgressDialogFactoryClass();
            IProgressDialog2 pProgressDialog = null;

            //IMxDocument pMxDoc = null;

            try
            {
                //pMxDoc = (IMxDocument)m_app.Document;

                // check the spatial reference of the first map in the draw order
                ISpatialReference pSpatialRef = m_pISDUTExt.MxDocument.Maps.get_Item(0).SpatialReference;

                // get the template file and store the pagelayout and map
                IGxFile pGxFile = new GxMapClass();
                pGxFile.Path = e.mapTemplatePath; //@"D:\Project\64235_SUT\development\current\src\templates\exA.mxt";
                IGxMapPageLayout pGxMapPageLayout = (IGxMapPageLayout)pGxFile;

                IPageLayout pPageLayoutTemplate = pGxMapPageLayout.PageLayout;
                IMap pMapTemplate = FindMap(pPageLayoutTemplate);

                // make sure the user understands that we're only supporting one map frame in the template
                if (m_pISDUTExt.MxDocument.Maps.Count > 1)
                {
                    System.Windows.Forms.DialogResult diagRes2 =
                        MessageBox.Show("You have more maps than the target template can accomodate.\n" +
                                        "The top most map in your Table of Contents will be used in \n" +
                                        "the template and the others will be discarded. \n\n" +
                                        "Do you want to continue?", "SUITT Question", MessageBoxButtons.YesNo);
                    if(diagRes2 == DialogResult.No)
                        return;
                }

                // setup the progress dialog and show it
                pProgressDialog = (IProgressDialog2)pPDFact.Create(pTrackCancel,m_app.hWnd);
                pProgressDialog.Animation = esriProgressAnimationTypes.esriProgressGlobe;
                pProgressDialog.Title = "Loading and applying template...";
                pProgressDialog.CancelEnabled = true;
                pProgressDialog.ShowDialog();

                // save the layers in the template target frame to a List
                System.Collections.Generic.List<ILayer> templateMapLayers = new System.Collections.Generic.List<ILayer>();
                for(int i = 0; i < pMapTemplate.LayerCount; i++)
                {
                    templateMapLayers.Add(pMapTemplate.get_Layer(i));
                }

                // remove other map frames from the current document (the 'destination')
                while (m_pISDUTExt.MxDocument.Maps.Count > 1)
                {
                    m_pISDUTExt.MxDocument.Maps.RemoveAt(m_pISDUTExt.MxDocument.Maps.Count - 1);
                }

                IMap pMapToBeReplaced = m_pISDUTExt.MxDocument.FocusMap;

                // set the active view to the pagelayout mode
                m_pISDUTExt.MxDocument.ActiveView = (IActiveView)m_pISDUTExt.MxDocument.PageLayout;

                // Get active views and extents for both the template and current map frames.
                // Will be used to set the extents later
                IActiveView pAVdest = (IActiveView)m_pISDUTExt.MxDocument.FocusMap;
                IActiveView pAVtemplate = (IActiveView)pMapTemplate;

                IEnvelope pEnvDest = pAVdest.Extent;
                pEnvDest.SpatialReference = m_pISDUTExt.MxDocument.FocusMap.SpatialReference;

                IEnvelope pEnvTemplate = pAVtemplate.Extent;
                pEnvTemplate.SpatialReference = pMapTemplate.SpatialReference;

                // deal with reprojecting data frames as necessary
                DialogResult diagResMatchSR = DialogResult.No;
                try
                {
                    if (pMapTemplate.SpatialReference.FactoryCode != pSpatialRef.FactoryCode)
                    {
                        diagResMatchSR = MessageBox.Show("The spatial reference of the map is not the\n" +
                            "same as the target map of the template.\n\n" +
                            "Do you want to change the current map spatial reference to\n" +
                            "match the template spatial reference?", "SUITT Question", MessageBoxButtons.YesNo);

                        if (diagResMatchSR == DialogResult.No)
                        {
                            //m_pISDUTExt.Document.Maps.get_Item(0).SpatialReference = pMapTemplate.SpatialReference;
                            pMapTemplate.SpatialReference = pMapToBeReplaced.SpatialReference;
                        }
                    }
                }
                catch { }

                // save the layers in the current map frame to a List
                System.Collections.Generic.List<ILayer> destMapLayers = new System.Collections.Generic.List<ILayer>();
                for(int i = 0; i < m_pISDUTExt.MxDocument.FocusMap.LayerCount; i++)
                {
                    destMapLayers.Add(m_pISDUTExt.MxDocument.FocusMap.get_Layer(i));
                }

                // if the spatial reference systems don't match, project the current map envelope
                // to match the SR of the template --> will use this to set the extents later
                if (pMapTemplate.SpatialReference.FactoryCode != pMapToBeReplaced.SpatialReference.FactoryCode)
                {
                    pEnvDest.Project(pEnvTemplate.SpatialReference);

                }

                IGroupLayer pGroupLayer = new GroupLayerClass();
                pGroupLayer.Name = "Layers From Template";

                // group the layers in the new map frame (from the template)
                //for(int i = 0; i < templateMapLayers.Count; i++)
                foreach(ILayer pLayer in templateMapLayers)
                {
                    pGroupLayer.Add(pLayer);
                    pMapTemplate.DeleteLayer(pLayer);
                }

                // copy all elements from the template to the current document pagelayout
                CopyPasteLayoutElements(m_pISDUTExt.MxDocument.PageLayout, pPageLayoutTemplate);

                // add the layers from the current (destination) document to the new map frame
                // that got copied over from the template
                for (int i = destMapLayers.Count - 1; i >= 0; i--)
                {
                    pMapTemplate.AddLayer(destMapLayers[i]);
                }

                // add and move group layer to top of TOC
                pMapTemplate.AddLayer((ILayer)pGroupLayer);
                pMapTemplate.MoveLayer((ILayer)pGroupLayer, 0);
                //m_pISDUTExt.Document.Maps.get_Item(0).AddLayer((ILayer)pGroupLayer);
                //m_pISDUTExt.Document.Maps.get_Item(0).MoveLayer((ILayer)pGroupLayer, 0);

                // set the extent of the map
                pAVtemplate.Extent = pEnvDest;

                // refresh the TOC
                m_pISDUTExt.MxDocument.CurrentContentsView.Refresh(null);

                // Do the search and replaces for the text elements
                Hashtable htSearchAndRep = new Hashtable();

                // add the date
                string strDate = DateTime.Today.ToLongDateString();
                htSearchAndRep.Add("<Enter Date Here>",strDate);

                pSpatialRef = m_pISDUTExt.MxDocument.Maps.get_Item(0).SpatialReference;

                // add the spatial ref info
                IProjectedCoordinateSystem pPCS = pSpatialRef as IProjectedCoordinateSystem;

                try
                {
                    // try to set the datum
                    if (pPCS != null)
                    {
                        IDatum pDatum = pPCS.GeographicCoordinateSystem.Datum;

                        /*
                        * esriSRDatum_NAD1927 6267 North American Datum 1927.
                        * esriSRDatum_NAD1983 6269 North American Datum 1983.
                        */

                        if (pDatum.FactoryCode == 6267)
                            htSearchAndRep.Add("<Enter NAD Here>","27");
                        else if (pDatum.FactoryCode == 6269)
                            htSearchAndRep.Add("<Enter NAD Here>","83");
                    }
                }
                catch{}

                try
                {
                    // try to set the utm zone
                    if (pPCS.FactoryCode >= 26903 && pPCS.FactoryCode <= 26923) // NAD83 UTM Zones 3-23
                    {
                        string strZone = Convert.ToString(pPCS.FactoryCode - 26900);
                        htSearchAndRep.Add("<Enter Zone Here>",strZone);

                    }
                    else if(pPCS.FactoryCode >= 26703 && pPCS.FactoryCode <= 26722) // NAD27 UTM Zones 3-22
                    {
                        string strZone = Convert.ToString(pPCS.FactoryCode - 26700);
                        htSearchAndRep.Add("<Enter Zone Here>",strZone);
                    }
                }
                catch{}

                // do all replacements
                ReplaceText(htSearchAndRep,m_pISDUTExt.MxDocument.PageLayout);

                //m_pISDUTExt.Document = (IMxDocument)m_app.Document;
                //m_pISDUTExt.Document.FocusMap.MapScale = Convert.ToInt32(e.mapScale);
                pMapTemplate.MapScale = Convert.ToInt32(e.mapScale);
                //IActiveView pAV = (IActiveView)m_pISDUTExt.Document.PageLayout;
                //util.Utils.Release(m_pISDUTExt.Document);
                //pAV.Refresh();
                pAVtemplate.Refresh();
            }
            catch(Exception ex)
            {
                Debug.WriteLine(ex.Message + "\n" + ex.StackTrace);
                util.Logger.Write(" Descrip  : Preparing map for output " +
                    "\n Message  : " + ex.Message +
                    "\n StackTrc : " + ex.StackTrace,util.Logger.LogLevel.Debug);
            }
            finally
            {
                try
                {
                    pProgressDialog.HideDialog();
                    //util.Utils.Release(m_pISDUTExt.Document);
                }
                catch{}
            }
        }
Example #2
0
        private void btnOK_Click(object sender, System.EventArgs e)
        {
            try
            {
                MapOutput_EventArgs moArgs = new MapOutput_EventArgs();

                for (int i = 0; i < m_templates.Count; i++)
                {
                    FileInfo fi = (FileInfo)m_templates[i];
                    if (fi.Name.ToLower() == lstboxTemplates.Text.ToLower())
                    {
                        moArgs.mapTemplatePath = fi.FullName;
                        break;
                    }
                }

                moArgs.mapScale = cboScales.Text;
                //moArgs.mapFrame = lstboxMapFrames.Text;

                //fire the event
                MapOutputFormEvent(this, moArgs);
                this.Close();
            }
            catch(Exception ex)
            {
                Debug.WriteLine(ex.Message + "\n" + ex.StackTrace);
            }
        }