Code snippet to demonstrate how to access, from a script, a Gui object created by the script

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
TheMarkster
Veteran
Posts: 5508
Joined: Thu Apr 05, 2018 1:53 am

Re: Code snippet to demonstrate how to access, from a script, a Gui object created by the script

Post by TheMarkster »

I have noticed sometimes when pasting code to the console enter must be pressed and sometimes not. This must be related to that, too.
edwilliams16
Veteran
Posts: 3179
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Code snippet to demonstrate how to access, from a script, a Gui object created by the script

Post by edwilliams16 »

DanielLeeWenger wrote: Fri Nov 12, 2021 7:56 pm
I too have been playing with polyhedra. Below is code that I recently completed that will produce the "great rhombicuboctahedron", incorrectly called a "truncated cuboctahedron". I have been working on finding the object from which the "great rhombicuboctahedro" can be found by truncation.
Here's some code I wrote that creates a disdyakis dodecahedron

Code: Select all

'''  Create a Disdyakis dodecahedron from vertex data'''
import Part
from math import sqrt


scaleFactor = 1.0  # =1 => middle edge length =1

#data from http://www.netlib.org/polyhedra/37  https://en.wikipedia.org/wiki/Disdyakis_dodecahedron
#these vertices are tilted and translated for no obvious reason

vertices = [
    App.Vector(2.6133122279038238, -4.3663082302150226, -7.140305645693695),
    App.Vector(2.8343891236796766, -5.0800291876449795, -7.163837801596551),
    App.Vector(2.8419869679386627, -4.0977146372750205, -6.4812216553007066),  # -X
    App.Vector(2.886667096895415, -5.0456133045766546, -6.1657984433994139),  
    App.Vector(3.009894100904886, -4.0067697532409839, -7.6621114168954244),   #+ Z
    App.Vector(3.1604567932353163, -4.8973184004605509, -8.091359576603294),   
    App.Vector(3.1728458528068138, -3.2955543914174693, -6.9782843659031492),  
    App.Vector(3.3382971302138604, -5.6314377767564596, -7.1346547570869689),   # -Y
    App.Vector(3.3513947319758703, -3.9380673488691023, -5.9579217083010231),
    App.Vector(3.6408427234443935, -3.7812913170646378, -7.9936075282074776),   
    App.Vector(3.6831062016314853, -5.5655035406455466, -6.4746655586534303),
    App.Vector(3.6907040458918208, -4.5831889902828067, -5.7920494123621763),    
    App.Vector(3.8510133346080949, -5.4745586566032474, -7.655555320247756),
    App.Vector(3.8662090231277648, -3.5099295558781736, -6.2903230276597041),
    App.Vector(4.0265183118308238, -4.4012992222023037, -8.1538289355476461),
    App.Vector(4.0341161560950081, -3.4189846718394306, -7.4712127892567944),
    App.Vector(4.0763796342737318, -5.2031968954186892, -5.952270819699509),   
    App.Vector(4.3658276257560316, -5.046420863604471, -7.9879566396081482),
    App.Vector(4.3789252275208671, -3.3530504357319652, -6.8112235908168941),   #+Y
    App.Vector(4.544376504923661, -5.6889338210650479, -6.9675939820056528),  
    App.Vector(4.5567655644914461, -4.0871698120259693, -5.8545187713067436),   
    App.Vector(4.7073282568126512, -4.9777184592536349, -6.283766931010124),    #- Z
    App.Vector(4.83055526083214, -3.9388749079095933, -7.7800799045112476),       
    App.Vector(4.8752353897887335, -4.8867735752081337, -7.4646566926071458),   #+X
    App.Vector(4.8828332340508313, -3.904459024847932, -6.7820405463160945),
    App.Vector(5.1039101298205366, -4.6181799822850979, -6.8055727022155921)]

# vertices making up faces
#  No_of vertices (3), vertex_index, vertex_index, vertex_index  indices offset by 50 in data table  i.e 50 -> vertices[0]
vlist =[
    (3, 52, 56, 50),
    (3, 50, 56, 54),
    (3, 54, 55, 50),
    (3, 50, 55, 51),
    (3, 51, 53, 50),
    (3, 50, 53, 52),
    (3, 65, 72, 59),
    (3, 59, 72, 64),
    (3, 64, 55, 59),
    (3, 59, 55, 54),
    (3, 54, 56, 59),
    (3, 59, 56, 65),
    (3, 73, 69, 67),
    (3, 67, 69, 62),
    (3, 62, 55, 67),
    (3, 67, 55, 64),
    (3, 64, 72, 67),
    (3, 67, 72, 73),
    (3, 60, 53, 57),
    (3, 57, 53, 51),
    (3, 51, 55, 57),
    (3, 57, 55, 62),
    (3, 62, 69, 57),
    (3, 57, 69, 60),
    (3, 60, 69, 66),
    (3, 66, 69, 71),
    (3, 71, 70, 66),
    (3, 66, 70, 61),
    (3, 61, 53, 66),
    (3, 66, 53, 60),
    (3, 73, 72, 75),
    (3, 75, 72, 74),
    (3, 74, 70, 75),
    (3, 75, 70, 71),
    (3, 71, 69, 75),
    (3, 75, 69, 73),
    (3, 65, 56, 68),
    (3, 68, 56, 63),
    (3, 63, 70, 68),
    (3, 68, 70, 74),
    (3, 74, 72, 68),
    (3, 68, 72, 65),
    (3, 52, 53, 58),
    (3, 58, 53, 61),
    (3, 61, 70, 58),
    (3, 58, 70, 63),
    (3, 63, 56, 58),
    (3, 58, 56, 52)]


