当前位置: 首页 > news >正文

maxscript中BoundingBox求一个模型的高度

 

BoundingBox反馈模型的选择盒子出左下角前面角的点和右上角后面的点的坐标,再乘以物体的缩放度,求出物体的 长,深,高 。

再搭配获取系统的单位命令 units.SystemType  反馈#centimeters

                                           units.SystemScale 反馈1个单位等于几个厘米缩放因子 反馈出 2

主代码
 

---max中单位厘米的标准化
units.Systemtype = #centimeters --系统单位为厘米
units.SystemScale= 1   --系统缩放为1
units.Displaytype = #metric  --系统显示单位制式为metric
units.MetricType = #centimeters  --系统显示单位为厘米
SetGridSpacing 1   ---网格显示1格为1厘米
-----求长,深,高度
in coordsys world (bbox = nodeLocalBoundingBox objdim = (bbox[2] - bbox[1]) * obj.scaledim = [abs(dim[1]), abs(dim[2]), abs(dim[3])]  --反馈出物体的长,深,高[2.40438,4.50988,8.93427]) 
mysize= units.SystemScale*dim[3] 
unity= units.SystemType as string
print ("模型高度:" + mysize as string + unity )

 当选择模型并运行脚本后,获得反馈  "模型高度:xxx centimeters "

   in coordsys world (bb = nodeLocalBoundingBox objdim = (bb[2] - bb[1]) * $.scale-- 使用绝对值函数处理每个维度的尺寸dim = [abs(dim[1]), abs(dim[2]), abs(dim[3])])

您可以使用nodeLocalBoundingBox()来获取不同坐标系中的变换:

in coordsys world (bb = nodeLocalBoundingBox obj )
#([-7.19332,5.22747,-25.3861],[43.5789,55.9997,25.3861])
in coordsys local(bbl = nodeLocalBoundingBox obj )
#([-25.3861,-25.3861,-25.3861],[25.3861,25.3861,25.3861]) 

引用网上的脚本和Transform from Screen to World - Autodesk Community

