Hi,
I am new to Ruby and I am trying to write a script to create a component that will glue to vertical faces and cut an opening. This would be the equivalent of going to the Edit tab in the Components dialog, selecting my component, and setting the "Glue to:" drop down to "Vertical" and checking the "Cut opening" box.
As far as I can tell from the documentation, what I need to do in the script is set properties on the Behavior object in the ComponentDefinition for my component. I set behavior.cuts_opening to true and behavior.snapto to SnapTo_Vertical.
When I do this, and then open the Components dialog to view my new component, the "Cut opening" box is checked but the "Glue to:" selection is "None" and, indeed, when I try to glue an instance of my component to a vertical face it does not work. Interesting, though, that if I use puts to inspect the value of the snapto property it is actually 2 (which I believe is the value of SnapTo_Vertical).
Is there something else I need to do to make these settings effective? I have attached an rb file that demonstrates this problem, and also included the script in the text below:
Ruby script example
Code below:
` require 'Sketchup'
def doComponent
colPoints = [
Geom::Point3d.new(0, 0, -2),
Geom::Point3d.new(10, 0, -2),
Geom::Point3d.new(10, 10, -2),
Geom::Point3d.new(0, 10, -2) ]
newFace = Sketchup.active_model.entities.add_face(colPoints[0], colPoints[1], colPoints[2], colPoints[3])
newFace.pushpull(8, false)
newGroupEntities = newFace.all_connected
newGroupEntities.each do |theEntity|
if theEntity.kind_of?(Sketchup::Face)
if theEntity.normal == newFace.normal or theEntity.normal == newFace.normal.reverse
theEntity.material = 0x948B4E
theEntity.material.alpha = 0.1
else
theEntity.material = 0xBDDEE1;
end
end
end
newGroup = Sketchup.active_model.entities.add_group(newGroupEntities.to_a)
#add entities for the cut plane
colPoints.each do |thePoint|
thePoint.z = 2
end
Sketchup.active_model.entities.add_line(colPoints[0], colPoints[1])
Sketchup.active_model.entities.add_line(colPoints[1], colPoints[2])
Sketchup.active_model.entities.add_line(colPoints[2], colPoints[3])
Sketchup.active_model.entities.add_line(colPoints[3], colPoints[0])
#create the component
newGroup = Sketchup.active_model.entities.add_group(Sketchup.active_model.entities.to_a)
newComponent = newGroup.to_component
newComponent.definition.name = "My Component"
newDefinition = Sketchup.active_model.definitions[newComponent.definition.name]
#transform the component's axes so the cut plane entities and the xy axes coincide
transformation = Geom::Transformation.translation(Geom::Vector3d.new(0, 0, -4))
newDefinition.entities.transform_entities(transformation, newComponent.definition.entities.to_a)
#set the component behavior to glue to ("snap to") vertical surfaces and cut an opening
newDefinition.behavior.snapto = SnapTo_Vertical
newDefinition.behavior.cuts_opening = true
puts "newDefinition.behavior.snapto=" + newDefinition.behavior.snapto.to_s
end
doComponent`