faceList = []
def makeFacesVlist(edgeList, start = 50):
    tList =edgeList[1:]
    noEdges = len(tList)
    assert noEdges == edgeList[0]
    eeList =[]
    for i in range(noEdges):
        jstart = tList[i] -start
        jend = tList[(i+1)%noEdges] - start
        #print (f'{jstart} {jend}')
        edge = Part.Edge(Part.LineSegment(vertices[jstart], vertices[jend]))
        ee = App.ActiveDocument.addObject("Part::Feature", "Edge") 
        ee.Shape = edge
        eeList.append(edge)
        added = eobj.addObject(ee)    
    #print(eeList)
    App.ActiveDocument.recompute()
    w = Part.Wire(eeList)
    face = Part.Face(w)
    ff = App.ActiveDocument.addObject("Part::Feature", "Face")
    ff.Shape = face
    added = fobj.addObject(ff)
    faceList.append(ff.Shape)

#find rotation and translation to orient the object on the z-axis, center at origin 
xhat = (vertices[23] - vertices[2]).normalize()
yhat = (vertices[18] - vertices[7]).normalize()
zhat = (vertices[4] - vertices[21]).normalize()
rot = App.Rotation(xhat, yhat, zhat).inverted()
center = App.Vector(0., 0., 0.)
for v in vertices:
    center = center + v

center = center/len(vertices)
vertices_cen = [ v - center for v in vertices]  #center on origin
vertices_rot = [scaleFactor*rot.multVec(v) for v in vertices_cen]  #scale and rotate
print(f'Circumradius = {((vertices_rot[4] - vertices_rot[21])/2).Length}')
print(f'Edge Lengths {(vertices_rot[2]-vertices_rot[6]).Length}  {(vertices_rot[6]-vertices_rot[0]).Length}  {(vertices_rot[0]-vertices_rot[2]).Length}')
#print(f'Edge Lengths {(vertices_rot[14]-vertices_rot[5]).Length}  {(vertices_rot[5]-vertices_rot[9]).Length}  {(vertices_rot[9]-vertices_rot[14]).Length}')
shortestEdgeLength =  (vertices_rot[0]-vertices_rot[2]).Length

vertices = vertices_rot 
eobj = App.ActiveDocument.addObject("App::DocumentObjectGroup","EdgeGroup")
fobj = App.ActiveDocument.addObject("App::DocumentObjectGroup","FaceGroup")
vobj = App.ActiveDocument.addObject("App::DocumentObjectGroup","VertexGroup")
pva=[]
#create vertices
for v in vertices:
    pv =App.ActiveDocument.addObject("Part::Vertex","Vertex")
    added = vobj.addObject(pv)
    pv.X = v.x
    pv.Y = v.y
    pv.Z = v.z
    pva.append(pv)

#create edges and faces
for edge in vlist:
    makeFacesVlist(edge)

#make solid
solid = Part.Solid(Part.Shell(faceList))
solidobj = App.ActiveDocument.addObject("Part::Feature", "Disdyakis dodecahedron")
solidobj.Shape= solid
App.ActiveDocument.recompute()
#check some parameters
print(f'Volume = {solidobj.Shape.Volume}  Area = {solidobj.Shape.Area}')
print(f'Volume_theory {shortestEdgeLength**3*sqrt(3*(2194 + 1513*sqrt(2)))/7}  Area_theory {shortestEdgeLength**2*sqrt(783+ 436*sqrt(2))*6/7}')



but it can be used to create any of the polyhedra in the netlib database by editing in the vertex data
http://www.netlib.org/polyhedra/
Screen Shot 2021-11-13 at 2.42.39 PM.png
Screen Shot 2021-11-13 at 2.42.39 PM.png (20.79 KiB) Viewed 1041 times
DanielLeeWenger
Posts: 53
Joined: Sun Feb 02, 2020 4:02 am
Location: Santa Cruz, California
Contact:

Re: Code snippet to demonstrate how to access, from a script, a Gui object created by the script

Post by DanielLeeWenger »

Nice to find others working on polyhedra. Thank you edwilliams16

I am happy to find the polyhedra web site. I did not know of it.
mario52
Veteran
Posts: 4690
Joined: Wed May 16, 2012 2:13 pm

Re: Code snippet to demonstrate how to access, from a script, a Gui object created by the script

Post by mario52 »

Hi
TheMarkster wrote: Sat Nov 13, 2021 11:05 pm I have noticed sometimes when pasting code to the console enter must be pressed and sometimes not. This must be related to that, too.
example:

copy as is and paste in the FC console Python

Code: Select all

print("tyty")
enter must be pressed

after try this

Code: Select all

print("tyty")
enter must be not pressed

difference ... end code without carriage return / end code with carriage return

carriagReturn00.png
carriagReturn00.png (19.95 KiB) Viewed 917 times
Wmayer give the explanation (for the loop" for..."), but the topic ... in the forum

mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
TheMarkster
Veteran
Posts: 5508
Joined: Thu Apr 05, 2018 1:53 am

Re: Code snippet to demonstrate how to access, from a script, a Gui object created by the script

Post by TheMarkster »

mario52 wrote: Sun Nov 14, 2021 11:58 am difference ... end code without carriage return / end code with carriage return
Ah, yes, that makes sense.
Post Reply