原問題
https://math.pro/db/viewthread.php?tid=3830&page=1#pid25718
程式碼
# Default code, use or delete...
mod = Sketchup.active_model # Open model
ent = mod.entities # All entities in model
sel = mod.selection # Current selection
#ent.add_face [5,5,0],[5,-5,0],[-5,-5,0],[-5,5,0]
r=5*Math.sqrt(2)
group = ent.add_group
group.entities.add_arc [0,0,0],[1,1,0],[1,-1,0],r,0.degrees,180.degrees
group.name="半圓形1"
group.visible=false
group = ent.add_group
group.entities.add_arc [0,0,0],[-1,1,0],[1,1,0],r,0.degrees,180.degrees
group.name="半圓形2"
group.visible=false
face=ent.add_face [5,5,0],[-5,5,0],[-5,-5,0],[5,-5,0]
for z in 0..6
x=Math.sqrt((r**2-z**2)/2)
y=x
pt1=[x,y,z]
pt2=[-x,y,z]
pt3=[-x,-y,z]
pt4=[x,-y,z]
group = ent.add_group
group.entities.add_curve [pt1,pt2,pt3,pt4,pt1]
group.name="正方形z=#{z}"
group.visible=false
if z==6
break
end
x=Math.sqrt((r**2-(z+1)**2)/2)
y=x
pt5=[x,y,z+1]
pt6=[-x,y,z+1]
pt7=[-x,-y,z+1]
pt8=[x,-y,z+1]
#增加為四邊形面
face=ent.add_face pt1,pt5,pt8,pt4
face=ent.add_face pt1,pt2,pt6,pt5
face=ent.add_face pt2,pt3,pt7,pt6
face=ent.add_face pt3,pt4,pt8,pt7
end
pt9=[0,0,r]
face=ent.add_face pt9,pt1,pt2
face=ent.add_face pt9,pt2,pt3
face=ent.add_face pt9,pt3,pt4
face=ent.add_face pt9,pt4,pt1
#將所有連接平面的邊都隱藏起來
for edge in face.all_connected.grep(Sketchup::Edge)
edge.visible=false#將邊隱藏
end
group = ent.add_group(face.all_connected)
group.name="積分體積"
group.visible=false
#複製原本的積分體積群組後再手動刪除一半的面,選取全部的面再反轉表面
group2=group.copy
group2.name="積分體積(刪除一半的面)"
group2.visible=false
#z軸
group = ent.add_group
group.entities.add_line [0,0,0],[0,0,r]
group.name="z軸"
group.visible=false
#直角三角形
group = ent.add_group
x=Math.sqrt(41/2)
y=-x
group.entities.add_line [0,0,0],[x,y,3]
group.entities.add_line [x,y,3],[0,0,3]
group.name="直角三角形"
group.visible=false
###########新增場景,依序顯示各個群組###########
ent[ 2 ].visible=true#顯示z=0正方形
mod.pages.add "場景號1"
ent[ 0 ].visible=true#顯示半圓形1
mod.pages.add "場景號2"
ent[ 1 ].visible=true#顯示半圓形2
mod.pages.add "場景號3"
for i in 3..8
ent[ i ].visible=true#顯示正方形
pages=mod.pages.add "場景號#{i+4}"
end
#顯示積分體積
ent[9].visible=true
pages=mod.pages.add "場景號11"
#只留下z=3的正方形,其餘正方形隱藏
for i in 2..4
ent[ i ].visible=false
end
for i in 6..8
ent[ i ].visible=false
end
pages=mod.pages.add "場景號12"
#改顯示積分體積(刪除一半的面)
ent[9].visible=false
ent[10].visible=true
pages=mod.pages.add "場景號13"
#顯示z軸
ent[11].visible=true
pages=mod.pages.add "場景號14"
#顯示直角三角形
ent[12].visible=true
pages=mod.pages.add "場景號15"
SketchUp檔下載