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

        public override void OnDeserialized(SerializationContext context)
        {
            base.OnDeserialized(context);

            if (context.Scope == SerializationScope.CopyPaste && _UpdateAssociatedShape() != null)
            {
                // Check if the back reference is still valid
                if (_AssociatedShape.Guid != Guid)
                {
                    // Create Clone
                    var boxJoint2 = new BoxJoint();
                    boxJoint2.AddOperand(new BodyShapeOperand(Body, this));
                    boxJoint2.RemoveExcess    = _RemoveExcess;
                    boxJoint2.IsFirst         = !_IsFirst;
                    boxJoint2.ReverseOrder    = _ReverseOrder;
                    boxJoint2.Ratio           = 1.0 - _Ratio;
                    boxJoint2.CustomBoxRatios = _CustomBoxRatios;

                    _AssociatedShape.Body.AddShape(boxJoint2);
                    Operands[1] = new BodyShapeOperand(boxJoint2.Body, boxJoint2);

                    RaisePropertyChanged(nameof(BoxCount));
                    RaisePropertyChanged(nameof(ReverseOrder));
                    RaisePropertyChanged(nameof(Ratio));
                    RaisePropertyChanged(nameof(CustomBoxRatios));
                }
            }

            _UpdateAssociatedShape();
        }
        //--------------------------------------------------------------------------------------------------

        void _AssociatedShape_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (sender != _AssociatedShape)
            {
                return;
            }

            if (e.PropertyName == nameof(BoxCount))
            {
                BoxCount = _AssociatedShape.BoxCount;
            }
            else if (e.PropertyName == nameof(ReverseOrder))
            {
                ReverseOrder = _AssociatedShape.ReverseOrder;
            }
            else if (e.PropertyName == nameof(Ratio))
            {
                Ratio = 1.0 - _AssociatedShape.Ratio;
            }
            else if (e.PropertyName == nameof(CustomBoxRatios))
            {
                CustomBoxRatios = _AssociatedShape.CustomBoxRatios;
            }
            else if (e.PropertyName == nameof(Body))
            {
                if (_AssociatedShape != null && _AssociatedShape.Body == null)
                {
                    _AssociatedShape = null;
                    Body?.RemoveShape(this);
                }
            }
        }
        //--------------------------------------------------------------------------------------------------

        BoxJoint _UpdateAssociatedShape()
        {
            if (_AssociatedShape != null)
            {
                _AssociatedShape.PropertyChanged -= _AssociatedShape_PropertyChanged;
                _AssociatedShape.RemoveDependent(this);
            }

            _AssociatedShape = null;

            if (Operands.Count != 2)
            {
                return(null);
            }

            var secondBodyOperand = GetOperand(1) as BodyShapeOperand;

            if (secondBodyOperand == null)
            {
                return(null);
            }

            _AssociatedShape = secondBodyOperand.Shape as BoxJoint;
            if (_AssociatedShape == null)
            {
                return(null);
            }

            _AssociatedShape.PropertyChanged += _AssociatedShape_PropertyChanged;
            _AssociatedShape.AddDependent(this);

            return(_AssociatedShape);
        }
        //--------------------------------------------------------------------------------------------------

        public static (BoxJoint First, BoxJoint Second) Create(Body body1, Body body2)
        {
            Debug.Assert(body1 != null);
            Debug.Assert(body2 != null);

            var boxJoint1 = new BoxJoint();
            var boxJoint2 = new BoxJoint();

            boxJoint1.AddOperand(new BodyShapeOperand(body2, boxJoint2));
            boxJoint2.AddOperand(new BodyShapeOperand(body1, boxJoint1));

            body1.AddShape(boxJoint1);
            body2.AddShape(boxJoint2);

            boxJoint1._UpdateAssociatedShape();
            boxJoint2._UpdateAssociatedShape();

            boxJoint1.IsFirst = true;
            boxJoint2.IsFirst = false;

            return(boxJoint1, boxJoint2);
        }