Export a polyhedron into a *.stl or * .gts file
Hi,
I created a polyhedron in YADE using polyhedra_utils, and exported it using export.
Thanks in advance,
Tina,
NTU
Question information
- Language:
- English Edit question
- Status:
- Solved
- For:
- Yade Edit question
- Assignee:
- No assignee Edit question
- Solved by:
- Jan Stránský
- Solved:
- Last query:
- Last reply:
Revision history for this message
|
#1 |
Hi Tina,
you can do it "manually" if you know how to write .stl or .gts format. It
is not (yet) implemented inside Yade.
cheers
Jan
Revision history for this message
|
#2 |
Hi,
Thanks Jan, I tried some methods to convert *.txt to *.stl, but all failed, Can you give me a help?
Thanks in advance,
Tina
Revision history for this message
|
#3 |
Hi Tina,
based on [1] and tested on [2], you can save to stl directly using the
following function. It saves all polyhedrons, if you need just some, it
should be easy to modify it.
#######
def polyhedra2stl(stl): # stl is string file name, e.g. 'test.stl'
# get triangles
trisCoords = []
for b in O.bodies:
if not isinstance(
continue
vs = [b.state.pos + b.state.ori*v for v in b.shape.v] # vertices in
global coords
trisIds = b.shape.
l = len(trisIds) / 3
trisIds = [trisIds[3*i:3*i+3] for i in range(l)] # triangles as array
of three indices
for t in trisIds:
trisCoord
vertices
# generate content of stl file, see [1]
lines = ['solid polyhedra']
for v1,v2,v3 in trisCoords:
n = (v2-v1)
lines.
lines.append(' outer loop')
for v in (v1,v2,v3):
lines.
lines.append(' endloop')
lines.
lines.
# write to file
with open(stl,'w') as f:
f.writeline
#######
cheers
Jan
[1] https:/
[2] http://
Revision history for this message
|
#4 |
Hi,
Jan, I have tried your code, but the *.stl only contains some triangles, My code is as follows (My YADE is the latest vision):
from yade import polyhedra_utils
from yade import export
from yade import plot
from yade import qt
gravel = PolyhedraMat()
gravel.IsSplitable = True
gravel.strength = 7.9e6
gravel.density = 2678#kg/m^3
gravel.young = 5.98e7 #Pa
gravel.poisson =0.3
gravel.
#create a polyhedron
poly=polyhedra_
O.bodies.
poly.state.
def polyhedra2stl(stl): # stl is string file name, e.g. 'test.stl'
# get triangles
trisCoords = []
for b in O.bodies:
if not isinstance(
vs = [b.state.pos + b.state.ori*v for v in b.shape.v] # vertices in global coords
trisIds = b.shape.
l = len(trisIds) / 3
trisIds = [trisIds[3*i:3*i+3] for i in range(l)] # triangles as array of three indices
for t in trisIds:
# generate content of stl file, see [1]
lines = ['solid polyhedra']
for v1,v2,v3 in trisCoords:
n = (v2-v1)
for v in (v1,v2,v3):
# write to file
with open(stl,'w') as f:
polyhedra2stl(
Revision history for this message
|
#5 |
Hi Tina,
I have tried your code and did not face any problem..
please be more specific on "the *.stl only contains some triangles".
The code is sopposed to save surface of the polyhedron as a set of
triangles. Are some triangles missing? Or did you expect some other output?
I don't use stl myself, but according to [1], "In practice, however, all
facets are simple triangles.". Probably it would be possible to save each
face as a polygon, not only triangles..
cheers
Jan
Revision history for this message
|
#6 |
Thanks Jan,
My goal is to fill this polyhedron with some sub-particles, export *.stl may be an unwise decision. Is there any better method to finish this task??
Tina,
NTU
Revision history for this message
|
#7 |
Hi Tina,
it pretty much depends on what program/method you use for filling. In my
opinion, both gts and stl are equally good, since they are relatively
easily convertible and saving basically the same information.
cheers
Jan
Revision history for this message
|
#8 |
Hi Jan,
Thanks for your patience, but I got an error by importing the *.stl file generated by the above code. The error and my new code are as follows.
from yade import ymport
from yade import pack
from yade import qt
pred=O.
spheres=
O.bodies.
qt.View()
qt.Controller()
I got this error:
$ yade fillsphere.
TCP python prompt on localhost:9000, auth cookie `sdcaek'
XMLRPC info provider on http://
Running script fillsphere.py
ERROR /home/tina/
Traceback (most recent call last):
File "/home/
execfile(
File "fillsphere.py", line 6, in <module>
spheres=
File "/home/
if not dim: dim=predicate.dim()
AttributeError: 'list' object has no attribute 'dim'
[[ ^L clears screen, ^U kills line. F12 controller, F11 3d view (use h-key for showing help), F10 both, F9 generator, F8 plot. ]]
Besides, I also tried pack.inConvexPo
This is my code:
from yade import polyhedra_utils
rock = PolyhedraMat()
rock.IsSplitable = True
rock.strength = 6e3
poly = polyhedra_
O.bodies.
pred=pack.
aabb=pred.aabb()
dim0=aabb[
spheres=
O.bodies.
from yade import qt
qt.View()
qt.Controller()
and this is the error:
~/Desktop/
Welcome to Yade 2017.01a
TCP python prompt on localhost:9000, auth cookie `yudssc'
XMLRPC info provider on http://
Running script poly.py
Traceback (most recent call last):
File "/home/
execfile(
File "poly.py", line 10, in <module>
pred=
File "/home/
self.
TypeError: 'Body' object is not iterable
[[ ^L clears screen, ^U kills line. F12 controller, F11 3d view (use h-key for showing help), F10 both, F9 generator, F8 plot. ]]
Revision history for this message
|
#9 |
Hi Tina,
thanks for clarification :-) you have several options here, using existing
answers of this question:
(ymport.stl returns list of facets, it cannot be used as a predicate for
randomDensePack. For this case, you have to use pack.inGtsSurface)
###############
import gts
facets = ymport.
surf = gts.Surface()
for f in facets:
vs = [f.state.pos + f.state.ori*v for v in f.shape.vertices]
gtsvs = [gts.Vertex(
es = [gts.Edge(
face = gts.Face(
surf.add(face)
surf.cleanup(1e-6)
assert surf.is_closed()
pred = pack.inGtsSurfa
sphs = pack.randomDens
O.bodies.
###############
cheers
Jan
Revision history for this message
|
#10 |
Jan,
Thanks for your patience, but the error---"Can't open file: tina.st" still exists. I guess there is something wrong with the code---"poly2stl" ,although the *.stl can be displayed in Meshlab and looks identical with polyhedron generated in Yade.
Besides, I converted *.stl to *.gts using stl2gts, and using the following code to fill this polyhedron, but it got a cuboid package.
I replaced this *.gts with horse.coarse.gts, and this one got correct result. Thus, I guess the poly2stl outputs something wrong, please check this code---poly2stl.
Many thanks.
My code was as following:
from yade import ymport
from yade import pack
surface=
pred=pack.
assembly=
O.bodies.
Revision history for this message
|
#11 |
Hi Tina,
> "Can't open file: tina.st"
it means that the file cannot be opened :-) could you check if it exists? I put here a complete script, that works for me:
#######
from yade import polyhedra_utils
from yade import export, ymport
from yade import plot
from yade import pack
from yade import qt
gravel = PolyhedraMat()
gravel.IsSplitable = True
gravel.strength = 7.9e6
gravel.density = 2678#kg/m^3
gravel.young = 5.98e7 #Pa
gravel.poisson =0.3
gravel.
#create a polyhedron
poly=polyhedra_
poly.shape.wire = True
O.bodies.
poly.state.
def polyhedra2stl(stl):
trisCoords = []
for b in O.bodies:
if not isinstance(
continue
vs = [b.state.pos + b.state.ori*v for v in b.shape.v]
trisIds = b.shape.
l = len(trisIds) / 3
trisIds = [trisIds[3*i:3*i+3] for i in range(l)]
for t in trisIds:
trisCoord
#
lines = ['solid polyhedra']
for v1,v2,v3 in trisCoords:
n = (v2-v1)
lines.
lines.append(' outer loop')
for v in (v1,v2,v3):
lines.
lines.append(' endloop')
lines.
lines.
#
with open(stl,'w') as f:
f.writeline
polyhedra2stl(
facets = ymport.
surf = gts.Surface()
for f in facets:
vs = [f.state.pos + f.state.ori*v for v in f.shape.vertices]
gtsvs = [gts.Vertex(
es = [gts.Edge(
face = gts.Face(
surf.add(face)
surf.cleanup(1e-6)
assert surf.is_closed()
pred = pack.inGtsSurfa
sphs = pack.randomDens
O.bodies.
#######
cheers
Jan
Revision history for this message
|
#12 |
Thanks Jan,
I just ran this code in my yade2017.01a, but it still failed. I guess something wrong happens when I copy the code, please send this code in .py format to my E-mail:<email address hidden>
Thanks for your patience,
Best regards.
Tina
Revision history for this message
|
#13 |
Thanks Jan Stránský, that solved my question.
Revision history for this message
|
#14 |
Hi Tina,
> but it still failed
please always attach the error message (or be more specific why it does not work). This information alone has no value for the problem solution.
The script sent to the email
cheers
Jan
Revision history for this message
|
#15 |
Hi Jan,
I have tried this code in Yade 1.20, and It gets a correct result. Anyway, thank you very much. The latest version (2017.01a) may contain a bug.
Best regards,
Tina
Revision history for this message
|
#16 |
Hi Tina,
> latest version (2017.01a) may contain a bug
please send here (or open a bug issue) what error you get
thanks
Jan
Revision history for this message
|
#17 |
Hi Jan,
I have tried the code you provided above in version(2018.02b). It also came out the error message as following:
###
ERROR /data/trunk/
** (python:7975): CRITICAL **: pygts_vertices_
(python:7975): Gts-CRITICAL **: gts_bb_tree_new: assertion 'bboxes != NULL' failed
Traceback (most recent call last):
File "/usr/bin/
execfile(
File "stl2sphere.py", line 59, in <module>
pred = pack.inGtsSurfa
RuntimeError: Could not create GTree
###
I also tried the other stl file which produced by scanning software, the same problem happened with "Can't open file: xxx.stl"
Revision history for this message
|
#18 |
Hi, Please don't re-open an old question.
http://
Revision history for this message
|
#19 |
Also see [1], might be the same problem
Jan
Revision history for this message
|
#20 |
Thanks Jan,
I have tried the procedure in stl-gts folder in trunk-2018.02b and I made it by following conversion:
STL->GTS->Yade
to convert format by using "stl2gts -r <xx.stl> xx.gts"
after that, the new version of Yadedaily can open the xx.gts
#it requires libgts-bin installed
# © 2009 Václav Šmilauer <email address hidden>
# © 2013 Anton Gladky <email address hidden>
That may be a solution to solve the problem. Thanks Jan~
Dez