Solved

Conversion

Posted on 2013-01-24
10
234 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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 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 …

749 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