-- zsScaleTools
--
-- author: Jan K�hl / www.greenfair.de
-- version: 1.10
-- date: 28.11.2015if zsScaleToolsRollout != undefined do destroyDialog zsScaleToolsRolloutrollout zsScaleToolsRollout "zsScaleTools"
(groupBox 'grp_dimensions' "Dimensions" pos:[5,5] width:115 height:120 align:#leftspinner 'spn_x' "X:" pos:[15,25] width:90 height:16 range:[0.0001,100000,0] type:#worldunits align:#leftspinner 'spn_y' "Y:" pos:[15,50] width:90 height:16 range:[0.0001,100000,0] type:#worldunits align:#leftspinner 'spn_z' "Z:" pos:[15,75] width:90 height:16 range:[0.0001,100000,0] type:#worldunits align:#leftcheckbox 'chk_seldim' "Selection Bound." pos:[15,100] width:100 height:15 align:#left default:false tooltip:"Display Selection Boundaries"groupBox 'grp_unify' "Unify" pos:[125,5] width:80 height:120 align:#leftbutton 'btn_xmin' "<" pos:[135,25] width:20 height:20 align:#leftbutton 'btn_xmid' "|" pos:[155,25] width:20 height:20 align:#leftbutton 'btn_xmax' ">" pos:[175,25] width:20 height:20 align:#leftbutton 'btn_ymin' "<" pos:[135,50] width:20 height:20 align:#leftbutton 'btn_ymid' "|" pos:[155,50] width:20 height:20 align:#leftbutton 'btn_ymax' ">" pos:[175,50] width:20 height:20 align:#leftbutton 'btn_zmin' "<" pos:[135,75] width:20 height:20 align:#leftbutton 'btn_zmid' "|" pos:[155,75] width:20 height:20 align:#leftbutton 'btn_zmax' ">" pos:[175,75] width:20 height:20 toolTip:"" align:#leftcheckbox 'chk_tooltips' "Tooltips" pos:[135, 100] width:60 height:15 align:#left checked:false tooltip:"Display Dimensions as Tooltips"checkbox 'chk_uselocal' "Use Local Coordinate System" pos:[15,130] width:195 height:15 align:#left checked:truecheckbox 'chk_scaledist' "Scale Object Distance" pos:[15,150] width:195 height:15 align:#left checked:falsegroupBox 'grp_proportional' "" pos:[5,165] width:200 height:75 align:#leftcheckbox 'chk_useprop' "Proportional Scale" pos:[15,180] width:185 height:15 align:#left checked:falseradioButtons 'rdo_lockedaxis' "Lock Axis" pos:[33,200] width:155 height:30 labels:#("none", "X", "Y", "Z") default:1 enabled:false align:#leftbutton 'btn_reset' "Reset Scale" pos:[5,250] width:95 height:25 align:#leftbutton 'btn_refresh' "Refresh" pos:[105,250] width:100 height:25 align:#left----------------------------------------- helper functions and variables -----------------------------------------local same = #(true, true, true) -- indicates identical size of objects in selection (x,y,z)local spinners = #(spn_x, spn_y, spn_z)local unifybuttons = #(btn_xmin, btn_xmid, btn_xmax, btn_ymin, btn_ymid, btn_ymax, btn_zmin, btn_zmid, btn_zmax)local uselocal = true -- use local coordinate systemlocal useprop = chk_useprop.checked -- proportional scalelocal lockedaxis = rdo_lockedaxis.state -- lock axis for proportional scalelocal scaledist = chk_scaledist.state -- scale distance proportionallylocal selobjpos = false -- positions of selected objectslocal selcenter = [0,0,0] -- selection centerfn trimnum num places:3 = -- cut float, point3 or array of those to x decimal places(if classof num == point3 do return [trimnum num.x, trimnum num.y, trimnum num.z]if classof num == array do return for n in num collect (trimnum n)a = (num as float) as stringpos = findstring a "."(substring a 1 pos + substring a (pos+1) places) as float)fn GetDim obj localcoord:false trim:true = -- get object dimensions (3 decimal places) in world or local coordinate system (chk_uselocal)(if not localcoord then trimnum (obj.max - obj.min) else(bbox = nodeGetBoundingBox obj obj.transformdim = (bbox[2] - bbox[1]) * obj.scaleif trim then trimnum dim else dim))fn GetDimRange objset localcoord:false trim:true aslist:true = -- get min/average/max dimensions of objects in objset (depends on GetDim)(xvals = #()yvals = #()zvals = #()selobjpos = #()for obj in objset do(dim = GetDim obj localcoord:localcoordappend xvals dim.xappend yvals dim.yappend zvals dim.zappend selobjpos obj.pos)xmin = amin xvalsxmax = amax xvalsymin = amin yvalsymax = amax yvalszmin = amin zvalszmax = amax zvalsif aslist thenresult = #(xmin, ((xmin+xmax)/2), xmax, ymin, ((ymin+ymax)/2), ymax, zmin, ((zmin+zmax)/2), zmax)elseresult = #([xmin, ymin, zmin], [((xmin+xmax)/2), ((ymin+ymax)/2), ((zmin+zmax)/2)], [xmax, ymax, zmax])if not trim then result else trimnum result)---------------------------------- store object positions ----------------------------------fn StorePositions = -- store positions of selected objects(selobjpos = #()for obj in selection do append selobjpos obj.pivotselcenter = selection.center)---------------------------- display functions ----------------------------fn DisplayLock = -- enable or disable ui elements and set warnings(rdo_lockedaxis.enabled = usepropif useprop then lockedaxis = rdo_lockedaxis.state else lockedaxis = 1if selection.count > 1 then(chk_scaledist.enabled = truescaledist = chk_scaledist.state)else(chk_scaledist.enabled = falsescaledist = false)if $ != undefined then(for i=1 to spinners.count do(s = spinners[i]if useprop and i == lockedaxis-1 then s.enabled = false else s.enabled = trueif same[i] then(s.setKeyBrackets = falses.tooltip = "")else(s.setKeyBrackets = trues.tooltip = "-VARIES-")))else(for s in spinners do(s.enabled = falses.tooltip = ""s.setKeyBrackets = false)for btn in unifybuttons do btn.tooltip=""))fn Refresh = -- refresh main display(if $ != undefined do(for btn in unifybuttons do btn.tooltip=""same = #(true, true, true)cnt = selection.countif cnt == 1 then(dim = GetDim $ localcoord:uselocal)else(dim = GetDim $ref = copy dim-- compare size of selected objectsif not chk_tooltips.state then -- no tooltips, faster calculation !!!(ref = GetDim selection[1] localcoord:uselocali = 1while same != #(false, false, false) and i <= cnt do(comp = GetDim selection[i] localcoord:uselocalif comp.x != ref.x do same[1] = falseif comp.y != ref.y do same[2] = falseif comp.z != ref.z do same[3] = falsei += 1))else -- display values as tooltips, slower calculation !!!(vals = GetDimRange selection localcoord:uselocalfor i=1 to vals.count do unifybuttons[i].tooltip = vals[i] as string -- set tooltips for unify buttonsif vals[1] == vals[3] then ref.x = vals[1] else same[1] = falseif vals[4] == vals[6] then ref.y = vals[4] else same[2] = falseif vals[7] == vals[9] then ref.z = vals[7] else same[3] = false)if not chk_seldim.state do -- display dimensions that are identical for all selected objects(if same[1] do dim.x = ref.x if same[2] do dim.y = ref.yif same[3] do dim.z = ref.z))-- set spinner valuesspn_x.value = dim.xspn_y.value = dim.yspn_z.value = dim.z)DisplayLock())fn RefreshAll = -- store objet positions and refresh display (helper function)(if $ != undefined do StorePositions()Refresh())------------------------ scale functions ------------------------fn ScaleSelected axis size = -- main scaling function(for obj in selection do(dim = getDim obj localcoord:uselocal trim:falsetarget = copy dimcase axis of("x":(target.x = sizeif useprop do -- proportional scale(fac = size/dim.xif lockedaxis != 3 do target.y = dim.y*facif lockedaxis != 4 do target.z = dim.z*fac))"y":(target.y = sizeif useprop do -- proportional scale(fac = size/dim.yif lockedaxis != 2 do target.x = dim.x*facif lockedaxis != 4 do target.z = dim.z*fac))"z":(target.z = sizeif useprop do -- proportional scale(fac = size/dim.zif lockedaxis != 2 do target.x = dim.x*facif lockedaxis != 3 do target.y = dim.y*fac)))-- scale objectif scaledist then(if uselocal then in coordsys local about selcenter scale obj (target/dim) else about selcenter scale obj(target/dim))else(if uselocal then in coordsys local scale obj (target/dim) else scale obj(target/dim)))Refresh())fn UnifySelected axis cond = -- unify dimensions of selected objects (or get values with cond:"get")(if selection.count < 2 do return falsesizes = for s in selection collect (GetDim s localcoord:uselocal trim:false)case axis of("x": dims = for s in sizes collect s.x"y": dims = for s in sizes collect s.y"z": dims = for s in sizes collect s.z)case cond of("min": ScaleSelected axis (amin dims)"max": ScaleSelected axis (amax dims)"mid":(sum = 0for s in dims do sum+=sScaleSelected axis (sum/dims.count))))------------------------ event handlers -------------------------- section "dimensions"on spn_x changed val do ScaleSelected "x" valon spn_y changed val do ScaleSelected "y" valon spn_z changed val do ScaleSelected "z" valon chk_seldim changed state do Refresh()-- section "unify"on btn_xmin pressed do UnifySelected "x" "min"on btn_xmid pressed do UnifySelected "x" "mid"on btn_xmax pressed do UnifySelected "x" "max"on btn_ymin pressed do UnifySelected "y" "min"on btn_ymid pressed do UnifySelected "y" "mid"on btn_ymax pressed do UnifySelected "y" "max"on btn_zmin pressed do UnifySelected "z" "min"on btn_zmid pressed do UnifySelected "z" "mid"on btn_zmax pressed do UnifySelected "z" "max"on chk_tooltips changed state do Refresh()-- section "scale options"on chk_uselocal changed state do -- checkbox "local coordinate system"(uselocal = stateRefresh())on chk_scaledist changed state do -- checkbox "scale distance"(if state do StorePositions()Refresh())on chk_useprop changed state do -- checkbox "proportional scale"(useprop = stateDisplayLock())on rdo_lockedaxis changed state do -- radiobuttons "lock axis"(lockedaxis = stateDisplayLock())-- section "reset/refresh"on btn_reset pressed do -- reset scale to 100%(if chk_scaledist.state then(for i=1 to selection.count do(obj = selection[i]obj.scale = [1,1,1] -- set scale to 100%obj.pos = selobjpos[i] -- reset object position))else for obj in selection do obj.scale = [1,1,1]Refresh())on btn_refresh pressed do RefreshAll()-- main floater --on zsScaleToolsRollout open do(global zsScaleToolsRollout = zsScaleToolsRolloutRefreshAll())on zsScaleToolsRollout close do(callbacks.removeScripts id:#zsScaleToolsSelChangeglobal zsScaleToolsRollout = undefined)
)
createDialog zsScaleToolsRollout 210 280
callbacks.addScript #selectionSetChanged "zsScaleToolsRollout.RefreshAll()" id:#zsScaleToolsSelChange


