PythonScript for draw ball bearing


#1

Hi All
I condivide the below Python script for draw a ball bearing :

# -*- encoding: UTF-8 -*-

import rhinoscriptsyntax as rs
import math
import strpunto
def EliminaZeri(quota):
    """
    Elimina gli zeri non significativi dopo il punto decimale
    quota  > può essere un numero o una stringa
    ritorna una stringa col numero senza zeri non significativi
    """
    stringa=str(quota)
    if "." in stringa: 
        k=0
        while k==0:
            a=stringa[-1]
            if a=="0" or a==".":
                stringa=stringa[:-1]  #elimina ultimo elemento della stringa
                if a==".":break
            else:
                k=1    
    return stringa

def orient_2_pt(objs,p1,p2): # funzione per orientare vite vecchia maniera
    while True:        
        newobjs=rs.CopyObjects(objs)        
        ps=rs.GetPoint("primo punto applicazione   < Enter per terminare>")
        if ps==None:
            rs.DeleteObjects(newobjs)
            break
        rs.EnableRedraw(False)
        rs.SelectObjects(newobjs)
        rs.Command("group")
        rs.EnableRedraw(True)
        rs.Command("-Orient  copy=no  W"+str(p1)+" W"+str(p2)+" W"+str(ps)+"  ",False)
        rs.UnselectAllObjects()

def cuscinetto():
    de=rs.GetDocumentData("RMAcuscinettoSfere","de")
    di=rs.GetDocumentData("RMAcuscinettoSfere","di")
    la=rs.GetDocumentData("RMAcuscinettoSfere","la")
    if de==None:de="42"
    if di==None: di="20"
    if la==None: la="9"
    arritems=["Diametro esterno","Diametro interno","Larghezza"]
    arrvalue=[de,di,la]
    newplb=rs.PropertyListBox(arritems,arrvalue,"Cuscinetto a sfere","immetti i dati")
    if newplb!=None:
        de=float(newplb[0])
        di=float(newplb[1])
        la=float(newplb[2])
    else:return
    rs.EnableRedraw(False)
    #definizione variabili per disegnare profilo anello
    spes=(de-di)/6
    smi=spes/14       	#*** SMUSSO INTERNO
    sme=spes/7		    #*** smusso esterno
    re=de/2		        #*** raggio esterno anello esterno cuscinetto
    ri=re-spes          #*** raggio interno anello esterno cuscinetto
    rsfe=spes*1         #*** raggio sfera
    csfey=(re+ri)/4     #*** centro sfera in Y
    trc=(la/2)-math.sqrt(rsfe*rsfe-(rsfe/2)*(rsfe/2))
    if spes*2>=la:
        rs.MessageBox("Misure cuscinetto incongruenti : cambia misure")
        return
    
    p1=[0,ri,trc]
    p2=[0,ri,smi]
    p3=[0,ri+smi,0]
    p4=[0,re-sme,0]
    p5=[0,re,sme]
    p6=[0,re,la-sme]
    p7=[0,re-sme,la]
    p8=[0,ri+smi,la]
    p9=[0,ri,la-smi]
    p10=[0,ri,la-trc]
    arrplane=rs.ViewCPlane()
    p1=rs.XformCPlaneToWorld(p1,arrplane)
    p2=rs.XformCPlaneToWorld(p2,arrplane)
    p3=rs.XformCPlaneToWorld(p3,arrplane)
    p4=rs.XformCPlaneToWorld(p4,arrplane)
    p5=rs.XformCPlaneToWorld(p5,arrplane)
    p6=rs.XformCPlaneToWorld(p6,arrplane)
    p7=rs.XformCPlaneToWorld(p7,arrplane)
    p8=rs.XformCPlaneToWorld(p8,arrplane)
    p9=rs.XformCPlaneToWorld(p9,arrplane)
    p10=rs.XformCPlaneToWorld(p10,arrplane)
    
    arrpoints=[p1,p2,p3,p4,p5,p6,p7,p8,p9,p10]
    idpl=rs.AddPolyline(arrpoints)    
    
    #******* arco sede sfera anello esterno  ******
    pc=[0,re-spes/2,la/2]
    pc=rs.XformCPlaneToWorld(pc,arrplane)
    idarco=rs.AddArc3Pt(p10,p1,pc)
    arrcurve=[idpl,idarco]
    pistae=rs.JoinCurves(arrcurve,True)
    #******* mirror profilo anello esterno anello interno ****
    pa1=[10,(di+de)/4,la/2]  #primo punto  per asse di simmetria
    pa2=[0,(di+de)/4,la/2]   # secondo punto per asse simmetria
    pa1=rs.XformCPlaneToWorld(pa1,arrplane)
    pa2=rs.XformCPlaneToWorld(pa2,arrplane)
    
    pistai=rs.MirrorObject(pistae[0],pa1,pa2,True)
    #******* creazione anelli con revolve
    prS=[0,0,0]        #***Start POint Asse Rotazione
    prE=[0,0,10]       #***End Poinrt Asse Rotazione
    prS=rs.XformCPlaneToWorld(prS,arrplane)  #trasforma i punti da CPlane a Wplane
    prE=rs.XformCPlaneToWorld(prE,arrplane)
    asse=[prS,prE]  #   asse rotazione per revolve
    ane=rs.AddRevSrf(pistae[0],asse) # anello esterno
    ani= rs.AddRevSrf(pistai,asse)   #anello interno
    rs.ObjectColor(ane,[180,180,180])
    rs.ObjectColor(ani,[180,180,180])
    rs.DeleteObject(pistae[0])
    rs.DeleteObject(pistai)
    sfera=rs.AddSphere(pa2,rsfe)
    import f_polararray   #funzione che fa il polar array e restituisce l'array delle sfere
    n=int((re+ri)/rsfe*0.8)    
    sfere=f_polararray.polararray(sfera,prS,n)
    de=EliminaZeri(de)
    di=EliminaZeri(di)
    la=EliminaZeri(la)
    rs.ObjectName(ane,"Cuscinetto Sfere "+de+"x"+di+"x"+la)
    
    objs=([ane]+[ani]+sfere)
    rs.EnableRedraw(True)
    orient_2_pt(objs,prS,prE) # orient ball bearing   
    rs.DeleteObjects(objs)
    de=rs.SetDocumentData("RMAcuscinettoSfere","de",str(de))
    di=rs.SetDocumentData("RMAcuscinettoSfere","di",str(di))
    la=rs.SetDocumentData("RMAcuscinettoSfere","la",str(la))
cuscinetto()

Ciao Vittorio


#2

Hi Steve
Steve, because the script is formatted in this way?
You can do that the script is ok as in Python Forum?
it is possible to attach a file?

Ciao Vittorio


(Steve Baer) #3

Hi Vittorio,
I edited your post to show you how to write syntax highlighting for python scripts.

We should have file attachments working in a day or two.
–Steve


#4

Hey @stevebaer the format it’s ok but the bottom part it’s overlaying the page graphics and it’s impossible to select it correctly and copy/paste it.

I’m on Firefox, FullHD resolution, 2/3 screen windows.
Thanks


#5

Yep, here too.
–Mitch


#6

@skysurfer @Helvetosaur

You guys probably already know this, but you can get the whole script by viewing the page source. (Firefox- right click - view page source)


(Steve Baer) #7

This is a known bug and sam mentioned he fixed it. This should display correctly the next time I upgrade the server.