Пример #1
0
        public static PartWrapper GetPartFromLif(LifFile lif, int partID, bool loadMeshes)
        {
            var primitiveFolder = lif.GetFolder("Primitives");
            var meshesFolder    = primitiveFolder.GetFolder("LOD0");

            var primitiveEntry = primitiveFolder.GetFile($"{partID}.xml");

            if (primitiveEntry == null)
            {
                throw new FileNotFoundException($"Primitive file not found. ({partID}.xml)");
            }

            var surfaces = new List <PartSurfaceMesh>();

            foreach (var meshEntry in meshesFolder.GetFiles($"{partID}.g*"))
            {
                if (!PartSurfaceMesh.ParseSurfaceID(meshEntry.Name, out int surfID))
                {
                    surfID = surfaces.Count;
                }

                var surfaceInfo = new PartSurfaceMesh(partID, surfID,
                                                      loadMeshes ? MeshFile.Read(meshEntry.GetStream()) : null);
                surfaces.Add(surfaceInfo);
            }

            if (!surfaces.Any())
            {
                throw new FileNotFoundException($"Mesh file not found. ({partID}.g)");
            }

            var primitive = Primitive.Load(primitiveEntry.GetStream());

            primitive.ID = partID;
            return(new PartWrapper(primitive, surfaces)
            {
                PartID = partID
            });
        }
Пример #2
0
        public static PartWrapper GetPartFromDirectory(string path, int partID, bool loadMeshes)
        {
            //var primitivesDir = Path.Combine(path, "db\\Primitives");
            //var meshesDir = Path.Combine(path, "db\\Primitives\\LOD0");

            var primitivesDir = path;
            var meshesDir     = Path.Combine(path, "LOD0");

            var primitiveFile = Path.Combine(primitivesDir, $"{partID}.xml");

            if (!File.Exists(primitiveFile))
            {
                throw new FileNotFoundException($"Primitive file not found. ({partID}.xml)");
            }

            var surfaces = new List <PartSurfaceMesh>();

            foreach (string meshPath in Directory.GetFiles(meshesDir, $"{partID}.g*"))
            {
                if (!PartSurfaceMesh.ParseSurfaceID(meshPath, out int surfID))
                {
                    surfID = surfaces.Count;
                }

                surfaces.Add(new PartSurfaceMesh(partID, surfID, loadMeshes ? MeshFile.Read(meshPath) : null));
            }

            if (!surfaces.Any())
            {
                throw new FileNotFoundException($"Mesh file not found. ({partID}.g)");
            }

            return(new PartWrapper(Primitive.Load(primitiveFile), surfaces)
            {
                PartID = partID
            });
        }
Пример #3
0
        public static void CreateLDDPackages()
        {
            string lddDir        = Environment.ExpandEnvironmentVariables(@"%appdata%\LEGO Company\LEGO Digital Designer\");
            string primitiveDir  = Path.Combine(lddDir, "db\\Primitives");
            string meshDir       = Path.Combine(lddDir, "db\\Primitives\\LOD0");
            string decorationDir = Path.Combine(lddDir, "db\\Decorations");
            var    lddPalette    = PaletteFile.FromLif(Path.Combine(lddDir, "Palettes\\LDD.lif")).Palettes[0];
            var    xmlDecMap     = XDocument.Load(Path.Combine(lddDir, "db\\DecorationMapping.xml"));
            var    decMappings   = new List <LDD.Data.DecorationMapping>();

            foreach (var elem in xmlDecMap.Root.Elements("Mapping"))
            {
                decMappings.Add(XmlHelper.DefaultDeserialize <LDD.Data.DecorationMapping>(elem));
            }

            foreach (var primitivePath in Directory.GetFiles(primitiveDir, "*.xml")
                     .OrderBy(x => int.Parse(Path.GetFileNameWithoutExtension(x))))
            {
                var primitive = Primitive.Load(primitivePath);

                var package = new PartPackage()
                {
                    PartID    = int.Parse(Path.GetFileNameWithoutExtension(primitivePath)),
                    Primitive = primitive
                };
                var myDecorations = decMappings.Where(x => x.DesignID == package.PartID);

                if (myDecorations.Any())
                {
                    package.DecorationMappings.AddRange(
                        myDecorations.Select(x =>
                                             new LDD.Palettes.Decoration(x.SurfaceID, x.DecorationID)));

                    foreach (string decID in package.DecorationMappings.Select(x => x.DecorationID).Distinct())
                    {
                        var imagePath = Directory.EnumerateFiles(decorationDir, decID + ".*").FirstOrDefault();
                        if (string.IsNullOrEmpty(imagePath))
                        {
                            continue;
                        }
                        var img = Image.FromFile(imagePath);
                        package.DecorationImages.Add(new DecorationImage(decID, img));
                    }
                }

                var myElements = lddPalette.Bricks.Where(x => x.DesignID == package.PartID);
                if (myElements.Any())
                {
                    package.Configurations.AddRange(myElements);
                }

                int surfaceId = 0;
                foreach (var meshPath in Directory.GetFiles(meshDir, package.PartID + ".g*").OrderBy(x => x))
                {
                    var mesh = MeshFile.Read(meshPath);
                    package.Meshes.Add(new PartMesh(package.PartID, surfaceId++, mesh));
                }

                string folderName = package.PartID.ToString()[0].ToString().PadRight(package.PartID.ToString().Length, '0');

                package.Save($"LPI TEST\\{folderName}\\{package.PartID}.lpi");

                package.Dispose();
            }
        }