Beispiel #1
0
        // Actual processing file section
        // reads file and open sub-part files or performs transitions as nesscary.
        // parameter root is used to tell processFile that its an orginial file and not a subpart.
        private int processFile(String file, bool inverted, bool root = false)
        {
            depth(1);
            StreamReader RF   = null;
            LdrawMatrix  work = null;
            String       line = null;

            String[] s               = null;
            String[] space           = { " " }; // Used for split line
            bool     thisOneInverted = false;   // Remember where it was flagged inverted.

            work = (LdrawMatrix)matrices.Peek();

            // If processing a MPD file, priority is always subparts in MPD first.
            if (mpdenable)
            {
                String a = Path.GetFileName(file);
                //Debug.WriteLine("checking mpd subpart. part being found out is " + a);
                RF = mpd.getMPDSubpart(a);
                //if (RF != null)
                //{
                //Debug.WriteLine("Got a subpart... with " + a);

                //a = RF.ReadLine();
                //Debug.WriteLine("#### debug print this main file");
                // debug
                //while (a != null)
                //{
                //  Debug.WriteLine(a);
                //   a = RF.ReadLine();
                //}
                //RF.BaseStream.Seek(0L,0);
                //}
            }

            // If RF is still null (or theres no MPD to use) load file
            if (RF == null)
            {
                //Check if file exists. The handle according.
                bool err = File.Exists(file);
                if (!err && !skiperror)
                {
                    depth(-1);
                    return(1); // ignore errors not enabled, return with error
                }
                else if (!err && skiperror)
                {
                    depth(-1);
                    return(0); // Return "its fine" even if file was not processed. (ignorerrors enabled)
                }
                RF = new StreamReader(file);
                // Was open a failure? if so return error.
                if (RF == null)
                {
                    depth(-1);
                    return(1); // Hard error, cannot be skipped
                }
            }

            // Was stop requested?
            if (stopConvert == ProcessStatus.FORCESTOP)
            {
                stopConvert = ProcessStatus.NOTSTARTED;
                RF.Close();
                depth(-1);
                return(100);
            }

            // Check only when mpd isnt enabled.
            if (root && !mpdenable)
            {
                // Get root folder if it was root file...
                // Used for findfile default if not found in source directories.
                fileDir = Path.GetDirectoryName(file);
                //Debug.WriteLine(fileDir);
                //Debug.WriteLine(Path.GetExtension(file));
                // Check if its MPD type
                if (Path.GetExtension(file).ToLower().CompareTo(".mpd") == 0)
                {
                    RF.Close();
                    mpd       = new MPDFileList(file);
                    RF        = mpd.getMPDSubpart(null);
                    mpdenable = true; // enable mpd mode
                    // String a = RF.ReadLine();
                    //Debug.WriteLine("#### debug print this main file");
                    // debug
                    //while (a != null)
                    //{
                    //  Debug.WriteLine(a);
                    //    a = RF.ReadLine();
                    //}
                    //RF.BaseStream.Seek(0L,0);
                }
            }

            // Read first line
            line = RF.ReadLine();
            //if (root)
            //    Debug.WriteLine(line + " // read");
            while (line != null)
            {
                if (root)
                {
                    overall = (int)(((double)RF.BaseStream.Position / (double)RF.BaseStream.Length) * 100.0);
                    OnPercentUpdate(overall);
                }

                // Split line into "words"
                s = line.Split(space, StringSplitOptions.RemoveEmptyEntries);
                if (s.Length >= 1)
                {
                    switch (s[0])
                    {
                    case "0":    // Comment, invertnext or step.
                        if (s.Length > 1 && s[1].CompareTo("STEP") == 0)
                        {
                            // step found!
                            setobjgroup(false, true);
                            //debug.WriteLine("step found.");
                        }
                        // Is it invert next? it should be in format 0 BFC INVERTNEXT.
                        else if (s.Length > 2 && s[1].CompareTo("BFC") == 0 && s[2].CompareTo("INVERTNEXT") == 0)
                        {
                            // 0 BFC INVERTNEXT found!
                            inverted        = !inverted;
                            thisOneInverted = true;
                            //debug.WriteLine("invert next found. Status now " + inverted);
                        }
                        break;

                    case "1":    // new subpart file...
                        //Debug.Write("subpart found... legit: ");
                        if (s.Length < 15)
                        {
                            //Debug.WriteLine("no!");
                            // error!
                            RF.Close();
                            return(1);
                        }
                        else
                        {
                            //Debug.WriteLine("yes");
                            // If this is in root file, start a new line group
                            if (root)
                            {
                                setobjgroup(true, false);
                            }

                            //debug.WriteLine("////////////////////////////////////////////////////////////////////////////////");
                            //debug.WriteLine("New subpart: " + s[14] + " depth: " + ++depth + " matrices size: " + matrices.ToArray().Length);
                            int x = type1(s, inverted);
                            //if(root)
                            //  Debug.WriteLine("type1 exit... X:" + x + " line:" + line);

                            //debug.WriteLine("Leaving subpart. Depth: " + --depth + " matrices size: " + matrices.ToArray().Length);
                            //debug.WriteLine("////////////////////////////////////////////////////////////////////////////////");
                            if (x != 0)
                            {
                                depth(-1);
                                return(x);
                            }
                        }
                        if (thisOneInverted)    // Was here where invert was enabled? if so it should be flipped.
                        {
                            inverted        = !inverted;
                            thisOneInverted = false;
                            //debug.WriteLine(" was inverted, undoing now. status: " + inverted);
                        }
                        break;

                    case "3":    // Triangle
                        //debug.Write("type 3... legal: ");
                        if (s.Length < 11)
                        {
                            //debug.WriteLine("no!");
                            // error!
                            RF.Close();
                            depth(-1);
                            return(1);
                        }
                        else
                        {
                            //debug.WriteLine("yes");
                            type3(s, inverted);
                        }
                        if (thisOneInverted)    // Was here where invert was enabled? if so it should be flipped.
                        {
                            inverted        = !inverted;
                            thisOneInverted = false;
                            //debug.WriteLine(" was inverted, undoing now. status: " + inverted);
                        }
                        break;

                    case "4":    // Quad
                        //debug.Write("type 4... legal: ");
                        if (s.Length < 14)
                        {
                            // debug.WriteLine("no!");
                            // error!
                            RF.Close();
                            depth(-1);
                            return(1);
                        }
                        else
                        {
                            // debug.WriteLine("yes");
                            type4(s, inverted);
                        }
                        if (thisOneInverted)    // Was here where invert was enabled? if so it should be flipped.
                        {
                            inverted        = !inverted;
                            thisOneInverted = false;
                            //debug.WriteLine(" was inverted, undoing now. status: " + inverted);
                        }
                        break;
                    }
                    //if(root)
                    //  Debug.WriteLine("Exit select case");
                }
                // Read next line
                //if(root)
                //  Debug.WriteLine(line + " after processed.");
                line = RF.ReadLine();
                //if (root)
                //  Debug.WriteLine(line + " // read");
            }


            // Clean up
            RF.Close();
            depth(-1);

            return(0);
        }