使用Sketchup Ruby API在圆柱体上打孔

使用Sketchup Ruby API在圆柱体上打孔,ruby,sketchup,Ruby,Sketchup,我正在写一个脚本,在SketchUp(免费版,Mac)中制作一个简单的长笛。我想做一个管子,然后做一个圆柱体,穿过管子,画管子和圆柱体之间的交叉线,然后擦掉圆柱体,留下圆圈从管子上切下来 如果我用鼠标来做的话,这是可行的,但是我发现很难用鼠标精确地定位和测量。不过,到目前为止,我还不能让它与脚本一起工作。目前我一直在用它在管子上画不完整的圆圈,所以我无法找到这张脸并将其抹掉。您应该能够在ruby控制台中运行以下脚本,并了解我的意思。我做错了什么 entities = Sketchup.activ

我正在写一个脚本,在SketchUp(免费版,Mac)中制作一个简单的长笛。我想做一个管子,然后做一个圆柱体,穿过管子,画管子和圆柱体之间的交叉线,然后擦掉圆柱体,留下圆圈从管子上切下来

如果我用鼠标来做的话,这是可行的,但是我发现很难用鼠标精确地定位和测量。不过,到目前为止,我还不能让它与脚本一起工作。目前我一直在用它在管子上画不完整的圆圈,所以我无法找到这张脸并将其抹掉。您应该能够在ruby控制台中运行以下脚本,并了解我的意思。我做错了什么

entities = Sketchup.active_model.entities

# make tube
tube = entities.add_group
tube_inner = tube.entities.add_circle Geom::Point3d.new(0,0,0), Geom::Vector3d.new(0,0,1), 5, 360
tube_outer = tube.entities.add_circle Geom::Point3d.new(0,0,0), Geom::Vector3d.new(0,0,1), 6, 360
cross_section_face = tube.entities.add_face tube_outer
inner_face = tube.entities.add_face tube_inner
tube.entities.erase_entities inner_face
cross_section_face.pushpull -10, false

# make a cylinder that punches through the wall
hole_punch = entities.add_group
hole_outer = hole_punch.entities.add_circle Geom::Point3d.new(0,0, 5), Geom::Vector3d.new(0,1,0), 3, 360
face = hole_punch.entities.add_face hole_outer
face.pushpull 10, false

# draw the intersection lines and erase the hole punch
entities.intersect_with true, hole_punch.transformation, tube, tube.transformation, true, hole_punch
hole_punch.erase!

确定相交后要擦除的正确面可能非常棘手

但是,由于您使用的是圆柱体形状(即实体),我建议您使用SketchUp 8 Pro中引入的实体布尔运算。例如,您可以使用
Group.subtract

但是,如果您没有使用SketchUp 8 Pro或更新版本,则这些方法将不可用


替代解决方案-避免使用Pro版本的Solid Tools方法:

entities = Sketchup.active_model.entities

# (!) You created a circle with so many edges that at the scale
#     you drew it they where pushing the boundary of how small
#     units SketchUp can handle. (1/1000th inch).
#     If you has Edge Outline style enabled you could see that
#     not all edges where fully merged.
#     I reduced the curve segments from 360 to 180.
#     (Do you really need such a high mesh density anyway?)

# make tube
tube = entities.add_group
tube_inner = tube.entities.add_circle Geom::Point3d.new(0,0,0), Geom::Vector3d.new(0,0,1), 5, 180
tube_outer = tube.entities.add_circle Geom::Point3d.new(0,0,0), Geom::Vector3d.new(0,0,1), 6, 180
cross_section_face = tube.entities.add_face tube_outer
inner_face = tube.entities.add_face tube_inner
tube.entities.erase_entities inner_face
cross_section_face.pushpull -10, false

# make a cylinder that punches through the wall
hole_punch = entities.add_group
hole_outer = hole_punch.entities.add_circle Geom::Point3d.new(0,0, 5), Geom::Vector3d.new(0,1,0), 3, 180
face = hole_punch.entities.add_face hole_outer
face.pushpull 10, false

# draw the intersection lines and erase the hole punch
entities.intersect_with true, hole_punch.transformation, tube, tube.transformation, true, hole_punch
hole_punch.erase!

# Find all the edges that belong to the Circle elements drawn
# earlier (including the ones push-pulled).
# (Could also collect these earlier before intersecting by
#  collecting all non-smooth edges.)
circles = tube.entities.grep(Sketchup::Edge).select { |e| e.curve }.uniq
# Then we pick out all the faces that isn't connected to these edges and erase them.
new_faces = tube.entities.grep(Sketchup::Face).select { |f| (f.edges & circles).empty? }
entities.erase_entities( new_faces )
如果您真的想要360段圆,您可以向上缩放组的内容,同时向下缩放组实例。这样一来,群体的定义规模就大得多了。(请参阅这篇关于SketchUp中的实例和定义的文章:)

此外,如果希望面填充内外蒙皮之间的孔,则还需要与该零件相交


请注意
实体。与
描述相交-当前文档不能很好地解释所有参数。有两个
实体
参数

第一个应该是一个
Sketchup::Entities
对象,其中应该显示相交的对象。(我有点惊讶,它居然能用
Sketchup::Group
对象来工作。)


第二个应该是而不是
Sketchup::Entities
,这将失败。它必须是一个
Sketchup:Entity
Sketchup:Entity
对象数组。

我看到pro版本中存在这些方法,但问题是我不希望使用Sketchup来证明花费500美元是合理的。哦,好吧。这对我来说并不能真正解决问题,但对其他人来说可能会,所以我会接受答案。谢谢你的建议。我添加了一个可供选择的解决方案,你可以尝试。到目前为止,我找到的
intersect\u的最佳分解是:谢谢,我会尝试一下。与此同时,我已经切换到OpenSCAD,它让我可以轻松地完成所有这些工作。