public void ConnectToShapes(StateShapeCollectionViewModel shapes)
        {
            _aVm = ParentVm.FindShape(ModelObject.AName, shapes);
            if (_aVm == null)
            {
                MessageBox.Show("The shape A pointed to by " + ModelObject.Name + " does not exists in CO " + ParentVm.Name, "Error parsing file", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            _bVm = ParentVm.FindShape(ModelObject.BName, shapes);
            if (_bVm == null)
            {
                MessageBox.Show("The shape B pointed to by " + ModelObject.Name + " does not exists in CO " + ParentVm.Name, "Error parsing file", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        public LfShapeViewModel FindShape(string name, StateShapeCollectionViewModel shapes)
        {
            foreach (object o in shapes.Shapes)
            {
                if (o is LfShapeViewModel)
                {
                    LfShapeViewModel shape = (LfShapeViewModel)o;

                    if (shape.Name == name)
                    {
                        return(shape);
                    }
                }
            }

            return(null);
        }
        public void BuildViewModel(bool enabledChildren = true)
        // This method connects references in the COVM. At the creation of the COVM
        // the ModelObject, ModelObjectProperties, ParentVm and ChildObjectOfParent is defined.
        // Here we build the shape, joint and system collections and connect those objects
        // with the necessary references. The ChildObjects are also added to the corresponding
        // state index in the StateChildObjects collection
        {
//         _statesWithChildObjects.Clear();
            _treeCollection.Clear();

            _shapes  = SetShapes(ModelObject, enabledChildren);
            _joints  = SetJoints(ModelObject, enabledChildren);
            _systems = SetSystems(ModelObject, enabledChildren);

            // Only now is the Joints property valid for this state
            foreach (object o in _joints.Joints)
            {
                if (o is WeldJointViewModel)
                {
                    if (o is RopeViewModel)
                    {
                        RopeViewModel joint = (RopeViewModel)o;

                        joint.ConnectToShapes(_shapes);
                    }
                    else
                    {
                        WeldJointViewModel joint = (WeldJointViewModel)o;

                        joint.ConnectToShapes(_shapes);
                    }
                }
            }

            ChildObjectsWithStates = SetChildren(ModelObject, enabledChildren);

            BuildTreeViewCollection();
        }
        private StateShapeCollectionViewModel SetShapes(CompoundObject co, bool enabledChildren = true)
        {
            StateShapeCollectionViewModel shapes = new StateShapeCollectionViewModel(this, this, MainVm, enabledChildren);

            foreach (LfSpriteBox sb in co.SpriteBoxes)
            {
                LfSpriteBoxViewModel shapevm = new LfSpriteBoxViewModel(shapes, this, MainVm, sb, enabledChildren);
                shapes.Shapes.Add(shapevm);
            }

            foreach (LfSpritePolygon sp in co.SpritePolygons)
            {
                LfSpritePolygonViewModel shapevm = new LfSpritePolygonViewModel(shapes, this, MainVm, sp, enabledChildren);

                foreach (LfDragablePoint dragPoint in sp.Points)
                {
                    LfDragablePointViewModel dragPointVm = new LfDragablePointViewModel(shapevm, this, MainVm, shapevm, dragPoint, enabledChildren);
                    shapevm.PointVms.Add(dragPointVm);
                }

                shapes.Shapes.Add(shapevm);
            }

            foreach (LfStaticBox sb in co.StaticBoxes)
            {
                LfStaticBoxViewModel shapevm = new LfStaticBoxViewModel(shapes, this, MainVm, sb, enabledChildren);
                shapes.Shapes.Add(shapevm);
            }

            foreach (LfStaticCircle sb in co.StaticCircles)
            {
                LfStaticCircleViewModel shapevm = new LfStaticCircleViewModel(shapes, this, MainVm, sb, enabledChildren);
                shapes.Shapes.Add(shapevm);
            }

            foreach (LfStaticPolygon sp in co.StaticPolygons)
            {
                LfStaticPolygonViewModel shapevm = new LfStaticPolygonViewModel(shapes, this, MainVm, sp, enabledChildren);

                foreach (LfDragablePoint dragPoint in sp.Points)
                {
                    LfDragablePointViewModel dragPointVm = new LfDragablePointViewModel(shapevm, this, MainVm, shapevm, dragPoint, enabledChildren);
                    shapevm.PointVms.Add(dragPointVm);
                }

                shapes.Shapes.Add(shapevm);
            }

            foreach (LfDynamicBox db in co.DynamicBoxes)
            {
                LfDynamicBoxViewModel shapevm = new LfDynamicBoxViewModel(shapes, this, MainVm, db, enabledChildren);
                shapes.Shapes.Add(shapevm);
            }

            foreach (LfDynamicCircle db in co.DynamicCircles)
            {
                LfDynamicCircleViewModel shapevm = new LfDynamicCircleViewModel(shapes, this, MainVm, db, enabledChildren);
                shapes.Shapes.Add(shapevm);
            }

            foreach (LfDynamicPolygon dp in co.DynamicPolygons)
            {
                LfDynamicPolygonViewModel shapevm = new LfDynamicPolygonViewModel(shapes, this, MainVm, dp, enabledChildren);

                foreach (LfDragablePoint dragPoint in dp.Points)
                {
                    LfDragablePointViewModel dragPointVm = new LfDragablePointViewModel(shapevm, this, MainVm, shapevm, dragPoint, enabledChildren);
                    shapevm.PointVms.Add(dragPointVm);
                }

                shapes.Shapes.Add(shapevm);
            }

            foreach (LfStaticBoxedSpritePolygon bsp in co.StaticBoxedSpritePolygons)
            {
                LfStaticBoxedSpritePolygonViewModel shapevm = new LfStaticBoxedSpritePolygonViewModel(shapes, this, MainVm, bsp, enabledChildren);

                foreach (LfDragablePoint dragPoint in bsp.Points)
                {
                    LfDragablePointViewModel dragPointVm = new LfDragablePointViewModel(shapevm, this, MainVm, shapevm, dragPoint, enabledChildren);
                    shapevm.PointVms.Add(dragPointVm);
                }

                shapes.Shapes.Add(shapevm);
            }

            foreach (LfDynamicBoxedSpritePolygon bsp in co.DynamicBoxedSpritePolygons)
            {
                LfDynamicBoxedSpritePolygonViewModel shapevm = new LfDynamicBoxedSpritePolygonViewModel(shapes, this, MainVm, bsp, enabledChildren);

                foreach (LfDragablePoint dragPoint in bsp.Points)
                {
                    LfDragablePointViewModel dragPointVm = new LfDragablePointViewModel(shapevm, this, MainVm, shapevm, dragPoint, enabledChildren);
                    shapevm.PointVms.Add(dragPointVm);
                }

                shapes.Shapes.Add(shapevm);
            }

            return(shapes);
        }