Пример #1
0
        public static void AddGeomToCache(string table, int id, SqlGeometry geom)
        {
            if (!_geomCacheByTableThenId.ContainsKey(table))
            {
                _geomCacheByTableThenId[table]             = new Dictionary <int, SqlGeometry>();
                _geomEnvelopeAreaCacheByTableThenId[table] = new Dictionary <int, double>();
                _geomCentroidCacheByTableThenId[table]     = new Dictionary <int, Models.Geometry.Point>();
                _spatialIndexSTR[table] = new STRtree <int>();
            }

            SqlGeometry envelope = geom.STEnvelope();
            Envelope    env      = new Envelope(envelope.STPointN(1).STX.Value, envelope.STPointN(3).STX.Value, envelope.STPointN(1).STY.Value, envelope.STPointN(3).STY.Value);

            _geomCacheByTableThenId[table][id]             = geom;
            _geomEnvelopeAreaCacheByTableThenId[table][id] = envelope.STArea().Value;

            SqlGeometry envelopeCentroid = envelope.STCentroid().STPointN(1);

            _geomCentroidCacheByTableThenId[table][id] = new Models.Geometry.Point((float)envelopeCentroid.STX.Value, (float)envelopeCentroid.STY.Value);


            _spatialIndexSTR[table].Insert(env, id);
        }
        public void SnapToImageBounds()
        {
            // select foreground window from several processes of supported applications
            var nativeWindowsList = Models.AppsInterop.NativeWindow.GetWindowsInTopMostOrder();

            var nativeWindows = nativeWindowsList
                                .Select(w => new Models.NativeWindowState
            {
                Handle    = w.Handle,
                ClassName = w.ClassName,
                Width     = w.Rect.Width,
                Height    = w.Rect.Height,
                Caption   = w.Title,
            })
                                .ToList();

            if (nativeWindows.Count > 0)
            {
                // TODO: refactor - move priority logic to model
                if (String.Compare(nativeWindows[0].ClassName, Models.AppsInterop.PhotoViewerWindow.MainWindowClassName, StringComparison.Ordinal) == 0)
                {
                    var photoViewerWindow = new Models.AppsInterop.PhotoViewerWindow(nativeWindows[0].Handle);

                    var rectViewedImage = photoViewerWindow.PhotoCanvasRect();
                    if (!rectViewedImage.IsEmpty)
                    {
                        var location = new Models.GridTargetLocation {
                            ImageBounds = rectViewedImage, Offset = Models.Geometry.Point.Zero,
                        };
                        this.PositionWindow(location);
                    }
                }
                else
                {
                    var nativeWindow = nativeWindows[0];

                    var isOctaneRender = false;
                    if (Models.AppsInterop.OctaneRenderWindow.GetFromAllProcesses().Count > 0)
                    {
                        var w = SelectOctaneRenderStandaloneMainWindow(nativeWindows, Models.AppsInterop.OctaneRenderWindow.GetFromAllProcesses()[0].ClassName);
                        if (w != null)
                        {
                            nativeWindow   = w;
                            isOctaneRender = true;
                        }
                    }

                    var selectedNativeWindow = new Models.AppsInterop.NativeWindow(nativeWindow.Handle);
                    var bitmap = selectedNativeWindow.GetShot();

                    Task.Factory.StartNew(() =>
                    {
                        var flatImage = new Models.FlatImage(bitmap);

                        Models.Geometry.Rectangle imageBounds;
                        if (isOctaneRender)
                        {
                            imageBounds = Models.AppsInterop.OctaneRenderWindow.FindRenderedImageBorders(flatImage);
                        }
                        else
                        {
                            imageBounds = flatImage.FindBoundsOfInnerImage();
                        }

                        var nativeWindowLocation = new Models.Geometry.Point(selectedNativeWindow.Location.X, selectedNativeWindow.Location.Y);
                        return(new Models.GridTargetLocation {
                            ImageBounds = imageBounds, Offset = nativeWindowLocation,
                        });
                    }).ContinueWith((t) =>
                    {
                        if (!t.Result.ImageBounds.IsEmpty)
                        {
                            if ((t.Result.ImageBounds.Width > 150) && (t.Result.ImageBounds.Height > 50))
                            {
                                this.PositionWindow(t.Result);
                            }
                        }
                    },
                                    TaskScheduler.FromCurrentSynchronizationContext());
                }
            }
        }