http://www.mrgr.cn/news/79722.html

相关文章:

  • QT 中 多线程(备查)
  • 「Mac玩转仓颉内测版51」基础篇13 - 高阶函数与闭包
  • 【代码随想录|动态规划】
  • Qt 手写一个电脑截图工具
  • 【汽车】-- 常见的汽车悬挂系统
  • 【数据结构——内排序】希尔排序(头歌实践教学平台习题)【合集】
  • C#-WPF 常见类型转换方法(持续更新)
  • 5G Multi-TRP R16~R18演进历程
  • Linux网络基础知识————网络编程
  • rk3588-ubuntu22.04系统网关实现路由器功能:
  • 《Hadoop大数据技术应用综合训练》----以NBA冠军球队计数为例
  • C# .NET CORE 开发问题汇总
  • day10 电商系统后台API——接口测试(使用postman)
  • 【Spark】Spark Join类型及Join实现方式
  • 数据结构DAY1
  • 开发EDA工具常用的三方开源
  • 第七天:Linux中文本编辑工具和文本三剑客-grep 第八天:Linux中shell脚本编程1
  • python爬虫--小白篇【爬虫实践】
  • FPGA 17 ,FPGA 与 SR-IOV虚拟化技术,高性能计算与虚拟化技术的结合(FPGA 与 SR-IOV 和 PCI,高性能计算与虚拟化的完美融合)
  • python学opencv|读取图像(七)抓取像素数据顺利修改图像大小
  • Flutter:开发环境搭建和Android Studio创建Flutter Project
  • Harbor 仓库部署(docker-compose 部署方式)
  • coco数据集转换SAM2格式
  • 文件下载和图片预览 Blob FileReader
  • SQL汇总数据:聚集函数
  • linux系统使用nginx代理mysql数据库