Solved

# Displaying Decimal

Posted on 2013-06-06
309 Views
Hi,

How can change the following code to display 6 decimal places?

``````using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace WebApplication1
{
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()
{

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

double totalVol = 0;

var bytes = _fileBytes.Skip(84);

//loop through all triangles data
for (int i = 0; i < length; i++)
{

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;
_triangles.Add(new Triangle { x = l, y = l + 1, z = l + 2 });

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)
{
}

{
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);
}
}
}
``````

thanks

ayha
0
Question by:ayha1999

LVL 51

Expert Comment

Where is it not showing 6 decimals
0

LVL 7

Author Comment

when I executed the code, the result should be 0.000128 but it displayed 0 instead. how can I display the result with decimals?

Thanks
0

LVL 42

Expert Comment

post the whole code.
what do u pass to STLUtils as parameters?
0

LVL 7

Author Comment

An stl file is passed.

``````using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;l

namespace WebApplication1
{
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()
{

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

double totalVol = 0;

var bytes = _fileBytes.Skip(84);

//loop through all triangles data
for (int i = 0; i < length; i++)
{

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;
_triangles.Add(new Triangle { x = l, y = l + 1, z = l + 2 });

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)
{
}

{
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);
}
}
}
STLUtils stlUtils = new STLUtils();
if (stlUtils.IsValidated(new string[] { @"c:\gf.stl", "cm" }))
{
{
double volume = stlUtils.CalculateVolume();
Response.Write(volume);
}
}
``````
0

LVL 74

Expert Comment

Change line 112 to:

``````Response.Write(volume.ToString("0.000000"));
``````
0

LVL 51

Expert Comment

can you post your stl file (gf.stl)
0

LVL 7

Author Comment

I cannot attache stl file but a sample is available at\

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

thanks
0

LVL 51

Expert Comment

Are you sure that is the right file?

In your code the ReadLength method is used to get the length of the file - but this is not working on this file because the method expects the file to have a header with a length in it - which is not present.
0

LVL 7

Author Comment

both bottle and gf are stl files.Anyway a working sample is here. I tested and it returns 0 volume.

the actual volume is 8.09431408456 cubic cm

STL file of the Cellphone front
0

LVL 51

Expert Comment

You posted an ASCII stl file not a binary file - your application is expecting a binary file.

Also you are reversing the data when you read it in which is messing everything up. The endianess of the data in the file is on a per item basis - reversing the whole file does not achieve the same result.

You are also passing raw bytes to the signedVolumeOfTriangle function without converting.

In short the code is not right for what you want to do.
0

LVL 7

Author Comment

Hi julianH,

this c# code is based the following python script which accepts stl file and returns volume.

``````'''
VOLUME CALCULATION STL binary MODELS
Author: Mar Canet (mar.canet@gmail.com) - september 2012
Description: useful to calculate cost in a 3D printing ABS or PLA usage
'''
import struct
import sys

class STLUtils:
def resetVariables(self):
self.normals = []
self.points = []
self.triangles = []
self.bytecount = []
self.fb = [] # debug list

# Calculate volume fo the 3D mesh using Tetrahedron volume
def signedVolumeOfTriangle(self,p1, p2, p3):
v321 = p3[0]*p2[1]*p1[2]
v231 = p2[0]*p3[1]*p1[2]
v312 = p3[0]*p1[1]*p2[2]
v132 = p1[0]*p3[1]*p2[2]
v213 = p2[0]*p1[1]*p3[2]
v123 = p1[0]*p2[1]*p3[2]
return (1.0/6.0)*(-v321 + v231 + v312 - v132 - v213 + v123)

def unpack(self, sig, l):
self.fb.append(s)
return struct.unpack(sig, s)

n  = self.unpack("<3f", 12)
p1 = self.unpack("<3f", 12)
p2 = self.unpack("<3f", 12)
p3 = self.unpack("<3f", 12)
b  = self.unpack("<h", 2)

self.normals.append(n)
l = len(self.points)
self.points.append(p1)
self.points.append(p2)
self.points.append(p3)
self.triangles.append((l, l+1, l+2))
self.bytecount.append(b[0])
return self.signedVolumeOfTriangle(p1,p2,p3)

return length[0]

self.f.seek(self.f.tell()+80)

def cm3_To_inch3Transform(self, v):
return v*0.0610237441

def calculateWeight(self,volumeIn_cm):
return volumeIn_cm*1.04

def calculateVolume(self,infilename, unit):
print infilename
self.resetVariables()
totalVolume = 0
try:
self.f = open( infilename, "rb")
print "total triangles:",l
try:
while True:
except Exception, e:
#print e
print "End calculate triangles volume"
#print len(self.normals), len(self.points), len(self.triangles), l,
if unit=="cm":
totalVolume = (totalVolume/1000)
print "Total volume:", totalVolume,"cm"
else:
totalVolume = self.cm3_To_inch3Transform(totalVolume/1000)
print "Total volume:", totalVolume,"inch"
except Exception, e:
print e

if __name__ == '__main__':
if len(sys.argv)==1:
print "Define model to calculate volume ej: python mesure_volume.py torus.stl"
else:
mySTLUtils = STLUtils()
if(len(sys.argv)>2 and sys.argv[2]=="inch"):
mySTLUtils.calculateVolume(sys.argv[1],"inch")
else:
mySTLUtils.calculateVolume(sys.argv[1],"cm")
``````

thanks
0

LVL 51

Accepted Solution

Julian Hansen earned 250 total points
Ok - but the python file makes sense - the C# code does not match what the Python file is doing.

1. You have to establish whether you are using ASCII or Binary STL files - code assumes binary but you posted an ASCII file
2. You are reversing the whole file if the system is LittleEndian based - which is going to mess up your data
3. You are passing vertices to the volume of Triangle (which is a misnomer - a triangle is typically 2d a 3d version of a triangle is a cone or a tetrahedron) in a form that they byte elements of a 32 bit number are being accesed as individual values instead of first converting the points to an Int32

That is where I stopped with the code. You posted this question as 250 points - not really an incentive to continue - I think the question has been answered if you want that code converted post another question and give it a decent point value.
0

LVL 7

Author Closing Comment

OK. I will open another question.

please have a look at the following question.

http://www.experts-exchange.com/Programming/Languages/Scripting/Python/Q_28149820.html

Thanks
0

## Featured Post

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…