private static bool GetTopMatrix(IMyPistonBase block, MyCubeSize topSize, out MatrixD matrix, MyCubeBlockDefinition def = null)
        {
            MyEntitySubpart subpart1;
            MyEntitySubpart subpart2;
            MyEntitySubpart subpart3;

            if (block.Closed || !block.TryGetSubpart("PistonSubpart1", out subpart1) || (!subpart1.Subparts.TryGetValue("PistonSubpart2", out subpart2) || !subpart2.Subparts.TryGetValue("PistonSubpart3", out subpart3)))
            {
                matrix = MatrixD.Identity;
                return(false);
            }

            IMyModelDummy subpart;

            if (!TryGetDummy("TopBlock", subpart3.Model, out subpart))
            {
                matrix = MatrixD.Identity;
                return(false);
            }

            MatrixD m = block.WorldMatrix;

            m.Translation = Vector3D.Transform(subpart.Matrix.Translation, subpart3.WorldMatrix);

            if (block.CubeGrid != null && topSize == block.CubeGrid.GridSizeEnum)
            {
                if (def == null)
                {
                    def = GetTopDef(block, topSize);
                }
                if (def.Center != Vector3.Zero)
                {
                    m.Translation = Vector3D.Transform(-def.Center * MyDefinitionManager.Static.GetCubeSize(topSize), m);
                }
            }
            else if (topSize == MyCubeSize.Small)
            {
                m.Translation += m.Up * 0.5;
            }

            matrix = m;
            return(true);
        }