private bool PerformLocate(string locateText)
        {
            Document document = AfaDocData.ActiveDocData.Document;
            Editor   editor   = document.Editor;

            if (string.IsNullOrEmpty(locateText))
            {
                editor.WriteMessage(AfaStrings.InvalidPlaceText);
                return(false);
            }
            AGSGeometryServer sampleServer = AGSGeometryServer.GetSampleServer();

            if (sampleServer == null)
            {
                ErrorReport.ShowErrorMessage(AfaStrings.UnableToConnectGeometryService);
                return(false);
            }
            string text = "PROJCS[\"WGS_1984_Web_Mercator_Auxiliary_Sphere\",GEOGCS[\"GCS_WGS_1984\",DATUM[\"D_WGS_1984\",SPHEROID[\"WGS_1984\",6378137.0,298.257223563]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]],PROJECTION[\"Mercator_Auxiliary_Sphere\"],PARAMETER[\"False_Easting\",0.0],PARAMETER[\"False_Northing\",0.0],PARAMETER[\"Central_Meridian\",0.0],PARAMETER[\"Standard_Parallel_1\",0.0],PARAMETER[\"Auxiliary_Sphere_Type\",0.0],UNIT[\"Meter\",1.0],AUTHORITY[\"EPSG\",3857]]".ToString();
            bool   result;

            try
            {
                text = MSCPrj.ReadWKT(document);
                if (string.IsNullOrEmpty(text))
                {
                    text = "PROJCS[\"WGS_1984_Web_Mercator_Auxiliary_Sphere\",GEOGCS[\"GCS_WGS_1984\",DATUM[\"D_WGS_1984\",SPHEROID[\"WGS_1984\",6378137.0,298.257223563]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]],PROJECTION[\"Mercator_Auxiliary_Sphere\"],PARAMETER[\"False_Easting\",0.0],PARAMETER[\"False_Northing\",0.0],PARAMETER[\"Central_Meridian\",0.0],PARAMETER[\"Standard_Parallel_1\",0.0],PARAMETER[\"Auxiliary_Sphere_Type\",0.0],UNIT[\"Meter\",1.0],AUTHORITY[\"EPSG\",3857]]".ToString();
                    MSCPrj.AssignWKT(document, "PROJCS[\"WGS_1984_Web_Mercator_Auxiliary_Sphere\",GEOGCS[\"GCS_WGS_1984\",DATUM[\"D_WGS_1984\",SPHEROID[\"WGS_1984\",6378137.0,298.257223563]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]],PROJECTION[\"Mercator_Auxiliary_Sphere\"],PARAMETER[\"False_Easting\",0.0],PARAMETER[\"False_Northing\",0.0],PARAMETER[\"Central_Meridian\",0.0],PARAMETER[\"Standard_Parallel_1\",0.0],PARAMETER[\"Auxiliary_Sphere_Type\",0.0],UNIT[\"Meter\",1.0],AUTHORITY[\"EPSG\",3857]]");
                }
            }
            catch
            {
                ErrorReport.ShowErrorMessage(AfaStrings.ErrorCreatingWKT);
                result = false;
                return(result);
            }
            if (!MSCPrj.IsWKID(text))
            {
                try
                {
                    text = sampleServer.GetSpatialReferenceWKID(text);
                    if (string.IsNullOrEmpty(text))
                    {
                        string text2 = sampleServer.ErrorMessage;
                        if (string.IsNullOrEmpty(text2))
                        {
                            text2 = AfaStrings.UnsupportedCoordinateSystemForLocate;
                        }
                        ErrorReport.ShowErrorMessage(text2);
                        result = false;
                        return(result);
                    }
                }
                catch
                {
                    text = "PROJCS[\"WGS_1984_Web_Mercator_Auxiliary_Sphere\",GEOGCS[\"GCS_WGS_1984\",DATUM[\"D_WGS_1984\",SPHEROID[\"WGS_1984\",6378137.0,298.257223563]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]],PROJECTION[\"Mercator_Auxiliary_Sphere\"],PARAMETER[\"False_Easting\",0.0],PARAMETER[\"False_Northing\",0.0],PARAMETER[\"Central_Meridian\",0.0],PARAMETER[\"Standard_Parallel_1\",0.0],PARAMETER[\"Auxiliary_Sphere_Type\",0.0],UNIT[\"Meter\",1.0],AUTHORITY[\"EPSG\",3857]]";
                }
            }
            try
            {
                Convert.ToInt32(text);
            }
            catch
            {
                editor.WriteMessage(AfaStrings.UnsupportedCoordinateSystemForLocate);
                result = false;
                return(result);
            }
            Mouse.OverrideCursor = Cursors.Wait;
            AGSLocator aGSLocator = this.cbLocator.SelectedItem as AGSLocator;
            string     uRL        = aGSLocator.URL;
            string     arg        = uRL.TrimEnd(new char[]
            {
                '/',
                '\\'
            });
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append("/findAddressCandidates?SingleLine=");
            stringBuilder.Append(this.tbPlace.Text);
            stringBuilder.AppendFormat("&outSR={0}", text);
            stringBuilder.AppendFormat("&f={0}", "json");
            try
            {
                IDictionary <string, object> dictionary = aGSLocator.ParentConnection.MakeDictionaryRequest(arg + stringBuilder);
                if (dictionary.ContainsKey("error"))
                {
                    editor.WriteMessage(AfaStrings.ErrorConnectingToServer);
                    Mouse.OverrideCursor = null;
                    result = false;
                }
                else
                {
                    CadField cadField = new CadField();
                    cadField.Name     = AfaStrings.SearchString;
                    cadField.Value    = new TypedValue(1, this.tbPlace.Text);
                    cadField.ReadOnly = true;
                    if (dictionary.ContainsKey("candidates"))
                    {
                        DocUtil.FixPDMode();
                        using (document.LockDocument((DocumentLockMode)20, null, null, false))
                        {
                            List <ObjectId> list     = new List <ObjectId>();
                            Database        database = document.Database;
                            try
                            {
                                using (Transaction transaction = document.TransactionManager.StartTransaction())
                                {
                                    BlockTable       blockTable       = (BlockTable)transaction.GetObject(database.BlockTableId, (OpenMode)1, false);
                                    BlockTableRecord blockTableRecord = (BlockTableRecord)transaction.GetObject(blockTable[(BlockTableRecord.ModelSpace)], (OpenMode)1, false);
                                    this.CreateLocationsFeatureClass();
                                    DocUtil.FixPDMode();
                                    string               text3           = "ESRI_Locations";
                                    ObjectId             layer           = DocUtil.GetLayer(database, transaction, ref text3, null);
                                    ObjectId             blockDefinition = DocUtil.GetBlockDefinition(document, transaction, "ESRI_Locations");
                                    IEnumerable <object> enumerable      = dictionary["candidates"] as IEnumerable <object>;
                                    using (IEnumerator <object> enumerator = enumerable.GetEnumerator())
                                    {
                                        while (enumerator.MoveNext())
                                        {
                                            IDictionary <string, object> dictionary2 = (IDictionary <string, object>)enumerator.Current;
                                            CadField cadField2 = null;
                                            CadField cadField3 = null;
                                            if (dictionary2.ContainsKey("address"))
                                            {
                                                string text4 = dictionary2["address"].ToString();
                                                cadField3          = new CadField();
                                                cadField3.Name     = "Address";
                                                cadField3.Value    = new TypedValue(1, text4);
                                                cadField3.ReadOnly = true;
                                            }
                                            if (dictionary2.ContainsKey("score"))
                                            {
                                                try
                                                {
                                                    int num = Convert.ToInt32(dictionary2["score"]);
                                                    cadField2          = new CadField();
                                                    cadField2.Name     = "Score";
                                                    cadField2.Value    = new TypedValue(90, num);
                                                    cadField2.ReadOnly = true;
                                                }
                                                catch
                                                {
                                                }
                                            }
                                            if (dictionary2.ContainsKey("location"))
                                            {
                                                try
                                                {
                                                    IDictionary <string, object> dictionary3 = dictionary2["location"] as IDictionary <string, object>;
                                                    double num2 = Convert.ToDouble(dictionary3["x"]);
                                                    double num3 = Convert.ToDouble(dictionary3["y"]);
                                                    Entity entity;
                                                    if (blockDefinition != ObjectId.Null)
                                                    {
                                                        entity = new BlockReference(new Point3d(num2, num3, 0.0), blockDefinition);
                                                    }
                                                    else
                                                    {
                                                        entity = new DBPoint(new Point3d(num2, num3, 0.0));
                                                    }
                                                    entity.LayerId    = (layer);
                                                    entity.ColorIndex = (256);
                                                    ObjectId item = blockTableRecord.AppendEntity(entity);
                                                    transaction.AddNewlyCreatedDBObject(entity, true);
                                                    list.Add(item);
                                                    if (cadField3 != null)
                                                    {
                                                        CadField.AddCadAttributeToEntity(database, transaction, entity.ObjectId, cadField3);
                                                    }
                                                    if (cadField2 != null)
                                                    {
                                                        CadField.AddCadAttributeToEntity(database, transaction, entity.ObjectId, cadField2);
                                                    }
                                                    CadField.AddCadAttributeToEntity(database, transaction, entity.ObjectId, cadField);
                                                    document.TransactionManager.QueueForGraphicsFlush();
                                                    document.TransactionManager.FlushGraphics();
                                                    document.Editor.UpdateScreen();
                                                }
                                                catch
                                                {
                                                }
                                            }
                                        }
                                    }
                                    transaction.Commit();
                                    if (list.Count > 0)
                                    {
                                        ObjectId[] array = list.ToArray();
                                        DocUtil.ZoomToEntity(array);
                                        AfaDocData.ActiveDocData.DocDataset.UpdateMaps();
                                        AfaDocData.ActiveDocData.Document.Editor.UpdateScreen();
                                        AfaDocData.ActiveDocData.Document.Editor.Regen();
                                        Mouse.OverrideCursor = null;
                                        CmdLine.CancelActiveCommand();
                                        TableView tableView = new TableView(AfaDocData.ActiveDocData.GetActiveFeatureClassOrSubtype(), array);
                                        Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(Autodesk.AutoCAD.ApplicationServices.Application.MainWindow.Handle, tableView, false);
                                        tableView.Uninitialize();
                                        tableView.Dispose();
                                        this.SelectAndDelete();
                                        AfaDocData.ActiveDocData.DocDataset.UpdateMaps();
                                        AfaDocData.ActiveDocData.Document.Editor.UpdateScreen();
                                        AfaDocData.ActiveDocData.Document.Editor.Regen();
                                        result = true;
                                        return(result);
                                    }
                                    Mouse.OverrideCursor = null;
                                    editor.WriteMessage(AfaStrings.NoFeaturesFound + "  (" + this.tbPlace.Text + ")");
                                    result = false;
                                    return(result);
                                }
                            }
                            catch
                            {
                                Mouse.OverrideCursor = null;
                            }
                        }
                    }
                    Mouse.OverrideCursor = null;
                    result = false;
                }
            }
            catch
            {
                Mouse.OverrideCursor = null;
                editor.WriteMessage(AfaStrings.ErrorConnectingToServer);
                result = false;
            }
            return(result);
        }
