New polyhedron macro based on netlib library

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
edwilliams16
Veteran
Posts: 3079
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

New polyhedron macro based on netlib library

Post by edwilliams16 »

netlib has a file of many (141!) polyhedra and their nets dating from the 80's.
I wrote a macro to extract the data and create the objects.

Code: Select all

''' This Macro creates polyhedra and their planar nets listed in the netlib database https://netlib.org/polyhedra
  
    Set the index polyType - index list at the bottom of the file
    With haveFile = False the data will be downloaded. For regular use, download the file yourself, set haveFile=True and
    provide the path to your downloaded file in filename
    Author edwilliams16
    Rev 0.1  11/26/2022
'''


import Part
from FreeCAD import Vector as V3
from urllib.request import urlopen



def extractVector(line):
    while line.find('[') >= 0:
        ind1 = line.index('[')
        ind2 = line.index(']')
        line = line[:ind1] + line[ind2+1:]
    return [float(v) for v in line.split(' ')]



def parseSfaces(lines):
    pass

def parseSvertices(lines):
    pass

def parseNet(lines):
    return [[int(x) for x in line.split(' ')[1:]] for line in lines[1:]]

def parseHinges(lines):
    splitLines = [line.split(' ') for line in lines[1:]]
    return [[int(n1), int(n2), int(n3), int(n4), float(x)] for (n1, n2, n3, n4, x) in splitLines]

def parseDih(lines):
    splitLines = [line.split(' ') for line in lines[1:]]
    return [[int(n1), int(n2), int(n3), float(x)] for (n1, n2, n3, x) in splitLines]

def parseVertices(lines, hasSolid):
    ''' Vertices[:n2] are net vertices
    Vertices[n2:] are solid vertices
    '''
    if hasSolid:
        n1, n2 = lines[0].split(' ')
        n1 = int(n1)
        n2 = int(n2)
    else:
        n1 = int(lines[0])
        n2 = -1
    
    Vertices = [extractVector(line) for line in lines[1:]]
    return (Vertices, n2)



## polyType  9-21 and 32 have nets but no solids
haveFile = False  #set this
polyType = '38'   #set this - list at end of file

if not haveFile:
    f = urlopen('https://netlib.org/polyhedra/' + polyType).read().decode('UTF-8').split('\n')
else:
    #download the required file to avoid fetching it from the net each time
    #https://netlib.org/polyhedra/38
    filename = "/path-to-my-data-file/pentagonal_icositetrahedron.txt"
    f = open(filename, 'r')

secDict = {}
secName = False
for line in f:
    if line[-1] == '\n':
        line = line[:-1]
    if line[0] == ':':
        if secName:
            secDict[secName] = secLines
        secName = line[1:]
        if secName == 'EOF':
            break
        secLines = []
    else:
        secLines.append(line)

#do we have solid info?
if 'solid' in secDict.keys():
    hasSolid = True
else:
    hasSolid = False

for key in secDict:
    if key == 'name':
        name = secDict[key][0]
    elif key == 'number':
        fileNumber = int(secDict[key][0])
    elif key == 'symbol':
        pass
    elif key == 'dual':
        pass
    elif key == 'sfaces':
        parseSfaces(secDict[key])
    elif key == 'svertices':
        parseSvertices(secDict[key])
    elif key == 'net':
        netLinks = parseNet(secDict[key])
    elif key == 'solid':
        solidLinks = parseNet(secDict[key])
    elif key == 'hinges':
        hinges = parseHinges(secDict[key])
    elif key == 'dih':
        dihedrals = parseDih(secDict[key])
    elif key == 'vertices':
            Vertices, solidIndex = parseVertices(secDict[key], hasSolid)
    else:
        pass


#find center of vertices
sumVect = V3()
for v in Vertices[solidIndex:]:
    sumVect += V3(v)

centerSolid = sumVect/len(Vertices[solidIndex:])

