Solved

Conversion

Posted on 2013-01-24
10
196 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
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…

708 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

13 Experts available now in Live!

Get 1:1 Help Now