Hi @onlyforpeace
There you go with the fixxed script
# coding: utf-8
#triblocpypse02
import rhinoscriptsyntax as rs
from collections import Counter
def NbAngleDroit(cont):
points=rs.CurvePoints(cont)
lp = len(points)-1
count=0
for i in range(lp):
angle=rs.Angle2( [points[i] , points[(i+1)%lp] ], [points[(i+1)%lp] , points[(i+2)%lp]])
#print (angle)
if round(angle[0])==90:count+=1
#print count
return count
def PseAPlat():
ep_std=(400,200,100)
tol=0.00001
stop=False
origine=rs.coerce3dpoint([0,0,0])
blocs=rs.GetObjects('selectionne les blocs à trier',16)
if not blocs : return
#Test NOM de chaque Blocs
rs.EnableRedraw(False)
for bloc in blocs:
if not rs.IsPolysurfaceClosed(bloc):
rs.ObjectColor(bloc,(0,127,0))
stop=True
if not rs.ObjectName(bloc):
rs.ObjectColor(bloc,(0,0,255))
stop=True
else:
try:
name=int(rs.ObjectName(bloc))
except:
rs.ObjectColor(bloc,(250,0,0))
stop=True
if stop:
print('les verts ne sont pas fermés,les bleus n\'ont pas de nom et les rouges sont només n\'importe comment')
return
#tri par numero
sorted_blocs=TriBlocParNum(blocs)
ep400=[]
ep200=[]
ep100=[]
epother=[]
second_passage=False
for bloc in sorted_blocs:
if second_passage:
decal= bboxdecal[1].X-bboxdecal[0].X+500
origine.X+=decal
second_passage=True
#trouve la boite orientée sur le bloc
orientedbox=OrientedBox(bloc,ep_std)
#if not orientedbox:
#second_passage=False
#continue
pt1=orientedbox[0]
pt2=orientedbox[1]
pt3=orientedbox[2]
newbloc=BlocAPlat(bloc,pt1,pt2,pt3,origine)
bboxdecal=rs.BoundingBox(newbloc)
rs.AddLayer('PSE à PLAT')
rs.AddLayer('PSE à PLAT::400',(123, 38, 205))
rs.AddLayer('PSE à PLAT::200',(123, 138, 205))
rs.AddLayer('PSE à PLAT::100',(123, 238, 205))
rs.AddLayer('PSE à PLAT::Hors Std',(0,100,180))
if orientedbox[5]== 400:
rs.ObjectLayer(newbloc[0],'PSE à PLAT::400')
elif orientedbox[5]== 200:
rs.ObjectLayer(newbloc[0],'PSE à PLAT::200')
elif orientedbox[5]== 100:
rs.ObjectLayer(newbloc[0],'PSE à PLAT::100')
else:
rs.ObjectLayer(newbloc[0],'PSE à PLAT::Hors Std')
#ajout de l'annotation
ptnote=rs.coerce3dpoint((origine.X+50,4,420))
note='Bloc' + rs.ObjectName(bloc)+' ep '+str(int(orientedbox[5]))+' '+str(int(orientedbox[3]))+' x '+str(int(orientedbox[4]))
textnote=rs.AddText(note,ptnote,100)
if orientedbox[3]==1200 and orientedbox[4]==3000:
if orientedbox[5]== 400:
rs.ObjectColor(textnote,(0,0,255))
elif orientedbox[5]== 200:
rs.ObjectColor(textnote,(255,0,0))
else:
rs.ObjectColor(textnote,(0,255,0))
rs.RotateObject(textnote,ptnote,90)
rs.ObjectLayer(textnote,'PSE à PLAT')
objs=rs.ObjectsByLayer('PSE à PLAT::400')
if not objs:
rs.DeleteLayer('PSE à PLAT::400')
objs=rs.ObjectsByLayer('PSE à PLAT::200')
if not objs:
rs.DeleteLayer('PSE à PLAT::200')
objs=rs.ObjectsByLayer('PSE à PLAT::100')
if not objs:
rs.DeleteLayer('PSE à PLAT::100')
objs=rs.ObjectsByLayer('PSE à PLAT::Hors Std')
if not objs:
rs.DeleteLayer('PSE à PLAT::Hors Std')
rs.EnableRedraw(True)
pass
def BlocAPlat(bloc,pt1,pt2,pt3,origine):
ref=(pt1,pt2,pt3)
t1=origine
t2=rs.coerce3dpoint((1,0,0))
t2.X=t1.X+1
t3=rs.coerce3dpoint((0,0,1))
t3.X=t1.X
targ=(t1,t2,t3)
bloc_a_plat=rs.OrientObject(bloc,ref,targ,1)
box=rs.BoundingBox(bloc_a_plat)
return (bloc_a_plat,box[1])
def TriBlocParNum(blocs):
lstnum=[]
for i in range (0,len(blocs)):
name=int(rs.ObjectName(blocs[i]))
lstnum.append((blocs[i],name))
lst_sort=sorted(lstnum,key=lambda s:s[1])
return [item[0] for item in lst_sort]
pass
def TriSurf (Surfs):
Surfs=sorted([(rs.SurfaceArea(Surf)[0],Surf) for Surf in Surfs])
return [item[1] for item in Surfs]
def CenterPoint(surf):
Usurfdomain=rs.SurfaceDomain(surf,0)
Vsurfdomain=rs.SurfaceDomain(surf,1)
u=(Usurfdomain[0]+Usurfdomain[1])/2
v=(Vsurfdomain[0]+Vsurfdomain[1])/2
ctpt=rs.EvaluateSurface(surf,u,v)
return ctpt
def Plan(surf):
contour=rs.DuplicateSurfaceBorder(surf,type=1)
edges=rs.ExplodeCurves(contour)
sorted_edges=sorted([(rs.CurveLength(edge),edge)for edge in edges])
pt1=rs.CurveStartPoint(sorted_edges[1][1])
pt2=rs.CurveEndPoint(sorted_edges[1][1])
Xvect=rs.VectorCreate(pt2,pt1)
Usurfdomain=rs.SurfaceDomain(surf,0)
Vsurfdomain=rs.SurfaceDomain(surf,1)
u=(Usurfdomain[0]+Usurfdomain[1])/2
v=(Vsurfdomain[0]+Vsurfdomain[1])/2
eval=rs.EvaluateSurface(surf,u,v)
vect=rs.SurfaceNormal(surf,[u,v])
plan=rs.PlaneFromNormal(CenterPoint(surf),vect,Xvect)
rs.DeleteObject(contour)
rs.DeleteObjects(edges)
return plan
def OrientedBox(bloc,ep_std):
#Bloc rectangulaire ou pas?
first_pass=True
if first_pass:
vbloc=rs.SurfaceVolume(bloc)
box=rs.BoundingBox(bloc)
vbbox=(rs.Distance(box[0],box[1]))*(rs.Distance(box[0],box[3]))*(rs.Distance(box[0],box[4]))
blocpp=False
if round(vbbox/1000)==round(vbloc[0]/1000):
blocpp=True
first_pass=False
surfs=rs.ExplodePolysurfaces(bloc)
sorted_surfs=sorted([(rs.SurfaceArea(surf)[0],surf) for surf in surfs],reverse=True)
if blocpp:#si le bloc est parallelepipedique
for epindex in range(0,len(ep_std)):
for i in range (0 , len(sorted_surfs)):
rs.SelectObject(sorted_surfs[i][1])
bplane=Plan(sorted_surfs[i][1])
rs.UnselectAllObjects()
box=rs.BoundingBox(bloc,bplane,in_world_coords=True)
Xtest=rs.Distance(box[0],box[1])
Ytest=rs.Distance(box[0],box[3])
Ztest=rs.Distance(box[0],box[4])
pass
if round(Ztest)== ep_std[epindex] and round(Xtest)==1200:
rs.DeleteObjects(surfs)
return ((box[0],box[1],box[4],round(Xtest),round(Ytest),round(Ztest)))
elif round(Ztest)== ep_std[epindex] and Xtest<Ytest:
rs.DeleteObjects(surfs)
return ((box[0],box[1],box[4],round(Xtest),round(Ytest),round(Ztest)))
elif round(Ztest)== ep_std[epindex] and Xtest > Ytest:
rs.DeleteObjects(surfs)
return((box[1],box[2],box[5],round(Ytest),round(Xtest),round(Ztest)))
elif Ztest < Xtest and Xtest < Ytest :
#rs.DeleteObjects(surfs)
boxtemp=(box[0],box[1],box[4],round(Xtest),round(Ytest),round(Ztest))
elif Ztest < Xtest and Xtest > Ytest :
boxtemp=(box[1],box[2],box[5],round(Xtest),round(Ytest),round(Ztest))
else:# si le bloc n'est pas parrellepidedeic
surfdec=[]
for surf in sorted_surfs:
pass
cont=rs.DuplicateSurfaceBorder(surf[1],1)
nb_angledroit=NbAngleDroit(cont)
if nb_angledroit<=3: surfdec.append(surf[1])
rs.DeleteObject(cont)
#print (surfdec)
for surf in surfdec:
cont=rs.DuplicateSurfaceBorder(surf)
nb_angledroit=NbAngleDroit(cont)
points=rs.CurvePoints(cont)
rs.DeleteObject(cont)
lp = len(points)-1
if nb_angledroit != 0:
for i in range (lp):
angle=rs.Angle2( [points[i] , points[(i+1)%lp] ], [points[(i+1)%lp] , points[(i+2)%lp]])
distx=rs.Distance(points[(i+1)%lp] , points[(i+2)%lp])
disty=rs.Distance(points[i] , points[(i+1)%lp])
if angle[0]==90 and distx<disty:
bplane=rs.PlaneFromPoints(points[(i+1)%lp],points[(i+2)%lp],points[i])
box=rs.BoundingBox(bloc,bplane,in_world_coords=True)
Xtest=rs.Distance(box[0],box[1])
Ytest=rs.Distance(box[0],box[3])
Ztest=rs.Distance(box[0],box[4])
rs.DeleteObjects(surfs)
return ((box[0],box[1],box[4],round(Xtest),round(Ytest),round(Ztest)))
elif angle[0]==90 and disty<distx:
bplane=rs.PlaneFromPoints(points[(i+1)%lp],points[(i+2)%lp],points[i])
box=rs.BoundingBox(bloc,bplane,in_world_coords=True)
Xtest=rs.Distance(box[0],box[1])
Ytest=rs.Distance(box[0],box[3])
Ztest=rs.Distance(box[0],box[4])
rs.DeleteObjects(surfs)
return ((box[1],box[2],box[5],round(Xtest),round(Ytest),round(Ztest)))
else:
for i in range (lp):
vect1=rs.VectorCreate(points[(i+1)%lp],points[i] )
vect2=rs.VectorCreate(points[(i+3)%lp],points[(i+2)%lp])
if rs.IsVectorParallelTo(vect1,vect2)!=0:
bplane=rs.PlaneFromPoints(points[i],points[(i+1)%lp],points[(i+3)%lp])
box=rs.BoundingBox(bloc,bplane,in_world_coords=True)
Xtest=rs.Distance(box[0],box[1])
Ytest=rs.Distance(box[0],box[3])
Ztest=rs.Distance(box[0],box[4])
rs.DeleteObjects(surfs)
return ((box[1],box[2],box[5],round(Xtest),round(Ytest),round(Ztest)))
# en dernier recour
for epindex in range(0,len(ep_std)):
for i in range (0 , len(sorted_surfs)):
rs.SelectObject(sorted_surfs[i][1])
bplane=Plan(sorted_surfs[i][1])
rs.UnselectAllObjects()
box=rs.BoundingBox(bloc,bplane,in_world_coords=True)
Xtest=rs.Distance(box[0],box[1])
Ytest=rs.Distance(box[0],box[3])
Ztest=rs.Distance(box[0],box[4])
pass
if round(Ztest)== ep_std[epindex] and round(Xtest)==1200:
rs.DeleteObjects(surfs)
return ((box[0],box[1],box[4],round(Xtest),round(Ytest),round(Ztest)))
elif round(Ztest)== ep_std[epindex] and Xtest<Ytest:
rs.DeleteObjects(surfs)
return ((box[0],box[1],box[4],round(Xtest),round(Ytest),round(Ztest)))
elif round(Ztest)== ep_std[epindex] and Xtest > Ytest:
rs.DeleteObjects(surfs)
return((box[1],box[2],box[5],round(Ytest),round(Xtest),round(Ztest)))
elif Ztest < Xtest and Xtest < Ytest :
#rs.DeleteObjects(surfs)
boxtemp=(box[0],box[1],box[4],round(Xtest),round(Ytest),round(Ztest))
elif Ztest < Xtest and Xtest > Ytest :
boxtemp=(box[1],box[2],box[5],round(Xtest),round(Ytest),round(Ztest))
rs.DeleteObjects(surfs)
return boxtemp
PseAPlat()
Hope this helps
-Farouk