Esempio n. 2
0
 private void DoTask()
 {
     try
     {
         IDictionary <string, object> dictionary = this.Parent.MakeDictionaryRequest(this.RequestURL);
         if (dictionary == null)
         {
             string text = this.Parent.ErrorMessage;
             if (string.IsNullOrEmpty(text))
             {
                 text = AfaStrings.AnUnexpectedErrorOccured;
             }
             ErrorReport.ShowErrorMessage(text);
         }
         this.UpdateForm.SetReady(false);
         SpatialReference spatialReference = null;
         if (dictionary != null)
         {
             object obj;
             if (!dictionary.TryGetValue("href", out obj))
             {
                 string text2 = AfaStrings.Error;
                 if (dictionary.ContainsKey("error"))
                 {
                     IDictionary <string, object> dictionary2 = dictionary["error"] as IDictionary <string, object>;
                     object obj2;
                     if (dictionary2.ContainsKey("details") && dictionary2.TryGetValue("details", out obj2))
                     {
                         object[] array = obj2 as object[];
                         if (array != null)
                         {
                             text2 = text2 + " - " + array[0].ToString();
                         }
                     }
                 }
                 Mouse.OverrideCursor = null;
                 ErrorReport.ShowErrorMessage(text2);
             }
             else
             {
                 string text3 = obj as string;
                 if (text3.Length > 0)
                 {
                     decimal value  = 0m;
                     decimal value2 = 0m;
                     decimal value3 = 0m;
                     decimal value4 = 0m;
                     if (dictionary.TryGetValue("extent", out obj))
                     {
                         IDictionary <string, object> dictionary3 = obj as IDictionary <string, object>;
                         object obj3;
                         if (dictionary3.TryGetValue("spatialReference", out obj3))
                         {
                             IDictionary <string, object> dictionary4 = obj3 as IDictionary <string, object>;
                             object obj4;
                             if (dictionary4.TryGetValue("wkid", out obj4))
                             {
                                 int num = int.Parse(obj4.ToString());
                                 spatialReference = AGSSpatialReference.SpRefFromWKID(ref num);
                             }
                             else if (dictionary4.TryGetValue("wkt", out obj4))
                             {
                                 string text4 = obj4.ToString();
                                 spatialReference = AGSSpatialReference.SpRefFromWKT(ref text4);
                             }
                         }
                         if (dictionary3.TryGetValue("xmin", out obj3))
                         {
                             value = (decimal)obj3;
                         }
                         if (dictionary3.TryGetValue("xmax", out obj3))
                         {
                             value3 = (decimal)obj3;
                         }
                         if (dictionary3.TryGetValue("ymax", out obj3))
                         {
                             value4 = (decimal)obj3;
                         }
                         if (dictionary3.TryGetValue("ymin", out obj3))
                         {
                             value2 = (decimal)obj3;
                         }
                         PointN pointN = new PointN();
                         if (spatialReference != null)
                         {
                             pointN.SpatialReference = spatialReference;
                         }
                         pointN.X = (double)value;
                         pointN.Y = (double)value2;
                         pointN.Z = 0.0;
                         PointN[] array2 = new PointN[]
                         {
                             pointN,
                             new PointN
                             {
                                 SpatialReference = spatialReference,
                                 X = (double)value3,
                                 Y = (double)value2,
                                 Z = 0.0
                             },
                             new PointN
                             {
                                 SpatialReference = spatialReference,
                                 X = (double)value,
                                 Y = (double)value4,
                                 Z = 0.0
                             }
                         };
                         try
                         {
                             if (File.Exists(this.OutputFile))
                             {
                                 File.Delete(this.OutputFile);
                                 if (App.TempFiles.Contains(this.OutputFile))
                                 {
                                     App.TempFiles.Remove(this.OutputFile);
                                 }
                             }
                             if (!this.Parent.DownloadFile(text3, this.OutputFile))
                             {
                                 this.OutputFile = text3;
                                 if (string.IsNullOrEmpty(this.OutputFile))
                                 {
                                     ErrorReport.ShowErrorMessage(AfaStrings.ErrorCreatingOutputFile);
                                     return;
                                 }
                             }
                         }
                         catch
                         {
                             this.OutputFile = text3;
                             if (string.IsNullOrEmpty(this.OutputFile))
                             {
                                 ErrorReport.ShowErrorMessage(AfaStrings.ErrorCreatingOutputFile);
                                 return;
                             }
                         }
                         this.UpdateForm.SetReady(false);
                         Point3d point3d  = new Point3d(pointN.X, array2[0].Y, 0.0);
                         Point3d point3d2 = new Point3d(array2[1].X, array2[1].Y, 0.0);
                         Point3d point3d3 = new Point3d(array2[2].X, array2[2].Y, 0.0);
                         this.UpdateForm.V1        = point3d2 - point3d;
                         this.UpdateForm.V2        = point3d3 - point3d;
                         this.UpdateForm.BasePoint = point3d;
                         this.UpdateForm.ImageURL  = this.OutputFile;
                         this.UpdateForm.SetReady(true);
                     }
                 }
             }
         }
     }
     catch (SystemException ex)
     {
         this.UpdateForm.SetReady(false);
         this._errMsg.Append(ex.Message);
     }
 }
        public static string MakeWebRequest(string origRequestURI)
        {
            string text = null;
            string result;

            try
            {
                ServicePointManager.ServerCertificateValidationCallback = ((object obj, X509Certificate x509Certificate, X509Chain x509Chain, SslPolicyErrors sslPolicyErrors) => true);
                bool flag = false;
                while (!flag)
                {
                    WebClient webClient = new WebClient();
                    webClient.Headers.Add("user-agent", "ArcGIS for AutoCAD");
                    webClient.Encoding = Encoding.UTF8;
                    if (AGS_ESRIMaps.IsProxyPresent(origRequestURI))
                    {
                        Uri proxy = WebRequest.DefaultWebProxy.GetProxy(new Uri(origRequestURI));
                        webClient.Proxy = new WebProxy(proxy)
                        {
                            Credentials = WebRequest.DefaultWebProxy.Credentials
                        };
                    }
                    if (AGS_ESRIMaps.Credentials == null)
                    {
                        webClient.Credentials           = CredentialCache.DefaultCredentials;
                        webClient.UseDefaultCredentials = true;
                    }
                    try
                    {
                        text = webClient.DownloadString(new Uri(origRequestURI));
                        flag = true;
                    }
                    catch (WebException ex)
                    {
                        HttpWebResponse httpWebResponse = (HttpWebResponse)ex.Response;
                        string          userName        = "";
                        if (httpWebResponse != null && httpWebResponse.StatusCode == HttpStatusCode.ProxyAuthenticationRequired)
                        {
                            NetworkCredential credentials;
                            if (!PromptForCredentials.GetCredentials("Proxy Authentication", "To connect to the internet, you need to provide credentials", userName, out credentials))
                            {
                                ErrorReport.ShowErrorMessage(httpWebResponse.StatusDescription);
                                AGS_ESRIMaps.ConnectionFailed = true;
                                result = "";
                                return(result);
                            }
                            if (WebRequest.DefaultWebProxy == null)
                            {
                                WebRequest.DefaultWebProxy = new WebProxy();
                            }
                            WebRequest.DefaultWebProxy.Credentials = credentials;
                        }
                        else if (httpWebResponse != null && httpWebResponse.StatusCode == HttpStatusCode.Unauthorized)
                        {
                            NetworkCredential credentials2;
                            if (!PromptForCredentials.GetCredentials("Network Authentication", "Message Text", userName, out credentials2))
                            {
                                ErrorReport.ShowErrorMessage(httpWebResponse.StatusDescription);
                                AGS_ESRIMaps.ConnectionFailed = true;
                                result = "";
                                return(result);
                            }
                            AGS_ESRIMaps.Credentials = credentials2;
                        }
                        else
                        {
                            AGS_ESRIMaps.ConnectionFailed = true;
                            ErrorReport.ShowErrorMessage(httpWebResponse.StatusDescription);
                        }
                    }
                }
                AGS_ESRIMaps.ConnectionFailed = false;
                result = text;
            }
            catch
            {
                AGS_ESRIMaps.ConnectionFailed = true;
                result = "";
            }
            return(result);
        }