More metadata
This commit is contained in:
parent
ec18e3998d
commit
4969937e6a
|
@ -1,5 +1,6 @@
|
|||
import os.path
|
||||
import struct
|
||||
import re
|
||||
|
||||
from hashlib import md5
|
||||
from datetime import datetime
|
||||
|
@ -7,7 +8,7 @@ from datetime import datetime
|
|||
from django.contrib.auth.models import User
|
||||
from django.db import models
|
||||
|
||||
|
||||
vertex_pattern = re.compile(r'vertex\s+([0-9.e+-]+)\s+([0-9.e+-]+)\s+([0-9.e+-]+)')
|
||||
|
||||
def safe_upload_path(base_dir):
|
||||
|
||||
|
@ -33,27 +34,99 @@ class Geometry(models.Model):
|
|||
user = models.ForeignKey(User, blank=True)
|
||||
date = models.DateTimeField(auto_now_add=True)
|
||||
polycount = models.IntegerField(blank=True, default=0)
|
||||
width = models.FloatField(blank=True, default=0)
|
||||
depth = models.FloatField(blank=True, default=0)
|
||||
height = models.FloatField(blank=True, default=0)
|
||||
file = models.FileField(upload_to=safe_upload_path('models'))
|
||||
sourcefile = models.FileField(upload_to=safe_upload_path('sources'), blank=True)
|
||||
|
||||
def _generate_meta_infos(self):
|
||||
print "Generating metainfos %s" % self.name
|
||||
|
||||
self.file.open()
|
||||
|
||||
count = 0
|
||||
min_coord = [None,None,None]
|
||||
max_coord = [None,None,None]
|
||||
|
||||
if self.file.read(5) != "solid":
|
||||
print "binary"
|
||||
#Skip header
|
||||
self.file.seek(80)
|
||||
count = struct.unpack("i",self.file.read(4))[0]
|
||||
|
||||
done = False
|
||||
for pos in range(0,count):
|
||||
#Skip normal (3 * 32bit)
|
||||
self.file.seek(self.file.tell()+3*4)
|
||||
#Loop over each coordinate
|
||||
for vert in range(0,3):
|
||||
# Loop over each coordinate
|
||||
for i in range(0,3):
|
||||
x = struct.unpack("<f",self.file.read(4))[0]
|
||||
|
||||
if min_coord[i] != None :
|
||||
min_coord[i] = min(min_coord[i], x)
|
||||
else:
|
||||
min_coord[i] = x
|
||||
|
||||
if max_coord[i] != None :
|
||||
max_coord[i] = max(max_coord[i], x)
|
||||
else:
|
||||
max_coord[i] = x
|
||||
#Skip attributes (16bit)
|
||||
self.file.seek(self.file.tell()+2)
|
||||
|
||||
else:
|
||||
print "ascii"
|
||||
|
||||
line = self.file.readline()
|
||||
while line != "":
|
||||
line = line.strip()
|
||||
if line.startswith("facet"):
|
||||
count = count + 1
|
||||
if line.startswith("vertex"):
|
||||
coords = vertex_pattern.match(line).groups();
|
||||
for i in range(0,3):
|
||||
x = float(coords[i])
|
||||
|
||||
if min_coord[i] != None :
|
||||
min_coord[i] = min(min_coord[i], x)
|
||||
else:
|
||||
min_coord[i] = x
|
||||
|
||||
if max_coord[i] != None :
|
||||
max_coord[i] = max(max_coord[i], x)
|
||||
else:
|
||||
max_coord[i] = x
|
||||
|
||||
line = self.file.readline()
|
||||
|
||||
self.file.close()
|
||||
|
||||
self.width = max_coord[0] - min_coord[0]
|
||||
self.depth = max_coord[1] - min_coord[1]
|
||||
self.height = max_coord[2] - min_coord[2]
|
||||
self.polycount = count
|
||||
|
||||
def get_polycount(self):
|
||||
if self.polycount == 0:
|
||||
self.file.open()
|
||||
self.polycount = 0
|
||||
|
||||
if self.file.read(5) != "solid":
|
||||
self.file.seek(80)
|
||||
self.polycount = struct.unpack("i",self.file.read(4))[0]
|
||||
else:
|
||||
line = self.file.readline()
|
||||
while line != "":
|
||||
line = line.strip()
|
||||
if line.startswith("facet"):
|
||||
self.polycount += 1
|
||||
line = self.file.readline()
|
||||
|
||||
self.save()
|
||||
|
||||
self._generate_meta_infos()
|
||||
return self.polycount
|
||||
|
||||
def get_width(self):
|
||||
if self.polycount == 0:
|
||||
self._generate_meta_infos()
|
||||
return self.width
|
||||
|
||||
def get_depth(self):
|
||||
if self.polycount == 0:
|
||||
self._generate_meta_infos()
|
||||
return self.depth
|
||||
|
||||
def get_height(self):
|
||||
if self.polycount == 0:
|
||||
self._generate_meta_infos()
|
||||
return self.height
|
||||
|
||||
|
||||
|
|
|
@ -127,6 +127,30 @@
|
|||
{{geometry.get_polycount}}
|
||||
</div>
|
||||
</div>
|
||||
<div class="pure-g-r">
|
||||
<div class="pure-u-1-3">
|
||||
<i class="icon-resize-horizontal"></i> <b>width:</b>
|
||||
</div>
|
||||
<div class="pure-u-2-3">
|
||||
{{geometry.get_width}}
|
||||
</div>
|
||||
</div>
|
||||
<div class="pure-g-r">
|
||||
<div class="pure-u-1-3">
|
||||
<i class="icon-resize-vertical"></i> <b>height:</b>
|
||||
</div>
|
||||
<div class="pure-u-2-3">
|
||||
{{geometry.get_height}}
|
||||
</div>
|
||||
</div>
|
||||
<div class="pure-g-r">
|
||||
<div class="pure-u-1-3">
|
||||
<i class="icon-resize-full"></i> <b>depth:</b>
|
||||
</div>
|
||||
<div class="pure-u-2-3">
|
||||
{{geometry.get_depth}}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<h2><i class="icon-download-alt"></i> Downloads</h2>
|
||||
<a class="pure-button button-green" href="{{geometry.file.url}}"><i class="icon-download"></i> Download Model</a><br/>
|
||||
|
|
Loading…
Reference in New Issue