Ciao a tutti, esiste un comando che mi da in automatico le quote di un parallelepipedo?
Ciao Thomas,
qui c’e’ uno script che avevo fatto anni fa per un amico che si occupava di carpenteria metallica.
Basta selezionare in sequenza i vari pezzi da esaminare, l’ouput si aggiorna di conseguenza.
Lo script mostra anche la bounding box ottenuta, da cui ricava le misure.
Con Enter l’esecuzione dello script termina.
E’ una cosa un po’ a tentativi, ma per un parallelepipedo credo possa servire.
Nessuna garanzia ovviamente sul risultato.
import Rhino
import scriptcontext
import System
def params():
tolerance = 0.1
wirecolor = System.Drawing.Color.FromArgb( 0, 255, 255 )
textcolor = System.Drawing.Color.FromArgb( 0, 255, 0 )
wirethickness = 4
textpoint = Rhino.Geometry.Point2d( 200, 200 )
textheight = 30
figures = 1
return (
tolerance, wirecolor, textcolor, wirethickness,
textpoint, textheight, figures )
def getbb( plane, obrefs ):
bigbox = Rhino.Geometry.BoundingBox.Empty
for obref in obrefs:
geom = obref.Geometry()
box = geom.GetBoundingBox( plane )
bigbox = Rhino.Geometry.BoundingBox.Union( bigbox, box )
return bigbox
def getsizes( box ):
pt = list( box.GetCorners() )
res = []
res.append( pt[ 0 ].DistanceTo( pt[ 1 ] ) )
res.append( pt[ 0 ].DistanceTo( pt[ 3 ] ) )
res.append( pt[ 0 ].DistanceTo( pt[ 4 ] ) )
res.sort()
return res
def getvolume( box ):
p0 = box.Corner( True, True, True )
p1 = box.Corner( False, True, True )
p2 = box.Corner( True, False, True )
p3 = box.Corner( True, True, False )
vol = (
p0.DistanceTo( p1 ) * p0.DistanceTo( p2 ) * p0.DistanceTo( p3 ) )
return vol
def bblines( box, plane ):
xform = Rhino.Geometry.Transform.PlaneToPlane(
Rhino.Geometry.Plane.WorldXY, plane )
co = list( box.GetCorners() )
lis = []
lis.append( Rhino.Geometry.Line( co[ 0 ], co[ 1 ] ) )
lis.append( Rhino.Geometry.Line( co[ 1 ], co[ 2 ] ) )
lis.append( Rhino.Geometry.Line( co[ 2 ], co[ 3 ] ) )
lis.append( Rhino.Geometry.Line( co[ 3 ], co[ 0 ] ) )
lis.append( Rhino.Geometry.Line( co[ 4 ], co[ 5 ] ) )
lis.append( Rhino.Geometry.Line( co[ 5 ], co[ 6 ] ) )
lis.append( Rhino.Geometry.Line( co[ 6 ], co[ 7 ] ) )
lis.append( Rhino.Geometry.Line( co[ 7 ], co[ 4 ] ) )
lis.append( Rhino.Geometry.Line( co[ 0 ], co[ 4 ] ) )
lis.append( Rhino.Geometry.Line( co[ 1 ], co[ 5 ] ) )
lis.append( Rhino.Geometry.Line( co[ 2 ], co[ 6 ] ) )
lis.append( Rhino.Geometry.Line( co[ 3 ], co[ 7 ] ) )
[ li.Transform( xform ) for li in lis ]
return lis
def surplane( su, tolr ):
ok, pla = su.TryGetPlane( tolr )
if ok:
dm0 = su.Domain( 0 )
dm1 = su.Domain( 1 )
ok, pla = su.FrameAt( dm0.Mid, dm1.Mid )
return pla
ok, cyl = su.TryGetCylinder( tolr )
if ok:
cir = cyl.CircleAt( 0.0 )
return cir.Plane
ok, con = su.TryGetCone( tolr )
if ok:
return con.Plane
dm0 = su.Domain( 0 )
dm1 = su.Domain( 1 )
ok, pla = su.FrameAt( dm0.Mid, dm1.Mid )
if ok:
return pla
else:
return None
def curplane( cu, tolr ):
ok, pla = cu.TryGetPlane( tolr )
if ok:
return pla
if cu.IsLinear( tolr ):
dm = cu.Domain
pla = Rhino.Geometry.Plane( cu.PointAtStart, cu.TangentAtStart )
return pla
else:
return None
def split( su ):
sp0 = list( su.GetSpanVector( 0 ) )
sp1 = list( su.GetSpanVector( 1 ) )
if len( sp0 ) == 3:
return list( su.Split( 0, sp0[ 1 ] ) )
elif len( sp0 ) > 3:
su0, su1 = list( su.Split( 0, sp0[ 1 ] ) )
return [ su0 ] + split( su1 )
elif len( sp1 ) == 3:
return list( su.Split( 1, sp1[ 1 ] ) )
elif len( sp1 ) > 3:
su0, su1 = list( su.Split( 1, sp1[ 1 ] ) )
return [ su0 ] + split( su1 )
else:
return [ su ]
class conduit( Rhino.Display.DisplayConduit ):
def __init__( self, colr, thik, tcolr, texp, thei, figs ):
Rhino.Display.DisplayConduit.__init__( self )
self.lins = []
self.colr = colr
self.thik = thik
self.tcolr = tcolr
self.tex = ''
self.texp = texp
self.thei = thei
self.fmt = '%%.%df' % figs
def addlins( self, lins ):
self.lins.extend( lins )
def setsize( self, s0, s1, s2 ):
vol = s0 * s1 * s2
format = '%s x %s x %s - volume: %s' % (
self.fmt, self.fmt, self.fmt, self.fmt )
self.tex = format % ( s0, s1, s2, vol )
def clear( self ):
self.lins = []
self.tex = ''
def pritex( self ):
if self.tex:
print( self.tex )
def PostDrawObjects( self, eventargs ):
for lin in self.lins:
eventargs.Display.DrawLine( lin, self.colr, self.thik )
if self.tex:
eventargs.Display.Draw2dText(
self.tex, self.tcolr, self.texp, False, self.thei )
def main():
tolr, colr, tcolr, thik, texp, thei, figs = params()
con = conduit( colr, thik, tcolr, texp, thei, figs )
con.Enabled = True
while True:
scriptcontext.escape_test()
gob = Rhino.Input.Custom.GetObject()
gob.SetCommandPrompt( '?' )
gob.AcceptNothing( True )
gob.Get()
res = gob.Result()
if res == Rhino.Input.GetResult.Cancel:
con.Enabled = False
return
elif res == Rhino.Input.GetResult.Nothing:
break
elif res == Rhino.Input.GetResult.Object:
obrefs = gob.Objects()
scriptcontext.doc.Objects.UnselectAll()
scriptcontext.doc.Views.Redraw()
minpla = Rhino.Geometry.Plane.WorldXY
box = getbb( minpla, obrefs )
minbox = box
minvol = getvolume( minbox )
for obref in obrefs:
scriptcontext.escape_test()
rep = obref.Brep()
if rep:
sucnt = 0
sunum = rep.Faces.Count
for ix in range( sunum ):
sucnt += 1
if ( sucnt % 100 ) == 0:
pro = '%d / %d' % ( sucnt, sunum )
Rhino.RhinoApp.SetCommandPrompt( pro )
scriptcontext.escape_test()
sur = rep.Faces[ ix ]
sus = split( sur.DuplicateSurface() )
for su in sus:
pla = surplane( su, tolr )
if pla:
box = getbb( pla, obrefs )
vol = getvolume( box )
if vol < minvol:
minbox = box
minvol = vol
minpla = pla
bre = sur.DuplicateFace( False )
cus = list( bre.DuplicateEdgeCurves() )
for cu in cus:
pla = curplane( cu, tolr )
if pla:
box = getbb( pla, obrefs )
vol = getvolume( box )
if vol < minvol:
minbox = box
minvol = vol
minpla = pla
cur = obref.Curve()
if cur:
flags = Rhino.Geometry.CurveSimplifyOptions.All
angtol = scriptcontext.doc.ModelAngleToleranceRadians
ncu = cur.Simplify( flags, tolr, angtol )
if ncu:
cus = list( ncu.DuplicateSegments() )
for cu in cus:
pla = curplane( cu, tolr )
if pla:
box = getbb( pla, obrefs )
vol = getvolume( box )
if vol < minvol:
minbox = box
minvol = vol
minpla = pla
s0, s1, s2 = getsizes( minbox )
con.clear()
con.setsize( s0, s1, s2 )
lins = bblines( minbox, minpla )
con.addlins( lins )
scriptcontext.doc.Views.Redraw()
con.pritex()
con.Enabled = False
scriptcontext.doc.Views.Redraw()
main()
2 Mi Piace
Perfetto… potresti dirmi dove modificare le cifre dopo la virgola? mi piacerebbe che ce ne fossero 2…
Grazie
1 Mi Piace
Alla riga 12, basta settare la variabile figures
figures = 2
2 Mi Piace