Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Text;
- using System.Numerics;
- namespace LDRaw_Parcer
- {
- class Line0
- {
- public Int16 Type { get; }
- public Line0(string line)
- {
- try
- {
- string[] data = line.Split().Where(x => !string.IsNullOrEmpty(x)).ToArray();
- if (String.Join(" ", data) == "0 BFC INVERTNEXT") Type = 0;
- }
- catch { Type = -1; }
- }
- override public string ToString()
- {
- if (Type == 0) return "0 BFC INVERTNEXT";
- return "0 ! bruh";
- }
- }
- class Line1
- {
- public Matrix4x4 transformation;
- public string File { get; }
- public int Color;
- public bool IsInverted;
- public Line1(string line)
- {
- string[] data = line.Split().Where(x => !string.IsNullOrEmpty(x)).ToArray();
- Color = int.Parse(data[1]);
- transformation = new(
- float.Parse(data[5]), float.Parse(data[6]), float.Parse(data[7]), float.Parse(data[2]),
- float.Parse(data[8]), float.Parse(data[9]), float.Parse(data[10]), float.Parse(data[3]),
- float.Parse(data[11]), float.Parse(data[12]), float.Parse(data[13]), float.Parse(data[4]),
- 0, 0, 0, 1
- );
- File = data[14];
- }
- override public string ToString()
- {
- string ldrLine = "1 ";
- ldrLine += Color.ToString() + " ";
- ldrLine += transformation.M14.ToString() + " " + transformation.M24.ToString() + " " + transformation.M34.ToString() + " ";
- ldrLine += transformation.M11.ToString() + " " + transformation.M12.ToString() + " " + transformation.M13.ToString() + " ";
- ldrLine += transformation.M21.ToString() + " " + transformation.M22.ToString() + " " + transformation.M23.ToString() + " ";
- ldrLine += transformation.M31.ToString() + " " + transformation.M32.ToString() + " " + transformation.M33.ToString() + " ";
- ldrLine += File;
- if (IsInverted)
- {
- ldrLine = "0 BFC INVERTNEXT\n" + ldrLine;
- }
- return ldrLine;
- }
- void Transform(Matrix4x4 transform)
- {
- transformation = Matrix4x4.Multiply(transform, transformation);
- }
- public void FitParent(Line1 parent)
- {
- if (Color == 16) Color = parent.Color;
- IsInverted = parent.IsInverted ^ IsInverted;
- Transform(parent.transformation);
- }
- }
- class Line2
- {
- private Vector3[] vertices;
- public int Color;
- public Line2(string line)
- {
- string[] data = line.Split().Where(x => !string.IsNullOrEmpty(x)).ToArray();
- Color = int.Parse(data[1]);
- vertices = new Vector3[]{
- new Vector3(float.Parse(data[2]), float.Parse(data[3]), float.Parse(data[4])),
- new Vector3(float.Parse(data[5]), float.Parse(data[6]), float.Parse(data[7]))
- };
- }
- override public string ToString()
- {
- string ldrLine = "2 ";
- ldrLine += Color.ToString() + " ";
- ldrLine += vertices[0].X.ToString() + " " + vertices[0].Y.ToString() + " " + vertices[0].Z.ToString() + " ";
- ldrLine += vertices[1].X.ToString() + " " + vertices[1].Y.ToString() + " " + vertices[1].Z.ToString() + " ";
- return ldrLine;
- }
- void Transform(Matrix4x4 transform)
- {
- vertices[0] = Vector3.Transform(vertices[0], transform);
- vertices[1] = Vector3.Transform(vertices[1], transform);
- }
- public void FitParent(Line1 parent)
- {
- if (Color == 16) Color = parent.Color;
- Transform(parent.transformation);
- }
- }
- class Line3
- {
- private Vector3[] vertices;
- public int Color;
- public Line3(string line)
- {
- string[] data = line.Split().Where(x => !string.IsNullOrEmpty(x)).ToArray();
- Color = int.Parse(data[1]);
- vertices = new Vector3[]{
- new Vector3(float.Parse(data[2]), float.Parse(data[3]), float.Parse(data[4])),
- new Vector3(float.Parse(data[5]), float.Parse(data[6]), float.Parse(data[7])),
- new Vector3(float.Parse(data[8]), float.Parse(data[9]), float.Parse(data[10]))
- };
- }
- override public string ToString()
- {
- string ldrLine = "3 ";
- ldrLine += Color.ToString() + " ";
- ldrLine += vertices[0].X.ToString() + " " + vertices[0].Y.ToString() + " " + vertices[0].Z.ToString() + " ";
- ldrLine += vertices[1].X.ToString() + " " + vertices[1].Y.ToString() + " " + vertices[1].Z.ToString() + " ";
- ldrLine += vertices[2].X.ToString() + " " + vertices[2].Y.ToString() + " " + vertices[2].Z.ToString() + " ";
- return ldrLine;
- }
- void Transform(Matrix4x4 transform, bool isInverted)
- {
- if ((transform.GetDeterminant() < 0) ^ isInverted)
- vertices = new Vector3[]
- {
- vertices[2],
- vertices[1],
- vertices[0]
- };
- vertices[0] = Vector3.Transform(vertices[0], transform);
- vertices[1] = Vector3.Transform(vertices[1], transform);
- vertices[2] = Vector3.Transform(vertices[2], transform);
- }
- public void FitParent(Line1 parent)
- {
- if (Color == 16) Color = parent.Color;
- Transform(parent.transformation, parent.IsInverted);
- }
- }
- class Line4
- {
- private Vector3[] vertices;
- public int Color;
- public Line4(string line)
- {
- string[] data = line.Split().Where(x => !string.IsNullOrEmpty(x)).ToArray();
- Color = int.Parse(data[1]);
- vertices = new Vector3[]{
- new Vector3(float.Parse(data[2]), float.Parse(data[3]), float.Parse(data[4])),
- new Vector3(float.Parse(data[5]), float.Parse(data[6]), float.Parse(data[7])),
- new Vector3(float.Parse(data[8]), float.Parse(data[9]), float.Parse(data[10])),
- new Vector3(float.Parse(data[11]), float.Parse(data[12]), float.Parse(data[13]))
- };
- }
- override public string ToString()
- {
- string ldrLine = "4 ";
- ldrLine += Color.ToString() + " ";
- ldrLine += vertices[0].X.ToString() + " " + vertices[0].Y.ToString() + " " + vertices[0].Z.ToString() + " ";
- ldrLine += vertices[1].X.ToString() + " " + vertices[1].Y.ToString() + " " + vertices[1].Z.ToString() + " ";
- ldrLine += vertices[2].X.ToString() + " " + vertices[2].Y.ToString() + " " + vertices[2].Z.ToString() + " ";
- ldrLine += vertices[3].X.ToString() + " " + vertices[3].Y.ToString() + " " + vertices[3].Z.ToString() + " ";
- return ldrLine;
- }
- void Transform(Matrix4x4 transform, bool isInverted)
- {
- if ((transform.GetDeterminant() < 0) ^ isInverted)
- vertices = new Vector3[]
- {
- vertices[3],
- vertices[2],
- vertices[1],
- vertices[0]
- };
- vertices[0] = Vector3.Transform(vertices[0], transform);
- vertices[1] = Vector3.Transform(vertices[1], transform);
- vertices[2] = Vector3.Transform(vertices[2], transform);
- vertices[3] = Vector3.Transform(vertices[3], transform);
- }
- public void FitParent(Line1 parent)
- {
- if (Color == 16) Color = parent.Color;
- Transform(parent.transformation, parent.IsInverted);
- }
- }
- class Line5
- {
- private Vector3[] vertices;
- public int Color;
- public Line5(string line)
- {
- string[] data = line.Split().Where(x => !string.IsNullOrEmpty(x)).ToArray();
- Color = int.Parse(data[1]);
- vertices = new Vector3[]{
- new Vector3(float.Parse(data[2]), float.Parse(data[3]), float.Parse(data[4])),
- new Vector3(float.Parse(data[5]), float.Parse(data[6]), float.Parse(data[7])),
- new Vector3(float.Parse(data[8]), float.Parse(data[9]), float.Parse(data[10])),
- new Vector3(float.Parse(data[11]), float.Parse(data[12]), float.Parse(data[13]))
- };
- }
- override public string ToString()
- {
- string ldrLine = "5 ";
- ldrLine += Color.ToString() + " ";
- ldrLine += vertices[0].X.ToString() + " " + vertices[0].Y.ToString() + " " + vertices[0].Z.ToString() + " ";
- ldrLine += vertices[1].X.ToString() + " " + vertices[1].Y.ToString() + " " + vertices[1].Z.ToString() + " ";
- ldrLine += vertices[2].X.ToString() + " " + vertices[2].Y.ToString() + " " + vertices[2].Z.ToString() + " ";
- ldrLine += vertices[3].X.ToString() + " " + vertices[3].Y.ToString() + " " + vertices[3].Z.ToString() + " ";
- return ldrLine;
- }
- void Transform(Matrix4x4 transform)
- {
- vertices[0] = Vector3.Transform(vertices[0], transform);
- vertices[1] = Vector3.Transform(vertices[1], transform);
- vertices[2] = Vector3.Transform(vertices[2], transform);
- vertices[3] = Vector3.Transform(vertices[3], transform);
- }
- public void FitParent(Line1 parent)
- {
- if (Color == 16) Color = parent.Color;
- Transform(parent.transformation);
- }
- }
- class LDRLine
- {
- public Int16 line { get; }
- public object LineData;
- public LDRLine(Line0 line)
- {
- this.line = 0;
- LineData = line;
- }
- public LDRLine(Line1 line)
- {
- this.line = 1;
- LineData = line;
- }
- public LDRLine(Line2 line)
- {
- this.line = 2;
- LineData = line;
- }
- public LDRLine(Line3 line)
- {
- this.line = 3;
- LineData = line;
- }
- public LDRLine(Line4 line)
- {
- this.line = 4;
- LineData = line;
- }
- public LDRLine(Line5 line)
- {
- this.line = 5;
- LineData = line;
- }
- override public string ToString()
- {
- return LineData.ToString();
- }
- public void FitParent(Line1 parent)
- {
- if (line == 1) ((Line1)LineData).FitParent(parent);
- if (line == 2) ((Line2)LineData).FitParent(parent);
- if (line == 3) ((Line3)LineData).FitParent(parent);
- if (line == 4) ((Line4)LineData).FitParent(parent);
- if (line == 5) ((Line5)LineData).FitParent(parent);
- }
- }
- class LDR
- {
- static string ldrDir = @"C:\Users\Public\Documents\LDraw\";
- static string ReplaceLine1(string line1, bool invertnext)
- {
- Console.WriteLine($"start of subfile. inverted? {invertnext}");
- string parcedline1 = "";
- string[] sParams = line1.Split(' ');
- string color = sParams[1];
- Matrix4x4 transform = new(float.Parse(sParams[5]), float.Parse(sParams[6]), float.Parse(sParams[7]), float.Parse(sParams[2]),
- float.Parse(sParams[8]), float.Parse(sParams[9]), float.Parse(sParams[10]), float.Parse(sParams[3]),
- float.Parse(sParams[11]), float.Parse(sParams[12]), float.Parse(sParams[13]), float.Parse(sParams[4]),
- 0, 0, 0, 1);
- Matrix4x4 tmp;
- string subfile = sParams[14].Trim('\r');
- string file;
- if (File.Exists(subfile))
- {
- file = subfile;
- }
- else if (File.Exists(ldrDir + @"p\\" + subfile))
- {
- file = ldrDir + "p\\" + subfile;
- }
- else if (File.Exists(ldrDir + "parts\\" + subfile))
- {
- file = ldrDir + "parts\\" + subfile;
- }
- else if (File.Exists(ldrDir + "Unofficial\\P\\" + subfile))
- {
- file = ldrDir + "Unofficial\\P\\" + subfile;
- }
- else if (File.Exists(ldrDir + "Unofficial\\Parts\\" + subfile))
- {
- file = ldrDir + "Unofficial\\Parts\\" + subfile;
- }
- else
- {
- return "";
- }
- string subfileData = "";
- using (FileStream fs = File.Open(file, FileMode.Open))
- {
- byte[] b = new byte[fs.Length];
- UTF8Encoding temp = new(true);
- while (fs.Read(b, 0, b.Length) > 0)
- {
- subfileData += temp.GetString(b);
- }
- }
- string[] lineInfo;
- int timesinvert = 0;
- foreach (string line in subfileData.Split('\n'))
- {
- string aline = line.Trim('\r');
- if (aline.Length > 0)
- {
- switch (aline[0])
- {
- case '0':
- if (aline.Trim('\r') == "0 BFC INVERTNEXT")
- {
- timesinvert++;
- Console.WriteLine(aline);
- Console.WriteLine("a");
- }
- break;
- case '1':
- lineInfo = aline.Split(' ');
- if (aline.Split(' ')[1] == "16")
- {
- lineInfo[1] = color;
- }
- else Console.WriteLine("b");
- tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
- float.Parse(lineInfo[5]), float.Parse(lineInfo[6]), float.Parse(lineInfo[7]), float.Parse(lineInfo[2]),
- float.Parse(lineInfo[8]), float.Parse(lineInfo[9]), float.Parse(lineInfo[10]), float.Parse(lineInfo[3]),
- float.Parse(lineInfo[11]), float.Parse(lineInfo[12]), float.Parse(lineInfo[13]), float.Parse(lineInfo[4]),
- 0, 0, 0, 1));
- Console.WriteLine(aline);
- if (tmp.GetDeterminant() < 0)
- {
- timesinvert++;
- Console.WriteLine("a");
- }
- else Console.WriteLine("b");
- Console.WriteLine(timesinvert % 2 == 1);
- if (timesinvert % 2 == 1) { invertnext = !invertnext; }
- lineInfo[2] = tmp.M14.ToString();
- lineInfo[3] = tmp.M24.ToString();
- lineInfo[4] = tmp.M34.ToString();
- lineInfo[5] = tmp.M11.ToString();
- lineInfo[6] = tmp.M12.ToString();
- lineInfo[7] = tmp.M13.ToString();
- lineInfo[8] = tmp.M21.ToString();
- lineInfo[9] = tmp.M22.ToString();
- lineInfo[10] = tmp.M23.ToString();
- lineInfo[11] = tmp.M31.ToString();
- lineInfo[12] = tmp.M32.ToString();
- lineInfo[13] = tmp.M33.ToString();
- //parcedline1 += String.Join(' ', lineInfo) + "\n";
- Console.WriteLine($"entering subfile. inverted? {invertnext}");
- parcedline1 += ReplaceLine1(String.Join(' ', lineInfo), invertnext);
- Console.WriteLine($"exiting subfile. inverted? {invertnext}");
- if (timesinvert % 2 == 1) invertnext = !invertnext;
- timesinvert = 0;
- break;
- case '2':
- lineInfo = aline.Split(' ');
- if (aline.Split(' ')[1] == "16")
- {
- lineInfo[1] = color;
- }
- tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
- 0, 0, 0, float.Parse(lineInfo[2]),
- 0, 0, 0, float.Parse(lineInfo[3]),
- 0, 0, 0, float.Parse(lineInfo[4]),
- 0, 0, 0, 1));
- lineInfo[2] = tmp.M14.ToString();
- lineInfo[3] = tmp.M24.ToString();
- lineInfo[4] = tmp.M34.ToString();
- tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
- 0, 0, 0, float.Parse(lineInfo[5]),
- 0, 0, 0, float.Parse(lineInfo[6]),
- 0, 0, 0, float.Parse(lineInfo[7]),
- 0, 0, 0, 1));
- lineInfo[5] = tmp.M14.ToString();
- lineInfo[6] = tmp.M24.ToString();
- lineInfo[7] = tmp.M34.ToString();
- parcedline1 += String.Join(' ', lineInfo) + "\n";
- break;
- case '3':
- lineInfo = aline.Split(' ');
- if (aline.Split(' ')[1] == "16")
- {
- lineInfo[1] = color;
- }
- if (invertnext)
- {
- string[] tmpLineInfo;
- tmpLineInfo = new string[lineInfo.Length];
- lineInfo.CopyTo(tmpLineInfo, 0);
- tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
- 0, 0, 0, float.Parse(tmpLineInfo[2]),
- 0, 0, 0, float.Parse(tmpLineInfo[3]),
- 0, 0, 0, float.Parse(tmpLineInfo[4]),
- 0, 0, 0, 1));
- lineInfo[8] = tmp.M14.ToString();
- lineInfo[9] = tmp.M24.ToString();
- lineInfo[10] = tmp.M34.ToString();
- tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
- 0, 0, 0, float.Parse(tmpLineInfo[5]),
- 0, 0, 0, float.Parse(tmpLineInfo[6]),
- 0, 0, 0, float.Parse(tmpLineInfo[7]),
- 0, 0, 0, 1));
- lineInfo[5] = tmp.M14.ToString();
- lineInfo[6] = tmp.M24.ToString();
- lineInfo[7] = tmp.M34.ToString();
- tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
- 0, 0, 0, float.Parse(tmpLineInfo[8]),
- 0, 0, 0, float.Parse(tmpLineInfo[9]),
- 0, 0, 0, float.Parse(tmpLineInfo[10]),
- 0, 0, 0, 1));
- lineInfo[2] = tmp.M14.ToString();
- lineInfo[3] = tmp.M24.ToString();
- lineInfo[4] = tmp.M34.ToString();
- }
- else
- {
- tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
- 0, 0, 0, float.Parse(lineInfo[2]),
- 0, 0, 0, float.Parse(lineInfo[3]),
- 0, 0, 0, float.Parse(lineInfo[4]),
- 0, 0, 0, 1));
- lineInfo[2] = tmp.M14.ToString();
- lineInfo[3] = tmp.M24.ToString();
- lineInfo[4] = tmp.M34.ToString();
- tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
- 0, 0, 0, float.Parse(lineInfo[5]),
- 0, 0, 0, float.Parse(lineInfo[6]),
- 0, 0, 0, float.Parse(lineInfo[7]),
- 0, 0, 0, 1));
- lineInfo[5] = tmp.M14.ToString();
- lineInfo[6] = tmp.M24.ToString();
- lineInfo[7] = tmp.M34.ToString();
- tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
- 0, 0, 0, float.Parse(lineInfo[8]),
- 0, 0, 0, float.Parse(lineInfo[9]),
- 0, 0, 0, float.Parse(lineInfo[10]),
- 0, 0, 0, 1));
- lineInfo[8] = tmp.M14.ToString();
- lineInfo[9] = tmp.M24.ToString();
- lineInfo[10] = tmp.M34.ToString();
- }
- parcedline1 += String.Join(' ', lineInfo) + "\n";
- break;
- case '4':
- lineInfo = aline.Split(' ');
- if (aline.Split(' ')[1] == "16")
- {
- lineInfo[1] = color;
- }
- if (invertnext)
- {
- string[] tmpLineInfo;
- tmpLineInfo = new string[lineInfo.Length];
- lineInfo.CopyTo(tmpLineInfo, 0);
- tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
- 0, 0, 0, float.Parse(tmpLineInfo[2]),
- 0, 0, 0, float.Parse(tmpLineInfo[3]),
- 0, 0, 0, float.Parse(tmpLineInfo[4]),
- 0, 0, 0, 1));
- lineInfo[11] = tmp.M14.ToString();
- lineInfo[12] = tmp.M24.ToString();
- lineInfo[13] = tmp.M34.ToString();
- tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
- 0, 0, 0, float.Parse(tmpLineInfo[5]),
- 0, 0, 0, float.Parse(tmpLineInfo[6]),
- 0, 0, 0, float.Parse(tmpLineInfo[7]),
- 0, 0, 0, 1));
- lineInfo[8] = tmp.M14.ToString();
- lineInfo[9] = tmp.M24.ToString();
- lineInfo[10] = tmp.M34.ToString();
- tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
- 0, 0, 0, float.Parse(tmpLineInfo[8]),
- 0, 0, 0, float.Parse(tmpLineInfo[9]),
- 0, 0, 0, float.Parse(tmpLineInfo[10]),
- 0, 0, 0, 1));
- lineInfo[5] = tmp.M14.ToString();
- lineInfo[6] = tmp.M24.ToString();
- lineInfo[7] = tmp.M34.ToString();
- tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
- 0, 0, 0, float.Parse(tmpLineInfo[11]),
- 0, 0, 0, float.Parse(tmpLineInfo[12]),
- 0, 0, 0, float.Parse(tmpLineInfo[13]),
- 0, 0, 0, 1));
- lineInfo[2] = tmp.M14.ToString();
- lineInfo[3] = tmp.M24.ToString();
- lineInfo[4] = tmp.M34.ToString();
- }
- else
- {
- tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
- 0, 0, 0, float.Parse(lineInfo[2]),
- 0, 0, 0, float.Parse(lineInfo[3]),
- 0, 0, 0, float.Parse(lineInfo[4]),
- 0, 0, 0, 1));
- lineInfo[2] = tmp.M14.ToString();
- lineInfo[3] = tmp.M24.ToString();
- lineInfo[4] = tmp.M34.ToString();
- tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
- 0, 0, 0, float.Parse(lineInfo[5]),
- 0, 0, 0, float.Parse(lineInfo[6]),
- 0, 0, 0, float.Parse(lineInfo[7]),
- 0, 0, 0, 1));
- lineInfo[5] = tmp.M14.ToString();
- lineInfo[6] = tmp.M24.ToString();
- lineInfo[7] = tmp.M34.ToString();
- tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
- 0, 0, 0, float.Parse(lineInfo[8]),
- 0, 0, 0, float.Parse(lineInfo[9]),
- 0, 0, 0, float.Parse(lineInfo[10]),
- 0, 0, 0, 1));
- lineInfo[8] = tmp.M14.ToString();
- lineInfo[9] = tmp.M24.ToString();
- lineInfo[10] = tmp.M34.ToString();
- tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
- 0, 0, 0, float.Parse(lineInfo[11]),
- 0, 0, 0, float.Parse(lineInfo[12]),
- 0, 0, 0, float.Parse(lineInfo[13]),
- 0, 0, 0, 1));
- lineInfo[11] = tmp.M14.ToString();
- lineInfo[12] = tmp.M24.ToString();
- lineInfo[13] = tmp.M34.ToString();
- }
- parcedline1 += String.Join(' ', lineInfo) + "\n";
- break;
- case '5':
- lineInfo = aline.Split(' ');
- if (aline.Split(' ')[1] == "16")
- {
- lineInfo[1] = color;
- }
- tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
- 0, 0, 0, float.Parse(lineInfo[2]),
- 0, 0, 0, float.Parse(lineInfo[3]),
- 0, 0, 0, float.Parse(lineInfo[4]),
- 0, 0, 0, 1));
- lineInfo[2] = tmp.M14.ToString();
- lineInfo[3] = tmp.M24.ToString();
- lineInfo[4] = tmp.M34.ToString();
- tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
- 0, 0, 0, float.Parse(lineInfo[5]),
- 0, 0, 0, float.Parse(lineInfo[6]),
- 0, 0, 0, float.Parse(lineInfo[7]),
- 0, 0, 0, 1));
- lineInfo[5] = tmp.M14.ToString();
- lineInfo[6] = tmp.M24.ToString();
- lineInfo[7] = tmp.M34.ToString();
- tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
- 0, 0, 0, float.Parse(lineInfo[8]),
- 0, 0, 0, float.Parse(lineInfo[9]),
- 0, 0, 0, float.Parse(lineInfo[10]),
- 0, 0, 0, 1));
- lineInfo[8] = tmp.M14.ToString();
- lineInfo[9] = tmp.M24.ToString();
- lineInfo[10] = tmp.M34.ToString();
- tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
- 0, 0, 0, float.Parse(lineInfo[11]),
- 0, 0, 0, float.Parse(lineInfo[12]),
- 0, 0, 0, float.Parse(lineInfo[13]),
- 0, 0, 0, 1));
- lineInfo[11] = tmp.M14.ToString();
- lineInfo[12] = tmp.M24.ToString();
- lineInfo[13] = tmp.M34.ToString();
- parcedline1 += String.Join(' ', lineInfo) + "\n";
- break;
- }
- }
- }
- return parcedline1;
- }
- static string ReadFile(string file)
- {
- string path;
- string fileText = "";
- if (File.Exists(file))
- {
- path = file;
- }
- else if (File.Exists(ldrDir + "Unofficial\\P\\" + file))
- {
- path = ldrDir + "Unofficial\\P\\" + file;
- }
- else if (File.Exists(ldrDir + "Unofficial\\Parts\\" + file))
- {
- path = ldrDir + "Unofficial\\Parts\\" + file;
- }
- else if (File.Exists(ldrDir + @"p\\" + file))
- {
- path = ldrDir + "p\\" + file;
- }
- else if (File.Exists(ldrDir + "parts\\" + file))
- {
- path = ldrDir + "parts\\" + file;
- }
- else
- {
- return "";
- }
- using (FileStream fs = File.Open(path, FileMode.Open))
- {
- byte[] b = new byte[fs.Length];
- UTF8Encoding temp = new(true);
- while (fs.Read(b, 0, b.Length) > 0)
- {
- fileText += temp.GetString(b);
- }
- }
- return fileText;
- }
- static List<LDRLine> ParseLine1(Line1 line)
- {
- List<LDRLine> file = new();
- string fileText = ReadFile(line.File);
- fileText = fileText.Replace("\r", "");
- string[] fileData = fileText.Split("\n").Where(x => !string.IsNullOrEmpty(x)).ToArray();
- file = file.Where(x => x.line != 0).ToList();
- foreach (string lineData in fileData)
- {
- switch (lineData[0])
- {
- case '0':
- file.Add(new LDRLine(new Line0(lineData)));
- break;
- case '1':
- /*Line1 line1ToParce = new(lineData);
- line1ToParce.FitParent(line);
- List<LDRLine> parcedLine = ParseLine1(line1ToParce);
- foreach (LDRLine ln in parcedLine)
- {
- file.Add(ln);
- }*/
- Line1 line1ToParce = new(lineData);
- try
- {
- if (((Line0)file[file.Count - 1].LineData).Type == 0)
- {
- line1ToParce.IsInverted = !line1ToParce.IsInverted;
- }
- }
- catch { }
- line1ToParce.FitParent(line);
- List<LDRLine> parcedLine = ParseLine1(line1ToParce);
- foreach (LDRLine ln in parcedLine)
- {
- file.Add(ln);
- }
- break;
- case '2':
- Line2 line2ToParce = new(lineData);
- line2ToParce.FitParent(line);
- file.Add(new LDRLine(
- line2ToParce
- ));
- break;
- case '3':
- Line3 line3ToParce = new(lineData);
- line3ToParce.FitParent(line);
- file.Add(new LDRLine(
- line3ToParce
- ));
- break;
- case '4':
- Line4 line4ToParce = new(lineData);
- line4ToParce.FitParent(line);
- file.Add(new LDRLine(
- line4ToParce
- ));
- break;
- case '5':
- Line5 line5ToParce = new(lineData);
- line5ToParce.FitParent(line);
- file.Add(new LDRLine(
- line5ToParce
- ));
- break;
- }
- }
- return file;
- }
- static void Main(string[] args)
- {
- string ldr = "0 Retopo_object1.001\r\n0 Name: Retopo_object1.001.dat\r\n0 Author: [converted by blender2ldr]\r\n0 !LDRAW_ORG Unofficial_Part\r\n0 !LICENSE Redistributable under CCAL version 2.0 : see CAreadme.txt\r\n0 BFC CERTIFY CCW\r\n";
- List<LDRLine> data = ParseLine1(new Line1("1 16 0 0 0 1 0 0 0 1 0 0 0 1 32174.dat")).Where(x => x.line != 0).ToList();
- foreach (LDRLine line in data)
- {
- ldr += line.ToString() + "\n";
- }
- using (FileStream fstream = new FileStream("a.ldr", FileMode.OpenOrCreate))
- {
- byte[] buffer = Encoding.Default.GetBytes(ldr);
- fstream.Write(buffer, 0, buffer.Length);
- Console.WriteLine("Текст записан в файл");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement