Link to home
Start Free TrialLog in
Avatar of ayha1999
ayha1999

asked on

C# code

Hi,

Does anyone have equivalent .net code for the attached python code? Please help me to convert to c#.

Thanks
mesure-volume.py
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

I have no way of testing if this code is right but as best as I can tell it is a port of the .py code.

There was quite a bit in the python script that is not used so I have not included it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

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

        LengthType _lengthType;
        string _filename;
        BinaryReader br;

        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 uint read_32bitInt()
        {
            byte[] b32 = new byte[4];

            br.Read(b32, 0, 4);
            if (BitConverter.IsLittleEndian)
            {
                b32.Reverse();
            }
            return (uint)BitConverter.ToInt32(b32, 0);

        }

        public uint[] get_vector(int size)
        {
            uint[] v = new uint[size];

            for (int i = 0; i < size; i++)
            {
                v[i] = read_32bitInt();
            }

            return v;
        }

        public double read_triangle()
        {
        
            byte[] bytecount = new byte[2];

            uint[] n = get_vector(3);
            uint[] p1 = get_vector(3);
            uint[] p2 = get_vector(3);
            uint[] p3 = get_vector(3);
            
            br.Read(bytecount, 0, 2);

            return signedVolumeOfTriangle(p1, p2, p3);
        }

        public double CalculateVolume2(string unit)
        {
            br = new BinaryReader(File.Open(_filename, FileMode.Open));
            byte[] buffer = new byte[128];
            double dTotal = 0;

            br.Read(buffer, 0, 80);
            uint nTriangles = read_32bitInt();
            double totalVolume = 0;
            for(int i=0;i < nTriangles; i++) {
                totalVolume += read_triangle();
            }
            totalVolume = totalVolume / 1000;
            if (unit =="inch")
            {
                totalVolume = cm3_To_inch3Transform(totalVolume);
            }
            return totalVolume;
        }


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

        public double signedVolumeOfTriangle(uint[] p1, uint[] p2, uint[] 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);
        }
    }

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            STLUtils stlUtils = new STLUtils();
            if (stlUtils.IsValidated(new string[] { @"c:\gf.stl", "cm" }))
            {
                double volume = stlUtils.CalculateVolume2("cm");
                Response.Write(volume);
            }
        }
    }
}

Open in new window

Avatar of ayha1999
ayha1999

ASKER

I tried the cellphone.stl, the result is in cm

60648440.3221672

the actual result is
Volume: 8.09431408456 cubic cm
Volume: 0.493945351361 cubic inch

Please check

thanks
I tried the cellphone.stl, the result is in cm

As I don't have this file how would you like me to check?
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It's working.

Thank you very much for help.

ayha
You are welcome - thanks for the points.
Hi julianH,

A small issue there, please have a look at the following question.

https://www.experts-exchange.com/questions/28151548/Cannot-read-stl-file.html

thanks