Solved

Conversion

Posted on 2013-01-24
10
212 Views
Last Modified: 2013-01-26
HI,

How can I convert attached python script to c#?

Please help.

Thanks
mesure-volume.py
0
Comment
Question by:ayha1999
10 Comments
 
LVL 10

Expert Comment

by:Monica P
ID: 38813739
0
 
LVL 7

Author Comment

by:ayha1999
ID: 38813745
I need python to c#
0
 
LVL 19

Expert Comment

by:Rikin Shah
ID: 38813760
You can try to compile your python app using IronPython (http://www.codeplex.com/Wiki/View.aspx?ProjectName=IronPython) to .NET, this should be possible without or just minor changes. This way you can make use of all the .NET features from python.

Then you can decompile your IronPython assembly with an .NET decompiler like Lutz Roeders Reflector (http://www.red-gate.com/products/reflector) which can decompile whole Assemblies into Visual Studio projects. This way you can convert from ANY .NET Langauge into one of the supported languages of this program.
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 400 total points
ID: 38813810
here:

public class Triangle
    {
        public double x; public double y; public double z;
    }

    public class STLUtils
    {
        public enum LengthType { inch, cm } ;

        List<byte[]> _normals = new List<byte[]>();
        List<byte[]> _points = new List<byte[]>();
        List<Triangle> _triangles = new List<Triangle>();
        List<byte> _bytecount = new List<byte>();
        byte[] _fileBytes;
        LengthType _lengthType;
        string _filename;

        public bool IsValidated(string[] args)
        {
            if (args.Length != 2) return false;
            if (!Enum.TryParse<LengthType>(args[1], true, out _lengthType))
            {
                return false;
            }

            _filename = args[0];

            return File.Exists(_filename);
        }

        public double CalculateVolume()
        {
            //read binary file
            _fileBytes = File.ReadAllBytes(_filename);

            //reverse array if little indian
            if (BitConverter.IsLittleEndian)
                Array.Reverse(_fileBytes);

            //read length
            int length = ReadLength();

            double totalVol = 0;

            //skip 80(header) + 4(length)
            var bytes = _fileBytes.Skip(84);

            //loop through all triangles data
            for (int i = 0; i < length; i++)
            {
                _normals.Add(bytes.Take(12).ToArray());

                var p1 = bytes.Skip(12).Take(12).ToArray();
                var p2 = bytes.Skip(24).Take(12).ToArray();
                var p3 = bytes.Skip(36).Take(12).ToArray();
                var b = bytes.Skip(48).Take(2);

                var l = _points.Count;
                _points.Add(p1);
                _points.Add(p2);
                _points.Add(p3);
                _triangles.Add(new Triangle { x = l, y = l + 1, z = l + 2 });

                _bytecount.Add(b.First());
                totalVol += signedVolumeOfTriangle(p1, p2, p3);
                bytes = bytes.Skip(50);
            }

            //convert to required length type (cm/inch)
            return (_lengthType == LengthType.cm) ? 
                totalVol / 1000 : 
                cm3_To_inch3Transform(totalVol / 1000);
        }

        private double cm3_To_inch3Transform(double totalVol)
        {
            return totalVol * 0.0610237441;
        }

        private int ReadLength()
        {
            //skip 80 chars (header), read next 4 bytes to get length
            int length = BitConverter.ToInt32(_fileBytes.Skip(80).Take(4).ToArray(), 0);
            return length;
        }

        public double signedVolumeOfTriangle(byte[] p1, byte[] p2, byte[] p3)
        {
            double v321 = p3[0] * p2[1] * p1[2];
            double v231 = p2[0] * p3[1] * p1[2];
            double v312 = p3[0] * p1[1] * p2[2];
            double v132 = p1[0] * p3[1] * p2[2];
            double v213 = p2[0] * p1[1] * p3[2];
            double v123 = p1[0] * p2[1] * p3[2];
            return (1.0 / 6.0) * (-v321 + v231 + v312 - v132 - v213 + v123);
        }
    }

Open in new window


to use it:
static void Main(string[] args)
        {
            STLUtils sTLUtils = new STLUtils();
            if (sTLUtils.IsValidated(args))
            {
                sTLUtils.CalculateVolume();
            }
        }

Open in new window

0
 
LVL 7

Author Comment

by:ayha1999
ID: 38822339
Hi sedgwick,

there is compilation error at

 return File.Exists(_filename);      <----- error at File


  public double CalculateVolume()
    {
        //read binary file
        _fileBytes = File.ReadAllBytes(_filename);     <----- error at File

ayha
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 42

Expert Comment

by:sedgwick
ID: 38822477
Add: using System.IO
0
 
LVL 7

Author Comment

by:ayha1999
ID: 38822526
How can I run the code in page_load or form load event by providing the STL file to calculate the volume?

I can't upload the sample file, here is the link.

http://orion.math.iastate.edu/burkardt/data/stl/bottle.stl

Thanks
0
 
LVL 7

Author Closing Comment

by:ayha1999
ID: 38822551
I will open another question for that.

Thnaks for your support.
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 38822599
No problem. Post link here to new question
0
 
LVL 7

Author Comment

by:ayha1999
ID: 38822605
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now