//--------------------------------------------------------------------------------------------------

        internal void InitElements(Dictionary <int, Pnt2d> tempPoints)
        {
            var sketch = _SketchEditorTool.Sketch;
            var plane  = sketch.Plane;

            foreach (var segmentKvp in sketch.Segments)
            {
                var element = new SketchEditorSegmentElement(_SketchEditorTool, segmentKvp.Key, segmentKvp.Value, _SketchEditorTool.Transform, plane);
                element.OnPointsChanged(tempPoints, sketch.Segments, _MarkerCounts);
                SegmentElements.Add(element);
            }

            foreach (var constraint in sketch.Constraints)
            {
                var element = new SketchEditorConstraintElement(_SketchEditorTool, constraint, _SketchEditorTool.Transform, plane);
                element.OnPointsChanged(tempPoints, sketch.Segments, _MarkerCounts);
                ConstraintElements.Add(element);
            }

            foreach (var pointKvp in sketch.Points)
            {
                var element = new SketchEditorPointElement(_SketchEditorTool, pointKvp.Key, pointKvp.Value, _SketchEditorTool.Transform, plane);
                PointElements.Add(element);
            }

            _UpdateElementList();

            Elements.ForEach(c =>
            {
                c.Activate(true);
                c.UpdateVisual();
            });

            _SketchEditorTool.WorkspaceController.Invalidate();
        }
        //--------------------------------------------------------------------------------------------------

        internal void OnSketchChanged(Sketch sketch, Sketch.ElementType types)
        {
            if (sketch == _SketchEditorTool.Sketch)
            {
                if (types.HasFlag(Sketch.ElementType.Point))
                {
                    // Check for lost points
                    var lostPoints = PointElements.Where(pc => !sketch.Points.ContainsKey(pc.PointIndex)).ToArray();
                    foreach (var lostPoint in lostPoints)
                    {
                        lostPoint.Remove();
                        PointElements.Remove(lostPoint);
                    }
                }
                if (types.HasFlag(Sketch.ElementType.Segment))
                {
                    // Check for lost segments
                    var lostSegs = SegmentElements.Where(sc => !sketch.Segments.ContainsValue(sc.Segment)).ToArray();
                    foreach (var lostSeg in lostSegs)
                    {
                        lostSeg.Remove();
                        SegmentElements.Remove(lostSeg);
                    }
                }
                if (types.HasFlag(Sketch.ElementType.Constraint))
                {
                    // Check for lost constraints
                    var lostCons = ConstraintElements.Where(cc => !sketch.Constraints.Contains(cc.Constraint)).ToArray();
                    foreach (var lostCon in lostCons)
                    {
                        lostCon.Remove();
                        ConstraintElements.Remove(lostCon);
                    }
                }

                // Check for new ones
                if (types.HasFlag(Sketch.ElementType.Point))
                {
                    // Check for new points
                    var newPoints = sketch.Points.Where(kvp => !PointElements.Exists(pc => pc.PointIndex == kvp.Key));
                    foreach (var pointKvp in newPoints)
                    {
                        var element = new SketchEditorPointElement(_SketchEditorTool, pointKvp.Key, pointKvp.Value, _SketchEditorTool.Transform, sketch.Plane);
                        element.UpdateVisual();
                        element.Activate(true);
                        PointElements.Add(element);
                    }
                }
                if (types.HasFlag(Sketch.ElementType.Segment))
                {
                    // Check for new segments
                    var newSegs = sketch.Segments.Where(seg => !SegmentElements.Exists(sc => sc.Segment == seg.Value));
                    foreach (var segmentKvp in newSegs)
                    {
                        var element = new SketchEditorSegmentElement(_SketchEditorTool, segmentKvp.Key, segmentKvp.Value, _SketchEditorTool.Transform, sketch.Plane);
                        element.OnPointsChanged(sketch.Points, sketch.Segments, _MarkerCounts);
                        element.UpdateVisual();
                        element.Activate(true);
                        SegmentElements.Add(element);
                    }
                }
                if (types.HasFlag(Sketch.ElementType.Constraint))
                {
                    // Check for new constraints
                    var newCons = sketch.Constraints.Where(con => !ConstraintElements.Exists(cc => cc.Constraint == con));
                    foreach (var sketchConstraint in newCons)
                    {
                        var element = new SketchEditorConstraintElement(_SketchEditorTool, sketchConstraint, _SketchEditorTool.Transform, sketch.Plane);
                        element.OnPointsChanged(sketch.Points, sketch.Segments, _MarkerCounts);
                        element.UpdateVisual();
                        element.Activate(true);
                        ConstraintElements.Add(element);
                    }
                }

                // Update elements
                _UpdateElementList();

                if (types.HasFlag(Sketch.ElementType.Point))
                {
                    OnPointsChanged(sketch.Points, sketch.Segments);
                }

                _SketchEditorTool.WorkspaceController.Invalidate();
            }
        }