#construct solid
if hasSolid:
    faces = []
    for link in solidLinks:
        points = [Vertices[i] for i in link]
        points.append(points[0])
        wire = Part.makePolygon([V3(p) - centerSolid for p in points])
        faces.append(Part.Face(wire))


    shell = Part.Shell(faces)
    solid = Part.Solid(shell)
    cog = solid.CenterOfMass

    Part.show(solid, name)

#construct net
wires = []
for link in netLinks:
    points = [Vertices[i] for i in link]
    points.append(points[0])
    wire = Part.makePolygon([V3(p) for p in points])
    wires.append(wire)


net = wires[0].fuse(wires[1:])
Part.show(net, name + '_net')

'''
#who has solid data
for i in range(142):
    hasSolidArray = []
    f = urlopen('https://netlib.org/polyhedra/' + str(i)).read().decode('UTF-8')
    if f.find(':solid') >= 0:
        print(f'Polyhedron {i} has solid')
        hasSolidArray.append(True)
    else:
        print(f'Polyhedron {i} has no solid')
        hasSolidArray.append(False)
'''


'''
#file contents
namelist =[]
for i in range(142):
    f = urlopen('https://netlib.org/polyhedra/' + str(i)).read().decode('UTF-8').split('\n')
    nextOne = False
    for line in f:
        if nextOne:
            namelist.append(f'{i}   {line}')
            break
        if line[0:5] == ':name':
            nextOne = True

'''
'''
0   tetrahedron
1   cube
2   octahedron
3   dodecahedron
4   icosahedron
5   small stellated dodecahedron
6   great dodecahedron
7   great stellated dodecahedron
8   great icosahedron
9   truncated tetrahedron
10   cuboctahedron
11   truncated cube
12   truncated octahedron
13   rhombicuboctahedron
14   great rhombicuboctahedron
15   snub cube (laevo)
16   icosidodecahedron
17   truncated dodecahedron
18   truncated icosahedron
19   rhombicosidodecahedron
20   great rhombicosidodecahedron
21   snub dodecahedron (laevo)
22   triangular prism
23   pentagonal prism
24   hexagonal prism
25   octagonal prism
26   decagonal prism
27   square antiprism
28   pentagonal antiprism
29   hexagonal antiprism
30   octagonal antiprism
31   decagonal antiprism
32   triakis tetrahedron
33   rhombic dodecahedron
34   triakis octahedron
35   tetrakis hexahedron
36   trapezoidal icositetrahedron
37   hexakis octahedron
38   pentagonal icositetrahedron (dextro)
39   rhombic triacontahedron
40   triakis icosahedron
41   pentakis dodecahedron
42   trapezoidal hexecontahedron
43   hexakis icosahedron
44   pentagonal hexecontahedron (dextro)
45   square pyramid (J1)
46   pentagonal pyramid (J2)
47   triangular cupola (J3)
48   square cupola (J4)
49   pentagonal cupola (J5)
50   pentagonal rotunda (J6)
51   elongated triangular pyramid (J7)
52   elongated square pyramid (J8)
53   elongated pentagonal pyramid (J9)
54   gyroelongated square pyramid (J10)
55   gyroelongated pentagonal pyramid (J11)
56   triangular dipyramid (J12)
57   pentagonal dipyramid (J13)
58   elongated triangular dipyramid (J14)
59   elongated square dipyramid (J15)
60   elongated pentagonal dipyramid (J16)
61   gyroelongated square dipyramid (J17)
62   elongated triangular cupola (J18)
63   elongated square cupola (J19)
64   elongated pentagonal cupola (J20)
65   elongated pentagonal rotunds (J21)
66   gyroelongated triangular cupola (J22)
67   gyroelongated square cupola (J23)
68   gyroelongated pentagonal cupola (J24)
69   gyroelongated pentagonal rotunda (J25)
70   gyrobifastigium (J26)
71   triangular orthobicupola (J27)
72   square orthobicupola (J28)
73   square gyrobicupola (J29)
74   pentagonal orthobicupola (J30)
75   pentagonal gyrobicupola (J31)
76   pentagonal orthocupolarontunda (J32)
77   pentagonal gyrocupolarotunda (J33)
78   pentagonal orthobirotunda (J34)
79   elongated triangular orthobicupola (J35)
80   elongated triangular gyrobicupola (J36)
81   elongated square gyrobicupola (J37)
82   elongated pentagonal orthobicupola (J38)
83   elongated pentagonal gyrobicupola (J39)
84   elongated pentagonal orthocupolarotunda (J40)
85   elongated pentagonal gyrocupolarotunda (J41)
86   elongated pentagonal orthobirotunda (J42)
87   elongated pentagonal gyrobirotunda (J43)
88   gyroelongated triangular bicupola (J44)
89   gyroelongated square bicupola (J45)
90   gyroelongated pentagonal bicupola (J46)
91   gyroelongated pentagonal cupolarotunda (J47)
92   gyroelongated pentagonal birotunda (J48)
93   augmented triangular prism (J49)
94   biaugmented triangular prism (J50)
95   triaugmented triangular prism (J51)
96   augmented pentagonal prism (J52)
97   biaugmented pentagonal prism (J53)
98   augmented hexagonal prism (J54)
99   parabiaugmented hexagonal prism (J55)
100   metabiaugmented hexagonal prism (J56)
101   triaugmented hexagonal prism (J57)
102   augmented dodecahedron (J58)
103   parabiaugmented dodecahedron (J59)
104   metabiaugmented dodecahedron (J60)
105   triaugmented dodecahedron (J61)
106   metabidiminished icosahedron (J62)
107   tridiminished icosahedron (J63)
108   augmented tridiminished icosahedron (J64)
109   augmented truncated tetrahedron (J65)
110   augmented truncated cube (J66)
111   biaugmented truncated cube (J67)
112   augmented truncated dodecahedron (J68)
113   parabiaugmented truncated dodecahedron (J69)
114   metabiaugmented truncated dodecahedron (J70)
115   triaugmented truncated dodecahedron (J71)
116   gyrate rhombicosidodecahedron (J72)
117   parabigyrate rhombicosidodecahedron (J73)
118   metabigyrate rhombicosidodecahedron (J74)
119   trigyrate rhombicosidodecahedron (J75)
120   diminished rhombicosidodecahedron (J76)
121   paragyrate diminished rhombicosidodecahedron (J77)
122   metagyrate diminished rhombicosidodecahedron (J78)
123   bigyrate diminished rhombicosidodecahedron (J79)
124   parabidiminished rhombicosidodecahedron (J80)
125   metabidiminished rhombicosidodecahedron (J81)
126   gyrate bidiminished rhombicosidodecahedron (J82)
127   tridiminished rhombicosidodecahedron (J83)
128   snub disphenoid (J84)
129   snub square antiprism (J85)
130   sphenocorona (J86)
131   augmented sphenocorona (J87)
132   sphenomegacorona (J88)
133   hebesphenomegacorona (J89)
134   disphenocingulum (J90)
135   bilunabirotunda (J91)
136   triangular hebesphenorotunda (J92)
137   tetrahemihexahedron
138   octahemioctahedron
139   small ditrigonal icosidodecahedron
140   dodecadodecahedron
141   echidnahedron
'''

I'll get it up on github if there is interest. Heres a random example - a paragyrate diminished rhombicosidodecahedron and its net.
(The net is is the thing you can fold up into the shape.)
Screen Shot 2022-11-26 at 4.57.57 PM.png
Screen Shot 2022-11-26 at 4.57.57 PM.png (28.56 KiB) Viewed 525 times
EDIT: fix to script that somehow didn't make it into the forum post.
Post Reply