Visual Components' Community

New Thinking for Factory Simulation
Welcome to Visual Components' Community Sign in | Help
in Search

C# Example: Read STL files using Fast PolygonSet interface

Last post pe, syys 4 2009 10:35 by RenfoJu1. 0 replies.
Page 1 of 1 (1 items)
Sort Posts: Previous Next
  • pe, syys 4 2009 10:35

    • RenfoJu1
    • Top 10 Contributor
    • Joined on pe, helmi 22 2008
    • Posts 9

    C# Example: Read STL files using Fast PolygonSet interface

    In order to make the following code snippet to work, create a new console application project and add COM references vc3DCreate, vcCOM and vcCOMgeo to it:

     

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Text;
    using System.IO;
    using vcCOM;
    using vc3DCreate;
    using vcCOMgeo;
     
     
    namespace StlLoaderSample
    {
        class Program
        {
            static void Main(string[ ] args)
            {
                if (args.Length == 0)
                {
                    System.Console.WriteLine("Usage: Load <filenames>");
                }
                else
                {
                    // a separator is usually space, but may also be a comma
                    string[ ] separators = new string[ ] { " ", "," };
     
                    // the decimal separator is always dot
                    System.Globalization.NumberFormatInfo ni = new System.Globalization.NumberFormatInfo();
                    ni.CurrencyDecimalSeparator = ".";
     
                    // connect to 3DCreate
                    IvcApplication vcApp = (IvcApplication)new vcc3DCreate();
     
                    // accepts multiple files (not very useful though)
                    foreach (string s in args)
                    {
                        // create a component by the name of the file
                        string filename = Path.GetFileName(s);
                        IvcComponent comp = vcApp.createComponent(Path.ChangeExtension(filename,""));
     
                        // in the root node, create one geometry feature
                        IvcGeometryFeature gf = (IvcGeometryFeature)comp.RootNode.RootFeature.createFeature("Geometry");
     
                        // fastpolygonset interface is the fast way...
                        IvcFastPolygonSet ps = null;
     
                        // point data and dynamic arrays for points and polygons
                        double[ ] point;
                        List<double[ ]> pointlist = new List<double[ ]>();
                        List<int> polygonlist = newList<int>();
     
                        int ptcounter = 0;
                        try
                        {
                            // parse the input file
                            using (StreamReader sr = new StreamReader(s))
                            {
                                String line;
                                while ((line = sr.ReadLine()) != null)
                                {
                                    line = line.Trim();
                                    line = line.ToLower();
                                    if (line == "solid")
                                    {
                                        // create one polygonset per each solid
                                        ps = (IvcFastPolygonSet)gf.Geometry.createPolygonSet();
                                    }
                                    else if (line.StartsWith("facet"))
                                    {
                                        // number of vertices per polygon is always 3
                                        polygonlist.Add(3);
                                    }
                                    else if (line.StartsWith("vertex"))
                                   {                                  
                                        // parse: vertex x y z
                                        string[ ] words = line.Split(separators, StringSplitOptions.RemoveEmptyEntries);
                                        point = new double[3];
                                        for (int i = 1; i < 4; i++)
                                            point[i - 1] = double.Parse(words[i], ni);
                                        // add point to the points array
                                        pointlist.Add(point);
                                        // add index of the point to the polygon array
                                        polygonlist.Add(ptcounter++);
                                    }
                                    else if (line =="endsolid")
                                    {
                                        // done with one polygonset : build all polygons with 2 calls over com
                                        // first convert dynamic points array to fixed size double[3,n]
                                        double[,] dpts = new double[3, pointlist.Count];
                                        int i = 0;
                                        foreach (double[ ] pt inpointlist)
                                        {
                                            for (int j = 0; j< 3; j++)
                                                dpts[j, i] = ((double[ ])(pointlist[i]))[j];
                                            i++;
                                        }
                                        // push through all points
                                        ps.addPoints(dpts, false);
                                        // push through polygons
                                       ps.addPolygons(polygonlist.ToArray(), false);
                                        // need to tell polygonset we are done:
                                        ps.rebuild();
                                    }
                                }
                                // component needs rebuilding
                                comp.rebuild();
                                // and the scene needs rendering
                                vcApp.render();
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("The file could not be read:");
                            Console.WriteLine(e.Message);
                        }
                    }
                }
            }
        }
    }
Page 1 of 1 (1 items)