Advertisement
Guest User

custom ldraw parser - smth is wrong

a guest
Apr 4th, 2023
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 34.59 KB | None | 0 0
  1. using System.Text;
  2. using System.Numerics;
  3.  
  4. namespace LDRaw_Parcer
  5. {
  6.     class Line0
  7.     {
  8.         public Int16 Type { get; }
  9.  
  10.         public Line0(string line)
  11.         {
  12.             try
  13.             {
  14.                 string[] data = line.Split().Where(x => !string.IsNullOrEmpty(x)).ToArray();
  15.  
  16.                 if (String.Join(" ", data) == "0 BFC INVERTNEXT") Type = 0;
  17.             }
  18.             catch { Type = -1; }
  19.         }
  20.         override public string ToString()
  21.         {
  22.             if (Type == 0) return "0 BFC INVERTNEXT";
  23.             return "0 ! bruh";
  24.         }
  25.     }
  26.  
  27.     class Line1
  28.     {
  29.         public Matrix4x4 transformation;
  30.         public string File { get; }
  31.         public int Color;
  32.         public bool IsInverted;
  33.  
  34.         public Line1(string line)
  35.         {
  36.             string[] data = line.Split().Where(x => !string.IsNullOrEmpty(x)).ToArray();
  37.  
  38.             Color = int.Parse(data[1]);
  39.             transformation = new(
  40.                 float.Parse(data[5]), float.Parse(data[6]), float.Parse(data[7]), float.Parse(data[2]),
  41.                 float.Parse(data[8]), float.Parse(data[9]), float.Parse(data[10]), float.Parse(data[3]),
  42.                 float.Parse(data[11]), float.Parse(data[12]), float.Parse(data[13]), float.Parse(data[4]),
  43.                 0, 0, 0, 1
  44.                 );
  45.             File = data[14];
  46.         }
  47.  
  48.         override public string ToString()
  49.         {
  50.             string ldrLine = "1 ";
  51.  
  52.             ldrLine += Color.ToString() + " ";
  53.  
  54.             ldrLine += transformation.M14.ToString() + " " + transformation.M24.ToString() + " " + transformation.M34.ToString() + " ";
  55.             ldrLine += transformation.M11.ToString() + " " + transformation.M12.ToString() + " " + transformation.M13.ToString() + " ";
  56.             ldrLine += transformation.M21.ToString() + " " + transformation.M22.ToString() + " " + transformation.M23.ToString() + " ";
  57.             ldrLine += transformation.M31.ToString() + " " + transformation.M32.ToString() + " " + transformation.M33.ToString() + " ";
  58.  
  59.             ldrLine += File;
  60.  
  61.             if (IsInverted)
  62.             {
  63.                 ldrLine = "0 BFC INVERTNEXT\n" + ldrLine;
  64.             }
  65.  
  66.             return ldrLine;
  67.         }
  68.  
  69.         void Transform(Matrix4x4 transform)
  70.         {
  71.             transformation = Matrix4x4.Multiply(transform, transformation);
  72.         }
  73.  
  74.         public void FitParent(Line1 parent)
  75.         {
  76.             if (Color == 16) Color = parent.Color;
  77.             IsInverted = parent.IsInverted ^ IsInverted;
  78.             Transform(parent.transformation);
  79.         }
  80.     }
  81.  
  82.     class Line2
  83.     {
  84.         private Vector3[] vertices;
  85.         public int Color;
  86.  
  87.         public Line2(string line)
  88.         {
  89.             string[] data = line.Split().Where(x => !string.IsNullOrEmpty(x)).ToArray();
  90.  
  91.             Color = int.Parse(data[1]);
  92.             vertices = new Vector3[]{
  93.                 new Vector3(float.Parse(data[2]), float.Parse(data[3]), float.Parse(data[4])),
  94.                 new Vector3(float.Parse(data[5]), float.Parse(data[6]), float.Parse(data[7]))
  95.                 };
  96.         }
  97.  
  98.         override public string ToString()
  99.         {
  100.             string ldrLine = "2 ";
  101.  
  102.             ldrLine += Color.ToString() + " ";
  103.  
  104.             ldrLine += vertices[0].X.ToString() + " " + vertices[0].Y.ToString() + " " + vertices[0].Z.ToString() + " ";
  105.             ldrLine += vertices[1].X.ToString() + " " + vertices[1].Y.ToString() + " " + vertices[1].Z.ToString() + " ";
  106.  
  107.             return ldrLine;
  108.         }
  109.  
  110.         void Transform(Matrix4x4 transform)
  111.         {
  112.             vertices[0] = Vector3.Transform(vertices[0], transform);
  113.             vertices[1] = Vector3.Transform(vertices[1], transform);
  114.         }
  115.  
  116.         public void FitParent(Line1 parent)
  117.         {
  118.             if (Color == 16) Color = parent.Color;
  119.             Transform(parent.transformation);
  120.         }
  121.     }
  122.  
  123.     class Line3
  124.     {
  125.         private Vector3[] vertices;
  126.         public int Color;
  127.  
  128.         public Line3(string line)
  129.         {
  130.             string[] data = line.Split().Where(x => !string.IsNullOrEmpty(x)).ToArray();
  131.  
  132.             Color = int.Parse(data[1]);
  133.             vertices = new Vector3[]{
  134.                 new Vector3(float.Parse(data[2]), float.Parse(data[3]), float.Parse(data[4])),
  135.                 new Vector3(float.Parse(data[5]), float.Parse(data[6]), float.Parse(data[7])),
  136.                 new Vector3(float.Parse(data[8]), float.Parse(data[9]), float.Parse(data[10]))
  137.                 };
  138.         }
  139.  
  140.         override public string ToString()
  141.         {
  142.             string ldrLine = "3 ";
  143.  
  144.             ldrLine += Color.ToString() + " ";
  145.  
  146.             ldrLine += vertices[0].X.ToString() + " " + vertices[0].Y.ToString() + " " + vertices[0].Z.ToString() + " ";
  147.             ldrLine += vertices[1].X.ToString() + " " + vertices[1].Y.ToString() + " " + vertices[1].Z.ToString() + " ";
  148.             ldrLine += vertices[2].X.ToString() + " " + vertices[2].Y.ToString() + " " + vertices[2].Z.ToString() + " ";
  149.  
  150.             return ldrLine;
  151.         }
  152.  
  153.         void Transform(Matrix4x4 transform, bool isInverted)
  154.         {
  155.             if ((transform.GetDeterminant() < 0) ^ isInverted)
  156.                 vertices = new Vector3[]
  157.                 {
  158.                     vertices[2],
  159.                     vertices[1],
  160.                     vertices[0]
  161.                 };
  162.             vertices[0] = Vector3.Transform(vertices[0], transform);
  163.             vertices[1] = Vector3.Transform(vertices[1], transform);
  164.             vertices[2] = Vector3.Transform(vertices[2], transform);
  165.         }
  166.  
  167.         public void FitParent(Line1 parent)
  168.         {
  169.             if (Color == 16) Color = parent.Color;
  170.             Transform(parent.transformation, parent.IsInverted);
  171.         }
  172.     }
  173.     class Line4
  174.     {
  175.         private Vector3[] vertices;
  176.         public int Color;
  177.  
  178.         public Line4(string line)
  179.         {
  180.             string[] data = line.Split().Where(x => !string.IsNullOrEmpty(x)).ToArray();
  181.  
  182.             Color = int.Parse(data[1]);
  183.             vertices = new Vector3[]{
  184.                 new Vector3(float.Parse(data[2]), float.Parse(data[3]), float.Parse(data[4])),
  185.                 new Vector3(float.Parse(data[5]), float.Parse(data[6]), float.Parse(data[7])),
  186.                 new Vector3(float.Parse(data[8]), float.Parse(data[9]), float.Parse(data[10])),
  187.                 new Vector3(float.Parse(data[11]), float.Parse(data[12]), float.Parse(data[13]))
  188.                 };
  189.         }
  190.         override public string ToString()
  191.         {
  192.             string ldrLine = "4 ";
  193.  
  194.             ldrLine += Color.ToString() + " ";
  195.  
  196.             ldrLine += vertices[0].X.ToString() + " " + vertices[0].Y.ToString() + " " + vertices[0].Z.ToString() + " ";
  197.             ldrLine += vertices[1].X.ToString() + " " + vertices[1].Y.ToString() + " " + vertices[1].Z.ToString() + " ";
  198.             ldrLine += vertices[2].X.ToString() + " " + vertices[2].Y.ToString() + " " + vertices[2].Z.ToString() + " ";
  199.             ldrLine += vertices[3].X.ToString() + " " + vertices[3].Y.ToString() + " " + vertices[3].Z.ToString() + " ";
  200.  
  201.             return ldrLine;
  202.         }
  203.  
  204.         void Transform(Matrix4x4 transform, bool isInverted)
  205.         {
  206.             if ((transform.GetDeterminant() < 0) ^ isInverted)
  207.                 vertices = new Vector3[]
  208.                 {
  209.                     vertices[3],
  210.                     vertices[2],
  211.                     vertices[1],
  212.                     vertices[0]
  213.                 };
  214.  
  215.             vertices[0] = Vector3.Transform(vertices[0], transform);
  216.             vertices[1] = Vector3.Transform(vertices[1], transform);
  217.             vertices[2] = Vector3.Transform(vertices[2], transform);
  218.             vertices[3] = Vector3.Transform(vertices[3], transform);
  219.         }
  220.  
  221.         public void FitParent(Line1 parent)
  222.         {
  223.             if (Color == 16) Color = parent.Color;
  224.             Transform(parent.transformation, parent.IsInverted);
  225.         }
  226.     }
  227.     class Line5
  228.     {
  229.         private Vector3[] vertices;
  230.         public int Color;
  231.  
  232.         public Line5(string line)
  233.         {
  234.             string[] data = line.Split().Where(x => !string.IsNullOrEmpty(x)).ToArray();
  235.  
  236.             Color = int.Parse(data[1]);
  237.             vertices = new Vector3[]{
  238.                 new Vector3(float.Parse(data[2]), float.Parse(data[3]), float.Parse(data[4])),
  239.                 new Vector3(float.Parse(data[5]), float.Parse(data[6]), float.Parse(data[7])),
  240.                 new Vector3(float.Parse(data[8]), float.Parse(data[9]), float.Parse(data[10])),
  241.                 new Vector3(float.Parse(data[11]), float.Parse(data[12]), float.Parse(data[13]))
  242.                 };
  243.         }
  244.         override public string ToString()
  245.         {
  246.             string ldrLine = "5 ";
  247.  
  248.             ldrLine += Color.ToString() + " ";
  249.  
  250.             ldrLine += vertices[0].X.ToString() + " " + vertices[0].Y.ToString() + " " + vertices[0].Z.ToString() + " ";
  251.             ldrLine += vertices[1].X.ToString() + " " + vertices[1].Y.ToString() + " " + vertices[1].Z.ToString() + " ";
  252.             ldrLine += vertices[2].X.ToString() + " " + vertices[2].Y.ToString() + " " + vertices[2].Z.ToString() + " ";
  253.             ldrLine += vertices[3].X.ToString() + " " + vertices[3].Y.ToString() + " " + vertices[3].Z.ToString() + " ";
  254.  
  255.             return ldrLine;
  256.         }
  257.  
  258.         void Transform(Matrix4x4 transform)
  259.         {
  260.             vertices[0] = Vector3.Transform(vertices[0], transform);
  261.             vertices[1] = Vector3.Transform(vertices[1], transform);
  262.             vertices[2] = Vector3.Transform(vertices[2], transform);
  263.             vertices[3] = Vector3.Transform(vertices[3], transform);
  264.         }
  265.  
  266.         public void FitParent(Line1 parent)
  267.         {
  268.             if (Color == 16) Color = parent.Color;
  269.             Transform(parent.transformation);
  270.         }
  271.     }
  272.  
  273.     class LDRLine
  274.     {
  275.         public Int16 line { get; }
  276.         public object LineData;
  277.  
  278.         public LDRLine(Line0 line)
  279.         {
  280.             this.line = 0;
  281.             LineData = line;
  282.         }
  283.         public LDRLine(Line1 line)
  284.         {
  285.             this.line = 1;
  286.             LineData = line;
  287.         }
  288.         public LDRLine(Line2 line)
  289.         {
  290.             this.line = 2;
  291.             LineData = line;
  292.         }
  293.         public LDRLine(Line3 line)
  294.         {
  295.             this.line = 3;
  296.             LineData = line;
  297.         }
  298.         public LDRLine(Line4 line)
  299.         {
  300.             this.line = 4;
  301.             LineData = line;
  302.         }
  303.         public LDRLine(Line5 line)
  304.         {
  305.             this.line = 5;
  306.             LineData = line;
  307.         }
  308.         override public string ToString()
  309.         {
  310.             return LineData.ToString();
  311.         }
  312.         public void FitParent(Line1 parent)
  313.         {
  314.             if (line == 1) ((Line1)LineData).FitParent(parent);
  315.             if (line == 2) ((Line2)LineData).FitParent(parent);
  316.             if (line == 3) ((Line3)LineData).FitParent(parent);
  317.             if (line == 4) ((Line4)LineData).FitParent(parent);
  318.             if (line == 5) ((Line5)LineData).FitParent(parent);
  319.         }
  320.     }
  321.  
  322.     class LDR
  323.     {
  324.         static string ldrDir = @"C:\Users\Public\Documents\LDraw\";
  325.  
  326.         static string ReplaceLine1(string line1, bool invertnext)
  327.         {
  328.             Console.WriteLine($"start of subfile. inverted? {invertnext}");
  329.             string parcedline1 = "";
  330.  
  331.             string[] sParams = line1.Split(' ');
  332.             string color = sParams[1];
  333.             Matrix4x4 transform = new(float.Parse(sParams[5]), float.Parse(sParams[6]), float.Parse(sParams[7]), float.Parse(sParams[2]),
  334.                 float.Parse(sParams[8]), float.Parse(sParams[9]), float.Parse(sParams[10]), float.Parse(sParams[3]),
  335.                 float.Parse(sParams[11]), float.Parse(sParams[12]), float.Parse(sParams[13]), float.Parse(sParams[4]),
  336.                 0, 0, 0, 1);
  337.             Matrix4x4 tmp;
  338.             string subfile = sParams[14].Trim('\r');
  339.             string file;
  340.  
  341.             if (File.Exists(subfile))
  342.             {
  343.                 file = subfile;
  344.             }
  345.             else if (File.Exists(ldrDir + @"p\\" + subfile))
  346.             {
  347.                 file = ldrDir + "p\\" + subfile;
  348.             }
  349.             else if (File.Exists(ldrDir + "parts\\" + subfile))
  350.             {
  351.                 file = ldrDir + "parts\\" + subfile;
  352.             }
  353.             else if (File.Exists(ldrDir + "Unofficial\\P\\" + subfile))
  354.             {
  355.                 file = ldrDir + "Unofficial\\P\\" + subfile;
  356.             }
  357.             else if (File.Exists(ldrDir + "Unofficial\\Parts\\" + subfile))
  358.             {
  359.                 file = ldrDir + "Unofficial\\Parts\\" + subfile;
  360.             }
  361.             else
  362.             {
  363.                 return "";
  364.             }
  365.  
  366.             string subfileData = "";
  367.  
  368.             using (FileStream fs = File.Open(file, FileMode.Open))
  369.             {
  370.                 byte[] b = new byte[fs.Length];
  371.                 UTF8Encoding temp = new(true);
  372.  
  373.                 while (fs.Read(b, 0, b.Length) > 0)
  374.                 {
  375.                     subfileData += temp.GetString(b);
  376.                 }
  377.             }
  378.  
  379.             string[] lineInfo;
  380.             int timesinvert = 0;
  381.             foreach (string line in subfileData.Split('\n'))
  382.             {
  383.                 string aline = line.Trim('\r');
  384.                 if (aline.Length > 0)
  385.                 {
  386.                     switch (aline[0])
  387.                     {
  388.                         case '0':
  389.                             if (aline.Trim('\r') == "0 BFC INVERTNEXT")
  390.                             {
  391.                                 timesinvert++;
  392.                                 Console.WriteLine(aline);
  393.                                 Console.WriteLine("a");
  394.                             }
  395.                             break;
  396.                         case '1':
  397.                             lineInfo = aline.Split(' ');
  398.                             if (aline.Split(' ')[1] == "16")
  399.                             {
  400.                                 lineInfo[1] = color;
  401.                             }
  402.                             else Console.WriteLine("b");
  403.                             tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
  404.                                 float.Parse(lineInfo[5]), float.Parse(lineInfo[6]), float.Parse(lineInfo[7]), float.Parse(lineInfo[2]),
  405.                                 float.Parse(lineInfo[8]), float.Parse(lineInfo[9]), float.Parse(lineInfo[10]), float.Parse(lineInfo[3]),
  406.                                 float.Parse(lineInfo[11]), float.Parse(lineInfo[12]), float.Parse(lineInfo[13]), float.Parse(lineInfo[4]),
  407.                                 0, 0, 0, 1));
  408.                             Console.WriteLine(aline);
  409.                             if (tmp.GetDeterminant() < 0)
  410.                             {
  411.                                 timesinvert++;
  412.                                 Console.WriteLine("a");
  413.                             }
  414.                             else Console.WriteLine("b");
  415.  
  416.                             Console.WriteLine(timesinvert % 2 == 1);
  417.  
  418.                             if (timesinvert % 2 == 1) { invertnext = !invertnext; }
  419.  
  420.                             lineInfo[2] = tmp.M14.ToString();
  421.                             lineInfo[3] = tmp.M24.ToString();
  422.                             lineInfo[4] = tmp.M34.ToString();
  423.                             lineInfo[5] = tmp.M11.ToString();
  424.                             lineInfo[6] = tmp.M12.ToString();
  425.                             lineInfo[7] = tmp.M13.ToString();
  426.                             lineInfo[8] = tmp.M21.ToString();
  427.                             lineInfo[9] = tmp.M22.ToString();
  428.                             lineInfo[10] = tmp.M23.ToString();
  429.                             lineInfo[11] = tmp.M31.ToString();
  430.                             lineInfo[12] = tmp.M32.ToString();
  431.                             lineInfo[13] = tmp.M33.ToString();
  432.                             //parcedline1 += String.Join(' ', lineInfo) + "\n";
  433.                             Console.WriteLine($"entering subfile. inverted? {invertnext}");
  434.                             parcedline1 += ReplaceLine1(String.Join(' ', lineInfo), invertnext);
  435.                             Console.WriteLine($"exiting subfile. inverted? {invertnext}");
  436.                             if (timesinvert % 2 == 1) invertnext = !invertnext;
  437.                             timesinvert = 0;
  438.                             break;
  439.                         case '2':
  440.                             lineInfo = aline.Split(' ');
  441.                             if (aline.Split(' ')[1] == "16")
  442.                             {
  443.                                 lineInfo[1] = color;
  444.                             }
  445.                             tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
  446.                                 0, 0, 0, float.Parse(lineInfo[2]),
  447.                                 0, 0, 0, float.Parse(lineInfo[3]),
  448.                                 0, 0, 0, float.Parse(lineInfo[4]),
  449.                                 0, 0, 0, 1));
  450.                             lineInfo[2] = tmp.M14.ToString();
  451.                             lineInfo[3] = tmp.M24.ToString();
  452.                             lineInfo[4] = tmp.M34.ToString();
  453.                             tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
  454.                                 0, 0, 0, float.Parse(lineInfo[5]),
  455.                                 0, 0, 0, float.Parse(lineInfo[6]),
  456.                                 0, 0, 0, float.Parse(lineInfo[7]),
  457.                                 0, 0, 0, 1));
  458.                             lineInfo[5] = tmp.M14.ToString();
  459.                             lineInfo[6] = tmp.M24.ToString();
  460.                             lineInfo[7] = tmp.M34.ToString();
  461.                             parcedline1 += String.Join(' ', lineInfo) + "\n";
  462.                             break;
  463.                         case '3':
  464.                             lineInfo = aline.Split(' ');
  465.                             if (aline.Split(' ')[1] == "16")
  466.                             {
  467.                                 lineInfo[1] = color;
  468.                             }
  469.                             if (invertnext)
  470.                             {
  471.                                 string[] tmpLineInfo;
  472.                                 tmpLineInfo = new string[lineInfo.Length];
  473.                                 lineInfo.CopyTo(tmpLineInfo, 0);
  474.                                 tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
  475.                                     0, 0, 0, float.Parse(tmpLineInfo[2]),
  476.                                     0, 0, 0, float.Parse(tmpLineInfo[3]),
  477.                                     0, 0, 0, float.Parse(tmpLineInfo[4]),
  478.                                     0, 0, 0, 1));
  479.                                 lineInfo[8] = tmp.M14.ToString();
  480.                                 lineInfo[9] = tmp.M24.ToString();
  481.                                 lineInfo[10] = tmp.M34.ToString();
  482.                                 tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
  483.                                     0, 0, 0, float.Parse(tmpLineInfo[5]),
  484.                                     0, 0, 0, float.Parse(tmpLineInfo[6]),
  485.                                     0, 0, 0, float.Parse(tmpLineInfo[7]),
  486.                                     0, 0, 0, 1));
  487.                                 lineInfo[5] = tmp.M14.ToString();
  488.                                 lineInfo[6] = tmp.M24.ToString();
  489.                                 lineInfo[7] = tmp.M34.ToString();
  490.                                 tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
  491.                                     0, 0, 0, float.Parse(tmpLineInfo[8]),
  492.                                     0, 0, 0, float.Parse(tmpLineInfo[9]),
  493.                                     0, 0, 0, float.Parse(tmpLineInfo[10]),
  494.                                     0, 0, 0, 1));
  495.                                 lineInfo[2] = tmp.M14.ToString();
  496.                                 lineInfo[3] = tmp.M24.ToString();
  497.                                 lineInfo[4] = tmp.M34.ToString();
  498.                             }
  499.                             else
  500.                             {
  501.                                 tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
  502.                                     0, 0, 0, float.Parse(lineInfo[2]),
  503.                                     0, 0, 0, float.Parse(lineInfo[3]),
  504.                                     0, 0, 0, float.Parse(lineInfo[4]),
  505.                                     0, 0, 0, 1));
  506.                                 lineInfo[2] = tmp.M14.ToString();
  507.                                 lineInfo[3] = tmp.M24.ToString();
  508.                                 lineInfo[4] = tmp.M34.ToString();
  509.                                 tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
  510.                                     0, 0, 0, float.Parse(lineInfo[5]),
  511.                                     0, 0, 0, float.Parse(lineInfo[6]),
  512.                                     0, 0, 0, float.Parse(lineInfo[7]),
  513.                                     0, 0, 0, 1));
  514.                                 lineInfo[5] = tmp.M14.ToString();
  515.                                 lineInfo[6] = tmp.M24.ToString();
  516.                                 lineInfo[7] = tmp.M34.ToString();
  517.                                 tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
  518.                                     0, 0, 0, float.Parse(lineInfo[8]),
  519.                                     0, 0, 0, float.Parse(lineInfo[9]),
  520.                                     0, 0, 0, float.Parse(lineInfo[10]),
  521.                                     0, 0, 0, 1));
  522.                                 lineInfo[8] = tmp.M14.ToString();
  523.                                 lineInfo[9] = tmp.M24.ToString();
  524.                                 lineInfo[10] = tmp.M34.ToString();
  525.                             }
  526.                             parcedline1 += String.Join(' ', lineInfo) + "\n";
  527.                             break;
  528.                         case '4':
  529.                             lineInfo = aline.Split(' ');
  530.                             if (aline.Split(' ')[1] == "16")
  531.                             {
  532.                                 lineInfo[1] = color;
  533.                             }
  534.                             if (invertnext)
  535.                             {
  536.                                 string[] tmpLineInfo;
  537.                                 tmpLineInfo = new string[lineInfo.Length];
  538.                                 lineInfo.CopyTo(tmpLineInfo, 0);
  539.                                 tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
  540.                                     0, 0, 0, float.Parse(tmpLineInfo[2]),
  541.                                     0, 0, 0, float.Parse(tmpLineInfo[3]),
  542.                                     0, 0, 0, float.Parse(tmpLineInfo[4]),
  543.                                     0, 0, 0, 1));
  544.                                 lineInfo[11] = tmp.M14.ToString();
  545.                                 lineInfo[12] = tmp.M24.ToString();
  546.                                 lineInfo[13] = tmp.M34.ToString();
  547.                                 tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
  548.                                     0, 0, 0, float.Parse(tmpLineInfo[5]),
  549.                                     0, 0, 0, float.Parse(tmpLineInfo[6]),
  550.                                     0, 0, 0, float.Parse(tmpLineInfo[7]),
  551.                                     0, 0, 0, 1));
  552.                                 lineInfo[8] = tmp.M14.ToString();
  553.                                 lineInfo[9] = tmp.M24.ToString();
  554.                                 lineInfo[10] = tmp.M34.ToString();
  555.                                 tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
  556.                                     0, 0, 0, float.Parse(tmpLineInfo[8]),
  557.                                     0, 0, 0, float.Parse(tmpLineInfo[9]),
  558.                                     0, 0, 0, float.Parse(tmpLineInfo[10]),
  559.                                     0, 0, 0, 1));
  560.                                 lineInfo[5] = tmp.M14.ToString();
  561.                                 lineInfo[6] = tmp.M24.ToString();
  562.                                 lineInfo[7] = tmp.M34.ToString();
  563.                                 tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
  564.                                     0, 0, 0, float.Parse(tmpLineInfo[11]),
  565.                                     0, 0, 0, float.Parse(tmpLineInfo[12]),
  566.                                     0, 0, 0, float.Parse(tmpLineInfo[13]),
  567.                                     0, 0, 0, 1));
  568.                                 lineInfo[2] = tmp.M14.ToString();
  569.                                 lineInfo[3] = tmp.M24.ToString();
  570.                                 lineInfo[4] = tmp.M34.ToString();
  571.                             }
  572.                             else
  573.                             {
  574.                                 tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
  575.                                     0, 0, 0, float.Parse(lineInfo[2]),
  576.                                     0, 0, 0, float.Parse(lineInfo[3]),
  577.                                     0, 0, 0, float.Parse(lineInfo[4]),
  578.                                     0, 0, 0, 1));
  579.                                 lineInfo[2] = tmp.M14.ToString();
  580.                                 lineInfo[3] = tmp.M24.ToString();
  581.                                 lineInfo[4] = tmp.M34.ToString();
  582.                                 tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
  583.                                     0, 0, 0, float.Parse(lineInfo[5]),
  584.                                     0, 0, 0, float.Parse(lineInfo[6]),
  585.                                     0, 0, 0, float.Parse(lineInfo[7]),
  586.                                     0, 0, 0, 1));
  587.                                 lineInfo[5] = tmp.M14.ToString();
  588.                                 lineInfo[6] = tmp.M24.ToString();
  589.                                 lineInfo[7] = tmp.M34.ToString();
  590.                                 tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
  591.                                     0, 0, 0, float.Parse(lineInfo[8]),
  592.                                     0, 0, 0, float.Parse(lineInfo[9]),
  593.                                     0, 0, 0, float.Parse(lineInfo[10]),
  594.                                     0, 0, 0, 1));
  595.                                 lineInfo[8] = tmp.M14.ToString();
  596.                                 lineInfo[9] = tmp.M24.ToString();
  597.                                 lineInfo[10] = tmp.M34.ToString();
  598.                                 tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
  599.                                     0, 0, 0, float.Parse(lineInfo[11]),
  600.                                     0, 0, 0, float.Parse(lineInfo[12]),
  601.                                     0, 0, 0, float.Parse(lineInfo[13]),
  602.                                     0, 0, 0, 1));
  603.                                 lineInfo[11] = tmp.M14.ToString();
  604.                                 lineInfo[12] = tmp.M24.ToString();
  605.                                 lineInfo[13] = tmp.M34.ToString();
  606.                             }
  607.                             parcedline1 += String.Join(' ', lineInfo) + "\n";
  608.                             break;
  609.                         case '5':
  610.                             lineInfo = aline.Split(' ');
  611.                             if (aline.Split(' ')[1] == "16")
  612.                             {
  613.                                 lineInfo[1] = color;
  614.                             }
  615.                             tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
  616.                                 0, 0, 0, float.Parse(lineInfo[2]),
  617.                                 0, 0, 0, float.Parse(lineInfo[3]),
  618.                                 0, 0, 0, float.Parse(lineInfo[4]),
  619.                                 0, 0, 0, 1));
  620.                             lineInfo[2] = tmp.M14.ToString();
  621.                             lineInfo[3] = tmp.M24.ToString();
  622.                             lineInfo[4] = tmp.M34.ToString();
  623.                             tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
  624.                                 0, 0, 0, float.Parse(lineInfo[5]),
  625.                                 0, 0, 0, float.Parse(lineInfo[6]),
  626.                                 0, 0, 0, float.Parse(lineInfo[7]),
  627.                                 0, 0, 0, 1));
  628.                             lineInfo[5] = tmp.M14.ToString();
  629.                             lineInfo[6] = tmp.M24.ToString();
  630.                             lineInfo[7] = tmp.M34.ToString();
  631.                             tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
  632.                                 0, 0, 0, float.Parse(lineInfo[8]),
  633.                                 0, 0, 0, float.Parse(lineInfo[9]),
  634.                                 0, 0, 0, float.Parse(lineInfo[10]),
  635.                                 0, 0, 0, 1));
  636.                             lineInfo[8] = tmp.M14.ToString();
  637.                             lineInfo[9] = tmp.M24.ToString();
  638.                             lineInfo[10] = tmp.M34.ToString();
  639.                             tmp = Matrix4x4.Multiply(transform, new Matrix4x4(
  640.                                 0, 0, 0, float.Parse(lineInfo[11]),
  641.                                 0, 0, 0, float.Parse(lineInfo[12]),
  642.                                 0, 0, 0, float.Parse(lineInfo[13]),
  643.                                 0, 0, 0, 1));
  644.                             lineInfo[11] = tmp.M14.ToString();
  645.                             lineInfo[12] = tmp.M24.ToString();
  646.                             lineInfo[13] = tmp.M34.ToString();
  647.                             parcedline1 += String.Join(' ', lineInfo) + "\n";
  648.                             break;
  649.                     }
  650.                 }
  651.             }
  652.  
  653.             return parcedline1;
  654.         }
  655.  
  656.         static string ReadFile(string file)
  657.         {
  658.             string path;
  659.             string fileText = "";
  660.  
  661.             if (File.Exists(file))
  662.             {
  663.                 path = file;
  664.             }
  665.             else if (File.Exists(ldrDir + "Unofficial\\P\\" + file))
  666.             {
  667.                 path = ldrDir + "Unofficial\\P\\" + file;
  668.             }
  669.             else if (File.Exists(ldrDir + "Unofficial\\Parts\\" + file))
  670.             {
  671.                 path = ldrDir + "Unofficial\\Parts\\" + file;
  672.             }
  673.             else if (File.Exists(ldrDir + @"p\\" + file))
  674.             {
  675.                 path = ldrDir + "p\\" + file;
  676.             }
  677.             else if (File.Exists(ldrDir + "parts\\" + file))
  678.             {
  679.                 path = ldrDir + "parts\\" + file;
  680.             }
  681.             else
  682.             {
  683.                 return "";
  684.             }
  685.  
  686.             using (FileStream fs = File.Open(path, FileMode.Open))
  687.             {
  688.                 byte[] b = new byte[fs.Length];
  689.                 UTF8Encoding temp = new(true);
  690.  
  691.                 while (fs.Read(b, 0, b.Length) > 0)
  692.                 {
  693.                     fileText += temp.GetString(b);
  694.                 }
  695.             }
  696.  
  697.             return fileText;
  698.         }
  699.  
  700.         static List<LDRLine> ParseLine1(Line1 line)
  701.         {
  702.             List<LDRLine> file = new();
  703.             string fileText = ReadFile(line.File);
  704.  
  705.             fileText = fileText.Replace("\r", "");
  706.  
  707.             string[] fileData = fileText.Split("\n").Where(x => !string.IsNullOrEmpty(x)).ToArray();
  708.  
  709.             file = file.Where(x => x.line != 0).ToList();
  710.  
  711.             foreach (string lineData in fileData)
  712.             {
  713.                 switch (lineData[0])
  714.                 {
  715.                     case '0':
  716.                         file.Add(new LDRLine(new Line0(lineData)));
  717.                         break;
  718.                     case '1':
  719.                         /*Line1 line1ToParce = new(lineData);
  720.                         line1ToParce.FitParent(line);
  721.                         List<LDRLine> parcedLine = ParseLine1(line1ToParce);
  722.                         foreach (LDRLine ln in parcedLine)
  723.                         {
  724.                             file.Add(ln);
  725.                         }*/
  726.                         Line1 line1ToParce = new(lineData);
  727.                         try
  728.                         {
  729.                             if (((Line0)file[file.Count - 1].LineData).Type == 0)
  730.                             {
  731.                                 line1ToParce.IsInverted = !line1ToParce.IsInverted;
  732.                             }
  733.                         }
  734.                         catch { }
  735.                         line1ToParce.FitParent(line);
  736.                         List<LDRLine> parcedLine = ParseLine1(line1ToParce);
  737.                         foreach (LDRLine ln in parcedLine)
  738.                         {
  739.                             file.Add(ln);
  740.                         }
  741.                         break;
  742.                     case '2':
  743.                         Line2 line2ToParce = new(lineData);
  744.                         line2ToParce.FitParent(line);
  745.                         file.Add(new LDRLine(
  746.                             line2ToParce
  747.                             ));
  748.                         break;
  749.                     case '3':
  750.                         Line3 line3ToParce = new(lineData);
  751.                         line3ToParce.FitParent(line);
  752.                         file.Add(new LDRLine(
  753.                             line3ToParce
  754.                             ));
  755.                         break;
  756.                     case '4':
  757.                         Line4 line4ToParce = new(lineData);
  758.                         line4ToParce.FitParent(line);
  759.                         file.Add(new LDRLine(
  760.                             line4ToParce
  761.                             ));
  762.                         break;
  763.                     case '5':
  764.                         Line5 line5ToParce = new(lineData);
  765.                         line5ToParce.FitParent(line);
  766.                         file.Add(new LDRLine(
  767.                             line5ToParce
  768.                             ));
  769.                         break;
  770.                 }
  771.             }
  772.  
  773.             return file;
  774.         }
  775.  
  776.         static void Main(string[] args)
  777.         {
  778.             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";
  779.  
  780.             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();
  781.  
  782.             foreach (LDRLine line in data)
  783.             {
  784.                 ldr += line.ToString() + "\n";
  785.             }
  786.  
  787.             using (FileStream fstream = new FileStream("a.ldr", FileMode.OpenOrCreate))
  788.             {
  789.                 byte[] buffer = Encoding.Default.GetBytes(ldr);
  790.                 fstream.Write(buffer, 0, buffer.Length);
  791.                 Console.WriteLine("Текст записан в файл");
  792.             }
  793.         }
  794.     }
  795. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement